937a1930ad
Integration tests previously TRUNCATEd the users table at setup, which wiped the operator's admin login every time the suite ran against a Postgres instance shared with their dev environment. This commit: - Adds internal/dbtest/reset.go exposing ResetDB(t, pool) and TestUserPrefix. ResetDB truncates every data table EXCEPT users, then deletes only users whose username starts with TestUserPrefix. - Migrates 9 test files (subsonic/scrobble, subsonic/star, recommendation/candidates, scrobble/queue, similarity/worker, playevents/writer, playsessions/service, api/auth, api/me) to call ResetDB instead of issuing TRUNCATE-with-users. - Renames hardcoded test usernames to use TestUserPrefix so ResetDB cleans them between runs. seedUser in api/auth_test now prefixes internally; existing call sites pass bare names. - Leaves auth/bootstrap_test.go alone — it legitimately tests first-time admin bootstrap and must wipe users. Verified locally: full integration suite with -p 1 runs cleanly under the existing MINSTREL_TEST_DATABASE_URL setup, and the admin row in the shared dev DB survives the run. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
162 lines
5.0 KiB
Go
162 lines
5.0 KiB
Go
package scrobble
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"log/slog"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db"
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
|
)
|
|
|
|
func testPool(t *testing.T) (*pgxpool.Pool, *dbq.Queries) {
|
|
t.Helper()
|
|
if testing.Short() {
|
|
t.Skip("skipping in -short mode")
|
|
}
|
|
dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL")
|
|
if dsn == "" {
|
|
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
|
|
}
|
|
if err := db.Migrate(dsn, slog.New(slog.NewTextHandler(io.Discard, nil))); err != nil {
|
|
t.Fatalf("migrate: %v", err)
|
|
}
|
|
pool, err := pgxpool.New(context.Background(), dsn)
|
|
if err != nil {
|
|
t.Fatalf("pool: %v", err)
|
|
}
|
|
t.Cleanup(pool.Close)
|
|
dbtest.ResetDB(t, pool)
|
|
return pool, dbq.New(pool)
|
|
}
|
|
|
|
type setup struct {
|
|
pool *pgxpool.Pool
|
|
q *dbq.Queries
|
|
user pgtype.UUID
|
|
playEvent pgtype.UUID
|
|
}
|
|
|
|
type seedOpts struct {
|
|
lbToken string
|
|
lbEnabled bool
|
|
durationMs int32
|
|
ratio float64
|
|
}
|
|
|
|
// seed creates: user (with optional LB token + enabled), one artist/album/track,
|
|
// one closed play_event with the given duration_played_ms and completion_ratio.
|
|
func seed(t *testing.T, opts seedOpts) setup {
|
|
t.Helper()
|
|
pool, q := testPool(t)
|
|
ctx := context.Background()
|
|
u, err := q.CreateUser(ctx, dbq.CreateUserParams{
|
|
Username: dbtest.TestUserPrefix + "tester", PasswordHash: "x", ApiToken: "x", IsAdmin: false,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("user: %v", err)
|
|
}
|
|
if opts.lbToken != "" {
|
|
if err := q.SetListenBrainzToken(ctx, dbq.SetListenBrainzTokenParams{
|
|
ID: u.ID, ListenbrainzToken: &opts.lbToken,
|
|
}); err != nil {
|
|
t.Fatalf("token: %v", err)
|
|
}
|
|
}
|
|
if opts.lbEnabled {
|
|
if err := q.SetListenBrainzEnabled(ctx, dbq.SetListenBrainzEnabledParams{
|
|
ID: u.ID, ListenbrainzEnabled: true,
|
|
}); err != nil {
|
|
t.Fatalf("enabled: %v", err)
|
|
}
|
|
}
|
|
a, _ := q.UpsertArtist(ctx, dbq.UpsertArtistParams{Name: "X", SortName: "X"})
|
|
al, _ := q.UpsertAlbum(ctx, dbq.UpsertAlbumParams{Title: "X", SortTitle: "X", ArtistID: a.ID})
|
|
tr, _ := q.UpsertTrack(ctx, dbq.UpsertTrackParams{
|
|
Title: "Y", AlbumID: al.ID, ArtistID: a.ID, FilePath: "/tmp/y.flac", DurationMs: 300_000,
|
|
})
|
|
var sessionID pgtype.UUID
|
|
if err := pool.QueryRow(ctx,
|
|
`INSERT INTO play_sessions (user_id, started_at, last_event_at, client_id)
|
|
VALUES ($1, now() - interval '5 minutes', now(), 'test') RETURNING id`,
|
|
u.ID).Scan(&sessionID); err != nil {
|
|
t.Fatalf("session: %v", err)
|
|
}
|
|
var peID pgtype.UUID
|
|
if err := pool.QueryRow(ctx,
|
|
`INSERT INTO play_events (user_id, track_id, session_id, started_at, ended_at, duration_played_ms, completion_ratio, was_skipped)
|
|
VALUES ($1, $2, $3, now() - interval '1 minute', now(), $4, $5, false) RETURNING id`,
|
|
u.ID, tr.ID, sessionID, opts.durationMs, opts.ratio).Scan(&peID); err != nil {
|
|
t.Fatalf("play_event: %v", err)
|
|
}
|
|
return setup{pool: pool, q: q, user: u.ID, playEvent: peID}
|
|
}
|
|
|
|
func countQueueRows(t *testing.T, s setup) int {
|
|
t.Helper()
|
|
var n int
|
|
if err := s.pool.QueryRow(context.Background(),
|
|
`SELECT count(*) FROM scrobble_queue WHERE play_event_id = $1`, s.playEvent).Scan(&n); err != nil {
|
|
t.Fatalf("count: %v", err)
|
|
}
|
|
return n
|
|
}
|
|
|
|
func TestMaybeEnqueue_QualifyingPlayWithEnabledUser_InsertsRow(t *testing.T) {
|
|
s := seed(t, seedOpts{lbToken: "tk", lbEnabled: true, durationMs: 250_000, ratio: 0.83})
|
|
if err := MaybeEnqueue(context.Background(), s.q, s.playEvent); err != nil {
|
|
t.Fatalf("MaybeEnqueue: %v", err)
|
|
}
|
|
if got := countQueueRows(t, s); got != 1 {
|
|
t.Errorf("rows = %d, want 1", got)
|
|
}
|
|
}
|
|
|
|
func TestMaybeEnqueue_SubThreshold_NoRow(t *testing.T) {
|
|
s := seed(t, seedOpts{lbToken: "tk", lbEnabled: true, durationMs: 100_000, ratio: 0.33})
|
|
if err := MaybeEnqueue(context.Background(), s.q, s.playEvent); err != nil {
|
|
t.Fatalf("MaybeEnqueue: %v", err)
|
|
}
|
|
if got := countQueueRows(t, s); got != 0 {
|
|
t.Errorf("rows = %d, want 0 (sub-threshold)", got)
|
|
}
|
|
}
|
|
|
|
func TestMaybeEnqueue_DisabledUser_NoRow(t *testing.T) {
|
|
s := seed(t, seedOpts{lbToken: "tk", lbEnabled: false, durationMs: 250_000, ratio: 0.83})
|
|
if err := MaybeEnqueue(context.Background(), s.q, s.playEvent); err != nil {
|
|
t.Fatalf("MaybeEnqueue: %v", err)
|
|
}
|
|
if got := countQueueRows(t, s); got != 0 {
|
|
t.Errorf("rows = %d, want 0 (disabled)", got)
|
|
}
|
|
}
|
|
|
|
func TestMaybeEnqueue_NoToken_NoRow(t *testing.T) {
|
|
s := seed(t, seedOpts{lbToken: "", lbEnabled: true, durationMs: 250_000, ratio: 0.83})
|
|
if err := MaybeEnqueue(context.Background(), s.q, s.playEvent); err != nil {
|
|
t.Fatalf("MaybeEnqueue: %v", err)
|
|
}
|
|
if got := countQueueRows(t, s); got != 0 {
|
|
t.Errorf("rows = %d, want 0 (no token)", got)
|
|
}
|
|
}
|
|
|
|
func TestMaybeEnqueue_Idempotent_TwoCallsOneRow(t *testing.T) {
|
|
s := seed(t, seedOpts{lbToken: "tk", lbEnabled: true, durationMs: 250_000, ratio: 0.83})
|
|
for i := 0; i < 2; i++ {
|
|
if err := MaybeEnqueue(context.Background(), s.q, s.playEvent); err != nil {
|
|
t.Fatalf("MaybeEnqueue #%d: %v", i, err)
|
|
}
|
|
}
|
|
if got := countQueueRows(t, s); got != 1 {
|
|
t.Errorf("rows = %d, want 1 (idempotent)", got)
|
|
}
|
|
}
|