1
0

resolution.test.ts 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  1. /**
  2. * Resolution Module Tests
  3. *
  4. * Tests for Phase 3: Reference Resolution
  5. */
  6. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  7. import * as fs from 'fs';
  8. import * as path from 'path';
  9. import * as os from 'os';
  10. import { CodeGraph } from '../src';
  11. import { Node, UnresolvedReference } from '../src/types';
  12. import { ReferenceResolver, createResolver, ResolutionContext } from '../src/resolution';
  13. import { matchReference } from '../src/resolution/name-matcher';
  14. import { resolveImportPath, extractImportMappings, loadCppIncludeDirs, clearCppIncludeDirCache } from '../src/resolution/import-resolver';
  15. import { detectFrameworks, getAllFrameworkResolvers } from '../src/resolution/frameworks';
  16. import { QueryBuilder } from '../src/db/queries';
  17. import { DatabaseConnection } from '../src/db';
  18. describe('Resolution Module', () => {
  19. let tempDir: string;
  20. let cg: CodeGraph;
  21. beforeEach(() => {
  22. // Create temp directory
  23. tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-resolution-test-'));
  24. });
  25. afterEach(() => {
  26. // Clean up
  27. if (cg) {
  28. cg.destroy();
  29. } else if (fs.existsSync(tempDir)) {
  30. fs.rmSync(tempDir, { recursive: true });
  31. }
  32. });
  33. describe('Name Matcher', () => {
  34. it('should match exact name references', () => {
  35. // Create a mock context
  36. const mockNodes: Node[] = [
  37. {
  38. id: 'func:test.ts:myFunction:10',
  39. kind: 'function',
  40. name: 'myFunction',
  41. qualifiedName: 'test.ts::myFunction',
  42. filePath: 'test.ts',
  43. language: 'typescript',
  44. startLine: 10,
  45. endLine: 20,
  46. startColumn: 0,
  47. endColumn: 0,
  48. updatedAt: Date.now(),
  49. },
  50. ];
  51. const context: ResolutionContext = {
  52. getNodesInFile: () => mockNodes,
  53. getNodesByName: (name) => mockNodes.filter((n) => n.name === name),
  54. getNodesByQualifiedName: () => [],
  55. getNodesByKind: () => [],
  56. fileExists: () => true,
  57. readFile: () => null,
  58. getProjectRoot: () => '/test',
  59. getAllFiles: () => ['test.ts'],
  60. };
  61. const ref = {
  62. fromNodeId: 'caller:main.ts:caller:5',
  63. referenceName: 'myFunction',
  64. referenceKind: 'calls' as const,
  65. line: 5,
  66. column: 10,
  67. filePath: 'main.ts',
  68. language: 'typescript' as const,
  69. };
  70. const result = matchReference(ref, context);
  71. expect(result).not.toBeNull();
  72. expect(result?.targetNodeId).toBe('func:test.ts:myFunction:10');
  73. expect(result?.resolvedBy).toBe('exact-match');
  74. });
  75. it('should prefer same-module candidates over cross-module matches', () => {
  76. // Simulates a Python monorepo where multiple apps define navigate()
  77. const candidateA: Node = {
  78. id: 'func:apps/app_a/src/server.py:navigate:10',
  79. kind: 'function',
  80. name: 'navigate',
  81. qualifiedName: 'apps/app_a/src/server.py::navigate',
  82. filePath: 'apps/app_a/src/server.py',
  83. language: 'python',
  84. startLine: 10,
  85. endLine: 20,
  86. startColumn: 0,
  87. endColumn: 0,
  88. updatedAt: Date.now(),
  89. };
  90. const candidateB: Node = {
  91. id: 'func:apps/app_b/src/server.py:navigate:15',
  92. kind: 'function',
  93. name: 'navigate',
  94. qualifiedName: 'apps/app_b/src/server.py::navigate',
  95. filePath: 'apps/app_b/src/server.py',
  96. language: 'python',
  97. startLine: 15,
  98. endLine: 25,
  99. startColumn: 0,
  100. endColumn: 0,
  101. updatedAt: Date.now(),
  102. };
  103. const context: ResolutionContext = {
  104. getNodesInFile: () => [],
  105. getNodesByName: (name) => name === 'navigate' ? [candidateA, candidateB] : [],
  106. getNodesByQualifiedName: () => [],
  107. getNodesByKind: () => [],
  108. fileExists: () => true,
  109. readFile: () => null,
  110. getProjectRoot: () => '/test',
  111. getAllFiles: () => [],
  112. getNodesByLowerName: () => [],
  113. getImportMappings: () => [],
  114. };
  115. // Reference from app_a should resolve to app_a's navigate, not app_b's
  116. const ref = {
  117. fromNodeId: 'func:apps/app_a/src/handler.py:handler:5',
  118. referenceName: 'navigate',
  119. referenceKind: 'calls' as const,
  120. line: 5,
  121. column: 10,
  122. filePath: 'apps/app_a/src/handler.py',
  123. language: 'python' as const,
  124. };
  125. const result = matchReference(ref, context);
  126. expect(result).not.toBeNull();
  127. expect(result?.targetNodeId).toBe('func:apps/app_a/src/server.py:navigate:10');
  128. expect(result?.resolvedBy).toBe('exact-match');
  129. });
  130. it('should lower confidence for cross-module exact matches', () => {
  131. // Only one candidate but in a completely different module
  132. const candidates: Node[] = [
  133. {
  134. id: 'func:apps/app_b/src/server.py:navigate:10',
  135. kind: 'function',
  136. name: 'navigate',
  137. qualifiedName: 'apps/app_b/src/server.py::navigate',
  138. filePath: 'apps/app_b/src/server.py',
  139. language: 'python',
  140. startLine: 10,
  141. endLine: 20,
  142. startColumn: 0,
  143. endColumn: 0,
  144. updatedAt: Date.now(),
  145. },
  146. {
  147. id: 'func:apps/app_c/src/server.py:navigate:10',
  148. kind: 'function',
  149. name: 'navigate',
  150. qualifiedName: 'apps/app_c/src/server.py::navigate',
  151. filePath: 'apps/app_c/src/server.py',
  152. language: 'python',
  153. startLine: 10,
  154. endLine: 20,
  155. startColumn: 0,
  156. endColumn: 0,
  157. updatedAt: Date.now(),
  158. },
  159. ];
  160. const context: ResolutionContext = {
  161. getNodesInFile: () => [],
  162. getNodesByName: (name) => name === 'navigate' ? candidates : [],
  163. getNodesByQualifiedName: () => [],
  164. getNodesByKind: () => [],
  165. fileExists: () => true,
  166. readFile: () => null,
  167. getProjectRoot: () => '/test',
  168. getAllFiles: () => [],
  169. getNodesByLowerName: () => [],
  170. getImportMappings: () => [],
  171. };
  172. // Reference from app_a — neither candidate is in the same module
  173. const ref = {
  174. fromNodeId: 'func:apps/app_a/src/handler.py:handler:5',
  175. referenceName: 'navigate',
  176. referenceKind: 'calls' as const,
  177. line: 5,
  178. column: 10,
  179. filePath: 'apps/app_a/src/handler.py',
  180. language: 'python' as const,
  181. };
  182. const result = matchReference(ref, context);
  183. // Should still resolve but with low confidence
  184. expect(result).not.toBeNull();
  185. expect(result?.confidence).toBeLessThanOrEqual(0.4);
  186. });
  187. it('should match qualified name references', () => {
  188. const mockClassNode: Node = {
  189. id: 'class:user.ts:User:5',
  190. kind: 'class',
  191. name: 'User',
  192. qualifiedName: 'user.ts::User',
  193. filePath: 'user.ts',
  194. language: 'typescript',
  195. startLine: 5,
  196. endLine: 30,
  197. startColumn: 0,
  198. endColumn: 0,
  199. updatedAt: Date.now(),
  200. };
  201. const mockMethodNode: Node = {
  202. id: 'method:user.ts:User.save:15',
  203. kind: 'method',
  204. name: 'save',
  205. qualifiedName: 'user.ts::User::save',
  206. filePath: 'user.ts',
  207. language: 'typescript',
  208. startLine: 15,
  209. endLine: 25,
  210. startColumn: 0,
  211. endColumn: 0,
  212. updatedAt: Date.now(),
  213. };
  214. const context: ResolutionContext = {
  215. getNodesInFile: (fp) => fp === 'user.ts' ? [mockClassNode, mockMethodNode] : [],
  216. getNodesByName: (name) => {
  217. if (name === 'User') return [mockClassNode];
  218. if (name === 'save') return [mockMethodNode];
  219. return [];
  220. },
  221. getNodesByQualifiedName: (qn) => {
  222. if (qn === 'user.ts::User::save') return [mockMethodNode];
  223. return [];
  224. },
  225. getNodesByKind: () => [],
  226. fileExists: () => true,
  227. readFile: () => null,
  228. getProjectRoot: () => '/test',
  229. getAllFiles: () => ['user.ts'],
  230. };
  231. const ref = {
  232. fromNodeId: 'caller:main.ts:main:5',
  233. referenceName: 'User.save',
  234. referenceKind: 'calls' as const,
  235. line: 5,
  236. column: 10,
  237. filePath: 'main.ts',
  238. language: 'typescript' as const,
  239. };
  240. const result = matchReference(ref, context);
  241. expect(result).not.toBeNull();
  242. expect(result?.targetNodeId).toBe('method:user.ts:User.save:15');
  243. });
  244. });
  245. describe('Import Resolver', () => {
  246. it('should resolve relative import paths', () => {
  247. const context: ResolutionContext = {
  248. getNodesInFile: () => [],
  249. getNodesByName: () => [],
  250. getNodesByQualifiedName: () => [],
  251. getNodesByKind: () => [],
  252. fileExists: (p) => p === 'src/components/utils.ts' || p === 'src/components/utils/index.ts',
  253. readFile: () => null,
  254. getProjectRoot: () => '',
  255. getAllFiles: () => ['src/components/utils.ts', 'src/components/utils/index.ts'],
  256. };
  257. const result = resolveImportPath(
  258. './utils',
  259. 'src/components/Button.ts',
  260. 'typescript',
  261. context
  262. );
  263. expect(result).toBe('src/components/utils.ts');
  264. });
  265. it('should resolve parent directory imports', () => {
  266. const context: ResolutionContext = {
  267. getNodesInFile: () => [],
  268. getNodesByName: () => [],
  269. getNodesByQualifiedName: () => [],
  270. getNodesByKind: () => [],
  271. fileExists: (p) => p === 'src/helpers.ts' || p === 'src/helpers/index.ts',
  272. readFile: () => null,
  273. getProjectRoot: () => '',
  274. getAllFiles: () => ['src/helpers.ts', 'src/helpers/index.ts'],
  275. };
  276. const result = resolveImportPath(
  277. '../helpers',
  278. 'src/components/Button.ts',
  279. 'typescript',
  280. context
  281. );
  282. expect(result).toBe('src/helpers.ts');
  283. });
  284. it('should extract JS/TS import mappings', () => {
  285. const content = `
  286. import { foo } from './foo';
  287. import bar from '../bar';
  288. import * as utils from './utils';
  289. import { baz, qux } from './baz';
  290. `;
  291. const mappings = extractImportMappings(
  292. 'src/index.ts',
  293. content,
  294. 'typescript'
  295. );
  296. expect(mappings.length).toBeGreaterThan(0);
  297. expect(mappings.some((m) => m.localName === 'foo')).toBe(true);
  298. expect(mappings.some((m) => m.localName === 'bar')).toBe(true);
  299. });
  300. it('should extract Python import mappings', () => {
  301. const content = `
  302. from utils import helper
  303. from .models import User
  304. import os
  305. from ..services import auth_service
  306. `;
  307. const mappings = extractImportMappings(
  308. 'src/main.py',
  309. content,
  310. 'python'
  311. );
  312. expect(mappings.length).toBeGreaterThan(0);
  313. expect(mappings.some((m) => m.localName === 'helper')).toBe(true);
  314. expect(mappings.some((m) => m.localName === 'User')).toBe(true);
  315. });
  316. });
  317. describe('Framework Detection', () => {
  318. it('should detect React framework', () => {
  319. const context: ResolutionContext = {
  320. getNodesInFile: () => [],
  321. getNodesByName: () => [],
  322. getNodesByQualifiedName: () => [],
  323. getNodesByKind: () => [],
  324. fileExists: () => false,
  325. readFile: (p) => {
  326. if (p === 'package.json') {
  327. return JSON.stringify({
  328. dependencies: { react: '^18.0.0' },
  329. });
  330. }
  331. return null;
  332. },
  333. getProjectRoot: () => '/test',
  334. getAllFiles: () => ['package.json', 'src/App.tsx'],
  335. };
  336. const frameworks = detectFrameworks(context);
  337. expect(frameworks.some((f) => f.name === 'react')).toBe(true);
  338. });
  339. it('should detect Express framework', () => {
  340. const context: ResolutionContext = {
  341. getNodesInFile: () => [],
  342. getNodesByName: () => [],
  343. getNodesByQualifiedName: () => [],
  344. getNodesByKind: () => [],
  345. fileExists: () => false,
  346. readFile: (p) => {
  347. if (p === 'package.json') {
  348. return JSON.stringify({
  349. dependencies: { express: '^4.18.0' },
  350. });
  351. }
  352. return null;
  353. },
  354. getProjectRoot: () => '/test',
  355. getAllFiles: () => ['package.json', 'src/app.js'],
  356. };
  357. const frameworks = detectFrameworks(context);
  358. expect(frameworks.some((f) => f.name === 'express')).toBe(true);
  359. });
  360. it('should detect Laravel framework', () => {
  361. const context: ResolutionContext = {
  362. getNodesInFile: () => [],
  363. getNodesByName: () => [],
  364. getNodesByQualifiedName: () => [],
  365. getNodesByKind: () => [],
  366. fileExists: (p) => p === 'artisan',
  367. readFile: () => null,
  368. getProjectRoot: () => '/test',
  369. getAllFiles: () => ['artisan', 'app/Http/Kernel.php'],
  370. };
  371. const frameworks = detectFrameworks(context);
  372. expect(frameworks.some((f) => f.name === 'laravel')).toBe(true);
  373. });
  374. it('should return all framework resolvers', () => {
  375. const resolvers = getAllFrameworkResolvers();
  376. expect(resolvers.length).toBeGreaterThan(0);
  377. expect(resolvers.some((r) => r.name === 'react')).toBe(true);
  378. expect(resolvers.some((r) => r.name === 'express')).toBe(true);
  379. expect(resolvers.some((r) => r.name === 'laravel')).toBe(true);
  380. });
  381. });
  382. describe('React Framework Resolver', () => {
  383. it('should resolve React component references', () => {
  384. const mockNodes: Node[] = [
  385. {
  386. id: 'component:src/Button.tsx:Button:5',
  387. kind: 'component',
  388. name: 'Button',
  389. qualifiedName: 'src/Button.tsx::Button',
  390. filePath: 'src/Button.tsx',
  391. language: 'tsx',
  392. startLine: 5,
  393. endLine: 20,
  394. startColumn: 0,
  395. endColumn: 0,
  396. updatedAt: Date.now(),
  397. },
  398. ];
  399. const context: ResolutionContext = {
  400. getNodesInFile: (fp) => (fp === 'src/Button.tsx' ? mockNodes : []),
  401. getNodesByName: () => mockNodes,
  402. getNodesByQualifiedName: () => [],
  403. getNodesByKind: () => [],
  404. fileExists: () => false,
  405. readFile: (p) => {
  406. if (p === 'package.json') {
  407. return JSON.stringify({ dependencies: { react: '^18.0.0' } });
  408. }
  409. return null;
  410. },
  411. getProjectRoot: () => '/test',
  412. getAllFiles: () => ['package.json', 'src/Button.tsx', 'src/App.tsx'],
  413. };
  414. const frameworks = detectFrameworks(context);
  415. const reactResolver = frameworks.find((f) => f.name === 'react');
  416. expect(reactResolver).toBeDefined();
  417. const ref = {
  418. fromNodeId: 'component:src/App.tsx:App:1',
  419. referenceName: 'Button',
  420. referenceKind: 'renders' as const,
  421. line: 10,
  422. column: 5,
  423. filePath: 'src/App.tsx',
  424. language: 'typescript' as const,
  425. };
  426. const result = reactResolver!.resolve(ref, context);
  427. expect(result).not.toBeNull();
  428. expect(result?.targetNodeId).toBe('component:src/Button.tsx:Button:5');
  429. });
  430. it('should resolve custom hook references', () => {
  431. const mockNodes: Node[] = [
  432. {
  433. id: 'hook:src/hooks/useAuth.ts:useAuth:1',
  434. kind: 'function',
  435. name: 'useAuth',
  436. qualifiedName: 'src/hooks/useAuth.ts::useAuth',
  437. filePath: 'src/hooks/useAuth.ts',
  438. language: 'typescript',
  439. startLine: 1,
  440. endLine: 20,
  441. startColumn: 0,
  442. endColumn: 0,
  443. updatedAt: Date.now(),
  444. },
  445. ];
  446. const context: ResolutionContext = {
  447. getNodesInFile: (fp) => (fp.includes('useAuth') ? mockNodes : []),
  448. getNodesByName: () => mockNodes,
  449. getNodesByQualifiedName: () => [],
  450. getNodesByKind: () => [],
  451. fileExists: () => false,
  452. readFile: (p) => {
  453. if (p === 'package.json') {
  454. return JSON.stringify({ dependencies: { react: '^18.0.0' } });
  455. }
  456. return null;
  457. },
  458. getProjectRoot: () => '/test',
  459. getAllFiles: () => ['package.json', 'src/hooks/useAuth.ts'],
  460. };
  461. const frameworks = detectFrameworks(context);
  462. const reactResolver = frameworks.find((f) => f.name === 'react');
  463. const ref = {
  464. fromNodeId: 'component:src/App.tsx:App:1',
  465. referenceName: 'useAuth',
  466. referenceKind: 'calls' as const,
  467. line: 5,
  468. column: 10,
  469. filePath: 'src/App.tsx',
  470. language: 'typescript' as const,
  471. };
  472. const result = reactResolver!.resolve(ref, context);
  473. expect(result).not.toBeNull();
  474. expect(result?.targetNodeId).toBe('hook:src/hooks/useAuth.ts:useAuth:1');
  475. });
  476. });
  477. describe('Integration Tests', () => {
  478. it('should create resolver from CodeGraph instance', async () => {
  479. // Create a simple TypeScript project
  480. fs.writeFileSync(
  481. path.join(tempDir, 'package.json'),
  482. JSON.stringify({ name: 'test', dependencies: { react: '^18.0.0' } })
  483. );
  484. const srcDir = path.join(tempDir, 'src');
  485. fs.mkdirSync(srcDir);
  486. // Create utility file
  487. fs.writeFileSync(
  488. path.join(srcDir, 'utils.ts'),
  489. `export function formatDate(date: Date): string {
  490. return date.toISOString();
  491. }
  492. export function parseDate(str: string): Date {
  493. return new Date(str);
  494. }`
  495. );
  496. // Create main file that uses utils
  497. fs.writeFileSync(
  498. path.join(srcDir, 'main.ts'),
  499. `import { formatDate, parseDate } from './utils';
  500. function processDate(input: string): string {
  501. const date = parseDate(input);
  502. return formatDate(date);
  503. }`
  504. );
  505. // Initialize and index
  506. cg = await CodeGraph.init(tempDir, { index: true });
  507. // Check that resolver detected React framework
  508. const frameworks = cg.getDetectedFrameworks();
  509. expect(frameworks).toContain('react');
  510. // Get stats to verify indexing worked
  511. const stats = cg.getStats();
  512. expect(stats.fileCount).toBe(2);
  513. expect(stats.nodeCount).toBeGreaterThan(0);
  514. });
  515. it('should resolve references after indexing', async () => {
  516. // Create a project with references
  517. const srcDir = path.join(tempDir, 'src');
  518. fs.mkdirSync(srcDir, { recursive: true });
  519. fs.writeFileSync(
  520. path.join(srcDir, 'helper.ts'),
  521. `export function helperFunction(): void {
  522. console.log('helper');
  523. }`
  524. );
  525. fs.writeFileSync(
  526. path.join(srcDir, 'main.ts'),
  527. `import { helperFunction } from './helper';
  528. function main(): void {
  529. helperFunction();
  530. }`
  531. );
  532. cg = await CodeGraph.init(tempDir, { index: true });
  533. // Run reference resolution
  534. const result = cg.resolveReferences();
  535. // Should have attempted resolution
  536. expect(result.stats.total).toBeGreaterThanOrEqual(0);
  537. });
  538. it('promotes calls→instantiates when target resolves to a class (Python)', async () => {
  539. // Python has no `new` keyword — `Foo()` is the standard
  540. // instantiation syntax. Extraction can't tell that apart from
  541. // a function call without symbol info, so it emits a `calls`
  542. // ref. Resolution promotes it to `instantiates` once the
  543. // target is known to be a class.
  544. const srcDir = path.join(tempDir, 'src');
  545. fs.mkdirSync(srcDir, { recursive: true });
  546. fs.writeFileSync(
  547. path.join(srcDir, 'app.py'),
  548. `class UserService:
  549. def __init__(self):
  550. self.db = None
  551. def bootstrap():
  552. return UserService()
  553. `
  554. );
  555. cg = await CodeGraph.init(tempDir, { index: true });
  556. cg.resolveReferences();
  557. const bootstrap = cg
  558. .getNodesByKind('function')
  559. .find((n) => n.name === 'bootstrap');
  560. expect(bootstrap).toBeDefined();
  561. const outgoing = cg.getOutgoingEdges(bootstrap!.id);
  562. const instantiates = outgoing.find((e) => e.kind === 'instantiates');
  563. expect(instantiates).toBeDefined();
  564. // Same edge must NOT also appear as a `calls` edge — promotion
  565. // replaces the kind, doesn't duplicate.
  566. const callsToUserService = outgoing.filter(
  567. (e) => e.kind === 'calls' && e.target === instantiates!.target
  568. );
  569. expect(callsToUserService).toHaveLength(0);
  570. });
  571. it('resolves Go cross-package qualified calls via go.mod module path (#388)', async () => {
  572. // Pre-#388, every `pkga.FuncX(...)` call in a Go monorepo was flagged
  573. // external (isExternalImport returned true for any non-`/internal/`
  574. // import without `.`-prefix) and resolution fell through to name-match
  575. // with path proximity — recall on cross-package callers was ~<1%.
  576. fs.writeFileSync(
  577. path.join(tempDir, 'go.mod'),
  578. 'module github.com/example/myproject\n\ngo 1.21\n'
  579. );
  580. const pkgaDir = path.join(tempDir, 'pkga');
  581. const pkgbDir = path.join(tempDir, 'pkgb');
  582. const pkgcDir = path.join(tempDir, 'pkgc');
  583. fs.mkdirSync(pkgaDir);
  584. fs.mkdirSync(pkgbDir);
  585. fs.mkdirSync(pkgcDir);
  586. // Same-name exported function in two packages — only the imported one
  587. // should resolve. Exercises disambiguation, not just connectivity.
  588. fs.writeFileSync(
  589. path.join(pkgaDir, 'conv.go'),
  590. 'package pkga\nfunc Convert(x int) int { return x * 2 }\n'
  591. );
  592. fs.writeFileSync(
  593. path.join(pkgbDir, 'conv.go'),
  594. 'package pkgb\nfunc Convert(x int) int { return x + 1 }\n'
  595. );
  596. fs.writeFileSync(
  597. path.join(pkgcDir, 'use.go'),
  598. `package pkgc
  599. import "github.com/example/myproject/pkga"
  600. func UsePkga() {
  601. pkga.Convert(5)
  602. }
  603. `
  604. );
  605. cg = await CodeGraph.init(tempDir, { index: true });
  606. const usePkga = cg.getNodesByKind('function').filter((n) => n.name ==='UsePkga')[0];
  607. expect(usePkga).toBeDefined();
  608. const outgoing = cg.getOutgoingEdges(usePkga!.id);
  609. const callEdges = outgoing.filter((e) => e.kind === 'calls');
  610. expect(callEdges).toHaveLength(1);
  611. const target = cg.getNode(callEdges[0]!.target);
  612. expect(target?.name).toBe('Convert');
  613. // Critical: the resolver must pick the imported pkga's Convert,
  614. // not pkgb's. With the broken (pre-fix) resolver this lands on
  615. // whichever Convert happens to be cheaper under path proximity.
  616. expect(target?.filePath.replace(/\\/g, '/')).toBe('pkga/conv.go');
  617. });
  618. it('resolves Go aliased imports across packages (#388)', async () => {
  619. fs.writeFileSync(
  620. path.join(tempDir, 'go.mod'),
  621. 'module github.com/example/myproject\n\ngo 1.21\n'
  622. );
  623. fs.mkdirSync(path.join(tempDir, 'pkgb'));
  624. fs.mkdirSync(path.join(tempDir, 'pkgd'));
  625. fs.writeFileSync(
  626. path.join(tempDir, 'pkgb', 'lib.go'),
  627. 'package pkgb\nfunc Compute(x int) int { return x }\n'
  628. );
  629. fs.writeFileSync(
  630. path.join(tempDir, 'pkgd', 'use.go'),
  631. `package pkgd
  632. import (
  633. "fmt"
  634. alias "github.com/example/myproject/pkgb"
  635. )
  636. func UseAliased() {
  637. fmt.Println("hi")
  638. alias.Compute(3)
  639. }
  640. `
  641. );
  642. cg = await CodeGraph.init(tempDir, { index: true });
  643. const useAliased = cg.getNodesByKind('function').filter((n) => n.name ==='UseAliased')[0];
  644. expect(useAliased).toBeDefined();
  645. const calls = cg.getOutgoingEdges(useAliased!.id).filter((e) => e.kind === 'calls');
  646. // fmt.Println is stdlib — must stay external. alias.Compute must resolve.
  647. expect(calls).toHaveLength(1);
  648. const target = cg.getNode(calls[0]!.target);
  649. expect(target?.name).toBe('Compute');
  650. expect(target?.filePath.replace(/\\/g, '/')).toBe('pkgb/lib.go');
  651. });
  652. it('TS type_alias object-shape members resolve method calls (#359)', async () => {
  653. // Pre-#359, `recorder.stop()` (recorder: RecorderHandle) attached
  654. // to `StdioMcpClient.stop` in a sibling directory via path-proximity
  655. // because the type_alias had no `stop` node — only the unrelated
  656. // class did. Now type_alias produces member nodes (property/method),
  657. // so the camelCase receiver↔type word overlap pulls the call to
  658. // `RecorderHandle::stop` instead of the look-alike class.
  659. fs.mkdirSync(path.join(tempDir, 'voice'));
  660. fs.mkdirSync(path.join(tempDir, 'codegraph'));
  661. fs.writeFileSync(
  662. path.join(tempDir, 'voice', 'recorder.ts'),
  663. `export type RecorderHandle = {
  664. wavPath: string;
  665. stop: () => Promise<{ ok: true }>;
  666. };
  667. `
  668. );
  669. fs.writeFileSync(
  670. path.join(tempDir, 'voice', 'controller.ts'),
  671. `import type { RecorderHandle } from "./recorder";
  672. export async function finaliseRecording(recorder: RecorderHandle) {
  673. return await recorder.stop();
  674. }
  675. `
  676. );
  677. fs.writeFileSync(
  678. path.join(tempDir, 'codegraph', 'stdio-client.ts'),
  679. `export class StdioMcpClient {
  680. private stopped = false;
  681. async stop(): Promise<void> { this.stopped = true; }
  682. }
  683. `
  684. );
  685. cg = await CodeGraph.init(tempDir, { index: true });
  686. const handleStop = cg
  687. .getNodesByKind('method')
  688. .find((n) => n.qualifiedName === 'RecorderHandle::stop');
  689. expect(handleStop).toBeDefined();
  690. const clientStop = cg
  691. .getNodesByKind('method')
  692. .find((n) => n.qualifiedName === 'StdioMcpClient::stop');
  693. expect(clientStop).toBeDefined();
  694. const handleCallers = cg.getIncomingEdges(handleStop!.id).filter((e) => e.kind === 'calls');
  695. const clientCallers = cg.getIncomingEdges(clientStop!.id).filter((e) => e.kind === 'calls');
  696. expect(handleCallers.length).toBeGreaterThanOrEqual(1);
  697. // The class method must have NO callers — voice/'s call must NOT
  698. // mis-attribute. A non-empty list would mean the false-positive
  699. // path is still firing.
  700. expect(clientCallers).toHaveLength(0);
  701. // Function-typed property surfaces as a `method` node, not `property`,
  702. // because `stop()` semantics at the call site are method semantics.
  703. expect(handleStop!.kind).toBe('method');
  704. });
  705. it('Java import disambiguates same-name classes across modules (#314)', async () => {
  706. // Pre-#314 the import resolver had no Java branch at all, so a
  707. // multi-module Maven repo where `dao/converter/FooConverter` and
  708. // `service/converter/FooConverter` both export a `convert` method
  709. // resolved by file-path proximity — picking whichever class was
  710. // closer to the caller, which is wrong any time the caller lives
  711. // in an equidistant cross-cutting module.
  712. const daoDir = path.join(tempDir, 'dao/src/main/java/com/example/dao/converter');
  713. const serviceDir = path.join(tempDir, 'service/src/main/java/com/example/service/converter');
  714. const webDir = path.join(tempDir, 'web/src/main/java/com/example/web');
  715. fs.mkdirSync(daoDir, { recursive: true });
  716. fs.mkdirSync(serviceDir, { recursive: true });
  717. fs.mkdirSync(webDir, { recursive: true });
  718. fs.writeFileSync(
  719. path.join(daoDir, 'FooConverter.java'),
  720. `package com.example.dao.converter;
  721. public class FooConverter { public String convert(String x) { return "dao:" + x; } }
  722. `
  723. );
  724. fs.writeFileSync(
  725. path.join(serviceDir, 'FooConverter.java'),
  726. `package com.example.service.converter;
  727. public class FooConverter { public String convert(String x) { return "svc:" + x; } }
  728. `
  729. );
  730. // The caller imports the SERVICE version — even though dao is
  731. // alphabetically/lexically first in the candidate list, the
  732. // import must trump that order.
  733. fs.writeFileSync(
  734. path.join(webDir, 'Handler.java'),
  735. `package com.example.web;
  736. import com.example.service.converter.FooConverter;
  737. public class Handler {
  738. private FooConverter fooConverter;
  739. public String use() { return fooConverter.convert("input"); }
  740. }
  741. `
  742. );
  743. cg = await CodeGraph.init(tempDir, { index: true });
  744. const use = cg
  745. .getNodesByKind('method')
  746. .find((n) => n.qualifiedName === 'Handler::use');
  747. expect(use).toBeDefined();
  748. const calls = cg.getOutgoingEdges(use!.id).filter((e) => e.kind === 'calls');
  749. expect(calls.length).toBeGreaterThanOrEqual(1);
  750. const target = cg.getNode(calls[0]!.target);
  751. expect(target?.name).toBe('convert');
  752. expect(target?.filePath.replace(/\\/g, '/')).toBe(
  753. 'service/src/main/java/com/example/service/converter/FooConverter.java'
  754. );
  755. });
  756. it('C# extracts references from method/property/field types (#381)', async () => {
  757. // Pre-#381, every C# project produced ZERO `references` edges:
  758. // csharp.ts was missing returnField, and the type-leaf walker
  759. // only recognized TS/Java's `type_identifier` nodes — C# uses
  760. // `identifier`/`predefined_type`/`qualified_name`/`generic_name`.
  761. const srcDir = path.join(tempDir, 'src');
  762. fs.mkdirSync(srcDir, { recursive: true });
  763. fs.writeFileSync(
  764. path.join(srcDir, 'Dtos.cs'),
  765. `namespace MyApp;
  766. public class SessionInfoDto { public string Id { get; set; } = ""; }
  767. public class UserDto { public string Name { get; set; } = ""; }
  768. `
  769. );
  770. fs.writeFileSync(
  771. path.join(srcDir, 'Service.cs'),
  772. `using System.Threading.Tasks;
  773. namespace MyApp;
  774. public class DataExporter
  775. {
  776. public SessionInfoDto Build(UserDto user, SessionInfoDto session) { return session; }
  777. public Task<SessionInfoDto> BuildAsync(UserDto user) { return Task.FromResult(new SessionInfoDto()); }
  778. public SessionInfoDto Latest { get; set; } = new();
  779. private UserDto _cached;
  780. }
  781. `
  782. );
  783. cg = await CodeGraph.init(tempDir, { index: true });
  784. const sessionDto = cg
  785. .getNodesByKind('class')
  786. .find((n) => n.name === 'SessionInfoDto');
  787. const userDto = cg
  788. .getNodesByKind('class')
  789. .find((n) => n.name === 'UserDto');
  790. expect(sessionDto).toBeDefined();
  791. expect(userDto).toBeDefined();
  792. const sessionIncoming = cg
  793. .getIncomingEdges(sessionDto!.id)
  794. .filter((e) => e.kind === 'references');
  795. const userIncoming = cg
  796. .getIncomingEdges(userDto!.id)
  797. .filter((e) => e.kind === 'references');
  798. // SessionInfoDto: Build return, Build param, BuildAsync return (inside Task<>), Latest property.
  799. // UserDto: Build param, BuildAsync param, _cached field.
  800. expect(sessionIncoming.length).toBeGreaterThanOrEqual(4);
  801. expect(userIncoming.length).toBeGreaterThanOrEqual(3);
  802. });
  803. it('Go: leaves stdlib calls (fmt.Println, etc.) external', async () => {
  804. fs.writeFileSync(
  805. path.join(tempDir, 'go.mod'),
  806. 'module github.com/example/myproject\n\ngo 1.21\n'
  807. );
  808. fs.writeFileSync(
  809. path.join(tempDir, 'main.go'),
  810. `package main
  811. import "fmt"
  812. func main() {
  813. fmt.Println("hi")
  814. }
  815. `
  816. );
  817. cg = await CodeGraph.init(tempDir, { index: true });
  818. const mainFn = cg.getNodesByKind('function').filter((n) => n.name ==='main')[0];
  819. const calls = cg.getOutgoingEdges(mainFn!.id).filter((e) => e.kind === 'calls');
  820. // No spurious in-project edge — fmt.* must stay unresolved/external.
  821. expect(calls).toHaveLength(0);
  822. });
  823. });
  824. describe('Name Matcher: kind bias for new ref kinds', () => {
  825. const baseContext = (candidates: Node[]): ResolutionContext => ({
  826. getNodesInFile: () => [],
  827. getNodesByName: (name) => candidates.filter((c) => c.name === name),
  828. getNodesByQualifiedName: () => [],
  829. getNodesByKind: () => [],
  830. fileExists: () => true,
  831. readFile: () => null,
  832. getProjectRoot: () => '/test',
  833. getAllFiles: () => [],
  834. getNodesByLowerName: () => [],
  835. getImportMappings: () => [],
  836. });
  837. it('prefers a class candidate over a function for `instantiates` refs', () => {
  838. // A class and a function share a name across the codebase.
  839. // Without the kind bias, the function (which gets the +25 `calls`
  840. // bonus historically applied to all candidates of that kind) would
  841. // win. Now the instantiates branch reverses it.
  842. const fn: Node = {
  843. id: 'func:utils.ts:Logger:5', kind: 'function', name: 'Logger',
  844. qualifiedName: 'utils.ts::Logger', filePath: 'utils.ts', language: 'typescript',
  845. startLine: 5, endLine: 7, startColumn: 0, endColumn: 0, updatedAt: Date.now(),
  846. };
  847. const cls: Node = {
  848. id: 'class:logger.ts:Logger:10', kind: 'class', name: 'Logger',
  849. qualifiedName: 'logger.ts::Logger', filePath: 'logger.ts', language: 'typescript',
  850. startLine: 10, endLine: 30, startColumn: 0, endColumn: 0, updatedAt: Date.now(),
  851. };
  852. const ref = {
  853. fromNodeId: 'func:main.ts:bootstrap:1',
  854. referenceName: 'Logger',
  855. referenceKind: 'instantiates' as const,
  856. line: 5, column: 0, filePath: 'main.ts', language: 'typescript' as const,
  857. };
  858. const result = matchReference(ref, baseContext([fn, cls]));
  859. expect(result?.targetNodeId).toBe('class:logger.ts:Logger:10');
  860. });
  861. it('prefers a function candidate over a non-function for `decorates` refs', () => {
  862. const variable: Node = {
  863. id: 'var:config.ts:Inject:5', kind: 'variable', name: 'Inject',
  864. qualifiedName: 'config.ts::Inject', filePath: 'config.ts', language: 'typescript',
  865. startLine: 5, endLine: 5, startColumn: 0, endColumn: 0, updatedAt: Date.now(),
  866. };
  867. const decorator: Node = {
  868. id: 'func:di.ts:Inject:10', kind: 'function', name: 'Inject',
  869. qualifiedName: 'di.ts::Inject', filePath: 'di.ts', language: 'typescript',
  870. startLine: 10, endLine: 20, startColumn: 0, endColumn: 0, updatedAt: Date.now(),
  871. };
  872. const ref = {
  873. fromNodeId: 'class:svc.ts:UserService:1',
  874. referenceName: 'Inject',
  875. referenceKind: 'decorates' as const,
  876. line: 5, column: 0, filePath: 'svc.ts', language: 'typescript' as const,
  877. };
  878. const result = matchReference(ref, baseContext([variable, decorator]));
  879. expect(result?.targetNodeId).toBe('func:di.ts:Inject:10');
  880. });
  881. });
  882. describe('tsconfig path aliases', () => {
  883. it('resolves an aliased import to the alias-mapped file (not a same-named file elsewhere)', async () => {
  884. // Two same-named exports in different directories. Without alias
  885. // resolution, name-matcher would pick whichever it finds first;
  886. // with alias resolution, the import path uniquely picks one.
  887. fs.mkdirSync(path.join(tempDir, 'src/utils'), { recursive: true });
  888. fs.mkdirSync(path.join(tempDir, 'src/legacy'), { recursive: true });
  889. fs.writeFileSync(
  890. path.join(tempDir, 'src/utils/format.ts'),
  891. `export function pickMe(): number { return 1; }\n`
  892. );
  893. fs.writeFileSync(
  894. path.join(tempDir, 'src/legacy/format.ts'),
  895. `export function pickMe(): number { return 99; }\n`
  896. );
  897. fs.writeFileSync(
  898. path.join(tempDir, 'src/main.ts'),
  899. `import { pickMe } from '@utils/format';\nexport function go(): number { return pickMe(); }\n`
  900. );
  901. fs.writeFileSync(
  902. path.join(tempDir, 'tsconfig.json'),
  903. JSON.stringify({
  904. compilerOptions: {
  905. baseUrl: './src',
  906. paths: { '@utils/*': ['utils/*'] },
  907. },
  908. })
  909. );
  910. cg = await CodeGraph.init(tempDir, { index: true });
  911. cg.resolveReferences();
  912. // The two pickMe nodes live in different files. The aliased
  913. // import should attach the call edge to the @utils-mapped one,
  914. // not the legacy duplicate.
  915. const all = cg.getNodesByKind('function').filter((n) => n.name === 'pickMe');
  916. const utilsNode = all.find((n) => n.filePath === 'src/utils/format.ts');
  917. const legacyNode = all.find((n) => n.filePath === 'src/legacy/format.ts');
  918. expect(utilsNode).toBeDefined();
  919. expect(legacyNode).toBeDefined();
  920. const utilsCallers = cg.getCallers(utilsNode!.id);
  921. const legacyCallers = cg.getCallers(legacyNode!.id);
  922. expect(utilsCallers.length).toBeGreaterThan(0);
  923. expect(utilsCallers.some((c) => c.node.filePath === 'src/main.ts')).toBe(true);
  924. // The legacy node should NOT have a caller from src/main.ts —
  925. // the alias correctly picked the utils version.
  926. expect(legacyCallers.some((c) => c.node.filePath === 'src/main.ts')).toBe(false);
  927. });
  928. it('falls back gracefully when tsconfig is absent', async () => {
  929. fs.mkdirSync(path.join(tempDir, 'src'), { recursive: true });
  930. fs.writeFileSync(
  931. path.join(tempDir, 'src/a.ts'),
  932. `export function aFn(): void {}\n`
  933. );
  934. fs.writeFileSync(
  935. path.join(tempDir, 'src/b.ts'),
  936. `import { aFn } from './a';\nexport function bFn(): void { aFn(); }\n`
  937. );
  938. cg = await CodeGraph.init(tempDir, { index: true });
  939. // No tsconfig present — index should still complete and the
  940. // relative-import-based call edge should be created.
  941. const aFn = cg.getNodesByKind('function').find((n) => n.name === 'aFn');
  942. expect(aFn).toBeDefined();
  943. const callers = cg.getCallers(aFn!.id);
  944. expect(callers.some((c) => c.node.filePath === 'src/b.ts')).toBe(true);
  945. });
  946. });
  947. describe('re-export chain following', () => {
  948. it('chases a 3-hop barrel chain (wildcard → named → declaration)', async () => {
  949. // main.ts → all.ts (wildcard) → index.ts (named) → auth.ts (declaration).
  950. // Without chain following, `signIn` resolves to nothing because
  951. // none of the barrel files declare it directly.
  952. fs.mkdirSync(path.join(tempDir, 'src/services'), { recursive: true });
  953. fs.writeFileSync(
  954. path.join(tempDir, 'src/services/auth.ts'),
  955. `export function signIn(): void {}\n`
  956. );
  957. fs.writeFileSync(
  958. path.join(tempDir, 'src/services/index.ts'),
  959. `export { signIn } from './auth';\n`
  960. );
  961. fs.writeFileSync(
  962. path.join(tempDir, 'src/all.ts'),
  963. `export * from './services/index';\n`
  964. );
  965. fs.writeFileSync(
  966. path.join(tempDir, 'src/main.ts'),
  967. `import { signIn } from './all';\nexport function go(): void { signIn(); }\n`
  968. );
  969. cg = await CodeGraph.init(tempDir, { index: true });
  970. cg.resolveReferences();
  971. const signInNode = cg
  972. .getNodesByKind('function')
  973. .find((n) => n.name === 'signIn' && n.filePath === 'src/services/auth.ts');
  974. expect(signInNode).toBeDefined();
  975. const callers = cg.getCallers(signInNode!.id);
  976. expect(callers.some((c) => c.node.filePath === 'src/main.ts')).toBe(true);
  977. });
  978. it('follows a renamed named re-export (export { foo as bar } from ...)', async () => {
  979. // The chase has to look up `foo` in the upstream module even
  980. // though the importer asked for `bar` — exercises the rename
  981. // branch of findExportedSymbol.
  982. fs.mkdirSync(path.join(tempDir, 'src'), { recursive: true });
  983. fs.writeFileSync(
  984. path.join(tempDir, 'src/auth.ts'),
  985. `export function signIn(): void {}\n`
  986. );
  987. fs.writeFileSync(
  988. path.join(tempDir, 'src/index.ts'),
  989. `export { signIn as login } from './auth';\n`
  990. );
  991. fs.writeFileSync(
  992. path.join(tempDir, 'src/main.ts'),
  993. `import { login } from './index';\nexport function go(): void { login(); }\n`
  994. );
  995. cg = await CodeGraph.init(tempDir, { index: true });
  996. cg.resolveReferences();
  997. const signInNode = cg
  998. .getNodesByKind('function')
  999. .find((n) => n.name === 'signIn' && n.filePath === 'src/auth.ts');
  1000. expect(signInNode).toBeDefined();
  1001. const callers = cg.getCallers(signInNode!.id);
  1002. expect(callers.some((c) => c.node.filePath === 'src/main.ts')).toBe(true);
  1003. });
  1004. });
  1005. describe('C/C++ Import Resolution', () => {
  1006. afterEach(() => {
  1007. clearCppIncludeDirCache();
  1008. });
  1009. it('should resolve C include to header in same directory', () => {
  1010. const context: ResolutionContext = {
  1011. getNodesInFile: () => [],
  1012. getNodesByName: () => [],
  1013. getNodesByQualifiedName: () => [],
  1014. getNodesByKind: () => [],
  1015. fileExists: (p) => p === 'utils.h',
  1016. readFile: () => null,
  1017. getProjectRoot: () => '',
  1018. getAllFiles: () => ['utils.h', 'main.c'],
  1019. };
  1020. const result = resolveImportPath(
  1021. 'utils.h',
  1022. 'main.c',
  1023. 'c',
  1024. context
  1025. );
  1026. expect(result).toBe('utils.h');
  1027. });
  1028. it('should resolve C++ include with .hpp extension', () => {
  1029. const context: ResolutionContext = {
  1030. getNodesInFile: () => [],
  1031. getNodesByName: () => [],
  1032. getNodesByQualifiedName: () => [],
  1033. getNodesByKind: () => [],
  1034. fileExists: (p) => p === 'include/myclass.hpp',
  1035. readFile: () => null,
  1036. getProjectRoot: () => '',
  1037. getAllFiles: () => ['include/myclass.hpp', 'src/main.cpp'],
  1038. getCppIncludeDirs: () => ['include'],
  1039. };
  1040. const result = resolveImportPath(
  1041. 'myclass.hpp',
  1042. 'src/main.cpp',
  1043. 'cpp',
  1044. context
  1045. );
  1046. expect(result).toBe('include/myclass.hpp');
  1047. });
  1048. it('should resolve include with subdirectory path', () => {
  1049. const context: ResolutionContext = {
  1050. getNodesInFile: () => [],
  1051. getNodesByName: () => [],
  1052. getNodesByQualifiedName: () => [],
  1053. getNodesByKind: () => [],
  1054. fileExists: (p) => p === 'utils/helpers.h',
  1055. readFile: () => null,
  1056. getProjectRoot: () => '',
  1057. getAllFiles: () => ['utils/helpers.h', 'main.c'],
  1058. };
  1059. const result = resolveImportPath(
  1060. 'utils/helpers.h',
  1061. 'main.c',
  1062. 'c',
  1063. context
  1064. );
  1065. expect(result).toBe('utils/helpers.h');
  1066. });
  1067. it('should resolve include via include directories', () => {
  1068. const context: ResolutionContext = {
  1069. getNodesInFile: () => [],
  1070. getNodesByName: () => [],
  1071. getNodesByQualifiedName: () => [],
  1072. getNodesByKind: () => [],
  1073. fileExists: (p) => p === 'include/myheader.h',
  1074. readFile: () => null,
  1075. getProjectRoot: () => '',
  1076. getAllFiles: () => ['include/myheader.h', 'src/main.cpp'],
  1077. getCppIncludeDirs: () => ['include'],
  1078. };
  1079. const result = resolveImportPath(
  1080. 'myheader.h',
  1081. 'src/main.cpp',
  1082. 'cpp',
  1083. context
  1084. );
  1085. expect(result).toBe('include/myheader.h');
  1086. });
  1087. it('should resolve include trying multiple extensions', () => {
  1088. const context: ResolutionContext = {
  1089. getNodesInFile: () => [],
  1090. getNodesByName: () => [],
  1091. getNodesByQualifiedName: () => [],
  1092. getNodesByKind: () => [],
  1093. // myclass.h does not exist, but myclass.hpp does
  1094. fileExists: (p) => p === 'include/myclass.hpp',
  1095. readFile: () => null,
  1096. getProjectRoot: () => '',
  1097. getAllFiles: () => ['include/myclass.hpp', 'src/main.cpp'],
  1098. getCppIncludeDirs: () => ['include'],
  1099. };
  1100. const result = resolveImportPath(
  1101. 'myclass',
  1102. 'src/main.cpp',
  1103. 'cpp',
  1104. context
  1105. );
  1106. expect(result).toBe('include/myclass.hpp');
  1107. });
  1108. it('should return null for system headers', () => {
  1109. const context: ResolutionContext = {
  1110. getNodesInFile: () => [],
  1111. getNodesByName: () => [],
  1112. getNodesByQualifiedName: () => [],
  1113. getNodesByKind: () => [],
  1114. fileExists: () => true,
  1115. readFile: () => null,
  1116. getProjectRoot: () => '',
  1117. getAllFiles: () => [],
  1118. };
  1119. // C standard library header
  1120. expect(resolveImportPath('stdio.h', 'main.c', 'c', context)).toBeNull();
  1121. // C++ standard library header
  1122. expect(resolveImportPath('vector', 'main.cpp', 'cpp', context)).toBeNull();
  1123. // C++ C-wrapper header
  1124. expect(resolveImportPath('cstdio', 'main.cpp', 'cpp', context)).toBeNull();
  1125. });
  1126. it('should return null for single-component third-party paths that cannot be resolved', () => {
  1127. const context: ResolutionContext = {
  1128. getNodesInFile: () => [],
  1129. getNodesByName: () => [],
  1130. getNodesByQualifiedName: () => [],
  1131. getNodesByKind: () => [],
  1132. fileExists: () => false,
  1133. readFile: () => null,
  1134. getProjectRoot: () => '',
  1135. getAllFiles: () => [],
  1136. getCppIncludeDirs: () => [],
  1137. };
  1138. // Third-party bare header without path — not resolvable, returns null
  1139. const result = resolveImportPath(
  1140. 'openssl/ssl.h',
  1141. 'main.cpp',
  1142. 'cpp',
  1143. context
  1144. );
  1145. expect(result).toBeNull();
  1146. });
  1147. it('should not filter project headers with path separators', () => {
  1148. const context: ResolutionContext = {
  1149. getNodesInFile: () => [],
  1150. getNodesByName: () => [],
  1151. getNodesByQualifiedName: () => [],
  1152. getNodesByKind: () => [],
  1153. fileExists: (p) => p === 'mylib/utils.h',
  1154. readFile: () => null,
  1155. getProjectRoot: () => '',
  1156. getAllFiles: () => ['mylib/utils.h'],
  1157. };
  1158. // Path with separator should NOT be filtered as external
  1159. const result = resolveImportPath(
  1160. 'mylib/utils.h',
  1161. 'main.c',
  1162. 'c',
  1163. context
  1164. );
  1165. expect(result).toBe('mylib/utils.h');
  1166. });
  1167. it('should extract C/C++ import mappings from #include directives', () => {
  1168. const code = `#include <iostream>
  1169. #include "myheader.h"
  1170. #include "utils/helpers.hpp"`;
  1171. const mappings = extractImportMappings('main.cpp', code, 'cpp');
  1172. expect(mappings.length).toBe(3);
  1173. expect(mappings[0]).toEqual({
  1174. localName: 'iostream',
  1175. exportedName: '*',
  1176. source: 'iostream',
  1177. isDefault: false,
  1178. isNamespace: true,
  1179. });
  1180. expect(mappings[1]).toEqual({
  1181. localName: 'myheader',
  1182. exportedName: '*',
  1183. source: 'myheader.h',
  1184. isDefault: false,
  1185. isNamespace: true,
  1186. });
  1187. expect(mappings[2]).toEqual({
  1188. localName: 'helpers',
  1189. exportedName: '*',
  1190. source: 'utils/helpers.hpp',
  1191. isDefault: false,
  1192. isNamespace: true,
  1193. });
  1194. });
  1195. it('should discover include directories from compile_commands.json', () => {
  1196. // Create a temp project with compile_commands.json
  1197. const tempProject = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-cpp-test-'));
  1198. try {
  1199. const compileDb = [
  1200. {
  1201. directory: tempProject,
  1202. command: 'g++ -Iinclude -Isrc/lib -isystem /usr/include -c src/main.cpp',
  1203. file: 'src/main.cpp',
  1204. },
  1205. ];
  1206. fs.writeFileSync(
  1207. path.join(tempProject, 'compile_commands.json'),
  1208. JSON.stringify(compileDb)
  1209. );
  1210. // Create the include dirs so they exist
  1211. fs.mkdirSync(path.join(tempProject, 'include'), { recursive: true });
  1212. fs.mkdirSync(path.join(tempProject, 'src', 'lib'), { recursive: true });
  1213. clearCppIncludeDirCache();
  1214. const dirs = loadCppIncludeDirs(tempProject);
  1215. // Should find include and src/lib (relative to project root)
  1216. // /usr/include is absolute and outside project, should be excluded
  1217. expect(dirs).toContain('include');
  1218. expect(dirs).toContain('src/lib');
  1219. expect(dirs.some(d => d.includes('usr'))).toBe(false);
  1220. } finally {
  1221. fs.rmSync(tempProject, { recursive: true });
  1222. }
  1223. });
  1224. it('should fall back to heuristic include dirs when no compile_commands.json', () => {
  1225. const tempProject = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-cpp-test-'));
  1226. try {
  1227. // Create include/ and src/ directories with headers
  1228. fs.mkdirSync(path.join(tempProject, 'include'), { recursive: true });
  1229. fs.writeFileSync(path.join(tempProject, 'include', 'types.h'), '');
  1230. fs.mkdirSync(path.join(tempProject, 'src'), { recursive: true });
  1231. fs.writeFileSync(path.join(tempProject, 'src', 'main.cpp'), '');
  1232. // Create a directory without headers — should not be included
  1233. fs.mkdirSync(path.join(tempProject, 'docs'), { recursive: true });
  1234. clearCppIncludeDirCache();
  1235. const dirs = loadCppIncludeDirs(tempProject);
  1236. expect(dirs).toContain('include');
  1237. expect(dirs).toContain('src');
  1238. expect(dirs).not.toContain('docs');
  1239. } finally {
  1240. fs.rmSync(tempProject, { recursive: true });
  1241. }
  1242. });
  1243. // Documents the cross-language `.h` behavior. Objective-C and C++ share
  1244. // the `.h` extension, so in a mixed iOS-style project an Obj-C header
  1245. // dir gets claimed as a C/C++ include dir too. That's intentional — a
  1246. // C++ file legitimately can `#include "Foo.h"` against an Obj-C header
  1247. // (Obj-C++ / .mm callers), and false-positive inclusion is far cheaper
  1248. // than missing real resolutions. The test pins this so a later
  1249. // "exclude objc dirs" refactor breaks loudly and reviewers see the
  1250. // trade-off explicitly.
  1251. it('heuristic claims any top-level dir containing .h files, including Obj-C', () => {
  1252. const tempProject = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-cpp-test-'));
  1253. try {
  1254. // C++ side: an `cppmod` dir with a .hpp (C++-only extension)
  1255. fs.mkdirSync(path.join(tempProject, 'cppmod'), { recursive: true });
  1256. fs.writeFileSync(path.join(tempProject, 'cppmod', 'shared.hpp'), '');
  1257. // Obj-C side: an `iosmod` dir with .h + .m (no .cpp/.hpp).
  1258. fs.mkdirSync(path.join(tempProject, 'iosmod'), { recursive: true });
  1259. fs.writeFileSync(path.join(tempProject, 'iosmod', 'View.h'), '');
  1260. fs.writeFileSync(path.join(tempProject, 'iosmod', 'View.m'), '');
  1261. clearCppIncludeDirCache();
  1262. const dirs = loadCppIncludeDirs(tempProject);
  1263. // Both included — Obj-C dirs are intentionally allowed.
  1264. expect(dirs).toContain('cppmod');
  1265. expect(dirs).toContain('iosmod');
  1266. } finally {
  1267. fs.rmSync(tempProject, { recursive: true });
  1268. }
  1269. });
  1270. // End-to-end: ensure `#include "X.h"` produces a file→file `imports` edge
  1271. // in the actual indexing pipeline (not just a phantom file→import-node
  1272. // edge). This pins the include-dir resolution path so the headline PR
  1273. // feature can't silently regress to a no-op in the indexing flow.
  1274. it('connects #include to the real header file via include-dir scan (end-to-end)', async () => {
  1275. const tempProject = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-cpp-e2e-'));
  1276. try {
  1277. fs.mkdirSync(path.join(tempProject, 'include'), { recursive: true });
  1278. fs.mkdirSync(path.join(tempProject, 'src'), { recursive: true });
  1279. fs.writeFileSync(
  1280. path.join(tempProject, 'include', 'utils.h'),
  1281. `#ifndef UTILS_H\n#define UTILS_H\nint add(int, int);\n#endif\n`
  1282. );
  1283. fs.writeFileSync(
  1284. path.join(tempProject, 'src', 'main.cpp'),
  1285. `#include "utils.h"\n#include <vector>\nint main(){ return add(1,2); }\n`
  1286. );
  1287. clearCppIncludeDirCache();
  1288. cg = await CodeGraph.init(tempProject, { index: true });
  1289. // Sanity: file nodes exist for the header and the cpp.
  1290. const allFiles = cg.getStats();
  1291. expect(allFiles.fileCount).toBe(2);
  1292. // The `#include "utils.h"` edge should target the real
  1293. // `include/utils.h` file node — not a floating `import` node
  1294. // living inside main.cpp.
  1295. const db = DatabaseConnection.open(path.join(tempProject, '.codegraph', 'codegraph.db'));
  1296. const rows = db.getDb().prepare(`
  1297. select dst.kind as dstKind, dst.file_path as dstPath
  1298. from edges e
  1299. join nodes src on e.source = src.id
  1300. join nodes dst on e.target = dst.id
  1301. where e.kind = 'imports'
  1302. and src.kind = 'file'
  1303. and src.file_path = 'src/main.cpp'
  1304. `).all() as Array<{ dstKind: string; dstPath: string }>;
  1305. const resolvedToHeader = rows.find(
  1306. (r) => r.dstKind === 'file' && r.dstPath === 'include/utils.h'
  1307. );
  1308. expect(resolvedToHeader, 'main.cpp → include/utils.h imports edge missing').toBeDefined();
  1309. // `<vector>` should NOT produce a file edge — it's a stdlib header.
  1310. const stdlibFile = rows.find(
  1311. (r) => r.dstKind === 'file' && r.dstPath && r.dstPath.endsWith('vector')
  1312. );
  1313. expect(stdlibFile).toBeUndefined();
  1314. } finally {
  1315. fs.rmSync(tempProject, { recursive: true, force: true });
  1316. }
  1317. });
  1318. });
  1319. });