- PUT/DELETE /wishlist/{artist} and GET /wishlist: the artists I want.
- POST/DELETE /discover/{artist}/dismiss and GET /discover/dismissed: the
artists I am not interested in. An artist is wished or dismissed, never both,
and both lists are mine alone.
- Discover never suggests a dismissed artist, does not use one as a seed taken
from my listening, and marks the wished ones it shows (wishlisted).
- New artist_feedback table (migration 0012, SQLite and PostgreSQL), removed
with the account.
- An artist I marked is not cleaned up when the list that named it is replaced,
and goes when its last mark is taken off and nobody names it. Taking a
provider's data back still removes what only it named, marks included.
- Spec, contract test and README updated.
Closes #35
231 lines
8.9 KiB
Go
231 lines
8.9 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/uptrace/bun"
|
|
|
|
"codeberg.org/squidd/sonda/internal/feedback"
|
|
"codeberg.org/squidd/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 can still be seen and listed.
|
|
if _, err := NewCatalogRepo(db).GetArtist(ctx, e.found); err != nil {
|
|
t.Errorf("GetArtist: %v", err)
|
|
}
|
|
if _, total, _ := e.repo.List(ctx, e.alice, feedback.Wishlist, 10, 0); total != 1 {
|
|
t.Errorf("the wishlist has %d artists, want 1", total)
|
|
}
|
|
// Another user marking it as well keeps it when the first one lets go.
|
|
if err := e.repo.Mark(ctx, e.bob, e.found, feedback.Dismissed, at); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
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) }
|