Files
minstrel/internal/subsonic/artistart_test.go
T
bvandeusenandClaude Opus 5.5 1965e82013
release / go (push) Successful in 1m47s
release / web (push) Successful in 1m10s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 4m3s
release / android (push) Successful in 4m27s
release / Build signed APK (push) Successful in 5m2s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 17s
release / Verify release artifacts (tag releases only) (push) Skipped
feat(subsonic): artist pictures in Subsonic apps
getCoverArt now answers an artist's id: the artist picture Minstrel
fetched, when its file is there, or else the cover of the artist's most
recently added album that has one, which is what Minstrel's own artist
cards show. Every artist entry carries its id as coverArt, so apps ask.

The lookup is coverart.ResolveArtistPath, beside the album one, and looks
through at most ten of an artist's albums for a cover.

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

85 lines
3.3 KiB
Go

package subsonic
import (
"context"
"encoding/json"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
)
// coverArt fetches getCoverArt for id and returns the image bytes, or the
// Subsonic error code when it answered with one.
func (f picksFixture) coverArt(t *testing.T, id string) (string, int) {
t.Helper()
m := newMediaHandlers(f.pool, nil, slog.New(slog.NewTextHandler(io.Discard, nil)))
w := httptest.NewRecorder()
m.handleGetCoverArt(w, httptest.NewRequest(http.MethodGet, "/rest/getCoverArt?f=json&id="+id, nil).
WithContext(context.WithValue(context.Background(), userCtxKey, f.user)))
if w.Header().Get("Content-Type") == "image/jpeg" {
return w.Body.String(), 0
}
var env struct {
Response struct {
Error *Error `json:"error"`
} `json:"subsonic-response"`
}
if err := json.Unmarshal(w.Body.Bytes(), &env); err != nil || env.Response.Error == nil {
t.Fatalf("getCoverArt %s: neither an image nor an error: %s", id, w.Body.String())
}
return "", env.Response.Error.Code
}
// An artist's cover art is their own picture when one was fetched, and
// otherwise the cover of their most recently added album that has one.
func TestGetCoverArt_AnArtistsPictureOrTheirNewestAlbumCover(t *testing.T) {
f := seedPicks(t)
dir := t.TempDir()
write := func(name, body string) string {
path := filepath.Join(dir, name)
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
t.Fatal(err)
}
return path
}
artist := uuidToID(f.artist.ID)
// Every song sits in a folder with no cover.jpg or folder.jpg, so an
// album has a cover only when it is given one below; Gone Tide never is.
f.exec(t, `UPDATE tracks SET file_path = $1 || '/songs/' || title || '.flac'`, dir)
if _, code := f.coverArt(t, artist); code != ErrDataNotFound {
t.Errorf("no art anywhere: code %d, want %d", code, ErrDataNotFound)
}
f.exec(t, `UPDATE albums SET created_at = '2020-01-01', cover_art_path = $2 WHERE id = $1`, f.albums["Lantern Hours"].ID, write("old.jpg", "old"))
f.exec(t, `UPDATE albums SET created_at = '2024-01-01', cover_art_path = $2 WHERE id = $1`, f.albums["Paper Boats"].ID, write("new.jpg", "new"))
f.exec(t, `UPDATE albums SET created_at = '2025-01-01' WHERE id = $1`, f.albums["Gone Tide"].ID)
if got, code := f.coverArt(t, artist); got != "new" {
t.Errorf("no artist picture: got %q (code %d), want the newest album cover", got, code)
}
f.exec(t, `UPDATE artists SET artist_thumb_path = $2 WHERE id = $1`, f.artist.ID, write("thumb.jpg", "thumb"))
if got, code := f.coverArt(t, artist); got != "thumb" {
t.Errorf("with an artist picture: got %q (code %d), want the picture", got, code)
}
f.exec(t, `UPDATE artists SET artist_thumb_path = $2 WHERE id = $1`, f.artist.ID, filepath.Join(dir, "gone.jpg"))
if got, code := f.coverArt(t, artist); got != "new" {
t.Errorf("a picture whose file is gone: got %q (code %d), want the newest album cover", got, code)
}
// Albums and songs still answer with the album's own cover.
for id, want := range map[string]string{
uuidToID(f.albums["Lantern Hours"].ID): "old",
uuidToID(f.tracks["Dusk"].ID): "old",
uuidToID(f.tracks["Gale"].ID): "new",
} {
if got, code := f.coverArt(t, id); got != want {
t.Errorf("cover art %s: got %q (code %d), want %q", id, got, code, want)
}
}
}