navidrome/persistence/e2e/e2e_suite_test.go
Deluan Quintão b293b96256
refactor(persistence): stateless repositories with per-call context (#6149)
* refactor(persistence): adopt generic deluan/rest repository API

Pin deluan/rest to the refactor branch. REST-facing repository methods
take a context and return typed values. Drop DataStore.Resource and
ResourceRepository; the native API names typed repositories directly
through a per-request adapter that later commits remove.

* refactor(persistence): base repository helpers take a context

* refactor(persistence): LibraryRepository takes a context per call

* refactor(persistence): PropertyRepository takes a context per call

* refactor(persistence): UserPropsRepository takes a context per call

* refactor(persistence): TranscodingRepository takes a context per call

* refactor(persistence): ShareRepository takes a context per call

* refactor(persistence): PlayerRepository takes a context per call

* refactor(persistence): RadioRepository takes a context per call

* refactor(persistence): PlayQueueRepository takes a context per call

* refactor(persistence): Tag and Genre repositories take a context per call

* refactor(persistence): PluginRepository takes a context per call

* refactor(persistence): Scrobble repositories take a context per call

* refactor(persistence): FolderRepository takes a context per call

* refactor(persistence): Artwork repositories take a context per call

* refactor(persistence): UserRepository takes a context per call

* refactor(persistence): ArtistRepository takes a context per call

ReadAll no longer rewrites the shared sort mappings for the role filter;
it works on a per-call copy.

* test(persistence): assert artist role sort sanitization in ReadAll

* refactor(persistence): AlbumRepository takes a context per call

* test(persistence): pass the test context to album repository helpers

* refactor(persistence): MediaFileRepository takes a context per call

* refactor(persistence): Playlist repositories take a context per call

* refactor(persistence): build all repositories once per store

* refactor(core): REST repository wrappers are built once

* refactor(persistence): repositories are stateless

Remove the context field from the base repository and the per-request
REST adapter. Enable the containedctx linter so no repository can hold a
request context again.

* chore(lint): skip containedctx in test files

* refactor: share simplifications from the stateless repositories sweep

Add deleteOwnedAll on sqlRepository and use it in player/share Delete
to remove the duplicated bulk-delete loop; have Share.Repository()
return model.ShareRepository so subsonic sharing.go drops its repeated
type assertions.

* chore(core): assert REST wrappers implement Persistable

* chore: reformat imports

* perf(persistence): build repositories on first use

Each transaction store used to construct all 21 repositories up front,
paying for filter and sort mapping setup the block never touched. Fields
are now sync.OnceValue thunks, so a store only builds what it uses.

* fix(persistence): clean plugin references per deleted user

A bulk user delete that fails on a later id had already removed the
earlier rows but skipped their plugin cleanup. Cleanup now runs right
after each successful delete.

* fix(core): unload disabled plugins even when a user delete fails

A bulk delete can fail on a later id after earlier users were removed
and their plugins auto-disabled. The wrapper returned before unloading,
leaving those plugins running until the next successful delete or a
restart.

* chore(deps): pin deluan/rest to v1.0.1

Replaces the pseudo-version of the refactor branch with the tagged
release. REST error messages now name the bare type (Artist, not
model.Artist).

* test: use the spec context instead of context.Background()

Replace the context.Background()/context.TODO() calls this branch added
to tests with the spec's ctx, GinkgoT().Context(), or t/b.Context(), so
repository calls are bound to the running spec's lifetime.

* test: declare the spec context once per Describe

Set ctx from GinkgoT().Context() first in each top-level BeforeEach and reuse it, building user contexts on top of it instead of repeating inline calls.
2026-09-25 18:06:10 -04:00

350 lines
11 KiB
Go

package e2e
import (
"context"
"encoding/json"
"os"
"path/filepath"
"sort"
"testing"
"testing/fstest"
"time"
"github.com/Masterminds/squirrel"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/conf/configtest"
"github.com/navidrome/navidrome/core/artwork"
"github.com/navidrome/navidrome/core/metrics"
"github.com/navidrome/navidrome/core/playlists"
"github.com/navidrome/navidrome/core/storage/storagetest"
"github.com/navidrome/navidrome/db"
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/model/criteria"
"github.com/navidrome/navidrome/model/request"
"github.com/navidrome/navidrome/persistence"
"github.com/navidrome/navidrome/scanner"
"github.com/navidrome/navidrome/server/events"
"github.com/navidrome/navidrome/tests"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func TestSmartPlaylistE2E(t *testing.T) {
tests.Init(t, false)
defer db.Close(t.Context())
log.SetLevel(log.LevelFatal)
RegisterFailHandler(Fail)
RunSpecs(t, "Smart Playlist E2E Suite")
}
type _t = map[string]any
var template = storagetest.Template
var track = storagetest.Track
var (
ctx context.Context
ds *tests.MockDataStore
lib model.Library
dbFilePath string
snapshotPath string
snapshotTables []string
adminUser = model.User{
ID: "sp-test-user-1",
UserName: "sptestuser",
Name: "SP Test User",
IsAdmin: true,
}
regularUser = model.User{
ID: "sp-test-user-2",
UserName: "spotheruser",
Name: "SP Other User",
IsAdmin: false,
}
)
func buildTestFS() {
abbeyRoad := template(_t{
"albumartist": "The Beatles",
"artist": "The Beatles",
"album": "Abbey Road",
"year": 1969,
"genre": "Rock;Blues",
"replaygain_album_gain": "-6.5 dB",
"replaygain_album_peak": "0.98",
})
ledZepIV := template(_t{
"albumartist": "Led Zeppelin",
"artist": "Led Zeppelin",
"album": "IV",
"year": 1971,
})
kindOfBlue := template(_t{
"albumartist": "Miles Davis",
"artist": "Miles Davis",
"album": "Kind of Blue",
"year": 1959,
"genre": "Jazz",
"composer": "Miles Davis",
})
nightAtOpera := template(_t{
"albumartist": "Queen",
"artist": "Queen",
"album": "A Night at the Opera",
"year": 1975,
"genre": "Rock",
})
electricLadyland := template(_t{
"albumartist": "Jimi Hendrix",
"artist": "Jimi Hendrix",
"album": "Electric Ladyland",
"year": 1968,
"genre": "Rock;Blues",
})
newsOfWorld := template(_t{
"albumartist": "Queen",
"artist": "Queen",
"album": "News of the World",
"year": 1977,
"genre": "Rock;Pop",
"compilation": "1",
})
fs := storagetest.FakeFS{}
fs.SetFiles(fstest.MapFS{
"Rock/The Beatles/Abbey Road/01 - Come Together.mp3": abbeyRoad(track(1, "Come Together",
_t{"genre": "Rock;Blues", "composer": "Lennon/McCartney", "bpm": 120, "grouping": "Beatles Tracks",
"replaygain_track_gain": "-7.1 dB", "replaygain_track_peak": "0.95"})),
"Rock/The Beatles/Abbey Road/02 - Something.mp3": abbeyRoad(track(2, "Something",
_t{"genre": "Rock", "composer": "Harrison", "bpm": 100, "grouping": "Beatles Tracks",
"replaygain_track_gain": "-6.0 dB", "replaygain_track_peak": "0.92"})),
// Stairway To Heaven has track gain but no album gain, to distinguish the two fields
"Rock/Led Zeppelin/IV/01 - Stairway To Heaven.flac": ledZepIV(track(1, "Stairway To Heaven",
_t{"genre": "Rock;Folk", "composer": "Page/Plant", "bpm": 82, "suffix": "flac",
"bitrate": 900, "samplerate": 44100, "bitdepth": 16,
"replaygain_track_gain": "-8.25 dB", "replaygain_track_peak": "0.99"})),
"Rock/Led Zeppelin/IV/02 - Black Dog.flac": ledZepIV(track(2, "Black Dog",
_t{"genre": "Rock;Blues", "composer": "Page/Plant/Jones", "bpm": 150, "suffix": "flac",
"bitrate": 900, "samplerate": 44100, "bitdepth": 16})),
"Jazz/Miles Davis/Kind of Blue/01 - So What.mp3": kindOfBlue(track(1, "So What",
_t{"bpm": 136})),
"Rock/Queen/A Night at the Opera/01 - Bohemian Rhapsody.mp3": nightAtOpera(track(1, "Bohemian Rhapsody",
_t{"composer": "Freddie Mercury", "bpm": 72})),
"Rock/Jimi Hendrix/Electric Ladyland/01 - All Along the Watchtower.mp3": electricLadyland(track(1, "All Along the Watchtower",
_t{"composer": "Bob Dylan", "bpm": 112})),
"Rock/Queen/News of the World/01 - We Are the Champions.mp3": newsOfWorld(track(1, "We Are the Champions",
_t{"composer": "Freddie Mercury", "bpm": 64})),
})
storagetest.Register("fake", &fs)
}
func findMediaFileByTitle(title string) string {
mfs, err := ds.MediaFile().GetAll(ctx, model.QueryOptions{
Filters: squirrel.Eq{"media_file.title": title},
})
Expect(err).ToNot(HaveOccurred())
Expect(mfs).To(HaveLen(1), "expected exactly one media file with title %q", title)
return mfs[0].ID
}
func evaluateRule(jsonRule string) []string {
titles := evaluateRuleOrderedAs(adminUser, jsonRule)
sort.Strings(titles)
return titles
}
func evaluateRuleOrdered(jsonRule string) []string {
return evaluateRuleOrderedAs(adminUser, jsonRule)
}
func evaluateRuleAs(owner model.User, jsonRule string) []string {
titles := evaluateRuleOrderedAs(owner, jsonRule)
sort.Strings(titles)
return titles
}
func evaluateRuleOrderedAs(owner model.User, jsonRule string) []string {
userCtx := request.WithUser(GinkgoT().Context(), owner)
var rules criteria.Criteria
err := json.Unmarshal([]byte(jsonRule), &rules)
Expect(err).ToNot(HaveOccurred(), "invalid criteria JSON: %s", jsonRule)
pls := &model.Playlist{
Name: "test-smart-playlist",
OwnerID: owner.ID,
Rules: &rules,
}
err = ds.Playlist().Put(userCtx, pls)
Expect(err).ToNot(HaveOccurred())
loaded, err := ds.Playlist().GetWithTracks(userCtx, pls.ID, true, false)
Expect(err).ToNot(HaveOccurred())
titles := make([]string, len(loaded.Tracks))
for i, t := range loaded.Tracks {
titles[i] = t.Title
}
return titles
}
func createPlaylist(owner model.User, public bool, titles ...string) string {
pls := &model.Playlist{
Name: "ref-playlist",
OwnerID: owner.ID,
Public: public,
}
for _, title := range titles {
mfID := findMediaFileByTitle(title)
pls.AddMediaFilesByID([]string{mfID})
}
Expect(ds.Playlist().Put(ctx, pls)).To(Succeed())
return pls.ID
}
func createPublicPlaylist(owner model.User, titles ...string) string {
return createPlaylist(owner, true, titles...)
}
func createPrivatePlaylist(owner model.User, titles ...string) string {
return createPlaylist(owner, false, titles...)
}
func createPublicSmartPlaylist(owner model.User, jsonRule string) string {
return createSmartPlaylist(owner, true, jsonRule)
}
func createPrivateSmartPlaylist(owner model.User, jsonRule string) string {
return createSmartPlaylist(owner, false, jsonRule)
}
func createSmartPlaylist(owner model.User, public bool, jsonRule string) string {
var rules criteria.Criteria
Expect(json.Unmarshal([]byte(jsonRule), &rules)).To(Succeed())
pls := &model.Playlist{
Name: "ref-smart-playlist",
OwnerID: owner.ID,
Public: public,
Rules: &rules,
}
Expect(ds.Playlist().Put(ctx, pls)).To(Succeed())
return pls.ID
}
var _ = BeforeSuite(func() {
ctx = request.WithUser(GinkgoT().Context(), adminUser)
tmpDir := GinkgoT().TempDir()
dbFilePath = filepath.Join(tmpDir, "smartplaylist-e2e.db")
snapshotPath = filepath.Join(tmpDir, "smartplaylist-e2e.db.snapshot")
conf.Server.DbPath = dbFilePath + "?_journal_mode=WAL"
db.Db().SetMaxOpenConns(1)
conf.Server.MusicFolder = "fake:///music"
conf.Server.DevExternalScanner = false
conf.Server.SmartPlaylistRefreshDelay = 0
db.Init(ctx)
initDS := &tests.MockDataStore{RealDS: persistence.New(db.Db())}
userWithPass := adminUser
userWithPass.NewPassword = "password"
Expect(initDS.User().Put(ctx, &userWithPass)).To(Succeed())
regularUserWithPass := regularUser
regularUserWithPass.NewPassword = "password"
Expect(initDS.User().Put(ctx, &regularUserWithPass)).To(Succeed())
lib = model.Library{ID: 1, Name: "Music Library", Path: "fake:///music"}
Expect(initDS.Library().Put(ctx, &lib)).To(Succeed())
Expect(initDS.User().SetUserLibraries(ctx, adminUser.ID, []int{lib.ID})).To(Succeed())
Expect(initDS.User().SetUserLibraries(ctx, regularUser.ID, []int{lib.ID})).To(Succeed())
loadedUser, err := initDS.User().FindByUsername(ctx, adminUser.UserName)
Expect(err).ToNot(HaveOccurred())
adminUser.Libraries = loadedUser.Libraries
loadedOther, err := initDS.User().FindByUsername(ctx, regularUser.UserName)
Expect(err).ToNot(HaveOccurred())
regularUser.Libraries = loadedOther.Libraries
ctx = request.WithUser(GinkgoT().Context(), adminUser)
buildTestFS()
s := scanner.New(ctx, initDS, events.NoopBroker(),
playlists.NewPlaylists(initDS, artwork.NewUploader(initDS)), metrics.NewNoopInstance())
_, err = s.ScanAll(ctx, true)
Expect(err).ToNot(HaveOccurred())
ds = &tests.MockDataStore{RealDS: persistence.New(db.Db())}
comeTogetherID := findMediaFileByTitle("Come Together")
Expect(ds.MediaFile().SetStar(ctx, true, comeTogetherID)).To(Succeed())
Expect(ds.MediaFile().SetStar(ctx, true, findMediaFileByTitle("So What"))).To(Succeed())
Expect(ds.MediaFile().SetRating(ctx, 3, findMediaFileByTitle("Stairway To Heaven"))).To(Succeed())
Expect(ds.MediaFile().SetRating(ctx, 5, findMediaFileByTitle("Bohemian Rhapsody"))).To(Succeed())
for range 10 {
Expect(ds.MediaFile().IncPlayCount(ctx, comeTogetherID, time.Now())).To(Succeed())
}
Expect(ds.MediaFile().IncPlayCount(ctx, findMediaFileByTitle("Black Dog"), time.Now())).To(Succeed())
rows, err := db.Db().Query("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' AND name NOT LIKE '%_fts' AND name NOT LIKE '%_fts_%'")
Expect(err).ToNot(HaveOccurred())
defer rows.Close()
for rows.Next() {
var name string
Expect(rows.Scan(&name)).To(Succeed())
snapshotTables = append(snapshotTables, name)
}
Expect(rows.Err()).ToNot(HaveOccurred())
_, err = db.Db().Exec("PRAGMA wal_checkpoint(TRUNCATE)")
Expect(err).ToNot(HaveOccurred())
data, err := os.ReadFile(dbFilePath)
Expect(err).ToNot(HaveOccurred())
Expect(os.WriteFile(snapshotPath, data, 0600)).To(Succeed())
})
var _ = AfterSuite(func() {
db.Close(ctx)
})
func restoreDB() {
sqlDB := db.Db()
_, err := sqlDB.Exec("PRAGMA foreign_keys = OFF")
Expect(err).ToNot(HaveOccurred())
defer func() { _, _ = sqlDB.Exec("PRAGMA foreign_keys = ON") }()
_, err = sqlDB.Exec("ATTACH DATABASE ? AS snapshot", snapshotPath)
Expect(err).ToNot(HaveOccurred())
defer func() { _, _ = sqlDB.Exec("DETACH DATABASE snapshot") }()
_, err = sqlDB.Exec("BEGIN TRANSACTION")
Expect(err).ToNot(HaveOccurred())
defer func() { _, _ = sqlDB.Exec("ROLLBACK") }()
for _, table := range snapshotTables {
_, err = sqlDB.Exec(`DELETE FROM main."` + table + `"`) //nolint:gosec
Expect(err).ToNot(HaveOccurred())
_, err = sqlDB.Exec(`INSERT INTO main."` + table + `" SELECT * FROM snapshot."` + table + `"`) //nolint:gosec
Expect(err).ToNot(HaveOccurred())
}
_, err = sqlDB.Exec("COMMIT")
Expect(err).ToNot(HaveOccurred())
}
func setupTestDB() {
ctx = request.WithUser(GinkgoT().Context(), adminUser)
DeferCleanup(configtest.SetupConfig())
conf.Server.MusicFolder = "fake:///music"
conf.Server.DevExternalScanner = false
conf.Server.SmartPlaylistRefreshDelay = 0
restoreDB()
ds = &tests.MockDataStore{RealDS: persistence.New(db.Db())}
}