test(backup, scrobble, playevents, recsettings, apierror, auth, providers): merge repeated cases (#8299)
release / web (push) Successful in 1m41s
release / go (push) Successful in 2m4s
release / govulncheck (push) Successful in 23s
release / integration (push) Successful in 3m5s
release / Attach APK to the Release (tag releases only) (push) Canceled after 0s
release / Build + push container image (push) Canceled after 0s
release / Publish the release image to GHCR (dispatch only) (push) Canceled after 0s
release / Verify release artifacts (tag releases only) (push) Canceled after 0s
release / android (push) Canceled after 4m35s
release / Build signed APK (push) Canceled after 3m53s

- backup: schema coverage's three tests and the three AWS signing
  examples, one table each.
- scrobble: Qualifies' six boundary tests; the ListenBrainz client's
  refusal mapping, twelve tests of the same rule across three calls, one
  table over call x status; the two similarity lookups' success and URL
  tests, one table.
- playevents: the skip rule's three plays share one fixture. Dropped
  TestRecordPlayEnded_SubThreshold_NoEnqueue: the writer only calls
  scrobble.MaybeEnqueue, whose own test and Qualifies' table hold that
  case; the qualifying-play test still proves the writer calls it.
- recsettings: the three tag-scale tests share one database reset.
- apierror: Error() and From, one test each; auth: RequireAdmin's three.
- providers: GetJSON's not-found and transient answers, one table. Its
  two 401 tests expected the same answer with and without
  TreatAuthAsTransient, which changes only the error's text; the table
  says so.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-02 15:16:20 -05:00
co-authored by Claude Opus 5.5
parent b6454270b5
commit cfdbfa30e3
9 changed files with 306 additions and 628 deletions
+93 -240
View File
@@ -54,55 +54,6 @@ func TestClient_SubmitListens_Success(t *testing.T) {
}
}
func TestClient_SubmitListens_401_ReturnsErrAuth(t *testing.T) {
c, srv := newTestClient(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusUnauthorized)
})
defer srv.Close()
err := c.SubmitListens(context.Background(), "bad", []Listen{{}})
if !errors.Is(err, ErrAuth) {
t.Errorf("err = %v, want ErrAuth", err)
}
}
func TestClient_SubmitListens_400_ReturnsErrPermanent(t *testing.T) {
c, srv := newTestClient(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusBadRequest)
})
defer srv.Close()
err := c.SubmitListens(context.Background(), "tk", []Listen{{}})
if !errors.Is(err, ErrPermanent) {
t.Errorf("err = %v, want ErrPermanent", err)
}
}
func TestClient_SubmitListens_503_ReturnsErrTransient(t *testing.T) {
c, srv := newTestClient(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusServiceUnavailable)
})
defer srv.Close()
err := c.SubmitListens(context.Background(), "tk", []Listen{{}})
if !errors.Is(err, ErrTransient) {
t.Errorf("err = %v, want ErrTransient", err)
}
}
func TestClient_SubmitListens_429_ReturnsRetryAfter(t *testing.T) {
c, srv := newTestClient(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Retry-After", "60")
w.WriteHeader(http.StatusTooManyRequests)
})
defer srv.Close()
err := c.SubmitListens(context.Background(), "tk", []Listen{{}})
var ra *RetryAfterError
if !errors.As(err, &ra) {
t.Fatalf("err = %v, want *RetryAfterError", err)
}
if ra.Wait != 60*time.Second {
t.Errorf("Wait = %v, want 60s", ra.Wait)
}
}
func TestClient_SubmitListens_NetworkFailure_ReturnsErrTransient(t *testing.T) {
c := &Client{BaseURL: "http://127.0.0.1:1", HTTP: &http.Client{Timeout: 100 * time.Millisecond}}
err := c.SubmitListens(context.Background(), "tk", []Listen{{}})
@@ -163,200 +114,102 @@ func TestClient_SubmitListens_OmitsEmptyMBIDs(t *testing.T) {
}
}
func TestClient_SimilarRecordings_Success(t *testing.T) {
c, srv := newTestClient(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(`[
{"recording_mbid": "11111111-1111-1111-1111-111111111111", "score": 0.95},
{"recording_mbid": "22222222-2222-2222-2222-222222222222", "score": 0.80}
]`))
})
defer srv.Close()
out, err := c.SimilarRecordings(context.Background(), "aaaa", 100)
if err != nil {
t.Fatalf("err: %v", err)
// Each call answers ListenBrainz's refusals the same way, and the scrobble
// worker acts on that answer: a bad token disables the user's scrobbling, a
// bad request drops the listen, a server error retries with backoff, and a
// rate limit waits as long as Retry-After says without counting an attempt.
func TestClient_RefusalsMapToWhatTheWorkerDoes(t *testing.T) {
calls := map[string]func(*Client) error{
"submit": func(c *Client) error { return c.SubmitListens(context.Background(), "tk", []Listen{{}}) },
"similar recordings": func(c *Client) error {
_, err := c.SimilarRecordings(context.Background(), "abc", 100)
return err
},
"similar artists": func(c *Client) error {
_, err := c.SimilarArtists(context.Background(), "abc", 100)
return err
},
}
if len(out) != 2 {
t.Fatalf("got %d results, want 2", len(out))
}
if out[0].MBID != "11111111-1111-1111-1111-111111111111" || out[0].Score != 0.95 {
t.Errorf("first result wrong: %+v", out[0])
for name, call := range calls {
for _, tc := range []struct {
status int
want error
}{
{http.StatusUnauthorized, ErrAuth},
{http.StatusBadRequest, ErrPermanent},
{http.StatusServiceUnavailable, ErrTransient},
{http.StatusTooManyRequests, nil}, // a *RetryAfterError
} {
c, srv := newTestClient(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Retry-After", "60")
w.WriteHeader(tc.status)
})
err := call(c)
srv.Close()
if tc.want != nil {
if !errors.Is(err, tc.want) {
t.Errorf("%s, HTTP %d: err = %v, want %v", name, tc.status, err, tc.want)
}
continue
}
var ra *RetryAfterError
if !errors.As(err, &ra) || ra.Wait != 60*time.Second {
t.Errorf("%s, HTTP %d: err = %v, want a 60s RetryAfterError", name, tc.status, err)
}
}
}
}
func TestClient_SimilarRecordings_AlgorithmParamSet(t *testing.T) {
var seenURL string
c, srv := newTestClient(func(w http.ResponseWriter, r *http.Request) {
seenURL = r.URL.String()
_, _ = w.Write([]byte(`[]`))
})
defer srv.Close()
_, _ = c.SimilarRecordings(context.Background(), "abc", 50)
if !strings.Contains(seenURL, "algorithm=") {
t.Errorf("URL missing algorithm param: %q", seenURL)
}
}
func TestClient_SimilarRecordings_MbidParamSet(t *testing.T) {
var seenURL string
c, srv := newTestClient(func(w http.ResponseWriter, r *http.Request) {
seenURL = r.URL.String()
_, _ = w.Write([]byte(`[]`))
})
defer srv.Close()
_, _ = c.SimilarRecordings(context.Background(), "abc", 50)
// Labs API takes the MBID as a query param, not a path segment; the
// result count is fixed by the algorithm (no count/limit param).
if !strings.Contains(seenURL, "recording_mbids=abc") {
t.Errorf("URL missing recording_mbids param: %q", seenURL)
}
if !strings.Contains(seenURL, "/similar-recordings/json") {
t.Errorf("URL not the Labs similar-recordings endpoint: %q", seenURL)
}
}
func TestClient_SimilarRecordings_401_ReturnsErrAuth(t *testing.T) {
c, srv := newTestClient(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusUnauthorized)
})
defer srv.Close()
_, err := c.SimilarRecordings(context.Background(), "abc", 100)
if !errors.Is(err, ErrAuth) {
t.Errorf("err = %v, want ErrAuth", err)
}
}
func TestClient_SimilarRecordings_400_ReturnsErrPermanent(t *testing.T) {
c, srv := newTestClient(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusBadRequest)
})
defer srv.Close()
_, err := c.SimilarRecordings(context.Background(), "abc", 100)
if !errors.Is(err, ErrPermanent) {
t.Errorf("err = %v, want ErrPermanent", err)
}
}
func TestClient_SimilarRecordings_503_ReturnsErrTransient(t *testing.T) {
c, srv := newTestClient(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusServiceUnavailable)
})
defer srv.Close()
_, err := c.SimilarRecordings(context.Background(), "abc", 100)
if !errors.Is(err, ErrTransient) {
t.Errorf("err = %v, want ErrTransient", err)
}
}
func TestClient_SimilarRecordings_429_ReturnsRetryAfter(t *testing.T) {
c, srv := newTestClient(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Retry-After", "120")
w.WriteHeader(http.StatusTooManyRequests)
})
defer srv.Close()
_, err := c.SimilarRecordings(context.Background(), "abc", 100)
var ra *RetryAfterError
if !errors.As(err, &ra) {
t.Fatalf("err = %v, want *RetryAfterError", err)
}
if ra.Wait != 120*time.Second {
t.Errorf("Wait = %v, want 120s", ra.Wait)
}
}
func TestClient_SimilarArtists_Success(t *testing.T) {
c, srv := newTestClient(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(`[
{"artist_mbid": "33333333-3333-3333-3333-333333333333", "score": 0.91},
{"artist_mbid": "44444444-4444-4444-4444-444444444444", "score": 0.72}
]`))
})
defer srv.Close()
out, err := c.SimilarArtists(context.Background(), "aaaa", 100)
if err != nil {
t.Fatalf("err: %v", err)
}
if len(out) != 2 {
t.Fatalf("got %d, want 2", len(out))
}
if out[0].MBID != "33333333-3333-3333-3333-333333333333" || out[0].Score != 0.91 {
t.Errorf("first result wrong: %+v", out[0])
}
}
func TestClient_SimilarArtists_AlgorithmParamSet(t *testing.T) {
var seenURL string
c, srv := newTestClient(func(w http.ResponseWriter, r *http.Request) {
seenURL = r.URL.String()
_, _ = w.Write([]byte(`[]`))
})
defer srv.Close()
_, _ = c.SimilarArtists(context.Background(), "abc", 50)
if !strings.Contains(seenURL, "algorithm=") {
t.Errorf("URL missing algorithm param: %q", seenURL)
}
}
func TestClient_SimilarArtists_MbidParamSet(t *testing.T) {
var seenURL string
c, srv := newTestClient(func(w http.ResponseWriter, r *http.Request) {
seenURL = r.URL.String()
_, _ = w.Write([]byte(`[]`))
})
defer srv.Close()
_, _ = c.SimilarArtists(context.Background(), "abc", 50)
if !strings.Contains(seenURL, "artist_mbids=abc") {
t.Errorf("URL missing artist_mbids param: %q", seenURL)
}
if !strings.Contains(seenURL, "/similar-artists/json") {
t.Errorf("URL not the Labs similar-artists endpoint: %q", seenURL)
}
}
func TestClient_SimilarArtists_401_ReturnsErrAuth(t *testing.T) {
c, srv := newTestClient(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusUnauthorized)
})
defer srv.Close()
_, err := c.SimilarArtists(context.Background(), "abc", 100)
if !errors.Is(err, ErrAuth) {
t.Errorf("err = %v, want ErrAuth", err)
}
}
func TestClient_SimilarArtists_400_ReturnsErrPermanent(t *testing.T) {
c, srv := newTestClient(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusBadRequest)
})
defer srv.Close()
_, err := c.SimilarArtists(context.Background(), "abc", 100)
if !errors.Is(err, ErrPermanent) {
t.Errorf("err = %v, want ErrPermanent", err)
}
}
func TestClient_SimilarArtists_503_ReturnsErrTransient(t *testing.T) {
c, srv := newTestClient(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusServiceUnavailable)
})
defer srv.Close()
_, err := c.SimilarArtists(context.Background(), "abc", 100)
if !errors.Is(err, ErrTransient) {
t.Errorf("err = %v, want ErrTransient", err)
}
}
func TestClient_SimilarArtists_429_ReturnsRetryAfter(t *testing.T) {
c, srv := newTestClient(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Retry-After", "60")
w.WriteHeader(http.StatusTooManyRequests)
})
defer srv.Close()
_, err := c.SimilarArtists(context.Background(), "abc", 100)
var ra *RetryAfterError
if !errors.As(err, &ra) {
t.Fatalf("err = %v, want *RetryAfterError", err)
}
if ra.Wait != 60*time.Second {
t.Errorf("Wait = %v, want 60s", ra.Wait)
// The similarity lookups ask ListenBrainz Labs for one MBID by query
// parameter, name the algorithm, and read each result's MBID and score.
func TestClient_SimilarLookupsAskLabsAndReadTheScores(t *testing.T) {
type first struct {
mbid string
score float64
n int
}
for _, tc := range []struct {
name, path, param, body string
call func(*Client) (first, error)
want first
}{
{"recordings", "/similar-recordings/json", "recording_mbids=abc",
`[{"recording_mbid": "11111111-1111-1111-1111-111111111111", "score": 0.95},
{"recording_mbid": "22222222-2222-2222-2222-222222222222", "score": 0.80}]`,
func(c *Client) (first, error) {
out, err := c.SimilarRecordings(context.Background(), "abc", 100)
if err != nil || len(out) == 0 {
return first{n: len(out)}, err
}
return first{out[0].MBID, out[0].Score, len(out)}, nil
},
first{"11111111-1111-1111-1111-111111111111", 0.95, 2}},
{"artists", "/similar-artists/json", "artist_mbids=abc",
`[{"artist_mbid": "33333333-3333-3333-3333-333333333333", "score": 0.91},
{"artist_mbid": "44444444-4444-4444-4444-444444444444", "score": 0.72}]`,
func(c *Client) (first, error) {
out, err := c.SimilarArtists(context.Background(), "abc", 100)
if err != nil || len(out) == 0 {
return first{n: len(out)}, err
}
return first{out[0].MBID, out[0].Score, len(out)}, nil
},
first{"33333333-3333-3333-3333-333333333333", 0.91, 2}},
} {
var seenURL string
c, srv := newTestClient(func(w http.ResponseWriter, r *http.Request) {
seenURL = r.URL.String()
_, _ = w.Write([]byte(tc.body))
})
got, err := tc.call(c)
srv.Close()
if err != nil || got != tc.want {
t.Errorf("%s: got %+v, %v; want %+v", tc.name, got, err, tc.want)
}
for _, part := range []string{tc.path, tc.param, "algorithm="} {
if !strings.Contains(seenURL, part) {
t.Errorf("%s: URL %q lacks %q", tc.name, seenURL, part)
}
}
}
}
+19 -36
View File
@@ -2,41 +2,24 @@ package scrobble
import "testing"
func TestQualifies_NeverPlayed(t *testing.T) {
if Qualifies(0, 0) {
t.Error("0/0 should not qualify")
}
}
func TestQualifies_AtLeast240Seconds(t *testing.T) {
if !Qualifies(240_000, 0.0) {
t.Error("240_000 ms played should qualify regardless of ratio")
}
}
func TestQualifies_AtLeastHalfRatio(t *testing.T) {
if !Qualifies(0, 0.5) {
t.Error("ratio=0.5 should qualify regardless of duration")
}
}
func TestQualifies_BelowBoth(t *testing.T) {
if Qualifies(120_000, 0.49) {
t.Error("120s + 49% should not qualify")
}
}
func TestQualifies_BoundaryJustUnder(t *testing.T) {
if Qualifies(239_999, 0.4999) {
t.Error("just-under both thresholds should not qualify")
}
}
func TestQualifies_BoundaryExactly(t *testing.T) {
if !Qualifies(240_000, 0.4999) {
t.Error("exactly 240s should qualify")
}
if !Qualifies(239_999, 0.5) {
t.Error("exactly 50% should qualify")
// A play is scrobbled once it ran four minutes or half the track, whichever
// comes first, the rule ListenBrainz and Last.fm both use.
func TestQualifies(t *testing.T) {
for _, tc := range []struct {
playedMs int
ratio float64
want bool
}{
{0, 0, false},
{240_000, 0, true},
{0, 0.5, true},
{120_000, 0.49, false},
{239_999, 0.4999, false},
{240_000, 0.4999, true},
{239_999, 0.5, true},
} {
if got := Qualifies(tc.playedMs, tc.ratio); got != tc.want {
t.Errorf("Qualifies(%d ms, %.4f) = %v, want %v", tc.playedMs, tc.ratio, got, tc.want)
}
}
}