watcher.test.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 type { CodeGraphConfig } from '../src/types';
  12. import CodeGraph from '../src/index';
  13. /**
  14. * Helper to wait for a condition with timeout
  15. */
  16. function waitFor(
  17. condition: () => boolean,
  18. timeoutMs = 10000,
  19. intervalMs = 100
  20. ): Promise<void> {
  21. return new Promise((resolve, reject) => {
  22. const start = Date.now();
  23. const check = () => {
  24. if (condition()) return resolve();
  25. if (Date.now() - start > timeoutMs) return reject(new Error('waitFor timed out'));
  26. setTimeout(check, intervalMs);
  27. };
  28. check();
  29. });
  30. }
  31. describe('FileWatcher', () => {
  32. let testDir: string;
  33. const baseConfig: CodeGraphConfig = {
  34. version: 1,
  35. rootDir: '.',
  36. include: ['**/*.ts', '**/*.js'],
  37. exclude: ['**/node_modules/**', '**/dist/**'],
  38. languages: [],
  39. frameworks: [],
  40. maxFileSize: 1024 * 1024,
  41. extractDocstrings: true,
  42. trackCallSites: true,
  43. };
  44. beforeEach(() => {
  45. testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-watcher-'));
  46. // Create a source file so the directory isn't empty
  47. const srcDir = path.join(testDir, 'src');
  48. fs.mkdirSync(srcDir);
  49. fs.writeFileSync(path.join(srcDir, 'index.ts'), 'export const x = 1;');
  50. });
  51. afterEach(() => {
  52. if (fs.existsSync(testDir)) {
  53. fs.rmSync(testDir, { recursive: true, force: true });
  54. }
  55. });
  56. describe('start/stop lifecycle', () => {
  57. it('should start and stop without errors', () => {
  58. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  59. const watcher = new FileWatcher(testDir, baseConfig, syncFn);
  60. const started = watcher.start();
  61. expect(started).toBe(true);
  62. expect(watcher.isActive()).toBe(true);
  63. watcher.stop();
  64. expect(watcher.isActive()).toBe(false);
  65. });
  66. it('should be idempotent on double start', () => {
  67. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  68. const watcher = new FileWatcher(testDir, baseConfig, syncFn);
  69. expect(watcher.start()).toBe(true);
  70. expect(watcher.start()).toBe(true); // Should not throw
  71. expect(watcher.isActive()).toBe(true);
  72. watcher.stop();
  73. });
  74. it('should be idempotent on double stop', () => {
  75. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  76. const watcher = new FileWatcher(testDir, baseConfig, syncFn);
  77. watcher.start();
  78. watcher.stop();
  79. watcher.stop(); // Should not throw
  80. expect(watcher.isActive()).toBe(false);
  81. });
  82. });
  83. describe('debounced sync', () => {
  84. it('should trigger sync after file change', async () => {
  85. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 1, durationMs: 10 });
  86. const watcher = new FileWatcher(testDir, baseConfig, syncFn, { debounceMs: 200 });
  87. watcher.start();
  88. // Create a new file
  89. fs.writeFileSync(path.join(testDir, 'src', 'new.ts'), 'export const y = 2;');
  90. // Wait for debounced sync to fire
  91. await waitFor(() => syncFn.mock.calls.length > 0, 5000);
  92. expect(syncFn).toHaveBeenCalled();
  93. watcher.stop();
  94. });
  95. it('should debounce rapid changes into a single sync', async () => {
  96. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 1, durationMs: 10 });
  97. const watcher = new FileWatcher(testDir, baseConfig, syncFn, { debounceMs: 500 });
  98. watcher.start();
  99. // Rapid-fire changes
  100. for (let i = 0; i < 5; i++) {
  101. fs.writeFileSync(
  102. path.join(testDir, 'src', `file${i}.ts`),
  103. `export const v${i} = ${i};`
  104. );
  105. await new Promise((r) => setTimeout(r, 50));
  106. }
  107. // Wait for the single debounced sync
  108. await waitFor(() => syncFn.mock.calls.length > 0, 5000);
  109. // Should have been called once (debounced), not 5 times
  110. expect(syncFn.mock.calls.length).toBe(1);
  111. watcher.stop();
  112. });
  113. });
  114. describe('filtering', () => {
  115. it('should ignore files not matching include patterns', async () => {
  116. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  117. const watcher = new FileWatcher(testDir, baseConfig, syncFn, { debounceMs: 200 });
  118. watcher.start();
  119. // Let watcher settle — fs.watch may fire residual events from beforeEach
  120. await new Promise((r) => setTimeout(r, 400));
  121. syncFn.mockClear();
  122. // Create a file that doesn't match include patterns
  123. fs.writeFileSync(path.join(testDir, 'src', 'readme.md'), '# Hello');
  124. // Wait a bit longer than debounce — sync should NOT trigger
  125. await new Promise((r) => setTimeout(r, 500));
  126. expect(syncFn).not.toHaveBeenCalled();
  127. watcher.stop();
  128. });
  129. it('should ignore .codegraph directory changes', async () => {
  130. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 0, durationMs: 0 });
  131. const watcher = new FileWatcher(testDir, baseConfig, syncFn, { debounceMs: 200 });
  132. watcher.start();
  133. // Let watcher settle — fs.watch may fire residual events from beforeEach
  134. await new Promise((r) => setTimeout(r, 400));
  135. syncFn.mockClear();
  136. // Simulate a .codegraph directory change
  137. const cgDir = path.join(testDir, '.codegraph');
  138. fs.mkdirSync(cgDir, { recursive: true });
  139. fs.writeFileSync(path.join(cgDir, 'db.sqlite'), 'fake');
  140. // Wait — sync should NOT trigger
  141. await new Promise((r) => setTimeout(r, 500));
  142. expect(syncFn).not.toHaveBeenCalled();
  143. watcher.stop();
  144. });
  145. });
  146. describe('callbacks', () => {
  147. it('should call onSyncComplete after successful sync', async () => {
  148. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 2, durationMs: 50 });
  149. const onSyncComplete = vi.fn();
  150. const watcher = new FileWatcher(testDir, baseConfig, syncFn, {
  151. debounceMs: 200,
  152. onSyncComplete,
  153. });
  154. watcher.start();
  155. fs.writeFileSync(path.join(testDir, 'src', 'test.ts'), 'export const z = 3;');
  156. await waitFor(() => onSyncComplete.mock.calls.length > 0, 5000);
  157. expect(onSyncComplete).toHaveBeenCalledWith({ filesChanged: 2, durationMs: 50 });
  158. watcher.stop();
  159. });
  160. it('should call onSyncError when sync throws', async () => {
  161. const syncFn = vi.fn().mockRejectedValue(new Error('sync failed'));
  162. const onSyncError = vi.fn();
  163. const watcher = new FileWatcher(testDir, baseConfig, syncFn, {
  164. debounceMs: 200,
  165. onSyncError,
  166. });
  167. watcher.start();
  168. fs.writeFileSync(path.join(testDir, 'src', 'test.ts'), 'export const z = 3;');
  169. await waitFor(() => onSyncError.mock.calls.length > 0, 5000);
  170. expect(onSyncError).toHaveBeenCalled();
  171. expect(onSyncError.mock.calls[0]![0]).toBeInstanceOf(Error);
  172. watcher.stop();
  173. });
  174. });
  175. describe('CodeGraph integration', () => {
  176. let cg: CodeGraph;
  177. afterEach(() => {
  178. if (cg) cg.close();
  179. });
  180. it('should watch and unwatch via CodeGraph API', async () => {
  181. cg = CodeGraph.initSync(testDir, {
  182. config: { include: ['**/*.ts'], exclude: [] },
  183. });
  184. await cg.indexAll();
  185. expect(cg.isWatching()).toBe(false);
  186. const started = cg.watch({ debounceMs: 200 });
  187. expect(started).toBe(true);
  188. expect(cg.isWatching()).toBe(true);
  189. cg.unwatch();
  190. expect(cg.isWatching()).toBe(false);
  191. });
  192. it('should stop watching on close', async () => {
  193. cg = CodeGraph.initSync(testDir, {
  194. config: { include: ['**/*.ts'], exclude: [] },
  195. });
  196. await cg.indexAll();
  197. cg.watch({ debounceMs: 200 });
  198. expect(cg.isWatching()).toBe(true);
  199. cg.close();
  200. // After close, isWatching should be false
  201. // (we can't call isWatching after close since DB is closed,
  202. // but we verify no errors are thrown)
  203. });
  204. it('should auto-sync when files change while watching', async () => {
  205. cg = CodeGraph.initSync(testDir, {
  206. config: { include: ['**/*.ts'], exclude: [] },
  207. });
  208. await cg.indexAll();
  209. const initialStats = cg.getStats();
  210. const initialNodes = initialStats.nodeCount;
  211. cg.watch({ debounceMs: 300 });
  212. // Add a new file with a function
  213. fs.writeFileSync(
  214. path.join(testDir, 'src', 'added.ts'),
  215. 'export function added() { return 42; }'
  216. );
  217. // Wait for auto-sync to pick it up
  218. await waitFor(() => {
  219. const stats = cg.getStats();
  220. return stats.nodeCount > initialNodes;
  221. }, 10000);
  222. // The new function should be in the graph
  223. const results = cg.searchNodes('added');
  224. expect(results.length).toBeGreaterThan(0);
  225. cg.unwatch();
  226. });
  227. });
  228. });