package database import ( "log/slog" "os" "gorm.io/gorm" ) // NewTestDatabase creates a Database wrapper around a pre-opened *gorm.DB. // Intended for use in tests that need a *database.Database without the // full fx lifecycle. The caller is responsible for closing the underlying // sql.DB connection. func NewTestDatabase(db *gorm.DB) *Database { return &Database{ db: db, log: slog.New(slog.NewTextHandler( os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug}, )), } } // NewTestWebhookDBManager creates a WebhookDBManager backed by the given // data directory. Intended for use in tests without the fx lifecycle. func NewTestWebhookDBManager(dataDir string) *WebhookDBManager { return NewTestWebhookDBManagerWithLogger( dataDir, slog.New(slog.NewTextHandler( os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug}, )), ) } // NewTestWebhookDBManagerWithLogger is NewTestWebhookDBManager with the // logger supplied by the caller. The per-webhook databases this manager // opens hand that logger to gormlog, so a test that needs to see the SQL // the service emits can capture it. func NewTestWebhookDBManagerWithLogger( dataDir string, log *slog.Logger, ) *WebhookDBManager { return &WebhookDBManager{ dataDir: dataDir, log: log, } } // testArgon2Memory is the Argon2id memory cost, in KiB, that test // binaries hash with: 1 MB instead of the shipped 64 MB. const testArgon2Memory = 1024 // LowerPasswordHashCostForTest makes HashPassword use a 1 MB Argon2id // memory cost instead of the shipped 64 MB, for the rest of the // process. Call it from TestMain, before any test runs. // // Every test that starts a database hashes the bootstrap admin // password, and a package runs dozens of those tests in parallel. // Under the race detector each 64 MB hash holds about 150 MB resident. // VerifyPassword reads the cost from the hash it checks, so // verification follows. Production code never calls this. func LowerPasswordHashCostForTest() { hashConfig.Memory = testArgon2Memory }