//go:build test package adminnotify import ( "context" "os" "testing" "crussell/db" "crussell/testutils/testdb" ) func TestMain(m *testing.M) { pool := testdb.CreateTestDatabase("crussell_test_adminnotify") db.Conn = db.NewPoolProxy(pool) code := m.Run() testdb.DestroyTestDatabase(pool, "crussell_test_adminnotify") os.Exit(code) } // TestMaxUnacknowledgedCriticalLogs_Value pins the exported flood-cap constant // that every capped insert site folds into its SQL (changing it re-scopes every // site at once, so the value is part of the shared contract). func TestMaxUnacknowledgedCriticalLogs_Value(t *testing.T) { if MaxUnacknowledgedCriticalLogs != 100 { t.Errorf("expected MaxUnacknowledgedCriticalLogs to be 100, got %d", MaxUnacknowledgedCriticalLogs) } } // TestCriticalLogsCapExceeded verifies the exported pre-check that logs a // suppression: below the cap it reports false, at the cap true, and // acknowledging rows re-arms it. func TestCriticalLogsCapExceeded(t *testing.T) { ctx := context.Background() t.Cleanup(func() { _, _ = db.Conn.Exec(ctx, "DELETE FROM admin_notifications WHERE reason = 'critical_payment_log'") }) if CriticalLogsCapExceeded(ctx, db.Conn, "critical_payment_log") { t.Fatal("expected the empty queue to be below the cap") } for i := 0; i < MaxUnacknowledgedCriticalLogs-1; i++ { if _, err := db.Conn.Exec(ctx, ` INSERT INTO admin_notifications (reason, created_at) VALUES ('critical_payment_log', NOW()) `); err != nil { t.Fatalf("failed to insert row %d: %v", i, err) } } if CriticalLogsCapExceeded(ctx, db.Conn, "critical_payment_log") { t.Fatalf("expected %d unacknowledged rows to stay below the cap", MaxUnacknowledgedCriticalLogs-1) } if _, err := db.Conn.Exec(ctx, ` INSERT INTO admin_notifications (reason, created_at) VALUES ('critical_payment_log', NOW()) `); err != nil { t.Fatalf("failed to insert the cap row: %v", err) } if !CriticalLogsCapExceeded(ctx, db.Conn, "critical_payment_log") { t.Fatalf("expected %d unacknowledged rows to be at the cap", MaxUnacknowledgedCriticalLogs) } if _, err := db.Conn.Exec(ctx, "UPDATE admin_notifications SET acknowledged_at = NOW() WHERE reason = 'critical_payment_log'"); err != nil { t.Fatalf("failed to acknowledge the queue: %v", err) } if CriticalLogsCapExceeded(ctx, db.Conn, "critical_payment_log") { t.Fatal("expected acknowledging the queue to re-arm inserts") } }