package subsonic import ( "context" "encoding/json" "io" "log/slog" "net/http" "net/http/httptest" "os" "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" ) // A compilation's songs are credited to their own performers, as Subsonic // clients show them, not all to Various Artists. func TestGetAlbum_EachSongCreditsItsOwnArtist(t *testing.T) { if testing.Short() { t.Skip("skipping 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) ctx := context.Background() q := dbq.New(pool) artist := func(name string) dbq.Artist { a, err := q.UpsertArtist(ctx, dbq.UpsertArtistParams{Name: name, SortName: name}) if err != nil { t.Fatalf("artist %s: %v", name, err) } return a } various, first, guest := artist("Various Artists"), artist("Harbour Choir"), artist("Ruby Lane") album, err := q.UpsertAlbum(ctx, dbq.UpsertAlbumParams{Title: "Sunday Morning Static", SortTitle: "Sunday Morning Static", ArtistID: various.ID}) if err != nil { t.Fatalf("album: %v", err) } n1, n2 := int32(1), int32(2) for _, tr := range []dbq.UpsertTrackParams{ {Title: "Low Light", AlbumID: album.ID, ArtistID: first.ID, TrackNumber: &n1, FilePath: "/tmp/getalbum-1.flac", DurationMs: 100_000}, {Title: "Saltlines", AlbumID: album.ID, ArtistID: guest.ID, TrackNumber: &n2, FilePath: "/tmp/getalbum-2.flac", DurationMs: 100_000}, } { if _, err := q.UpsertTrack(ctx, tr); err != nil { t.Fatalf("track %s: %v", tr.Title, err) } } b := &browseHandlers{pool: pool} req := httptest.NewRequest(http.MethodGet, "/rest/getAlbum?f=json&id="+uuidToID(album.ID), nil) w := httptest.NewRecorder() b.getAlbum(w, req) if w.Code != http.StatusOK { t.Fatalf("status = %d body=%s", w.Code, w.Body.String()) } var raw struct { Response struct { Album AlbumDetail `json:"album"` } `json:"subsonic-response"` } if err := json.Unmarshal(w.Body.Bytes(), &raw); err != nil { t.Fatalf("decode: %v body=%s", err, w.Body.String()) } got := raw.Response.Album if got.Artist != "Various Artists" { t.Errorf("album artist = %q, want Various Artists", got.Artist) } if len(got.Songs) != 2 || got.Songs[0].Artist != "Harbour Choir" || got.Songs[1].Artist != "Ruby Lane" { t.Errorf("songs = %+v, want each song credited to its own artist", got.Songs) } }