# B-1 Pre-Fix Test Suite

**Date:** 2026-03-29
**Branch:** culurien
**Purpose:** Document database migration and schema bugs BEFORE fixes.
**Reference:** docs/B1_Database_Audit.md

---

## Test Files Created

| File | Package | Bugs Documented |
|------|---------|-----------------|
| `aggregator-server/internal/database/migration_runner_test.go` | `database_test` | F-B1-11, F-B1-13 |
| `aggregator-server/internal/database/migrations/migration024_test.go` | `migrations_test` | F-B1-1, F-B1-2 |
| `aggregator-server/internal/database/migrations/migration018_test.go` | `migrations_test` | F-B1-3, F-B1-4 |
| `aggregator-server/internal/database/migrations/idempotency_test.go` | `migrations_test` | F-B1-15 |
| `aggregator-server/internal/database/migrations/index_audit_test.go` | `migrations_test` | F-B1-5 |
| `aggregator-server/internal/api/handlers/stats_n1_test.go` | `handlers_test` | F-B1-6 |
| `aggregator-server/internal/database/refresh_token_cleanup_test.go` | `database_test` | F-B1-10 |

---

## How to Run

```bash
cd aggregator-server && go test ./internal/database/... -v
cd aggregator-server && go test ./internal/database/migrations/... -v
cd aggregator-server && go test ./internal/api/handlers/... -v -run TestGetDashboardStats
```

---

## Test Inventory

### migration_runner_test.go

| Test | Bug | Asserts | State | After Fix |
|------|-----|---------|-------|-----------|
| TestMigrationFailureReturnsError | F-B1-11 | Runner processes .up.sql files | PASS | PASS |
| TestServerStartsAfterMigrationFailure | F-B1-11 | main.go swallows migration errors | PASS | update |
| TestServerMustAbortOnMigrationFailure | F-B1-11 | main.go must abort on failure | **FAIL** | PASS |
| TestMigrationRunnerDetectsDuplicateNumbers | F-B1-13 | Duplicate 009/012 prefixes exist | PASS | update |
| TestMigrationRunnerShouldRejectDuplicateNumbers | F-B1-13 | No duplicate prefixes allowed | **FAIL** | PASS |

### migration024_test.go

| Test | Bug | Asserts | State | After Fix |
|------|-----|---------|-------|-----------|
| TestMigration024HasSelfInsert | F-B1-1 | 024 contains INSERT INTO schema_migrations | PASS | update |
| TestMigration024ShouldNotHaveSelfInsert | F-B1-1 | 024 must NOT self-insert | **FAIL** | PASS |
| TestMigration024ReferencesDeprecatedColumn | F-B1-2 | 024 uses `deprecated` column | PASS | update |
| TestMigration024ColumnExistsInSchema | F-B1-2 | `deprecated` must be defined before 024 | **FAIL** | PASS |

### migration018_test.go

| Test | Bug | Asserts | State | After Fix |
|------|-----|---------|-------|-----------|
| TestMigration018ScannerConfigHasWrongSuffix | F-B1-3 | .sql file exists (not .up.sql) | PASS | update |
| TestMigration018ScannerConfigHasCorrectSuffix | F-B1-3 | .up.sql file must exist | **FAIL** | PASS |
| TestMigration018ScannerConfigHasNoGrantToWrongRole | F-B1-4 | No GRANT to redflag_user | **FAIL** | PASS |

### idempotency_test.go

| Test | Bug | Asserts | State | After Fix |
|------|-----|---------|-------|-----------|
| TestMigrationsHaveIdempotencyViolations | F-B1-15 | Violations exist (>0) | PASS | update |
| TestAllMigrationsAreIdempotent | F-B1-15 | Zero violations | **FAIL** | PASS |

### index_audit_test.go

| Test | Bug | Asserts | State | After Fix |
|------|-----|---------|-------|-----------|
| TestStuckCommandsIndexIsMissing | F-B1-5 | No sent_at index on agent_commands | PASS | update |
| TestStuckCommandsIndexExists | F-B1-5 | sent_at index must exist | **FAIL** | PASS |

### stats_n1_test.go

| Test | Bug | Asserts | State | After Fix |
|------|-----|---------|-------|-----------|
| TestGetDashboardStatsHasNPlusOneLoop | F-B1-6 | Query inside agent loop | PASS | update |
| TestGetDashboardStatsUsesJoin | F-B1-6 | No per-agent query loop | **FAIL** | PASS |

### refresh_token_cleanup_test.go

| Test | Bug | Asserts | State | After Fix |
|------|-----|---------|-------|-----------|
| TestNoBackgroundRefreshTokenCleanup | F-B1-10 | No background cleanup exists | PASS | update |
| TestBackgroundRefreshTokenCleanupExists | F-B1-10 | Background cleanup must exist | **FAIL** | PASS |

---

## State-Change Summary

| Test | Current | After Fix |
|------|---------|-----------|
| TestMigrationFailureReturnsError | PASS | PASS |
| TestServerStartsAfterMigrationFailure | PASS | FAIL (update) |
| TestServerMustAbortOnMigrationFailure | **FAIL** | PASS |
| TestMigrationRunnerDetectsDuplicateNumbers | PASS | FAIL (update) |
| TestMigrationRunnerShouldRejectDuplicateNumbers | **FAIL** | PASS |
| TestNoBackgroundRefreshTokenCleanup | PASS | FAIL (update) |
| TestBackgroundRefreshTokenCleanupExists | **FAIL** | PASS |
| TestMigrationsHaveIdempotencyViolations | PASS | FAIL (update) |
| TestAllMigrationsAreIdempotent | **FAIL** | PASS |
| TestStuckCommandsIndexIsMissing | PASS | FAIL (update) |
| TestStuckCommandsIndexExists | **FAIL** | PASS |
| TestMigration018ScannerConfigHasWrongSuffix | PASS | FAIL (update) |
| TestMigration018ScannerConfigHasCorrectSuffix | **FAIL** | PASS |
| TestMigration018ScannerConfigHasNoGrantToWrongRole | **FAIL** | PASS |
| TestMigration024HasSelfInsert | PASS | FAIL (update) |
| TestMigration024ShouldNotHaveSelfInsert | **FAIL** | PASS |
| TestMigration024ReferencesDeprecatedColumn | PASS | FAIL (update) |
| TestMigration024ColumnExistsInSchema | **FAIL** | PASS |
| TestGetDashboardStatsHasNPlusOneLoop | PASS | FAIL (update) |
| TestGetDashboardStatsUsesJoin | **FAIL** | PASS |

**Bold FAIL** = tests asserting correct post-fix behavior (will flip to PASS).

---

## Notes

1. All tests are static analysis / source inspection — no live database required.
2. All A-series tests continue to pass (no regressions from B-1 test additions).
3. The idempotency test excludes migrations 025-026 (A-series, already idempotent).
