From 8cc9f75a1a35a6ccc294861dc01526a4ea9bd6da Mon Sep 17 00:00:00 2001 From: John O'Keefe Date: Sat, 16 May 2026 19:31:46 -0400 Subject: [PATCH] fix(tests): protect dev admin from test cleanup, use isolated test names Tests were deleting the development admin user, causing ON DELETE SET NULL to cascade and set created_by_admin_id to NULL on all libraries. - test_helpers: skip deletion of testuser@tests.bookhoard.internal - sync_integration_test: use test-sync% prefix for isolated test data --- cmd/server/tests/sync_integration_test.go | 2 +- cmd/server/tests/test_helpers_test.go | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/cmd/server/tests/sync_integration_test.go b/cmd/server/tests/sync_integration_test.go index 1350284..9dc5819 100644 --- a/cmd/server/tests/sync_integration_test.go +++ b/cmd/server/tests/sync_integration_test.go @@ -37,7 +37,7 @@ func setupSyncTestDB(t *testing.T) *database.Queries { _, _ = dbPool.Exec(ctx, "DELETE FROM media_items WHERE title LIKE 'Test %'") _, _ = dbPool.Exec(ctx, "DELETE FROM libraries WHERE name LIKE 'Test %'") _, _ = dbPool.Exec(ctx, "DELETE FROM devices WHERE device_name LIKE 'Test %'") - _, _ = dbPool.Exec(ctx, "DELETE FROM users WHERE email LIKE 'test%'") + _, _ = dbPool.Exec(ctx, "DELETE FROM users WHERE email LIKE 'test-sync%'") dbPool.Close() }) diff --git a/cmd/server/tests/test_helpers_test.go b/cmd/server/tests/test_helpers_test.go index 43e00a9..a79906d 100644 --- a/cmd/server/tests/test_helpers_test.go +++ b/cmd/server/tests/test_helpers_test.go @@ -558,11 +558,15 @@ func setupTestServer(t *testing.T) *TestServerSetup { ctx := context.Background() - // Delete ALL test users (any user with test email domains) to ensure clean state - // This handles users created during tests that may have been promoted to admin, etc. + // Delete transient test users but preserve the dev admin user + // testuser@tests.bookhoard.internal is the shared dev admin — deleting it + // triggers ON DELETE SET NULL on libraries.created_by_admin_id allUsers, err := queries.ListUsers(ctx) if err == nil { for _, user := range allUsers { + if user.Email == "testuser@tests.bookhoard.internal" { + continue + } if strings.HasSuffix(user.Email, "@example.com") || strings.HasSuffix(user.Email, "@tests.bookhoard.internal") { queries.DeleteUser(ctx, user.ID) }