package catalogue import ( "context" "errors" "fmt" "io" "log/slog" "net/http" "net/http/httptest" "os" "sync/atomic" "testing" "github.com/jackc/pgx/v5/pgxpool" "git.fabledsword.com/bvandeusen/minstrel/internal/db" "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" "git.fabledsword.com/bvandeusen/minstrel/internal/dbtest" "git.fabledsword.com/bvandeusen/minstrel/internal/musicbrainz" ) func newPool(t *testing.T) *pgxpool.Pool { t.Helper() if testing.Short() { t.Skip("skipping integration test in -short mode") } dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL") if dsn == "" { t.Skip("MINSTREL_TEST_DATABASE_URL not set") } if err := db.Migrate(dsn, slog.New(slog.NewTextHandler(io.Discard, nil))); err != nil { t.Fatalf("migrate: %v", err) } pool, err := pgxpool.New(context.Background(), dsn) if err != nil { t.Fatalf("pool: %v", err) } t.Cleanup(pool.Close) dbtest.ResetDB(t, pool) return pool } const ( gorillaz = "e21857d5-3256-4547-afb3-4b6ded592596" demonDays = "0c4d5b5f-2f3c-3d31-9c4e-4b4f6b4c1a01" demonDaysR = "1a2b3c4d-0000-4000-8000-000000000001" demonDaysJ = "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/"+demonDays: _, _ = fmt.Fprintf(w, `{"id": %q, "title": "Demon Days", "primary-type": "Album", "first-release-date": "2005-05-11", "artist-credit": [{"name": "Gorillaz", "artist": {"id": %q}}]}`, demonDays, gorillaz) case r.URL.Path == "/release" && q.Get("release-group") == demonDays: _, _ = fmt.Fprintf(w, `{"release-count": 2, "releases": [ {"id": %q, "title": "Demon Days", "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": "Demon Days", "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"}}]}]} ]}`, demonDaysJ, demonDaysR) 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 := newPool(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, demonDays); err != nil { t.Fatal(err) } group, err := q.GetMBReleaseGroup(ctx, demonDays) if err != nil { t.Fatal(err) } if group.Title != "Demon Days" || !group.ReleasesRefreshedAt.Valid { t.Fatalf("group = %+v", group) } tracks, err := q.ListMBReleaseTracksForGroup(ctx, demonDays) 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 != demonDaysR || lls.LengthMs != 190000 { t.Errorf("Last Living Souls = %+v", lls) } // Refreshing replaces rather than adds. if err := s.RefreshReleaseGroup(ctx, demonDays); err != nil { t.Fatal(err) } if again, _ := q.ListMBReleaseTracksForGroup(ctx, demonDays); 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(newPool(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, demonDays); !errors.Is(err, musicbrainz.ErrUnreachable) { t.Errorf("MusicBrainz down: err = %v, want ErrUnreachable", err) } }