Files
minstrel/internal/musicbrainz/client_test.go
T
bvandeusenandClaude Opus 5.5 14aa3dcd5e chore: remove code left from acquisition and Discover suggestions, and uncalled queries
Nothing reached any of it:
- MusicBrainz search (artists, albums, songs), artist lookups and the
  query escaping they used. The release-group lookups the duplicate
  resolver uses stay.
- Release-group covers, and artist-level tags for artists not in the
  library.
- Nine named queries with no caller (CountTracksByArtist went with the
  previous commit), two of them describing request-progress reporting.

The MusicBrainz client's retry and not-found tests now drive the
release-group lookup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 22:02:00 -05:00

109 lines
3.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package musicbrainz
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"time"
)
// testClient talks to srv with no pacing and fast retries.
func testClient(srv *httptest.Server) *Client {
return &Client{BaseURL: srv.URL, Pace: &Pacer{}, Backoff: time.Millisecond}
}
func TestReleaseGroupReleases_ReadsMediaAndTracks(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("inc") != "media recordings" {
t.Errorf("inc = %q", r.URL.Query().Get("inc"))
}
_, _ = w.Write([]byte(`{"release-count": 1, "releases": [{
"id": "r1", "title": "OK Calculator", "status": "Official", "date": "1997-06-16", "country": "GB",
"media": [
{"position": 1, "format": "CD", "tracks": [
{"position": 1, "number": "1", "title": "Lifebelt", "length": 284000, "recording": {"id": "rec-a"}},
{"position": 2, "number": "2", "title": "Anxious Robot", "length": null, "recording": {"id": "rec-b", "length": 383000}}
]},
{"position": 2, "format": "CD", "tracks": []}
]
}]}`))
}))
defer srv.Close()
got, err := testClient(srv).ReleaseGroupReleases(context.Background(), "rg1")
if err != nil {
t.Fatal(err)
}
if len(got) != 1 {
t.Fatalf("got %+v", got)
}
r := got[0]
if r.TrackCount() != 2 || r.Formats() != "2×CD" {
t.Errorf("tracks %d, formats %q", r.TrackCount(), r.Formats())
}
if tr := r.Media[0].Tracks[1]; tr.LengthMs != 383000 || tr.RecordingMBID != "rec-b" {
t.Errorf("a track with no length of its own should take the recording's: %+v", tr)
}
}
func TestGet_ThrottleIsRetriedThenUnreachable(t *testing.T) {
var calls atomic.Int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
if calls.Add(1) == 1 {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
_, _ = w.Write([]byte(`{"id": "rg1", "title": "Paper Boats"}`))
}))
defer srv.Close()
got, err := testClient(srv).ReleaseGroup(context.Background(), "rg1")
if err != nil || got.Title != "Paper Boats" || calls.Load() != 2 {
t.Fatalf("a throttled first answer should be retried: %v, %+v, %d calls", err, got, calls.Load())
}
down := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusBadGateway)
}))
defer down.Close()
if _, err := testClient(down).ReleaseGroup(context.Background(), "rg1"); !errors.Is(err, ErrUnreachable) {
t.Errorf("err = %v, want ErrUnreachable", err)
}
}
func TestGet_NotFoundAndNoServer(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNotFound)
}))
if _, err := testClient(srv).ReleaseGroup(context.Background(), "nope"); !errors.Is(err, ErrNotFound) {
t.Errorf("err = %v, want ErrNotFound", err)
}
srv.Close()
c := testClient(srv)
c.Retries = -1
if _, err := c.ReleaseGroup(context.Background(), "rg1"); !errors.Is(err, ErrUnreachable) {
t.Errorf("a closed server: err = %v, want ErrUnreachable", err)
}
}
func TestPacer_SpacesCallers(t *testing.T) {
p := &Pacer{Interval: 30 * time.Millisecond}
start := time.Now()
for range 3 {
if err := p.Wait(context.Background()); err != nil {
t.Fatal(err)
}
}
if elapsed := time.Since(start); elapsed < 60*time.Millisecond {
t.Errorf("three waits took %v, want at least two intervals", elapsed)
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
if err := p.Wait(ctx); err == nil {
t.Error("a cancelled wait should return the context's error")
}
}