sonda/internal/api/http/playlist_covers_test.go
tgiachi a1e192066c feat: playlists with a cover, a collage of the covers of their albums
- A playlist says which albums stand for it (album_ids): up to 4 albums that have a cover, in the order they first appear in it, each once, read for a whole page of playlists in one query. An empty one has an empty list.
- The Playlists page is a grid of cards with the collage, and the page of a playlist shows it in its header. The pages of a smart playlist and of a suggested one show a collage of the albums of their first songs.
- Nothing is fetched for it: the covers are the ones the library already has.
2026-11-13 11:32:01 +01:00

57 lines
1.9 KiB
Go

package httpapi
import (
"fmt"
"net/http"
"slices"
"testing"
)
func TestAPlaylistSaysWhichAlbumsStandForIt(t *testing.T) {
m := newMixAPI(t)
id := m.playlistWithSongs(t, "hard song", 6) // six songs by six artists: six albums
empty := do(t, m.h, "POST", "/api/v1/playlists", m.alice, map[string]any{"name": "Empty"})
if ids, ok := empty.Body["album_ids"].([]any); !ok || len(ids) != 0 {
t.Fatalf("an empty playlist = %v, want an empty list", empty.Body["album_ids"])
}
var albums []int64
rows, err := m.db.Query(`SELECT t.album_id FROM tracks t JOIN playlist_items i ON i.track_id = t.id WHERE i.playlist_id = ? ORDER BY i.position`, id)
if err != nil {
t.Fatal(err)
}
for rows.Next() {
var a int64
if err := rows.Scan(&a); err != nil {
t.Fatal(err)
}
albums = append(albums, a)
}
rows.Close()
if len(albums) != 6 {
t.Fatalf("albums = %v", albums)
}
// The second, third, fifth and sixth have a cover; the first and the fourth do not.
for _, i := range []int{1, 2, 4, 5} {
if _, err := m.db.Exec("DELETE FROM images WHERE kind = 'album' AND owner_id = ?", albums[i]); err != nil {
t.Fatal(err)
}
if _, err := m.db.Exec("INSERT INTO images (kind, owner_id, status, checked_at) VALUES ('album', ?, 'ok', 1)", albums[i]); err != nil {
t.Fatal(err)
}
}
want := []any{float64(albums[1]), float64(albums[2]), float64(albums[4]), float64(albums[5])}
one := do(t, m.h, "GET", fmt.Sprintf("/api/v1/playlists/%d", id), m.alice, nil)
if one.Code != http.StatusOK || !slices.Equal(one.Body["album_ids"].([]any), want) {
t.Errorf("get = %d %v, want %v", one.Code, one.Body["album_ids"], want)
}
list := do(t, m.h, "GET", "/api/v1/playlists", m.alice, nil)
var got []any
for _, x := range list.Body["items"].([]any) {
if x.(map[string]any)["id"] == float64(id) {
got = x.(map[string]any)["album_ids"].([]any)
}
}
if !slices.Equal(got, want) {
t.Errorf("list = %v, want %v", got, want)
}
}