1
0

watcher.test.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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('debounced sync', () => {
  294. it('should trigger sync after file change', async () => {
  295. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 1, durationMs: 10 });
  296. const watcher = newWatcher(syncFn, { debounceMs: 200 });
  297. watcher.start();
  298. await watcher.waitUntilReady();
  299. __emitWatchEventForTests(testDir, 'src/new.ts');
  300. // Wait for debounced sync to fire (real timer; 200ms + epsilon).
  301. await waitFor(() => syncFn.mock.calls.length > 0);
  302. expect(syncFn).toHaveBeenCalled();
  303. watcher.stop();
  304. });
  305. it('should debounce rapid changes into a single sync', async () => {
  306. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 1, durationMs: 10 });
  307. const watcher = newWatcher(syncFn, { debounceMs: 400 });
  308. watcher.start();
  309. await watcher.waitUntilReady();
  310. // Rapid-fire synthesized changes — each call resets the debounce timer.
  311. // Spacing them tighter than the debounce window proves the debounce
  312. // collapses them into one syncFn call.
  313. for (let i = 0; i < 5; i++) {
  314. __emitWatchEventForTests(testDir, `src/file${i}.ts`);
  315. await new Promise((r) => setTimeout(r, 50));
  316. }
  317. // Wait for the single debounced sync.
  318. await waitFor(() => syncFn.mock.calls.length > 0);
  319. // Should have been called once (debounced), not 5 times.
  320. expect(syncFn.mock.calls.length).toBe(1);
  321. watcher.stop();
  322. });
  323. });
  324. describe('filtering', () => {
  325. it('should ignore files not matching include patterns', async () => {
  326. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  327. const watcher = newWatcher(syncFn, { debounceMs: 200 });
  328. watcher.start();
  329. await watcher.waitUntilReady();
  330. // A non-source-file event — FileWatcher's `isSourceFile` gate must drop
  331. // it before scheduling sync.
  332. __emitWatchEventForTests(testDir, 'src/readme.md');
  333. // Wait a bit longer than debounce — sync should NOT trigger.
  334. await new Promise((r) => setTimeout(r, 400));
  335. expect(syncFn).not.toHaveBeenCalled();
  336. watcher.stop();
  337. });
  338. it('should ignore .codegraph directory changes', async () => {
  339. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  340. const watcher = newWatcher(syncFn, { debounceMs: 200 });
  341. watcher.start();
  342. await watcher.waitUntilReady();
  343. // A .codegraph event — FileWatcher's `isAlwaysIgnored` filter must drop
  344. // it before scheduling sync.
  345. __emitWatchEventForTests(testDir, '.codegraph/db.sqlite');
  346. await new Promise((r) => setTimeout(r, 400));
  347. expect(syncFn).not.toHaveBeenCalled();
  348. watcher.stop();
  349. });
  350. it('should drop ignored/non-source paths but sync real source edits', async () => {
  351. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  352. const watcher = newWatcher(syncFn, { debounceMs: 200 });
  353. watcher.start();
  354. await watcher.waitUntilReady();
  355. // node_modules is in the default-ignore set (#407) → dropped by the
  356. // ignore matcher even without a .gitignore.
  357. __emitWatchEventForTests(testDir, 'node_modules/dep/index.js');
  358. // A normal source file still schedules sync (positive control).
  359. __emitWatchEventForTests(testDir, 'src/live.ts');
  360. await waitFor(() => syncFn.mock.calls.length > 0);
  361. expect(syncFn).toHaveBeenCalled();
  362. watcher.stop();
  363. });
  364. });
  365. describe('pending file tracking (#403)', () => {
  366. it('should expose edited paths via getPendingFiles before sync fires', async () => {
  367. // Slow debounce — pending entries are visible until the debounce fires.
  368. // The synthetic event is synchronous, so we can assert immediately.
  369. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 1, durationMs: 10 });
  370. const watcher = newWatcher(syncFn, { debounceMs: 2000 });
  371. watcher.start();
  372. await watcher.waitUntilReady();
  373. expect(watcher.getPendingFiles()).toEqual([]);
  374. __emitWatchEventForTests(testDir, 'src/pending.ts');
  375. const pending = watcher.getPendingFiles();
  376. const paths = pending.map((p) => p.path);
  377. expect(paths).toContain('src/pending.ts');
  378. const entry = pending.find((p) => p.path === 'src/pending.ts')!;
  379. expect(entry.firstSeenMs).toBeGreaterThan(0);
  380. expect(entry.lastSeenMs).toBeGreaterThanOrEqual(entry.firstSeenMs);
  381. // No sync running yet → indexing flag is false.
  382. expect(entry.indexing).toBe(false);
  383. watcher.stop();
  384. });
  385. it('should clear an entry only after a successful sync absorbing that edit', async () => {
  386. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 1, durationMs: 10 });
  387. const watcher = newWatcher(syncFn, { debounceMs: 200 });
  388. watcher.start();
  389. await watcher.waitUntilReady();
  390. __emitWatchEventForTests(testDir, 'src/fresh.ts');
  391. // Watcher saw the change → pendingFiles has the entry IMMEDIATELY.
  392. expect(watcher.getPendingFiles().some((p) => p.path === 'src/fresh.ts')).toBe(true);
  393. // Wait through debounce + sync; the entry should drop out.
  394. await waitFor(() => syncFn.mock.calls.length > 0);
  395. await waitFor(() => !watcher.getPendingFiles().some((p) => p.path === 'src/fresh.ts'));
  396. expect(watcher.getPendingFiles()).toEqual([]);
  397. watcher.stop();
  398. });
  399. it('should keep entries unchanged when sync fails (rescheduled work sees the same set)', async () => {
  400. // No initial-scan-triggered sync, so syncFn outcomes line up 1:1 with
  401. // explicit events.
  402. const syncFn = vi
  403. .fn()
  404. .mockRejectedValueOnce(new Error('boom')) // first sync rejects
  405. .mockResolvedValueOnce({ filesChanged: 1, durationMs: 10 }); // retry succeeds
  406. const onSyncError = vi.fn();
  407. const watcher = newWatcher(syncFn, { debounceMs: 100, onSyncError });
  408. watcher.start();
  409. await watcher.waitUntilReady();
  410. __emitWatchEventForTests(testDir, 'src/will-fail.ts');
  411. // Wait for the sync to reject.
  412. await waitFor(() => onSyncError.mock.calls.length > 0);
  413. // The file is STILL in pendingFiles — failure didn't drop it.
  414. const after = watcher.getPendingFiles();
  415. expect(after.some((p) => p.path === 'src/will-fail.ts')).toBe(true);
  416. // Retry resolves automatically; entry clears.
  417. await waitFor(
  418. () => !watcher.getPendingFiles().some((p) => p.path === 'src/will-fail.ts'),
  419. );
  420. watcher.stop();
  421. });
  422. it('should retain pending files and retry when syncFn throws LockUnavailableError (#449)', async () => {
  423. // CodeGraph.watch() converts the cross-process lock-failure no-op
  424. // into LockUnavailableError so the watcher's retry path picks it up
  425. // instead of falsely clearing pendingFiles. This test exercises the
  426. // contract directly.
  427. const syncFn = vi
  428. .fn()
  429. .mockRejectedValueOnce(new LockUnavailableError())
  430. .mockResolvedValueOnce({ filesChanged: 1, durationMs: 10 });
  431. const onSyncComplete = vi.fn();
  432. const onSyncError = vi.fn();
  433. const watcher = newWatcher(syncFn, {
  434. debounceMs: 100,
  435. onSyncComplete,
  436. onSyncError,
  437. });
  438. watcher.start();
  439. await watcher.waitUntilReady();
  440. __emitWatchEventForTests(testDir, 'src/locked.ts');
  441. await waitFor(() => syncFn.mock.calls.length >= 1);
  442. expect(watcher.getPendingFiles().some((p) => p.path === 'src/locked.ts')).toBe(true);
  443. // A held-lock no-op is not a sync failure — onSyncError stays quiet
  444. // so a long-running external indexer doesn't spam stderr every cycle.
  445. expect(onSyncError).not.toHaveBeenCalled();
  446. expect(onSyncComplete).not.toHaveBeenCalled();
  447. await waitFor(() => syncFn.mock.calls.length >= 2);
  448. await waitFor(
  449. () => !watcher.getPendingFiles().some((p) => p.path === 'src/locked.ts'),
  450. );
  451. expect(onSyncComplete).toHaveBeenCalledTimes(1);
  452. expect(onSyncComplete).toHaveBeenCalledWith({ filesChanged: 1, durationMs: 10 });
  453. expect(onSyncError).not.toHaveBeenCalled();
  454. watcher.stop();
  455. });
  456. });
  457. describe('callbacks', () => {
  458. it('should call onSyncComplete after successful sync', async () => {
  459. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 2, durationMs: 50 });
  460. const onSyncComplete = vi.fn();
  461. const watcher = newWatcher(syncFn, {
  462. debounceMs: 200,
  463. onSyncComplete,
  464. });
  465. watcher.start();
  466. await watcher.waitUntilReady();
  467. __emitWatchEventForTests(testDir, 'src/test.ts');
  468. await waitFor(() => onSyncComplete.mock.calls.length > 0);
  469. expect(onSyncComplete).toHaveBeenCalledWith({ filesChanged: 2, durationMs: 50 });
  470. watcher.stop();
  471. });
  472. it('should call onSyncError when sync throws', async () => {
  473. const syncFn = vi.fn().mockRejectedValue(new Error('sync failed'));
  474. const onSyncError = vi.fn();
  475. const watcher = newWatcher(syncFn, {
  476. debounceMs: 200,
  477. onSyncError,
  478. });
  479. watcher.start();
  480. await watcher.waitUntilReady();
  481. __emitWatchEventForTests(testDir, 'src/test.ts');
  482. await waitFor(() => onSyncError.mock.calls.length > 0);
  483. expect(onSyncError).toHaveBeenCalled();
  484. expect(onSyncError.mock.calls[0]![0]).toBeInstanceOf(Error);
  485. watcher.stop();
  486. });
  487. });
  488. describe('CodeGraph integration', () => {
  489. let cg: CodeGraph;
  490. afterEach(() => {
  491. if (cg) cg.close();
  492. });
  493. it('should watch and unwatch via CodeGraph API', async () => {
  494. cg = CodeGraph.initSync(testDir, {
  495. config: { include: ['**/*.ts'], exclude: [] },
  496. });
  497. await cg.indexAll();
  498. expect(cg.isWatching()).toBe(false);
  499. const started = cg.watch({ debounceMs: 200, inertForTests: true });
  500. expect(started).toBe(true);
  501. expect(cg.isWatching()).toBe(true);
  502. cg.unwatch();
  503. expect(cg.isWatching()).toBe(false);
  504. });
  505. it('should stop watching on close', async () => {
  506. cg = CodeGraph.initSync(testDir, {
  507. config: { include: ['**/*.ts'], exclude: [] },
  508. });
  509. await cg.indexAll();
  510. cg.watch({ debounceMs: 200, inertForTests: true });
  511. expect(cg.isWatching()).toBe(true);
  512. cg.close();
  513. // After close, isWatching should be false
  514. // (we can't call isWatching after close since DB is closed,
  515. // but we verify no errors are thrown)
  516. });
  517. it('should auto-sync when files change while watching (real fs.watch end-to-end)', async () => {
  518. // The one test that exercises the genuine native watcher: a real file
  519. // write must propagate through fs.watch → debounce → sync into the graph.
  520. cg = CodeGraph.initSync(testDir, {
  521. config: { include: ['**/*.ts'], exclude: [] },
  522. });
  523. await cg.indexAll();
  524. const initialStats = cg.getStats();
  525. const initialNodes = initialStats.nodeCount;
  526. cg.watch({ debounceMs: 300 });
  527. // Let the watcher install before writing, so the event isn't missed.
  528. await new Promise((r) => setTimeout(r, 100));
  529. // Real fs write — no synthetic event. The live watcher must catch it.
  530. fs.writeFileSync(
  531. path.join(testDir, 'src', 'added.ts'),
  532. 'export function added() { return 42; }'
  533. );
  534. // Wait for auto-sync to pick it up (real OS event delivery + debounce).
  535. await waitFor(() => {
  536. const stats = cg.getStats();
  537. return stats.nodeCount > initialNodes;
  538. }, 8000);
  539. // The new function should be in the graph.
  540. const results = cg.searchNodes('added');
  541. expect(results.length).toBeGreaterThan(0);
  542. cg.unwatch();
  543. });
  544. });
  545. });