package storage import ( "context" "errors" "slices" "testing" "time" "github.com/uptrace/bun" "git.kmalloc.org/squid/sonda/internal/smart" ) // smartEnv is two users and a library of songs that differ in what the rules ask about. type smartEnv struct { t *testing.T db *bun.DB repo *SmartRepo alice int64 bob int64 tracks map[string]int64 artist map[string]int64 album map[string]int64 now time.Time } type smartSong struct { title, artist, album, format string genres []string bitrate int mtime int64 seconds int64 } func newSmartEnv(t *testing.T, db *bun.DB, songs []smartSong) *smartEnv { t.Helper() ctx := context.Background() users := NewUserRepo(db) a, err := users.CreateUser(ctx, "alice@example.org", "hash", false) if err != nil { t.Fatal(err) } b, err := users.CreateUser(ctx, "bob@example.org", "hash", false) if err != nil { t.Fatal(err) } lib := NewLibraryRepo(db) l, err := lib.CreateLibrary(ctx, "m", "/lib/m") if err != nil { t.Fatal(err) } e := &smartEnv{t: t, db: db, repo: NewSmartRepo(db), alice: a.ID, bob: b.ID, now: time.Unix(1_700_000_000, 0), tracks: map[string]int64{}, artist: map[string]int64{}, album: map[string]int64{}} for i, s := range songs { f := pf("x/"+s.title+"."+s.format, s.title, s.album, s.artist, i+1, 1, s.format, s.artist) f.Tags.Genres = s.genres f.BitRate = s.bitrate f.MTimeUnix = s.mtime if s.seconds > 0 { f.DurationMs = s.seconds * 1000 } if _, err := lib.SaveFile(ctx, l.ID, f, 0); err != nil { t.Fatal(err) } var id int64 if err := db.QueryRow("SELECT id FROM tracks WHERE title = ?", s.title).Scan(&id); err != nil { t.Fatal(err) } e.tracks[s.title] = id } for _, q := range []struct { sql string to map[string]int64 }{{"SELECT id, name FROM artists", e.artist}, {"SELECT id, title FROM albums", e.album}} { rows, err := db.Query(q.sql) if err != nil { t.Fatal(err) } for rows.Next() { var id int64 var name string if err := rows.Scan(&id, &name); err != nil { t.Fatal(err) } q.to[name] = id } rows.Close() } return e } func (e *smartEnv) tick() time.Time { e.now = e.now.Add(time.Minute); return e.now } func smartBackends(t *testing.T, songs []smartSong, fn func(t *testing.T, e *smartEnv)) { forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { fn(t, newSmartEnv(t, db, songs)) }) } var smartSongs = []smartSong{ {"a", "Queen", "News", "flac", []string{"rock", "classic rock"}, 1000, 1_000, 120}, {"b", "Queen", "News", "mpeg", []string{"rock"}, 320, 2_000, 200}, {"c", "Kiss", "Alive", "mpeg", []string{"rock", "hard rock"}, 192, 3_000, 240}, {"d", "Slayer", "Reign", "flac", []string{"metal"}, 800, 4_000, 180}, {"e", "Miles", "Blue", "flac", []string{"jazz+funk"}, 1100, 5_000, 300}, } func names(e *smartEnv, cands []smart.Candidate) []string { byID := map[int64]string{} for k, v := range e.tracks { byID[v] = k } out := make([]string, len(cands)) for i, c := range cands { out[i] = byID[c.TrackID] } slices.Sort(out) return out } func TestSmartRepoCandidatesNarrowByEveryList(t *testing.T) { smartBackends(t, smartSongs, func(t *testing.T, e *smartEnv) { ctx := context.Background() for name, tc := range map[string]struct { f smart.Filter want []string }{ "no filter: every playable song": {smart.Filter{}, []string{"a", "b", "c", "d", "e"}}, "a genre": {smart.Filter{Genres: []string{"rock"}}, []string{"a", "b", "c"}}, "one of two genres": {smart.Filter{Genres: []string{"metal", "jazz+funk"}}, []string{"d", "e"}}, "not a genre": {smart.Filter{NotGenres: []string{"hard rock", "metal"}}, []string{"a", "b", "e"}}, "a genre and not another": {smart.Filter{Genres: []string{"rock"}, NotGenres: []string{"hard rock"}}, []string{"a", "b"}}, "an artist": {smart.Filter{ArtistIDs: []int64{e.artist["Queen"]}}, []string{"a", "b"}}, "not an artist": {smart.Filter{NotArtistIDs: []int64{e.artist["Queen"], e.artist["Kiss"]}}, []string{"d", "e"}}, "an album": {smart.Filter{AlbumIDs: []int64{e.album["Alive"]}}, []string{"c"}}, "not an album": {smart.Filter{NotAlbumIDs: []int64{e.album["News"]}}, []string{"c", "d", "e"}}, "a format": {smart.Filter{Formats: []string{"mpeg"}}, []string{"b", "c"}}, "two formats": {smart.Filter{Formats: []string{"mpeg", "flac"}}, []string{"a", "b", "c", "d", "e"}}, "a bitrate": {smart.Filter{MinBitrateKbps: 900}, []string{"a", "e"}}, "a format and a bitrate": {smart.Filter{Formats: []string{"flac"}, MinBitrateKbps: 900}, []string{"a", "e"}}, "a format that nothing has": {smart.Filter{Formats: []string{"ogg"}}, []string{}}, "two lists must both hold": {smart.Filter{Genres: []string{"rock"}, Formats: []string{"flac"}}, []string{"a"}}, } { t.Run(name, func(t *testing.T) { got, err := e.repo.Candidates(ctx, tc.f) if err != nil { t.Fatal(err) } if g := names(e, got); !slices.Equal(g, tc.want) { t.Errorf("got %v, want %v", g, tc.want) } }) } }) } func TestSmartRepoAFormatAndABitrateAreAboutTheSameFile(t *testing.T) { songs := []smartSong{{"a", "Queen", "News", "flac", nil, 500, 1, 60}} smartBackends(t, songs, func(t *testing.T, e *smartEnv) { // A second file of the same song: an mp3 with a high bitrate. flac AND 900 holds for no file. if _, err := e.db.Exec(`INSERT INTO audio_files (track_id, library_id, rel_path, format, size_bytes, mtime_unix, bitrate) SELECT track_id, library_id, 'x/a.mp3', 'mpeg', 1, 9000, 1000 FROM audio_files LIMIT 1`); err != nil { t.Fatal(err) } got, err := e.repo.Candidates(context.Background(), smart.Filter{Formats: []string{"flac"}, MinBitrateKbps: 900}) if err != nil { t.Fatal(err) } if len(got) != 0 { t.Errorf("got %v: the format and the bitrate belong to one file", names(e, got)) } got, _ = e.repo.Candidates(context.Background(), smart.Filter{Formats: []string{"mpeg"}, MinBitrateKbps: 900}) if len(got) != 1 { t.Errorf("the mp3 file has both: got %v", names(e, got)) } }) } func TestSmartRepoCandidatesGiveWhatTheRulesNeedAndLeaveOutSongsWithNoFile(t *testing.T) { smartBackends(t, smartSongs, func(t *testing.T, e *smartEnv) { ctx := context.Background() if _, err := e.db.Exec("UPDATE audio_files SET missing_at = 1 WHERE track_id = ?", e.tracks["e"]); err != nil { t.Fatal(err) } got, err := e.repo.Candidates(ctx, smart.Filter{}) if err != nil { t.Fatal(err) } if g := names(e, got); !slices.Equal(g, []string{"a", "b", "c", "d"}) { t.Errorf("got %v: a song whose files are gone is not offered", g) } for _, c := range got { if c.TrackID != e.tracks["c"] { continue } if c.ArtistID != e.artist["Kiss"] || c.DurationMs != 240_000 || c.Title != "c" || !c.AddedAt.Equal(time.Unix(3_000, 0)) { t.Errorf("candidate = %+v", c) } } // The date is the newest file that is not missing. if _, err := e.db.Exec(`INSERT INTO audio_files (track_id, library_id, rel_path, format, size_bytes, mtime_unix) SELECT track_id, library_id, 'x/c2.mpeg', 'mpeg', 1, 7000 FROM audio_files WHERE track_id = ? LIMIT 1`, e.tracks["c"]); err != nil { t.Fatal(err) } if _, err := e.db.Exec(`INSERT INTO audio_files (track_id, library_id, rel_path, format, size_bytes, mtime_unix, missing_at) SELECT track_id, library_id, 'x/c3.mpeg', 'mpeg', 1, 9000, 5 FROM audio_files WHERE track_id = ? LIMIT 1`, e.tracks["c"]); err != nil { t.Fatal(err) } got, _ = e.repo.Candidates(ctx, smart.Filter{ArtistIDs: []int64{e.artist["Kiss"]}}) if len(got) != 1 || !got[0].AddedAt.Equal(time.Unix(7_000, 0)) { t.Errorf("got %+v, want one song added at 7000", got) } }) } func sample() smart.SmartPlaylist { return smart.SmartPlaylist{ Name: "Fresh", Description: "new things", Sort: smart.SortRecentlyAdded, Limit: 30, Rules: smart.Rules{Genres: []string{"rock"}, ArtistIDs: []int64{3, 4}, Formats: []string{"flac"}, MinBitrateKbps: 900, AddedWithinDays: 30, NeverPlayed: true, MinDurationSec: 60}, } } func TestSmartRepoKeepsAPlaylistAndItsRulesPerUser(t *testing.T) { smartBackends(t, smartSongs, func(t *testing.T, e *smartEnv) { ctx := context.Background() p, err := e.repo.Create(ctx, e.alice, sample(), e.tick()) if err != nil { t.Fatal(err) } want := sample() want.ID, want.CreatedAt, want.UpdatedAt = p.ID, e.now, e.now if p.ID == 0 || p.Name != want.Name || p.Description != want.Description || p.Sort != want.Sort || p.Limit != want.Limit || !slices.Equal(p.Rules.Genres, want.Rules.Genres) || !slices.Equal(p.Rules.ArtistIDs, want.Rules.ArtistIDs) || !slices.Equal(p.Rules.Formats, want.Rules.Formats) || p.Rules.MinBitrateKbps != 900 || p.Rules.AddedWithinDays != 30 || !p.Rules.NeverPlayed || p.Rules.MinDurationSec != 60 || !p.CreatedAt.Equal(e.now) || !p.UpdatedAt.Equal(e.now) { t.Errorf("created = %+v, want %+v", p, want) } got, err := e.repo.Get(ctx, e.alice, p.ID) if err != nil || got.Name != "Fresh" || !slices.Equal(got.Rules.Genres, []string{"rock"}) { t.Errorf("get = %+v, %v", got, err) } // Somebody else's is as if it did not exist, whatever is done to it. if _, err := e.repo.Get(ctx, e.bob, p.ID); !errors.Is(err, smart.ErrNotFound) { t.Errorf("get by another user: %v", err) } if _, err := e.repo.Update(ctx, e.bob, p.ID, sample(), e.tick()); !errors.Is(err, smart.ErrNotFound) { t.Errorf("update by another user: %v", err) } if err := e.repo.Delete(ctx, e.bob, p.ID); !errors.Is(err, smart.ErrNotFound) { t.Errorf("delete by another user: %v", err) } if got, _ := e.repo.Get(ctx, e.alice, p.ID); got.Name != "Fresh" { t.Errorf("the playlist changed: %+v", got) } if _, err := e.repo.Get(ctx, e.alice, p.ID+100); !errors.Is(err, smart.ErrNotFound) { t.Errorf("get of nothing: %v", err) } }) } func TestSmartRepoListsTheOneChangedLastFirstInPages(t *testing.T) { smartBackends(t, smartSongs, func(t *testing.T, e *smartEnv) { ctx := context.Background() var ids []int64 for _, n := range []string{"one", "two", "three"} { p := sample() p.Name = n made, err := e.repo.Create(ctx, e.alice, p, e.tick()) if err != nil { t.Fatal(err) } ids = append(ids, made.ID) } other := sample() other.Name = "bob's" if _, err := e.repo.Create(ctx, e.bob, other, e.tick()); err != nil { t.Fatal(err) } // Changing "one" brings it to the top. upd := sample() upd.Name = "one, again" if _, err := e.repo.Update(ctx, e.alice, ids[0], upd, e.tick()); err != nil { t.Fatal(err) } list, total, err := e.repo.List(ctx, e.alice, 2, 0) if err != nil || total != 3 || len(list) != 2 || list[0].Name != "one, again" || list[1].Name != "three" { t.Fatalf("page 1 = %+v, total %d, %v", list, total, err) } list, _, _ = e.repo.List(ctx, e.alice, 2, 2) if len(list) != 1 || list[0].Name != "two" { t.Errorf("page 2 = %+v", list) } if err := e.repo.Delete(ctx, e.alice, ids[1]); err != nil { t.Fatal(err) } if _, total, _ := e.repo.List(ctx, e.alice, 10, 0); total != 2 { t.Errorf("total after a delete = %d", total) } }) } func TestSmartRepoStopsAtTheMostASmartPlaylistsAUserCanHave(t *testing.T) { smartBackends(t, smartSongs, func(t *testing.T, e *smartEnv) { ctx := context.Background() for i := 0; i < smart.MaxPlaylists; i++ { if _, err := e.repo.Create(ctx, e.alice, sample(), e.tick()); err != nil { t.Fatalf("playlist %d: %v", i+1, err) } } if _, err := e.repo.Create(ctx, e.alice, sample(), e.tick()); !errors.Is(err, smart.ErrTooMany) { t.Errorf("err = %v, want ErrTooMany", err) } if _, err := e.repo.Create(ctx, e.bob, sample(), e.tick()); err != nil { t.Errorf("another user is not held back: %v", err) } }) } func TestSmartPlaylistsGoWhenTheirOwnerGoes(t *testing.T) { smartBackends(t, smartSongs, func(t *testing.T, e *smartEnv) { ctx := context.Background() if _, err := e.repo.Create(ctx, e.bob, sample(), e.tick()); err != nil { t.Fatal(err) } if _, err := e.db.Exec("DELETE FROM users WHERE id = ?", e.bob); err != nil { t.Fatal(err) } var n int if err := e.db.QueryRow("SELECT COUNT(*) FROM smart_playlists").Scan(&n); err != nil || n != 0 { t.Errorf("%d smart playlists left, %v", n, err) } }) } func TestSmartRepoCountsTheListensOfEachSongSinceAMoment(t *testing.T) { smartBackends(t, smartSongs, func(t *testing.T, e *smartEnv) { ctx := context.Background() at := func(track string, user int64, kind string, played int64, when int64, key string) { t.Helper() if _, err := e.db.Exec(`INSERT INTO listening_events (user_id, track_id, event_type, occurred_at, duration_played_ms, idempotency_key) VALUES (?, ?, ?, ?, ?, ?)`, user, e.tracks[track], kind, when, played, key); err != nil { t.Fatal(err) } } const day = 86_400 now := int64(1_700_000_000) at("a", e.alice, "complete", 180_000, now-1*day, "k1") // counts at("a", e.alice, "complete", 180_000, now-2*day, "k2") // counts at("a", e.alice, "play", 30_000, now-3*day, "k3") // played long enough: counts at("a", e.alice, "play", 5_000, now-3*day, "k4") // too short: not a listen at("a", e.alice, "skip", 2_000, now-1*day, "k5") // a skip at("a", e.alice, "complete", 180_000, now-60*day, "k6") // before the window at("b", e.alice, "complete", 180_000, now-5*day, "k7") at("a", e.bob, "complete", 180_000, now-1*day, "k8") // somebody else's got, err := e.repo.Plays(ctx, e.alice, 20*time.Second, time.Unix(now-30*day, 0)) if err != nil { t.Fatal(err) } if got[e.tracks["a"]] != 3 || got[e.tracks["b"]] != 1 || len(got) != 2 { t.Errorf("last 30 days = %v, want a: 3, b: 1", got) } all, _ := e.repo.Plays(ctx, e.alice, 20*time.Second, time.Unix(0, 0)) if all[e.tracks["a"]] != 4 { t.Errorf("all time = %v, want a: 4", all) } if none, _ := e.repo.Plays(ctx, e.alice, 20*time.Second, time.Unix(now+day, 0)); len(none) != 0 { t.Errorf("a window in the future = %v", none) } }) } func TestSmartRepoAlbumCoversFollowTheOrderOfTheSongsAndSkipWhatHasNoCover(t *testing.T) { smartBackends(t, smartSongs, func(t *testing.T, e *smartEnv) { ctx := context.Background() cover := func(album string, status string) { t.Helper() if _, err := e.db.Exec("DELETE FROM images WHERE kind = 'album' AND owner_id = ?", e.album[album]); err != nil { t.Fatal(err) } if _, err := e.db.Exec("INSERT INTO images (kind, owner_id, status, checked_at) VALUES ('album', ?, ?, 1)", e.album[album], status); err != nil { t.Fatal(err) } } // a and b share the album News, c has Alive, d has Reign, e has Blue. cover("News", "ok") cover("Alive", "none") cover("Reign", "ok") cover("Blue", "stale") ids := func(names ...string) []int64 { out := make([]int64, len(names)) for i, n := range names { out[i] = e.tracks[n] } return out } got, err := e.repo.AlbumCovers(ctx, ids("d", "c", "b", "a", "e"), 4) if err != nil { t.Fatal(err) } if want := []int64{e.album["Reign"], e.album["News"]}; !slices.Equal(got, want) { t.Errorf("covers = %v, want %v: in the order of the songs, News once, no cover and a stale one left out", got, want) } if got, _ := e.repo.AlbumCovers(ctx, ids("d", "a"), 1); !slices.Equal(got, []int64{e.album["Reign"]}) { t.Errorf("limit 1 = %v", got) } if got, err := e.repo.AlbumCovers(ctx, nil, 4); err != nil || got == nil || len(got) != 0 { t.Errorf("no songs = %#v, %v", got, err) } if got, _ := e.repo.AlbumCovers(ctx, ids("c", "e"), 4); len(got) != 0 { t.Errorf("songs of albums with no cover = %v", got) } }) } func TestSmartRepoCandidatesNarrowByYear(t *testing.T) { smartBackends(t, smartSongs, func(t *testing.T, e *smartEnv) { ctx := context.Background() // a 1975, b 1985, c 1985, d 1995, e has no year. for title, year := range map[string]int{"a": 1975, "b": 1985, "c": 1985, "d": 1995} { if _, err := e.db.Exec("UPDATE tracks SET year = ? WHERE id = ?", year, e.tracks[title]); err != nil { t.Fatal(err) } } for name, tc := range map[string]struct { f smart.Filter want []string }{ "from a year": {smart.Filter{MinYear: 1985}, []string{"b", "c", "d"}}, "up to a year": {smart.Filter{MaxYear: 1985}, []string{"a", "b", "c"}}, "between two years": {smart.Filter{MinYear: 1980, MaxYear: 1989}, []string{"b", "c"}}, "one year": {smart.Filter{MinYear: 1995, MaxYear: 1995}, []string{"d"}}, "a year and a genre": {smart.Filter{MinYear: 1980, Genres: []string{"rock"}}, []string{"b", "c"}}, "no year rule: a song with no year is still there": {smart.Filter{}, []string{"a", "b", "c", "d", "e"}}, } { t.Run(name, func(t *testing.T) { got, err := e.repo.Candidates(ctx, tc.f) if err != nil { t.Fatal(err) } if g := names(e, got); !slices.Equal(g, tc.want) { t.Errorf("got %v, want %v", g, tc.want) } }) } }) }