sonda/internal/api/http/discover_test.go
tgiachi a6dbf60ac8 feat(feedback): a wishlist and "not interested"
- 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
2026-11-11 20:11:18 +01:00

262 lines
9.6 KiB
Go

package httpapi
import (
"math"
"net/http"
"strconv"
"testing"
"time"
"github.com/uptrace/bun"
"go.uber.org/zap"
"codeberg.org/squidd/sonda/internal/discover"
"codeberg.org/squidd/sonda/internal/discovery"
"codeberg.org/squidd/sonda/internal/feedback"
"codeberg.org/squidd/sonda/internal/providers"
"codeberg.org/squidd/sonda/internal/storage"
"codeberg.org/squidd/sonda/internal/taste"
"codeberg.org/squidd/sonda/internal/usersettings"
)
type discoverEnv struct {
*similarEnv
lf *fakeTags // what Last.fm says about tags, for any artist
mb *fakeTags // MusicBrainz, which Discover must never ask
}
// newDiscoverEnv wires the real Discover on the similar-artists environment: Last.fm
// is off until a test turns it on. The fake answers say Beta (match 1, in the
// library) and Gamma (match 0.5, not in it) are similar to anyone.
func newDiscoverEnv(t *testing.T) *discoverEnv {
t.Helper()
e := &discoverEnv{
lf: &fakeTags{tags: []discovery.Tag{{Name: "rock", Weight: 100}, {Name: "grunge", Weight: 40}}},
mb: &fakeTags{tags: []discovery.Tag{{Name: "alternative rock", Weight: 43, Genre: true}}},
}
e.similarEnv = newSimilarEnvWith(t, func(d *Deps, db *bun.DB, prov *providers.Service) {
tasteSvc := taste.NewService(storage.NewTasteRepo(db), taste.Options{MinListen: 20 * time.Second, HalfLife: 90 * 24 * time.Hour})
settings := usersettings.NewService(storage.NewUserSettingsRepo(db))
tags := discovery.NewTagService(storage.NewDiscoveryRepo(db), map[string]discovery.TagSource{providers.LastFM: e.lf, providers.MusicBrainz: e.mb}, prov, zap.NewNop())
d.Taste, d.Settings = tasteSvc, settings
fb := feedback.NewService(storage.NewFeedbackRepo(db))
d.Feedback = fb
d.Discover = discover.NewService(tasteSvc, d.Discovery.(discover.Similar), tags, storage.NewTasteRepo(db), settings, fb, discover.DefaultWeights())
})
return e
}
func (e *discoverEnv) get(t *testing.T, query string) resp {
t.Helper()
return do(t, e.h, "GET", "/api/v1/discover"+query, e.admin, nil)
}
func (e *discoverEnv) allowListening(t *testing.T) {
t.Helper()
if r := do(t, e.h, "PATCH", "/api/v1/me/settings", e.admin, map[string]any{"discover_from_listening": true}); r.Code != http.StatusOK {
t.Fatalf("turn the setting on: %d %v", r.Code, r.Body)
}
}
func (e *discoverEnv) seed(id int64) string { return "seed=" + strconv.FormatInt(id, 10) }
func itemFor(t *testing.T, r resp, name string) map[string]any {
t.Helper()
items, _ := r.Body["items"].([]any)
for _, it := range items {
m := it.(map[string]any)
if m["artist"].(map[string]any)["name"] == name {
return m
}
}
t.Fatalf("%s is not in %v", name, r.Body["items"])
return nil
}
func TestDiscoverNeedsASignedInUser(t *testing.T) {
e := newDiscoverEnv(t)
if r := do(t, e.h, "GET", "/api/v1/discover", "", nil); r.Code != http.StatusUnauthorized {
t.Errorf("no token: %d", r.Code)
}
}
func TestDiscoverWithoutASeedAndTheSettingOffAsksNobody(t *testing.T) {
e := newDiscoverEnv(t)
e.enableLastfm(t)
r := e.get(t, "")
if r.Code != http.StatusBadRequest || errCode(r) != "seed_required" {
t.Errorf("%d %v, want 400 seed_required", r.Code, r.Body)
}
if e.source.count() != 0 || e.lf.calls != 0 {
t.Errorf("Last.fm was asked (%d similar, %d tags) without the listener's permission", e.source.count(), e.lf.calls)
}
}
func TestDiscoverFromANamedSeed(t *testing.T) {
e := newDiscoverEnv(t)
e.enableLastfm(t)
r := e.get(t, "?"+e.seed(e.alpha))
if r.Code != http.StatusOK {
t.Fatalf("%d %v", r.Code, r.Body)
}
sameKeys(t, "discover", r.Body, "provider_status", "attribution", "from_listening", "seeds", "items")
if r.Body["provider_status"] != "ok" || r.Body["from_listening"] != false || r.Body["attribution"] == "" {
t.Errorf("answer = %v", r.Body)
}
seeds := r.Body["seeds"].([]any)
if len(seeds) != 1 {
t.Fatalf("seeds = %v", seeds)
}
sameKeys(t, "seed", seeds[0].(map[string]any), "id", "name", "weight")
if s := seeds[0].(map[string]any); s["name"] != "Alpha" || s["weight"] != float64(1) {
t.Errorf("seed = %v", s)
}
beta, gamma := itemFor(t, r, "Beta"), itemFor(t, r, "Gamma")
sameKeys(t, "item", beta, "artist", "in_library", "wishlisted", "score", "components", "because")
sameKeys(t, "components", beta["components"].(map[string]any), "a", "g", "p", "d")
if beta["in_library"] != true || gamma["in_library"] != false {
t.Errorf("in_library: Beta %v, Gamma %v", beta["in_library"], gamma["in_library"])
}
// Nothing was ever listened to: no profile, so no genre, a neutral preference, full discovery.
c := beta["components"].(map[string]any)
if v, present := c["g"]; !present || v != nil {
t.Errorf("g = %#v, want an explicit null", v)
}
if c["p"] != 0.5 || c["d"] != float64(1) || c["a"] != float64(1) {
t.Errorf("components = %v, want a=1 p=0.5 d=1", c)
}
because := beta["because"].([]any)
if len(because) != 1 {
t.Fatalf("because = %v", because)
}
sameKeys(t, "because", because[0].(map[string]any), "artist", "match")
if because[0].(map[string]any)["artist"].(map[string]any)["name"] != "Alpha" {
t.Errorf("because = %v", because)
}
if e.lf.calls != 0 {
t.Error("tags were read although there is no taste to compare them with")
}
}
func TestDiscoverFromMyListeningNeedsAndUsesTheSetting(t *testing.T) {
e := newDiscoverEnv(t)
e.enableLastfm(t)
alpha := e.trackOf(t, "Alpha")
e.listen(t, e.admin, alpha, "complete", 1000, "a1")
e.listen(t, e.admin, alpha, "complete", 1000, "a2")
if _, err := e.db.Exec("INSERT INTO track_genres (track_id, genre) VALUES (?, 'rock')", alpha); err != nil {
t.Fatal(err)
}
if r := e.get(t, ""); r.Code != http.StatusBadRequest || errCode(r) != "seed_required" {
t.Fatalf("setting off: %d %v", r.Code, r.Body)
}
if e.source.count() != 0 {
t.Fatal("Last.fm was asked about the listening although the setting is off")
}
e.allowListening(t)
r := e.get(t, "")
if r.Code != http.StatusOK || r.Body["from_listening"] != true {
t.Fatalf("%d %v", r.Code, r.Body)
}
seeds := r.Body["seeds"].([]any)
if len(seeds) != 1 || seeds[0].(map[string]any)["name"] != "Alpha" {
t.Fatalf("seeds = %v, want Alpha", seeds)
}
if w := seeds[0].(map[string]any)["weight"].(float64); math.Abs(w-math.Tanh(2.0/3)) > 0.001 {
t.Errorf("seed weight = %v, want the preference %v", w, math.Tanh(2.0/3))
}
gamma := itemFor(t, r, "Gamma")
comp := gamma["components"].(map[string]any)
// The profile is {rock: 1}; Gamma's Last.fm tags are rock 1 and grunge 0.4.
if g, _ := comp["g"].(float64); math.Abs(g-1/math.Sqrt(1.16)) > 0.001 {
t.Errorf("g = %v, want %v", comp["g"], 1/math.Sqrt(1.16))
}
if e.lf.calls == 0 {
t.Error("the tags of the candidates were never read")
}
if e.mb.calls != 0 {
t.Errorf("MusicBrainz was asked %d times: Discover reads tags from Last.fm only", e.mb.calls)
}
}
func TestTheSettingBelongsToEachUser(t *testing.T) {
e := newDiscoverEnv(t)
e.enableLastfm(t)
e.listen(t, e.admin, e.trackOf(t, "Alpha"), "complete", 1000, "a1")
e.allowListening(t) // alice allows it
body := map[string]any{"username": "bob", "password": "longenough"}
if r := do(t, e.h, "POST", "/api/v1/users", e.admin, body); r.Code != http.StatusCreated {
t.Fatalf("create bob: %d %v", r.Code, r.Body)
}
bob, _ := do(t, e.h, "POST", "/api/v1/auth/login", "", body).Body["access_token"].(string)
r := do(t, e.h, "GET", "/api/v1/discover", bob, nil)
if r.Code != http.StatusBadRequest || errCode(r) != "seed_required" {
t.Errorf("bob did not allow it: %d %v, want 400 seed_required", r.Code, r.Body)
}
if e.source.count() != 0 {
t.Error("Last.fm was asked on behalf of bob, who never allowed it")
}
if r := e.get(t, ""); r.Code != http.StatusOK {
t.Errorf("alice did: %d %v", r.Code, r.Body)
}
}
func TestDiscoverFilterAndLimit(t *testing.T) {
e := newDiscoverEnv(t)
e.enableLastfm(t)
base := "?" + e.seed(e.alpha)
r := e.get(t, base+"&in_library=false")
if r.Code != http.StatusOK || len(r.Body["items"].([]any)) != 1 {
t.Fatalf("outside: %d %v", r.Code, r.Body)
}
itemFor(t, r, "Gamma")
r = e.get(t, base+"&in_library=true")
if r.Code != http.StatusOK || len(r.Body["items"].([]any)) != 1 {
t.Fatalf("inside: %d %v", r.Code, r.Body)
}
itemFor(t, r, "Beta")
if r = e.get(t, base+"&limit=1"); len(r.Body["items"].([]any)) != 1 {
t.Errorf("limit 1: %v", r.Body["items"])
}
if r = e.get(t, base+"&limit=1000"); r.Code != http.StatusOK {
t.Errorf("a large limit is lowered, not refused: %d", r.Code)
}
}
func TestDiscoverRefusesWhatItCannotUnderstand(t *testing.T) {
e := newDiscoverEnv(t)
e.enableLastfm(t)
for name, q := range map[string]string{
"seed not a number": "?seed=abc",
"unknown seed": "?seed=99999",
"zero seed": "?seed=0",
"too many seeds": "?seed=1&seed=2&seed=3&seed=4&seed=5&seed=6",
"limit not number": "?" + e.seed(e.alpha) + "&limit=lots",
"negative limit": "?" + e.seed(e.alpha) + "&limit=-1",
"bad in_library": "?" + e.seed(e.alpha) + "&in_library=maybe",
"in_library twice": "?" + e.seed(e.alpha) + "&in_library=true&in_library=false",
} {
r := e.get(t, q)
if r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" {
t.Errorf("%s: %d %v, want 400 invalid_input", name, r.Code, r.Body)
}
}
if e.source.count() != 0 {
t.Error("Last.fm was asked about a request that was refused")
}
}
func TestDiscoverWithLastfmOffSaysSoAndAsksNobody(t *testing.T) {
e := newDiscoverEnv(t) // Last.fm is off
r := e.get(t, "?"+e.seed(e.alpha))
if r.Code != http.StatusOK || r.Body["provider_status"] != "disabled" {
t.Fatalf("%d %v, want 200 and status disabled", r.Code, r.Body)
}
if len(r.Body["items"].([]any)) != 0 || e.source.count() != 0 || e.lf.calls != 0 {
t.Errorf("items %v, similar calls %d, tag calls %d", r.Body["items"], e.source.count(), e.lf.calls)
}
}