test(library): merge repeated cases into tables, drop tests of other code (#8296)
release / web (push) Successful in 1m30s
release / go (push) Successful in 1m57s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 3m26s
release / android (push) Successful in 4m40s
release / Build signed APK (push) Successful in 5m12s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Publish the release image to GHCR (dispatch only) (push) Skipped
release / Build + push container image (push) Successful in 28s
release / Verify release artifacts (tag releases only) (push) Skipped
release / web (push) Successful in 1m30s
release / go (push) Successful in 1m57s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 3m26s
release / android (push) Successful in 4m40s
release / Build signed APK (push) Successful in 5m12s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Publish the release image to GHCR (dispatch only) (push) Skipped
release / Build + push container image (push) Successful in 28s
release / Verify release artifacts (tag releases only) (push) Skipped
- Moved-file adoption: 14 tests become one table. uniqueMatch, the row converters and pgtype's zero UUID are dropped: the adoption table covers the first two, and the third tests pgx. - Reconcile: marking and its change rows, and restoring and its change rows, are one test each; the five "a volume looks absent" refusals are one table. Dropped: an error-propagation test. - Genres: the ID3 reader cases, the normalisers and extractGenres across ID3, FLAC, Ogg and MP4 are one table each. - MusicBrainz tag ids: eleven per-format tests become one table. - Scan trigger: start / skip / reap is one table. Dropped: a closed-pool test. - Stage publisher: six tests become three. - Duplicate resolver: the cross-release cases (rip, renamed, kept as fine, two titles) asserted the same outcome once the resolver stopped removing files. They become one table that also checks the group's note, which is where rip and clean still differ. The auto-resolve-off song-link check moves into the existing auto-resolve-off test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,20 +2,18 @@ package library
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"log/slog"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"github.com/Fabled-Sword/minstrel/internal/dbtest"
|
||||
)
|
||||
|
||||
// triggerTestPool returns a pool against MINSTREL_TEST_DATABASE_URL or skips.
|
||||
// triggerTestPool opens the test database with no scan runs recorded.
|
||||
func triggerTestPool(t *testing.T) *pgxpool.Pool {
|
||||
t.Helper()
|
||||
pool := dbtest.Open(t)
|
||||
@@ -31,121 +29,61 @@ func quietLogger() *slog.Logger {
|
||||
return slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
}
|
||||
|
||||
func TestTryStartScan_NoInFlight_Starts(t *testing.T) {
|
||||
pool := triggerTestPool(t)
|
||||
// A scan starts unless another is running. A run that has been "running" for
|
||||
// two hours is a crashed one: it is closed as reaped and a new scan starts.
|
||||
func TestTryStartScan_StartsUnlessAFreshScanIsRunning(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
inFlightAge string // "" seeds no run
|
||||
wantStarted bool
|
||||
}{
|
||||
{"nothing running", "", true},
|
||||
{"a fresh run", "0 seconds", false},
|
||||
{"a stale run", "2 hours", true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
pool := triggerTestPool(t)
|
||||
ctx := context.Background()
|
||||
var seeded pgtype.UUID
|
||||
if tc.inFlightAge != "" {
|
||||
if err := pool.QueryRow(ctx,
|
||||
`INSERT INTO scan_runs (started_at) VALUES (now() - $1::interval) RETURNING id`, tc.inFlightAge,
|
||||
).Scan(&seeded); err != nil {
|
||||
t.Fatalf("seed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
started, existing, err := TryStartScan(
|
||||
context.Background(), pool,
|
||||
nil, nil, // scanner/enricher: nil OK because the goroutine that uses them is detached and we don't await it.
|
||||
quietLogger(), RunScanConfig{},
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("err = %v, want nil", err)
|
||||
}
|
||||
if !started {
|
||||
t.Errorf("started = false, want true")
|
||||
}
|
||||
if existing != nil {
|
||||
t.Errorf("existing = %v, want nil", existing)
|
||||
}
|
||||
// Give the detached goroutine a moment.
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
}
|
||||
|
||||
func TestTryStartScan_FreshInFlight_Skips(t *testing.T) {
|
||||
pool := triggerTestPool(t)
|
||||
|
||||
// Seed a fresh in-flight row.
|
||||
var rowID pgtype.UUID
|
||||
if err := pool.QueryRow(context.Background(),
|
||||
`INSERT INTO scan_runs (started_at) VALUES (now()) RETURNING id`,
|
||||
).Scan(&rowID); err != nil {
|
||||
t.Fatalf("seed: %v", err)
|
||||
}
|
||||
|
||||
started, existing, err := TryStartScan(
|
||||
context.Background(), pool,
|
||||
nil, nil,
|
||||
quietLogger(), RunScanConfig{},
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("err = %v, want nil", err)
|
||||
}
|
||||
if started {
|
||||
t.Errorf("started = true, want false")
|
||||
}
|
||||
if existing == nil {
|
||||
t.Fatal("existing = nil, want non-nil")
|
||||
}
|
||||
if existing.ID != rowID {
|
||||
t.Errorf("existing.ID = %v, want %v", existing.ID, rowID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTryStartScan_StaleInFlight_Reaps(t *testing.T) {
|
||||
pool := triggerTestPool(t)
|
||||
|
||||
// Seed a stale in-flight row (started 2 hours ago).
|
||||
var staleID pgtype.UUID
|
||||
if err := pool.QueryRow(context.Background(),
|
||||
`INSERT INTO scan_runs (started_at) VALUES (now() - interval '2 hours') RETURNING id`,
|
||||
).Scan(&staleID); err != nil {
|
||||
t.Fatalf("seed: %v", err)
|
||||
}
|
||||
|
||||
started, existing, err := TryStartScan(
|
||||
context.Background(), pool,
|
||||
nil, nil,
|
||||
quietLogger(), RunScanConfig{},
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("err = %v, want nil", err)
|
||||
}
|
||||
if !started {
|
||||
t.Errorf("started = false, want true (stale row should reap and proceed)")
|
||||
}
|
||||
if existing != nil {
|
||||
t.Errorf("existing = %v, want nil", existing)
|
||||
}
|
||||
|
||||
// Verify the stale row was reaped: finished_at non-NULL, error_message
|
||||
// contains "reaped".
|
||||
var finishedAt pgtype.Timestamptz
|
||||
var errMsg *string
|
||||
if err := pool.QueryRow(context.Background(),
|
||||
`SELECT finished_at, error_message FROM scan_runs WHERE id = $1`, staleID,
|
||||
).Scan(&finishedAt, &errMsg); err != nil {
|
||||
t.Fatalf("verify reap: %v", err)
|
||||
}
|
||||
if !finishedAt.Valid {
|
||||
t.Errorf("finished_at = NULL, want non-NULL after reap")
|
||||
}
|
||||
if errMsg == nil || *errMsg != "reaped (stale)" {
|
||||
t.Errorf("error_message = %v, want 'reaped (stale)'", errMsg)
|
||||
}
|
||||
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
}
|
||||
|
||||
func TestTryStartScan_DBError_Propagates(t *testing.T) {
|
||||
pool := dbtest.Open(t)
|
||||
pool.Close() // close immediately to force errors on subsequent queries
|
||||
|
||||
started, existing, err := TryStartScan(
|
||||
context.Background(), pool,
|
||||
nil, nil,
|
||||
quietLogger(), RunScanConfig{},
|
||||
)
|
||||
if err == nil {
|
||||
t.Errorf("err = nil, want non-nil for closed pool")
|
||||
}
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
t.Errorf("err is ErrNoRows; want a different DB error type")
|
||||
}
|
||||
if started {
|
||||
t.Errorf("started = true, want false on DB error")
|
||||
}
|
||||
if existing != nil {
|
||||
t.Errorf("existing = %v, want nil on DB error", existing)
|
||||
// scanner/enricher nil: the goroutine that uses them is detached
|
||||
// and not awaited.
|
||||
started, existing, err := TryStartScan(ctx, pool, nil, nil, quietLogger(), RunScanConfig{})
|
||||
if err != nil {
|
||||
t.Fatalf("err = %v, want nil", err)
|
||||
}
|
||||
if started != tc.wantStarted {
|
||||
t.Errorf("started = %v, want %v", started, tc.wantStarted)
|
||||
}
|
||||
if tc.wantStarted && existing != nil {
|
||||
t.Errorf("existing = %v, want nil", existing)
|
||||
}
|
||||
if !tc.wantStarted && (existing == nil || existing.ID != seeded) {
|
||||
t.Errorf("existing = %v, want the running row %v", existing, seeded)
|
||||
}
|
||||
if tc.name == "a stale run" {
|
||||
var finishedAt pgtype.Timestamptz
|
||||
var errMsg *string
|
||||
if err := pool.QueryRow(ctx,
|
||||
`SELECT finished_at, error_message FROM scan_runs WHERE id = $1`, seeded,
|
||||
).Scan(&finishedAt, &errMsg); err != nil {
|
||||
t.Fatalf("verify reap: %v", err)
|
||||
}
|
||||
if !finishedAt.Valid || errMsg == nil || *errMsg != "reaped (stale)" {
|
||||
t.Errorf("stale row finished=%v message=%v, want closed as 'reaped (stale)'", finishedAt.Valid, errMsg)
|
||||
}
|
||||
}
|
||||
if started {
|
||||
// Give the detached goroutine a moment.
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user