docs/dev-notes/MAIN_TEST_REFACTOR_NOTES.md
Attempted to refactor main_test.go (18 tests, 14 newTestStore() calls) to use shared DB pattern like P1 files. Discovered fundamental incompatibility with shared DB approach due to global state manipulation and integration test characteristics.
TestAutoFlushSuite and TestAutoImportSuite with shared DBflushToJSONL() which accesses the databasenewTestStore()) tries to close the databasedatabase/sql.(*DB).Close() waiting while flushToJSONL() is accessing DBThese tests heavily manipulate package-level globals:
autoFlushEnabled, isDirty, flushTimerstore, storeActive, storeMutexdbPath (used to compute JSONL path dynamically)flushFailureCount, lastFlushError| Aspect | P1 Tests (create, dep, etc.) | main_test.go |
|---|---|---|
| DB Usage | Pure DB operations | Global state + DB + filesystem |
| Isolation | Data-level only | Requires process-level isolation |
| Cleanup | Simple | Complex (timers, goroutines, mutexes) |
| Pattern | CRUD operations | Workflow simulation |
jsonlPath is computed dynamically from dbPath via findJSONLPath()
dbPath affect where JSONL files are written/readTests need to control exact JSONL paths for:
Concurrent access issues:
isDirty, autoFlushEnabled)flushTimer)markDirtyAndScheduleFlush())<<<<<<< markers in file)os.Chtimes()Rationale: These are integration tests, not unit tests. The overhead of 14 DB setups is acceptable for:
Expected speedup: Minimal (2-3x at most) vs. complexity cost
Changes:
Example:
func TestAutoFlushGroup(t *testing.T) {
tmpDir := t.TempDir()
testDB := filepath.Join(tmpDir, "test.db")
testStore := newTestStore(t, testDB)
// Helper to reset state
resetState := func() {
autoFlushEnabled = true
isDirty = false
if flushTimer != nil {
flushTimer.Stop()
flushTimer = nil
}
}
t.Run("DirtyMarking", func(t *testing.T) {
resetState()
// test...
})
t.Run("Disabled", func(t *testing.T) {
resetState()
// test...
})
}
Changes:
flushToJSONL and autoImportIfNewerTrade-offs: More refactoring, loses integration test value
cmd/bd/main_test.go - Reverted to originalcmd/bd/duplicates_test.go - Fixed unused import (kept fix)Not all tests benefit from shared DB pattern
P1 test pattern assumes:
Test classification matters:
integrity_test.go and export_import_test.go insteadRather than forcing shared DB pattern on integration tests, we deleted redundant tests that were duplicating coverage from flush_manager_test.go.
After FlushManager refactoring (bd-52), main_test.go was testing the DEPRECATED legacy path while flush_manager_test.go tested the NEW FlushManager. Solution: delete the redundant legacy tests.
Deleted 7 redundant tests (407 lines):
Kept 2 integration tests:
Updated clearAutoFlushState() to no-op when FlushManager exists
markDirtyAndScheduleFlush() entirelylabel_test.go, P1 refactored filesdocs/MAIN_TEST_CLEANUP_PLAN.md