watcher.test.ts 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /**
  2. * FileWatcher Tests
  3. *
  4. * Tests for the file watcher that auto-syncs on changes.
  5. *
  6. * **Why inert mode + a synthetic event seam**: the watcher now uses Node's
  7. * native `fs.watch` (recursive on macOS/Windows, per-directory on Linux).
  8. * Under parallel vitest the OS watch subsystems (FSEvents / inotify) serve
  9. * many test files at once and event-delivery latency becomes non-deterministic
  10. * — a real fs change made in `beforeEach` can even leak into a later "should
  11. * NOT sync" assertion. So the unit tests construct the watcher with
  12. * `inertForTests: true` (no OS watcher installed) and drive its filter →
  13. * pendingFiles → debounce pipeline directly via
  14. * `__emitWatchEventForTests(root, relPath)` — deterministic, the same
  15. * convergence point a real event reaches. The debounce timer itself is the
  16. * real `setTimeout` (the unit under test). One end-to-end test ("auto-sync …
  17. * real fs.watch") runs the genuine native watcher against a real file write.
  18. */
  19. import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
  20. import { EventEmitter } from 'events';
  21. import * as fs from 'fs';
  22. import * as path from 'path';
  23. import * as os from 'os';
  24. import {
  25. FileWatcher,
  26. LockUnavailableError,
  27. __emitWatchEventForTests,
  28. __setFsWatchForTests,
  29. type WatchOptions,
  30. } from '../src/sync/watcher';
  31. import CodeGraph from '../src/index';
  32. type SyncFn = () => Promise<{ filesChanged: number; durationMs: number }>;
  33. /**
  34. * Helper to wait for a condition with timeout. Used for assertions that depend
  35. * on the debounce timer (real setTimeout) firing, or on the real watcher's
  36. * event delivery in the end-to-end test.
  37. */
  38. function waitFor(
  39. condition: () => boolean,
  40. timeoutMs = 2000,
  41. intervalMs = 25
  42. ): Promise<void> {
  43. return new Promise((resolve, reject) => {
  44. const start = Date.now();
  45. const check = () => {
  46. if (condition()) return resolve();
  47. if (Date.now() - start > timeoutMs) return reject(new Error('waitFor timed out'));
  48. setTimeout(check, intervalMs);
  49. };
  50. check();
  51. });
  52. }
  53. describe('FileWatcher', () => {
  54. let testDir: string;
  55. // Inert by default — unit tests drive events via __emitWatchEventForTests
  56. // and never depend on real OS watch delivery.
  57. const newWatcher = (syncFn: SyncFn, opts: WatchOptions = {}) =>
  58. new FileWatcher(testDir, syncFn, { inertForTests: true, ...opts });
  59. beforeEach(() => {
  60. testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-watcher-'));
  61. // Create a source file so the directory isn't empty
  62. const srcDir = path.join(testDir, 'src');
  63. fs.mkdirSync(srcDir);
  64. fs.writeFileSync(path.join(srcDir, 'index.ts'), 'export const x = 1;');
  65. });
  66. afterEach(() => {
  67. __setFsWatchForTests(null); // reset the injected fs.watch seam
  68. vi.restoreAllMocks();
  69. if (fs.existsSync(testDir)) {
  70. fs.rmSync(testDir, { recursive: true, force: true });
  71. }
  72. });
  73. describe('start/stop lifecycle', () => {
  74. it('should start and stop without errors', () => {
  75. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  76. const watcher = newWatcher(syncFn);
  77. const started = watcher.start();
  78. expect(started).toBe(true);
  79. expect(watcher.isActive()).toBe(true);
  80. watcher.stop();
  81. expect(watcher.isActive()).toBe(false);
  82. });
  83. it('should be idempotent on double start', () => {
  84. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  85. const watcher = newWatcher(syncFn);
  86. expect(watcher.start()).toBe(true);
  87. expect(watcher.start()).toBe(true); // Should not throw
  88. expect(watcher.isActive()).toBe(true);
  89. watcher.stop();
  90. });
  91. it('should be idempotent on double stop', () => {
  92. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  93. const watcher = newWatcher(syncFn);
  94. watcher.start();
  95. watcher.stop();
  96. watcher.stop(); // Should not throw
  97. expect(watcher.isActive()).toBe(false);
  98. });
  99. });
  100. describe('watch-resource exhaustion (#876)', () => {
  101. // These exercise the REAL fs.watch path (not inert) with an injected watch
  102. // that throws / emits EMFILE, covering whichever strategy the host platform
  103. // uses — recursive on macOS/Windows, per-directory on Linux. Each uses its
  104. // OWN EMPTY temp dir so exactly one watch is installed and the close-count
  105. // is deterministic across platforms.
  106. const mkEmptyDir = () => fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-exhaust-'));
  107. it('fails to start and degrades when fs.watch setup exhausts watch resources', () => {
  108. const dir = mkEmptyDir();
  109. const onDegraded = vi.fn();
  110. const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
  111. __setFsWatchForTests(() => {
  112. const err = new Error('too many open files') as NodeJS.ErrnoException;
  113. err.code = 'EMFILE';
  114. throw err;
  115. });
  116. const watcher = new FileWatcher(
  117. dir,
  118. vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 }),
  119. { debounceMs: 100, onDegraded }
  120. );
  121. try {
  122. // Both watch strategies must report startup exhaustion identically.
  123. expect(watcher.start()).toBe(false);
  124. expect(watcher.isActive()).toBe(false);
  125. expect(watcher.isDegraded()).toBe(true);
  126. expect(watcher.getDegradedReason()).toContain('auto-sync disabled');
  127. expect(onDegraded).toHaveBeenCalledTimes(1);
  128. expect(onDegraded).toHaveBeenCalledWith(expect.stringContaining('auto-sync disabled'));
  129. const disableWarnings = warnSpy.mock.calls.filter(
  130. (c) => typeof c[0] === 'string' && c[0].includes('File watcher disabled')
  131. );
  132. expect(disableWarnings).toHaveLength(1);
  133. } finally {
  134. fs.rmSync(dir, { recursive: true, force: true });
  135. }
  136. });
  137. it('degrades exactly once when the live watcher emits EMFILE at runtime', () => {
  138. const dir = mkEmptyDir();
  139. const onDegraded = vi.fn();
  140. const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
  141. const emitter = new EventEmitter();
  142. let closed = 0;
  143. const fakeWatcher = {
  144. on: (event: string, handler: (...a: unknown[]) => void) => {
  145. emitter.on(event, handler);
  146. return fakeWatcher;
  147. },
  148. close: () => {
  149. closed += 1;
  150. },
  151. } as unknown as fs.FSWatcher;
  152. __setFsWatchForTests(() => fakeWatcher);
  153. const watcher = new FileWatcher(
  154. dir,
  155. vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 }),
  156. { debounceMs: 100, onDegraded }
  157. );
  158. try {
  159. expect(watcher.start()).toBe(true);
  160. expect(watcher.isActive()).toBe(true);
  161. const err = new Error('too many open files') as NodeJS.ErrnoException;
  162. err.code = 'EMFILE';
  163. emitter.emit('error', err);
  164. emitter.emit('error', err); // a second burst must NOT degrade / close again
  165. expect(watcher.isActive()).toBe(false);
  166. expect(watcher.isDegraded()).toBe(true);
  167. expect(onDegraded).toHaveBeenCalledTimes(1);
  168. expect(closed).toBe(1);
  169. const disableWarnings = warnSpy.mock.calls.filter(
  170. (c) => typeof c[0] === 'string' && c[0].includes('File watcher disabled')
  171. );
  172. expect(disableWarnings).toHaveLength(1);
  173. } finally {
  174. fs.rmSync(dir, { recursive: true, force: true });
  175. }
  176. });
  177. it('reports isDegraded false / null reason while healthy', () => {
  178. const watcher = newWatcher(vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 }));
  179. watcher.start();
  180. expect(watcher.isDegraded()).toBe(false);
  181. expect(watcher.getDegradedReason()).toBeNull();
  182. watcher.stop();
  183. });
  184. it('warns once (NOT degrade) when Linux inotify watches are exhausted (ENOSPC)', () => {
  185. // ENOSPC only arises on the Linux per-directory path; force it so the test
  186. // runs the per-directory branch on any host. Synchronous test, restored in
  187. // finally — no await window for another test to observe the override.
  188. const realPlatform = process.platform;
  189. Object.defineProperty(process, 'platform', { value: 'linux', configurable: true });
  190. try {
  191. // Empty-but-for-one-subdir temp dir: the root watch succeeds, then the
  192. // child watch hits the (simulated) inotify budget — the realistic
  193. // "partial watch installed, then exhausted" shape.
  194. const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-inotify-'));
  195. fs.mkdirSync(path.join(dir, 'sub'));
  196. const onDegraded = vi.fn();
  197. const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
  198. const emitter = new EventEmitter();
  199. let calls = 0;
  200. const okWatcher = {
  201. on: (event: string, handler: (...a: unknown[]) => void) => {
  202. emitter.on(event, handler);
  203. return okWatcher;
  204. },
  205. close: () => {},
  206. } as unknown as fs.FSWatcher;
  207. __setFsWatchForTests(() => {
  208. calls += 1;
  209. if (calls === 1) return okWatcher; // root dir watch succeeds
  210. const err = new Error('ENOSPC: System limit for number of file watchers reached') as NodeJS.ErrnoException;
  211. err.code = 'ENOSPC';
  212. throw err; // every subsequent dir exhausts the inotify budget
  213. });
  214. const watcher = new FileWatcher(
  215. dir,
  216. vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 }),
  217. { debounceMs: 100, onDegraded }
  218. );
  219. try {
  220. // NON-fatal: the watcher starts (partial watch on the root), does NOT
  221. // degrade, and warns exactly once with the actionable sysctl remedy.
  222. expect(watcher.start()).toBe(true);
  223. expect(watcher.isActive()).toBe(true);
  224. expect(watcher.isDegraded()).toBe(false);
  225. expect(onDegraded).not.toHaveBeenCalled();
  226. const inotifyWarnings = warnSpy.mock.calls.filter(
  227. (c) => typeof c[0] === 'string' && c[0].includes('inotify watch limit')
  228. );
  229. expect(inotifyWarnings).toHaveLength(1);
  230. expect(String(inotifyWarnings[0]![0])).toContain('fs.inotify.max_user_watches');
  231. } finally {
  232. watcher.stop();
  233. fs.rmSync(dir, { recursive: true, force: true });
  234. }
  235. } finally {
  236. Object.defineProperty(process, 'platform', { value: realPlatform, configurable: true });
  237. }
  238. });
  239. });
  240. describe('lock contention degradation (#876)', () => {
  241. it('disables auto-sync after prolonged lock contention, with bounded retries', async () => {
  242. const syncFn = vi.fn().mockRejectedValue(new LockUnavailableError());
  243. const onSyncComplete = vi.fn();
  244. const onSyncError = vi.fn();
  245. const onDegraded = vi.fn();
  246. const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
  247. const watcher = newWatcher(syncFn, {
  248. debounceMs: 25,
  249. onSyncComplete,
  250. onSyncError,
  251. onDegraded,
  252. });
  253. watcher.start();
  254. await watcher.waitUntilReady();
  255. __emitWatchEventForTests(testDir, 'src/long-lock.ts');
  256. // 5 backoff retries (25·1,2,4,8,16 ms), then degrade on the 6th attempt.
  257. await waitFor(() => !watcher.isActive(), 8000, 20);
  258. expect(syncFn.mock.calls.length).toBeGreaterThanOrEqual(6); // MAX_LOCK_RETRIES + 1
  259. expect(watcher.isDegraded()).toBe(true);
  260. expect(onDegraded).toHaveBeenCalledTimes(1);
  261. expect(onDegraded).toHaveBeenCalledWith(expect.stringContaining('auto-sync disabled'));
  262. // A held lock is neither a sync error nor a completion.
  263. expect(onSyncError).not.toHaveBeenCalled();
  264. expect(onSyncComplete).not.toHaveBeenCalled();
  265. // Degrade stops the watcher, which clears pending state.
  266. expect(watcher.getPendingFiles()).toEqual([]);
  267. const disableWarnings = warnSpy.mock.calls.filter(
  268. (c) => typeof c[0] === 'string' && c[0].includes('File watcher disabled')
  269. );
  270. expect(disableWarnings).toHaveLength(1);
  271. });
  272. it('does NOT degrade on brief contention — backoff resets after a clean sync', async () => {
  273. const syncFn = vi
  274. .fn()
  275. .mockRejectedValueOnce(new LockUnavailableError())
  276. .mockRejectedValueOnce(new LockUnavailableError())
  277. .mockRejectedValueOnce(new LockUnavailableError())
  278. .mockResolvedValue({ filesChanged: 1, durationMs: 5 });
  279. const onDegraded = vi.fn();
  280. const onSyncComplete = vi.fn();
  281. const watcher = newWatcher(syncFn, { debounceMs: 25, onDegraded, onSyncComplete });
  282. watcher.start();
  283. await watcher.waitUntilReady();
  284. __emitWatchEventForTests(testDir, 'src/brief-lock.ts');
  285. await waitFor(() => onSyncComplete.mock.calls.length > 0, 4000, 20);
  286. expect(onDegraded).not.toHaveBeenCalled();
  287. expect(watcher.isDegraded()).toBe(false);
  288. expect(watcher.isActive()).toBe(true);
  289. expect(watcher.getPendingFiles().some((p) => p.path === 'src/brief-lock.ts')).toBe(false);
  290. watcher.stop();
  291. });
  292. });
  293. describe('persistent sync-failure degradation (#1127)', () => {
  294. it('disables auto-sync after a persistent non-lock sync failure, with bounded retries', async () => {
  295. // A deterministic pipeline failure (broken extractor on a file, DB
  296. // corruption, SQLITE_FULL, OOM) recurs every cycle. Unbounded it retried
  297. // forever at the debounce cadence; it must now back off and degrade.
  298. const syncFn = vi.fn().mockRejectedValue(new Error('extractor crashed on src/bad.ts'));
  299. const onSyncComplete = vi.fn();
  300. const onSyncError = vi.fn();
  301. const onDegraded = vi.fn();
  302. const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
  303. const watcher = newWatcher(syncFn, {
  304. debounceMs: 25,
  305. onSyncComplete,
  306. onSyncError,
  307. onDegraded,
  308. });
  309. watcher.start();
  310. await watcher.waitUntilReady();
  311. __emitWatchEventForTests(testDir, 'src/persistent-fail.ts');
  312. // 5 backoff retries (25·1,2,4,8,16 ms), then degrade on the 6th attempt.
  313. await waitFor(() => !watcher.isActive(), 8000, 20);
  314. expect(syncFn.mock.calls.length).toBeGreaterThanOrEqual(6); // MAX_SYNC_FAILURE_RETRIES + 1
  315. expect(watcher.isDegraded()).toBe(true);
  316. expect(onDegraded).toHaveBeenCalledTimes(1);
  317. expect(onDegraded).toHaveBeenCalledWith(expect.stringContaining('auto-sync disabled'));
  318. // The degrade reason carries the underlying error so the user can act.
  319. expect(onDegraded).toHaveBeenCalledWith(expect.stringContaining('extractor crashed'));
  320. // Unlike a held lock, a generic failure IS surfaced per-attempt.
  321. expect(onSyncError.mock.calls.length).toBeGreaterThanOrEqual(6);
  322. expect(onSyncComplete).not.toHaveBeenCalled();
  323. // Degrade stops the watcher, which clears pending state.
  324. expect(watcher.getPendingFiles()).toEqual([]);
  325. const disableWarnings = warnSpy.mock.calls.filter(
  326. (c) => typeof c[0] === 'string' && c[0].includes('File watcher disabled')
  327. );
  328. expect(disableWarnings).toHaveLength(1);
  329. });
  330. it('does NOT degrade on a transient sync failure — backoff resets after a clean sync', async () => {
  331. const syncFn = vi
  332. .fn()
  333. .mockRejectedValueOnce(new Error('transient blip'))
  334. .mockRejectedValueOnce(new Error('transient blip'))
  335. .mockRejectedValueOnce(new Error('transient blip'))
  336. .mockResolvedValue({ filesChanged: 1, durationMs: 5 });
  337. const onDegraded = vi.fn();
  338. const onSyncComplete = vi.fn();
  339. const watcher = newWatcher(syncFn, { debounceMs: 25, onDegraded, onSyncComplete });
  340. watcher.start();
  341. await watcher.waitUntilReady();
  342. __emitWatchEventForTests(testDir, 'src/transient-fail.ts');
  343. await waitFor(() => onSyncComplete.mock.calls.length > 0, 4000, 20);
  344. expect(onDegraded).not.toHaveBeenCalled();
  345. expect(watcher.isDegraded()).toBe(false);
  346. expect(watcher.isActive()).toBe(true);
  347. expect(watcher.getPendingFiles().some((p) => p.path === 'src/transient-fail.ts')).toBe(false);
  348. watcher.stop();
  349. });
  350. });
  351. describe('debounced sync', () => {
  352. it('should trigger sync after file change', async () => {
  353. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 1, durationMs: 10 });
  354. const watcher = newWatcher(syncFn, { debounceMs: 200 });
  355. watcher.start();
  356. await watcher.waitUntilReady();
  357. __emitWatchEventForTests(testDir, 'src/new.ts');
  358. // Wait for debounced sync to fire (real timer; 200ms + epsilon).
  359. await waitFor(() => syncFn.mock.calls.length > 0);
  360. expect(syncFn).toHaveBeenCalled();
  361. watcher.stop();
  362. });
  363. it('should debounce rapid changes into a single sync', async () => {
  364. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 1, durationMs: 10 });
  365. const watcher = newWatcher(syncFn, { debounceMs: 400 });
  366. watcher.start();
  367. await watcher.waitUntilReady();
  368. // Rapid-fire synthesized changes — each call resets the debounce timer.
  369. // Spacing them tighter than the debounce window proves the debounce
  370. // collapses them into one syncFn call.
  371. for (let i = 0; i < 5; i++) {
  372. __emitWatchEventForTests(testDir, `src/file${i}.ts`);
  373. await new Promise((r) => setTimeout(r, 50));
  374. }
  375. // Wait for the single debounced sync.
  376. await waitFor(() => syncFn.mock.calls.length > 0);
  377. // Should have been called once (debounced), not 5 times.
  378. expect(syncFn.mock.calls.length).toBe(1);
  379. watcher.stop();
  380. });
  381. });
  382. describe('filtering', () => {
  383. it('should ignore files not matching include patterns', async () => {
  384. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  385. const watcher = newWatcher(syncFn, { debounceMs: 200 });
  386. watcher.start();
  387. await watcher.waitUntilReady();
  388. // A non-source-file event — FileWatcher's `isSourceFile` gate must drop
  389. // it before scheduling sync.
  390. __emitWatchEventForTests(testDir, 'src/readme.md');
  391. // Wait a bit longer than debounce — sync should NOT trigger.
  392. await new Promise((r) => setTimeout(r, 400));
  393. expect(syncFn).not.toHaveBeenCalled();
  394. watcher.stop();
  395. });
  396. it('should ignore .codegraph directory changes', async () => {
  397. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  398. const watcher = newWatcher(syncFn, { debounceMs: 200 });
  399. watcher.start();
  400. await watcher.waitUntilReady();
  401. // A .codegraph event — FileWatcher's `isAlwaysIgnored` filter must drop
  402. // it before scheduling sync.
  403. __emitWatchEventForTests(testDir, '.codegraph/db.sqlite');
  404. await new Promise((r) => setTimeout(r, 400));
  405. expect(syncFn).not.toHaveBeenCalled();
  406. watcher.stop();
  407. });
  408. it('should drop ignored/non-source paths but sync real source edits', async () => {
  409. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  410. const watcher = newWatcher(syncFn, { debounceMs: 200 });
  411. watcher.start();
  412. await watcher.waitUntilReady();
  413. // node_modules is in the default-ignore set (#407) → dropped by the
  414. // ignore matcher even without a .gitignore.
  415. __emitWatchEventForTests(testDir, 'node_modules/dep/index.js');
  416. // A normal source file still schedules sync (positive control).
  417. __emitWatchEventForTests(testDir, 'src/live.ts');
  418. await waitFor(() => syncFn.mock.calls.length > 0);
  419. expect(syncFn).toHaveBeenCalled();
  420. watcher.stop();
  421. });
  422. });
  423. describe('pending file tracking (#403)', () => {
  424. it('should expose edited paths via getPendingFiles before sync fires', async () => {
  425. // Slow debounce — pending entries are visible until the debounce fires.
  426. // The synthetic event is synchronous, so we can assert immediately.
  427. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 1, durationMs: 10 });
  428. const watcher = newWatcher(syncFn, { debounceMs: 2000 });
  429. watcher.start();
  430. await watcher.waitUntilReady();
  431. expect(watcher.getPendingFiles()).toEqual([]);
  432. __emitWatchEventForTests(testDir, 'src/pending.ts');
  433. const pending = watcher.getPendingFiles();
  434. const paths = pending.map((p) => p.path);
  435. expect(paths).toContain('src/pending.ts');
  436. const entry = pending.find((p) => p.path === 'src/pending.ts')!;
  437. expect(entry.firstSeenMs).toBeGreaterThan(0);
  438. expect(entry.lastSeenMs).toBeGreaterThanOrEqual(entry.firstSeenMs);
  439. // No sync running yet → indexing flag is false.
  440. expect(entry.indexing).toBe(false);
  441. watcher.stop();
  442. });
  443. it('should clear an entry only after a successful sync absorbing that edit', async () => {
  444. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 1, durationMs: 10 });
  445. const watcher = newWatcher(syncFn, { debounceMs: 200 });
  446. watcher.start();
  447. await watcher.waitUntilReady();
  448. __emitWatchEventForTests(testDir, 'src/fresh.ts');
  449. // Watcher saw the change → pendingFiles has the entry IMMEDIATELY.
  450. expect(watcher.getPendingFiles().some((p) => p.path === 'src/fresh.ts')).toBe(true);
  451. // Wait through debounce + sync; the entry should drop out.
  452. await waitFor(() => syncFn.mock.calls.length > 0);
  453. await waitFor(() => !watcher.getPendingFiles().some((p) => p.path === 'src/fresh.ts'));
  454. expect(watcher.getPendingFiles()).toEqual([]);
  455. watcher.stop();
  456. });
  457. it('should keep entries unchanged when sync fails (rescheduled work sees the same set)', async () => {
  458. // No initial-scan-triggered sync, so syncFn outcomes line up 1:1 with
  459. // explicit events.
  460. const syncFn = vi
  461. .fn()
  462. .mockRejectedValueOnce(new Error('boom')) // first sync rejects
  463. .mockResolvedValueOnce({ filesChanged: 1, durationMs: 10 }); // retry succeeds
  464. const onSyncError = vi.fn();
  465. const watcher = newWatcher(syncFn, { debounceMs: 100, onSyncError });
  466. watcher.start();
  467. await watcher.waitUntilReady();
  468. __emitWatchEventForTests(testDir, 'src/will-fail.ts');
  469. // Wait for the sync to reject.
  470. await waitFor(() => onSyncError.mock.calls.length > 0);
  471. // The file is STILL in pendingFiles — failure didn't drop it.
  472. const after = watcher.getPendingFiles();
  473. expect(after.some((p) => p.path === 'src/will-fail.ts')).toBe(true);
  474. // Retry resolves automatically; entry clears.
  475. await waitFor(
  476. () => !watcher.getPendingFiles().some((p) => p.path === 'src/will-fail.ts'),
  477. );
  478. watcher.stop();
  479. });
  480. it('should retain pending files and retry when syncFn throws LockUnavailableError (#449)', async () => {
  481. // CodeGraph.watch() converts the cross-process lock-failure no-op
  482. // into LockUnavailableError so the watcher's retry path picks it up
  483. // instead of falsely clearing pendingFiles. This test exercises the
  484. // contract directly.
  485. const syncFn = vi
  486. .fn()
  487. .mockRejectedValueOnce(new LockUnavailableError())
  488. .mockResolvedValueOnce({ filesChanged: 1, durationMs: 10 });
  489. const onSyncComplete = vi.fn();
  490. const onSyncError = vi.fn();
  491. const watcher = newWatcher(syncFn, {
  492. debounceMs: 100,
  493. onSyncComplete,
  494. onSyncError,
  495. });
  496. watcher.start();
  497. await watcher.waitUntilReady();
  498. __emitWatchEventForTests(testDir, 'src/locked.ts');
  499. await waitFor(() => syncFn.mock.calls.length >= 1);
  500. expect(watcher.getPendingFiles().some((p) => p.path === 'src/locked.ts')).toBe(true);
  501. // A held-lock no-op is not a sync failure — onSyncError stays quiet
  502. // so a long-running external indexer doesn't spam stderr every cycle.
  503. expect(onSyncError).not.toHaveBeenCalled();
  504. expect(onSyncComplete).not.toHaveBeenCalled();
  505. await waitFor(() => syncFn.mock.calls.length >= 2);
  506. await waitFor(
  507. () => !watcher.getPendingFiles().some((p) => p.path === 'src/locked.ts'),
  508. );
  509. expect(onSyncComplete).toHaveBeenCalledTimes(1);
  510. expect(onSyncComplete).toHaveBeenCalledWith({ filesChanged: 1, durationMs: 10 });
  511. expect(onSyncError).not.toHaveBeenCalled();
  512. watcher.stop();
  513. });
  514. });
  515. describe('callbacks', () => {
  516. it('should call onSyncComplete after successful sync', async () => {
  517. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 2, durationMs: 50 });
  518. const onSyncComplete = vi.fn();
  519. const watcher = newWatcher(syncFn, {
  520. debounceMs: 200,
  521. onSyncComplete,
  522. });
  523. watcher.start();
  524. await watcher.waitUntilReady();
  525. __emitWatchEventForTests(testDir, 'src/test.ts');
  526. await waitFor(() => onSyncComplete.mock.calls.length > 0);
  527. expect(onSyncComplete).toHaveBeenCalledWith({ filesChanged: 2, durationMs: 50 });
  528. watcher.stop();
  529. });
  530. it('should call onSyncError when sync throws', async () => {
  531. const syncFn = vi.fn().mockRejectedValue(new Error('sync failed'));
  532. const onSyncError = vi.fn();
  533. const watcher = newWatcher(syncFn, {
  534. debounceMs: 200,
  535. onSyncError,
  536. });
  537. watcher.start();
  538. await watcher.waitUntilReady();
  539. __emitWatchEventForTests(testDir, 'src/test.ts');
  540. await waitFor(() => onSyncError.mock.calls.length > 0);
  541. expect(onSyncError).toHaveBeenCalled();
  542. expect(onSyncError.mock.calls[0]![0]).toBeInstanceOf(Error);
  543. watcher.stop();
  544. });
  545. });
  546. describe('CodeGraph integration', () => {
  547. let cg: CodeGraph;
  548. afterEach(() => {
  549. if (cg) cg.close();
  550. });
  551. it('should watch and unwatch via CodeGraph API', async () => {
  552. cg = CodeGraph.initSync(testDir, {
  553. config: { include: ['**/*.ts'], exclude: [] },
  554. });
  555. await cg.indexAll();
  556. expect(cg.isWatching()).toBe(false);
  557. const started = cg.watch({ debounceMs: 200, inertForTests: true });
  558. expect(started).toBe(true);
  559. expect(cg.isWatching()).toBe(true);
  560. cg.unwatch();
  561. expect(cg.isWatching()).toBe(false);
  562. });
  563. it('should stop watching on close', async () => {
  564. cg = CodeGraph.initSync(testDir, {
  565. config: { include: ['**/*.ts'], exclude: [] },
  566. });
  567. await cg.indexAll();
  568. cg.watch({ debounceMs: 200, inertForTests: true });
  569. expect(cg.isWatching()).toBe(true);
  570. cg.close();
  571. // After close, isWatching should be false
  572. // (we can't call isWatching after close since DB is closed,
  573. // but we verify no errors are thrown)
  574. });
  575. it('should auto-sync when files change while watching (real fs.watch end-to-end)', async () => {
  576. // The one test that exercises the genuine native watcher: a real file
  577. // write must propagate through fs.watch → debounce → sync into the graph.
  578. cg = CodeGraph.initSync(testDir, {
  579. config: { include: ['**/*.ts'], exclude: [] },
  580. });
  581. await cg.indexAll();
  582. const initialStats = cg.getStats();
  583. const initialNodes = initialStats.nodeCount;
  584. cg.watch({ debounceMs: 300 });
  585. // Let the watcher install before writing, so the event isn't missed.
  586. await new Promise((r) => setTimeout(r, 100));
  587. // Real fs write — no synthetic event. The live watcher must catch it.
  588. fs.writeFileSync(
  589. path.join(testDir, 'src', 'added.ts'),
  590. 'export function added() { return 42; }'
  591. );
  592. // Wait for auto-sync to pick it up (real OS event delivery + debounce).
  593. await waitFor(() => {
  594. const stats = cg.getStats();
  595. return stats.nodeCount > initialNodes;
  596. }, 8000);
  597. // The new function should be in the graph.
  598. const results = cg.searchNodes('added');
  599. expect(results.length).toBeGreaterThan(0);
  600. cg.unwatch();
  601. });
  602. });
  603. });