release / web (push) Failing after 1m16s
release / go (push) Successful in 1m48s
release / govulncheck (push) Successful in 20s
release / integration (push) Successful in 4m0s
release / android (push) Successful in 4m34s
release / Build signed APK (push) Successful in 5m9s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Skipped
release / Verify release artifacts (tag releases only) (push) Skipped
Test data and example comments named real artists, records and songs, and carried real MusicBrainz ids. One mapping swaps them for invented ones throughout (Fields of Ontario, Plastic Apes, Marlo Davies, ...), keeping what each case tests: sort order, index letter, escaping, title keys, artist-prefix stripping. A test username is no longer the operator's name. The cover-art and Last.fm connection checks still query real releases; they reach live services and need something that exists there. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
152 lines
4.7 KiB
Go
152 lines
4.7 KiB
Go
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 (
|
|
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 := 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, 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(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, ghostNights); !errors.Is(err, musicbrainz.ErrUnreachable) {
|
|
t.Errorf("MusicBrainz down: err = %v, want ErrUnreachable", err)
|
|
}
|
|
}
|