Plans 013 and 014, the album page that prompted them, and the smaller fixes they turned up. Changelog, largest first. ## The local library is shaped like files, not like MusicBrainz `audio_files` carries its own tags and points at `albums` and `artists`; `file_genres` is the one real many-to-many. `recordings`, `release_group_recordings`, `artist_credit`, `artist_credit_artist`, `recording_genres`, `release_groups` and `release_to_rg` are gone from the local side, and with them a six-way join in every read, a `MIN(release_group_id)` subquery in eleven queries and a first-credited-artist subquery in nine. Measured on a real 25,966-file library, every many-to-many that model expressed was 1:1 in the data. - Ownership is a file. `GetFilePathsByRecordingMBIDs`, `LibraryMBIDIndex.CheckMBIDs`, `collectLibraryEntities` and `pruneStaleLocalCrossReferences` all join `audio_files`, so the 812 orphaned recordings, 216 release groups and 260 artists that library carried are now structurally impossible. - One projection: every track query selects from the `track_metadata` view, one row type, one mapper. Nine hand-rolled copies had drifted far enough to report different years on different screens. - `library_id = 0` means every library, so each list query exists once instead of scoped and unscoped with a branch at every call site. - No migration chain. `sql/schemas/` is the one description of the shape; `sql/migrations/`, `applyMigrations` and `schema_migrations` are squashed away, along with the drift between them that had sqlc generating against a stale schema. - `database.InsertTestTrack` is the one test seeder; twenty test files had been assembling the old FK chain each in its own order. ## The catalog stores its ids as bytes `explore_index`'s three 36-char MBID columns and its entity-type text are 16 raw bytes and a small integer. The table and its six indexes go 780 MB to 405 MB on a real 2,052,200-row catalog, which is why a fresh install is ~0.6 GB rather than ~1.0 GB. - `backend/explore/mbid.go` is the only place the encoding is known; everything above it speaks dashed strings. - `CHECK(length(mbid) = 16)` makes a stringly write fail at the insert rather than silently returning no rows, since SQLite does not coerce between TEXT and BLOB. - The importer asks the artifact what encoding it carries and converts on the way in, so the artifact already published keeps working and no format bump is needed. - `indexRowColumns`/`scanIndexRow` replace four copies of a 22-column list, and `TestStoredEncodingRoundTrips` sweeps every read path. ## An album page that says how much of the album is yours - One question, asked once: is there a file. `filePaths` is filled by a single batched lookup when the tracklist settles, and the badge, the Play count, the dimmed rows and every menu item read it — replacing four claims of decreasing confidence that could show a green tick on an album whose every action did nothing. - Play, Play 7 of 12, or no play button at all. - `total_tracks` on `explore_index` (~2 bytes over 400,677 release groups) and on `audio_files` from tags that have always carried it: a complete MBID-matched album now makes no catalog call at all, where it used to spend the most expensive request the app makes. - A merged cluster shows the running order the most releases agree on, and the version list marks the release you own rather than standing a synthetic entry in for it. - `AlbumReleasesFailed`: a slow fetch is no longer reported as a failed one by a 12-second timer. - Rows not in the library are dimmed in place (with `aria-disabled`) instead of the owned ones wearing a green tick and a legend. ## Caches and cover art get ceilings - Only the three tiers of a cover are stored; the full-resolution copy nothing rendered was 1,134 MB of a 1.4 GB covers directory. - One artist portrait is downloaded and the rest are remembered as URLs — 4.1 GB of a 5.3 GB cache was candidates no code path reads. - `browsedArtBudget` and `httpCacheBudget` bound what an age cannot: the same install held art for 5,770 artists in a 1,301-artist library. - `OrphanedArtistImagesJob` joined a bare MBID onto a sharded directory, so it deleted the rows that were the only record of the files it left behind. `explore.ArtistImageDir` is that layout's one definition now. ## The autotag queue asks whether there is work `tagging_items` was a row per album folder, not a queue, and no query read the `tag_status` column that held the answer. The four queue queries ask the files, which matters most where it is least visible: `startPrefetch` was scoring every album in a tagged library against MusicBrainz. ## Phantom playlist tracks resolve in place An M3U8 imported before its files leaves phantom rows; they now match by path and fall back to position, keep their place in the playlist when resolved, and pair best-first so two phantoms cannot claim the same file. ## Playing a track plays the list it is in Double-click, and Play on a single row's menu, queue the list as displayed with `startIndex` on that row — the album page and the track list used to queue one track and discard the album around it. A multi-row selection still plays exactly itself. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AfVYUVExXsx1nSWrXN8mAh
427 lines
9.6 KiB
Go
427 lines
9.6 KiB
Go
package queue
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
"testing"
|
|
|
|
"yellowjacket/backend/database"
|
|
)
|
|
|
|
// mockTrackLoader satisfies the TrackLoader interface for tests.
|
|
// All methods are no-ops.
|
|
type mockTrackLoader struct {
|
|
loadedFile string
|
|
}
|
|
|
|
func (m *mockTrackLoader) LoadFile(filePath string) error {
|
|
m.loadedFile = filePath
|
|
|
|
return nil
|
|
}
|
|
|
|
func (m *mockTrackLoader) Play() error { return nil }
|
|
func (m *mockTrackLoader) IsPlaying() bool { return false }
|
|
func (m *mockTrackLoader) UnloadTrack() {}
|
|
|
|
func (m *mockTrackLoader) CurrentPositionSeconds() (int, error) {
|
|
return 0, nil
|
|
}
|
|
|
|
// setupTestQueue creates an isolated Queue backed by an in-memory DB.
|
|
func setupTestQueue(t *testing.T) (*Queue, *database.DB) {
|
|
t.Helper()
|
|
|
|
db := database.NewTestDB(t)
|
|
q := NewQueue(slog.Default(), db)
|
|
q.SetPlayer(&mockTrackLoader{})
|
|
|
|
return q, db
|
|
}
|
|
|
|
// seedAudioFiles inserts `count` audio_file rows (with FK chain) and
|
|
// returns the file paths as a string slice.
|
|
func seedAudioFiles(t *testing.T, db *database.DB, count int) []string {
|
|
t.Helper()
|
|
|
|
paths := make([]string, count)
|
|
|
|
for i := range count {
|
|
paths[i] = fmt.Sprintf("/test/track%d.mp3", i+1)
|
|
|
|
// Some tests seed overlapping ranges to build a fallback set.
|
|
if _, err := db.Queries.GetAudioFileByPath(db.Ctx, paths[i]); err == nil {
|
|
continue
|
|
}
|
|
|
|
database.InsertTestTrack(t, db, database.TestTrack{
|
|
FilePath: paths[i],
|
|
Title: fmt.Sprintf("Track %d", i+1),
|
|
Artist: "Test Artist",
|
|
LengthMs: 180000,
|
|
})
|
|
}
|
|
|
|
return paths
|
|
}
|
|
|
|
func TestSetQueue_PopulatesTracks(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
q, db := setupTestQueue(t)
|
|
paths := seedAudioFiles(t, db, 5)
|
|
|
|
q.SetQueue(paths, 0, false, Source{})
|
|
|
|
state := q.GetState()
|
|
if got := len(state.Tracks); got != 5 {
|
|
t.Errorf("track count: got %d, want 5", got)
|
|
}
|
|
|
|
if state.CurrentIndex != 0 {
|
|
t.Errorf("currentIndex: got %d, want 0", state.CurrentIndex)
|
|
}
|
|
}
|
|
|
|
func TestSetQueue_RecordsSource(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
q, db := setupTestQueue(t)
|
|
paths := seedAudioFiles(t, db, 5)
|
|
source := Source{Type: "playlist", ID: 42, Label: "Road Trip"}
|
|
|
|
q.SetQueue(paths, 0, false, source)
|
|
|
|
if got := q.GetState().Source; got != source {
|
|
t.Errorf("source: got %+v, want %+v", got, source)
|
|
}
|
|
}
|
|
|
|
func TestSetQueue_ReplacesPriorSource(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
q, db := setupTestQueue(t)
|
|
paths := seedAudioFiles(t, db, 5)
|
|
|
|
q.SetQueue(paths, 0, false, Source{Type: "album", ID: 1, Label: "First"})
|
|
q.SetQueue(paths, 0, false, Source{Type: "genre", Label: "Jazz"})
|
|
|
|
want := Source{Type: "genre", Label: "Jazz"}
|
|
if got := q.GetState().Source; got != want {
|
|
t.Errorf("source: got %+v, want %+v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestClear_ResetsSource(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
q, db := setupTestQueue(t)
|
|
paths := seedAudioFiles(t, db, 5)
|
|
|
|
q.SetQueue(paths, 0, false, Source{Type: "album", ID: 1, Label: "Some Album"})
|
|
q.Clear()
|
|
|
|
if got := q.GetState().Source; got != (Source{}) {
|
|
t.Errorf("source after Clear: got %+v, want zero value", got)
|
|
}
|
|
}
|
|
|
|
func TestSetQueue_WithStartIndex(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
q, db := setupTestQueue(t)
|
|
paths := seedAudioFiles(t, db, 5)
|
|
|
|
q.SetQueue(paths, 2, false, Source{})
|
|
|
|
state := q.GetState()
|
|
if state.CurrentIndex != 2 {
|
|
t.Errorf("currentIndex: got %d, want 2", state.CurrentIndex)
|
|
}
|
|
}
|
|
|
|
func TestSetQueue_WithShuffleStart(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
q, db := setupTestQueue(t)
|
|
paths := seedAudioFiles(t, db, 5)
|
|
|
|
// Enable shuffle mode first.
|
|
q.ToggleShuffle()
|
|
|
|
q.SetQueue(paths, 0, true, Source{})
|
|
|
|
state := q.GetState()
|
|
if !state.ShuffleMode {
|
|
t.Error("shuffleMode: got false, want true")
|
|
}
|
|
|
|
q.mu.Lock()
|
|
soLen := len(q.shuffleOrder)
|
|
q.mu.Unlock()
|
|
|
|
if soLen != 5 {
|
|
t.Errorf("shuffleOrder length: got %d, want 5", soLen)
|
|
}
|
|
}
|
|
|
|
func TestAddTrack_AppendsToQueue(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
q, db := setupTestQueue(t)
|
|
paths := seedAudioFiles(t, db, 4)
|
|
|
|
q.SetQueue(paths[:3], 0, false, Source{})
|
|
q.AddTrack(paths[3])
|
|
|
|
state := q.GetState()
|
|
if got := len(state.Tracks); got != 4 {
|
|
t.Errorf("track count: got %d, want 4", got)
|
|
}
|
|
|
|
lastTrack := state.Tracks[len(state.Tracks)-1]
|
|
if lastTrack.FilePath != paths[3] {
|
|
t.Errorf("last track path: got %q, want %q", lastTrack.FilePath, paths[3])
|
|
}
|
|
}
|
|
|
|
func TestInsertTracksAt_BeforeCurrentIndex(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
q, db := setupTestQueue(t)
|
|
paths := seedAudioFiles(t, db, 7)
|
|
|
|
q.SetQueue(paths[:5], 2, false, Source{})
|
|
|
|
// Insert 2 tracks at index 1 (before currentIndex=2).
|
|
q.InsertTracksAt(paths[5:7], 1)
|
|
|
|
state := q.GetState()
|
|
// currentIndex should shift by 2 (the number of inserted tracks).
|
|
if state.CurrentIndex != 4 {
|
|
t.Errorf("currentIndex after insert before: got %d, want 4", state.CurrentIndex)
|
|
}
|
|
|
|
if got := len(state.Tracks); got != 7 {
|
|
t.Errorf("track count: got %d, want 7", got)
|
|
}
|
|
}
|
|
|
|
func TestInsertTracksAt_AfterCurrentIndex(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
q, db := setupTestQueue(t)
|
|
paths := seedAudioFiles(t, db, 7)
|
|
|
|
q.SetQueue(paths[:5], 2, false, Source{})
|
|
|
|
// Insert 2 tracks at index 3 (after currentIndex=2).
|
|
q.InsertTracksAt(paths[5:7], 3)
|
|
|
|
state := q.GetState()
|
|
// currentIndex should remain 2.
|
|
if state.CurrentIndex != 2 {
|
|
t.Errorf("currentIndex after insert after: got %d, want 2", state.CurrentIndex)
|
|
}
|
|
}
|
|
|
|
func TestMoveQueueTracks_ForwardMove(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
q, db := setupTestQueue(t)
|
|
paths := seedAudioFiles(t, db, 5)
|
|
|
|
q.SetQueue(paths, 0, false, Source{})
|
|
|
|
// Move track at index 1 to index 3.
|
|
q.MoveQueueTracks([]int{1}, 3)
|
|
|
|
state := q.GetState()
|
|
// After moving index 1 forward: the track originally at index 1
|
|
// should now be at index 2 (adjustedIdx = 3-1 = 2).
|
|
if state.Tracks[2].FilePath != paths[1] {
|
|
t.Errorf("moved track: got %q at index 2, want %q", state.Tracks[2].FilePath, paths[1])
|
|
}
|
|
}
|
|
|
|
func TestMoveQueueTracks_BackwardMove(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
q, db := setupTestQueue(t)
|
|
paths := seedAudioFiles(t, db, 5)
|
|
|
|
q.SetQueue(paths, 0, false, Source{})
|
|
|
|
// Move track at index 3 to index 1.
|
|
q.MoveQueueTracks([]int{3}, 1)
|
|
|
|
state := q.GetState()
|
|
// Track originally at index 3 should now be at index 1.
|
|
if state.Tracks[1].FilePath != paths[3] {
|
|
t.Errorf("moved track: got %q at index 1, want %q", state.Tracks[1].FilePath, paths[3])
|
|
}
|
|
}
|
|
|
|
func TestMoveQueueTracks_MoveCurrentTrack(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
q, db := setupTestQueue(t)
|
|
paths := seedAudioFiles(t, db, 5)
|
|
|
|
q.SetQueue(paths, 2, false, Source{})
|
|
|
|
// Move the current track (index 2) to index 4.
|
|
q.MoveQueueTracks([]int{2}, 4)
|
|
|
|
state := q.GetState()
|
|
// The current track should follow to its new position.
|
|
currentPath := state.Tracks[state.CurrentIndex].FilePath
|
|
if currentPath != paths[2] {
|
|
t.Errorf("current track after move: got %q, want %q", currentPath, paths[2])
|
|
}
|
|
}
|
|
|
|
func TestRemoveTrack_RemovesCorrectTrack(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
q, db := setupTestQueue(t)
|
|
paths := seedAudioFiles(t, db, 5)
|
|
|
|
q.SetQueue(paths, 0, false, Source{})
|
|
|
|
q.RemoveTrack(2)
|
|
|
|
state := q.GetState()
|
|
if got := len(state.Tracks); got != 4 {
|
|
t.Errorf("track count: got %d, want 4", got)
|
|
}
|
|
|
|
// Verify the removed track (paths[2]) is not present.
|
|
for _, track := range state.Tracks {
|
|
if track.FilePath == paths[2] {
|
|
t.Errorf("removed track %q still present in queue", paths[2])
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRemoveTrack_RemoveCurrentTrack(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
q, db := setupTestQueue(t)
|
|
paths := seedAudioFiles(t, db, 5)
|
|
|
|
q.SetQueue(paths, 2, false, Source{})
|
|
|
|
q.RemoveTrack(2)
|
|
|
|
state := q.GetState()
|
|
if got := len(state.Tracks); got != 4 {
|
|
t.Errorf("track count: got %d, want 4", got)
|
|
}
|
|
|
|
// After removing currentIndex=2, index should be clamped to valid range.
|
|
if state.CurrentIndex < 0 || state.CurrentIndex >= len(state.Tracks) {
|
|
t.Errorf(
|
|
"currentIndex out of range: got %d, track count %d",
|
|
state.CurrentIndex, len(state.Tracks),
|
|
)
|
|
}
|
|
}
|
|
|
|
func TestClear_EmptiesQueue(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
q, db := setupTestQueue(t)
|
|
paths := seedAudioFiles(t, db, 5)
|
|
|
|
q.SetQueue(paths, 0, false, Source{})
|
|
q.Clear()
|
|
|
|
state := q.GetState()
|
|
if got := len(state.Tracks); got != 0 {
|
|
t.Errorf("track count after clear: got %d, want 0", got)
|
|
}
|
|
|
|
if state.CurrentIndex != -1 {
|
|
t.Errorf("currentIndex after clear: got %d, want -1", state.CurrentIndex)
|
|
}
|
|
}
|
|
|
|
func TestToggleShuffle_TogglesMode(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
q, db := setupTestQueue(t)
|
|
paths := seedAudioFiles(t, db, 5)
|
|
|
|
q.SetQueue(paths, 0, false, Source{})
|
|
|
|
// Toggle on.
|
|
q.ToggleShuffle()
|
|
state := q.GetState()
|
|
|
|
if !state.ShuffleMode {
|
|
t.Error("shuffleMode after first toggle: got false, want true")
|
|
}
|
|
|
|
q.mu.Lock()
|
|
soLen := len(q.shuffleOrder)
|
|
q.mu.Unlock()
|
|
|
|
if soLen != 5 {
|
|
t.Errorf("shuffleOrder length after toggle on: got %d, want 5", soLen)
|
|
}
|
|
|
|
// Toggle off.
|
|
q.ToggleShuffle()
|
|
state = q.GetState()
|
|
|
|
if state.ShuffleMode {
|
|
t.Error("shuffleMode after second toggle: got true, want false")
|
|
}
|
|
|
|
q.mu.Lock()
|
|
soLen = len(q.shuffleOrder)
|
|
q.mu.Unlock()
|
|
|
|
if soLen != 0 {
|
|
t.Errorf("shuffleOrder length after toggle off: got %d, want 0", soLen)
|
|
}
|
|
}
|
|
|
|
func TestCycleRepeat_CyclesThroughModes(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
q, db := setupTestQueue(t)
|
|
_ = seedAudioFiles(t, db, 1)
|
|
|
|
// Default is RepeatOff.
|
|
state := q.GetState()
|
|
if state.RepeatMode != RepeatOff {
|
|
t.Errorf("initial repeatMode: got %q, want %q", state.RepeatMode, RepeatOff)
|
|
}
|
|
|
|
// off -> all
|
|
q.CycleRepeat()
|
|
state = q.GetState()
|
|
|
|
if state.RepeatMode != RepeatAll {
|
|
t.Errorf("after first cycle: got %q, want %q", state.RepeatMode, RepeatAll)
|
|
}
|
|
|
|
// all -> one
|
|
q.CycleRepeat()
|
|
state = q.GetState()
|
|
|
|
if state.RepeatMode != RepeatOne {
|
|
t.Errorf("after second cycle: got %q, want %q", state.RepeatMode, RepeatOne)
|
|
}
|
|
|
|
// one -> off
|
|
q.CycleRepeat()
|
|
state = q.GetState()
|
|
|
|
if state.RepeatMode != RepeatOff {
|
|
t.Errorf("after third cycle: got %q, want %q", state.RepeatMode, RepeatOff)
|
|
}
|
|
}
|