package api import ( "context" "encoding/json" "net/http" "net/http/httptest" "os" "path/filepath" "testing" "github.com/jackc/pgx/v5/pgxpool" ) // getArt fetches path and returns the image body, or "" with the status // when the answer is not an image. func getArt(t *testing.T, h *handlers, path string) (string, int) { t.Helper() w := httptest.NewRecorder() newLibraryRouter(h).ServeHTTP(w, httptest.NewRequest(http.MethodGet, path, nil)) if w.Code != http.StatusOK { return "", w.Code } return w.Body.String(), w.Code } // coverURLs returns the cover_url of every artist in the alphabetical // listing, by name. func coverURLs(t *testing.T, h *handlers) map[string]string { t.Helper() w := httptest.NewRecorder() newLibraryRouter(h).ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/api/artists", nil)) var got Page[ArtistRef] if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil { t.Fatalf("decode artists: %v body=%s", err, w.Body.String()) } out := map[string]string{} for _, a := range got.Items { out[a.Name] = a.CoverURL } return out } func detailCoverURL(t *testing.T, h *handlers, id string) string { t.Helper() w := httptest.NewRecorder() newLibraryRouter(h).ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/api/artists/"+id, nil)) var got ArtistDetail if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil { t.Fatalf("decode artist: %v body=%s", err, w.Body.String()) } return got.CoverURL } func exec(t *testing.T, pool *pgxpool.Pool, sql string, args ...any) { t.Helper() if _, err := pool.Exec(context.Background(), sql, args...); err != nil { t.Fatalf("%s: %v", sql, err) } } // An artist's image is the picture fetched for them, or else their newest // album cover; artist listings point at it when there is a picture, and the // artist page whenever the artist has albums. func TestArtistImage_PictureOrNewestAlbumCover(t *testing.T) { h, pool := testHandlers(t) truncateLibrary(t, pool) 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 } orchard := seedArtist(t, pool, "Tin Orchard") album := seedAlbum(t, pool, orchard.ID, "Paper Boats", 2012) exec(t, pool, `UPDATE albums SET cover_art_path = $2 WHERE id = $1`, album.ID, write("boats.jpg", "boats")) quiet := seedArtist(t, pool, "Quiet Field") orchardImage := "/api/artists/" + uuidToString(orchard.ID) + "/image" albumCover := "/api/albums/" + uuidToString(album.ID) + "/cover" if got, code := getArt(t, h, orchardImage); got != "boats" { t.Errorf("no picture: got %q (status %d), want the album cover", got, code) } if got := coverURLs(t, h); got["Tin Orchard"] != albumCover || got["Quiet Field"] != "" { t.Errorf("no picture: listing cover_url = %v, want the album cover for Tin Orchard and none for Quiet Field", got) } exec(t, pool, `UPDATE artists SET artist_thumb_path = $2 WHERE id = $1`, orchard.ID, write("thumb.jpg", "thumb")) if got, code := getArt(t, h, orchardImage); got != "thumb" { t.Errorf("with a picture: got %q (status %d), want the picture", got, code) } if got := coverURLs(t, h); got["Tin Orchard"] != orchardImage { t.Errorf("with a picture: listing cover_url = %q, want %q", got["Tin Orchard"], orchardImage) } if got := detailCoverURL(t, h, uuidToString(orchard.ID)); got != orchardImage { t.Errorf("artist page cover_url = %q, want %q", got, orchardImage) } if got := detailCoverURL(t, h, uuidToString(quiet.ID)); got != "" { t.Errorf("artist page with no albums: cover_url = %q, want none", got) } if _, code := getArt(t, h, "/api/artists/"+uuidToString(quiet.ID)+"/image"); code != http.StatusNotFound { t.Errorf("no picture and no albums: status %d, want 404", code) } }