Files
yellowjacket/backend/home/home_test.go
T
logan 862e8a0468
Build & publish Arch package / arch-package (push) Successful in 2m3s
CI / check (push) Canceled after 10s
CI / e2e (push) Canceled after 0s
Search index maintenance / maintain-index (push) Canceled after 0s
feat(home): land on Home, and make it worth landing on
The app opened on Tracks — an alphabetical list of everything, which is
the one entry point that is identical every time and therefore gives the
user nothing to start from. Home is listed first in the nav and is the
page built to answer 'what should I play' (H-8).

Two things had to be true before that was an improvement.

An album with no cover rendered as a small dim icon on a surface the
same colour as the page, so a shelf read as having holes in it, while
the Albums and Artists grids both drew a letter tile (H-9). It draws the
same tile now.

And a shelf that repeats the one above it is suppressed, the way an
empty one already is — 'On repeat' was 'Pick up where you left off'
reordered. The rule fires only when the shelf is not showing the whole
library: a repeat is a fault only if a different row was possible, and
measured against a fixed shelf size instead this let an 11-album library
keep three identical shelves while a 13-album one lost them.

The first two versions of that rule were wrong and the *existing* Go
tests caught both — it collapsed a four-album library to a single shelf.

Nine e2e specs assumed the app starts on Tracks and now navigate there,
and one new spec freezes the landing itself. Home's page-header action
is 'Shuffle suggestions': 'Shuffle' alone was two different controls
with one accessible name, which only became reachable together once a
cached Home was always in the tree.
2026-08-12 11:42:26 -04:00

339 lines
9.4 KiB
Go

package home_test
import (
"log/slog"
"testing"
"yellowjacket/backend/database"
"yellowjacket/backend/home"
"yellowjacket/backend/library"
)
// fakeLibrary answers the one question the home service asks of the
// library package, so these tests are about shelf selection rather than
// about album rendering.
type fakeLibrary struct {
albums []library.Album
err error
}
func (f fakeLibrary) GetAllAlbums() ([]library.Album, error) {
return f.albums, f.err
}
// seed inserts one album with one played-or-not track, returning the
// release group id. Written with raw SQL rather than the library
// scanner because the shelves are queries, and a query is best tested
// against rows it can be given precisely.
func seed(
t *testing.T,
db *database.DB,
name, artist, genre string,
playCount int,
lastPlayed string,
) int64 {
t.Helper()
exec := func(query string, args ...any) {
t.Helper()
if _, err := db.ExecContext(query, args...); err != nil {
t.Fatalf("seed %q: %v", query, err)
}
}
exec(`INSERT INTO artist_credit (text) VALUES (?)
ON CONFLICT DO NOTHING`, artist)
exec(`INSERT INTO release_groups (name, album_artist_credit_id)
VALUES (?, (SELECT id FROM artist_credit WHERE text = ?))`,
name, artist)
exec(`INSERT INTO recordings (name, artist_credit_id)
VALUES (?, (SELECT id FROM artist_credit WHERE text = ?))`,
name+" track", artist)
exec(`INSERT INTO release_group_recordings (release_group_id, recording_id)
VALUES ((SELECT MAX(id) FROM release_groups),
(SELECT MAX(id) FROM recordings))`)
exec(`INSERT INTO file_types (extension) VALUES ('mp3')
ON CONFLICT DO NOTHING`)
exec(`INSERT INTO audio_files
(file_path, length_milliseconds, file_type_id, recording_id,
play_count, last_played)
VALUES (?, 1000,
(SELECT MAX(id) FROM file_types),
(SELECT MAX(id) FROM recordings),
?, ?)`,
"/music/"+name+".mp3", playCount, nullable(lastPlayed))
if genre != "" {
exec(`INSERT INTO genres (name) VALUES (?) ON CONFLICT DO NOTHING`, genre)
exec(`INSERT INTO recording_genres (recording_id, genre_id)
VALUES ((SELECT MAX(id) FROM recordings),
(SELECT id FROM genres WHERE name = ?))`, genre)
}
var id int64
if err := db.QueryRowWriter(
`SELECT MAX(id) FROM release_groups`,
).Scan(&id); err != nil {
t.Fatalf("seed: read album id: %v", err)
}
return id
}
func nullable(s string) any {
if s == "" {
return nil
}
return s
}
func shelfKinds(shelves []home.Shelf) []home.Kind {
kinds := make([]home.Kind, 0, len(shelves))
for _, s := range shelves {
kinds = append(kinds, s.Kind)
}
return kinds
}
func hasKind(shelves []home.Shelf, kind home.Kind) bool {
for _, s := range shelves {
if s.Kind == kind {
return true
}
}
return false
}
func shelfFor(shelves []home.Shelf, kind home.Kind) home.Shelf {
for _, s := range shelves {
if s.Kind == kind {
return s
}
}
return home.Shelf{}
}
func TestGetShelvesEmptyLibraryHasNoShelves(t *testing.T) {
db := database.NewTestDB(t)
svc := home.NewService(slog.Default(), db, fakeLibrary{})
shelves, err := svc.GetShelves()
if err != nil {
t.Fatalf("GetShelves: %v", err)
}
if len(shelves) != 0 {
t.Fatalf("empty library produced shelves: %v", shelfKinds(shelves))
}
}
func TestGetShelvesOmitsShelvesWithNothingBehindThem(t *testing.T) {
// A library nothing has ever been played from must not claim to
// know what is on repeat: an empty row labelled with a reason is a
// worse answer than no row.
db := database.NewTestDB(t)
id := seed(t, db, "Quiet", "Nobody", "", 0, "")
svc := home.NewService(slog.Default(), db, fakeLibrary{
albums: []library.Album{{ID: id, Name: "Quiet", ArtistName: "Nobody"}},
})
shelves, err := svc.GetShelves()
if err != nil {
t.Fatalf("GetShelves: %v", err)
}
if hasKind(shelves, home.KindRecentlyPlayed) {
t.Error("recently-played shelf built from no plays")
}
if hasKind(shelves, home.KindMostPlayed) {
t.Error("most-played shelf built from no plays")
}
if !hasKind(shelves, home.KindUnplayed) {
t.Errorf("expected an unplayed shelf, got %v", shelfKinds(shelves))
}
if !hasKind(shelves, home.KindRecentlyAdded) {
t.Errorf("expected a recently-added shelf, got %v", shelfKinds(shelves))
}
}
func TestGetShelvesRanksPlayHistory(t *testing.T) {
db := database.NewTestDB(t)
old := seed(t, db, "Old Favourite", "A", "", 20, "2020-01-01 00:00:00")
recent := seed(t, db, "Last Night", "B", "", 3, "2999-01-01 00:00:00")
svc := home.NewService(slog.Default(), db, fakeLibrary{
albums: []library.Album{
{ID: old, Name: "Old Favourite", ArtistName: "A"},
{ID: recent, Name: "Last Night", ArtistName: "B"},
},
})
shelves, err := svc.GetShelves()
if err != nil {
t.Fatalf("GetShelves: %v", err)
}
played := shelfFor(shelves, home.KindRecentlyPlayed)
if len(played.Albums) == 0 || played.Albums[0].ID != recent {
t.Errorf("recently played led with %v, want the newest play", played.Albums)
}
most := shelfFor(shelves, home.KindMostPlayed)
if len(most.Albums) == 0 || most.Albums[0].ID != old {
t.Errorf("most played led with %v, want the highest play count", most.Albums)
}
// Played long ago is "forgotten"; the never-played fallback must
// not take over while there is real history to report.
forgotten := shelfFor(shelves, home.KindStale)
if len(forgotten.Albums) == 0 || forgotten.Albums[0].ID != old {
t.Errorf("forgotten shelf = %v, want the album last played in 2020", forgotten.Albums)
}
}
func TestGetShelvesBuildsAGenreShelf(t *testing.T) {
db := database.NewTestDB(t)
albums := make([]library.Album, 0, 4)
for _, name := range []string{"One", "Two", "Three", "Four"} {
id := seed(t, db, name, "Various", "Doom Jazz", 1, "2024-01-01 00:00:00")
albums = append(albums, library.Album{
ID: id, Name: name, ArtistName: "Various",
})
}
svc := home.NewService(slog.Default(), db, fakeLibrary{albums: albums})
shelves, err := svc.GetShelves()
if err != nil {
t.Fatalf("GetShelves: %v", err)
}
genre := shelfFor(shelves, home.KindGenre)
if genre.Title != "Doom Jazz" {
t.Fatalf("genre shelf = %q, want the library's one genre", genre.Title)
}
if len(genre.Albums) != len(albums) {
t.Errorf("genre shelf had %d albums, want %d", len(genre.Albums), len(albums))
}
}
func TestGetShelvesSkipsAlbumsTheLibraryNoLongerHas(t *testing.T) {
// The id queries and the album list are two reads, and a scan can
// remove an album between them. A stale id must vanish from the
// shelf, not render as a blank card.
db := database.NewTestDB(t)
kept := seed(t, db, "Kept", "A", "", 5, "2024-01-01 00:00:00")
seed(t, db, "Removed", "B", "", 5, "2024-01-02 00:00:00")
svc := home.NewService(slog.Default(), db, fakeLibrary{
albums: []library.Album{{ID: kept, Name: "Kept", ArtistName: "A"}},
})
shelves, err := svc.GetShelves()
if err != nil {
t.Fatalf("GetShelves: %v", err)
}
for _, shelf := range shelves {
for _, album := range shelf.Albums {
if album.ID != kept {
t.Fatalf("shelf %q surfaced a removed album: %+v", shelf.ID, album)
}
}
}
}
// A shelf has to be worth its own row.
//
// A library with one signal answers several questions with the same
// albums, so shelves render the same covers in different orders under
// different reasons and the page reads as repeating itself (H-9).
// Confirmed in the running app on the fixture library before this
// existed: "On repeat" was "Pick up where you left off" reordered.
//
// The library has to be bigger than one shelf for the rule to apply at
// all, which is the point: a repeat is only a fault if a different row
// was possible.
func TestGetShelvesOmitsAShelfThatRepeatsTheOneAboveIt(t *testing.T) {
db := database.NewTestDB(t)
albums := make([]library.Album, 0, 16)
// Four albums carry the whole play history, so "what you played
// last" and "what you play most" can only answer with those four.
for i := range 4 {
name := "Played " + string(rune('A'+i))
id := seed(t, db, name, "Solo", "", 50+i, "2026-08-1"+string(rune('0'+i))+" 00:00:00")
albums = append(albums, library.Album{ID: id, Name: name, ArtistName: "Solo"})
}
// …and ten more the user has never touched, so the library is
// larger than a single shelf and a different row was possible.
for i := range 10 {
name := "Quiet " + string(rune('A'+i))
id := seed(t, db, name, "Nobody", "", 0, "")
albums = append(albums, library.Album{ID: id, Name: name, ArtistName: "Nobody"})
}
svc := home.NewService(slog.Default(), db, fakeLibrary{albums: albums})
shelves, err := svc.GetShelves()
if err != nil {
t.Fatalf("GetShelves: %v", err)
}
// The first of a duplicate pair survives: the reason the user sees
// is the one that came first, not the one built last.
if !hasKind(shelves, home.KindRecentlyPlayed) {
t.Fatalf("expected a recently-played shelf, got %v", shelfKinds(shelves))
}
// And the page still has somewhere to start.
if len(shelves) < 2 {
t.Fatalf("suppression left %d shelves: %v", len(shelves), shelfKinds(shelves))
}
for i := 1; i < len(shelves); i++ {
above := shelves[i-1]
shared := 0
for _, a := range shelves[i].Albums {
for _, b := range above.Albums {
if a.ID == b.ID {
shared++
break
}
}
}
if len(shelves[i].Albums) < 3 {
continue
}
if ratio := float64(shared) / float64(len(shelves[i].Albums)); ratio >= 2.0/3.0 {
t.Errorf(
"shelf %q repeats %q (%d of %d albums)",
shelves[i].Kind, above.Kind, shared, len(shelves[i].Albums),
)
}
}
}