refactor(backend): migrate test infrastructure to isolated databases

Add CreateTestDatabase function for parallel isolated test databases per package.

- Add CreateTestDatabase() for isolated test DBs (parallel-safe)
- Move all TestMain functions to per-package testmain_test.go files
- Remove old TestMain from handlers_test.go, jwt_test.go, main_test.go
- Add JWT init guard in main.go to skip when -test.* flags detected
- Update testdb.go with admin DSN and proper cleanup
- Rename test database to crussell_test_db for consistency
- Replace testdb.NewPool + testdb.Migrate pattern with CreateTestDatabase

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
2026-06-20 16:56:57 +01:00
co-authored by Sisyphus
parent 482958584a
commit 9c68918c20
20 changed files with 403 additions and 244 deletions
-22
View File
@@ -14,35 +14,13 @@ package auth
import (
"context"
"fmt"
"os"
"strings"
"testing"
"time"
"crussell/db"
"crussell/testutils/testdb"
)
func TestMain(m *testing.M) {
InitJWT("test-secret-key-for-jwt-test")
pool, err := testdb.NewPool("")
if err != nil {
fmt.Fprintf(os.Stderr, "WARN: No test DB available — JTI revocation tests will fail: %v\n", err)
} else {
// Run migration to ensure the revoked_jtis and refresh_tokens tables exist
testdb.Migrate(&testing.T{}, pool)
db.DB = pool
}
code := m.Run()
if pool != nil {
pool.Close()
}
os.Exit(code)
}
// requiresDB skips the test if the database is not available (e.g. running
// tests standalone without test DB setup). DB-backed JTI revocation tests
// need a connection to the revoked_jtis table.
+23
View File
@@ -0,0 +1,23 @@
//go:build test
// +build test
package auth
import (
"os"
"testing"
"crussell/db"
"crussell/testutils/testdb"
)
func TestMain(m *testing.M) {
InitJWT("test-secret-key-for-jwt-test")
pool := testdb.CreateTestDatabase("crussell_test_auth")
db.DB = pool
code := m.Run()
testdb.DestroyTestDatabase(pool, "crussell_test_auth")
os.Exit(code)
}