sonda/internal/library/plan_test.go
tgiachi b0a7a562a6 fix(discovery): purge lock, cleanup chains and rescans that cannot read
- fix: on PostgreSQL a purge could delete a discovered artist while a scan was
  adopting it. The scan then failed on the missing artist for the whole file,
  and the two could also deadlock. Purge now takes the same artist lock as the
  scanner and discovery before it removes anything. Saving a list or tags does
  too, and an artist that is gone by then is reported as not found instead of a
  foreign key error, so a user who opened an artist that was removed meanwhile
  gets a 404
- fix: a discovered artist that was only the source of a list stayed forever and
  kept the artists of its own list alive, so chains built up. Cleanup now removes
  such an artist and then follows the chain until nothing more goes
- fix: in a full rescan a file that came back but could not be read stayed
  marked missing. It is restored first, as in a normal scan, and read as well
- fix: ?full given twice is refused, so a typo cannot start a scan of another
  kind
- fix: genre cleaning lives in one place, used by the reader and the store
- docs: the files genres count the tracks the artist is credited on, and the spec
  says every files tag is a genre
- test: a purge colliding with a scan on both databases (it fails without the
  lock), a chain of discovered artists, saving for an artist that is gone, a
  file that returns unreadable during a full scan
2026-11-11 17:36:14 +01:00

71 lines
2.8 KiB
Go

package library
import "testing"
// A file whose stat failed for a transient reason is unknown, not gone.
func TestPlanDoesNotFlagFilesWithStatErrors(t *testing.T) {
existing := []ExistingFile{
{ID: 1, FileInfo: FileInfo{RelPath: "flaky.mp3", Size: 10, MTimeUnix: 5}},
{ID: 2, FileInfo: FileInfo{RelPath: "gone.mp3", Size: 20, MTimeUnix: 6}},
}
w := walkResult{
files: []FileInfo{{RelPath: "new.mp3", Size: 10, MTimeUnix: 5}}, // same size+mtime as flaky.mp3
failedFiles: map[string]bool{"flaky.mp3": true},
}
p := makePlan(w, existing, scanOptions{})
for _, id := range p.missing {
if id == 1 {
t.Error("flaky.mp3 was marked missing after a stat error")
}
}
if len(p.renames) != 0 {
t.Errorf("renames = %+v: a file with a stat error must not be a rename source", p.renames)
}
if len(p.missing) != 1 || p.missing[0] != 2 {
t.Errorf("missing = %v, want only gone.mp3", p.missing)
}
if len(p.parse) != 1 || p.parse[0].file.RelPath != "new.mp3" {
t.Errorf("parse = %+v, want new.mp3 as a new file", p.parse)
}
}
// A full rescan reads every file again, whether or not it changed, so what the
// reader learned since (ids, genres) reaches files that are already indexed.
func TestFullPlanReadsEveryPresentFileAgain(t *testing.T) {
existing := []ExistingFile{
{ID: 1, FileInfo: FileInfo{RelPath: "same.mp3", Size: 10, MTimeUnix: 5}},
{ID: 2, FileInfo: FileInfo{RelPath: "changed.mp3", Size: 20, MTimeUnix: 6}},
{ID: 3, FileInfo: FileInfo{RelPath: "back.mp3", Size: 30, MTimeUnix: 7}, Missing: true},
{ID: 4, FileInfo: FileInfo{RelPath: "gone.mp3", Size: 40, MTimeUnix: 8}},
}
w := walkResult{files: []FileInfo{
{RelPath: "same.mp3", Size: 10, MTimeUnix: 5},
{RelPath: "changed.mp3", Size: 21, MTimeUnix: 6},
{RelPath: "back.mp3", Size: 30, MTimeUnix: 7},
{RelPath: "new.mp3", Size: 50, MTimeUnix: 9},
}}
normal := makePlan(w, existing, scanOptions{})
if len(normal.parse) != 2 || normal.unchanged != 1 || len(normal.restore) != 1 {
t.Fatalf("normal plan: parse=%d unchanged=%d restore=%d", len(normal.parse), normal.unchanged, len(normal.restore))
}
full := makePlan(w, existing, scanOptions{full: true})
ids := map[int64]bool{}
for _, j := range full.parse {
ids[j.existingID] = true
}
if len(full.parse) != 4 || !ids[1] || !ids[2] || !ids[3] || !ids[0] {
t.Errorf("full plan parses %+v, want the four present files (three known, one new)", full.parse)
}
// A file that came back is restored even if reading it fails this time, as in a
// normal scan, and it is read as well.
if full.unchanged != 0 || len(full.restore) != 1 || full.restore[0] != 3 {
t.Errorf("full plan: unchanged=%d restore=%v, want only back.mp3 restored", full.unchanged, full.restore)
}
// What is gone is still gone.
if len(full.missing) != 1 || full.missing[0] != 4 {
t.Errorf("missing = %v, want only gone.mp3", full.missing)
}
}