Skip to content

Fix unhandled promise rejections in test suite - #268

Merged
malibio merged 3 commits into
mainfrom
feature/issue-258-fix-unhandled-promise-rejections
Oct 13, 2025
Merged

malibio merged 3 commits into
mainfrom
feature/issue-258-fix-unhandled-promise-rejections

Conversation

@malibio

@malibio malibio commented Oct 13, 2025 •

Copy link
Copy Markdown
Collaborator

Problem

The SharedNodeStore test suite had 966 unhandled promise rejections from PersistenceCoordinator.cancelPending() when cleanup cancelled pending operations during test teardown.

Solution

Implemented a comprehensive fix following architectural guidance from the frontend-architect agent, with all code review feedback addressed:

1. Custom Error Type Pattern

  • Created OperationCancelledError class - Distinguishes expected cancellation from real errors
  • Updated cancelPending() - Rejects with OperationCancelledError instead of generic Error
  • Type-safe error handling - Uses instanceof checks to filter cancellations

2. Async Reset with Promise Cleanup

  • Made reset() async - Properly handles cancellation promises and waits for all rejections to be caught
  • Added concurrent reset guard - Prevents race conditions from multiple reset() calls
  • Improved documentation - Enhanced comments explaining promise capture timing

3. Fixed All Unhandled Rejections

  • Root cause: SharedNodeStore methods (setNode, updateNode, deleteNode) called persist() but didn't capture/handle the returned promises
  • Solution: Added catch handlers for OperationCancelledError in all three methods
  • Result: Reduced from 966 to 0 unhandled rejections ✅

4. Updated Test Cleanup

  • Updated all test afterEach blocks to await coordinator.reset()
  • Removed workaround setTimeout delays
  • Makes tests more reliable and deterministic

Changes

Core Implementation

  • persistence-coordinator.svelte.ts (persistence-coordinator.svelte.ts:33):
    • Added OperationCancelledError class with explicit intent
    • Updated cancelPending() to use custom error
    • Made reset() async with concurrent guard and proper promise handling
    • Enhanced code documentation

SharedNodeStore Fixes

  • shared-node-store.ts:
    • Added OperationCancelledError import
    • Added catch handlers in setNode() (line 455)
    • Added catch handlers in updateNode() (line 319)
    • Added catch handlers in deleteNode() (line 526)

Test Updates

  • shared-node-store.test.ts (shared-node-store.test.ts:62): Updated afterEach to await reset()
  • persistence-coordinator.test.ts (persistence-coordinator.test.ts:30): Updated afterEach to await reset()
  • setup.ts (setup.ts:182): Updated global afterEach to await reset()

Documentation

  • dependency-based-persistence.md: Added comprehensive section on OperationCancelledError explaining:
    • Why it exists (distinguishes control flow from failures)
    • When operations are cancelled
    • How to handle cancellations in production code
    • Type-safe error handling patterns

Breaking Changes

PersistenceCoordinator.reset() is now async

  • Change: reset(): void → async reset(): Promise<void>
  • Impact: Test-only method (no production usage found)
  • Migration: Update all coordinator.reset() calls to await coordinator.reset()

All test call sites have been updated in this PR.

Results

  • ✅ Reduced unhandled rejections from 966 to 0 (100% reduction!)
  • ✅ All tests pass successfully (52/52 in SharedNodeStore)
  • ✅ Quality checks pass (lint, format, type check)
  • ✅ Concurrent reset protection added
  • ✅ Comprehensive documentation added
  • ✅ All code review feedback addressed

Testing

# Verify the fix
bunx vitest run src/tests/services/shared-node-store.test.ts
# Output: "✓ 52 tests passed" with 0 unhandled errors

# Full test suite
bunx vitest run
# All tests pass

Architecture Notes

This implementation follows recommended patterns:

  • Preserves cancellation semantics - Operations still reject promises (correct behavior)
  • Makes intent explicit - Custom error type distinguishes cancellation from failure
  • Type-safe error handling - instanceof checks enable precise control flow
  • Non-breaking for production - Only affects test code
  • Proper async cleanup - Prevents unhandled rejections without masking real errors

Closes #258

🤖 Generated with Claude Code

Co-Authored-By: Claude [email protected]

malibio and others added 3 commits October 13, 2025 02:13
## Problem
SharedNodeStore test suite had 966 unhandled promise rejections from
PersistenceCoordinator.cancelPending() when cleanup cancelled pending
operations during test teardown.

## Solution
1. Created OperationCancelledError custom error class to distinguish
   cancellation from real errors
2. Updated cancelPending() to use OperationCancelledError
3. Made reset() async to properly handle cancellation promises
4. Updated all test cleanup to await coordinator.reset()

## Changes
- persistence-coordinator.svelte.ts:
  - Added OperationCancelledError class
  - Updated cancelPending() to reject with OperationCancelledError
  - Made reset() async with proper promise handling
- shared-node-store.test.ts: Updated afterEach to await reset()
- persistence-coordinator.test.ts: Updated afterEach to await reset()
- setup.ts: Updated global afterEach to await reset()

## Results
- Reduced unhandled rejections from 966 to 8 (99.2% reduction)
- Remaining 8 are edge cases from operations cancelled during
  test execution (not cleanup)
- All tests pass successfully

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
## Changes Made

### 1. Fixed Remaining 8 Unhandled Rejections
- **Root cause**: SharedNodeStore calls `persist()` but doesn't capture/handle promises
- **Solution**: Added catch handlers for OperationCancelledError in three locations:
  - `setNode()` method (line 455)
  - `updateNode()` method (line 319)
  - `deleteNode()` method (line 526)
- **Result**: Reduced from 8 unhandled rejections to **0** ✅

### 2. Added Concurrent Reset Guard
- Added `isResetting` flag to prevent concurrent reset() calls
- Wrapped reset() logic in try/finally to ensure flag is always cleared
- Prevents race conditions if reset() called multiple times

### 3. Improved Code Documentation
- Enhanced comments in reset() method explaining promise capture timing
- Clarified why we iterate before calling cancelPending()
- Added explanation of concurrent reset guard behavior

### 4. Verified Production Code Impact
- Confirmed reset() is test-only (no production usage)
- Breaking change (async reset) has no production impact

## Test Results
- ✅ SharedNodeStore tests: 52 passed, **0 unhandled rejections** (down from 966!)
- ✅ All quality checks pass (lint, format, type check)

## Files Modified
- `persistence-coordinator.svelte.ts`: Added reset guard, improved comments
- `shared-node-store.ts`: Added OperationCancelledError import and catch handlers

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Added comprehensive documentation to dependency-based-persistence.md explaining:
- Why OperationCancelledError exists (distinguishes control flow from failures)
- When operations are cancelled (superseded, test cleanup, node deletion)
- How to handle cancellations in production code
- Type-safe error handling with instanceof checks

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@malibio
malibio merged commit 2c585f8 into main Oct 13, 2025
@malibio
malibio deleted the feature/issue-258-fix-unhandled-promise-rejections branch October 13, 2025 00:25
malibio added a commit that referenced this pull request Feb 4, 2026
* Fix unhandled promise rejections in test suite (closes #258)

## Problem
SharedNodeStore test suite had 966 unhandled promise rejections from
PersistenceCoordinator.cancelPending() when cleanup cancelled pending
operations during test teardown.

## Solution
1. Created OperationCancelledError custom error class to distinguish
   cancellation from real errors
2. Updated cancelPending() to use OperationCancelledError
3. Made reset() async to properly handle cancellation promises
4. Updated all test cleanup to await coordinator.reset()

## Changes
- persistence-coordinator.svelte.ts:
  - Added OperationCancelledError class
  - Updated cancelPending() to reject with OperationCancelledError
  - Made reset() async with proper promise handling
- shared-node-store.test.ts: Updated afterEach to await reset()
- persistence-coordinator.test.ts: Updated afterEach to await reset()
- setup.ts: Updated global afterEach to await reset()

## Results
- Reduced unhandled rejections from 966 to 8 (99.2% reduction)
- Remaining 8 are edge cases from operations cancelled during
  test execution (not cleanup)
- All tests pass successfully

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Address code review feedback: Complete fix for unhandled rejections

## Changes Made

### 1. Fixed Remaining 8 Unhandled Rejections
- **Root cause**: SharedNodeStore calls `persist()` but doesn't capture/handle promises
- **Solution**: Added catch handlers for OperationCancelledError in three locations:
  - `setNode()` method (line 455)
  - `updateNode()` method (line 319)
  - `deleteNode()` method (line 526)
- **Result**: Reduced from 8 unhandled rejections to **0** ✅

### 2. Added Concurrent Reset Guard
- Added `isResetting` flag to prevent concurrent reset() calls
- Wrapped reset() logic in try/finally to ensure flag is always cleared
- Prevents race conditions if reset() called multiple times

### 3. Improved Code Documentation
- Enhanced comments in reset() method explaining promise capture timing
- Clarified why we iterate before calling cancelPending()
- Added explanation of concurrent reset guard behavior

### 4. Verified Production Code Impact
- Confirmed reset() is test-only (no production usage)
- Breaking change (async reset) has no production impact

## Test Results
- ✅ SharedNodeStore tests: 52 passed, **0 unhandled rejections** (down from 966!)
- ✅ All quality checks pass (lint, format, type check)

## Files Modified
- `persistence-coordinator.svelte.ts`: Added reset guard, improved comments
- `shared-node-store.ts`: Added OperationCancelledError import and catch handlers

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Add documentation for OperationCancelledError

Added comprehensive documentation to dependency-based-persistence.md explaining:
- Why OperationCancelledError exists (distinguishes control flow from failures)
- When operations are cancelled (superseded, test cleanup, node deletion)
- How to handle cancellations in production code
- Type-safe error handling with instanceof checks

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

---------

Co-authored-by: Claude <[email protected]>
malibio added a commit that referenced this pull request Feb 5, 2026
* Fix unhandled promise rejections in test suite (closes #258)

## Problem
SharedNodeStore test suite had 966 unhandled promise rejections from
PersistenceCoordinator.cancelPending() when cleanup cancelled pending
operations during test teardown.

## Solution
1. Created OperationCancelledError custom error class to distinguish
   cancellation from real errors
2. Updated cancelPending() to use OperationCancelledError
3. Made reset() async to properly handle cancellation promises
4. Updated all test cleanup to await coordinator.reset()

## Changes
- persistence-coordinator.svelte.ts:
  - Added OperationCancelledError class
  - Updated cancelPending() to reject with OperationCancelledError
  - Made reset() async with proper promise handling
- shared-node-store.test.ts: Updated afterEach to await reset()
- persistence-coordinator.test.ts: Updated afterEach to await reset()
- setup.ts: Updated global afterEach to await reset()

## Results
- Reduced unhandled rejections from 966 to 8 (99.2% reduction)
- Remaining 8 are edge cases from operations cancelled during
  test execution (not cleanup)
- All tests pass successfully

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Address code review feedback: Complete fix for unhandled rejections

## Changes Made

### 1. Fixed Remaining 8 Unhandled Rejections
- **Root cause**: SharedNodeStore calls `persist()` but doesn't capture/handle promises
- **Solution**: Added catch handlers for OperationCancelledError in three locations:
  - `setNode()` method (line 455)
  - `updateNode()` method (line 319)
  - `deleteNode()` method (line 526)
- **Result**: Reduced from 8 unhandled rejections to **0** ✅

### 2. Added Concurrent Reset Guard
- Added `isResetting` flag to prevent concurrent reset() calls
- Wrapped reset() logic in try/finally to ensure flag is always cleared
- Prevents race conditions if reset() called multiple times

### 3. Improved Code Documentation
- Enhanced comments in reset() method explaining promise capture timing
- Clarified why we iterate before calling cancelPending()
- Added explanation of concurrent reset guard behavior

### 4. Verified Production Code Impact
- Confirmed reset() is test-only (no production usage)
- Breaking change (async reset) has no production impact

## Test Results
- ✅ SharedNodeStore tests: 52 passed, **0 unhandled rejections** (down from 966!)
- ✅ All quality checks pass (lint, format, type check)

## Files Modified
- `persistence-coordinator.svelte.ts`: Added reset guard, improved comments
- `shared-node-store.ts`: Added OperationCancelledError import and catch handlers

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Add documentation for OperationCancelledError

Added comprehensive documentation to dependency-based-persistence.md explaining:
- Why OperationCancelledError exists (distinguishes control flow from failures)
- When operations are cancelled (superseded, test cleanup, node deletion)
- How to handle cancellations in production code
- Type-safe error handling with instanceof checks

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

---------

Co-authored-by: Claude <[email protected]>
malibio added a commit that referenced this pull request Feb 26, 2026
* Fix unhandled promise rejections in test suite (closes #258)

## Problem
SharedNodeStore test suite had 966 unhandled promise rejections from
PersistenceCoordinator.cancelPending() when cleanup cancelled pending
operations during test teardown.

## Solution
1. Created OperationCancelledError custom error class to distinguish
   cancellation from real errors
2. Updated cancelPending() to use OperationCancelledError
3. Made reset() async to properly handle cancellation promises
4. Updated all test cleanup to await coordinator.reset()

## Changes
- persistence-coordinator.svelte.ts:
  - Added OperationCancelledError class
  - Updated cancelPending() to reject with OperationCancelledError
  - Made reset() async with proper promise handling
- shared-node-store.test.ts: Updated afterEach to await reset()
- persistence-coordinator.test.ts: Updated afterEach to await reset()
- setup.ts: Updated global afterEach to await reset()

## Results
- Reduced unhandled rejections from 966 to 8 (99.2% reduction)
- Remaining 8 are edge cases from operations cancelled during
  test execution (not cleanup)
- All tests pass successfully

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Address code review feedback: Complete fix for unhandled rejections

## Changes Made

### 1. Fixed Remaining 8 Unhandled Rejections
- **Root cause**: SharedNodeStore calls `persist()` but doesn't capture/handle promises
- **Solution**: Added catch handlers for OperationCancelledError in three locations:
  - `setNode()` method (line 455)
  - `updateNode()` method (line 319)
  - `deleteNode()` method (line 526)
- **Result**: Reduced from 8 unhandled rejections to **0** ✅

### 2. Added Concurrent Reset Guard
- Added `isResetting` flag to prevent concurrent reset() calls
- Wrapped reset() logic in try/finally to ensure flag is always cleared
- Prevents race conditions if reset() called multiple times

### 3. Improved Code Documentation
- Enhanced comments in reset() method explaining promise capture timing
- Clarified why we iterate before calling cancelPending()
- Added explanation of concurrent reset guard behavior

### 4. Verified Production Code Impact
- Confirmed reset() is test-only (no production usage)
- Breaking change (async reset) has no production impact

## Test Results
- ✅ SharedNodeStore tests: 52 passed, **0 unhandled rejections** (down from 966!)
- ✅ All quality checks pass (lint, format, type check)

## Files Modified
- `persistence-coordinator.svelte.ts`: Added reset guard, improved comments
- `shared-node-store.ts`: Added OperationCancelledError import and catch handlers

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Add documentation for OperationCancelledError

Added comprehensive documentation to dependency-based-persistence.md explaining:
- Why OperationCancelledError exists (distinguishes control flow from failures)
- When operations are cancelled (superseded, test cleanup, node deletion)
- How to handle cancellations in production code
- Type-safe error handling with instanceof checks

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

---------

Co-authored-by: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix SharedNodeStore test suite unhandled promise rejections

1 participant