sync.test.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /**
  2. * Sync Module Tests
  3. *
  4. * Tests for sync functionality (incremental updates).
  5. * Note: Git hooks functionality has been removed in favor of codegraph's
  6. * Claude Code hooks integration.
  7. */
  8. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  9. import * as fs from 'fs';
  10. import * as path from 'path';
  11. import * as os from 'os';
  12. import { execFileSync } from 'child_process';
  13. import CodeGraph from '../src/index';
  14. describe('Sync Module', () => {
  15. describe('Sync Functionality', () => {
  16. let testDir: string;
  17. let cg: CodeGraph;
  18. beforeEach(async () => {
  19. testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-sync-func-'));
  20. // Create initial source files
  21. const srcDir = path.join(testDir, 'src');
  22. fs.mkdirSync(srcDir);
  23. fs.writeFileSync(
  24. path.join(srcDir, 'index.ts'),
  25. `export function hello() { return 'world'; }`
  26. );
  27. // Initialize and index
  28. cg = CodeGraph.initSync(testDir, {
  29. config: {
  30. include: ['**/*.ts'],
  31. exclude: [],
  32. },
  33. });
  34. await cg.indexAll();
  35. });
  36. afterEach(() => {
  37. if (cg) {
  38. cg.destroy();
  39. }
  40. if (fs.existsSync(testDir)) {
  41. fs.rmSync(testDir, { recursive: true, force: true });
  42. }
  43. });
  44. describe('getChangedFiles()', () => {
  45. it('should detect added files', () => {
  46. // Add a new file
  47. fs.writeFileSync(
  48. path.join(testDir, 'src', 'new.ts'),
  49. `export function newFunc() { return 42; }`
  50. );
  51. const changes = cg.getChangedFiles();
  52. expect(changes.added).toContain('src/new.ts');
  53. expect(changes.modified).toHaveLength(0);
  54. expect(changes.removed).toHaveLength(0);
  55. });
  56. it('should detect modified files', () => {
  57. // Modify existing file
  58. fs.writeFileSync(
  59. path.join(testDir, 'src', 'index.ts'),
  60. `export function hello() { return 'modified'; }`
  61. );
  62. const changes = cg.getChangedFiles();
  63. expect(changes.added).toHaveLength(0);
  64. expect(changes.modified).toContain('src/index.ts');
  65. expect(changes.removed).toHaveLength(0);
  66. });
  67. it('should detect removed files', () => {
  68. // Remove file
  69. fs.unlinkSync(path.join(testDir, 'src', 'index.ts'));
  70. const changes = cg.getChangedFiles();
  71. expect(changes.added).toHaveLength(0);
  72. expect(changes.modified).toHaveLength(0);
  73. expect(changes.removed).toContain('src/index.ts');
  74. });
  75. });
  76. describe('sync()', () => {
  77. it('should reindex added files', async () => {
  78. // Add a new file
  79. fs.writeFileSync(
  80. path.join(testDir, 'src', 'new.ts'),
  81. `export function newFunc() { return 42; }`
  82. );
  83. const result = await cg.sync();
  84. expect(result.filesAdded).toBe(1);
  85. expect(result.filesModified).toBe(0);
  86. expect(result.filesRemoved).toBe(0);
  87. // Verify new function is in the graph
  88. const nodes = cg.searchNodes('newFunc');
  89. expect(nodes.length).toBeGreaterThan(0);
  90. });
  91. it('should reindex modified files', async () => {
  92. // Modify existing file
  93. fs.writeFileSync(
  94. path.join(testDir, 'src', 'index.ts'),
  95. `export function goodbye() { return 'farewell'; }`
  96. );
  97. const result = await cg.sync();
  98. expect(result.filesModified).toBe(1);
  99. // Verify new function is in the graph
  100. const nodes = cg.searchNodes('goodbye');
  101. expect(nodes.length).toBeGreaterThan(0);
  102. // Verify old function is gone
  103. const oldNodes = cg.searchNodes('hello');
  104. expect(oldNodes.length).toBe(0);
  105. });
  106. it('should remove nodes from deleted files', async () => {
  107. // Remove file
  108. fs.unlinkSync(path.join(testDir, 'src', 'index.ts'));
  109. const result = await cg.sync();
  110. expect(result.filesRemoved).toBe(1);
  111. // Verify function is gone
  112. const nodes = cg.searchNodes('hello');
  113. expect(nodes.length).toBe(0);
  114. });
  115. it('should report no changes when nothing changed', async () => {
  116. const result = await cg.sync();
  117. expect(result.filesAdded).toBe(0);
  118. expect(result.filesModified).toBe(0);
  119. expect(result.filesRemoved).toBe(0);
  120. expect(result.filesChecked).toBeGreaterThan(0);
  121. });
  122. });
  123. });
  124. describe('Git-based sync', () => {
  125. let testDir: string;
  126. let cg: CodeGraph;
  127. function git(...args: string[]) {
  128. execFileSync('git', args, { cwd: testDir, stdio: 'pipe' });
  129. }
  130. beforeEach(async () => {
  131. testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-git-sync-'));
  132. // Initialize a git repo with an initial commit
  133. git('init');
  134. git('config', 'user.email', 'test@test.com');
  135. git('config', 'user.name', 'Test');
  136. const srcDir = path.join(testDir, 'src');
  137. fs.mkdirSync(srcDir);
  138. fs.writeFileSync(
  139. path.join(srcDir, 'index.ts'),
  140. `export function hello() { return 'world'; }`
  141. );
  142. git('add', '-A');
  143. git('commit', '-m', 'initial');
  144. // Initialize CodeGraph and index
  145. cg = CodeGraph.initSync(testDir, {
  146. config: {
  147. include: ['**/*.ts'],
  148. exclude: [],
  149. },
  150. });
  151. await cg.indexAll();
  152. });
  153. afterEach(() => {
  154. if (cg) {
  155. cg.destroy();
  156. }
  157. if (fs.existsSync(testDir)) {
  158. fs.rmSync(testDir, { recursive: true, force: true });
  159. }
  160. });
  161. it('should detect modified files via git', async () => {
  162. fs.writeFileSync(
  163. path.join(testDir, 'src', 'index.ts'),
  164. `export function hello() { return 'modified'; }`
  165. );
  166. const result = await cg.sync();
  167. expect(result.filesModified).toBe(1);
  168. expect(result.changedFilePaths).toContain('src/index.ts');
  169. });
  170. it('should detect new untracked files via git', async () => {
  171. fs.writeFileSync(
  172. path.join(testDir, 'src', 'new.ts'),
  173. `export function newFunc() { return 42; }`
  174. );
  175. const result = await cg.sync();
  176. expect(result.filesAdded).toBe(1);
  177. expect(result.changedFilePaths).toContain('src/new.ts');
  178. // Verify the function was indexed
  179. const nodes = cg.searchNodes('newFunc');
  180. expect(nodes.length).toBeGreaterThan(0);
  181. });
  182. it('should stop reporting untracked files once they are indexed (issue #206)', async () => {
  183. // Untracked files stay `??` in git status even after codegraph indexes
  184. // them. Change detection must compare them against the DB by hash, not
  185. // report every untracked file as "added" on every sync/status.
  186. fs.writeFileSync(
  187. path.join(testDir, 'src', 'new.ts'),
  188. `export function newFunc() { return 42; }`
  189. );
  190. // First sync indexes the untracked file.
  191. const first = await cg.sync();
  192. expect(first.filesAdded).toBe(1);
  193. // The file is still untracked in git, but now lives in the DB.
  194. expect(cg.searchNodes('newFunc').length).toBeGreaterThan(0);
  195. // status must not keep flagging it as a pending addition...
  196. const changes = cg.getChangedFiles();
  197. expect(changes.added).not.toContain('src/new.ts');
  198. expect(changes.modified).not.toContain('src/new.ts');
  199. // ...and a second sync must be a no-op for it.
  200. const second = await cg.sync();
  201. expect(second.filesAdded).toBe(0);
  202. expect(second.filesModified).toBe(0);
  203. });
  204. it('should re-index an untracked file when its contents change', async () => {
  205. const filePath = path.join(testDir, 'src', 'new.ts');
  206. fs.writeFileSync(filePath, `export function newFunc() { return 42; }`);
  207. await cg.sync();
  208. // Modify the still-untracked file.
  209. fs.writeFileSync(filePath, `export function renamedFunc() { return 7; }`);
  210. const changes = cg.getChangedFiles();
  211. expect(changes.modified).toContain('src/new.ts');
  212. const result = await cg.sync();
  213. expect(result.filesModified).toBe(1);
  214. expect(cg.searchNodes('renamedFunc').length).toBeGreaterThan(0);
  215. expect(cg.searchNodes('newFunc').length).toBe(0);
  216. });
  217. it('should detect deleted files via git', async () => {
  218. fs.unlinkSync(path.join(testDir, 'src', 'index.ts'));
  219. const result = await cg.sync();
  220. expect(result.filesRemoved).toBe(1);
  221. // Verify function is gone
  222. const nodes = cg.searchNodes('hello');
  223. expect(nodes.length).toBe(0);
  224. });
  225. it('should skip files not matching config', async () => {
  226. // Create a .js file which doesn't match **/*.ts
  227. fs.writeFileSync(
  228. path.join(testDir, 'src', 'ignored.js'),
  229. `function ignored() {}`
  230. );
  231. const result = await cg.sync();
  232. expect(result.filesAdded).toBe(0);
  233. expect(result.filesModified).toBe(0);
  234. });
  235. it('should report no changes on clean working tree', async () => {
  236. const result = await cg.sync();
  237. expect(result.filesAdded).toBe(0);
  238. expect(result.filesModified).toBe(0);
  239. expect(result.filesRemoved).toBe(0);
  240. expect(result.changedFilePaths).toBeUndefined();
  241. });
  242. });
  243. });