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") } }