sync.test.ts 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  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. it('persists an oversized skipped file so later syncs do not retry it (#1557)', async () => {
  123. const filePath = path.join(testDir, 'src', 'oversized.ts');
  124. fs.writeFileSync(filePath, 'const value = 1;\n'.repeat(70_000));
  125. const first = await cg.sync();
  126. expect(first.filesAdded).toBe(1);
  127. expect(cg.getFiles().find((f) => f.path === 'src/oversized.ts')?.errors?.[0]?.code).toBe('size_exceeded');
  128. const second = await cg.sync();
  129. expect(second.filesAdded).toBe(0);
  130. expect(second.filesModified).toBe(0);
  131. });
  132. it('marks a successfully recovered indexing state complete (#1556)', async () => {
  133. (cg as any).queries.setMetadata('index_state', 'indexing');
  134. await cg.sync({ paths: ['src/index.ts'] });
  135. expect(cg.getIndexState()).toBe('indexing');
  136. await cg.sync();
  137. expect(cg.getIndexState()).toBe('complete');
  138. });
  139. });
  140. });
  141. describe('Git-based sync', () => {
  142. let testDir: string;
  143. let cg: CodeGraph;
  144. function git(...args: string[]) {
  145. execFileSync('git', args, { cwd: testDir, stdio: 'pipe' });
  146. }
  147. beforeEach(async () => {
  148. testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-git-sync-'));
  149. // Initialize a git repo with an initial commit
  150. git('init');
  151. git('config', 'user.email', 'test@test.com');
  152. git('config', 'user.name', 'Test');
  153. const srcDir = path.join(testDir, 'src');
  154. fs.mkdirSync(srcDir);
  155. fs.writeFileSync(
  156. path.join(srcDir, 'index.ts'),
  157. `export function hello() { return 'world'; }`
  158. );
  159. git('add', '-A');
  160. git('commit', '-m', 'initial');
  161. // Initialize CodeGraph and index
  162. cg = CodeGraph.initSync(testDir, {
  163. config: {
  164. include: ['**/*.ts'],
  165. exclude: [],
  166. },
  167. });
  168. await cg.indexAll();
  169. });
  170. afterEach(() => {
  171. if (cg) {
  172. cg.destroy();
  173. }
  174. if (fs.existsSync(testDir)) {
  175. fs.rmSync(testDir, { recursive: true, force: true });
  176. }
  177. });
  178. it('should detect modified files via git', async () => {
  179. fs.writeFileSync(
  180. path.join(testDir, 'src', 'index.ts'),
  181. `export function hello() { return 'modified'; }`
  182. );
  183. const result = await cg.sync();
  184. expect(result.filesModified).toBe(1);
  185. expect(result.changedFilePaths).toContain('src/index.ts');
  186. });
  187. it('should detect new untracked files via git', async () => {
  188. fs.writeFileSync(
  189. path.join(testDir, 'src', 'new.ts'),
  190. `export function newFunc() { return 42; }`
  191. );
  192. const result = await cg.sync();
  193. expect(result.filesAdded).toBe(1);
  194. expect(result.changedFilePaths).toContain('src/new.ts');
  195. // Verify the function was indexed
  196. const nodes = cg.searchNodes('newFunc');
  197. expect(nodes.length).toBeGreaterThan(0);
  198. });
  199. it('should stop reporting untracked files once they are indexed (issue #206)', async () => {
  200. // Untracked files stay `??` in git status even after codegraph indexes
  201. // them. Change detection must compare them against the DB by hash, not
  202. // report every untracked file as "added" on every sync/status.
  203. fs.writeFileSync(
  204. path.join(testDir, 'src', 'new.ts'),
  205. `export function newFunc() { return 42; }`
  206. );
  207. // First sync indexes the untracked file.
  208. const first = await cg.sync();
  209. expect(first.filesAdded).toBe(1);
  210. // The file is still untracked in git, but now lives in the DB.
  211. expect(cg.searchNodes('newFunc').length).toBeGreaterThan(0);
  212. // status must not keep flagging it as a pending addition...
  213. const changes = cg.getChangedFiles();
  214. expect(changes.added).not.toContain('src/new.ts');
  215. expect(changes.modified).not.toContain('src/new.ts');
  216. // ...and a second sync must be a no-op for it.
  217. const second = await cg.sync();
  218. expect(second.filesAdded).toBe(0);
  219. expect(second.filesModified).toBe(0);
  220. });
  221. it('should re-index an untracked file when its contents change', async () => {
  222. const filePath = path.join(testDir, 'src', 'new.ts');
  223. fs.writeFileSync(filePath, `export function newFunc() { return 42; }`);
  224. await cg.sync();
  225. // Modify the still-untracked file.
  226. fs.writeFileSync(filePath, `export function renamedFunc() { return 7; }`);
  227. const changes = cg.getChangedFiles();
  228. expect(changes.modified).toContain('src/new.ts');
  229. const result = await cg.sync();
  230. expect(result.filesModified).toBe(1);
  231. expect(cg.searchNodes('renamedFunc').length).toBeGreaterThan(0);
  232. expect(cg.searchNodes('newFunc').length).toBe(0);
  233. });
  234. it('should detect deleted files via git', async () => {
  235. fs.unlinkSync(path.join(testDir, 'src', 'index.ts'));
  236. const result = await cg.sync();
  237. expect(result.filesRemoved).toBe(1);
  238. // Verify function is gone
  239. const nodes = cg.searchNodes('hello');
  240. expect(nodes.length).toBe(0);
  241. });
  242. it('should skip files with unsupported extensions', async () => {
  243. // A .txt file has no supported grammar, so sync must not index it.
  244. fs.writeFileSync(
  245. path.join(testDir, 'src', 'notes.txt'),
  246. `just some notes`
  247. );
  248. const result = await cg.sync();
  249. expect(result.filesAdded).toBe(0);
  250. expect(result.filesModified).toBe(0);
  251. });
  252. it('should report no changes on clean working tree', async () => {
  253. const result = await cg.sync();
  254. expect(result.filesAdded).toBe(0);
  255. expect(result.filesModified).toBe(0);
  256. expect(result.filesRemoved).toBe(0);
  257. expect(result.changedFilePaths).toBeUndefined();
  258. });
  259. });
  260. // Incremental sync's git fast path used to consume `git status` output without
  261. // the ignore matcher the full index applies — so a committed dependency dir
  262. // (built-in default exclude) or a tracked file under a .gitignored dir would
  263. // leak into the index via `sync`, then vanish on the next `index --force`. The
  264. // git fast path must exclude exactly what the full scan does. (#766)
  265. describe('Incremental sync honors the ignore matcher (#766)', () => {
  266. let testDir: string;
  267. let cg: CodeGraph;
  268. function git(...args: string[]) {
  269. execFileSync('git', args, { cwd: testDir, stdio: 'pipe' });
  270. }
  271. beforeEach(async () => {
  272. testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-766-'));
  273. git('init');
  274. git('config', 'user.email', 'test@test.com');
  275. git('config', 'user.name', 'Test');
  276. // Real project source — must keep flowing through sync untouched.
  277. fs.mkdirSync(path.join(testDir, 'src'));
  278. fs.writeFileSync(
  279. path.join(testDir, 'src', 'index.ts'),
  280. `export function hello() { return 'world'; }`
  281. );
  282. // A COMMITTED vendor/ dir: tracked in git, but a built-in default exclude
  283. // git knows nothing about. git status happily reports edits to it.
  284. fs.mkdirSync(path.join(testDir, 'vendor'));
  285. fs.writeFileSync(
  286. path.join(testDir, 'vendor', 'lib.ts'),
  287. `export function vendoredHelper() { return 1; }`
  288. );
  289. // A tracked file inside a .gitignored dir: gitignore is a no-op for files
  290. // already committed, so git status still reports modifications to it.
  291. fs.writeFileSync(path.join(testDir, '.gitignore'), 'generated/\n');
  292. fs.mkdirSync(path.join(testDir, 'generated'));
  293. fs.writeFileSync(
  294. path.join(testDir, 'generated', 'out.ts'),
  295. `export function generatedThing() { return 2; }`
  296. );
  297. git('add', '-A'); // .gitignore + src/ + vendor/ (generated/ is now ignored)
  298. git('add', '-f', 'generated/out.ts'); // force the ignored-but-tracked file in
  299. git('commit', '-m', 'initial');
  300. cg = CodeGraph.initSync(testDir, {
  301. config: { include: ['**/*.ts'], exclude: [] },
  302. });
  303. await cg.indexAll();
  304. });
  305. afterEach(() => {
  306. if (cg) cg.destroy();
  307. if (fs.existsSync(testDir)) fs.rmSync(testDir, { recursive: true, force: true });
  308. });
  309. it('the full index excludes both (baseline the sync path must match)', () => {
  310. expect(cg.searchNodes('hello').length).toBeGreaterThan(0);
  311. expect(cg.searchNodes('vendoredHelper')).toHaveLength(0);
  312. expect(cg.searchNodes('generatedThing')).toHaveLength(0);
  313. });
  314. it('does not re-index a modified tracked file in a built-in excluded dir (vendor/)', () => {
  315. fs.writeFileSync(
  316. path.join(testDir, 'vendor', 'lib.ts'),
  317. `export function vendoredHelper() { return 999; }`
  318. );
  319. const changes = cg.getChangedFiles();
  320. expect(changes.modified).not.toContain('vendor/lib.ts');
  321. expect(changes.added).not.toContain('vendor/lib.ts');
  322. });
  323. it('does not re-index a modified tracked file in a .gitignored dir', () => {
  324. fs.writeFileSync(
  325. path.join(testDir, 'generated', 'out.ts'),
  326. `export function generatedThing() { return 999; }`
  327. );
  328. const changes = cg.getChangedFiles();
  329. expect(changes.modified).not.toContain('generated/out.ts');
  330. expect(changes.added).not.toContain('generated/out.ts');
  331. });
  332. it('does not index a new untracked file in an excluded dir', () => {
  333. // vendor/ isn't in .gitignore, so an untracked file there surfaces as `??`
  334. // in git status — it must still be filtered to match the full index.
  335. fs.writeFileSync(
  336. path.join(testDir, 'vendor', 'extra.ts'),
  337. `export function vendoredExtra() { return 3; }`
  338. );
  339. const changes = cg.getChangedFiles();
  340. expect(changes.added).not.toContain('vendor/extra.ts');
  341. });
  342. it('status (getChangedFiles) agrees with sync — no phantom pending changes', async () => {
  343. // The user-visible symptom today: `codegraph status` reads getChangedFiles
  344. // and reports a vendor edit as a pending change that `sync` (a filesystem
  345. // reconcile) then never indexes — so the count never clears. Both must now
  346. // agree that nothing happened.
  347. fs.writeFileSync(
  348. path.join(testDir, 'vendor', 'lib.ts'),
  349. `export function vendoredHelper() { return 999; }`
  350. );
  351. const changes = cg.getChangedFiles();
  352. expect(changes.added).toHaveLength(0);
  353. expect(changes.modified).toHaveLength(0);
  354. const result = await cg.sync();
  355. expect(result.filesModified).toBe(0);
  356. expect(result.changedFilePaths ?? []).not.toContain('vendor/lib.ts');
  357. expect(cg.searchNodes('vendoredHelper')).toHaveLength(0);
  358. });
  359. it('still syncs a normal modified source file (no over-filtering)', () => {
  360. fs.writeFileSync(
  361. path.join(testDir, 'src', 'index.ts'),
  362. `export function hello() { return 'changed'; }`
  363. );
  364. const changes = cg.getChangedFiles();
  365. expect(changes.modified).toContain('src/index.ts');
  366. });
  367. });
  368. // Incremental sync used to scope resolution to the CHANGED files' refs, and
  369. // a completed pass deleted every ref it failed to resolve — so when a changed
  370. // file introduced an export/symbol that would satisfy a previously-failed ref
  371. // in an UNCHANGED file, nothing ever revisited it: the cross-file edge stayed
  372. // missing (with status reporting a clean index) until a full re-index. Failed
  373. // refs are now parked as status='failed' and retried when a sync lands files
  374. // carrying a matching symbol name. (#1240)
  375. describe('Sync resolves refs satisfied by a new export in another file (#1240)', () => {
  376. let testDir: string;
  377. let cg: CodeGraph;
  378. function write(rel: string, content: string) {
  379. fs.writeFileSync(path.join(testDir, rel), content);
  380. }
  381. function callersOf(fnName: string, kind: string = 'function'): string[] {
  382. const results = cg.searchNodes(fnName);
  383. const def = results.map((r) => r.node).find((n) => n.kind === kind && n.name === fnName);
  384. if (!def) return [];
  385. return cg.getCallers(def.id).map((c) => c.node.name);
  386. }
  387. beforeEach(async () => {
  388. testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-1240-'));
  389. // a.ts references `greet`, which does not exist anywhere yet — the ref
  390. // fails resolution during the initial index.
  391. write('a.ts', `import { greet } from './b';\n\nexport function run(): number {\n return greet();\n}\n`);
  392. write('b.ts', `export function other(): number {\n return 1;\n}\n`);
  393. cg = CodeGraph.initSync(testDir, {
  394. config: { include: ['**/*.ts'], exclude: [] },
  395. });
  396. await cg.indexAll();
  397. });
  398. afterEach(() => {
  399. if (cg) cg.destroy();
  400. if (fs.existsSync(testDir)) fs.rmSync(testDir, { recursive: true, force: true });
  401. });
  402. it('baseline: the unresolvable ref creates no edge and does not report as pending', () => {
  403. expect(callersOf('greet')).toHaveLength(0);
  404. // Failed refs are parked, not pending — status must keep reporting a
  405. // healthy index, or every repo with external-library imports would
  406. // permanently warn about an "interrupted run".
  407. expect(cg.getPendingReferenceCount()).toBe(0);
  408. });
  409. it('creates the cross-file calls edge from the UNCHANGED file after sync', async () => {
  410. write('b.ts', `export function greet(): number {\n return 42;\n}\n`);
  411. const result = await cg.sync();
  412. expect(result.filesModified).toBe(1);
  413. // The ref lives in a.ts, which did NOT change — only the retry of the
  414. // parked failed ref can create this edge.
  415. expect(callersOf('greet')).toContain('run');
  416. expect(cg.getPendingReferenceCount()).toBe(0);
  417. });
  418. it('the synced graph matches a full re-index (the issue\'s exact complaint)', async () => {
  419. write('b.ts', `export function greet(): number {\n return 42;\n}\n`);
  420. await cg.sync();
  421. const synced = cg.getStats();
  422. await cg.indexAll();
  423. const reindexed = cg.getStats();
  424. expect(synced.edgeCount).toBe(reindexed.edgeCount);
  425. expect(synced.nodeCount).toBe(reindexed.nodeCount);
  426. });
  427. it('a second sync is a no-op and does not duplicate edges', async () => {
  428. write('b.ts', `export function greet(): number {\n return 42;\n}\n`);
  429. await cg.sync();
  430. const afterFirst = cg.getStats();
  431. const second = await cg.sync();
  432. expect(second.filesModified).toBe(0);
  433. expect(cg.getStats().edgeCount).toBe(afterFirst.edgeCount);
  434. expect(callersOf('greet')).toContain('run');
  435. });
  436. it('retries dotted method refs via the name tail when a class gains the method', async () => {
  437. // `h.greet()` is stored as reference_name 'h.greet'; the retry lookup
  438. // must match it through name_tail ('greet') when Helper gains greet.
  439. write('use.ts', `import { Helper } from './helper';\n\nexport function useHelper(): number {\n const h = new Helper();\n return h.greet();\n}\n`);
  440. write('helper.ts', `export class Helper {\n other(): number {\n return 1;\n }\n}\n`);
  441. await cg.sync();
  442. expect(callersOf('greet', 'method')).toHaveLength(0);
  443. write('helper.ts', `export class Helper {\n other(): number {\n return 1;\n }\n greet(): number {\n return 42;\n }\n}\n`);
  444. const result = await cg.sync();
  445. expect(result.filesModified).toBe(1);
  446. expect(callersOf('greet', 'method')).toContain('useHelper');
  447. });
  448. });
  449. // The removal-side counterpart of #1240: when a re-index (or file deletion)
  450. // drops a symbol other files had resolved edges to, those edges cascade away
  451. // and the referencing files — which did not change — were never given a
  452. // chance to re-resolve, so they could not rebind to an alternative
  453. // definition the way a full re-index would. Resolution edges now carry their
  454. // originating reference (metadata.refName), and a dropped edge is
  455. // resurrected as that exact ref: re-resolved in the same sync, or parked as
  456. // failed until the symbol reappears.
  457. describe('Sync rebinds or parks refs when a resolved symbol is removed (#1240 removal case)', () => {
  458. let testDir: string;
  459. let cg: CodeGraph;
  460. function write(rel: string, content: string) {
  461. fs.writeFileSync(path.join(testDir, rel), content);
  462. }
  463. function greetDef(): { id: string; filePath: string } | undefined {
  464. const results = cg.searchNodes('greet');
  465. const def = results.map((r) => r.node).find((n) => n.kind === 'function' && n.name === 'greet');
  466. return def ? { id: def.id, filePath: def.filePath } : undefined;
  467. }
  468. function greetCallers(): string[] {
  469. const def = greetDef();
  470. return def ? cg.getCallers(def.id).map((c) => c.node.name) : [];
  471. }
  472. beforeEach(async () => {
  473. testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-1240-removal-'));
  474. // No import — cross-file name matching, so the caller can legitimately
  475. // rebind to a definition in ANY file, which is what a full re-index does.
  476. write('a.ts', `export function run(): number {\n return greet();\n}\n`);
  477. write('b.ts', `export function greet(): number {\n return 42;\n}\n`);
  478. cg = CodeGraph.initSync(testDir, {
  479. config: { include: ['**/*.ts'], exclude: [] },
  480. });
  481. await cg.indexAll();
  482. // Baseline: the call resolved into b.ts.
  483. expect(greetCallers()).toContain('run');
  484. });
  485. afterEach(() => {
  486. if (cg) cg.destroy();
  487. if (fs.existsSync(testDir)) fs.rmSync(testDir, { recursive: true, force: true });
  488. });
  489. it('rebinds the unchanged caller when the symbol moves to another file', async () => {
  490. write('b.ts', `export function other(): number {\n return 1;\n}\n`);
  491. write('d.ts', `export function greet(): number {\n return 42;\n}\n`);
  492. await cg.sync();
  493. const def = greetDef();
  494. expect(def?.filePath).toBe('d.ts');
  495. expect(greetCallers()).toContain('run');
  496. // Parity with a full re-index — the issue's contract.
  497. const synced = cg.getStats();
  498. await cg.indexAll();
  499. expect(cg.getStats().edgeCount).toBe(synced.edgeCount);
  500. });
  501. it('drops the edge on removal and restores it when the symbol returns', async () => {
  502. write('b.ts', `export function other(): number {\n return 1;\n}\n`);
  503. await cg.sync();
  504. // Removed with no alternative: the edge must be gone (not preserved
  505. // against a nonexistent symbol) and status must stay clean while the
  506. // ref waits parked.
  507. expect(greetDef()).toBeUndefined();
  508. expect(cg.getPendingReferenceCount()).toBe(0);
  509. write('b.ts', `export function other(): number {\n return 1;\n}\nexport function greet(): number {\n return 42;\n}\n`);
  510. await cg.sync();
  511. expect(greetCallers()).toContain('run');
  512. });
  513. it('handles whole-file deletion: parks the ref, then rebinds when the symbol reappears elsewhere', async () => {
  514. fs.unlinkSync(path.join(testDir, 'b.ts'));
  515. const removal = await cg.sync();
  516. expect(removal.filesRemoved).toBe(1);
  517. expect(greetDef()).toBeUndefined();
  518. expect(cg.getPendingReferenceCount()).toBe(0);
  519. write('d.ts', `export function greet(): number {\n return 99;\n}\n`);
  520. await cg.sync();
  521. expect(greetDef()?.filePath).toBe('d.ts');
  522. expect(greetCallers()).toContain('run');
  523. });
  524. });
  525. describe('Cross-file module-attribute caller edges survive callee re-index (#899)', () => {
  526. let testDir: string;
  527. let cg: CodeGraph;
  528. beforeEach(async () => {
  529. testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-899-'));
  530. // pkg/mod.py — a module with two functions, both called from a separate
  531. // test file via `mod.<fn>(...)` (module-attribute access). This is the
  532. // exact shape from the RAGFlow production case in issue #899.
  533. fs.mkdirSync(path.join(testDir, 'pkg'), { recursive: true });
  534. fs.mkdirSync(path.join(testDir, 'test'), { recursive: true });
  535. fs.writeFileSync(
  536. path.join(testDir, 'pkg', '__init__.py'),
  537. ``
  538. );
  539. fs.writeFileSync(
  540. path.join(testDir, 'pkg', 'mod.py'),
  541. [
  542. `def callee_one(value):`,
  543. ` """First callee — docstring above the second callee so edits here shift its line."""`,
  544. ` return value + 1`,
  545. ``,
  546. ``,
  547. `def callee_two(value):`,
  548. ` """Second callee, called from the test file via mod.callee_two(...)."""`,
  549. ` return value + 2`,
  550. ``,
  551. ].join('\n')
  552. );
  553. fs.writeFileSync(
  554. path.join(testDir, 'test', 'test_callers.py'),
  555. [
  556. `from pkg import mod`,
  557. ``,
  558. ``,
  559. `def test_calls_callee_one():`,
  560. ` assert mod.callee_one(1) == 2`,
  561. ``,
  562. ``,
  563. `def test_calls_callee_two():`,
  564. ` assert mod.callee_two(1) == 3`,
  565. ``,
  566. ].join('\n')
  567. );
  568. cg = CodeGraph.initSync(testDir, {
  569. config: { include: ['**/*.py'], exclude: [] },
  570. });
  571. await cg.indexAll();
  572. });
  573. afterEach(() => {
  574. if (cg) cg.destroy();
  575. if (fs.existsSync(testDir)) fs.rmSync(testDir, { recursive: true, force: true });
  576. });
  577. function callerCount(fnName: string): number {
  578. const results = cg.searchNodes(fnName);
  579. const def = results.map(r => r.node).find(n => n.kind === 'function' && n.name === fnName);
  580. if (!def) return -1;
  581. return cg.getCallers(def.id).length;
  582. }
  583. it('preserves incoming cross-file calls edges when the callee file is re-indexed', async () => {
  584. // Baseline: both callees have one cross-file caller each.
  585. expect(callerCount('callee_one')).toBe(1);
  586. expect(callerCount('callee_two')).toBe(1);
  587. // Docstring-only edit to callee_one — adds 1 line, shifting callee_two's
  588. // line number. A naive ID-based edge restore would drop callee_two's
  589. // incoming edge (its node id changed); the (kind, name) re-resolve
  590. // preserves it. A docstring-only edit also confirms zero-AST-change
  591. // re-indexes don't sever edges.
  592. fs.writeFileSync(
  593. path.join(testDir, 'pkg', 'mod.py'),
  594. [
  595. `def callee_one(value):`,
  596. ` """First callee — docstring above the second callee so edits here shift its line."""`,
  597. ` """Probe: extra docstring line to shift callee_two's start line by 1."""`,
  598. ` return value + 1`,
  599. ``,
  600. ``,
  601. `def callee_two(value):`,
  602. ` """Second callee, called from the test file via mod.callee_two(...)."""`,
  603. ` return value + 2`,
  604. ``,
  605. ].join('\n')
  606. );
  607. const result = await cg.sync();
  608. expect(result.filesModified).toBe(1);
  609. // Both incoming cross-file calls edges must survive the callee re-index.
  610. expect(callerCount('callee_one')).toBe(1);
  611. expect(callerCount('callee_two')).toBe(1);
  612. });
  613. it('drops incoming edges for a callee that was renamed during re-index', async () => {
  614. // Baseline.
  615. expect(callerCount('callee_one')).toBe(1);
  616. // Rename callee_one -> callee_one_renamed. The old edge's target
  617. // (kind=function, name=callee_one) no longer matches any re-indexed
  618. // node, so the edge is correctly dropped (not preserved against a
  619. // non-existent symbol).
  620. fs.writeFileSync(
  621. path.join(testDir, 'pkg', 'mod.py'),
  622. [
  623. `def callee_one_renamed(value):`,
  624. ` """Renamed callee — the old edge targeting callee_one must not be restored."""`,
  625. ` return value + 1`,
  626. ``,
  627. ``,
  628. `def callee_two(value):`,
  629. ` """Second callee, called from the test file via mod.callee_two(...)."""`,
  630. ` return value + 2`,
  631. ``,
  632. ].join('\n')
  633. );
  634. await cg.sync();
  635. // The renamed callee has no callers (the test still calls mod.callee_one,
  636. // which no longer exists). The old callee_one node is gone, so its
  637. // callerCount is -1 (definition not found); callee_one_renamed exists
  638. // but has no incoming edges (the test calls the old name).
  639. expect(callerCount('callee_one')).toBe(-1);
  640. expect(callerCount('callee_one_renamed')).toBe(0);
  641. // callee_two is untouched by the rename and its edge survives.
  642. expect(callerCount('callee_two')).toBe(1);
  643. });
  644. });
  645. });
  646. describe('Scoped sync parity (#watcher-scoped)', () => {
  647. let testDir: string;
  648. let cg: CodeGraph;
  649. const snapshot = (g: CodeGraph): string => {
  650. // Natural-key snapshot of the whole graph, mirroring dump-graph.mjs at
  651. // unit scale: scoped and full sync must land the DB in the same state.
  652. const nodes = g
  653. .searchNodes('', { limit: 100000 })
  654. .map((r) => r.node)
  655. .map((n) => `${n.kind}|${n.qualifiedName}|${n.filePath}|${n.startLine}`)
  656. .sort()
  657. .join('\n');
  658. return nodes;
  659. };
  660. beforeEach(async () => {
  661. testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-sync-scoped-'));
  662. const srcDir = path.join(testDir, 'src');
  663. fs.mkdirSync(srcDir);
  664. fs.writeFileSync(path.join(srcDir, 'a.ts'), `export function alpha() { return beta(); }`);
  665. fs.writeFileSync(path.join(srcDir, 'b.ts'), `export function beta() { return 1; }`);
  666. cg = CodeGraph.initSync(testDir);
  667. await cg.indexAll();
  668. });
  669. afterEach(() => {
  670. cg?.destroy();
  671. if (fs.existsSync(testDir)) fs.rmSync(testDir, { recursive: true, force: true });
  672. });
  673. it('a scoped modify lands the same graph as a full sync of the same edit', async () => {
  674. fs.writeFileSync(path.join(testDir, 'src', 'b.ts'), `export function beta() { return 2; }\nexport function gamma() { return 3; }`);
  675. const scoped = await cg.sync({ paths: ['src/b.ts'] });
  676. expect(scoped.filesModified).toBe(1);
  677. const scopedSnap = snapshot(cg);
  678. // Re-apply the same end state through a FULL sync from the same start
  679. // state: revert, full-sync, edit again, full-sync.
  680. fs.writeFileSync(path.join(testDir, 'src', 'b.ts'), `export function beta() { return 1; }`);
  681. await cg.sync();
  682. fs.writeFileSync(path.join(testDir, 'src', 'b.ts'), `export function beta() { return 2; }\nexport function gamma() { return 3; }`);
  683. const full = await cg.sync();
  684. expect(full.filesModified).toBe(1);
  685. expect(snapshot(cg)).toBe(scopedSnap);
  686. });
  687. it('a scoped delete removes the file and resurrects cross-file refs like a full sync', async () => {
  688. fs.rmSync(path.join(testDir, 'src', 'b.ts'));
  689. const scoped = await cg.sync({ paths: ['src/b.ts'] });
  690. expect(scoped.filesRemoved).toBe(1);
  691. expect(scoped.filesChecked).toBe(1); // checked paths, not found files (#449 lock signature)
  692. const gone = cg.searchNodes('beta');
  693. expect(gone.filter((r) => r.node.filePath === 'src/b.ts').length).toBe(0);
  694. });
  695. it('a scoped add indexes the new file', async () => {
  696. fs.writeFileSync(path.join(testDir, 'src', 'c.ts'), `export function delta() { return 4; }`);
  697. const scoped = await cg.sync({ paths: ['src/c.ts'] });
  698. expect(scoped.filesAdded).toBe(1);
  699. expect(cg.searchNodes('delta').length).toBeGreaterThan(0);
  700. });
  701. it('scoped sync ignores paths outside the change without touching them', async () => {
  702. fs.writeFileSync(path.join(testDir, 'src', 'a.ts'), `export function alpha() { return beta() + 1; }`);
  703. const scoped = await cg.sync({ paths: ['src/a.ts'] });
  704. expect(scoped.filesModified).toBe(1);
  705. expect(scoped.filesRemoved).toBe(0);
  706. // b.ts untouched and still present
  707. expect(cg.searchNodes('beta').length).toBeGreaterThan(0);
  708. });
  709. it('a scoped path that codegraph.json now excludes is removed, never re-parsed (#1590)', async () => {
  710. // The daemon's watcher hands sync the exact edited path. If the project's
  711. // scope changed underneath it, that path must be treated the way the full
  712. // scan treats it — out of scope, hence gone — never parsed on trust.
  713. const cfg = path.join(testDir, 'codegraph.json');
  714. fs.writeFileSync(cfg, JSON.stringify({ exclude: ['src/b.ts'] }));
  715. fs.writeFileSync(path.join(testDir, 'src', 'b.ts'), `export function beta() { return 2; }\nexport function gamma() { return 3; }`);
  716. const scoped = await cg.sync({ paths: ['src/b.ts'] });
  717. expect(scoped.filesRemoved).toBe(1);
  718. expect(scoped.filesModified).toBe(0);
  719. expect(scoped.filesAdded).toBe(0);
  720. expect(cg.searchNodes('gamma').length).toBe(0);
  721. expect(cg.searchNodes('beta').filter((r) => r.node.filePath === 'src/b.ts').length).toBe(0);
  722. // Idempotent: the file stays out on a repeat scoped sync.
  723. const again = await cg.sync({ paths: ['src/b.ts'] });
  724. expect(again.filesRemoved).toBe(0);
  725. expect(again.filesAdded).toBe(0);
  726. // Dropping the exclude readmits it through the same scoped path. The
  727. // scope matcher is mtime-keyed, so give the rewrite a distinct mtime even
  728. // on a coarse-timestamp filesystem.
  729. fs.writeFileSync(cfg, JSON.stringify({}));
  730. const later = new Date(Date.now() + 5000);
  731. fs.utimesSync(cfg, later, later);
  732. const readmitted = await cg.sync({ paths: ['src/b.ts'] });
  733. expect(readmitted.filesAdded).toBe(1);
  734. expect(cg.searchNodes('gamma').length).toBe(1);
  735. });
  736. });
  737. // A change that is COMMITTED but not yet indexed used to read as zero pending
  738. // changes: getChangedFiles' git fast path built its candidate list from
  739. // `git status --porcelain`, and committing is exactly what removes a file from
  740. // that output. The hash comparison below it was correct and simply never
  741. // reached. Committed work is now sourced from `git diff <indexed commit> HEAD`.
  742. // (#1829)
  743. describe('committed-but-unindexed changes (#1829)', () => {
  744. let testDir: string;
  745. let cg: CodeGraph;
  746. const git = (...args: string[]) =>
  747. execFileSync('git', args, { cwd: testDir, encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'] });
  748. beforeEach(async () => {
  749. testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-1829-'));
  750. git('init');
  751. git('config', 'user.email', 'test@test.com');
  752. git('config', 'user.name', 'Test');
  753. fs.mkdirSync(path.join(testDir, 'src'));
  754. fs.writeFileSync(path.join(testDir, 'src', 'one.ts'), `export function alpha() { return 1; }`);
  755. git('add', '-A');
  756. git('commit', '-m', 'initial');
  757. cg = CodeGraph.initSync(testDir, { config: { include: ['**/*.ts'], exclude: [] } });
  758. await cg.indexAll();
  759. });
  760. afterEach(() => {
  761. if (cg) cg.destroy();
  762. if (fs.existsSync(testDir)) fs.rmSync(testDir, { recursive: true, force: true });
  763. });
  764. it('sees a committed NEW file (git status shows nothing; the DB has no row)', async () => {
  765. fs.writeFileSync(path.join(testDir, 'src', 'two.ts'), `export function beta() { return 2; }`);
  766. git('add', '-A');
  767. git('commit', '-m', 'add two');
  768. const changes = cg.getChangedFiles();
  769. expect(changes.added).toContain('src/two.ts');
  770. // status and sync must agree — the whole point is that the number a user
  771. // reads matches the work that is actually outstanding.
  772. const result = await cg.sync();
  773. expect(result.filesAdded).toBe(1);
  774. expect(cg.searchNodes('beta').length).toBeGreaterThan(0);
  775. });
  776. it('sees a committed MODIFICATION to an already-tracked file', async () => {
  777. fs.writeFileSync(path.join(testDir, 'src', 'one.ts'), `export function alphaRenamed() { return 99; }`);
  778. git('add', '-A');
  779. git('commit', '-m', 'edit one');
  780. expect(cg.getChangedFiles().modified).toContain('src/one.ts');
  781. const result = await cg.sync();
  782. expect(result.filesModified).toBe(1);
  783. expect(cg.searchNodes('alphaRenamed').length).toBeGreaterThan(0);
  784. });
  785. it('sees a committed DELETE', async () => {
  786. fs.writeFileSync(path.join(testDir, 'src', 'two.ts'), `export function beta() { return 2; }`);
  787. git('add', '-A');
  788. git('commit', '-m', 'add two');
  789. await cg.sync();
  790. fs.rmSync(path.join(testDir, 'src', 'two.ts'));
  791. git('add', '-A');
  792. git('commit', '-m', 'remove two');
  793. expect(cg.getChangedFiles().removed).toContain('src/two.ts');
  794. const result = await cg.sync();
  795. expect(result.filesRemoved).toBe(1);
  796. });
  797. it('reports zero once the sync has absorbed the commit (the stamp advances)', async () => {
  798. fs.writeFileSync(path.join(testDir, 'src', 'two.ts'), `export function beta() { return 2; }`);
  799. git('add', '-A');
  800. git('commit', '-m', 'add two');
  801. await cg.sync();
  802. const after = cg.getChangedFiles();
  803. expect(after.added).toHaveLength(0);
  804. expect(after.modified).toHaveLength(0);
  805. expect(after.removed).toHaveLength(0);
  806. });
  807. it('counts a file once when it was committed AND edited again since', async () => {
  808. // The same path now reaches the candidate list from both sources — the
  809. // committed diff and `git status`. It is still one changed file.
  810. fs.writeFileSync(path.join(testDir, 'src', 'one.ts'), `export function alpha() { return 2; }`);
  811. git('add', '-A');
  812. git('commit', '-m', 'edit one');
  813. fs.writeFileSync(path.join(testDir, 'src', 'one.ts'), `export function alpha() { return 3; }`);
  814. const changes = cg.getChangedFiles();
  815. expect(changes.modified.filter((f) => f === 'src/one.ts')).toHaveLength(1);
  816. expect(changes.added).toHaveLength(0);
  817. const result = await cg.sync();
  818. expect(result.filesModified).toBe(1);
  819. });
  820. it('sees a committed RENAME as a removal plus an add', async () => {
  821. // `--no-renames` on the committed diff is deliberate: the index keys files
  822. // by path, so a rename IS a removal and an add, and pairing them up would
  823. // only have to be taken apart again.
  824. fs.renameSync(path.join(testDir, 'src', 'one.ts'), path.join(testDir, 'src', 'renamed.ts'));
  825. git('add', '-A');
  826. git('commit', '-m', 'rename one');
  827. const changes = cg.getChangedFiles();
  828. expect(changes.removed).toContain('src/one.ts');
  829. expect(changes.added).toContain('src/renamed.ts');
  830. const result = await cg.sync();
  831. expect(result.filesRemoved).toBe(1);
  832. expect(result.filesAdded).toBe(1);
  833. expect(cg.searchNodes('alpha').every((r) => r.node.filePath !== 'src/one.ts')).toBe(true);
  834. });
  835. it('still filters committed changes by the rules the full index uses', async () => {
  836. // vendor/ is a built-in exclude git knows nothing about. Sourcing candidates
  837. // from `git diff` must not smuggle in files `git status` would have had
  838. // filtered out (#766) — same classifier, both sources.
  839. fs.mkdirSync(path.join(testDir, 'vendor'));
  840. fs.writeFileSync(path.join(testDir, 'vendor', 'lib.ts'), `export function vendored() { return 1; }`);
  841. git('add', '-A');
  842. git('commit', '-m', 'add vendor');
  843. const changes = cg.getChangedFiles();
  844. expect(changes.added).not.toContain('vendor/lib.ts');
  845. expect(changes.modified).not.toContain('vendor/lib.ts');
  846. });
  847. it('falls back to the full scan when history moved under the index', async () => {
  848. // A rebase/gc/shallow clone can leave the stamped commit unreachable. The
  849. // fast path cannot diff against a commit that is gone, so the (correct,
  850. // slower) full scan has to answer instead of silently reporting zero.
  851. fs.writeFileSync(path.join(testDir, 'src', 'two.ts'), `export function beta() { return 2; }`);
  852. git('add', '-A');
  853. git('commit', '-m', 'add two');
  854. (cg as unknown as { queries: { setMetadata(k: string, v: string): void } })
  855. .queries.setMetadata('indexed_at_commit', '0'.repeat(40));
  856. expect(cg.getChangedFiles().added).toContain('src/two.ts');
  857. });
  858. it('an index with no stamp still answers correctly (pre-#1829 index upgrading)', async () => {
  859. (cg as unknown as { queries: { setMetadata(k: string, v: string): void } })
  860. .queries.setMetadata('indexed_at_commit', '');
  861. fs.writeFileSync(path.join(testDir, 'src', 'two.ts'), `export function beta() { return 2; }`);
  862. git('add', '-A');
  863. git('commit', '-m', 'add two');
  864. expect(cg.getChangedFiles().added).toContain('src/two.ts');
  865. // ...and it self-heals: the sync writes a stamp, so the next read is clean.
  866. await cg.sync();
  867. expect(cg.getChangedFiles().added).toHaveLength(0);
  868. });
  869. });