watcher.test.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. });
  134. describe('callbacks', () => {
  135. it('should call onSyncComplete after successful sync', async () => {
  136. const syncFn = vi.fn().mockResolvedValue({ filesChanged: 2, durationMs: 50 });
  137. const onSyncComplete = vi.fn();
  138. const watcher = new FileWatcher(testDir, syncFn, {
  139. debounceMs: 200,
  140. onSyncComplete,
  141. });
  142. watcher.start();
  143. fs.writeFileSync(path.join(testDir, 'src', 'test.ts'), 'export const z = 3;');
  144. await waitFor(() => onSyncComplete.mock.calls.length > 0, 5000);
  145. expect(onSyncComplete).toHaveBeenCalledWith({ filesChanged: 2, durationMs: 50 });
  146. watcher.stop();
  147. });
  148. it('should call onSyncError when sync throws', async () => {
  149. const syncFn = vi.fn().mockRejectedValue(new Error('sync failed'));
  150. const onSyncError = vi.fn();
  151. const watcher = new FileWatcher(testDir, syncFn, {
  152. debounceMs: 200,
  153. onSyncError,
  154. });
  155. watcher.start();
  156. fs.writeFileSync(path.join(testDir, 'src', 'test.ts'), 'export const z = 3;');
  157. await waitFor(() => onSyncError.mock.calls.length > 0, 5000);
  158. expect(onSyncError).toHaveBeenCalled();
  159. expect(onSyncError.mock.calls[0]![0]).toBeInstanceOf(Error);
  160. watcher.stop();
  161. });
  162. });
  163. describe('CodeGraph integration', () => {
  164. let cg: CodeGraph;
  165. afterEach(() => {
  166. if (cg) cg.close();
  167. });
  168. it('should watch and unwatch via CodeGraph API', async () => {
  169. cg = CodeGraph.initSync(testDir, {
  170. config: { include: ['**/*.ts'], exclude: [] },
  171. });
  172. await cg.indexAll();
  173. expect(cg.isWatching()).toBe(false);
  174. const started = cg.watch({ debounceMs: 200 });
  175. expect(started).toBe(true);
  176. expect(cg.isWatching()).toBe(true);
  177. cg.unwatch();
  178. expect(cg.isWatching()).toBe(false);
  179. });
  180. it('should stop watching on close', async () => {
  181. cg = CodeGraph.initSync(testDir, {
  182. config: { include: ['**/*.ts'], exclude: [] },
  183. });
  184. await cg.indexAll();
  185. cg.watch({ debounceMs: 200 });
  186. expect(cg.isWatching()).toBe(true);
  187. cg.close();
  188. // After close, isWatching should be false
  189. // (we can't call isWatching after close since DB is closed,
  190. // but we verify no errors are thrown)
  191. });
  192. it('should auto-sync when files change while watching', async () => {
  193. cg = CodeGraph.initSync(testDir, {
  194. config: { include: ['**/*.ts'], exclude: [] },
  195. });
  196. await cg.indexAll();
  197. const initialStats = cg.getStats();
  198. const initialNodes = initialStats.nodeCount;
  199. cg.watch({ debounceMs: 300 });
  200. // Add a new file with a function
  201. fs.writeFileSync(
  202. path.join(testDir, 'src', 'added.ts'),
  203. 'export function added() { return 42; }'
  204. );
  205. // Wait for auto-sync to pick it up
  206. await waitFor(() => {
  207. const stats = cg.getStats();
  208. return stats.nodeCount > initialNodes;
  209. }, 10000);
  210. // The new function should be in the graph
  211. const results = cg.searchNodes('added');
  212. expect(results.length).toBeGreaterThan(0);
  213. cg.unwatch();
  214. });
  215. });
  216. });