watcher.test.ts 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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. // An EXISTING non-source file changing — FileWatcher's `isSourceFile`
  389. // gate must drop it before scheduling sync. (It must exist on disk:
  390. // a VANISHED non-source path is the deleted-directory shape, which
  391. // deliberately schedules a sync — #1285.)
  392. fs.writeFileSync(path.join(testDir, 'src', 'readme.md'), '# docs\n');
  393. __emitWatchEventForTests(testDir, 'src/readme.md');
  394. // Wait a bit longer than debounce — sync should NOT trigger.
  395. await new Promise((r) => setTimeout(r, 400));
  396. expect(syncFn).not.toHaveBeenCalled();
  397. watcher.stop();
  398. });
  399. it('a deleted directory schedules a sync so child records get removed (#1285)', async () => {
  400. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  401. const watcher = newWatcher(syncFn, { debounceMs: 100 });
  402. watcher.start();
  403. await watcher.waitUntilReady();
  404. // A directory deletion arrives as ONE event on the directory path —
  405. // no extension, nothing on disk anymore. Must schedule a sync (the
  406. // sync's scan-diff removes the children), not be dropped as
  407. // "non-source".
  408. const sub = path.join(testDir, 'docs');
  409. fs.mkdirSync(path.join(sub, 'nested'), { recursive: true });
  410. fs.writeFileSync(path.join(sub, 'nested', 'mod.ts'), 'export const q = 1;');
  411. fs.rmSync(sub, { recursive: true, force: true });
  412. __emitWatchEventForTests(testDir, 'docs');
  413. await waitFor(() => syncFn.mock.calls.length > 0);
  414. expect(syncFn).toHaveBeenCalled();
  415. watcher.stop();
  416. });
  417. it('end-to-end: deleting a subdirectory removes its files from the index via watch sync (#1285)', async () => {
  418. // Real CodeGraph as the sync target; the watcher is inert and driven
  419. // by the synthetic event seam for determinism.
  420. fs.writeFileSync(path.join(testDir, 'root.ts'), 'export const r = 1;');
  421. const deep = path.join(testDir, 'docs', 'a', 'b');
  422. fs.mkdirSync(deep, { recursive: true });
  423. fs.writeFileSync(path.join(deep, 'inner.ts'), 'export const i = 2;');
  424. const cg = CodeGraph.initSync(testDir);
  425. await cg.indexAll();
  426. const before = cg.getFiles().map((f) => f.path);
  427. expect(before).toContain('docs/a/b/inner.ts');
  428. const syncFn = vi.fn(async () => {
  429. const r = await cg.sync();
  430. return { filesChanged: r.filesAdded + r.filesModified + r.filesRemoved, durationMs: r.durationMs };
  431. });
  432. const watcher = newWatcher(syncFn, { debounceMs: 100 });
  433. watcher.start();
  434. await watcher.waitUntilReady();
  435. fs.rmSync(path.join(testDir, 'docs'), { recursive: true, force: true });
  436. __emitWatchEventForTests(testDir, 'docs');
  437. await waitFor(() => syncFn.mock.calls.length > 0, 5000);
  438. // The sync body is async — poll the DB until the removal commits.
  439. await waitFor(() => !cg.getFiles().some((f) => f.path.startsWith('docs/')), 5000);
  440. const after = cg.getFiles().map((f) => f.path);
  441. expect(after).toContain('root.ts');
  442. expect(after.some((p) => p.startsWith('docs/'))).toBe(false);
  443. watcher.stop();
  444. cg.close();
  445. });
  446. it('should ignore .codegraph directory changes', async () => {
  447. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  448. const watcher = newWatcher(syncFn, { debounceMs: 200 });
  449. watcher.start();
  450. await watcher.waitUntilReady();
  451. // A .codegraph event — FileWatcher's `isAlwaysIgnored` filter must drop
  452. // it before scheduling sync.
  453. __emitWatchEventForTests(testDir, '.codegraph/db.sqlite');
  454. await new Promise((r) => setTimeout(r, 400));
  455. expect(syncFn).not.toHaveBeenCalled();
  456. watcher.stop();
  457. });
  458. it('should drop ignored/non-source paths but sync real source edits', async () => {
  459. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  460. const watcher = newWatcher(syncFn, { debounceMs: 200 });
  461. watcher.start();
  462. await watcher.waitUntilReady();
  463. // node_modules is in the default-ignore set (#407) → dropped by the
  464. // ignore matcher even without a .gitignore.
  465. __emitWatchEventForTests(testDir, 'node_modules/dep/index.js');
  466. // A normal source file still schedules sync (positive control).
  467. __emitWatchEventForTests(testDir, 'src/live.ts');
  468. await waitFor(() => syncFn.mock.calls.length > 0);
  469. expect(syncFn).toHaveBeenCalled();
  470. watcher.stop();
  471. });
  472. });
  473. describe('pending file tracking (#403)', () => {
  474. it('should expose edited paths via getPendingFiles before sync fires', async () => {
  475. // Slow debounce — pending entries are visible until the debounce fires.
  476. // The synthetic event is synchronous, so we can assert immediately.
  477. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 1, durationMs: 10 });
  478. const watcher = newWatcher(syncFn, { debounceMs: 2000 });
  479. watcher.start();
  480. await watcher.waitUntilReady();
  481. expect(watcher.getPendingFiles()).toEqual([]);
  482. __emitWatchEventForTests(testDir, 'src/pending.ts');
  483. const pending = watcher.getPendingFiles();
  484. const paths = pending.map((p) => p.path);
  485. expect(paths).toContain('src/pending.ts');
  486. const entry = pending.find((p) => p.path === 'src/pending.ts')!;
  487. expect(entry.firstSeenMs).toBeGreaterThan(0);
  488. expect(entry.lastSeenMs).toBeGreaterThanOrEqual(entry.firstSeenMs);
  489. // No sync running yet → indexing flag is false.
  490. expect(entry.indexing).toBe(false);
  491. watcher.stop();
  492. });
  493. it('should clear an entry only after a successful sync absorbing that edit', async () => {
  494. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 1, durationMs: 10 });
  495. const watcher = newWatcher(syncFn, { debounceMs: 200 });
  496. watcher.start();
  497. await watcher.waitUntilReady();
  498. __emitWatchEventForTests(testDir, 'src/fresh.ts');
  499. // Watcher saw the change → pendingFiles has the entry IMMEDIATELY.
  500. expect(watcher.getPendingFiles().some((p) => p.path === 'src/fresh.ts')).toBe(true);
  501. // Wait through debounce + sync; the entry should drop out.
  502. await waitFor(() => syncFn.mock.calls.length > 0);
  503. await waitFor(() => !watcher.getPendingFiles().some((p) => p.path === 'src/fresh.ts'));
  504. expect(watcher.getPendingFiles()).toEqual([]);
  505. watcher.stop();
  506. });
  507. it('should keep entries unchanged when sync fails (rescheduled work sees the same set)', async () => {
  508. // No initial-scan-triggered sync, so syncFn outcomes line up 1:1 with
  509. // explicit events.
  510. const syncFn = vi
  511. .fn()
  512. .mockRejectedValueOnce(new Error('boom')) // first sync rejects
  513. .mockResolvedValueOnce({ filesChanged: 1, durationMs: 10 }); // retry succeeds
  514. const onSyncError = vi.fn();
  515. const watcher = newWatcher(syncFn, { debounceMs: 100, onSyncError });
  516. watcher.start();
  517. await watcher.waitUntilReady();
  518. __emitWatchEventForTests(testDir, 'src/will-fail.ts');
  519. // Wait for the sync to reject.
  520. await waitFor(() => onSyncError.mock.calls.length > 0);
  521. // The file is STILL in pendingFiles — failure didn't drop it.
  522. const after = watcher.getPendingFiles();
  523. expect(after.some((p) => p.path === 'src/will-fail.ts')).toBe(true);
  524. // Retry resolves automatically; entry clears.
  525. await waitFor(
  526. () => !watcher.getPendingFiles().some((p) => p.path === 'src/will-fail.ts'),
  527. );
  528. watcher.stop();
  529. });
  530. it('should retain pending files and retry when syncFn throws LockUnavailableError (#449)', async () => {
  531. // CodeGraph.watch() converts the cross-process lock-failure no-op
  532. // into LockUnavailableError so the watcher's retry path picks it up
  533. // instead of falsely clearing pendingFiles. This test exercises the
  534. // contract directly.
  535. const syncFn = vi
  536. .fn()
  537. .mockRejectedValueOnce(new LockUnavailableError())
  538. .mockResolvedValueOnce({ filesChanged: 1, durationMs: 10 });
  539. const onSyncComplete = vi.fn();
  540. const onSyncError = vi.fn();
  541. const watcher = newWatcher(syncFn, {
  542. debounceMs: 100,
  543. onSyncComplete,
  544. onSyncError,
  545. });
  546. watcher.start();
  547. await watcher.waitUntilReady();
  548. __emitWatchEventForTests(testDir, 'src/locked.ts');
  549. await waitFor(() => syncFn.mock.calls.length >= 1);
  550. expect(watcher.getPendingFiles().some((p) => p.path === 'src/locked.ts')).toBe(true);
  551. // A held-lock no-op is not a sync failure — onSyncError stays quiet
  552. // so a long-running external indexer doesn't spam stderr every cycle.
  553. expect(onSyncError).not.toHaveBeenCalled();
  554. expect(onSyncComplete).not.toHaveBeenCalled();
  555. await waitFor(() => syncFn.mock.calls.length >= 2);
  556. await waitFor(
  557. () => !watcher.getPendingFiles().some((p) => p.path === 'src/locked.ts'),
  558. );
  559. expect(onSyncComplete).toHaveBeenCalledTimes(1);
  560. expect(onSyncComplete).toHaveBeenCalledWith({ filesChanged: 1, durationMs: 10 });
  561. expect(onSyncError).not.toHaveBeenCalled();
  562. watcher.stop();
  563. });
  564. });
  565. describe('callbacks', () => {
  566. it('should call onSyncComplete after successful sync', async () => {
  567. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 2, durationMs: 50 });
  568. const onSyncComplete = vi.fn();
  569. const watcher = newWatcher(syncFn, {
  570. debounceMs: 200,
  571. onSyncComplete,
  572. });
  573. watcher.start();
  574. await watcher.waitUntilReady();
  575. __emitWatchEventForTests(testDir, 'src/test.ts');
  576. await waitFor(() => onSyncComplete.mock.calls.length > 0);
  577. expect(onSyncComplete).toHaveBeenCalledWith({ filesChanged: 2, durationMs: 50 });
  578. watcher.stop();
  579. });
  580. it('should call onSyncError when sync throws', async () => {
  581. const syncFn = vi.fn().mockRejectedValue(new Error('sync failed'));
  582. const onSyncError = vi.fn();
  583. const watcher = newWatcher(syncFn, {
  584. debounceMs: 200,
  585. onSyncError,
  586. });
  587. watcher.start();
  588. await watcher.waitUntilReady();
  589. __emitWatchEventForTests(testDir, 'src/test.ts');
  590. await waitFor(() => onSyncError.mock.calls.length > 0);
  591. expect(onSyncError).toHaveBeenCalled();
  592. expect(onSyncError.mock.calls[0]![0]).toBeInstanceOf(Error);
  593. watcher.stop();
  594. });
  595. });
  596. describe('CodeGraph integration', () => {
  597. let cg: CodeGraph;
  598. afterEach(() => {
  599. if (cg) cg.close();
  600. });
  601. it('should watch and unwatch via CodeGraph API', async () => {
  602. cg = CodeGraph.initSync(testDir, {
  603. config: { include: ['**/*.ts'], exclude: [] },
  604. });
  605. await cg.indexAll();
  606. expect(cg.isWatching()).toBe(false);
  607. const started = cg.watch({ debounceMs: 200, inertForTests: true });
  608. expect(started).toBe(true);
  609. expect(cg.isWatching()).toBe(true);
  610. cg.unwatch();
  611. expect(cg.isWatching()).toBe(false);
  612. });
  613. it('should stop watching on close', async () => {
  614. cg = CodeGraph.initSync(testDir, {
  615. config: { include: ['**/*.ts'], exclude: [] },
  616. });
  617. await cg.indexAll();
  618. cg.watch({ debounceMs: 200, inertForTests: true });
  619. expect(cg.isWatching()).toBe(true);
  620. cg.close();
  621. // After close, isWatching should be false
  622. // (we can't call isWatching after close since DB is closed,
  623. // but we verify no errors are thrown)
  624. });
  625. it('should auto-sync when files change while watching (real fs.watch end-to-end)', async () => {
  626. // The one test that exercises the genuine native watcher: a real file
  627. // write must propagate through fs.watch → debounce → sync into the graph.
  628. cg = CodeGraph.initSync(testDir, {
  629. config: { include: ['**/*.ts'], exclude: [] },
  630. });
  631. await cg.indexAll();
  632. const initialStats = cg.getStats();
  633. const initialNodes = initialStats.nodeCount;
  634. cg.watch({ debounceMs: 300 });
  635. // Let the watcher install before writing, so the event isn't missed.
  636. await new Promise((r) => setTimeout(r, 100));
  637. // Real fs write — no synthetic event. The live watcher must catch it.
  638. fs.writeFileSync(
  639. path.join(testDir, 'src', 'added.ts'),
  640. 'export function added() { return 42; }'
  641. );
  642. // Wait for auto-sync to pick it up (real OS event delivery + debounce).
  643. await waitFor(() => {
  644. const stats = cg.getStats();
  645. return stats.nodeCount > initialNodes;
  646. }, 8000);
  647. // The new function should be in the graph.
  648. const results = cg.searchNodes('added');
  649. expect(results.length).toBeGreaterThan(0);
  650. cg.unwatch();
  651. });
  652. });
  653. });