test(library): merge repeated cases into tables, drop tests of other code (#8296)
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
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>
This commit is contained in:
@@ -2,7 +2,6 @@ package library
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
@@ -99,13 +98,15 @@ func testScanner(t *testing.T, roots ...string) *Scanner {
|
||||
}
|
||||
}
|
||||
|
||||
// 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. (An earlier version of this
|
||||
// test used 2-of-4 and was really testing the guard by accident.)
|
||||
// this exercises marking rather than the cap.
|
||||
rows := make([]dbq.ListTrackPathsForReconcileRow, 0, 10)
|
||||
seen := map[string]struct{}{}
|
||||
for i := 0; i < 10; i++ {
|
||||
@@ -121,11 +122,8 @@ func TestReconcileMissing_MarksRowsTheWalkDidNotSee(t *testing.T) {
|
||||
if err := s.reconcileMissing(context.Background(), q, seen, &stats); err != nil {
|
||||
t.Fatalf("reconcile: %v", err)
|
||||
}
|
||||
if len(q.marked) != 2 {
|
||||
t.Fatalf("marked %d rows, want 2", len(q.marked))
|
||||
}
|
||||
if q.marked[0] != testUUID(0) || q.marked[1] != testUUID(1) {
|
||||
t.Errorf("marked the wrong rows: %v", q.marked)
|
||||
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)
|
||||
@@ -133,8 +131,20 @@ func TestReconcileMissing_MarksRowsTheWalkDidNotSee(t *testing.T) {
|
||||
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)
|
||||
@@ -160,6 +170,9 @@ func TestReconcileMissing_ClearsRowsWhoseFileReturned(t *testing.T) {
|
||||
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
|
||||
@@ -242,67 +255,40 @@ func TestReconcileMissing_ClearsEvenWhenMarkCapTrips(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestReconcileMissing_RefusesOnEmptyWalk(t *testing.T) {
|
||||
root := populatedRoot(t)
|
||||
s := testScanner(t, root)
|
||||
q := &fakeReconciler{rows: []dbq.ListTrackPathsForReconcileRow{
|
||||
row(1, "/music/a.mp3", false),
|
||||
}}
|
||||
|
||||
var stats Stats
|
||||
if err := s.reconcileMissing(context.Background(), q, map[string]struct{}{}, &stats); err == nil {
|
||||
t.Fatal("expected refusal when the walk saw no files")
|
||||
}
|
||||
if len(q.marked) != 0 {
|
||||
t.Errorf("marked rows on an empty walk: %v", q.marked)
|
||||
}
|
||||
}
|
||||
|
||||
// The unmounted-volume case: the configured root doesn't exist at all.
|
||||
func TestReconcileMissing_RefusesWhenRootMissing(t *testing.T) {
|
||||
s := testScanner(t, filepath.Join(t.TempDir(), "not-mounted"))
|
||||
q := &fakeReconciler{rows: []dbq.ListTrackPathsForReconcileRow{
|
||||
row(1, "/music/a.mp3", false),
|
||||
}}
|
||||
|
||||
var stats Stats
|
||||
if err := s.reconcileMissing(context.Background(), q, map[string]struct{}{"/x": {}}, &stats); err == nil {
|
||||
t.Fatal("expected refusal when a scan root is absent")
|
||||
}
|
||||
if len(q.marked) != 0 {
|
||||
t.Errorf("marked rows with an absent root: %v", q.marked)
|
||||
}
|
||||
}
|
||||
|
||||
// A mount point that exists but has nothing mounted on it: os.Stat succeeds on
|
||||
// the bare directory, which is why emptiness is checked separately.
|
||||
func TestReconcileMissing_RefusesWhenRootEmpty(t *testing.T) {
|
||||
s := testScanner(t, t.TempDir())
|
||||
q := &fakeReconciler{rows: []dbq.ListTrackPathsForReconcileRow{
|
||||
row(1, "/music/a.mp3", false),
|
||||
}}
|
||||
|
||||
var stats Stats
|
||||
if err := s.reconcileMissing(context.Background(), q, map[string]struct{}{"/x": {}}, &stats); err == nil {
|
||||
t.Fatal("expected refusal when a scan root is empty")
|
||||
}
|
||||
}
|
||||
|
||||
// Several roots, one detached. Marking must not proceed on partial evidence
|
||||
// just because the other roots looked fine.
|
||||
func TestReconcileMissing_RefusesWhenAnyRootMissing(t *testing.T) {
|
||||
good := populatedRoot(t)
|
||||
s := testScanner(t, good, filepath.Join(t.TempDir(), "detached"))
|
||||
q := &fakeReconciler{rows: []dbq.ListTrackPathsForReconcileRow{
|
||||
row(1, "/music/a.mp3", false),
|
||||
}}
|
||||
|
||||
var stats Stats
|
||||
if err := s.reconcileMissing(context.Background(), q, map[string]struct{}{"/x": {}}, &stats); err == nil {
|
||||
t.Fatal("expected refusal when one of several roots is absent")
|
||||
// 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)
|
||||
@@ -313,88 +299,3 @@ func TestReconcileMissing_NoRowsIsNotAnError(t *testing.T) {
|
||||
t.Fatalf("empty library should reconcile cleanly, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReconcileMissing_PropagatesListError(t *testing.T) {
|
||||
root := populatedRoot(t)
|
||||
s := testScanner(t, root)
|
||||
sentinel := errors.New("boom")
|
||||
q := &fakeReconciler{listErr: sentinel}
|
||||
|
||||
var stats Stats
|
||||
err := s.reconcileMissing(context.Background(), q, map[string]struct{}{"/x": {}}, &stats)
|
||||
if !errors.Is(err, sentinel) {
|
||||
t.Fatalf("err = %v, want it to wrap %v", err, sentinel)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyRootsPresent_NoRootsConfigured(t *testing.T) {
|
||||
s := testScanner(t)
|
||||
if err := s.verifyRootsPresent(); err == nil {
|
||||
t.Fatal("expected an error with no scan roots configured")
|
||||
}
|
||||
}
|
||||
|
||||
// Without a change row the mark is invisible to every client:
|
||||
// /api/library/sync is a change-log feed, so a plain UPDATE never reaches
|
||||
// anyone, and the wire field would exist with nothing ever telling a client to
|
||||
// re-read the track.
|
||||
func TestReconcileMissing_MarkingEmitsSyncChanges(t *testing.T) {
|
||||
s := testScanner(t, populatedRoot(t))
|
||||
|
||||
// 10 rows, 2 absent — under the cap, so this exercises marking.
|
||||
rows := make([]dbq.ListTrackPathsForReconcileRow, 0, 10)
|
||||
seen := map[string]struct{}{}
|
||||
for i := 0; i < 10; i++ {
|
||||
path := fmt.Sprintf("/music/track-%02d.mp3", i)
|
||||
rows = append(rows, row(byte(i), path, false))
|
||||
if i >= 2 {
|
||||
seen[path] = 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.loggedChanges) != 1 {
|
||||
t.Fatalf("want one batch of change rows, got %d", len(q.loggedChanges))
|
||||
}
|
||||
got := q.loggedChanges[0]
|
||||
if got.EntityType != "track" {
|
||||
t.Errorf("entity_type = %q, want track", got.EntityType)
|
||||
}
|
||||
// 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.Op != "upsert" {
|
||||
t.Errorf("op = %q, want upsert — the row survives, only its state changed", got.Op)
|
||||
}
|
||||
if len(got.EntityIds) != 2 {
|
||||
t.Errorf("want both missing tracks logged, got %d ids", len(got.EntityIds))
|
||||
}
|
||||
}
|
||||
|
||||
// A file coming back must reach clients too, or a restored track stays
|
||||
// greyed out on every device until something unrelated touches it.
|
||||
func TestReconcileMissing_RestoringEmitsSyncChanges(t *testing.T) {
|
||||
s := testScanner(t, populatedRoot(t))
|
||||
q := &fakeReconciler{rows: []dbq.ListTrackPathsForReconcileRow{
|
||||
row(1, "/music/back.mp3", true),
|
||||
}}
|
||||
var stats Stats
|
||||
|
||||
if err := s.reconcileMissing(
|
||||
context.Background(), q, map[string]struct{}{"/music/back.mp3": {}}, &stats,
|
||||
); err != nil {
|
||||
t.Fatalf("reconcile: %v", err)
|
||||
}
|
||||
|
||||
if len(q.loggedChanges) != 1 {
|
||||
t.Fatalf("a restored file must produce a change row, got %d batches", len(q.loggedChanges))
|
||||
}
|
||||
if q.loggedChanges[0].Op != "upsert" {
|
||||
t.Errorf("op = %q, want upsert", q.loggedChanges[0].Op)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user