mirror of
https://github.com/navidrome/navidrome.git
synced 2026-10-08 10:27:08 +02:00
* test(plugins): build test plugins in Go instead of shelling out to make The plugins suite built its .ndp test packages by running `make -C plugins/testdata`, which needs make and zip on the PATH. That is the reason the 26 WASM-dependent spec files are tagged //go:build !windows. buildTestPlugins now does the same work in Go: the same mtime check make performed, `GOOS=wasip1 GOARCH=wasm go build` per plugin, and archive/zip for the package. TinyGo was already optional and unused in CI, so nothing is lost there. The first plugin builds on its own so the shared wasip1 stdlib and PDK objects land in the build cache before the rest fan out: on a cold cache that is 2.2s against 3.4s for the sequential make and 7.3s for an unrestrained fan-out. Packaging moved into a writeNdp helper shared with createTestPackage, which was already writing the same two-entry archive. Entries are written in a fixed order, so the .ndp bytes are now reproducible; the loader hashes those bytes, and `zip` also stored file mtimes, so the previous packages differed on every rebuild. The Makefile is unchanged and still works for building the plugins by hand. Removing the !windows tags is a separate step, once CI is green here. * test(plugins): run the WASM plugin specs on Windows With the test plugins now built in Go, nothing in the suite needs a Unix toolchain, so the //go:build !windows tags come off all 25 spec files. The Windows CI job runs `go test ./...`, so it picks the suite up with no workflow change. plugins_suite_windows_test.go existed only to bootstrap the handful of specs that compiled on Windows; plugins_suite_test.go now serves both. * test(plugins): skip the planted-symlink spec where symlinks need privileges os.Symlink needs an elevated token or Developer Mode on Windows, so the unconditional Expect(...).To(Succeed()) would fail for contributors running the suite on an ordinary Windows box. The elevated GitHub runner hides this. The equivalent spec in sandbox_fs_internal_test.go already attempts the symlink and skips on error; this does the same, keeping the pin live everywhere it can run, including Windows CI. * fix(ci): stop the Windows ndpgen test failing silently The ndpgen suite builds its helper binary to %TEMP%\ndpgen-test, and Windows will not exec a file without an executable extension, so the "supports verbose mode" spec has been failing there. Nobody noticed because the Test ndpgen step ran under pwsh, which carries on after a non-zero exit and takes the step's status from the last command, so the job stayed green with a FAIL line in its log. Add the .exe suffix, and run the step under bash like the Linux job does, so a failure in any of its three commands fails the job.
385 lines
12 KiB
Go
385 lines
12 KiB
Go
package plugins
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/navidrome/navidrome/core/agents"
|
|
"github.com/navidrome/navidrome/core/scrobbler"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/model/request"
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
// ctxWithUser returns a fresh context with the test user.
|
|
// Must be called within each test, not in BeforeAll, because the context
|
|
// from BeforeAll gets cancelled before tests run.
|
|
func ctxWithUser() context.Context {
|
|
return request.WithUser(GinkgoT().Context(), model.User{ID: "user-1", UserName: "testuser"})
|
|
}
|
|
|
|
var _ = Describe("ScrobblerPlugin", Ordered, func() {
|
|
var (
|
|
scrobblerManager *Manager
|
|
s scrobbler.Scrobbler
|
|
)
|
|
|
|
BeforeAll(func() {
|
|
// Load the scrobbler via a new manager with the test-scrobbler plugin
|
|
scrobblerManager, _ = createTestManagerWithPlugins(nil, "test-scrobbler"+PackageExtension)
|
|
|
|
var ok bool
|
|
s, ok = scrobblerManager.LoadScrobbler("test-scrobbler")
|
|
Expect(ok).To(BeTrue())
|
|
})
|
|
|
|
Describe("LoadScrobbler", func() {
|
|
It("returns a scrobbler for a plugin with Scrobbler capability", func() {
|
|
Expect(s).ToNot(BeNil())
|
|
})
|
|
|
|
It("returns false for a plugin without Scrobbler capability", func() {
|
|
_, ok := testManager.LoadScrobbler("test-metadata-agent")
|
|
Expect(ok).To(BeFalse())
|
|
})
|
|
|
|
It("returns false for non-existent plugin", func() {
|
|
_, ok := scrobblerManager.LoadScrobbler("non-existent")
|
|
Expect(ok).To(BeFalse())
|
|
})
|
|
})
|
|
|
|
Describe("IsAuthorized", func() {
|
|
It("returns true when plugin is configured to authorize", func() {
|
|
result := s.IsAuthorized(ctxWithUser(), "user-1")
|
|
Expect(result).To(BeTrue())
|
|
})
|
|
|
|
Context("when plugin is configured to not authorize", Ordered, func() {
|
|
var notAuthScrobbler scrobbler.Scrobbler
|
|
|
|
BeforeAll(func() {
|
|
mgr, _ := createTestManagerWithPlugins(map[string]map[string]string{
|
|
"test-scrobbler": {"authorized": "false"},
|
|
}, "test-scrobbler"+PackageExtension)
|
|
|
|
var ok bool
|
|
notAuthScrobbler, ok = mgr.LoadScrobbler("test-scrobbler")
|
|
Expect(ok).To(BeTrue())
|
|
})
|
|
|
|
It("returns false", func() {
|
|
result := notAuthScrobbler.IsAuthorized(ctxWithUser(), "user-1")
|
|
Expect(result).To(BeFalse())
|
|
})
|
|
})
|
|
})
|
|
|
|
Describe("isUserAllowed", func() {
|
|
It("returns true when allUsers is true", func() {
|
|
sp := &ScrobblerPlugin{allUsers: true}
|
|
Expect(sp.isUserAllowed("any-user")).To(BeTrue())
|
|
})
|
|
|
|
It("returns false when allowedUserIDs is empty and allUsers is false", func() {
|
|
sp := &ScrobblerPlugin{allUsers: false, allowedUserIDs: []string{}}
|
|
Expect(sp.isUserAllowed("user-1")).To(BeFalse())
|
|
})
|
|
|
|
It("returns false when allowedUserIDs is nil and allUsers is false", func() {
|
|
sp := &ScrobblerPlugin{allUsers: false}
|
|
Expect(sp.isUserAllowed("user-1")).To(BeFalse())
|
|
})
|
|
|
|
It("returns true when user is in allowedUserIDs", func() {
|
|
sp := &ScrobblerPlugin{
|
|
allUsers: false,
|
|
allowedUserIDs: []string{"user-1", "user-2"},
|
|
userIDMap: map[string]struct{}{"user-1": {}, "user-2": {}},
|
|
}
|
|
Expect(sp.isUserAllowed("user-1")).To(BeTrue())
|
|
})
|
|
|
|
It("returns false when user is not in allowedUserIDs", func() {
|
|
sp := &ScrobblerPlugin{
|
|
allUsers: false,
|
|
allowedUserIDs: []string{"user-1", "user-2"},
|
|
userIDMap: map[string]struct{}{"user-1": {}, "user-2": {}},
|
|
}
|
|
Expect(sp.isUserAllowed("user-3")).To(BeFalse())
|
|
})
|
|
})
|
|
|
|
Describe("NowPlaying", func() {
|
|
It("successfully calls the plugin", func() {
|
|
track := &model.MediaFile{
|
|
ID: "track-1",
|
|
Title: "Test Song",
|
|
Album: "Test Album",
|
|
Artist: "Test Artist",
|
|
AlbumArtist: "Test Album Artist",
|
|
Duration: 180,
|
|
TrackNumber: 1,
|
|
DiscNumber: 1,
|
|
Participants: model.Participants{
|
|
model.RoleArtist: {{Artist: model.Artist{ID: "artist-1", Name: "Test Artist"}}},
|
|
model.RoleAlbumArtist: {{Artist: model.Artist{ID: "album-artist-1", Name: "Test Album Artist"}}},
|
|
},
|
|
}
|
|
|
|
err := s.NowPlaying(ctxWithUser(), "user-1", track, 30)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
})
|
|
|
|
Context("when plugin returns error", Ordered, func() {
|
|
var retryScrobbler scrobbler.Scrobbler
|
|
|
|
BeforeAll(func() {
|
|
mgr, _ := createTestManagerWithPlugins(map[string]map[string]string{
|
|
"test-scrobbler": {"error": "service unavailable", "error_type": "scrobbler(retry_later)"},
|
|
}, "test-scrobbler"+PackageExtension)
|
|
|
|
var ok bool
|
|
retryScrobbler, ok = mgr.LoadScrobbler("test-scrobbler")
|
|
Expect(ok).To(BeTrue())
|
|
})
|
|
|
|
It("returns ErrRetryLater", func() {
|
|
track := &model.MediaFile{ID: "track-1", Title: "Test Song"}
|
|
err := retryScrobbler.NowPlaying(ctxWithUser(), "user-1", track, 30)
|
|
Expect(err).To(HaveOccurred())
|
|
Expect(err).To(MatchError(scrobbler.ErrRetryLater))
|
|
})
|
|
})
|
|
})
|
|
|
|
Describe("Scrobble", func() {
|
|
It("successfully calls the plugin", func() {
|
|
sc := scrobbler.Scrobble{
|
|
MediaFile: model.MediaFile{
|
|
ID: "track-1",
|
|
Title: "Test Song",
|
|
Album: "Test Album",
|
|
Artist: "Test Artist",
|
|
AlbumArtist: "Test Album Artist",
|
|
Duration: 180,
|
|
TrackNumber: 1,
|
|
DiscNumber: 1,
|
|
Participants: model.Participants{
|
|
model.RoleArtist: {{Artist: model.Artist{ID: "artist-1", Name: "Test Artist"}}},
|
|
model.RoleAlbumArtist: {{Artist: model.Artist{ID: "album-artist-1", Name: "Test Album Artist"}}},
|
|
},
|
|
},
|
|
TimeStamp: time.Now(),
|
|
}
|
|
|
|
err := s.Scrobble(ctxWithUser(), "user-1", sc)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
})
|
|
|
|
Context("when plugin returns not_authorized error", Ordered, func() {
|
|
var notAuthScrobbler scrobbler.Scrobbler
|
|
|
|
BeforeAll(func() {
|
|
mgr, _ := createTestManagerWithPlugins(map[string]map[string]string{
|
|
"test-scrobbler": {"error": "user not linked", "error_type": "scrobbler(not_authorized)"},
|
|
}, "test-scrobbler"+PackageExtension)
|
|
|
|
var ok bool
|
|
notAuthScrobbler, ok = mgr.LoadScrobbler("test-scrobbler")
|
|
Expect(ok).To(BeTrue())
|
|
})
|
|
|
|
It("returns ErrNotAuthorized", func() {
|
|
scrobble := scrobbler.Scrobble{
|
|
MediaFile: model.MediaFile{ID: "track-1", Title: "Test Song"},
|
|
TimeStamp: time.Now(),
|
|
}
|
|
err := notAuthScrobbler.Scrobble(ctxWithUser(), "user-1", scrobble)
|
|
Expect(err).To(HaveOccurred())
|
|
Expect(err).To(MatchError(scrobbler.ErrNotAuthorized))
|
|
})
|
|
})
|
|
|
|
Context("when plugin returns unrecoverable error", Ordered, func() {
|
|
var unrecoverableScrobbler scrobbler.Scrobbler
|
|
|
|
BeforeAll(func() {
|
|
mgr, _ := createTestManagerWithPlugins(map[string]map[string]string{
|
|
"test-scrobbler": {"error": "track rejected", "error_type": "scrobbler(unrecoverable)"},
|
|
}, "test-scrobbler"+PackageExtension)
|
|
|
|
var ok bool
|
|
unrecoverableScrobbler, ok = mgr.LoadScrobbler("test-scrobbler")
|
|
Expect(ok).To(BeTrue())
|
|
})
|
|
|
|
It("returns ErrUnrecoverable", func() {
|
|
scrobble := scrobbler.Scrobble{
|
|
MediaFile: model.MediaFile{ID: "track-1", Title: "Test Song"},
|
|
TimeStamp: time.Now(),
|
|
}
|
|
err := unrecoverableScrobbler.Scrobble(ctxWithUser(), "user-1", scrobble)
|
|
Expect(err).To(HaveOccurred())
|
|
Expect(err).To(MatchError(scrobbler.ErrUnrecoverable))
|
|
})
|
|
})
|
|
})
|
|
|
|
Describe("PlaybackReport", func() {
|
|
It("successfully calls the plugin", func() {
|
|
info := scrobbler.PlaybackSession{
|
|
MediaFile: model.MediaFile{
|
|
ID: "track-1",
|
|
Title: "Test Song",
|
|
Album: "Test Album",
|
|
Artist: "Test Artist",
|
|
AlbumArtist: "Test Album Artist",
|
|
Duration: 180,
|
|
TrackNumber: 1,
|
|
DiscNumber: 1,
|
|
Participants: model.Participants{
|
|
model.RoleArtist: {{Artist: model.Artist{ID: "artist-1", Name: "Test Artist"}}},
|
|
model.RoleAlbumArtist: {{Artist: model.Artist{ID: "album-artist-1", Name: "Test Album Artist"}}},
|
|
},
|
|
},
|
|
Username: "testuser",
|
|
PlayerId: "player-1",
|
|
PlayerName: "Test Player",
|
|
State: "playing",
|
|
PositionMs: 30000,
|
|
PlaybackRate: 1.0,
|
|
LastReport: time.Now(),
|
|
}
|
|
|
|
err := s.PlaybackReport(ctxWithUser(), info)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
})
|
|
|
|
Context("when plugin returns error", Ordered, func() {
|
|
var retryScrobbler scrobbler.Scrobbler
|
|
|
|
BeforeAll(func() {
|
|
mgr, _ := createTestManagerWithPlugins(map[string]map[string]string{
|
|
"test-scrobbler": {"error": "service unavailable", "error_type": "scrobbler(retry_later)"},
|
|
}, "test-scrobbler"+PackageExtension)
|
|
|
|
var ok bool
|
|
retryScrobbler, ok = mgr.LoadScrobbler("test-scrobbler")
|
|
Expect(ok).To(BeTrue())
|
|
})
|
|
|
|
It("returns ErrRetryLater", func() {
|
|
info := scrobbler.PlaybackSession{
|
|
MediaFile: model.MediaFile{ID: "track-1", Title: "Test Song"},
|
|
State: "playing",
|
|
LastReport: time.Now(),
|
|
}
|
|
err := retryScrobbler.PlaybackReport(ctxWithUser(), info)
|
|
Expect(err).To(HaveOccurred())
|
|
Expect(err).To(MatchError(scrobbler.ErrRetryLater))
|
|
})
|
|
})
|
|
})
|
|
|
|
Describe("PluginNames", func() {
|
|
It("returns plugin names with Scrobbler capability", func() {
|
|
names := scrobblerManager.PluginNames("Scrobbler")
|
|
Expect(names).To(ContainElement("test-scrobbler"))
|
|
})
|
|
|
|
It("does not return metadata agent plugins for Scrobbler capability", func() {
|
|
names := testManager.PluginNames("Scrobbler")
|
|
Expect(names).ToNot(ContainElement("test-metadata-agent"))
|
|
})
|
|
})
|
|
|
|
Describe("mediaFileToTrackInfo", func() {
|
|
var track *model.MediaFile
|
|
|
|
BeforeEach(func() {
|
|
track = &model.MediaFile{
|
|
ID: "track-1",
|
|
Title: "Test Song",
|
|
Path: "/music/test.flac",
|
|
LibraryID: 1,
|
|
}
|
|
})
|
|
|
|
fsManifest := &Manifest{
|
|
Permissions: &Permissions{
|
|
Library: &LibraryPermission{Filesystem: true},
|
|
},
|
|
}
|
|
|
|
It("includes LibraryID and Path when the plugin has filesystem access to the track's library", func() {
|
|
p := &plugin{manifest: fsManifest, libraries: newLibraryAccess([]int{1}, false)}
|
|
ti := mediaFileToTrackInfo(p, track)
|
|
Expect(ti.LibraryID).To(Equal(int32(1)))
|
|
Expect(ti.Path).To(Equal("/music/test.flac"))
|
|
})
|
|
|
|
It("omits LibraryID and Path when the plugin lacks filesystem permission", func() {
|
|
p := &plugin{manifest: &Manifest{}, libraries: newLibraryAccess([]int{1}, false)}
|
|
ti := mediaFileToTrackInfo(p, track)
|
|
Expect(ti.LibraryID).To(BeZero())
|
|
Expect(ti.Path).To(BeEmpty())
|
|
})
|
|
|
|
It("omits LibraryID and Path when the track's library is not in the allowed set", func() {
|
|
p := &plugin{manifest: fsManifest, libraries: newLibraryAccess([]int{2}, false)}
|
|
ti := mediaFileToTrackInfo(p, track)
|
|
Expect(ti.LibraryID).To(BeZero())
|
|
Expect(ti.Path).To(BeEmpty())
|
|
})
|
|
})
|
|
})
|
|
|
|
var _ = Describe("mapScrobblerError", func() {
|
|
It("returns nil for nil error", func() {
|
|
Expect(mapScrobblerError(nil)).ToNot(HaveOccurred())
|
|
})
|
|
|
|
It("returns ErrNotAuthorized for error containing 'not_authorized'", func() {
|
|
err := mapScrobblerError(errors.New("plugin error: scrobbler(not_authorized)"))
|
|
Expect(err).To(MatchError(scrobbler.ErrNotAuthorized))
|
|
})
|
|
|
|
It("returns ErrRetryLater for error containing 'retry_later'", func() {
|
|
err := mapScrobblerError(errors.New("temporary failure: scrobbler(retry_later)"))
|
|
Expect(err).To(MatchError(scrobbler.ErrRetryLater))
|
|
})
|
|
|
|
It("returns ErrUnrecoverable for error containing 'unrecoverable'", func() {
|
|
err := mapScrobblerError(errors.New("fatal error: scrobbler(unrecoverable)"))
|
|
Expect(err).To(MatchError(scrobbler.ErrUnrecoverable))
|
|
})
|
|
|
|
It("returns ErrUnrecoverable for unknown error", func() {
|
|
err := mapScrobblerError(errors.New("some unknown error"))
|
|
Expect(err).To(MatchError(scrobbler.ErrUnrecoverable))
|
|
})
|
|
|
|
DescribeTable("mapScrobblerError retry-later",
|
|
func(msg string, wantDelay time.Duration) {
|
|
err := mapScrobblerError(errors.New(msg))
|
|
Expect(errors.Is(err, scrobbler.ErrRetryLater)).To(BeTrue())
|
|
retry, _ := errors.AsType[*agents.RetryLaterError](err)
|
|
d := retry.RetryIn
|
|
Expect(d).To(Equal(wantDelay))
|
|
},
|
|
Entry("bare token", "scrobbler(retry_later)", time.Duration(0)),
|
|
Entry("with seconds", "scrobbler(retry_later:30)", 30*time.Second),
|
|
Entry("capped at 1h", "scrobbler(retry_later:999999)", time.Hour),
|
|
// Scaling to nanoseconds before capping wraps past 2^64, landing on ~0.29s.
|
|
Entry("capped before it can overflow", "scrobbler(retry_later:18446744074)", time.Hour),
|
|
Entry("wrapped in context", "plugin xyz: scrobbler(retry_later:5)", 5*time.Second),
|
|
)
|
|
|
|
It("still maps unknown errors to unrecoverable", func() {
|
|
err := mapScrobblerError(errors.New("scrobbler(retry_later_garbage"))
|
|
Expect(errors.Is(err, scrobbler.ErrUnrecoverable)).To(BeTrue())
|
|
})
|
|
})
|