fix(tests): Stabilize tests and document chokidar-inspired Node watcher architecture

This commit is contained in:
2025-12-11 11:35:45 +00:00
parent afe462990f
commit f4243f190b
5 changed files with 830 additions and 578 deletions

View File

@@ -71,6 +71,57 @@ The `WriteStabilizer` class replaces chokidar's built-in write stabilization:
- **Deno**: Works on all versions with `Deno.watchFs()`
- **Bun**: Uses Node.js compatibility layer
### Architecture (v6.3.0+) - Chokidar-Inspired
The Node.js watcher has been refactored with elegant patterns inspired by [chokidar](https://github.com/paulmillr/chokidar):
**DirEntry Class:**
- Tracks directory contents with proper disposal
- Encapsulates file tracking and inode management
- `dispose()` method freezes object to catch use-after-cleanup bugs
**Throttler Pattern:**
- More sophisticated than simple debounce
- Tracks count of suppressed events
- Returns `false` if already throttled, `Throttler` object otherwise
- Used for change events to prevent duplicate emissions
**Atomic Write Handling:**
- Unlink events are queued with 100ms delay
- If add event arrives for same path within delay, unlink is cancelled
- Emits single `change` event instead of `unlink` + `add`
- Handles editor atomic saves elegantly
**Closer Registry:**
- Maps watch paths to cleanup functions
- Ensures proper resource cleanup on stop
- `addCloser()` / `runClosers()` pattern
**Event Constants Object:**
```typescript
const EV = {
ADD: 'add',
CHANGE: 'change',
UNLINK: 'unlink',
ADD_DIR: 'addDir',
UNLINK_DIR: 'unlinkDir',
READY: 'ready',
ERROR: 'error',
} as const;
```
**Configuration Constants:**
```typescript
const CONFIG = {
MAX_RETRIES: 3,
INITIAL_RESTART_DELAY: 1000,
MAX_RESTART_DELAY: 30000,
HEALTH_CHECK_INTERVAL: 30000,
ATOMIC_DELAY: 100,
TEMP_FILE_DELAY: 50,
} as const;
```
### Robustness Features (v6.1.0+)
The Node.js watcher includes automatic recovery mechanisms based on learnings from [chokidar](https://github.com/paulmillr/chokidar) and known [fs.watch issues](https://github.com/nodejs/node/issues/47058):
@@ -155,10 +206,20 @@ Example log output:
pnpm test
```
Test files:
- **test.basic.ts** - Core functionality (add, change, unlink events)
- **test.inode.ts** - Inode change detection, atomic writes
- **test.stress.ts** - Rapid modifications, many files, interleaved operations
Tests verify:
- Creating Smartwatch instance
- Adding glob patterns
- Receiving 'add' events for new files
- Receiving 'add', 'change', 'unlink' events
- Inode change detection (delete+recreate pattern)
- Atomic write pattern (temp file + rename)
- Rapid file modifications (debouncing)
- Many files created rapidly
- Interleaved add/change/delete operations
- Graceful shutdown
## Dev Dependencies