package catalogue import ( "context" "errors" "fmt" "net/http" "net/http/httptest" "sync/atomic" "testing" "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" "git.fabledsword.com/bvandeusen/minstrel/internal/dbtest" "git.fabledsword.com/bvandeusen/minstrel/internal/musicbrainz" ) const ( apes = "0a11ce00-0000-4000-8000-000000000001" ghostNights = "0a11ce00-0000-4000-8000-000000000002" ghostNightsR = "1a2b3c4d-0000-4000-8000-000000000001" ghostNightsJ = "1a2b3c4d-0000-4000-8000-000000000002" ) // fakeMB is a MusicBrainz with one album in two editions. down makes every // request a 503. type fakeMB struct { srv *httptest.Server calls atomic.Int32 down atomic.Bool } func newFakeMB(t *testing.T) *fakeMB { t.Helper() f := &fakeMB{} f.srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { f.calls.Add(1) if f.down.Load() { w.WriteHeader(http.StatusServiceUnavailable) return } q := r.URL.Query() switch { case r.URL.Path == "/release-group/"+ghostNights: _, _ = fmt.Fprintf(w, `{"id": %q, "title": "Ghost Nights", "primary-type": "Album", "first-release-date": "2005-05-11", "artist-credit": [{"name": "Plastic Apes", "artist": {"id": %q}}]}`, ghostNights, apes) case r.URL.Path == "/release" && q.Get("release-group") == ghostNights: _, _ = fmt.Fprintf(w, `{"release-count": 2, "releases": [ {"id": %q, "title": "Ghost Nights", "status": "Promotion", "date": "2005-04", "country": "JP", "media": [{"position": 1, "format": "CD", "tracks": [ {"position": 1, "number": "1", "title": "Intro", "length": 63000, "recording": {"id": "rec-intro"}}]}]}, {"id": %q, "title": "Ghost Nights", "status": "Official", "date": "2005-05-11", "country": "GB", "media": [{"position": 1, "format": "CD", "tracks": [ {"position": 1, "number": "1", "title": "Intro", "length": 63000, "recording": {"id": "rec-intro"}}, {"position": 2, "number": "2", "title": "Last Living Souls", "length": 190000, "recording": {"id": "rec-lls"}}]}]} ]}`, ghostNightsJ, ghostNightsR) default: w.WriteHeader(http.StatusNotFound) } })) t.Cleanup(f.srv.Close) return f } func (f *fakeMB) client() *musicbrainz.Client { return &musicbrainz.Client{BaseURL: f.srv.URL, Pace: &musicbrainz.Pacer{}, Retries: -1} } func TestRefreshReleaseGroup_ReadsEditionsAndTrackLists_Integration(t *testing.T) { ctx := context.Background() mb := newFakeMB(t) pool := dbtest.Pool(t) s := New(pool, mb.client(), nil) q := dbq.New(pool) // The group isn't held yet: it is looked up first. if err := s.RefreshReleaseGroup(ctx, ghostNights); err != nil { t.Fatal(err) } group, err := q.GetMBReleaseGroup(ctx, ghostNights) if err != nil { t.Fatal(err) } if group.Title != "Ghost Nights" || !group.ReleasesRefreshedAt.Valid { t.Fatalf("group = %+v", group) } tracks, err := q.ListMBReleaseTracksForGroup(ctx, ghostNights) if err != nil { t.Fatal(err) } if len(tracks) != 3 { t.Fatalf("want 3 tracks across both editions, got %+v", tracks) } var lls *dbq.MbReleaseTrack for i := range tracks { if tracks[i].RecordingMbid == "rec-lls" { lls = &tracks[i] } } if lls == nil || lls.ReleaseMbid != ghostNightsR || lls.LengthMs != 190000 { t.Errorf("Last Living Souls = %+v", lls) } // Refreshing replaces rather than adds. if err := s.RefreshReleaseGroup(ctx, ghostNights); err != nil { t.Fatal(err) } if again, _ := q.ListMBReleaseTracksForGroup(ctx, ghostNights); len(again) != 3 { t.Errorf("after a refresh: %d tracks, want 3", len(again)) } } func TestRefreshReleaseGroup_UnknownAndUnreachable_Integration(t *testing.T) { ctx := context.Background() mb := newFakeMB(t) s := New(dbtest.Pool(t), mb.client(), nil) if err := s.RefreshReleaseGroup(ctx, "00000000-0000-4000-8000-000000000000"); !errors.Is(err, ErrNotFound) { t.Errorf("unknown group: err = %v, want ErrNotFound", err) } mb.down.Store(true) if err := s.RefreshReleaseGroup(ctx, ghostNights); !errors.Is(err, musicbrainz.ErrUnreachable) { t.Errorf("MusicBrainz down: err = %v, want ErrUnreachable", err) } }