feat(library): removing a bad file keeps its track, waiting for a good copy
release / go (push) Successful in 2m6s
release / web (push) Successful in 1m15s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 4m46s
release / android (push) Successful in 4m47s
release / Build signed APK (push) Successful in 5m38s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 31s
release / Verify release artifacts (tag releases only) (push) Skipped
release / go (push) Successful in 2m6s
release / web (push) Successful in 1m15s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 4m46s
release / android (push) Successful in 4m47s
release / Build signed APK (push) Successful in 5m38s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 31s
release / Verify release artifacts (tag releases only) (push) Skipped
Reports, Duplicates and Suspect sources now end the same way: the file
goes and the track stays, marked missing with its likes, plays and
playlist places, until a good copy lands in the library and move
adoption or missing-pair folding brings it back.
- library.RemoveFile removes one track's file and marks the track
missing with the queue it was removed from and who removed it
(tracks.removed_reason / removed_by, migration 0104). The duplicate
resolver's wrong-slot removal shares the same marking.
- POST /api/admin/quarantine/{id}/remove-file removes a reported file,
clears the listeners' reports, marks open playback errors deleted and
records removed_file in the Reports history. A wrong-file or
wrong-tags report drops the file's recording MBID.
- POST /api/admin/library/suspect-sources/{id}/remove removes a video
rip's file and its MBID, and writes a suspect_file_remove audit row.
- The removal reason clears with the missing mark when a file returns.
Grimoire #7743 (milestone 635).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user