package storage import ( "context" "errors" "strconv" "testing" "time" "github.com/uptrace/bun" "git.kmalloc.org/squid/sonda/internal/feedback" "git.kmalloc.org/squid/sonda/internal/providers/lastfm" ) type fbEnv struct { db *bun.DB repo *FeedbackRepo alice int64 bob int64 played int64 // Radiohead, has files found int64 // Staind, named by a provider, no files } func newFbEnv(t *testing.T, db *bun.DB) *fbEnv { t.Helper() seedCatalog(t, db) ctx := context.Background() users := NewUserRepo(db) a, _ := users.CreateUser(ctx, "alice", "h", false) b, _ := users.CreateUser(ctx, "bob", "h", false) e := &fbEnv{db: db, repo: NewFeedbackRepo(db), alice: a.ID, bob: b.ID, played: artistID(t, db, "Radiohead")} if err := NewDiscoveryRepo(db).SaveSimilar(ctx, e.played, "lastfm", at, true, []lastfm.Similar{{Name: "Staind", Match: 1}, {Name: "Seether", Match: 0.5}}); err != nil { t.Fatal(err) } e.found = artistID(t, db, "Staind") return e } func TestFeedbackMarkListAndUnmark(t *testing.T) { forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { e := newFbEnv(t, db) ctx := context.Background() t0 := time.Unix(1_700_000_000, 0) if err := e.repo.Mark(ctx, e.alice, e.found, feedback.Wishlist, t0); err != nil { t.Fatal(err) } if err := e.repo.Mark(ctx, e.alice, e.played, feedback.Wishlist, t0.Add(time.Hour)); err != nil { t.Fatal(err) } entries, total, err := e.repo.List(ctx, e.alice, feedback.Wishlist, 10, 0) if err != nil || total != 2 || len(entries) != 2 { t.Fatalf("list = %+v total %d err %v", entries, total, err) } // Newest first, with the name and whether there is music. if entries[0].Artist.ID != e.played || entries[0].Artist.Name != "Radiohead" || !entries[0].InLibrary || !entries[0].At.Equal(t0.Add(time.Hour)) { t.Errorf("first = %+v", entries[0]) } if entries[1].Artist.ID != e.found || entries[1].InLibrary { t.Errorf("second = %+v, want Staind without music", entries[1]) } // Paging. if page, total, _ := e.repo.List(ctx, e.alice, feedback.Wishlist, 1, 1); len(page) != 1 || total != 2 || page[0].Artist.ID != e.found { t.Errorf("page 2 = %+v total %d", page, total) } if page, total, _ := e.repo.List(ctx, e.alice, feedback.Wishlist, 10, 5); len(page) != 0 || total != 2 { t.Errorf("past the end = %+v total %d", page, total) } // Mine only. if got, total, _ := e.repo.List(ctx, e.bob, feedback.Wishlist, 10, 0); len(got) != 0 || total != 0 || got == nil { t.Errorf("bob's list = %+v (%d), want empty and not nil", got, total) } // Unmark removes only that kind. if err := e.repo.Unmark(ctx, e.alice, e.found, feedback.Dismissed); err != nil { t.Fatal(err) } if _, total, _ := e.repo.List(ctx, e.alice, feedback.Wishlist, 10, 0); total != 2 { t.Errorf("removing a dismissal removed a wish: total %d", total) } if err := e.repo.Unmark(ctx, e.alice, e.found, feedback.Wishlist); err != nil { t.Fatal(err) } if err := e.repo.Unmark(ctx, e.alice, e.found, feedback.Wishlist); err != nil { t.Errorf("unmark again: %v", err) } if _, total, _ := e.repo.List(ctx, e.alice, feedback.Wishlist, 10, 0); total != 1 { t.Errorf("total after unmark = %d", total) } }) } func TestFeedbackAnArtistIsWishedOrDismissedNeverBoth(t *testing.T) { forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { e := newFbEnv(t, db) ctx := context.Background() t0 := time.Unix(1_700_000_000, 0) _ = e.repo.Mark(ctx, e.alice, e.found, feedback.Dismissed, t0) _ = e.repo.Mark(ctx, e.alice, e.found, feedback.Wishlist, t0.Add(time.Hour)) marks, _ := e.repo.Marks(ctx, e.alice) if len(marks) != 1 || marks[e.found] != feedback.Wishlist { t.Errorf("marks = %v, want only a wish", marks) } // Marking again with the same kind keeps the first date. _ = e.repo.Mark(ctx, e.alice, e.found, feedback.Wishlist, t0.Add(48*time.Hour)) entries, _, _ := e.repo.List(ctx, e.alice, feedback.Wishlist, 10, 0) if len(entries) != 1 || !entries[0].At.Equal(t0.Add(time.Hour)) { t.Errorf("entries = %+v, want the date of the first wish", entries) } if got, _ := e.repo.Marks(ctx, e.bob); len(got) != 0 { t.Errorf("bob's marks = %v", got) } }) } func TestFeedbackNeedsAnArtistThatCanBeSeen(t *testing.T) { forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { e := newFbEnv(t, db) ctx := context.Background() t0 := time.Unix(1_700_000_000, 0) for name, id := range map[string]int64{"unknown": 99999, "files are gone": artistID(t, db, "Only Missing")} { if err := e.repo.Mark(ctx, e.alice, id, feedback.Wishlist, t0); !errors.Is(err, feedback.ErrArtistNotFound) { t.Errorf("%s: err = %v", name, err) } } if err := e.repo.Unmark(ctx, e.alice, 99999, feedback.Wishlist); !errors.Is(err, feedback.ErrArtistNotFound) { t.Errorf("unmark unknown: err = %v", err) } if err := e.repo.Mark(ctx, 99999, e.played, feedback.Wishlist, t0); !errors.Is(err, feedback.ErrUserNotFound) { t.Errorf("unknown user: err = %v", err) } }) } func TestFeedbackGoesWithTheAccountAndTheArtist(t *testing.T) { forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { e := newFbEnv(t, db) ctx := context.Background() t0 := time.Unix(1_700_000_000, 0) _ = e.repo.Mark(ctx, e.alice, e.found, feedback.Wishlist, t0) _ = e.repo.Mark(ctx, e.bob, e.found, feedback.Dismissed, t0) if err := NewUserRepo(db).DeleteUser(ctx, e.bob); err != nil { t.Fatal(err) } if n := count(t, db, "SELECT COUNT(*) FROM artist_feedback WHERE user_id = "+itoa(e.bob)); n != 0 { t.Errorf("rows left for a deleted user: %d", n) } if _, err := db.Exec("DELETE FROM artists WHERE id = ?", e.found); err != nil { t.Fatal(err) } if n := count(t, db, "SELECT COUNT(*) FROM artist_feedback"); n != 0 { t.Errorf("rows left for a deleted artist: %d", n) } }) } // An artist I marked must not disappear when the list that named it is replaced: // it would be a different row, with another id, if a provider names it again. func TestFeedbackKeepsAnArtistWhenItsListIsReplaced(t *testing.T) { forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { e := newFbEnv(t, db) ctx := context.Background() seether := artistID(t, db, "Seether") if err := e.repo.Mark(ctx, e.alice, e.found, feedback.Wishlist, at); err != nil { t.Fatal(err) } // The new list names nobody: Staind (marked) stays, Seether (not) goes. if err := NewDiscoveryRepo(db).SaveSimilar(ctx, e.played, "lastfm", at, true, nil); err != nil { t.Fatal(err) } if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE id = "+itoa(e.found)); n != 1 { t.Errorf("the artist I wished for was removed") } if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE id = "+itoa(seether)); n != 0 { t.Errorf("an artist nobody marked or names stayed") } // It is still on the wishlist, but other users cannot see that it exists: only a // mark keeps it, and marks are private. if _, total, _ := e.repo.List(ctx, e.alice, feedback.Wishlist, 10, 0); total != 1 { t.Errorf("the wishlist has %d artists, want 1", total) } if _, err := NewCatalogRepo(db).GetArtist(ctx, e.found); err == nil { t.Error("an artist only kept by marks can be opened by anybody: that shows what others marked") } if err := e.repo.Mark(ctx, e.bob, e.found, feedback.Wishlist, at); !errors.Is(err, feedback.ErrArtistNotFound) { t.Errorf("bob marking an artist only alice marked: err = %v, want not found", err) } if err := e.repo.Unmark(ctx, e.bob, e.found, feedback.Wishlist); !errors.Is(err, feedback.ErrArtistNotFound) { t.Errorf("bob unmarking it: err = %v, want not found, the same as for marking", err) } // Alice can still change her mind about it. if err := e.repo.Mark(ctx, e.alice, e.found, feedback.Dismissed, at); err != nil { t.Errorf("alice dismissing her own wish: %v", err) } if err := e.repo.Mark(ctx, e.alice, e.found, feedback.Wishlist, at); err != nil { t.Errorf("alice wishing again: %v", err) } // With another user's mark as well, it is still hidden from a third person's calls // and kept when the first one lets go only if the second can still see it. // A second user marks it while it is still named (put the list back). if err := NewDiscoveryRepo(db).SaveSimilar(ctx, e.played, "lastfm", at, true, []lastfm.Similar{{Name: "Staind", Match: 1}}); err != nil { t.Fatal(err) } if err := e.repo.Mark(ctx, e.bob, e.found, feedback.Dismissed, at); err != nil { t.Fatal(err) } // ...and it goes away again from the list. if err := NewDiscoveryRepo(db).SaveSimilar(ctx, e.played, "lastfm", at, true, nil); err != nil { t.Fatal(err) } if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE id = "+itoa(e.found)); n != 1 { t.Fatal("the artist went although two users mark it") } if err := e.repo.Unmark(ctx, e.alice, e.found, feedback.Wishlist); err != nil { t.Errorf("Unmark: %v", err) } if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE id = "+itoa(e.found)); n != 1 { t.Error("the artist went although another user still marks it") } // The last mark gone and nobody naming it: nothing keeps the artist. if err := e.repo.Unmark(ctx, e.bob, e.found, feedback.Dismissed); err != nil { t.Errorf("Unmark: %v", err) } if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE id = "+itoa(e.found)); n != 0 { t.Error("an artist nobody names or marks stayed") } // A real artist is never removed that way. if err := e.repo.Mark(ctx, e.alice, e.played, feedback.Wishlist, at); err != nil { t.Fatal(err) } if err := e.repo.Unmark(ctx, e.alice, e.played, feedback.Wishlist); err != nil { t.Fatal(err) } if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE id = "+itoa(e.played)); n != 1 { t.Error("an artist with music was removed") } }) } // Taking a provider's data back removes what only it named, wishes included. func TestFeedbackDoesNotSurviveAPurgeOfWhatOnlyTheProviderNamed(t *testing.T) { forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { e := newFbEnv(t, db) ctx := context.Background() _ = e.repo.Mark(ctx, e.alice, e.found, feedback.Wishlist, at) if _, err := NewDiscoveryRepo(db).Purge(ctx, "lastfm"); err != nil { t.Fatal(err) } if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE id = "+itoa(e.found)); n != 0 { t.Error("the purge left an artist only the provider named") } if n := count(t, db, "SELECT COUNT(*) FROM artist_feedback"); n != 0 { t.Errorf("marks left for a purged artist: %d", n) } }) } func itoa(n int64) string { return strconv.FormatInt(n, 10) } // Purging a provider that never names artists must not touch the artists another // provider named, even when only a mark keeps them. func TestFeedbackSurvivesAPurgeOfAnotherProvider(t *testing.T) { forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { e := newFbEnv(t, db) ctx := context.Background() _ = e.repo.Mark(ctx, e.alice, e.found, feedback.Wishlist, at) // Last.fm's list is replaced: Staind stays, kept by the wish alone. if err := NewDiscoveryRepo(db).SaveSimilar(ctx, e.played, "lastfm", at, true, nil); err != nil { t.Fatal(err) } if _, err := NewDiscoveryRepo(db).Purge(ctx, "musicbrainz"); err != nil { t.Fatal(err) } if n := count(t, db, "SELECT COUNT(*) FROM artist_feedback"); n != 1 { t.Errorf("a purge of MusicBrainz removed %d marks of artists it never named", 1-n) } if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE id = "+itoa(e.found)); n != 1 { t.Error("a purge of MusicBrainz removed an artist only Last.fm named") } // Purging Last.fm, which named it, does remove it, the wish with it. if _, err := NewDiscoveryRepo(db).Purge(ctx, "lastfm"); err != nil { t.Fatal(err) } }) } func TestFeedbackDeletingAnAccountCleansUpTheArtistsOnlyItsMarksKept(t *testing.T) { forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { e := newFbEnv(t, db) ctx := context.Background() _ = e.repo.Mark(ctx, e.alice, e.found, feedback.Wishlist, at) if err := NewDiscoveryRepo(db).SaveSimilar(ctx, e.played, "lastfm", at, true, nil); err != nil { t.Fatal(err) } if err := NewUserRepo(db).DeleteUser(ctx, e.alice); err != nil { t.Fatal(err) } if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE id = "+itoa(e.found)); n != 0 { t.Error("an artist only a deleted user's wish kept is still there") } // An artist that a list still names is kept, and so is a real one. if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE id = "+itoa(e.played)); n != 1 { t.Error("a real artist was removed with an account") } }) } func TestFeedbackSurvivesTheScannerAdoptingTheArtist(t *testing.T) { forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { e := newFbEnv(t, db) ctx := context.Background() _ = e.repo.Mark(ctx, e.alice, e.found, feedback.Wishlist, at) lib := NewLibraryRepo(db) l, err := lib.CreateLibrary(ctx, "new", "/lib/new") if err != nil { t.Fatal(err) } // The music of Staind arrives: the scan takes the artist over instead of making another. if _, err := lib.SaveFile(ctx, l.ID, pf("s/1.flac", "Outside", "Break the Cycle", "Staind", 1, 1, "flac", "Staind"), 0); err != nil { t.Fatal(err) } if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE name = 'Staind'"); n != 1 { t.Fatalf("%d artists called Staind", n) } entries, _, _ := e.repo.List(ctx, e.alice, feedback.Wishlist, 10, 0) if len(entries) != 1 || entries[0].Artist.ID != e.found || !entries[0].InLibrary { t.Errorf("wishlist = %+v, want the same artist, now with music", entries) } // It is a real artist now: taking the wish off never removes it. if err := e.repo.Unmark(ctx, e.alice, e.found, feedback.Wishlist); err != nil { t.Fatal(err) } if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE id = "+itoa(e.found)); n != 1 { t.Error("an artist with music was removed when its wish was taken off") } }) }