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) } } }