sonda/internal/api/http/feedback_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

189 lines
7.5 KiB
Go

package httpapi
import (
"net/http"
"strconv"
"testing"
)
func id(n int64) string { return strconv.FormatInt(n, 10) }
// gamma runs Discover once so that Last.fm's answer is stored and Gamma (an artist
// with no files) exists, and returns its id.
func (e *discoverEnv) gamma(t *testing.T) int64 {
t.Helper()
r := e.get(t, "?"+e.seed(e.alpha))
if r.Code != http.StatusOK {
t.Fatalf("discover: %d %v", r.Code, r.Body)
}
return int64(itemFor(t, r, "Gamma")["artist"].(map[string]any)["id"].(float64))
}
func (e *discoverEnv) bob(t *testing.T) string {
t.Helper()
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)
}
tok, _ := do(t, e.h, "POST", "/api/v1/auth/login", "", body).Body["access_token"].(string)
return tok
}
func TestFeedbackRoutesNeedASignedInUser(t *testing.T) {
e := newDiscoverEnv(t)
for _, rt := range []struct{ method, path string }{
{"GET", "/api/v1/wishlist"}, {"PUT", "/api/v1/wishlist/1"}, {"DELETE", "/api/v1/wishlist/1"},
{"GET", "/api/v1/discover/dismissed"}, {"POST", "/api/v1/discover/1/dismiss"}, {"DELETE", "/api/v1/discover/1/dismiss"},
} {
if r := do(t, e.h, rt.method, rt.path, "", nil); r.Code != http.StatusUnauthorized {
t.Errorf("%s %s without a token: %d", rt.method, rt.path, r.Code)
}
}
}
func TestWishlist(t *testing.T) {
e := newDiscoverEnv(t)
e.enableLastfm(t)
gamma := e.gamma(t)
if r := do(t, e.h, "GET", "/api/v1/wishlist", e.admin, nil); r.Code != http.StatusOK || len(r.Body["items"].([]any)) != 0 || r.Body["total"] != float64(0) {
t.Fatalf("empty wishlist: %d %v", r.Code, r.Body)
}
for i := 0; i < 2; i++ { // twice: the same answer
if r := do(t, e.h, "PUT", "/api/v1/wishlist/"+id(gamma), e.admin, nil); r.Code != http.StatusNoContent {
t.Fatalf("PUT %d: %d %v", i, r.Code, r.Body)
}
}
if r := do(t, e.h, "PUT", "/api/v1/wishlist/"+id(e.beta), e.admin, nil); r.Code != http.StatusNoContent {
t.Fatalf("PUT beta: %d %v", r.Code, r.Body)
}
r := do(t, e.h, "GET", "/api/v1/wishlist", e.admin, nil)
if r.Code != http.StatusOK {
t.Fatalf("%d %v", r.Code, r.Body)
}
sameKeys(t, "page", r.Body, "items", "total", "limit", "offset")
items := r.Body["items"].([]any)
if r.Body["total"] != float64(2) || len(items) != 2 || r.Body["limit"] != float64(50) || r.Body["offset"] != float64(0) {
t.Fatalf("wishlist = %v", r.Body)
}
first := items[0].(map[string]any)
sameKeys(t, "entry", first, "artist", "in_library", "marked_at")
sameKeys(t, "artist", first["artist"].(map[string]any), "id", "name")
byName := map[string]map[string]any{}
for _, it := range items {
m := it.(map[string]any)
byName[m["artist"].(map[string]any)["name"].(string)] = m
}
if byName["Gamma"]["in_library"] != false || byName["Beta"]["in_library"] != true {
t.Errorf("in_library: Gamma %v, Beta %v", byName["Gamma"]["in_library"], byName["Beta"]["in_library"])
}
// Discover says which suggestions are wished for.
d := e.get(t, "?"+e.seed(e.alpha))
if itemFor(t, d, "Gamma")["wishlisted"] != true || itemFor(t, d, "Beta")["wishlisted"] != true {
t.Errorf("wishlisted flags: %v", d.Body["items"])
}
// Take it off, twice.
for i := 0; i < 2; i++ {
if r := do(t, e.h, "DELETE", "/api/v1/wishlist/"+id(gamma), e.admin, nil); r.Code != http.StatusNoContent {
t.Fatalf("DELETE %d: %d %v", i, r.Code, r.Body)
}
}
if r := do(t, e.h, "GET", "/api/v1/wishlist", e.admin, nil); r.Body["total"] != float64(1) {
t.Errorf("after DELETE: %v", r.Body)
}
if d = e.get(t, "?"+e.seed(e.alpha)); itemFor(t, d, "Gamma")["wishlisted"] != false {
t.Error("Gamma is still marked as wished")
}
}
func TestWishlistRefusesWhatIsNotAnArtistAndPagesStrictly(t *testing.T) {
e := newDiscoverEnv(t)
for _, rt := range []struct {
method, path string
code int
err string
}{
{"PUT", "/api/v1/wishlist/99999", 404, "artist_not_found"},
{"DELETE", "/api/v1/wishlist/99999", 404, "artist_not_found"},
{"PUT", "/api/v1/wishlist/abc", 400, "invalid_input"},
{"PUT", "/api/v1/wishlist/0", 400, "invalid_input"},
{"DELETE", "/api/v1/wishlist/-2", 400, "invalid_input"},
{"GET", "/api/v1/wishlist?limit=x", 400, "invalid_input"},
{"GET", "/api/v1/wishlist?limit=-1", 400, "invalid_input"},
{"GET", "/api/v1/wishlist?offset=-1", 400, "invalid_input"},
{"POST", "/api/v1/discover/99999/dismiss", 404, "artist_not_found"},
{"POST", "/api/v1/discover/abc/dismiss", 400, "invalid_input"},
{"DELETE", "/api/v1/discover/99999/dismiss", 404, "artist_not_found"},
{"GET", "/api/v1/discover/dismissed?limit=-1", 400, "invalid_input"},
} {
r := do(t, e.h, rt.method, rt.path, e.admin, nil)
if r.Code != rt.code || errCode(r) != rt.err {
t.Errorf("%s %s: %d %v, want %d %s", rt.method, rt.path, r.Code, r.Body, rt.code, rt.err)
}
}
if r := do(t, e.h, "GET", "/api/v1/wishlist?limit=100000", e.admin, nil); r.Code != http.StatusOK || r.Body["limit"] != float64(200) {
t.Errorf("a large limit is lowered: %d %v", r.Code, r.Body)
}
}
func TestDismissHidesAnArtistFromDiscoverAndCanBeUndone(t *testing.T) {
e := newDiscoverEnv(t)
e.enableLastfm(t)
gamma := e.gamma(t)
if r := do(t, e.h, "POST", "/api/v1/discover/"+id(gamma)+"/dismiss", e.admin, nil); r.Code != http.StatusNoContent {
t.Fatalf("dismiss: %d %v", r.Code, r.Body)
}
d := e.get(t, "?"+e.seed(e.alpha))
for _, it := range d.Body["items"].([]any) {
if it.(map[string]any)["artist"].(map[string]any)["name"] == "Gamma" {
t.Fatal("a dismissed artist is still suggested")
}
}
r := do(t, e.h, "GET", "/api/v1/discover/dismissed", e.admin, nil)
if r.Code != http.StatusOK || r.Body["total"] != float64(1) {
t.Fatalf("dismissed = %d %v", r.Code, r.Body)
}
if r.Body["items"].([]any)[0].(map[string]any)["artist"].(map[string]any)["name"] != "Gamma" {
t.Errorf("dismissed = %v", r.Body)
}
// It is not on the wishlist, and wishing for it replaces the dismissal.
if w := do(t, e.h, "GET", "/api/v1/wishlist", e.admin, nil); w.Body["total"] != float64(0) {
t.Errorf("wishlist = %v", w.Body)
}
if r := do(t, e.h, "PUT", "/api/v1/wishlist/"+id(gamma), e.admin, nil); r.Code != http.StatusNoContent {
t.Fatalf("wish: %d", r.Code)
}
if r := do(t, e.h, "GET", "/api/v1/discover/dismissed", e.admin, nil); r.Body["total"] != float64(0) {
t.Errorf("a wish must replace the dismissal: %v", r.Body)
}
// Dismiss again, then undo it.
do(t, e.h, "POST", "/api/v1/discover/"+id(gamma)+"/dismiss", e.admin, nil)
if r := do(t, e.h, "DELETE", "/api/v1/discover/"+id(gamma)+"/dismiss", e.admin, nil); r.Code != http.StatusNoContent {
t.Fatalf("undo: %d", r.Code)
}
d = e.get(t, "?"+e.seed(e.alpha))
itemFor(t, d, "Gamma") // back
}
func TestFeedbackIsPrivate(t *testing.T) {
e := newDiscoverEnv(t)
e.enableLastfm(t)
gamma := e.gamma(t)
bob := e.bob(t)
do(t, e.h, "PUT", "/api/v1/wishlist/"+id(gamma), e.admin, nil)
do(t, e.h, "POST", "/api/v1/discover/"+id(e.beta)+"/dismiss", e.admin, nil)
if r := do(t, e.h, "GET", "/api/v1/wishlist", bob, nil); r.Body["total"] != float64(0) {
t.Errorf("bob sees alice's wishlist: %v", r.Body)
}
if r := do(t, e.h, "GET", "/api/v1/discover/dismissed", bob, nil); r.Body["total"] != float64(0) {
t.Errorf("bob sees alice's dismissals: %v", r.Body)
}
d := do(t, e.h, "GET", "/api/v1/discover?"+e.seed(e.alpha), bob, nil)
if d.Code != http.StatusOK || itemFor(t, d, "Beta") == nil || itemFor(t, d, "Gamma")["wishlisted"] != false {
t.Errorf("bob's Discover is affected by alice's choices: %v", d.Body)
}
}