watcher.test.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /**
  2. * FileWatcher Tests
  3. *
  4. * Tests for the file watcher that auto-syncs on changes.
  5. */
  6. import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
  7. import * as fs from 'fs';
  8. import * as path from 'path';
  9. import * as os from 'os';
  10. import { FileWatcher } from '../src/sync/watcher';
  11. import CodeGraph from '../src/index';
  12. /**
  13. * Helper to wait for a condition with timeout
  14. */
  15. function waitFor(
  16. condition: () => boolean,
  17. timeoutMs = 10000,
  18. intervalMs = 100
  19. ): Promise<void> {
  20. return new Promise((resolve, reject) => {
  21. const start = Date.now();
  22. const check = () => {
  23. if (condition()) return resolve();
  24. if (Date.now() - start > timeoutMs) return reject(new Error('waitFor timed out'));
  25. setTimeout(check, intervalMs);
  26. };
  27. check();
  28. });
  29. }
  30. describe('FileWatcher', () => {
  31. let testDir: string;
  32. beforeEach(() => {
  33. testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-watcher-'));
  34. // Create a source file so the directory isn't empty
  35. const srcDir = path.join(testDir, 'src');
  36. fs.mkdirSync(srcDir);
  37. fs.writeFileSync(path.join(srcDir, 'index.ts'), 'export const x = 1;');
  38. });
  39. afterEach(() => {
  40. if (fs.existsSync(testDir)) {
  41. fs.rmSync(testDir, { recursive: true, force: true });
  42. }
  43. });
  44. describe('start/stop lifecycle', () => {
  45. it('should start and stop without errors', () => {
  46. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  47. const watcher = new FileWatcher(testDir, syncFn);
  48. const started = watcher.start();
  49. expect(started).toBe(true);
  50. expect(watcher.isActive()).toBe(true);
  51. watcher.stop();
  52. expect(watcher.isActive()).toBe(false);
  53. });
  54. it('should be idempotent on double start', () => {
  55. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  56. const watcher = new FileWatcher(testDir, syncFn);
  57. expect(watcher.start()).toBe(true);
  58. expect(watcher.start()).toBe(true); // Should not throw
  59. expect(watcher.isActive()).toBe(true);
  60. watcher.stop();
  61. });
  62. it('should be idempotent on double stop', () => {
  63. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  64. const watcher = new FileWatcher(testDir, syncFn);
  65. watcher.start();
  66. watcher.stop();
  67. watcher.stop(); // Should not throw
  68. expect(watcher.isActive()).toBe(false);
  69. });
  70. });
  71. describe('debounced sync', () => {
  72. it('should trigger sync after file change', async () => {
  73. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 1, durationMs: 10 });
  74. const watcher = new FileWatcher(testDir, syncFn, { debounceMs: 200 });
  75. watcher.start();
  76. // Create a new file
  77. fs.writeFileSync(path.join(testDir, 'src', 'new.ts'), 'export const y = 2;');
  78. // Wait for debounced sync to fire
  79. await waitFor(() => syncFn.mock.calls.length > 0, 5000);
  80. expect(syncFn).toHaveBeenCalled();
  81. watcher.stop();
  82. });
  83. it('should debounce rapid changes into a single sync', async () => {
  84. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 1, durationMs: 10 });
  85. const watcher = new FileWatcher(testDir, syncFn, { debounceMs: 500 });
  86. watcher.start();
  87. // Rapid-fire changes
  88. for (let i = 0; i < 5; i++) {
  89. fs.writeFileSync(
  90. path.join(testDir, 'src', `file${i}.ts`),
  91. `export const v${i} = ${i};`
  92. );
  93. await new Promise((r) => setTimeout(r, 50));
  94. }
  95. // Wait for the single debounced sync
  96. await waitFor(() => syncFn.mock.calls.length > 0, 5000);
  97. // Should have been called once (debounced), not 5 times
  98. expect(syncFn.mock.calls.length).toBe(1);
  99. watcher.stop();
  100. });
  101. });
  102. describe('filtering', () => {
  103. it('should ignore files not matching include patterns', async () => {
  104. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  105. const watcher = new FileWatcher(testDir, syncFn, { debounceMs: 200 });
  106. watcher.start();
  107. // Let watcher settle — fs.watch may fire residual events from beforeEach
  108. await new Promise((r) => setTimeout(r, 400));
  109. syncFn.mockClear();
  110. // Create a file that doesn't match include patterns
  111. fs.writeFileSync(path.join(testDir, 'src', 'readme.md'), '# Hello');
  112. // Wait a bit longer than debounce — sync should NOT trigger
  113. await new Promise((r) => setTimeout(r, 500));
  114. expect(syncFn).not.toHaveBeenCalled();
  115. watcher.stop();
  116. });
  117. it('should ignore .codegraph directory changes', async () => {
  118. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  119. const watcher = new FileWatcher(testDir, syncFn, { debounceMs: 200 });
  120. watcher.start();
  121. // Let watcher settle — fs.watch may fire residual events from beforeEach
  122. await new Promise((r) => setTimeout(r, 400));
  123. syncFn.mockClear();
  124. // Simulate a .codegraph directory change
  125. const cgDir = path.join(testDir, '.codegraph');
  126. fs.mkdirSync(cgDir, { recursive: true });
  127. fs.writeFileSync(path.join(cgDir, 'db.sqlite'), 'fake');
  128. // Wait — sync should NOT trigger
  129. await new Promise((r) => setTimeout(r, 500));
  130. expect(syncFn).not.toHaveBeenCalled();
  131. watcher.stop();
  132. });
  133. it('should not watch node_modules even without a .gitignore (#276/#417)', async () => {
  134. // No .gitignore in testDir — exclusion relies on the built-in
  135. // default-ignore set the indexer uses (buildDefaultIgnore), which a
  136. // .gitignore-only filter would miss.
  137. fs.mkdirSync(path.join(testDir, 'node_modules', 'dep', 'lib'), { recursive: true });
  138. fs.writeFileSync(path.join(testDir, 'node_modules', 'dep', 'index.ts'), 'export const dep = 1;');
  139. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  140. const watcher = new FileWatcher(testDir, syncFn, { debounceMs: 200 });
  141. watcher.start();
  142. // Let the watcher settle past any residual crawl events.
  143. await new Promise((r) => setTimeout(r, 400));
  144. syncFn.mockClear();
  145. // A source-extension edit INSIDE node_modules must NOT trigger a sync —
  146. // the directory was never watched.
  147. fs.writeFileSync(path.join(testDir, 'node_modules', 'dep', 'lib', 'extra.ts'), 'export const e = 2;');
  148. await new Promise((r) => setTimeout(r, 600));
  149. expect(syncFn).not.toHaveBeenCalled();
  150. // Positive control: a real source edit still triggers sync, proving the
  151. // watcher is live (not merely inert).
  152. fs.writeFileSync(path.join(testDir, 'src', 'live.ts'), 'export const live = 3;');
  153. await waitFor(() => syncFn.mock.calls.length > 0, 5000);
  154. expect(syncFn).toHaveBeenCalled();
  155. watcher.stop();
  156. });
  157. });
  158. describe('pending file tracking (#403)', () => {
  159. it('should expose edited paths via getPendingFiles before sync fires', async () => {
  160. // Slow debounce — events arrive but sync hasn't run yet.
  161. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 1, durationMs: 10 });
  162. const watcher = new FileWatcher(testDir, syncFn, { debounceMs: 2000 });
  163. watcher.start();
  164. // Deterministic boundary: wait for chokidar's initial scan to complete
  165. // so any late initial-scan events have fired before we assert. A bare
  166. // sleep is flaky under test-parallelism load.
  167. await watcher.waitUntilReady();
  168. expect(watcher.getPendingFiles()).toEqual([]);
  169. fs.writeFileSync(path.join(testDir, 'src', 'pending.ts'), 'export const p = 1;');
  170. // Allow chokidar to emit, but DON'T let the 2s debounce fire.
  171. await waitFor(() => watcher.getPendingFiles().length > 0, 3000);
  172. const pending = watcher.getPendingFiles();
  173. const paths = pending.map((p) => p.path);
  174. expect(paths).toContain('src/pending.ts');
  175. const entry = pending.find((p) => p.path === 'src/pending.ts')!;
  176. expect(entry.firstSeenMs).toBeGreaterThan(0);
  177. expect(entry.lastSeenMs).toBeGreaterThanOrEqual(entry.firstSeenMs);
  178. // No sync running yet → indexing flag is false.
  179. expect(entry.indexing).toBe(false);
  180. watcher.stop();
  181. });
  182. it('should clear an entry only after a successful sync absorbing that edit', async () => {
  183. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 1, durationMs: 10 });
  184. const watcher = new FileWatcher(testDir, syncFn, { debounceMs: 200 });
  185. watcher.start();
  186. await watcher.waitUntilReady();
  187. fs.writeFileSync(path.join(testDir, 'src', 'fresh.ts'), 'export const f = 1;');
  188. // Watcher saw the change → pendingFiles has the entry. Longer windows
  189. // here because chokidar event delivery on macOS slows under heavy
  190. // parallel test-suite load (4× slower than isolation).
  191. await waitFor(() => watcher.getPendingFiles().some((p) => p.path === 'src/fresh.ts'), 8000);
  192. // Wait through debounce + sync; the entry should drop out.
  193. await waitFor(() => syncFn.mock.calls.length > 0, 8000);
  194. await waitFor(() => !watcher.getPendingFiles().some((p) => p.path === 'src/fresh.ts'), 8000);
  195. expect(watcher.getPendingFiles()).toEqual([]);
  196. watcher.stop();
  197. });
  198. it('should keep entries unchanged when sync fails (rescheduled work sees the same set)', async () => {
  199. // First post-settle sync rejects, second resolves. The initial-scan
  200. // sync (triggered by chokidar's pre-existing add events) is allowed to
  201. // resolve cleanly so it doesn't consume one of our scripted outcomes.
  202. const syncFn = vi
  203. .fn()
  204. .mockResolvedValueOnce({ filesChanged: 0, durationMs: 1 }) // initial scan
  205. .mockRejectedValueOnce(new Error('boom')) // first real edit fails
  206. .mockResolvedValueOnce({ filesChanged: 1, durationMs: 10 }); // retry succeeds
  207. const onSyncError = vi.fn();
  208. const watcher = new FileWatcher(testDir, syncFn, { debounceMs: 200, onSyncError });
  209. watcher.start();
  210. // Wait through chokidar `ready` AND the initial-scan-triggered sync, so
  211. // the next sync corresponds to the explicit edit below.
  212. await watcher.waitUntilReady();
  213. await waitFor(() => syncFn.mock.calls.length >= 1, 5000);
  214. await new Promise((r) => setTimeout(r, 100));
  215. fs.writeFileSync(path.join(testDir, 'src', 'will-fail.ts'), 'export const wf = 1;');
  216. // Wait for the sync that handles the explicit edit to reject.
  217. await waitFor(() => onSyncError.mock.calls.length > 0, 5000);
  218. // The file is STILL in pendingFiles — failure didn't drop it.
  219. const after = watcher.getPendingFiles();
  220. expect(after.some((p) => p.path === 'src/will-fail.ts')).toBe(true);
  221. // Retry resolves; entry clears.
  222. await waitFor(
  223. () => !watcher.getPendingFiles().some((p) => p.path === 'src/will-fail.ts'),
  224. 5000,
  225. );
  226. watcher.stop();
  227. });
  228. });
  229. describe('callbacks', () => {
  230. it('should call onSyncComplete after successful sync', async () => {
  231. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 2, durationMs: 50 });
  232. const onSyncComplete = vi.fn();
  233. const watcher = new FileWatcher(testDir, syncFn, {
  234. debounceMs: 200,
  235. onSyncComplete,
  236. });
  237. watcher.start();
  238. fs.writeFileSync(path.join(testDir, 'src', 'test.ts'), 'export const z = 3;');
  239. await waitFor(() => onSyncComplete.mock.calls.length > 0, 5000);
  240. expect(onSyncComplete).toHaveBeenCalledWith({ filesChanged: 2, durationMs: 50 });
  241. watcher.stop();
  242. });
  243. it('should call onSyncError when sync throws', async () => {
  244. const syncFn = vi.fn().mockRejectedValue(new Error('sync failed'));
  245. const onSyncError = vi.fn();
  246. const watcher = new FileWatcher(testDir, syncFn, {
  247. debounceMs: 200,
  248. onSyncError,
  249. });
  250. watcher.start();
  251. fs.writeFileSync(path.join(testDir, 'src', 'test.ts'), 'export const z = 3;');
  252. await waitFor(() => onSyncError.mock.calls.length > 0, 5000);
  253. expect(onSyncError).toHaveBeenCalled();
  254. expect(onSyncError.mock.calls[0]![0]).toBeInstanceOf(Error);
  255. watcher.stop();
  256. });
  257. });
  258. describe('CodeGraph integration', () => {
  259. let cg: CodeGraph;
  260. afterEach(() => {
  261. if (cg) cg.close();
  262. });
  263. it('should watch and unwatch via CodeGraph API', async () => {
  264. cg = CodeGraph.initSync(testDir, {
  265. config: { include: ['**/*.ts'], exclude: [] },
  266. });
  267. await cg.indexAll();
  268. expect(cg.isWatching()).toBe(false);
  269. const started = cg.watch({ debounceMs: 200 });
  270. expect(started).toBe(true);
  271. expect(cg.isWatching()).toBe(true);
  272. cg.unwatch();
  273. expect(cg.isWatching()).toBe(false);
  274. });
  275. it('should stop watching on close', async () => {
  276. cg = CodeGraph.initSync(testDir, {
  277. config: { include: ['**/*.ts'], exclude: [] },
  278. });
  279. await cg.indexAll();
  280. cg.watch({ debounceMs: 200 });
  281. expect(cg.isWatching()).toBe(true);
  282. cg.close();
  283. // After close, isWatching should be false
  284. // (we can't call isWatching after close since DB is closed,
  285. // but we verify no errors are thrown)
  286. });
  287. it('should auto-sync when files change while watching', async () => {
  288. cg = CodeGraph.initSync(testDir, {
  289. config: { include: ['**/*.ts'], exclude: [] },
  290. });
  291. await cg.indexAll();
  292. const initialStats = cg.getStats();
  293. const initialNodes = initialStats.nodeCount;
  294. cg.watch({ debounceMs: 300 });
  295. // Add a new file with a function
  296. fs.writeFileSync(
  297. path.join(testDir, 'src', 'added.ts'),
  298. 'export function added() { return 42; }'
  299. );
  300. // Wait for auto-sync to pick it up
  301. await waitFor(() => {
  302. const stats = cg.getStats();
  303. return stats.nodeCount > initialNodes;
  304. }, 10000);
  305. // The new function should be in the graph
  306. const results = cg.searchNodes('added');
  307. expect(results.length).toBeGreaterThan(0);
  308. cg.unwatch();
  309. });
  310. });
  311. });