Repository navigation
Description
Fix Test Infrastructure: Add PersistenceCoordinator Setup
Problem (UPDATED)
After implementing PersistenceCoordinator (PR #243), ALL tests need proper coordinator initialization in the test infrastructure. Currently failing with two types of issues:
- Service Tests:
DatabaseInitializationError- need test mode enabled - Integration Tests: Race conditions, FOREIGN KEY violations, "database is locked" - need proper coordinator setup
Root Cause
Tests are using SharedNodeStore which now delegates to PersistenceCoordinator, but the test infrastructure doesn't initialize the coordinator properly.
Solution: Centralized Test Infrastructure Fix
Instead of updating each test file individually, fix at the infrastructure level.
Implementation Steps
Step 1: Update packages/desktop-app/src/tests/global-setup.ts
Location: After line 102 (after plugin registry initialization)
Add this code:
// Import at top of file (after existing imports)
import { PersistenceCoordinator } from '$lib/services/persistence-coordinator.svelte';
// In the setup() function, after registerCorePlugins():
// Initialize PersistenceCoordinator for all tests
PersistenceCoordinator.resetInstance();
const coordinator = PersistenceCoordinator.getInstance();
coordinator.enableTestMode(); // Service tests use test mode by default
console.log('✅ PersistenceCoordinator initialized in test mode');Expected result: File should have PersistenceCoordinator import at top and initialization in setup() function.
Step 2: Update packages/desktop-app/src/tests/setup.ts
Location: After existing imports, add new global hooks
Add this code:
// Import at top of file (after existing imports)
import { beforeEach, afterEach } from 'vitest';
import { PersistenceCoordinator } from '$lib/services/persistence-coordinator.svelte';
// Add these hooks after all the mock setup code (around line 145)
// PersistenceCoordinator cleanup for each test
beforeEach(() => {
const coordinator = PersistenceCoordinator.getInstance();
coordinator.resetTestState();
});
afterEach(async () => {
const coordinator = PersistenceCoordinator.getInstance();
// Wait a bit for pending operations to settle
await new Promise((resolve) => setTimeout(resolve, 10));
coordinator.reset();
});Expected result: File should have coordinator cleanup hooks that run before/after each test.
Step 3: Verify the Fix
Run these commands to verify all tests pass:
# Run all tests
bun run test
# Should show: Test Files XX passed (XX) with 0 failures
# Previously failing tests should now pass
# Run specific service test (was failing with DatabaseInitializationError)
bun run test src/tests/services/hierarchy-service.test.ts
# Run specific integration test (was failing with race conditions)
bun run test src/tests/integration/backspace-operations.test.ts
# Check for errors in output
# Should NOT see: "DatabaseInitializationError"
# Should NOT see: "database is locked"
# Should NOT see: "FOREIGN KEY constraint"Integration Tests Note
Some integration tests may need to disable test mode if they explicitly want real database operations. They can override in their test file:
beforeAll(() => {
PersistenceCoordinator.getInstance().disableTestMode();
});Impact
ALL failing tests will be fixed by this infrastructure change:
- Service tests: 7 files (DatabaseInitializationError fixed)
- Integration tests: 9+ files (race conditions, FOREIGN KEY violations fixed)
- Performance tests: 1 file
Acceptance Criteria
- Import
PersistenceCoordinatoringlobal-setup.ts - Initialize coordinator in
global-setup.tssetup() function - Import
PersistenceCoordinatorand vitest hooks insetup.ts - Add beforeEach/afterEach cleanup in
setup.ts - All tests pass:
bun run testshows 0 failures - No
DatabaseInitializationErrorin test output - No "database is locked" errors
- No FOREIGN KEY constraint violations
- Run
bun run quality:fixbefore committing
Estimated Effort
- Time: 30 minutes - 1 hour
- Complexity: Low (2 file changes, add ~20 lines total)
- Files to modify: 2 (test infrastructure only)
- Risk: Low (infrastructure change, easy to revert)
Related Issues
- Issues Fix Indent/Outdent Operations (12 test failures) #228-231, Fix UI & Service Integration Issues (14 test failures) #233: These have REAL BUGS that will become visible after this fix
- Issue Fix Enter Key Node Creation Bugs (Post-Infrastructure) #247: Enter key bugs (new issue, replaces Fix Enter Key & Node Creation Operations (17 test failures) #232)
- Issue Fix Remaining Sibling Chain Race Conditions #248: Remaining race conditions to fix after infrastructure is stable
Activity
Fix test infrastructure: Add PersistenceCoordinator setup
Fix test failures after PersistenceCoordinator integration (#249)
Fix test failures after PersistenceCoordinator integration (#249)
Fix test failures after PersistenceCoordinator integration (#249)
Fix test failures after PersistenceCoordinator integration (#249)