Skip to content

Fix test failures after PersistenceCoordinator integration #245

Closed

Description

@malibio

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:

  1. Service Tests: DatabaseInitializationError - need test mode enabled
  2. 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 PersistenceCoordinator in global-setup.ts
  • Initialize coordinator in global-setup.ts setup() function
  • Import PersistenceCoordinator and vitest hooks in setup.ts
  • Add beforeEach/afterEach cleanup in setup.ts
  • All tests pass: bun run test shows 0 failures
  • No DatabaseInitializationError in test output
  • No "database is locked" errors
  • No FOREIGN KEY constraint violations
  • Run bun run quality:fix before 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

Activity

  1. self-assigned this
    on Oct 12, 2025
  2. added a commit that references this issue on Oct 12, 2025
    7b44e90
  3. added a commit that references this issue on Oct 12, 2025
    78b36c5
  4. added 2 commits that reference this issue on Feb 4, 2026
    a459544
    00cd764
  5. added a commit that references this issue on Feb 26, 2026
    856722c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions