Skip to content

Fix Test Environment to Surface Database Errors & Race Conditions #235

Closed

Description

@malibio

Critical: Test Environment Hiding Real Bugs

Related to: Issue #208 (Comprehensive Test Coverage)
Blocks: Issues #228-#233 (all bug fix issues)
Priority: CRITICAL - Must fix before working on other bug issues

Problem

The comprehensive tests are giving false confidence because:

  1. SharedNodeStore silently swallows database errors in tests (lines 352-360 in shared-node-store.ts)
  2. Tests don't wait for async database writes before checking persistence
  3. Result: Tests pass even when database persistence is broken!

This explains why:

  • Tests show 93.3% pass rate
  • Manual testing reveals basic bugs (create node fails)
  • HTTP 500 errors in dev server logs are hidden from tests

Root Cause

1. Silent Error Swallowing

// shared-node-store.ts:352-360
} catch (dbError) {
  // Only log database errors in non-test environments
  // Tests run without Tauri and database writes are expected to fail
  if (typeof process === 'undefined' || process.env.NODE_ENV \!== 'test') {
    console.error(`[SharedNodeStore] Database write failed for node ${node.id}:`, dbError);
  }
}

This comment is WRONG: Tests DO have database access via HttpAdapter, but errors are hidden!

2. Race Conditions

// Test creates node (queues async database write)
const nodeId = service.createNode('node-1', '', 'text');

// IMMEDIATELY checks database (write might not be done\!)
const dbNode = await adapter.getNode(nodeId);
expect(dbNode).toBeDefined(); // ❌ May pass/fail randomly

Impact

CRITICAL: All test results in #208 are unreliable. Issues #228-#233 may have:

  • False failures (race conditions)
  • OR worse: Missing real failures (errors swallowed)

Solution

Part 1: Surface Database Errors in Tests

// In shared-node-store.ts, change error handler:
} catch (dbError) {
  // Log in all environments
  console.error(`[SharedNodeStore] Database write failed for node ${node.id}:`, dbError);
  
  // In test environment, also track failures so tests can assert
  if (typeof process \!== 'undefined' && process.env.NODE_ENV === 'test') {
    // Store error for test verification
    this.lastDatabaseError = dbError;
  }
}

Part 2: Add Wait Helper for Tests

// In test-database.ts or new test-helpers.ts
export async function waitForDatabaseWrites(maxWaitMs = 5000): Promise<void> {
  // Wait for database write queue to flush
  // Implementation options:
  // 1. Poll SharedNodeStore.hasPendingWrites()
  // 2. Add callback to queueDatabaseWrite completion
  // 3. Simple timeout-based wait with verification
}

Part 3: Update Test Pattern

// Before (race condition):
const nodeId = service.createNode('node-1', '', 'text');
const dbNode = await adapter.getNode(nodeId);
expect(dbNode).toBeDefined();

// After (properly waits):
const nodeId = service.createNode('node-1', '', 'text');
await waitForDatabaseWrites(); // ← Add this
const dbNode = await adapter.getNode(nodeId);
expect(dbNode).toBeDefined();

Acceptance Criteria

Expected Outcome

After this fix:

  • ✅ Tests will FAIL when they should (catching real bugs)
  • ✅ We'll see HTTP 500 errors that are currently hidden
  • ✅ Manual testing bugs WILL be caught by tests
  • ⚠️ More tests will fail (this is GOOD - they're catching real bugs!)

Testing

# After fix, run full suite:
bun run test

# Expect: More failures revealed (not fewer)
# Why: Tests are now catching bugs that were silently swallowed

Work Order

  1. This issue FIRST - Makes tests reliable
  2. Then re-assess Fix Indent/Outdent Operations (12 test failures) #228-Fix UI & Service Integration Issues (14 test failures) #233 with accurate failure counts
  3. Then fix the real bugs revealed by tests

Activity

  1. self-assigned this
    on Oct 11, 2025
  2. added a commit that references this issue on Oct 11, 2025
    486257c
  3. added a commit that references this issue on Oct 11, 2025
    42e1781
  4. malibio commented on Oct 11, 2025

    @malibio
    CollaboratorAuthor

    ✅ Issue Completed

    Successfully fixed test infrastructure to surface database errors and prevent race conditions.

    Implementation Summary

    Core Changes:

    • ✅ Added test error tracking to SharedNodeStore (testErrors[], getTestErrors(), clearTestErrors())
    • ✅ Added hasPendingWrites() getter for write queue visibility
    • ✅ Implemented waitForDatabaseWrites() helper with configurable polling
    • ✅ Removed error silencing - all database errors now logged in all environments
    • ✅ Updated 12 integration test files with new wait pattern (133 operations)

    Quality Improvements (from code review):

    • Added comprehensive JSDoc documentation
    • Made poll interval configurable
    • Added defensive checks for edge cases
    • Improved code comments for clarity

    Results

    Before: Tests passed even when database persistence was broken (false confidence)
    After: Tests now fail visibly when database writes fail, catching real bugs

    Test Coverage:

    • 12 test files updated
    • 133 wait patterns applied
    • All database operations now properly synchronized

    Quality Gates:

    • ✅ 0 linting errors/warnings
    • ✅ 0 formatting issues
    • ✅ 0 type errors
    • ✅ All Rust clippy checks pass

    Merged PR

    PR #236 merged to main in commit 42e1781

    Impact on Related Issues

    This infrastructure fix enables reliable testing for issues #228-#233. The test suite now properly detects database persistence failures that were previously hidden.

  5. added 2 commits that reference this issue on Feb 4, 2026
    0e493c1
    7756dac
  6. added a commit that references this issue on Feb 26, 2026
    d72cd93
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions