release / web (push) Successful in 1m40s
release / go (push) Failing after 2m7s
release / govulncheck (push) Successful in 21s
release / Attach APK to the Release (tag releases only) (push) Canceled after 0s
release / Build + push container image (push) Canceled after 0s
release / Publish the release image to GHCR (dispatch only) (push) Canceled after 0s
release / Verify release artifacts (tag releases only) (push) Canceled after 0s
release / android (push) Canceled after 1m2s
release / Build signed APK (push) Canceled after 23s
release / integration (push) Canceled after 2m48s
Every case marked every track, so the 25% cap refused before the root checks were reached: run 11272 broke the empty-root check and the test still passed. The walk now marks one track of ten and clears a returned one, which reconcile would do, so only the root and empty-walk checks can refuse it; and the test asserts nothing was cleared either, since those checks run before restores. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
312 lines
10 KiB
Go
312 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 changes nothing, neither marking nor clearing:
|
|
// 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. The walk below
|
|
// would otherwise mark one track of ten, under missingMarkMaxFraction, and
|
|
// clear a returned one, so only these checks can refuse it.
|
|
func TestReconcileMissing_RefusesWhenAVolumeLooksAbsent(t *testing.T) {
|
|
rows := []dbq.ListTrackPathsForReconcileRow{row(99, "/music/back.mp3", true)}
|
|
walked := map[string]struct{}{"/music/back.mp3": {}}
|
|
for i := 0; i < 10; i++ {
|
|
p := fmt.Sprintf("/music/track-%02d.mp3", i)
|
|
rows = append(rows, row(byte(i), p, false))
|
|
if i > 0 {
|
|
walked[p] = struct{}{}
|
|
}
|
|
}
|
|
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")} }, walked},
|
|
{"a root is empty", func(t *testing.T) []string { return []string{t.TempDir()} }, walked},
|
|
{"one of several roots is absent", func(t *testing.T) []string {
|
|
return []string{populatedRoot(t), filepath.Join(t.TempDir(), "detached")}
|
|
}, walked},
|
|
{"no roots configured", func(*testing.T) []string { return nil }, walked},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
s := testScanner(t, tc.roots(t)...)
|
|
q := &fakeReconciler{rows: rows}
|
|
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 || len(q.cleared) != 0 {
|
|
t.Errorf("marked %v, cleared %v; want nothing changed", q.marked, q.cleared)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
}
|