package library import ( "context" "errors" "io" "log/slog" "os" "path/filepath" "syscall" "testing" "github.com/jackc/pgx/v5/pgtype" "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" "git.fabledsword.com/bvandeusen/minstrel/internal/dbtest" ) // A removed file's track stays, missing, with its likes and plays and the // reason it waits, and the good copy that lands later takes its history over. func TestRemoveFile_KeepsTheTrackUntilAGoodCopyLands(t *testing.T) { pool := newPool(t) ctx := context.Background() q := dbq.New(pool) admin, err := q.CreateUser(ctx, dbq.CreateUserParams{ Username: dbtest.TestUserPrefix + "remover", PasswordHash: "x", ApiTokenHash: "remover", IsAdmin: true, }) if err != nil { t.Fatal(err) } dir := t.TempDir() bad := filepath.Join(dir, "01 bad.mp3") if err := os.WriteFile(bad, []byte("x"), 0o644); err != nil { t.Fatal(err) } track, album, artist := seedTrack(t, pool, bad) mbid := "rec-kept" if _, err := pool.Exec(ctx, `UPDATE tracks SET mbid = $2, mbid_source = 'tag' WHERE id = $1`, track.ID, mbid); err != nil { t.Fatal(err) } if _, err := pool.Exec(ctx, `INSERT INTO general_likes (user_id, track_id) VALUES ($1, $2)`, admin.ID, track.ID); err != nil { t.Fatal(err) } if _, err := RemoveFile(ctx, pool, slog.New(slog.NewTextHandler(io.Discard, nil)), RemoveFileParams{ TrackID: track.ID, ActorID: admin.ID, Reason: RemovedForReport, }); err != nil { t.Fatalf("RemoveFile: %v", err) } if _, err := os.Stat(bad); !errors.Is(err, os.ErrNotExist) { t.Fatalf("the file is still there: %v", err) } waiting, err := q.GetTrackByID(ctx, track.ID) if err != nil { t.Fatalf("the track went with its file: %v", err) } if !waiting.MissingSince.Valid || waiting.RemovedReason == nil || *waiting.RemovedReason != "report" || waiting.RemovedBy != admin.ID { t.Fatalf("waiting track = missing %v, reason %v, by %v; want missing, report, the admin", waiting.MissingSince.Valid, waiting.RemovedReason, waiting.RemovedBy) } if waiting.Mbid == nil || *waiting.Mbid != mbid { t.Fatalf("mbid = %v, want %q kept for a report that does not doubt it", waiting.Mbid, mbid) } // A good copy lands on the album under the song's title. good, err := q.UpsertTrack(ctx, dbq.UpsertTrackParams{ Title: track.Title, AlbumID: album.ID, ArtistID: artist.ID, DurationMs: 1000, FilePath: filepath.Join(dir, "01 good.flac"), FileSize: 100, FileFormat: "flac", }) if err != nil { t.Fatal(err) } if _, err := PairMissingTracks(ctx, pool, nil); err != nil { t.Fatalf("PairMissingTracks: %v", err) } var likes int if err := pool.QueryRow(ctx, `SELECT count(*) FROM general_likes WHERE track_id = $1`, good.ID).Scan(&likes); err != nil { t.Fatal(err) } if likes != 1 { t.Fatalf("the good copy has %d likes, want the removed track's one", likes) } } // ForgetMBID drops the recording MBID that came from a file that was not the // track's song. func TestRemoveFile_ForgetsADoubtedMBID(t *testing.T) { pool := newPool(t) ctx := context.Background() path := filepath.Join(t.TempDir(), "rip.mp3") if err := os.WriteFile(path, []byte("x"), 0o644); err != nil { t.Fatal(err) } track, _, _ := seedTrack(t, pool, path) if _, err := pool.Exec(ctx, `UPDATE tracks SET mbid = 'rec-wrong', mbid_source = 'tag' WHERE id = $1`, track.ID); err != nil { t.Fatal(err) } if _, err := RemoveFile(ctx, pool, nil, RemoveFileParams{ TrackID: track.ID, Reason: RemovedForSuspect, ForgetMBID: true, }); err != nil { t.Fatalf("RemoveFile: %v", err) } got, err := dbq.New(pool).GetTrackByID(ctx, track.ID) if err != nil { t.Fatal(err) } if got.Mbid != nil { t.Fatalf("mbid = %q, want it forgotten", *got.Mbid) } } // A file that cannot be removed leaves the track as it was. func TestRemoveFile_FailedRemovalMarksNothing(t *testing.T) { pool := newPool(t) ctx := context.Background() path := filepath.Join(t.TempDir(), "stuck.mp3") if err := os.WriteFile(path, []byte("x"), 0o644); err != nil { t.Fatal(err) } track, _, _ := seedTrack(t, pool, path) stubRemoveFile(t, func(p string) error { return &os.PathError{Op: "remove", Path: p, Err: syscall.EROFS} }) _, err := RemoveFile(ctx, pool, nil, RemoveFileParams{TrackID: track.ID, Reason: RemovedForReport}) var fre *FileRemoveError if !errors.As(err, &fre) { t.Fatalf("err = %v, want a FileRemoveError", err) } got, err := dbq.New(pool).GetTrackByID(ctx, track.ID) if err != nil { t.Fatal(err) } if got.MissingSince.Valid || got.RemovedReason != nil { t.Fatalf("a track whose file stayed was marked: missing %v, reason %v", got.MissingSince.Valid, got.RemovedReason) } } // The reason describes one missing spell: a file back at its path clears it // with the missing mark. func TestRemoveFile_ReasonGoesWhenTheFileIsBack(t *testing.T) { pool := newPool(t) ctx := context.Background() path := filepath.Join(t.TempDir(), "back.mp3") if err := os.WriteFile(path, []byte("x"), 0o644); err != nil { t.Fatal(err) } track, _, _ := seedTrack(t, pool, path) if _, err := RemoveFile(ctx, pool, nil, RemoveFileParams{TrackID: track.ID, Reason: RemovedForReport}); err != nil { t.Fatalf("RemoveFile: %v", err) } q := dbq.New(pool) if _, err := q.ClearTracksMissing(ctx, []pgtype.UUID{track.ID}); err != nil { t.Fatalf("ClearTracksMissing: %v", err) } got, err := q.GetTrackByID(ctx, track.ID) if err != nil { t.Fatal(err) } if got.MissingSince.Valid || got.RemovedReason != nil || got.RemovedBy.Valid { t.Fatalf("back track = missing %v, reason %v, by %v; want all cleared", got.MissingSince.Valid, got.RemovedReason, got.RemovedBy.Valid) } }