Files
minstrel/internal/dbtest/reset.go
T
bvandeusen d212621eaa test: fix 4 integration-suite root causes surfaced by CI (C+A+B+F)
Latent failures exposed now that integration tests run in CI (#339).
All test/test-harness only — no production code changes.

A (dbtest.ResetDB): also truncate cover_art_provider_settings +
cover_art_sources_meta. The monotonic source-version counter (seeded 1
by 0018) accumulated across internal/coverart tests → CurrentVersion=4
want 1, key-only-bump assertions, enricher source skips. SettingsService
re-seeds both at boot, so a truncated start is the correct fresh state.

B (playlists system_test.seedPlayEvent): inserted play_events with a
random session_id → play_events_session_id_fkey violation (7 tests).
Create the parent play_sessions row in the same statement (CTE).

C (similarity worker_integration_test.newTestWorker): built the client
with only BaseURL. Post-4fca0e6 similarity hits the Labs API via
LabsBaseURL, so an unset LabsBaseURL fell through to the real labs.api
and the stub never ran → 0 similarity rows. Set LabsBaseURL to the stub.

F (library scanner_test): NOT a flake — deterministic. Synthetic
ID3-only MP3s have no decodable duration; the scanner intentionally
won't skip duration_ms=0 rows (retries duration backfill), so every
re-scan reported Updated not Skipped. Seed duration_ms>0 before the
second scan so the mtime-based incremental-skip path under test is
actually exercised. Production scanner behavior unchanged.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-17 13:32:42 -04:00

88 lines
3.1 KiB
Go

// Package dbtest provides shared helpers for integration tests that need
// a clean Postgres state without disturbing the operator's admin user.
//
// Background: when integration tests run against the same Postgres
// instance an operator uses for local dev (a common dev-laptop setup),
// a TRUNCATE that includes the users table wipes the operator's admin
// login between every test run. To avoid that, tests should:
//
// 1. Call ResetDB instead of issuing TRUNCATE statements directly. It
// truncates every data table EXCEPT users, then deletes only those
// users whose username starts with TestUserPrefix.
// 2. Always create test user rows with a username that begins with
// TestUserPrefix; otherwise those rows survive across runs and
// leak into other tests.
//
// One test legitimately needs to wipe the entire users table:
// internal/auth/bootstrap_test.go, which exercises first-time admin
// bootstrap. That file does its own TRUNCATE and does not use this
// helper.
package dbtest
import (
"context"
"strings"
"testing"
"github.com/jackc/pgx/v5/pgxpool"
)
// TestUserPrefix is the required username prefix for any user row
// created by an integration test. ResetDB removes every user matching
// this prefix and leaves all others intact.
const TestUserPrefix = "test-"
// dataTables is the union of every non-users table touched by any
// integration test in the repo. Truncating the union is harmless for
// callers that only care about a subset; truncating tables that don't
// exist would error, but every name here corresponds to a migration
// that has shipped.
var dataTables = []string{
"artist_similarity",
"track_similarity",
"artist_similarity_unmatched", // M5c
"scrobble_queue",
"contextual_likes",
"general_likes_albums",
"general_likes_artists",
"general_likes",
"play_events",
"skip_events",
"play_sessions",
"sessions",
"lidarr_quarantine_actions",
"lidarr_quarantine",
"playlist_tracks",
"playlists",
"library_changes", // M7 #357 — must reset to keep cursor isolated per test
// Cover-art provider settings + the monotonic source-version
// counter (cover_art_sources_meta, seeded at 1 by migration 0018).
// Not resetting these let the version accumulate across tests in
// internal/coverart (CurrentVersion=4 want 1, key-only-bump
// assertions, enricher source skips). SettingsService re-seeds both
// at boot, so a truncated start is the correct fresh state.
"cover_art_provider_settings",
"cover_art_sources_meta",
"tracks",
"albums",
"artists",
}
// ResetDB clears all data tables and removes any user whose username
// begins with TestUserPrefix. Users without that prefix (notably the
// operator's admin row) are left alone. Calls t.Fatalf on error.
func ResetDB(t *testing.T, pool *pgxpool.Pool) {
t.Helper()
ctx := context.Background()
stmt := "TRUNCATE " + strings.Join(dataTables, ", ") + " RESTART IDENTITY CASCADE"
if _, err := pool.Exec(ctx, stmt); err != nil {
t.Fatalf("dbtest.ResetDB truncate: %v", err)
}
if _, err := pool.Exec(ctx,
"DELETE FROM users WHERE username LIKE $1",
TestUserPrefix+"%",
); err != nil {
t.Fatalf("dbtest.ResetDB delete test users: %v", err)
}
}