package storage import ( "context" "errors" "slices" "testing" "time" "github.com/uptrace/bun" "git.kmalloc.org/squid/sonda/internal/duplicates" "git.kmalloc.org/squid/sonda/internal/library" "git.kmalloc.org/squid/sonda/internal/listening" ) // song is a scanned file: a title on an album by artists, with the facts that tell copies apart. func song(rel, title, album string, durationMs, size int64, format string, bitrate int, artists ...string) library.ParsedFile { p := parsed(rel, title, album, artists...) p.DurationMs, p.Size, p.Format, p.BitRate = durationMs, size, format, bitrate return p } func saved(t *testing.T, repo *LibraryRepo, lib int64, f library.ParsedFile) { t.Helper() if _, err := repo.SaveFile(context.Background(), lib, f, 0); err != nil { t.Fatal(err) } } func trackOf(t *testing.T, db *bun.DB, rel string) int64 { t.Helper() var id int64 if err := db.QueryRow("SELECT track_id FROM audio_files WHERE rel_path = ?", rel).Scan(&id); err != nil { t.Fatal(err) } return id } func candidateByRel(t *testing.T, db *bun.DB, cs []duplicates.Candidate, rel string) (duplicates.Candidate, bool) { t.Helper() id := trackOf(t, db, rel) for _, c := range cs { if c.TrackID == id { return c, true } } return duplicates.Candidate{}, false } func TestDuplicateCandidatesAreTheFilesWhoseTitleIsSharedWithAnotherFile(t *testing.T) { forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { ctx := context.Background() lib := NewLibraryRepo(db) l := mkLib(t, lib, "music") saved(t, lib, l, song("a/1.flac", "Song", "Album X", 200_000, 30_000_000, "flac", 900, "Ann")) saved(t, lib, l, song("b/2.mp3", "Song", "Album Y", 201_000, 5_000_000, "mp3", 192, "Ann")) saved(t, lib, l, song("c/3.flac", "Song", "Album Z", 200_000, 30_000_000, "flac", 900, "Bob")) saved(t, lib, l, song("d/4.flac", "Song", "Album W", 200_000, 30_000_000, "flac", 900, "Ann", "Bob")) saved(t, lib, l, song("e/5.flac", "Other", "Album X", 200_000, 30_000_000, "flac", 900, "Ann")) cs, err := NewDuplicatesRepo(db).Candidates(ctx) if err != nil { t.Fatal(err) } if _, ok := candidateByRel(t, db, cs, "e/5.flac"); ok { t.Error("a title nobody shares is a candidate") } if len(cs) != 4 { t.Fatalf("%d candidates, want the 4 files called Song", len(cs)) } c, ok := candidateByRel(t, db, cs, "b/2.mp3") if !ok { t.Fatal("the mp3 is not a candidate") } if c.Title != "Song" || c.TitleKey != "song" || c.Album != "Album Y" || c.Format != "mp3" || c.BitRate != 192 || c.SizeBytes != 5_000_000 || c.DurationMs != 201_000 || c.FileID == 0 || c.Hash != "" { t.Errorf("candidate = %+v", c) } if len(c.Artists) != 1 || c.Artists[0].Name != "Ann" { t.Errorf("artists = %+v, want Ann", c.Artists) } duo, _ := candidateByRel(t, db, cs, "d/4.flac") if len(duo.Artists) != 2 { t.Errorf("a duet has %d artists, want 2", len(duo.Artists)) } // Seen through the grouping, the two by Ann alone are the same song, the others are not. groups := duplicates.Build(cs, duplicates.DurationTolerance) if len(groups) != 1 || len(groups[0].Files) != 2 { t.Errorf("groups = %+v, want one of the two copies by Ann", groups) } }) } func TestAFileFlaggedMissingIsNotACandidate(t *testing.T) { forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { ctx := context.Background() lib := NewLibraryRepo(db) l := mkLib(t, lib, "music") saved(t, lib, l, song("a/1.flac", "Song", "A", 200_000, 1, "flac", 900, "Ann")) saved(t, lib, l, song("b/2.flac", "Song", "B", 200_000, 1, "flac", 900, "Ann")) repo := NewDuplicatesRepo(db) if cs, _ := repo.Candidates(ctx); len(cs) != 2 { t.Fatalf("%d candidates before, want 2", len(cs)) } var id int64 if err := db.QueryRow("SELECT id FROM audio_files WHERE rel_path = 'b/2.flac'").Scan(&id); err != nil { t.Fatal(err) } if err := lib.MarkMissing(ctx, []int64{id}, time.Now()); err != nil { t.Fatal(err) } if cs, _ := repo.Candidates(ctx); len(cs) != 0 { t.Errorf("%d candidates after one went missing, want 0", len(cs)) } }) } func TestFileOfSaysWhereTheFileIsAndWhatItLookedLike(t *testing.T) { forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { ctx := context.Background() lib := NewLibraryRepo(db) l := mkLib(t, lib, "music") saved(t, lib, l, song("a/1.flac", "Song", "A", 200_000, 1234, "flac", 900, "Ann")) repo := NewDuplicatesRepo(db) rec, err := repo.FileOf(ctx, trackOf(t, db, "a/1.flac")) if err != nil { t.Fatal(err) } if rec.Root != "/lib/music" || rec.RelPath != "a/1.flac" || rec.SizeBytes != 1234 || rec.MTimeUnix != 1_700_000_000 || rec.Hash != "" || rec.Missing { t.Errorf("record = %+v", rec) } if _, err := repo.FileOf(ctx, 9999); !errors.Is(err, duplicates.ErrTrackNotFound) { t.Errorf("an unknown track gave %v, want ErrTrackNotFound", err) } }) } func TestAHashIsStoredOnlyForTheFileThatWasRead(t *testing.T) { forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { ctx := context.Background() lib := NewLibraryRepo(db) l := mkLib(t, lib, "music") saved(t, lib, l, song("a/1.flac", "Song", "A", 200_000, 1234, "flac", 900, "Ann")) saved(t, lib, l, song("b/2.flac", "Song", "B", 200_000, 1234, "flac", 900, "Ann")) repo := NewDuplicatesRepo(db) rec, _ := repo.FileOf(ctx, trackOf(t, db, "a/1.flac")) // The file changed after it was read: its hash is no hash of it. if ok, err := repo.SaveHash(ctx, rec.FileID, rec.SizeBytes+1, rec.MTimeUnix, "aa"); err != nil || ok { t.Errorf("a hash of a file that changed in size was stored: ok=%v err=%v", ok, err) } if ok, err := repo.SaveHash(ctx, rec.FileID, rec.SizeBytes, rec.MTimeUnix+1, "aa"); err != nil || ok { t.Errorf("a hash of a file that changed in date was stored: ok=%v err=%v", ok, err) } if ok, err := repo.SaveHash(ctx, rec.FileID, rec.SizeBytes, rec.MTimeUnix, "aa"); err != nil || !ok { t.Fatalf("the hash was not stored: ok=%v err=%v", ok, err) } cs, _ := repo.Candidates(ctx) c, _ := candidateByRel(t, db, cs, "a/1.flac") other, _ := candidateByRel(t, db, cs, "b/2.flac") if c.Hash != "aa" || other.Hash != "" { t.Errorf("hashes = %q and %q, want aa and nothing", c.Hash, other.Hash) } }) } // A hash says "this is what the file held when it was read". A scan that finds the file changed forgets it. func TestAScanThatFindsAFileChangedForgetsItsHash(t *testing.T) { forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { ctx := context.Background() lib := NewLibraryRepo(db) l := mkLib(t, lib, "music") saved(t, lib, l, song("a/1.flac", "Song", "A", 200_000, 1234, "flac", 900, "Ann")) repo := NewDuplicatesRepo(db) rec, _ := repo.FileOf(ctx, trackOf(t, db, "a/1.flac")) if _, err := repo.SaveHash(ctx, rec.FileID, rec.SizeBytes, rec.MTimeUnix, "aa"); err != nil { t.Fatal(err) } // The same file read again, unchanged (a full rescan): the hash is still true. if _, err := lib.SaveFile(ctx, l, song("a/1.flac", "Song", "A", 200_000, 1234, "flac", 900, "Ann"), rec.FileID); err != nil { t.Fatal(err) } if got, _ := repo.FileOf(ctx, rec.TrackID); got.Hash != "aa" { t.Errorf("an unchanged file lost its hash: %q", got.Hash) } // The file was edited: another size. if _, err := lib.SaveFile(ctx, l, song("a/1.flac", "Song", "A", 200_000, 4321, "flac", 900, "Ann"), rec.FileID); err != nil { t.Fatal(err) } if got, _ := repo.FileOf(ctx, rec.TrackID); got.Hash != "" || got.SizeBytes != 4321 { t.Errorf("after a change: hash %q size %d, want no hash and the new size", got.Hash, got.SizeBytes) } }) } func TestRemovingACopyMovesItsListensToTheOneThatStays(t *testing.T) { forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { ctx := context.Background() lib := NewLibraryRepo(db) l := mkLib(t, lib, "music") saved(t, lib, l, song("a/1.flac", "Song", "A", 200_000, 1, "flac", 900, "Ann")) saved(t, lib, l, song("b/2.mp3", "Song", "B", 200_000, 1, "mp3", 192, "Ann")) keep, gone := trackOf(t, db, "a/1.flac"), trackOf(t, db, "b/2.mp3") user, err := NewUserRepo(db).CreateUser(ctx, "alice", "h", false) if err != nil { t.Fatal(err) } plays := NewListeningRepo(db) for _, e := range []struct { track int64 key string }{{keep, "k1"}, {gone, "g1"}, {gone, "g2"}} { if _, err := plays.Record(ctx, listening.Event{UserID: user.ID, TrackID: e.track, Type: listening.Complete, DurationPlayedMs: 200_000, IdempotencyKey: e.key}); err != nil { t.Fatal(err) } } if err := NewDuplicatesRepo(db).RemoveCopy(ctx, keep, gone); err != nil { t.Fatal(err) } if n := count(t, db, "SELECT COUNT(*) FROM listening_events WHERE track_id = ?", keep); n != 3 { t.Errorf("the track that stays has %d listens, want its own and the 2 moved (3)", n) } if n := count(t, db, "SELECT COUNT(*) FROM listening_events"); n != 3 { t.Errorf("%d listens in all, want 3: none lost", n) } if n := count(t, db, "SELECT COUNT(*) FROM tracks WHERE id = ?", gone); n != 0 { t.Error("the removed track is still there") } if n := count(t, db, "SELECT COUNT(*) FROM audio_files WHERE track_id = ?", gone); n != 0 { t.Error("the file row of the removed track is still there") } if n := count(t, db, "SELECT COUNT(*) FROM track_artists WHERE track_id = ?", gone); n != 0 { t.Error("the credits of the removed track are still there") } if n := count(t, db, "SELECT COUNT(*) FROM tracks WHERE id = ?", keep); n != 1 { t.Error("the track that stays is gone") } }) } func TestRemovingACopyKeepsItsPlaceInThePlaylists(t *testing.T) { forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { ctx := context.Background() lib := NewLibraryRepo(db) l := mkLib(t, lib, "music") saved(t, lib, l, song("a/1.flac", "Song", "A", 200_000, 1, "flac", 900, "Ann")) saved(t, lib, l, song("b/2.mp3", "Song", "B", 200_000, 1, "mp3", 192, "Ann")) saved(t, lib, l, song("c/3.mp3", "Other", "C", 100_000, 1, "mp3", 192, "Ann")) keep, gone, other := trackOf(t, db, "a/1.flac"), trackOf(t, db, "b/2.mp3"), trackOf(t, db, "c/3.mp3") user, err := NewUserRepo(db).CreateUser(ctx, "alice@example.org", "h", false) if err != nil { t.Fatal(err) } pl := NewPlaylistRepo(db) now := time.Unix(1_700_000_000, 0) p, err := pl.Create(ctx, user.ID, "Mix", "", now) if err != nil { t.Fatal(err) } // The song twice in a row (the removed copy between two others), then another song. if _, err := pl.AddTracks(ctx, user.ID, p.ID, []int64{keep, gone, other}, nil, now); err != nil { t.Fatal(err) } if err := NewDuplicatesRepo(db).RemoveCopy(ctx, keep, gone); err != nil { t.Fatal(err) } items, total, err := pl.Items(ctx, user.ID, p.ID, 50, 0) if err != nil { t.Fatal(err) } if total != 3 || len(items) != 3 { t.Fatalf("the playlist has %d items, want 3: the removed copy's place must stay", total) } if got := []int64{items[0].Track.ID, items[1].Track.ID, items[2].Track.ID}; !slices.Equal(got, []int64{keep, keep, other}) { t.Errorf("tracks in the playlist = %v, want %v", got, []int64{keep, keep, other}) } }) } func TestRemovingACopyNeedsTwoTracksThatExistAndAreDifferent(t *testing.T) { forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { ctx := context.Background() lib := NewLibraryRepo(db) l := mkLib(t, lib, "music") saved(t, lib, l, song("a/1.flac", "Song", "A", 200_000, 1, "flac", 900, "Ann")) keep := trackOf(t, db, "a/1.flac") repo := NewDuplicatesRepo(db) if err := repo.RemoveCopy(ctx, keep, 9999); !errors.Is(err, duplicates.ErrTrackNotFound) { t.Errorf("a track that does not exist: %v, want ErrTrackNotFound", err) } if err := repo.RemoveCopy(ctx, 9999, keep); !errors.Is(err, duplicates.ErrTrackNotFound) { t.Errorf("a track to keep that does not exist: %v, want ErrTrackNotFound", err) } if err := repo.RemoveCopy(ctx, keep, keep); !errors.Is(err, duplicates.ErrInvalid) { t.Errorf("a track removed in favour of itself: %v, want ErrInvalid", err) } if n := count(t, db, "SELECT COUNT(*) FROM tracks"); n != 1 { t.Errorf("%d tracks left, want the one that was there", n) } }) }