feat(scanner): repoint rows on cross-library moves of identical content
Previously the scanner's SHA-256 dedup was library-scoped: moving a book between libraries created a duplicate row (new ID) while the old row went missing and archived after two scans, orphaning reading progress and annotations from the file that users still see. processMediaFile now falls through to cross-library detection when the same-library hash lookup misses: - GetMediaItemsBySHA256AndSameLibraryType returns candidates in other same-type libraries; each candidate's file is stat'd through ITS OWN library's folders (the old existence check stat'd against the current scan's folder tree, which is meaningless across libraries). - File gone -> it is a move: the candidate must pass the target library's type rules (ValidateMediaItemForLibrary), then the row is repointed via MoveMediaItemToLibrary with the recomputed relative path. Reading history, annotations, and collections follow automatically because the row keeps its ID. Incompatible formats (e.g. a reflowable EPUB into a manga library) are rejected with a processing issue instead of being force-imported; the upsert on (media_item_id, issue_type) keeps repeated scans from spamming duplicates. - File still present -> deliberate multi-library copy: fall through to normal import so both libraries keep independent rows. Candidate selection is factored into selectMoveCandidate (pure function, unit-tested in media_scanner_move_test.go): input arrives pre-ordered (archived first, then most missing scans, then oldest) and only candidates whose file is verifiably gone qualify, so deliberate copies are never repointed.
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
func mustUUID(t *testing.T, raw string) pgtype.UUID {
|
||||
t.Helper()
|
||||
parsed, err := uuid.Parse(raw)
|
||||
if err != nil {
|
||||
t.Fatalf("invalid test uuid %q: %v", raw, err)
|
||||
}
|
||||
return pgtype.UUID{Bytes: parsed, Valid: true}
|
||||
}
|
||||
|
||||
// alwaysExists simulates every candidate's file still being on disk:
|
||||
// deliberate copies across libraries must never be repointed.
|
||||
func TestSelectMoveCandidateAllFilesExist(t *testing.T) {
|
||||
candidates := []moveCandidate{
|
||||
{ID: mustUUID(t, "00000000-0000-0000-0000-000000000001"), FilePath: "a/book.epub"},
|
||||
{ID: mustUUID(t, "00000000-0000-0000-0000-000000000002"), FilePath: "b/book.epub"},
|
||||
}
|
||||
|
||||
if _, ok := selectMoveCandidate(candidates, func(moveCandidate) bool { return true }); ok {
|
||||
t.Fatal("expected no move candidate when every file still exists")
|
||||
}
|
||||
}
|
||||
|
||||
// Input arrives pre-ordered (archived first, then most missing scans, then
|
||||
// oldest), so the first missing candidate must win.
|
||||
func TestSelectMoveCandidatePicksFirstMissing(t *testing.T) {
|
||||
onDisk := moveCandidate{ID: mustUUID(t, "00000000-0000-0000-0000-000000000001"), FilePath: "a/book.epub"}
|
||||
gone := moveCandidate{ID: mustUUID(t, "00000000-0000-0000-0000-000000000002"), FilePath: "b/book.epub"}
|
||||
candidates := []moveCandidate{onDisk, gone}
|
||||
|
||||
got, ok := selectMoveCandidate(candidates, func(c moveCandidate) bool {
|
||||
return c.ID == onDisk.ID
|
||||
})
|
||||
if !ok {
|
||||
t.Fatal("expected a move candidate")
|
||||
}
|
||||
if got.ID != gone.ID {
|
||||
t.Fatalf("expected candidate %v, got %v", gone.ID, got.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSelectMoveCandidateOnlyMissingQualifies(t *testing.T) {
|
||||
gone := moveCandidate{ID: mustUUID(t, "00000000-0000-0000-0000-000000000001"), FilePath: "old/book.epub"}
|
||||
candidates := []moveCandidate{gone}
|
||||
|
||||
got, ok := selectMoveCandidate(candidates, func(moveCandidate) bool { return false })
|
||||
if !ok {
|
||||
t.Fatal("expected the missing candidate to qualify")
|
||||
}
|
||||
if got.ID != gone.ID {
|
||||
t.Fatalf("expected candidate %v, got %v", gone.ID, got.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSelectMoveCandidateEmptyList(t *testing.T) {
|
||||
if _, ok := selectMoveCandidate(nil, func(moveCandidate) bool { return false }); ok {
|
||||
t.Fatal("expected no candidate for an empty list")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user