Files
minstrel/internal/library/scanner_test.go
T
bvandeusenandClaude Opus 5.5 1a98a23d1f
release / go (push) Failing after 1m13s
release / web (push) Successful in 1m16s
release / govulncheck (push) Successful in 20s
release / integration (push) Successful in 4m4s
release / android (push) Successful in 4m48s
release / Build signed APK (push) Successful in 5m13s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Skipped
release / Verify release artifacts (tag releases only) (push) Skipped
feat(library): albums are filed under their album artist, and empty rows are tidied after a scan
The scanner read only the track artist, so a compilation tagged
Various Artists, or an album with one featured-artist track, became one
album per track artist. It now files the album under the album artist
the file names (ALBUMARTIST / TPE2 / aART, with its MusicBrainz id),
and each track keeps its own artist. Tag-read version 9 re-reads the
library so existing tracks move back onto one album.

A scan now ends by removing album rows no track points to, and artists
left with no albums or tracks, telling clients they are gone. Albums
whose tracks are only missing stay for Needs a file, an artist a Songs
like playlist is seeded from stays, and rows made in the last ten
minutes are left alone so a file being added is never caught half way.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 19:08:21 -05:00

663 lines
23 KiB
Go

package library
import (
"bytes"
"context"
"encoding/binary"
"io"
"log/slog"
"os"
"path/filepath"
"testing"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
"git.fabledsword.com/bvandeusen/minstrel/internal/db"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
)
func TestReleaseDateFromYear(t *testing.T) {
cases := []struct {
in int
ok bool
label string
}{
{0, false, "missing tag"},
{-1, false, "negative"},
{1, true, "lower bound"},
{1999, true, "ordinary"},
{9999, true, "upper bound"},
{10000, false, "five digits"},
{99999999, false, "tag corruption"},
}
for _, c := range cases {
got, ok := releaseDateFromYear(c.in)
if ok != c.ok {
t.Errorf("%s: ok = %v, want %v", c.label, ok, c.ok)
}
if ok && (!got.Valid || got.Time.Year() != c.in) {
t.Errorf("%s: date = %+v, want year=%d", c.label, got, c.in)
}
}
}
// TestScanner_Integration exercises walk → tag-parse → upsert → incremental
// skip against a real Postgres. Gated on MINSTREL_TEST_DATABASE_URL.
func TestScanner_Integration(t *testing.T) {
if testing.Short() {
t.Skip("skipping scanner integration in -short mode")
}
dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL")
if dsn == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
ctx := context.Background()
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
if err := db.Migrate(dsn, logger); err != nil {
t.Fatalf("migrate: %v", err)
}
pool, err := pgxpool.New(ctx, dsn)
if err != nil {
t.Fatalf("pool: %v", err)
}
t.Cleanup(pool.Close)
if _, err := pool.Exec(ctx, "TRUNCATE tracks, albums, artists RESTART IDENTITY CASCADE"); err != nil {
t.Fatalf("truncate: %v", err)
}
root := t.TempDir()
writeTestMP3(t, filepath.Join(root, "artistX/albumA/01.mp3"), map[string]string{
"TIT2": "Track One", "TPE1": "Artist X", "TALB": "Album A", "TRCK": "1", "TYER": "2020",
})
writeTestMP3(t, filepath.Join(root, "artistX/albumA/02.mp3"), map[string]string{
"TIT2": "Track Two", "TPE1": "Artist X", "TALB": "Album A", "TRCK": "2", "TYER": "2020",
})
writeTestMP3(t, filepath.Join(root, "artistX/albumB/01.mp3"), map[string]string{
"TIT2": "B Song", "TPE1": "Artist X", "TALB": "Album B", "TRCK": "1",
})
writeTestMP3(t, filepath.Join(root, "artistY/01.mp3"), map[string]string{
"TIT2": "Solo", "TPE1": "The Artist Y", "TALB": "Y Album", "TRCK": "1",
})
scanner := New(pool, logger, []string{root}, nil)
stats, err := scanner.Scan(ctx, nil)
if err != nil {
t.Fatalf("first scan: %v", err)
}
if stats.Scanned != 4 || stats.Added != 4 {
t.Errorf("first scan stats = %+v, want Scanned=4 Added=4", stats)
}
q := dbq.New(pool)
artists, err := q.ListArtists(ctx)
if err != nil {
t.Fatalf("ListArtists: %v", err)
}
if len(artists) != 2 {
t.Fatalf("artist rows = %d (%+v), want 2", len(artists), artists)
}
// "The Artist Y" should sort under "Artist Y" (article stripped).
if artists[0].SortName != "Artist X" || artists[1].SortName != "Artist Y" {
t.Errorf("artist sort = [%q, %q], want [Artist X, Artist Y]", artists[0].SortName, artists[1].SortName)
}
// The synthetic MP3s carry only an ID3 tag — no decodable audio —
// so ffprobe yields duration 0. The scanner deliberately refuses to
// skip zero-duration rows (it re-runs them so a later scan can
// backfill duration once probing works), which is orthogonal to the
// mtime-based incremental-skip this test covers. Simulate a
// normally-probed library so the skip path is actually exercised;
// updated_at is left untouched (still ≥ file mtime).
if _, err := pool.Exec(ctx, "UPDATE tracks SET duration_ms = 1000 WHERE duration_ms = 0"); err != nil {
t.Fatalf("seed durations: %v", err)
}
stats2, err := scanner.Scan(ctx, nil)
if err != nil {
t.Fatalf("second scan: %v", err)
}
if stats2.Added != 0 || stats2.Updated != 0 || stats2.Skipped != 4 {
t.Errorf("incremental scan stats = %+v, want Skipped=4", stats2)
}
// ScanFiles (watcher-driven targeted scan): a newly-added file is picked
// up by path without walking the whole tree, and its album ID is returned
// so the watcher can enrich just that album. A non-audio path in the batch
// is silently ignored.
newFile := filepath.Join(root, "artistX/albumA/03.mp3")
writeTestMP3(t, newFile, map[string]string{
"TIT2": "Track Three", "TPE1": "Artist X", "TALB": "Album A", "TRCK": "3", "TYER": "2020",
})
notAudio := filepath.Join(root, "artistX/albumA/cover.txt")
if err := os.WriteFile(notAudio, []byte("nope"), 0o644); err != nil {
t.Fatalf("write non-audio: %v", err)
}
changed, err := scanner.ScanFiles(ctx, []string{newFile, notAudio})
if err != nil {
t.Fatalf("ScanFiles: %v", err)
}
if len(changed) != 1 || !changed[0].Valid {
t.Fatalf("ScanFiles changed albums = %+v, want exactly 1 valid", changed)
}
// The targeted scan upserts only the named file; the rest of the library
// is untouched (no full walk).
if _, err := q.GetTrackByPath(ctx, newFile); err != nil {
t.Fatalf("GetTrackByPath after ScanFiles: %v", err)
}
// Re-running ScanFiles on the now-unchanged file (duration probed) skips
// it, so no album is reported as changed.
if _, err := pool.Exec(ctx, "UPDATE tracks SET duration_ms = 1000 WHERE duration_ms = 0"); err != nil {
t.Fatalf("seed durations (2): %v", err)
}
changed2, err := scanner.ScanFiles(ctx, []string{newFile})
if err != nil {
t.Fatalf("ScanFiles (rescan): %v", err)
}
if len(changed2) != 0 {
t.Errorf("ScanFiles on unchanged file changed = %+v, want none", changed2)
}
}
// writeTestMP3 emits a file with only an ID3v2.3 tag payload plus a few
// bytes of MPEG-sync trailer. dhowden/tag reads tags without decoding audio,
// so this is enough for scanner round-trip tests without shipping binaries.
func writeTestMP3(t *testing.T, path string, frames map[string]string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
var framesBuf bytes.Buffer
for id, value := range frames {
if len(id) != 4 {
t.Fatalf("frame id %q not 4 bytes", id)
}
payload := append([]byte{0x03}, []byte(value)...)
framesBuf.WriteString(id)
_ = binary.Write(&framesBuf, binary.BigEndian, uint32(len(payload)))
framesBuf.WriteByte(0x00)
framesBuf.WriteByte(0x00)
framesBuf.Write(payload)
}
total := framesBuf.Len()
header := []byte{
'I', 'D', '3', 0x03, 0x00, 0x00,
byte((total >> 21) & 0x7F),
byte((total >> 14) & 0x7F),
byte((total >> 7) & 0x7F),
byte(total & 0x7F),
}
f, err := os.Create(path)
if err != nil {
t.Fatal(err)
}
defer func() { _ = f.Close() }()
if _, err := f.Write(header); err != nil {
t.Fatal(err)
}
if _, err := f.Write(framesBuf.Bytes()); err != nil {
t.Fatal(err)
}
if _, err := f.Write([]byte{0xFF, 0xFB, 0x90, 0x00}); err != nil {
t.Fatal(err)
}
}
// TestScanner_AdoptsMovedFile_Integration: a renamed file
// must keep its existing tracks row — same id, so likes, play history and
// playlist memberships travel with it — rather than forking into a marked ghost
// plus a fresh zero-history row.
//
// Uses the MBID path. The synthetic MP3s here carry no real audio, so their
// audio stream hash cannot be relied on — which is why the recording MBID is
// the signal under test. The audio-hash path is covered by moved_test.go.
//
// Eight tracks with one rename keeps the marked fraction at 12.5%, under
// missingMarkMaxFraction. That is load-bearing: if the rename exceeded the cap,
// reconcile would refuse to mark, adoption could not fire, and the file would
// fork. See the "reconcile skipped" warning in Scan.
func TestScanner_AdoptsMovedFile_Integration(t *testing.T) {
if testing.Short() {
t.Skip("skipping scanner integration in -short mode")
}
dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL")
if dsn == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
ctx := context.Background()
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
if err := db.Migrate(dsn, logger); err != nil {
t.Fatalf("migrate: %v", err)
}
pool, err := pgxpool.New(ctx, dsn)
if err != nil {
t.Fatalf("pool: %v", err)
}
t.Cleanup(pool.Close)
if _, err := pool.Exec(ctx, "TRUNCATE tracks, albums, artists RESTART IDENTITY CASCADE"); err != nil {
t.Fatalf("truncate: %v", err)
}
root := t.TempDir()
const movedMBID = "11111111-2222-3333-4444-555555555555"
movedFrom := filepath.Join(root, "artistM/albumM/04 - Bleed It Out.mp3")
writeTestMP3(t, movedFrom, map[string]string{
"TIT2": "Bleed It Out", "TPE1": "Artist M", "TALB": "Album M", "TRCK": "4",
// dhowden surfaces TXXX as a Comm whose Description is the Picard tag
// name; "MusicBrainz Track Id" is mbz.Recording.
"TXXX": "MusicBrainz Track Id\x00" + movedMBID,
})
// Filler so one rename stays under the mark cap.
for i := 1; i <= 7; i++ {
writeTestMP3(t, filepath.Join(root, "artistM/albumM/filler", string(rune('a'+i))+".mp3"),
map[string]string{
"TIT2": "Filler " + string(rune('0'+i)), "TPE1": "Artist M", "TALB": "Album M",
})
}
scanner := New(pool, logger, []string{root}, nil)
if _, err := scanner.Scan(ctx, nil); err != nil {
t.Fatalf("first scan: %v", err)
}
q := dbq.New(pool)
before, err := q.GetTrackByPath(ctx, movedFrom)
if err != nil {
t.Fatalf("track not indexed on first scan: %v", err)
}
if before.Mbid == nil || *before.Mbid != movedMBID {
t.Fatalf("recording mbid not stored: %v", before.Mbid)
}
// Renumber the file, exactly as a tag editor would.
movedTo := filepath.Join(root, "artistM/albumM/02 - Bleed It Out.mp3")
if err := os.Rename(movedFrom, movedTo); err != nil {
t.Fatalf("rename: %v", err)
}
if _, err := scanner.Scan(ctx, nil); err != nil {
t.Fatalf("second scan: %v", err)
}
after, err := q.GetTrackByPath(ctx, movedTo)
if err != nil {
t.Fatalf("track not found at its new path: %v", err)
}
if after.ID != before.ID {
t.Errorf("track id changed on rename: %v -> %v (history would be stranded)",
before.ID, after.ID)
}
if after.MissingSince.Valid {
t.Errorf("adopted row is still marked missing: %v", after.MissingSince)
}
// The old path must be gone entirely — not lingering as a marked ghost.
if _, err := q.GetTrackByPath(ctx, movedFrom); err == nil {
t.Error("old path still has a tracks row; the track forked instead of moving")
}
var total int
if err := pool.QueryRow(ctx, "SELECT count(*) FROM tracks").Scan(&total); err != nil {
t.Fatalf("count: %v", err)
}
if total != 8 {
t.Errorf("tracks = %d, want 8 — a rename must not add a row", total)
}
}
// TestScanner_ReleaseGroupMBID_Integration: a new album
// takes its release-group id from the tag, and an album indexed before this
// read existed gets it on the next scan, through the tagReadVersion bump,
// without its file changing.
func TestScanner_ReleaseGroupMBID_Integration(t *testing.T) {
if testing.Short() {
t.Skip("skipping scanner integration in -short mode")
}
dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL")
if dsn == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
ctx := context.Background()
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
if err := db.Migrate(dsn, logger); err != nil {
t.Fatalf("migrate: %v", err)
}
pool, err := pgxpool.New(ctx, dsn)
if err != nil {
t.Fatalf("pool: %v", err)
}
t.Cleanup(pool.Close)
if _, err := pool.Exec(ctx, "TRUNCATE tracks, albums, artists RESTART IDENTITY CASCADE"); err != nil {
t.Fatalf("truncate: %v", err)
}
const (
groupA = "aaaaaaaa-1111-2222-3333-444444444444"
groupB = "bbbbbbbb-1111-2222-3333-444444444444"
)
root := t.TempDir()
writeTestMP3(t, filepath.Join(root, "rg/A/01.mp3"), map[string]string{
"TIT2": "One", "TPE1": "Artist RG", "TALB": "Album A",
"TXXX": "MusicBrainz Release Group Id\x00" + groupA,
})
pathB := filepath.Join(root, "rg/B/01.mp3")
writeTestMP3(t, pathB, map[string]string{
"TIT2": "Two", "TPE1": "Artist RG", "TALB": "Album B",
"TXXX": "MusicBrainz Release Group Id\x00" + groupB,
})
scanner := New(pool, logger, []string{root}, nil)
if _, err := scanner.Scan(ctx, nil); err != nil {
t.Fatalf("first scan: %v", err)
}
groupOf := func(title string) *string {
t.Helper()
var rg *string
if err := pool.QueryRow(ctx, "SELECT release_group_mbid FROM albums WHERE title = $1", title).Scan(&rg); err != nil {
t.Fatalf("album %q: %v", title, err)
}
return rg
}
if rg := groupOf("Album A"); rg == nil || *rg != groupA {
t.Fatalf("new album release group = %v, want %s", rg, groupA)
}
// Album B as a library indexed under tag-read version 2 left it: no
// release group, file untouched since.
if _, err := pool.Exec(ctx, "UPDATE albums SET release_group_mbid = NULL WHERE title = 'Album B'"); err != nil {
t.Fatal(err)
}
if _, err := pool.Exec(ctx, "UPDATE tracks SET tag_read_version = 2 WHERE file_path = $1", pathB); err != nil {
t.Fatal(err)
}
if _, err := scanner.Scan(ctx, nil); err != nil {
t.Fatalf("second scan: %v", err)
}
if rg := groupOf("Album B"); rg == nil || *rg != groupB {
t.Errorf("existing album release group after rescan = %v, want %s", rg, groupB)
}
if rg := groupOf("Album A"); rg == nil || *rg != groupA {
t.Errorf("album A release group changed to %v", rg)
}
}
// Two albums of one name by one artist, in different release groups, are two
// albums, as Weezer's self-titled albums are. A
// track of the same group joins its album, and a track filed under the other
// album's row by the old lookup moves to its own on the tagReadVersion re-read.
func TestScanner_SameTitledAlbumsOfTwoGroupsAreTwoAlbums_Integration(t *testing.T) {
if testing.Short() {
t.Skip("skipping scanner integration in -short mode")
}
dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL")
if dsn == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
ctx := context.Background()
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
if err := db.Migrate(dsn, logger); err != nil {
t.Fatalf("migrate: %v", err)
}
pool, err := pgxpool.New(ctx, dsn)
if err != nil {
t.Fatalf("pool: %v", err)
}
t.Cleanup(pool.Close)
if _, err := pool.Exec(ctx, "TRUNCATE tracks, albums, artists RESTART IDENTITY CASCADE"); err != nil {
t.Fatalf("truncate: %v", err)
}
const (
blue = "aaaaaaaa-1994-2222-3333-444444444444"
green = "bbbbbbbb-2001-2222-3333-444444444444"
)
root := t.TempDir()
tagged := func(rel, title, group string) string {
p := filepath.Join(root, rel)
writeTestMP3(t, p, map[string]string{
"TIT2": title, "TPE1": "Weezer", "TALB": "Weezer",
"TXXX": "MusicBrainz Release Group Id\x00" + group,
})
return p
}
tagged("Weezer (1994)/01.mp3", "My Name Is Jonas", blue)
tagged("Weezer (1994)/02.mp3", "No One Else", blue)
greenPath := tagged("Weezer (2001)/01.mp3", "Don't Let Go", green)
scanner := New(pool, logger, []string{root}, nil)
if _, err := scanner.Scan(ctx, nil); err != nil {
t.Fatalf("scan: %v", err)
}
albumsByGroup := func() map[string]int {
t.Helper()
rows, err := pool.Query(ctx, `SELECT a.release_group_mbid, count(t.id) FROM albums a
JOIN tracks t ON t.album_id = a.id WHERE a.title = 'Weezer' GROUP BY a.id, a.release_group_mbid`)
if err != nil {
t.Fatal(err)
}
defer rows.Close()
out := map[string]int{}
for rows.Next() {
var g *string
var n int
if err := rows.Scan(&g, &n); err != nil {
t.Fatal(err)
}
if g == nil {
t.Fatal("an album with no release group")
}
out[*g] = n
}
return out
}
if got := albumsByGroup(); len(got) != 2 || got[blue] != 2 || got[green] != 1 {
t.Fatalf("albums by group = %v, want blue with 2 tracks and green with 1", got)
}
// A bonus track tagged with no release group joins the album its folder
// holds, whichever row is older.
writeTestMP3(t, filepath.Join(root, "Weezer (2001)/02.mp3"), map[string]string{
"TIT2": "Photograph (live)", "TPE1": "Weezer", "TALB": "Weezer",
})
if _, err := scanner.Scan(ctx, nil); err != nil {
t.Fatalf("scan the bonus track: %v", err)
}
if got := albumsByGroup(); len(got) != 2 || got[blue] != 2 || got[green] != 2 {
t.Fatalf("albums by group = %v, want the ungrouped 2001 bonus track on green", got)
}
// The green track as the old lookup left it: on the blue album's row,
// under an older tag-read version.
if _, err := pool.Exec(ctx, `
WITH b AS (SELECT id FROM albums WHERE release_group_mbid = $1)
UPDATE tracks SET album_id = (SELECT id FROM b), tag_read_version = 6 WHERE file_path = $2`, blue, greenPath); err != nil {
t.Fatal(err)
}
if _, err := scanner.Scan(ctx, nil); err != nil {
t.Fatalf("rescan: %v", err)
}
if got := albumsByGroup(); len(got) != 2 || got[blue] != 2 || got[green] != 2 {
t.Errorf("after the re-read, albums by group = %v, want the green track on its own album", got)
}
}
// TestScanFiles_ClearsTheMarkWhenAFileReturnsToItsPath_Integration: a file
// put back at the path of a track waiting for one (an admin removed it, or it
// went missing) brings the track back as soon as the watcher scans it, even
// with its old mtime, which takes the unchanged-file skip. The track keeps its
// id, and clients are told it plays again.
func TestScanFiles_ClearsTheMarkWhenAFileReturnsToItsPath_Integration(t *testing.T) {
pool := newPool(t)
ctx := context.Background()
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
root := t.TempDir()
path := filepath.Join(root, "Artist R/Album R/03 - Returning.mp3")
writeTestMP3(t, path, map[string]string{
"TIT2": "Returning", "TPE1": "Artist R", "TALB": "Album R", "TRCK": "3",
})
scanner := New(pool, logger, []string{root}, nil)
if _, err := scanner.ScanFiles(ctx, []string{path}); err != nil {
t.Fatalf("first scan: %v", err)
}
q := dbq.New(pool)
before, err := q.GetTrackByPath(ctx, path)
if err != nil {
t.Fatalf("track not indexed: %v", err)
}
// Removed from Reports: the row waits, and the file is back unchanged.
if _, err := q.MarkTracksRemoved(ctx, dbq.MarkTracksRemovedParams{
Reason: string(RemovedForReport), Ids: []pgtype.UUID{before.ID},
}); err != nil {
t.Fatalf("mark removed: %v", err)
}
var changesBefore int
if err := pool.QueryRow(ctx, "SELECT count(*) FROM library_changes").Scan(&changesBefore); err != nil {
t.Fatalf("count changes: %v", err)
}
if _, err := scanner.ScanFiles(ctx, []string{path}); err != nil {
t.Fatalf("watcher scan: %v", err)
}
after, err := q.GetTrackByID(ctx, before.ID)
if err != nil {
t.Fatalf("track gone: %v", err)
}
if after.MissingSince.Valid {
t.Errorf("track still waits for a file it has: missing_since=%v", after.MissingSince.Time)
}
if after.RemovedReason != nil {
t.Errorf("removed_reason = %q, want none once the file is back", *after.RemovedReason)
}
if after.FilePath != path {
t.Errorf("file_path = %q, want %q", after.FilePath, path)
}
var changesAfter int
if err := pool.QueryRow(ctx, "SELECT count(*) FROM library_changes").Scan(&changesAfter); err != nil {
t.Fatalf("count changes: %v", err)
}
if changesAfter <= changesBefore {
t.Error("no library change logged, so clients would still show the track as unplayable")
}
}
// An album is filed under its album artist: a compilation's tracks by
// different artists, and an album with one featured-artist track, are each
// one album, and every track keeps its own artist. The row the old filing
// left behind is removed by the scan's tidy-up once a re-read empties it.
func TestScanner_AlbumArtistKeepsAnAlbumTogether_Integration(t *testing.T) {
if testing.Short() {
t.Skip("skipping scanner integration in -short mode")
}
dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL")
if dsn == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
ctx := context.Background()
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
if err := db.Migrate(dsn, logger); err != nil {
t.Fatalf("migrate: %v", err)
}
pool, err := pgxpool.New(ctx, dsn)
if err != nil {
t.Fatalf("pool: %v", err)
}
t.Cleanup(pool.Close)
if _, err := pool.Exec(ctx, "TRUNCATE tracks, albums, artists RESTART IDENTITY CASCADE"); err != nil {
t.Fatalf("truncate: %v", err)
}
root := t.TempDir()
song := func(rel, title, artist, albumArtist, album string) string {
p := filepath.Join(root, rel)
frames := map[string]string{"TIT2": title, "TPE1": artist, "TALB": album}
if albumArtist != "" {
frames["TPE2"] = albumArtist
}
writeTestMP3(t, p, frames)
return p
}
song("Summer Hits/01.mp3", "Wave", "Low Tide Choir", "Various Artists", "Summer Hits")
song("Summer Hits/02.mp3", "Glass", "Paper Lanterns", "Various Artists", "Summer Hits")
song("Night Drive/01.mp3", "Neon", "Paper Lanterns", "Paper Lanterns", "Night Drive")
guest := song("Night Drive/02.mp3", "Static", "Paper Lanterns feat. Low Tide Choir", "Paper Lanterns", "Night Drive")
scanner := New(pool, logger, []string{root}, nil)
if _, err := scanner.Scan(ctx, nil); err != nil {
t.Fatalf("scan: %v", err)
}
type filing struct{ albumArtist, tracks, trackArtists int }
albums := func() map[string]filing {
t.Helper()
rows, err := pool.Query(ctx, `
SELECT a.title, ar.name, count(t.id), count(DISTINCT t.artist_id)
FROM albums a JOIN artists ar ON ar.id = a.artist_id
LEFT JOIN tracks t ON t.album_id = a.id
GROUP BY a.id, a.title, ar.name`)
if err != nil {
t.Fatal(err)
}
defer rows.Close()
out := map[string]filing{}
names := map[string]string{}
for rows.Next() {
var title, artist string
var tracks, trackArtists int
if err := rows.Scan(&title, &artist, &tracks, &trackArtists); err != nil {
t.Fatal(err)
}
if _, dup := out[title]; dup {
t.Fatalf("album %q has more than one row", title)
}
names[title] = artist
out[title] = filing{tracks: tracks, trackArtists: trackArtists}
}
if names["Summer Hits"] != "Various Artists" || names["Night Drive"] != "Paper Lanterns" {
t.Fatalf("album artists = %v, want Summer Hits by Various Artists and Night Drive by Paper Lanterns", names)
}
return out
}
got := albums()
if got["Summer Hits"].tracks != 2 || got["Summer Hits"].trackArtists != 2 {
t.Errorf("Summer Hits = %+v, want 2 tracks by 2 artists on one album", got["Summer Hits"])
}
if got["Night Drive"].tracks != 2 || got["Night Drive"].trackArtists != 2 {
t.Errorf("Night Drive = %+v, want 2 tracks by 2 artists on one album", got["Night Drive"])
}
// The guest track as the old filing left it: on an album of its own,
// made long enough ago for the tidy-up, under an older tag-read version.
if _, err := pool.Exec(ctx, `
WITH g AS (SELECT artist_id FROM tracks WHERE file_path = $1),
a AS (INSERT INTO albums (title, sort_title, artist_id, created_at)
SELECT 'Night Drive', 'night drive', artist_id, now() - interval '1 day' FROM g
RETURNING id)
UPDATE tracks SET album_id = (SELECT id FROM a), tag_read_version = 8 WHERE file_path = $1`, guest); err != nil {
t.Fatal(err)
}
if _, err := scanner.Scan(ctx, nil); err != nil {
t.Fatalf("rescan: %v", err)
}
if got := albums(); got["Night Drive"].tracks != 2 {
t.Errorf("after the re-read, Night Drive = %+v, want both tracks back on one album", got["Night Drive"])
}
var empty int
if err := pool.QueryRow(ctx,
`SELECT count(*) FROM albums a WHERE NOT EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = a.id)`,
).Scan(&empty); err != nil {
t.Fatal(err)
}
if empty != 0 {
t.Errorf("%d empty album rows left after the scan, want the tidy-up to remove them", empty)
}
}