watcher.test.ts 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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 = (paths?: string[]) => 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('scope config refresh (#1590)', () => {
  474. // The matcher used to be built once in start() and kept for the watcher's
  475. // lifetime, so a `codegraph.json` written AFTER the daemon started was
  476. // invisible to the live watcher while `codegraph sync` honoured it: the
  477. // CLI removed a newly excluded file and the watcher re-added it.
  478. it('a codegraph.json edit rebuilds the matcher and forces a full sync', async () => {
  479. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  480. const watcher = newWatcher(syncFn, { debounceMs: 100 });
  481. watcher.start();
  482. await watcher.waitUntilReady();
  483. // Scope the project after the watcher is already running.
  484. fs.mkdirSync(path.join(testDir, 'skipme'));
  485. fs.writeFileSync(path.join(testDir, 'skipme', 'b.ts'), 'export const b = 1;\n');
  486. fs.writeFileSync(path.join(testDir, 'codegraph.json'), JSON.stringify({ exclude: ['skipme/'] }));
  487. __emitWatchEventForTests(testDir, 'codegraph.json');
  488. // The config edit schedules a FULL sync (no scoped path list): only the
  489. // scan-diff can find the files the new scope drops or admits.
  490. await waitFor(() => syncFn.mock.calls.length > 0);
  491. expect(syncFn.mock.calls.length).toBe(1);
  492. expect(syncFn.mock.calls[0]![0]).toBeUndefined();
  493. expect(watcher.getPendingFiles()).toEqual([]);
  494. await new Promise((r) => setTimeout(r, 50)); // let runSync settle
  495. // An edit inside the newly excluded tree is dropped by the LIVE matcher:
  496. // not pending, and no sync scheduled for it.
  497. __emitWatchEventForTests(testDir, 'skipme/b.ts');
  498. expect(watcher.getPendingFiles().map((p) => p.path)).not.toContain('skipme/b.ts');
  499. await new Promise((r) => setTimeout(r, 300)); // > debounce
  500. expect(syncFn.mock.calls.length).toBe(1);
  501. // In-scope edits still sync, scoped to the edited path as before.
  502. __emitWatchEventForTests(testDir, 'src/index.ts');
  503. await waitFor(() => syncFn.mock.calls.length > 1);
  504. expect(syncFn.mock.calls[1]![0]).toEqual(['src/index.ts']);
  505. watcher.stop();
  506. });
  507. it('a root .gitignore edit is a scope change too', async () => {
  508. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  509. const watcher = newWatcher(syncFn, { debounceMs: 100 });
  510. watcher.start();
  511. await watcher.waitUntilReady();
  512. fs.mkdirSync(path.join(testDir, 'gen'));
  513. fs.writeFileSync(path.join(testDir, 'gen', 'out.ts'), 'export const g = 1;\n');
  514. fs.writeFileSync(path.join(testDir, '.gitignore'), 'gen/\n');
  515. __emitWatchEventForTests(testDir, '.gitignore');
  516. await waitFor(() => syncFn.mock.calls.length > 0);
  517. expect(syncFn.mock.calls[0]![0]).toBeUndefined();
  518. await new Promise((r) => setTimeout(r, 50));
  519. __emitWatchEventForTests(testDir, 'gen/out.ts');
  520. expect(watcher.getPendingFiles().map((p) => p.path)).not.toContain('gen/out.ts');
  521. await new Promise((r) => setTimeout(r, 300));
  522. expect(syncFn.mock.calls.length).toBe(1);
  523. watcher.stop();
  524. });
  525. it('a nested .gitignore inside the scope forces a full sync', async () => {
  526. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  527. const watcher = newWatcher(syncFn, { debounceMs: 100 });
  528. watcher.start();
  529. await watcher.waitUntilReady();
  530. fs.mkdirSync(path.join(testDir, 'sub'));
  531. fs.writeFileSync(path.join(testDir, 'sub', '.gitignore'), 'build/\n');
  532. __emitWatchEventForTests(testDir, 'sub/.gitignore');
  533. await waitFor(() => syncFn.mock.calls.length > 0);
  534. expect(syncFn.mock.calls[0]![0]).toBeUndefined();
  535. watcher.stop();
  536. });
  537. it('a .gitignore under an ignored tree (npm install churn) schedules nothing', async () => {
  538. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  539. const watcher = newWatcher(syncFn, { debounceMs: 100 });
  540. watcher.start();
  541. await watcher.waitUntilReady();
  542. fs.mkdirSync(path.join(testDir, 'node_modules', 'pkg'), { recursive: true });
  543. fs.writeFileSync(path.join(testDir, 'node_modules', 'pkg', '.gitignore'), 'lib/\n');
  544. __emitWatchEventForTests(testDir, 'node_modules/pkg/.gitignore');
  545. await new Promise((r) => setTimeout(r, 300));
  546. expect(syncFn).not.toHaveBeenCalled();
  547. watcher.stop();
  548. });
  549. it('removing the exclude again readmits the tree', async () => {
  550. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  551. const watcher = newWatcher(syncFn, { debounceMs: 100 });
  552. watcher.start();
  553. await watcher.waitUntilReady();
  554. fs.mkdirSync(path.join(testDir, 'skipme'));
  555. fs.writeFileSync(path.join(testDir, 'skipme', 'b.ts'), 'export const b = 1;\n');
  556. const cfg = path.join(testDir, 'codegraph.json');
  557. fs.writeFileSync(cfg, JSON.stringify({ exclude: ['skipme/'] }));
  558. __emitWatchEventForTests(testDir, 'codegraph.json');
  559. await waitFor(() => syncFn.mock.calls.length > 0);
  560. await new Promise((r) => setTimeout(r, 50));
  561. __emitWatchEventForTests(testDir, 'skipme/b.ts');
  562. expect(watcher.getPendingFiles().map((p) => p.path)).not.toContain('skipme/b.ts');
  563. // Drop the exclude. The loader is mtime-keyed, so make sure the second
  564. // write carries a distinct mtime even on a coarse-timestamp filesystem.
  565. fs.writeFileSync(cfg, JSON.stringify({}));
  566. const later = new Date(Date.now() + 5000);
  567. fs.utimesSync(cfg, later, later);
  568. __emitWatchEventForTests(testDir, 'codegraph.json');
  569. await waitFor(() => syncFn.mock.calls.length > 1);
  570. expect(syncFn.mock.calls[1]![0]).toBeUndefined();
  571. await new Promise((r) => setTimeout(r, 50));
  572. __emitWatchEventForTests(testDir, 'skipme/b.ts');
  573. expect(watcher.getPendingFiles().map((p) => p.path)).toContain('skipme/b.ts');
  574. watcher.stop();
  575. });
  576. });
  577. describe('pending file tracking (#403)', () => {
  578. it('should expose edited paths via getPendingFiles before sync fires', async () => {
  579. // Slow debounce — pending entries are visible until the debounce fires.
  580. // The synthetic event is synchronous, so we can assert immediately.
  581. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 1, durationMs: 10 });
  582. const watcher = newWatcher(syncFn, { debounceMs: 2000 });
  583. watcher.start();
  584. await watcher.waitUntilReady();
  585. expect(watcher.getPendingFiles()).toEqual([]);
  586. __emitWatchEventForTests(testDir, 'src/pending.ts');
  587. const pending = watcher.getPendingFiles();
  588. const paths = pending.map((p) => p.path);
  589. expect(paths).toContain('src/pending.ts');
  590. const entry = pending.find((p) => p.path === 'src/pending.ts')!;
  591. expect(entry.firstSeenMs).toBeGreaterThan(0);
  592. expect(entry.lastSeenMs).toBeGreaterThanOrEqual(entry.firstSeenMs);
  593. // No sync running yet → indexing flag is false.
  594. expect(entry.indexing).toBe(false);
  595. watcher.stop();
  596. });
  597. it('should clear an entry only after a successful sync absorbing that edit', async () => {
  598. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 1, durationMs: 10 });
  599. const watcher = newWatcher(syncFn, { debounceMs: 200 });
  600. watcher.start();
  601. await watcher.waitUntilReady();
  602. __emitWatchEventForTests(testDir, 'src/fresh.ts');
  603. // Watcher saw the change → pendingFiles has the entry IMMEDIATELY.
  604. expect(watcher.getPendingFiles().some((p) => p.path === 'src/fresh.ts')).toBe(true);
  605. // Wait through debounce + sync; the entry should drop out.
  606. await waitFor(() => syncFn.mock.calls.length > 0);
  607. await waitFor(() => !watcher.getPendingFiles().some((p) => p.path === 'src/fresh.ts'));
  608. expect(watcher.getPendingFiles()).toEqual([]);
  609. watcher.stop();
  610. });
  611. it('should keep entries unchanged when sync fails (rescheduled work sees the same set)', async () => {
  612. // No initial-scan-triggered sync, so syncFn outcomes line up 1:1 with
  613. // explicit events.
  614. const syncFn = vi
  615. .fn()
  616. .mockRejectedValueOnce(new Error('boom')) // first sync rejects
  617. .mockResolvedValueOnce({ filesChanged: 1, durationMs: 10 }); // retry succeeds
  618. const onSyncError = vi.fn();
  619. const watcher = newWatcher(syncFn, { debounceMs: 100, onSyncError });
  620. watcher.start();
  621. await watcher.waitUntilReady();
  622. __emitWatchEventForTests(testDir, 'src/will-fail.ts');
  623. // Wait for the sync to reject.
  624. await waitFor(() => onSyncError.mock.calls.length > 0);
  625. // The file is STILL in pendingFiles — failure didn't drop it.
  626. const after = watcher.getPendingFiles();
  627. expect(after.some((p) => p.path === 'src/will-fail.ts')).toBe(true);
  628. // Retry resolves automatically; entry clears.
  629. await waitFor(
  630. () => !watcher.getPendingFiles().some((p) => p.path === 'src/will-fail.ts'),
  631. );
  632. watcher.stop();
  633. });
  634. it('should retain pending files and retry when syncFn throws LockUnavailableError (#449)', async () => {
  635. // CodeGraph.watch() converts the cross-process lock-failure no-op
  636. // into LockUnavailableError so the watcher's retry path picks it up
  637. // instead of falsely clearing pendingFiles. This test exercises the
  638. // contract directly.
  639. const syncFn = vi
  640. .fn()
  641. .mockRejectedValueOnce(new LockUnavailableError())
  642. .mockResolvedValueOnce({ filesChanged: 1, durationMs: 10 });
  643. const onSyncComplete = vi.fn();
  644. const onSyncError = vi.fn();
  645. const watcher = newWatcher(syncFn, {
  646. debounceMs: 100,
  647. onSyncComplete,
  648. onSyncError,
  649. });
  650. watcher.start();
  651. await watcher.waitUntilReady();
  652. __emitWatchEventForTests(testDir, 'src/locked.ts');
  653. await waitFor(() => syncFn.mock.calls.length >= 1);
  654. expect(watcher.getPendingFiles().some((p) => p.path === 'src/locked.ts')).toBe(true);
  655. // A held-lock no-op is not a sync failure — onSyncError stays quiet
  656. // so a long-running external indexer doesn't spam stderr every cycle.
  657. expect(onSyncError).not.toHaveBeenCalled();
  658. expect(onSyncComplete).not.toHaveBeenCalled();
  659. await waitFor(() => syncFn.mock.calls.length >= 2);
  660. await waitFor(
  661. () => !watcher.getPendingFiles().some((p) => p.path === 'src/locked.ts'),
  662. );
  663. expect(onSyncComplete).toHaveBeenCalledTimes(1);
  664. expect(onSyncComplete).toHaveBeenCalledWith({ filesChanged: 1, durationMs: 10 });
  665. expect(onSyncError).not.toHaveBeenCalled();
  666. watcher.stop();
  667. });
  668. });
  669. describe('callbacks', () => {
  670. it('should call onSyncComplete after successful sync', async () => {
  671. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 2, durationMs: 50 });
  672. const onSyncComplete = vi.fn();
  673. const watcher = newWatcher(syncFn, {
  674. debounceMs: 200,
  675. onSyncComplete,
  676. });
  677. watcher.start();
  678. await watcher.waitUntilReady();
  679. __emitWatchEventForTests(testDir, 'src/test.ts');
  680. await waitFor(() => onSyncComplete.mock.calls.length > 0);
  681. expect(onSyncComplete).toHaveBeenCalledWith({ filesChanged: 2, durationMs: 50 });
  682. watcher.stop();
  683. });
  684. it('should call onSyncError when sync throws', async () => {
  685. const syncFn = vi.fn().mockRejectedValue(new Error('sync failed'));
  686. const onSyncError = vi.fn();
  687. const watcher = newWatcher(syncFn, {
  688. debounceMs: 200,
  689. onSyncError,
  690. });
  691. watcher.start();
  692. await watcher.waitUntilReady();
  693. __emitWatchEventForTests(testDir, 'src/test.ts');
  694. await waitFor(() => onSyncError.mock.calls.length > 0);
  695. expect(onSyncError).toHaveBeenCalled();
  696. expect(onSyncError.mock.calls[0]![0]).toBeInstanceOf(Error);
  697. watcher.stop();
  698. });
  699. });
  700. describe('CodeGraph integration', () => {
  701. let cg: CodeGraph;
  702. afterEach(() => {
  703. if (cg) cg.close();
  704. });
  705. it('should watch and unwatch via CodeGraph API', async () => {
  706. cg = CodeGraph.initSync(testDir, {
  707. config: { include: ['**/*.ts'], exclude: [] },
  708. });
  709. await cg.indexAll();
  710. expect(cg.isWatching()).toBe(false);
  711. const started = cg.watch({ debounceMs: 200, inertForTests: true });
  712. expect(started).toBe(true);
  713. expect(cg.isWatching()).toBe(true);
  714. cg.unwatch();
  715. expect(cg.isWatching()).toBe(false);
  716. });
  717. it('should stop watching on close', async () => {
  718. cg = CodeGraph.initSync(testDir, {
  719. config: { include: ['**/*.ts'], exclude: [] },
  720. });
  721. await cg.indexAll();
  722. cg.watch({ debounceMs: 200, inertForTests: true });
  723. expect(cg.isWatching()).toBe(true);
  724. cg.close();
  725. // After close, isWatching should be false
  726. // (we can't call isWatching after close since DB is closed,
  727. // but we verify no errors are thrown)
  728. });
  729. it('should auto-sync when files change while watching (real fs.watch end-to-end)', async () => {
  730. // The one test that exercises the genuine native watcher: a real file
  731. // write must propagate through fs.watch → debounce → sync into the graph.
  732. cg = CodeGraph.initSync(testDir, {
  733. config: { include: ['**/*.ts'], exclude: [] },
  734. });
  735. await cg.indexAll();
  736. const initialStats = cg.getStats();
  737. const initialNodes = initialStats.nodeCount;
  738. cg.watch({ debounceMs: 300 });
  739. // Let the watcher install before writing, so the event isn't missed.
  740. await new Promise((r) => setTimeout(r, 100));
  741. // Real fs write — no synthetic event. The live watcher must catch it.
  742. fs.writeFileSync(
  743. path.join(testDir, 'src', 'added.ts'),
  744. 'export function added() { return 42; }'
  745. );
  746. // Wait for auto-sync to pick it up (real OS event delivery + debounce).
  747. await waitFor(() => {
  748. const stats = cg.getStats();
  749. return stats.nodeCount > initialNodes;
  750. }, 8000);
  751. // The new function should be in the graph.
  752. const results = cg.searchNodes('added');
  753. expect(results.length).toBeGreaterThan(0);
  754. cg.unwatch();
  755. });
  756. });
  757. describe('scoped sync fast path (#watcher-scoped)', () => {
  758. it('passes the exact pending paths to syncFn for plain file events', async () => {
  759. const calls: (string[] | undefined)[] = [];
  760. const syncFn: SyncFn = async (paths?: string[]) => {
  761. calls.push(paths);
  762. return { filesChanged: 1, durationMs: 5 };
  763. };
  764. const watcher = newWatcher(syncFn, { debounceMs: 30 });
  765. expect(watcher.start()).toBe(true);
  766. fs.writeFileSync(path.join(testDir, 'src', 'a.ts'), 'export const a = 1;');
  767. __emitWatchEventForTests(testDir, 'src/a.ts');
  768. await new Promise((r) => setTimeout(r, 500));
  769. watcher.stop();
  770. expect(calls.length).toBeGreaterThan(0);
  771. expect(calls[0]).toEqual(['src/a.ts']);
  772. });
  773. it('falls back to a full sync (undefined paths) after a directory removal event', async () => {
  774. const calls: (string[] | undefined)[] = [];
  775. const syncFn: SyncFn = async (paths?: string[]) => {
  776. calls.push(paths);
  777. return { filesChanged: 0, durationMs: 5 };
  778. };
  779. const watcher = newWatcher(syncFn, { debounceMs: 30 });
  780. expect(watcher.start()).toBe(true);
  781. // A non-source path that does not exist on disk = the #1285 dir-removal shape.
  782. __emitWatchEventForTests(testDir, 'src/removed-dir');
  783. await new Promise((r) => setTimeout(r, 500));
  784. watcher.stop();
  785. expect(calls.length).toBeGreaterThan(0);
  786. expect(calls[0]).toBeUndefined();
  787. });
  788. it('a lone file event fires on the quick window, well before the full debounce', async () => {
  789. const calls: (string[] | undefined)[] = [];
  790. const syncFn: SyncFn = async (paths?: string[]) => {
  791. calls.push(paths);
  792. return { filesChanged: 1, durationMs: 1 };
  793. };
  794. // Full debounce is deliberately huge; the quick window (300ms) must win
  795. // for a single pending file.
  796. const watcher = newWatcher(syncFn, { debounceMs: 30_000 });
  797. expect(watcher.start()).toBe(true);
  798. fs.writeFileSync(path.join(testDir, 'src', 'quick.ts'), 'export const q = 1;');
  799. __emitWatchEventForTests(testDir, 'src/quick.ts');
  800. await new Promise((r) => setTimeout(r, 1500));
  801. watcher.stop();
  802. expect(calls.length).toBe(1);
  803. expect(calls[0]).toEqual(['src/quick.ts']);
  804. });
  805. });
  806. });