release / web (push) Successful in 1m30s
release / go (push) Successful in 1m57s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 3m26s
release / android (push) Successful in 4m40s
release / Build signed APK (push) Successful in 5m12s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Publish the release image to GHCR (dispatch only) (push) Skipped
release / Build + push container image (push) Successful in 28s
release / Verify release artifacts (tag releases only) (push) Skipped
- Moved-file adoption: 14 tests become one table. uniqueMatch, the row converters and pgtype's zero UUID are dropped: the adoption table covers the first two, and the third tests pgx. - Reconcile: marking and its change rows, and restoring and its change rows, are one test each; the five "a volume looks absent" refusals are one table. Dropped: an error-propagation test. - Genres: the ID3 reader cases, the normalisers and extractGenres across ID3, FLAC, Ogg and MP4 are one table each. - MusicBrainz tag ids: eleven per-format tests become one table. - Scan trigger: start / skip / reap is one table. Dropped: a closed-pool test. - Stage publisher: six tests become three. - Duplicate resolver: the cross-release cases (rip, renamed, kept as fine, two titles) asserted the same outcome once the resolver stopped removing files. They become one table that also checks the group's note, which is where rip and clean still differ. The auto-resolve-off song-link check moves into the existing auto-resolve-off test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
302 lines
10 KiB
Go
302 lines
10 KiB
Go
package library
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"log/slog"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
|
|
"github.com/Fabled-Sword/minstrel/internal/db/dbq"
|
|
)
|
|
|
|
// fakeReconciler records what reconcileMissing decided to do, so the guards can
|
|
// be tested without a database. The guards are the whole point of this pass —
|
|
// they are what stands between an unmounted volume and the library disappearing
|
|
// from every mix — so they get tested directly rather than via integration.
|
|
type fakeReconciler struct {
|
|
rows []dbq.ListTrackPathsForReconcileRow
|
|
marked []pgtype.UUID
|
|
cleared []pgtype.UUID
|
|
listErr error
|
|
markErr error
|
|
clearErr error
|
|
// Change rows the reconcile pass asked the delta sync to emit.
|
|
loggedChanges []dbq.InsertLibraryChangesParams
|
|
}
|
|
|
|
func (f *fakeReconciler) ListTrackPathsForReconcile(context.Context) ([]dbq.ListTrackPathsForReconcileRow, error) {
|
|
return f.rows, f.listErr
|
|
}
|
|
|
|
func (f *fakeReconciler) MarkTracksMissing(_ context.Context, ids []pgtype.UUID) (int64, error) {
|
|
if f.markErr != nil {
|
|
return 0, f.markErr
|
|
}
|
|
f.marked = append(f.marked, ids...)
|
|
return int64(len(ids)), nil
|
|
}
|
|
|
|
func (f *fakeReconciler) InsertLibraryChanges(
|
|
_ context.Context, arg dbq.InsertLibraryChangesParams,
|
|
) error {
|
|
f.loggedChanges = append(f.loggedChanges, arg)
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeReconciler) ClearTracksMissing(_ context.Context, ids []pgtype.UUID) (int64, error) {
|
|
if f.clearErr != nil {
|
|
return 0, f.clearErr
|
|
}
|
|
f.cleared = append(f.cleared, ids...)
|
|
return int64(len(ids)), nil
|
|
}
|
|
|
|
// Compile-time proof the real queries still satisfy what reconcile needs — the
|
|
// interface exists to narrow dbq.Queries, not to diverge from it.
|
|
var _ trackReconciler = (*dbq.Queries)(nil)
|
|
|
|
func testUUID(n byte) pgtype.UUID {
|
|
var u pgtype.UUID
|
|
u.Bytes[15] = n
|
|
u.Valid = true
|
|
return u
|
|
}
|
|
|
|
func markedAt() pgtype.Timestamptz {
|
|
return pgtype.Timestamptz{Valid: true}
|
|
}
|
|
|
|
func row(n byte, path string, missing bool) dbq.ListTrackPathsForReconcileRow {
|
|
r := dbq.ListTrackPathsForReconcileRow{ID: testUUID(n), FilePath: path}
|
|
if missing {
|
|
r.MissingSince = markedAt()
|
|
}
|
|
return r
|
|
}
|
|
|
|
// populatedRoot returns a directory containing one file, so verifyRootsPresent
|
|
// treats it as a real, mounted library root.
|
|
func populatedRoot(t *testing.T) string {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
if err := os.WriteFile(filepath.Join(dir, "a.mp3"), []byte("x"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return dir
|
|
}
|
|
|
|
func testScanner(t *testing.T, roots ...string) *Scanner {
|
|
t.Helper()
|
|
return &Scanner{
|
|
logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
|
|
paths: roots,
|
|
}
|
|
}
|
|
|
|
// A row the walk did not see is marked missing, and the mark reaches clients
|
|
// as a change row. Without one the mark is invisible: /api/library/sync is a
|
|
// change-log feed, so a plain UPDATE never reaches anyone.
|
|
func TestReconcileMissing_MarksRowsTheWalkDidNotSee(t *testing.T) {
|
|
root := populatedRoot(t)
|
|
s := testScanner(t, root)
|
|
|
|
// 10 rows with 2 absent — 20%, deliberately under missingMarkMaxFraction so
|
|
// this exercises marking rather than the cap.
|
|
rows := make([]dbq.ListTrackPathsForReconcileRow, 0, 10)
|
|
seen := map[string]struct{}{}
|
|
for i := 0; i < 10; i++ {
|
|
p := fmt.Sprintf("/music/track-%02d.mp3", i)
|
|
rows = append(rows, row(byte(i), p, false))
|
|
if i >= 2 {
|
|
seen[p] = struct{}{}
|
|
}
|
|
}
|
|
q := &fakeReconciler{rows: rows}
|
|
|
|
var stats Stats
|
|
if err := s.reconcileMissing(context.Background(), q, seen, &stats); err != nil {
|
|
t.Fatalf("reconcile: %v", err)
|
|
}
|
|
if len(q.marked) != 2 || q.marked[0] != testUUID(0) || q.marked[1] != testUUID(1) {
|
|
t.Fatalf("marked %v, want rows 0 and 1", q.marked)
|
|
}
|
|
if stats.Missing != 2 {
|
|
t.Errorf("stats.Missing = %d, want 2", stats.Missing)
|
|
}
|
|
if len(q.cleared) != 0 {
|
|
t.Errorf("cleared %d rows, want 0", len(q.cleared))
|
|
}
|
|
if len(q.loggedChanges) != 1 {
|
|
t.Fatalf("want one batch of change rows, got %d", len(q.loggedChanges))
|
|
}
|
|
got := q.loggedChanges[0]
|
|
// Upsert, not delete: the track still exists and keeps its history. A
|
|
// delete would tell clients to drop the row, and a missing file is
|
|
// expected to come back.
|
|
if got.EntityType != "track" || got.Op != "upsert" || len(got.EntityIds) != 2 {
|
|
t.Errorf("change rows = (%s, %s, %d ids), want an upsert of both tracks", got.EntityType, got.Op, len(got.EntityIds))
|
|
}
|
|
}
|
|
|
|
// A file coming back clears its mark and reaches clients too, or a restored
|
|
// track stays greyed out on every device until something unrelated touches it.
|
|
func TestReconcileMissing_ClearsRowsWhoseFileReturned(t *testing.T) {
|
|
root := populatedRoot(t)
|
|
s := testScanner(t, root)
|
|
q := &fakeReconciler{rows: []dbq.ListTrackPathsForReconcileRow{
|
|
row(1, "/music/back.mp3", true),
|
|
row(2, "/music/still-here.mp3", false),
|
|
}}
|
|
seen := map[string]struct{}{
|
|
"/music/back.mp3": {},
|
|
"/music/still-here.mp3": {},
|
|
}
|
|
|
|
var stats Stats
|
|
if err := s.reconcileMissing(context.Background(), q, seen, &stats); err != nil {
|
|
t.Fatalf("reconcile: %v", err)
|
|
}
|
|
if len(q.cleared) != 1 || q.cleared[0] != testUUID(1) {
|
|
t.Fatalf("cleared = %v, want just row 1", q.cleared)
|
|
}
|
|
if stats.Restored != 1 {
|
|
t.Errorf("stats.Restored = %d, want 1", stats.Restored)
|
|
}
|
|
if len(q.marked) != 0 {
|
|
t.Errorf("marked %d rows, want 0", len(q.marked))
|
|
}
|
|
if len(q.loggedChanges) != 1 || q.loggedChanges[0].Op != "upsert" {
|
|
t.Errorf("change rows = %+v, want one upsert batch for the restored track", q.loggedChanges)
|
|
}
|
|
}
|
|
|
|
// An already-marked row must not be re-marked: the timestamp is the "how long
|
|
// has this been gone" clock that any future cleanup policy depends on.
|
|
func TestReconcileMissing_DoesNotRemarkAlreadyMissingRows(t *testing.T) {
|
|
root := populatedRoot(t)
|
|
s := testScanner(t, root)
|
|
q := &fakeReconciler{rows: []dbq.ListTrackPathsForReconcileRow{
|
|
row(1, "/music/long-gone.mp3", true),
|
|
row(2, "/music/present.mp3", false),
|
|
}}
|
|
seen := map[string]struct{}{"/music/present.mp3": {}}
|
|
|
|
var stats Stats
|
|
if err := s.reconcileMissing(context.Background(), q, seen, &stats); err != nil {
|
|
t.Fatalf("reconcile: %v", err)
|
|
}
|
|
if len(q.marked) != 0 {
|
|
t.Errorf("re-marked an already-missing row: %v", q.marked)
|
|
}
|
|
if len(q.cleared) != 0 {
|
|
t.Errorf("cleared = %v, want none", q.cleared)
|
|
}
|
|
}
|
|
|
|
// The guard that matters most. A half-attached mount makes the walk succeed
|
|
// while seeing only part of the library — evidence indistinguishable from a
|
|
// mass deletion, so reconcile must refuse rather than guess.
|
|
func TestReconcileMissing_RefusesWhenTooMuchWouldBeMarked(t *testing.T) {
|
|
root := populatedRoot(t)
|
|
s := testScanner(t, root)
|
|
rows := make([]dbq.ListTrackPathsForReconcileRow, 0, 100)
|
|
seen := map[string]struct{}{}
|
|
for i := 0; i < 100; i++ {
|
|
p := fmt.Sprintf("/music/track-%03d.mp3", i)
|
|
rows = append(rows, row(byte(i), p, false))
|
|
// Only 60 of 100 present -> 40% would be marked, over the 25% cap.
|
|
if i < 60 {
|
|
seen[p] = struct{}{}
|
|
}
|
|
}
|
|
q := &fakeReconciler{rows: rows}
|
|
|
|
var stats Stats
|
|
err := s.reconcileMissing(context.Background(), q, seen, &stats)
|
|
if err == nil {
|
|
t.Fatal("expected reconcile to refuse, got nil error")
|
|
}
|
|
if len(q.marked) != 0 {
|
|
t.Errorf("marked %d rows despite refusing", len(q.marked))
|
|
}
|
|
if stats.Missing != 0 {
|
|
t.Errorf("stats.Missing = %d, want 0", stats.Missing)
|
|
}
|
|
}
|
|
|
|
// Restoring is never the dangerous direction, so it must survive the cap —
|
|
// otherwise a library that tripped the cap once could never clear its marks
|
|
// even after the volume came back.
|
|
func TestReconcileMissing_ClearsEvenWhenMarkCapTrips(t *testing.T) {
|
|
root := populatedRoot(t)
|
|
s := testScanner(t, root)
|
|
rows := []dbq.ListTrackPathsForReconcileRow{row(1, "/music/back.mp3", true)}
|
|
seen := map[string]struct{}{"/music/back.mp3": {}}
|
|
// Add enough absent rows to blow the cap.
|
|
for i := 2; i < 10; i++ {
|
|
rows = append(rows, row(byte(i), fmt.Sprintf("/music/absent-%02d.mp3", i), false))
|
|
}
|
|
q := &fakeReconciler{rows: rows}
|
|
|
|
var stats Stats
|
|
if err := s.reconcileMissing(context.Background(), q, seen, &stats); err == nil {
|
|
t.Fatal("expected the mark cap to trip")
|
|
}
|
|
if len(q.cleared) != 1 {
|
|
t.Errorf("cleared %d rows, want 1 — restores must not be blocked by the cap", len(q.cleared))
|
|
}
|
|
if stats.Restored != 1 {
|
|
t.Errorf("stats.Restored = %d, want 1", stats.Restored)
|
|
}
|
|
}
|
|
|
|
// A walk that cannot be trusted marks nothing: one that saw no files, a root
|
|
// that is not mounted (absent), a mount point with nothing on it (present but
|
|
// empty, which os.Stat alone would pass), one detached root among several,
|
|
// or no roots configured at all.
|
|
func TestReconcileMissing_RefusesWhenAVolumeLooksAbsent(t *testing.T) {
|
|
seenSomething := map[string]struct{}{"/x": {}}
|
|
for _, tc := range []struct {
|
|
name string
|
|
roots func(t *testing.T) []string
|
|
seen map[string]struct{}
|
|
}{
|
|
{"the walk saw no files", func(t *testing.T) []string { return []string{populatedRoot(t)} }, map[string]struct{}{}},
|
|
{"a root is absent", func(t *testing.T) []string { return []string{filepath.Join(t.TempDir(), "not-mounted")} }, seenSomething},
|
|
{"a root is empty", func(t *testing.T) []string { return []string{t.TempDir()} }, seenSomething},
|
|
{"one of several roots is absent", func(t *testing.T) []string {
|
|
return []string{populatedRoot(t), filepath.Join(t.TempDir(), "detached")}
|
|
}, seenSomething},
|
|
{"no roots configured", func(*testing.T) []string { return nil }, seenSomething},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
s := testScanner(t, tc.roots(t)...)
|
|
q := &fakeReconciler{rows: []dbq.ListTrackPathsForReconcileRow{row(1, "/music/a.mp3", false)}}
|
|
var stats Stats
|
|
if err := s.reconcileMissing(context.Background(), q, tc.seen, &stats); err == nil {
|
|
t.Fatal("reconcile went ahead; want a refusal")
|
|
}
|
|
if len(q.marked) != 0 {
|
|
t.Errorf("marked rows: %v", q.marked)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// A fresh install has no tracks yet; its first scan must not fail.
|
|
func TestReconcileMissing_NoRowsIsNotAnError(t *testing.T) {
|
|
root := populatedRoot(t)
|
|
s := testScanner(t, root)
|
|
q := &fakeReconciler{}
|
|
|
|
var stats Stats
|
|
if err := s.reconcileMissing(context.Background(), q, map[string]struct{}{"/x": {}}, &stats); err != nil {
|
|
t.Fatalf("empty library should reconcile cleanly, got %v", err)
|
|
}
|
|
}
|