|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 2 | +import { promises as fs } from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import os from 'os'; |
| 5 | +import { startFileWatcher } from '../src/core/file-watcher.js'; |
| 6 | + |
| 7 | +describe('FileWatcher', () => { |
| 8 | + let tempDir: string; |
| 9 | + |
| 10 | + beforeEach(async () => { |
| 11 | + tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'file-watcher-test-')); |
| 12 | + }); |
| 13 | + |
| 14 | + afterEach(async () => { |
| 15 | + await fs.rm(tempDir, { recursive: true, force: true }); |
| 16 | + }); |
| 17 | + |
| 18 | + it('triggers onChanged after debounce window', async () => { |
| 19 | + const debounceMs = 400; |
| 20 | + let callCount = 0; |
| 21 | + |
| 22 | + const stop = startFileWatcher({ |
| 23 | + rootPath: tempDir, |
| 24 | + debounceMs, |
| 25 | + onChanged: () => { callCount++; }, |
| 26 | + }); |
| 27 | + |
| 28 | + try { |
| 29 | + // Give chokidar a moment to finish initializing before the first write |
| 30 | + await new Promise((resolve) => setTimeout(resolve, 100)); |
| 31 | + await fs.writeFile(path.join(tempDir, 'test.ts'), 'export const x = 1;'); |
| 32 | + // Wait for chokidar to pick up the event (including awaitWriteFinish stabilityThreshold) |
| 33 | + // + debounce window + OS scheduling slack |
| 34 | + await new Promise((resolve) => setTimeout(resolve, debounceMs + 1000)); |
| 35 | + expect(callCount).toBe(1); |
| 36 | + } finally { |
| 37 | + stop(); |
| 38 | + } |
| 39 | + }, 8000); |
| 40 | + |
| 41 | + it('debounces rapid changes into a single callback', async () => { |
| 42 | + const debounceMs = 300; |
| 43 | + let callCount = 0; |
| 44 | + |
| 45 | + const stop = startFileWatcher({ |
| 46 | + rootPath: tempDir, |
| 47 | + debounceMs, |
| 48 | + onChanged: () => { callCount++; }, |
| 49 | + }); |
| 50 | + |
| 51 | + try { |
| 52 | + // Give chokidar a moment to finish initializing before the first write |
| 53 | + await new Promise((resolve) => setTimeout(resolve, 100)); |
| 54 | + // Write 5 files in quick succession — all within the debounce window |
| 55 | + for (let i = 0; i < 5; i++) { |
| 56 | + await fs.writeFile(path.join(tempDir, `file${i}.ts`), `export const x${i} = ${i};`); |
| 57 | + await new Promise((resolve) => setTimeout(resolve, 50)); |
| 58 | + } |
| 59 | + // Wait for debounce to settle |
| 60 | + await new Promise((resolve) => setTimeout(resolve, debounceMs + 400)); |
| 61 | + expect(callCount).toBe(1); |
| 62 | + } finally { |
| 63 | + stop(); |
| 64 | + } |
| 65 | + }, 8000); |
| 66 | + |
| 67 | + it('stop() cancels a pending callback', async () => { |
| 68 | + const debounceMs = 500; |
| 69 | + let callCount = 0; |
| 70 | + |
| 71 | + const stop = startFileWatcher({ |
| 72 | + rootPath: tempDir, |
| 73 | + debounceMs, |
| 74 | + onChanged: () => { callCount++; }, |
| 75 | + }); |
| 76 | + |
| 77 | + // Give chokidar a moment to finish initializing before the first write |
| 78 | + await new Promise((resolve) => setTimeout(resolve, 100)); |
| 79 | + await fs.writeFile(path.join(tempDir, 'cancel.ts'), 'export const y = 99;'); |
| 80 | + // Let chokidar detect the event (including awaitWriteFinish stabilityThreshold) |
| 81 | + // but stop before the debounce window expires. |
| 82 | + await new Promise((resolve) => setTimeout(resolve, 350)); |
| 83 | + stop(); |
| 84 | + // Wait past where debounce would have fired |
| 85 | + await new Promise((resolve) => setTimeout(resolve, debounceMs + 200)); |
| 86 | + expect(callCount).toBe(0); |
| 87 | + }, 5000); |
| 88 | +}); |
0 commit comments