watcher.test.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 * as fs from 'fs';
  21. import * as path from 'path';
  22. import * as os from 'os';
  23. import {
  24. FileWatcher,
  25. LockUnavailableError,
  26. __emitWatchEventForTests,
  27. type WatchOptions,
  28. } from '../src/sync/watcher';
  29. import CodeGraph from '../src/index';
  30. type SyncFn = () => Promise<{ filesChanged: number; durationMs: number }>;
  31. /**
  32. * Helper to wait for a condition with timeout. Used for assertions that depend
  33. * on the debounce timer (real setTimeout) firing, or on the real watcher's
  34. * event delivery in the end-to-end test.
  35. */
  36. function waitFor(
  37. condition: () => boolean,
  38. timeoutMs = 2000,
  39. intervalMs = 25
  40. ): Promise<void> {
  41. return new Promise((resolve, reject) => {
  42. const start = Date.now();
  43. const check = () => {
  44. if (condition()) return resolve();
  45. if (Date.now() - start > timeoutMs) return reject(new Error('waitFor timed out'));
  46. setTimeout(check, intervalMs);
  47. };
  48. check();
  49. });
  50. }
  51. describe('FileWatcher', () => {
  52. let testDir: string;
  53. // Inert by default — unit tests drive events via __emitWatchEventForTests
  54. // and never depend on real OS watch delivery.
  55. const newWatcher = (syncFn: SyncFn, opts: WatchOptions = {}) =>
  56. new FileWatcher(testDir, syncFn, { inertForTests: true, ...opts });
  57. beforeEach(() => {
  58. testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-watcher-'));
  59. // Create a source file so the directory isn't empty
  60. const srcDir = path.join(testDir, 'src');
  61. fs.mkdirSync(srcDir);
  62. fs.writeFileSync(path.join(srcDir, 'index.ts'), 'export const x = 1;');
  63. });
  64. afterEach(() => {
  65. if (fs.existsSync(testDir)) {
  66. fs.rmSync(testDir, { recursive: true, force: true });
  67. }
  68. });
  69. describe('start/stop lifecycle', () => {
  70. it('should start and stop without errors', () => {
  71. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  72. const watcher = newWatcher(syncFn);
  73. const started = watcher.start();
  74. expect(started).toBe(true);
  75. expect(watcher.isActive()).toBe(true);
  76. watcher.stop();
  77. expect(watcher.isActive()).toBe(false);
  78. });
  79. it('should be idempotent on double start', () => {
  80. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  81. const watcher = newWatcher(syncFn);
  82. expect(watcher.start()).toBe(true);
  83. expect(watcher.start()).toBe(true); // Should not throw
  84. expect(watcher.isActive()).toBe(true);
  85. watcher.stop();
  86. });
  87. it('should be idempotent on double stop', () => {
  88. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  89. const watcher = newWatcher(syncFn);
  90. watcher.start();
  91. watcher.stop();
  92. watcher.stop(); // Should not throw
  93. expect(watcher.isActive()).toBe(false);
  94. });
  95. });
  96. describe('debounced sync', () => {
  97. it('should trigger sync after file change', async () => {
  98. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 1, durationMs: 10 });
  99. const watcher = newWatcher(syncFn, { debounceMs: 200 });
  100. watcher.start();
  101. await watcher.waitUntilReady();
  102. __emitWatchEventForTests(testDir, 'src/new.ts');
  103. // Wait for debounced sync to fire (real timer; 200ms + epsilon).
  104. await waitFor(() => syncFn.mock.calls.length > 0);
  105. expect(syncFn).toHaveBeenCalled();
  106. watcher.stop();
  107. });
  108. it('should debounce rapid changes into a single sync', async () => {
  109. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 1, durationMs: 10 });
  110. const watcher = newWatcher(syncFn, { debounceMs: 400 });
  111. watcher.start();
  112. await watcher.waitUntilReady();
  113. // Rapid-fire synthesized changes — each call resets the debounce timer.
  114. // Spacing them tighter than the debounce window proves the debounce
  115. // collapses them into one syncFn call.
  116. for (let i = 0; i < 5; i++) {
  117. __emitWatchEventForTests(testDir, `src/file${i}.ts`);
  118. await new Promise((r) => setTimeout(r, 50));
  119. }
  120. // Wait for the single debounced sync.
  121. await waitFor(() => syncFn.mock.calls.length > 0);
  122. // Should have been called once (debounced), not 5 times.
  123. expect(syncFn.mock.calls.length).toBe(1);
  124. watcher.stop();
  125. });
  126. });
  127. describe('filtering', () => {
  128. it('should ignore files not matching include patterns', async () => {
  129. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  130. const watcher = newWatcher(syncFn, { debounceMs: 200 });
  131. watcher.start();
  132. await watcher.waitUntilReady();
  133. // A non-source-file event — FileWatcher's `isSourceFile` gate must drop
  134. // it before scheduling sync.
  135. __emitWatchEventForTests(testDir, 'src/readme.md');
  136. // Wait a bit longer than debounce — sync should NOT trigger.
  137. await new Promise((r) => setTimeout(r, 400));
  138. expect(syncFn).not.toHaveBeenCalled();
  139. watcher.stop();
  140. });
  141. it('should ignore .codegraph directory changes', async () => {
  142. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  143. const watcher = newWatcher(syncFn, { debounceMs: 200 });
  144. watcher.start();
  145. await watcher.waitUntilReady();
  146. // A .codegraph event — FileWatcher's `isAlwaysIgnored` filter must drop
  147. // it before scheduling sync.
  148. __emitWatchEventForTests(testDir, '.codegraph/db.sqlite');
  149. await new Promise((r) => setTimeout(r, 400));
  150. expect(syncFn).not.toHaveBeenCalled();
  151. watcher.stop();
  152. });
  153. it('should drop ignored/non-source paths but sync real source edits', async () => {
  154. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  155. const watcher = newWatcher(syncFn, { debounceMs: 200 });
  156. watcher.start();
  157. await watcher.waitUntilReady();
  158. // node_modules is in the default-ignore set (#407) → dropped by the
  159. // ignore matcher even without a .gitignore.
  160. __emitWatchEventForTests(testDir, 'node_modules/dep/index.js');
  161. // A normal source file still schedules sync (positive control).
  162. __emitWatchEventForTests(testDir, 'src/live.ts');
  163. await waitFor(() => syncFn.mock.calls.length > 0);
  164. expect(syncFn).toHaveBeenCalled();
  165. watcher.stop();
  166. });
  167. });
  168. describe('pending file tracking (#403)', () => {
  169. it('should expose edited paths via getPendingFiles before sync fires', async () => {
  170. // Slow debounce — pending entries are visible until the debounce fires.
  171. // The synthetic event is synchronous, so we can assert immediately.
  172. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 1, durationMs: 10 });
  173. const watcher = newWatcher(syncFn, { debounceMs: 2000 });
  174. watcher.start();
  175. await watcher.waitUntilReady();
  176. expect(watcher.getPendingFiles()).toEqual([]);
  177. __emitWatchEventForTests(testDir, 'src/pending.ts');
  178. const pending = watcher.getPendingFiles();
  179. const paths = pending.map((p) => p.path);
  180. expect(paths).toContain('src/pending.ts');
  181. const entry = pending.find((p) => p.path === 'src/pending.ts')!;
  182. expect(entry.firstSeenMs).toBeGreaterThan(0);
  183. expect(entry.lastSeenMs).toBeGreaterThanOrEqual(entry.firstSeenMs);
  184. // No sync running yet → indexing flag is false.
  185. expect(entry.indexing).toBe(false);
  186. watcher.stop();
  187. });
  188. it('should clear an entry only after a successful sync absorbing that edit', async () => {
  189. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 1, durationMs: 10 });
  190. const watcher = newWatcher(syncFn, { debounceMs: 200 });
  191. watcher.start();
  192. await watcher.waitUntilReady();
  193. __emitWatchEventForTests(testDir, 'src/fresh.ts');
  194. // Watcher saw the change → pendingFiles has the entry IMMEDIATELY.
  195. expect(watcher.getPendingFiles().some((p) => p.path === 'src/fresh.ts')).toBe(true);
  196. // Wait through debounce + sync; the entry should drop out.
  197. await waitFor(() => syncFn.mock.calls.length > 0);
  198. await waitFor(() => !watcher.getPendingFiles().some((p) => p.path === 'src/fresh.ts'));
  199. expect(watcher.getPendingFiles()).toEqual([]);
  200. watcher.stop();
  201. });
  202. it('should keep entries unchanged when sync fails (rescheduled work sees the same set)', async () => {
  203. // No initial-scan-triggered sync, so syncFn outcomes line up 1:1 with
  204. // explicit events.
  205. const syncFn = vi
  206. .fn()
  207. .mockRejectedValueOnce(new Error('boom')) // first sync rejects
  208. .mockResolvedValueOnce({ filesChanged: 1, durationMs: 10 }); // retry succeeds
  209. const onSyncError = vi.fn();
  210. const watcher = newWatcher(syncFn, { debounceMs: 100, onSyncError });
  211. watcher.start();
  212. await watcher.waitUntilReady();
  213. __emitWatchEventForTests(testDir, 'src/will-fail.ts');
  214. // Wait for the sync to reject.
  215. await waitFor(() => onSyncError.mock.calls.length > 0);
  216. // The file is STILL in pendingFiles — failure didn't drop it.
  217. const after = watcher.getPendingFiles();
  218. expect(after.some((p) => p.path === 'src/will-fail.ts')).toBe(true);
  219. // Retry resolves automatically; entry clears.
  220. await waitFor(
  221. () => !watcher.getPendingFiles().some((p) => p.path === 'src/will-fail.ts'),
  222. );
  223. watcher.stop();
  224. });
  225. it('should retain pending files and retry when syncFn throws LockUnavailableError (#449)', async () => {
  226. // CodeGraph.watch() converts the cross-process lock-failure no-op
  227. // into LockUnavailableError so the watcher's retry path picks it up
  228. // instead of falsely clearing pendingFiles. This test exercises the
  229. // contract directly.
  230. const syncFn = vi
  231. .fn()
  232. .mockRejectedValueOnce(new LockUnavailableError())
  233. .mockResolvedValueOnce({ filesChanged: 1, durationMs: 10 });
  234. const onSyncComplete = vi.fn();
  235. const onSyncError = vi.fn();
  236. const watcher = newWatcher(syncFn, {
  237. debounceMs: 100,
  238. onSyncComplete,
  239. onSyncError,
  240. });
  241. watcher.start();
  242. await watcher.waitUntilReady();
  243. __emitWatchEventForTests(testDir, 'src/locked.ts');
  244. await waitFor(() => syncFn.mock.calls.length >= 1);
  245. expect(watcher.getPendingFiles().some((p) => p.path === 'src/locked.ts')).toBe(true);
  246. // A held-lock no-op is not a sync failure — onSyncError stays quiet
  247. // so a long-running external indexer doesn't spam stderr every cycle.
  248. expect(onSyncError).not.toHaveBeenCalled();
  249. expect(onSyncComplete).not.toHaveBeenCalled();
  250. await waitFor(() => syncFn.mock.calls.length >= 2);
  251. await waitFor(
  252. () => !watcher.getPendingFiles().some((p) => p.path === 'src/locked.ts'),
  253. );
  254. expect(onSyncComplete).toHaveBeenCalledTimes(1);
  255. expect(onSyncComplete).toHaveBeenCalledWith({ filesChanged: 1, durationMs: 10 });
  256. expect(onSyncError).not.toHaveBeenCalled();
  257. watcher.stop();
  258. });
  259. });
  260. describe('callbacks', () => {
  261. it('should call onSyncComplete after successful sync', async () => {
  262. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 2, durationMs: 50 });
  263. const onSyncComplete = vi.fn();
  264. const watcher = newWatcher(syncFn, {
  265. debounceMs: 200,
  266. onSyncComplete,
  267. });
  268. watcher.start();
  269. await watcher.waitUntilReady();
  270. __emitWatchEventForTests(testDir, 'src/test.ts');
  271. await waitFor(() => onSyncComplete.mock.calls.length > 0);
  272. expect(onSyncComplete).toHaveBeenCalledWith({ filesChanged: 2, durationMs: 50 });
  273. watcher.stop();
  274. });
  275. it('should call onSyncError when sync throws', async () => {
  276. const syncFn = vi.fn().mockRejectedValue(new Error('sync failed'));
  277. const onSyncError = vi.fn();
  278. const watcher = newWatcher(syncFn, {
  279. debounceMs: 200,
  280. onSyncError,
  281. });
  282. watcher.start();
  283. await watcher.waitUntilReady();
  284. __emitWatchEventForTests(testDir, 'src/test.ts');
  285. await waitFor(() => onSyncError.mock.calls.length > 0);
  286. expect(onSyncError).toHaveBeenCalled();
  287. expect(onSyncError.mock.calls[0]![0]).toBeInstanceOf(Error);
  288. watcher.stop();
  289. });
  290. });
  291. describe('CodeGraph integration', () => {
  292. let cg: CodeGraph;
  293. afterEach(() => {
  294. if (cg) cg.close();
  295. });
  296. it('should watch and unwatch via CodeGraph API', async () => {
  297. cg = CodeGraph.initSync(testDir, {
  298. config: { include: ['**/*.ts'], exclude: [] },
  299. });
  300. await cg.indexAll();
  301. expect(cg.isWatching()).toBe(false);
  302. const started = cg.watch({ debounceMs: 200, inertForTests: true });
  303. expect(started).toBe(true);
  304. expect(cg.isWatching()).toBe(true);
  305. cg.unwatch();
  306. expect(cg.isWatching()).toBe(false);
  307. });
  308. it('should stop watching on close', async () => {
  309. cg = CodeGraph.initSync(testDir, {
  310. config: { include: ['**/*.ts'], exclude: [] },
  311. });
  312. await cg.indexAll();
  313. cg.watch({ debounceMs: 200, inertForTests: true });
  314. expect(cg.isWatching()).toBe(true);
  315. cg.close();
  316. // After close, isWatching should be false
  317. // (we can't call isWatching after close since DB is closed,
  318. // but we verify no errors are thrown)
  319. });
  320. it('should auto-sync when files change while watching (real fs.watch end-to-end)', async () => {
  321. // The one test that exercises the genuine native watcher: a real file
  322. // write must propagate through fs.watch → debounce → sync into the graph.
  323. cg = CodeGraph.initSync(testDir, {
  324. config: { include: ['**/*.ts'], exclude: [] },
  325. });
  326. await cg.indexAll();
  327. const initialStats = cg.getStats();
  328. const initialNodes = initialStats.nodeCount;
  329. cg.watch({ debounceMs: 300 });
  330. // Let the watcher install before writing, so the event isn't missed.
  331. await new Promise((r) => setTimeout(r, 100));
  332. // Real fs write — no synthetic event. The live watcher must catch it.
  333. fs.writeFileSync(
  334. path.join(testDir, 'src', 'added.ts'),
  335. 'export function added() { return 42; }'
  336. );
  337. // Wait for auto-sync to pick it up (real OS event delivery + debounce).
  338. await waitFor(() => {
  339. const stats = cg.getStats();
  340. return stats.nodeCount > initialNodes;
  341. }, 8000);
  342. // The new function should be in the graph.
  343. const results = cg.searchNodes('added');
  344. expect(results.length).toBeGreaterThan(0);
  345. cg.unwatch();
  346. });
  347. });
  348. });