resolution.test.ts 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886
  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, resolveJvmImport, loadCppIncludeDirs, clearCppIncludeDirCache } from '../src/resolution/import-resolver';
  15. import type { UnresolvedRef } from '../src/resolution/types';
  16. import { detectFrameworks, getAllFrameworkResolvers } from '../src/resolution/frameworks';
  17. import { QueryBuilder } from '../src/db/queries';
  18. import { DatabaseConnection } from '../src/db';
  19. describe('Resolution Module', () => {
  20. let tempDir: string;
  21. let cg: CodeGraph;
  22. beforeEach(() => {
  23. // Create temp directory
  24. tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-resolution-test-'));
  25. });
  26. afterEach(() => {
  27. // Clean up
  28. if (cg) {
  29. cg.destroy();
  30. } else if (fs.existsSync(tempDir)) {
  31. fs.rmSync(tempDir, { recursive: true });
  32. }
  33. });
  34. describe('Name Matcher', () => {
  35. it('should match exact name references', () => {
  36. // Create a mock context
  37. const mockNodes: Node[] = [
  38. {
  39. id: 'func:test.ts:myFunction:10',
  40. kind: 'function',
  41. name: 'myFunction',
  42. qualifiedName: 'test.ts::myFunction',
  43. filePath: 'test.ts',
  44. language: 'typescript',
  45. startLine: 10,
  46. endLine: 20,
  47. startColumn: 0,
  48. endColumn: 0,
  49. updatedAt: Date.now(),
  50. },
  51. ];
  52. const context: ResolutionContext = {
  53. getNodesInFile: () => mockNodes,
  54. getNodesByName: (name) => mockNodes.filter((n) => n.name === name),
  55. getNodesByQualifiedName: () => [],
  56. getNodesByKind: () => [],
  57. fileExists: () => true,
  58. readFile: () => null,
  59. getProjectRoot: () => '/test',
  60. getAllFiles: () => ['test.ts'],
  61. };
  62. const ref = {
  63. fromNodeId: 'caller:main.ts:caller:5',
  64. referenceName: 'myFunction',
  65. referenceKind: 'calls' as const,
  66. line: 5,
  67. column: 10,
  68. filePath: 'main.ts',
  69. language: 'typescript' as const,
  70. };
  71. const result = matchReference(ref, context);
  72. expect(result).not.toBeNull();
  73. expect(result?.targetNodeId).toBe('func:test.ts:myFunction:10');
  74. expect(result?.resolvedBy).toBe('exact-match');
  75. });
  76. it('should prefer same-module candidates over cross-module matches', () => {
  77. // Simulates a Python monorepo where multiple apps define navigate()
  78. const candidateA: Node = {
  79. id: 'func:apps/app_a/src/server.py:navigate:10',
  80. kind: 'function',
  81. name: 'navigate',
  82. qualifiedName: 'apps/app_a/src/server.py::navigate',
  83. filePath: 'apps/app_a/src/server.py',
  84. language: 'python',
  85. startLine: 10,
  86. endLine: 20,
  87. startColumn: 0,
  88. endColumn: 0,
  89. updatedAt: Date.now(),
  90. };
  91. const candidateB: Node = {
  92. id: 'func:apps/app_b/src/server.py:navigate:15',
  93. kind: 'function',
  94. name: 'navigate',
  95. qualifiedName: 'apps/app_b/src/server.py::navigate',
  96. filePath: 'apps/app_b/src/server.py',
  97. language: 'python',
  98. startLine: 15,
  99. endLine: 25,
  100. startColumn: 0,
  101. endColumn: 0,
  102. updatedAt: Date.now(),
  103. };
  104. const context: ResolutionContext = {
  105. getNodesInFile: () => [],
  106. getNodesByName: (name) => name === 'navigate' ? [candidateA, candidateB] : [],
  107. getNodesByQualifiedName: () => [],
  108. getNodesByKind: () => [],
  109. fileExists: () => true,
  110. readFile: () => null,
  111. getProjectRoot: () => '/test',
  112. getAllFiles: () => [],
  113. getNodesByLowerName: () => [],
  114. getImportMappings: () => [],
  115. };
  116. // Reference from app_a should resolve to app_a's navigate, not app_b's
  117. const ref = {
  118. fromNodeId: 'func:apps/app_a/src/handler.py:handler:5',
  119. referenceName: 'navigate',
  120. referenceKind: 'calls' as const,
  121. line: 5,
  122. column: 10,
  123. filePath: 'apps/app_a/src/handler.py',
  124. language: 'python' as const,
  125. };
  126. const result = matchReference(ref, context);
  127. expect(result).not.toBeNull();
  128. expect(result?.targetNodeId).toBe('func:apps/app_a/src/server.py:navigate:10');
  129. expect(result?.resolvedBy).toBe('exact-match');
  130. });
  131. it('should lower confidence for cross-module exact matches', () => {
  132. // Only one candidate but in a completely different module
  133. const candidates: Node[] = [
  134. {
  135. id: 'func:apps/app_b/src/server.py:navigate:10',
  136. kind: 'function',
  137. name: 'navigate',
  138. qualifiedName: 'apps/app_b/src/server.py::navigate',
  139. filePath: 'apps/app_b/src/server.py',
  140. language: 'python',
  141. startLine: 10,
  142. endLine: 20,
  143. startColumn: 0,
  144. endColumn: 0,
  145. updatedAt: Date.now(),
  146. },
  147. {
  148. id: 'func:apps/app_c/src/server.py:navigate:10',
  149. kind: 'function',
  150. name: 'navigate',
  151. qualifiedName: 'apps/app_c/src/server.py::navigate',
  152. filePath: 'apps/app_c/src/server.py',
  153. language: 'python',
  154. startLine: 10,
  155. endLine: 20,
  156. startColumn: 0,
  157. endColumn: 0,
  158. updatedAt: Date.now(),
  159. },
  160. ];
  161. const context: ResolutionContext = {
  162. getNodesInFile: () => [],
  163. getNodesByName: (name) => name === 'navigate' ? candidates : [],
  164. getNodesByQualifiedName: () => [],
  165. getNodesByKind: () => [],
  166. fileExists: () => true,
  167. readFile: () => null,
  168. getProjectRoot: () => '/test',
  169. getAllFiles: () => [],
  170. getNodesByLowerName: () => [],
  171. getImportMappings: () => [],
  172. };
  173. // Reference from app_a — neither candidate is in the same module
  174. const ref = {
  175. fromNodeId: 'func:apps/app_a/src/handler.py:handler:5',
  176. referenceName: 'navigate',
  177. referenceKind: 'calls' as const,
  178. line: 5,
  179. column: 10,
  180. filePath: 'apps/app_a/src/handler.py',
  181. language: 'python' as const,
  182. };
  183. const result = matchReference(ref, context);
  184. // Should still resolve but with low confidence
  185. expect(result).not.toBeNull();
  186. expect(result?.confidence).toBeLessThanOrEqual(0.4);
  187. });
  188. it('should match qualified name references', () => {
  189. const mockClassNode: Node = {
  190. id: 'class:user.ts:User:5',
  191. kind: 'class',
  192. name: 'User',
  193. qualifiedName: 'user.ts::User',
  194. filePath: 'user.ts',
  195. language: 'typescript',
  196. startLine: 5,
  197. endLine: 30,
  198. startColumn: 0,
  199. endColumn: 0,
  200. updatedAt: Date.now(),
  201. };
  202. const mockMethodNode: Node = {
  203. id: 'method:user.ts:User.save:15',
  204. kind: 'method',
  205. name: 'save',
  206. qualifiedName: 'user.ts::User::save',
  207. filePath: 'user.ts',
  208. language: 'typescript',
  209. startLine: 15,
  210. endLine: 25,
  211. startColumn: 0,
  212. endColumn: 0,
  213. updatedAt: Date.now(),
  214. };
  215. const context: ResolutionContext = {
  216. getNodesInFile: (fp) => fp === 'user.ts' ? [mockClassNode, mockMethodNode] : [],
  217. getNodesByName: (name) => {
  218. if (name === 'User') return [mockClassNode];
  219. if (name === 'save') return [mockMethodNode];
  220. return [];
  221. },
  222. getNodesByQualifiedName: (qn) => {
  223. if (qn === 'user.ts::User::save') return [mockMethodNode];
  224. return [];
  225. },
  226. getNodesByKind: () => [],
  227. fileExists: () => true,
  228. readFile: () => null,
  229. getProjectRoot: () => '/test',
  230. getAllFiles: () => ['user.ts'],
  231. };
  232. const ref = {
  233. fromNodeId: 'caller:main.ts:main:5',
  234. referenceName: 'User.save',
  235. referenceKind: 'calls' as const,
  236. line: 5,
  237. column: 10,
  238. filePath: 'main.ts',
  239. language: 'typescript' as const,
  240. };
  241. const result = matchReference(ref, context);
  242. expect(result).not.toBeNull();
  243. expect(result?.targetNodeId).toBe('method:user.ts:User.save:15');
  244. });
  245. });
  246. describe('Import Resolver', () => {
  247. it('should resolve relative import paths', () => {
  248. const context: ResolutionContext = {
  249. getNodesInFile: () => [],
  250. getNodesByName: () => [],
  251. getNodesByQualifiedName: () => [],
  252. getNodesByKind: () => [],
  253. fileExists: (p) => p === 'src/components/utils.ts' || p === 'src/components/utils/index.ts',
  254. readFile: () => null,
  255. getProjectRoot: () => '',
  256. getAllFiles: () => ['src/components/utils.ts', 'src/components/utils/index.ts'],
  257. };
  258. const result = resolveImportPath(
  259. './utils',
  260. 'src/components/Button.ts',
  261. 'typescript',
  262. context
  263. );
  264. expect(result).toBe('src/components/utils.ts');
  265. });
  266. it('should resolve parent directory imports', () => {
  267. const context: ResolutionContext = {
  268. getNodesInFile: () => [],
  269. getNodesByName: () => [],
  270. getNodesByQualifiedName: () => [],
  271. getNodesByKind: () => [],
  272. fileExists: (p) => p === 'src/helpers.ts' || p === 'src/helpers/index.ts',
  273. readFile: () => null,
  274. getProjectRoot: () => '',
  275. getAllFiles: () => ['src/helpers.ts', 'src/helpers/index.ts'],
  276. };
  277. const result = resolveImportPath(
  278. '../helpers',
  279. 'src/components/Button.ts',
  280. 'typescript',
  281. context
  282. );
  283. expect(result).toBe('src/helpers.ts');
  284. });
  285. it('should extract JS/TS import mappings', () => {
  286. const content = `
  287. import { foo } from './foo';
  288. import bar from '../bar';
  289. import * as utils from './utils';
  290. import { baz, qux } from './baz';
  291. `;
  292. const mappings = extractImportMappings(
  293. 'src/index.ts',
  294. content,
  295. 'typescript'
  296. );
  297. expect(mappings.length).toBeGreaterThan(0);
  298. expect(mappings.some((m) => m.localName === 'foo')).toBe(true);
  299. expect(mappings.some((m) => m.localName === 'bar')).toBe(true);
  300. });
  301. it('should extract Python import mappings', () => {
  302. const content = `
  303. from utils import helper
  304. from .models import User
  305. import os
  306. from ..services import auth_service
  307. `;
  308. const mappings = extractImportMappings(
  309. 'src/main.py',
  310. content,
  311. 'python'
  312. );
  313. expect(mappings.length).toBeGreaterThan(0);
  314. expect(mappings.some((m) => m.localName === 'helper')).toBe(true);
  315. expect(mappings.some((m) => m.localName === 'User')).toBe(true);
  316. });
  317. });
  318. describe('JVM FQN Import Resolution', () => {
  319. // Build a ResolutionContext stub whose getNodesByQualifiedName answers
  320. // from a fixed table — the only context method resolveJvmImport touches.
  321. const makeContext = (byQName: Record<string, Node[]>): ResolutionContext => ({
  322. getNodesInFile: () => [],
  323. getNodesByName: () => [],
  324. getNodesByQualifiedName: (q) => byQName[q] ?? [],
  325. getNodesByKind: () => [],
  326. fileExists: () => false,
  327. readFile: () => null,
  328. getProjectRoot: () => '',
  329. getAllFiles: () => [],
  330. });
  331. const node = (id: string, name: string, qualifiedName: string, kind: Node['kind'] = 'class', language: Node['language'] = 'kotlin'): Node => ({
  332. id, kind, name, qualifiedName,
  333. filePath: 'Models.kt', language,
  334. startLine: 1, endLine: 1, startColumn: 0, endColumn: 0,
  335. updatedAt: 0,
  336. });
  337. const importRef = (referenceName: string, language: Node['language'] = 'kotlin'): UnresolvedRef => ({
  338. fromNodeId: 'caller',
  339. referenceName,
  340. referenceKind: 'imports',
  341. line: 1, column: 0,
  342. filePath: 'Caller.kt',
  343. language,
  344. });
  345. it('resolves a Kotlin class import by FQN regardless of filename', () => {
  346. const target = node('n1', 'Bar', 'com.example.foo::Bar');
  347. const ctx = makeContext({ 'com.example.foo::Bar': [target] });
  348. const result = resolveJvmImport(importRef('com.example.foo.Bar'), ctx);
  349. expect(result?.targetNodeId).toBe('n1');
  350. expect(result?.resolvedBy).toBe('import');
  351. });
  352. it('resolves a Kotlin top-level function import by FQN', () => {
  353. const util = node('n2', 'util', 'com.example.foo::util', 'function');
  354. const ctx = makeContext({ 'com.example.foo::util': [util] });
  355. const result = resolveJvmImport(importRef('com.example.foo.util'), ctx);
  356. expect(result?.targetNodeId).toBe('n2');
  357. });
  358. it('resolves a Java import by FQN', () => {
  359. const target = node('n3', 'Bar', 'com.example.foo::Bar', 'class', 'java');
  360. const ctx = makeContext({ 'com.example.foo::Bar': [target] });
  361. const result = resolveJvmImport(importRef('com.example.foo.Bar', 'java'), ctx);
  362. expect(result?.targetNodeId).toBe('n3');
  363. });
  364. it('resolves cross-language: Kotlin importing a Java class', () => {
  365. // The Kotlin file declares `import com.example.JavaBar` — the target is
  366. // a Java class node. JVM interop means the resolver doesn't care about
  367. // the source language of the target, only that the FQN matches.
  368. const target = node('n4', 'JavaBar', 'com.example::JavaBar', 'class', 'java');
  369. const ctx = makeContext({ 'com.example::JavaBar': [target] });
  370. const result = resolveJvmImport(importRef('com.example.JavaBar'), ctx);
  371. expect(result?.targetNodeId).toBe('n4');
  372. });
  373. it('disambiguates a name collision across packages', () => {
  374. // Two classes named `Bar` in different packages. Each import resolves
  375. // to the one whose FQN matches — not to "whichever was found first".
  376. const barA = node('n5a', 'Bar', 'com.example.alpha::Bar');
  377. const barB = node('n5b', 'Bar', 'com.example.beta::Bar');
  378. const ctx = makeContext({
  379. 'com.example.alpha::Bar': [barA],
  380. 'com.example.beta::Bar': [barB],
  381. });
  382. expect(resolveJvmImport(importRef('com.example.alpha.Bar'), ctx)?.targetNodeId).toBe('n5a');
  383. expect(resolveJvmImport(importRef('com.example.beta.Bar'), ctx)?.targetNodeId).toBe('n5b');
  384. });
  385. it('returns null for wildcard imports', () => {
  386. const ctx = makeContext({});
  387. expect(resolveJvmImport(importRef('com.example.foo.*'), ctx)).toBeNull();
  388. });
  389. it('returns null for unqualified names', () => {
  390. // A single-segment name has no package; nothing to look up by FQN.
  391. const ctx = makeContext({ 'Bar': [node('n6', 'Bar', 'Bar')] });
  392. expect(resolveJvmImport(importRef('Bar'), ctx)).toBeNull();
  393. });
  394. it('returns null for non-JVM languages', () => {
  395. const target = node('n7', 'Bar', 'com.example::Bar');
  396. const ctx = makeContext({ 'com.example::Bar': [target] });
  397. expect(resolveJvmImport(importRef('com.example.Bar', 'typescript'), ctx)).toBeNull();
  398. });
  399. it('returns null for non-imports reference kinds', () => {
  400. // The resolver intentionally only acts on `imports` refs; ordinary
  401. // `calls`/`extends` refs fall through to the framework + name-matcher
  402. // strategies.
  403. const target = node('n8', 'Bar', 'com.example::Bar');
  404. const ctx = makeContext({ 'com.example::Bar': [target] });
  405. const ref: UnresolvedRef = {
  406. fromNodeId: 'caller', referenceName: 'com.example.Bar',
  407. referenceKind: 'calls', line: 1, column: 0,
  408. filePath: 'Caller.kt', language: 'kotlin',
  409. };
  410. expect(resolveJvmImport(ref, ctx)).toBeNull();
  411. });
  412. it('returns null when the FQN is not in the index', () => {
  413. const ctx = makeContext({});
  414. expect(resolveJvmImport(importRef('com.example.Unknown'), ctx)).toBeNull();
  415. });
  416. });
  417. describe('Framework Detection', () => {
  418. it('should detect React framework', () => {
  419. const context: ResolutionContext = {
  420. getNodesInFile: () => [],
  421. getNodesByName: () => [],
  422. getNodesByQualifiedName: () => [],
  423. getNodesByKind: () => [],
  424. fileExists: () => false,
  425. readFile: (p) => {
  426. if (p === 'package.json') {
  427. return JSON.stringify({
  428. dependencies: { react: '^18.0.0' },
  429. });
  430. }
  431. return null;
  432. },
  433. getProjectRoot: () => '/test',
  434. getAllFiles: () => ['package.json', 'src/App.tsx'],
  435. };
  436. const frameworks = detectFrameworks(context);
  437. expect(frameworks.some((f) => f.name === 'react')).toBe(true);
  438. });
  439. it('should detect Express framework', () => {
  440. const context: ResolutionContext = {
  441. getNodesInFile: () => [],
  442. getNodesByName: () => [],
  443. getNodesByQualifiedName: () => [],
  444. getNodesByKind: () => [],
  445. fileExists: () => false,
  446. readFile: (p) => {
  447. if (p === 'package.json') {
  448. return JSON.stringify({
  449. dependencies: { express: '^4.18.0' },
  450. });
  451. }
  452. return null;
  453. },
  454. getProjectRoot: () => '/test',
  455. getAllFiles: () => ['package.json', 'src/app.js'],
  456. };
  457. const frameworks = detectFrameworks(context);
  458. expect(frameworks.some((f) => f.name === 'express')).toBe(true);
  459. });
  460. it('should detect Laravel framework', () => {
  461. const context: ResolutionContext = {
  462. getNodesInFile: () => [],
  463. getNodesByName: () => [],
  464. getNodesByQualifiedName: () => [],
  465. getNodesByKind: () => [],
  466. fileExists: (p) => p === 'artisan',
  467. readFile: () => null,
  468. getProjectRoot: () => '/test',
  469. getAllFiles: () => ['artisan', 'app/Http/Kernel.php'],
  470. };
  471. const frameworks = detectFrameworks(context);
  472. expect(frameworks.some((f) => f.name === 'laravel')).toBe(true);
  473. });
  474. it('should return all framework resolvers', () => {
  475. const resolvers = getAllFrameworkResolvers();
  476. expect(resolvers.length).toBeGreaterThan(0);
  477. expect(resolvers.some((r) => r.name === 'react')).toBe(true);
  478. expect(resolvers.some((r) => r.name === 'express')).toBe(true);
  479. expect(resolvers.some((r) => r.name === 'laravel')).toBe(true);
  480. });
  481. });
  482. describe('React Framework Resolver', () => {
  483. it('should resolve React component references', () => {
  484. const mockNodes: Node[] = [
  485. {
  486. id: 'component:src/Button.tsx:Button:5',
  487. kind: 'component',
  488. name: 'Button',
  489. qualifiedName: 'src/Button.tsx::Button',
  490. filePath: 'src/Button.tsx',
  491. language: 'tsx',
  492. startLine: 5,
  493. endLine: 20,
  494. startColumn: 0,
  495. endColumn: 0,
  496. updatedAt: Date.now(),
  497. },
  498. ];
  499. const context: ResolutionContext = {
  500. getNodesInFile: (fp) => (fp === 'src/Button.tsx' ? mockNodes : []),
  501. getNodesByName: () => mockNodes,
  502. getNodesByQualifiedName: () => [],
  503. getNodesByKind: () => [],
  504. fileExists: () => false,
  505. readFile: (p) => {
  506. if (p === 'package.json') {
  507. return JSON.stringify({ dependencies: { react: '^18.0.0' } });
  508. }
  509. return null;
  510. },
  511. getProjectRoot: () => '/test',
  512. getAllFiles: () => ['package.json', 'src/Button.tsx', 'src/App.tsx'],
  513. };
  514. const frameworks = detectFrameworks(context);
  515. const reactResolver = frameworks.find((f) => f.name === 'react');
  516. expect(reactResolver).toBeDefined();
  517. const ref = {
  518. fromNodeId: 'component:src/App.tsx:App:1',
  519. referenceName: 'Button',
  520. referenceKind: 'renders' as const,
  521. line: 10,
  522. column: 5,
  523. filePath: 'src/App.tsx',
  524. language: 'typescript' as const,
  525. };
  526. const result = reactResolver!.resolve(ref, context);
  527. expect(result).not.toBeNull();
  528. expect(result?.targetNodeId).toBe('component:src/Button.tsx:Button:5');
  529. });
  530. it('should resolve custom hook references', () => {
  531. const mockNodes: Node[] = [
  532. {
  533. id: 'hook:src/hooks/useAuth.ts:useAuth:1',
  534. kind: 'function',
  535. name: 'useAuth',
  536. qualifiedName: 'src/hooks/useAuth.ts::useAuth',
  537. filePath: 'src/hooks/useAuth.ts',
  538. language: 'typescript',
  539. startLine: 1,
  540. endLine: 20,
  541. startColumn: 0,
  542. endColumn: 0,
  543. updatedAt: Date.now(),
  544. },
  545. ];
  546. const context: ResolutionContext = {
  547. getNodesInFile: (fp) => (fp.includes('useAuth') ? mockNodes : []),
  548. getNodesByName: () => mockNodes,
  549. getNodesByQualifiedName: () => [],
  550. getNodesByKind: () => [],
  551. fileExists: () => false,
  552. readFile: (p) => {
  553. if (p === 'package.json') {
  554. return JSON.stringify({ dependencies: { react: '^18.0.0' } });
  555. }
  556. return null;
  557. },
  558. getProjectRoot: () => '/test',
  559. getAllFiles: () => ['package.json', 'src/hooks/useAuth.ts'],
  560. };
  561. const frameworks = detectFrameworks(context);
  562. const reactResolver = frameworks.find((f) => f.name === 'react');
  563. const ref = {
  564. fromNodeId: 'component:src/App.tsx:App:1',
  565. referenceName: 'useAuth',
  566. referenceKind: 'calls' as const,
  567. line: 5,
  568. column: 10,
  569. filePath: 'src/App.tsx',
  570. language: 'typescript' as const,
  571. };
  572. const result = reactResolver!.resolve(ref, context);
  573. expect(result).not.toBeNull();
  574. expect(result?.targetNodeId).toBe('hook:src/hooks/useAuth.ts:useAuth:1');
  575. });
  576. });
  577. describe('Integration Tests', () => {
  578. it('should create resolver from CodeGraph instance', async () => {
  579. // Create a simple TypeScript project
  580. fs.writeFileSync(
  581. path.join(tempDir, 'package.json'),
  582. JSON.stringify({ name: 'test', dependencies: { react: '^18.0.0' } })
  583. );
  584. const srcDir = path.join(tempDir, 'src');
  585. fs.mkdirSync(srcDir);
  586. // Create utility file
  587. fs.writeFileSync(
  588. path.join(srcDir, 'utils.ts'),
  589. `export function formatDate(date: Date): string {
  590. return date.toISOString();
  591. }
  592. export function parseDate(str: string): Date {
  593. return new Date(str);
  594. }`
  595. );
  596. // Create main file that uses utils
  597. fs.writeFileSync(
  598. path.join(srcDir, 'main.ts'),
  599. `import { formatDate, parseDate } from './utils';
  600. function processDate(input: string): string {
  601. const date = parseDate(input);
  602. return formatDate(date);
  603. }`
  604. );
  605. // Initialize and index
  606. cg = await CodeGraph.init(tempDir, { index: true });
  607. // Check that resolver detected React framework
  608. const frameworks = cg.getDetectedFrameworks();
  609. expect(frameworks).toContain('react');
  610. // Get stats to verify indexing worked
  611. const stats = cg.getStats();
  612. expect(stats.fileCount).toBe(2);
  613. expect(stats.nodeCount).toBeGreaterThan(0);
  614. });
  615. it('should resolve references after indexing', async () => {
  616. // Create a project with references
  617. const srcDir = path.join(tempDir, 'src');
  618. fs.mkdirSync(srcDir, { recursive: true });
  619. fs.writeFileSync(
  620. path.join(srcDir, 'helper.ts'),
  621. `export function helperFunction(): void {
  622. console.log('helper');
  623. }`
  624. );
  625. fs.writeFileSync(
  626. path.join(srcDir, 'main.ts'),
  627. `import { helperFunction } from './helper';
  628. function main(): void {
  629. helperFunction();
  630. }`
  631. );
  632. cg = await CodeGraph.init(tempDir, { index: true });
  633. // Run reference resolution
  634. const result = cg.resolveReferences();
  635. // Should have attempted resolution
  636. expect(result.stats.total).toBeGreaterThanOrEqual(0);
  637. });
  638. it('promotes calls→instantiates when target resolves to a class (Python)', async () => {
  639. // Python has no `new` keyword — `Foo()` is the standard
  640. // instantiation syntax. Extraction can't tell that apart from
  641. // a function call without symbol info, so it emits a `calls`
  642. // ref. Resolution promotes it to `instantiates` once the
  643. // target is known to be a class.
  644. const srcDir = path.join(tempDir, 'src');
  645. fs.mkdirSync(srcDir, { recursive: true });
  646. fs.writeFileSync(
  647. path.join(srcDir, 'app.py'),
  648. `class UserService:
  649. def __init__(self):
  650. self.db = None
  651. def bootstrap():
  652. return UserService()
  653. `
  654. );
  655. cg = await CodeGraph.init(tempDir, { index: true });
  656. cg.resolveReferences();
  657. const bootstrap = cg
  658. .getNodesByKind('function')
  659. .find((n) => n.name === 'bootstrap');
  660. expect(bootstrap).toBeDefined();
  661. const outgoing = cg.getOutgoingEdges(bootstrap!.id);
  662. const instantiates = outgoing.find((e) => e.kind === 'instantiates');
  663. expect(instantiates).toBeDefined();
  664. // Same edge must NOT also appear as a `calls` edge — promotion
  665. // replaces the kind, doesn't duplicate.
  666. const callsToUserService = outgoing.filter(
  667. (e) => e.kind === 'calls' && e.target === instantiates!.target
  668. );
  669. expect(callsToUserService).toHaveLength(0);
  670. });
  671. it('resolves Go cross-package qualified calls via go.mod module path (#388)', async () => {
  672. // Pre-#388, every `pkga.FuncX(...)` call in a Go monorepo was flagged
  673. // external (isExternalImport returned true for any non-`/internal/`
  674. // import without `.`-prefix) and resolution fell through to name-match
  675. // with path proximity — recall on cross-package callers was ~<1%.
  676. fs.writeFileSync(
  677. path.join(tempDir, 'go.mod'),
  678. 'module github.com/example/myproject\n\ngo 1.21\n'
  679. );
  680. const pkgaDir = path.join(tempDir, 'pkga');
  681. const pkgbDir = path.join(tempDir, 'pkgb');
  682. const pkgcDir = path.join(tempDir, 'pkgc');
  683. fs.mkdirSync(pkgaDir);
  684. fs.mkdirSync(pkgbDir);
  685. fs.mkdirSync(pkgcDir);
  686. // Same-name exported function in two packages — only the imported one
  687. // should resolve. Exercises disambiguation, not just connectivity.
  688. fs.writeFileSync(
  689. path.join(pkgaDir, 'conv.go'),
  690. 'package pkga\nfunc Convert(x int) int { return x * 2 }\n'
  691. );
  692. fs.writeFileSync(
  693. path.join(pkgbDir, 'conv.go'),
  694. 'package pkgb\nfunc Convert(x int) int { return x + 1 }\n'
  695. );
  696. fs.writeFileSync(
  697. path.join(pkgcDir, 'use.go'),
  698. `package pkgc
  699. import "github.com/example/myproject/pkga"
  700. func UsePkga() {
  701. pkga.Convert(5)
  702. }
  703. `
  704. );
  705. cg = await CodeGraph.init(tempDir, { index: true });
  706. const usePkga = cg.getNodesByKind('function').filter((n) => n.name ==='UsePkga')[0];
  707. expect(usePkga).toBeDefined();
  708. const outgoing = cg.getOutgoingEdges(usePkga!.id);
  709. const callEdges = outgoing.filter((e) => e.kind === 'calls');
  710. expect(callEdges).toHaveLength(1);
  711. const target = cg.getNode(callEdges[0]!.target);
  712. expect(target?.name).toBe('Convert');
  713. // Critical: the resolver must pick the imported pkga's Convert,
  714. // not pkgb's. With the broken (pre-fix) resolver this lands on
  715. // whichever Convert happens to be cheaper under path proximity.
  716. expect(target?.filePath.replace(/\\/g, '/')).toBe('pkga/conv.go');
  717. });
  718. it('resolves Go aliased imports across packages (#388)', async () => {
  719. fs.writeFileSync(
  720. path.join(tempDir, 'go.mod'),
  721. 'module github.com/example/myproject\n\ngo 1.21\n'
  722. );
  723. fs.mkdirSync(path.join(tempDir, 'pkgb'));
  724. fs.mkdirSync(path.join(tempDir, 'pkgd'));
  725. fs.writeFileSync(
  726. path.join(tempDir, 'pkgb', 'lib.go'),
  727. 'package pkgb\nfunc Compute(x int) int { return x }\n'
  728. );
  729. fs.writeFileSync(
  730. path.join(tempDir, 'pkgd', 'use.go'),
  731. `package pkgd
  732. import (
  733. "fmt"
  734. alias "github.com/example/myproject/pkgb"
  735. )
  736. func UseAliased() {
  737. fmt.Println("hi")
  738. alias.Compute(3)
  739. }
  740. `
  741. );
  742. cg = await CodeGraph.init(tempDir, { index: true });
  743. const useAliased = cg.getNodesByKind('function').filter((n) => n.name ==='UseAliased')[0];
  744. expect(useAliased).toBeDefined();
  745. const calls = cg.getOutgoingEdges(useAliased!.id).filter((e) => e.kind === 'calls');
  746. // fmt.Println is stdlib — must stay external. alias.Compute must resolve.
  747. expect(calls).toHaveLength(1);
  748. const target = cg.getNode(calls[0]!.target);
  749. expect(target?.name).toBe('Compute');
  750. expect(target?.filePath.replace(/\\/g, '/')).toBe('pkgb/lib.go');
  751. });
  752. it('resolves Python module-attribute calls after `from pkg import module` (#578)', async () => {
  753. // Pre-#578, a `module.func()` call where `module` was bound via
  754. // `from pkg import module` dropped its `calls` edge. The file→file import
  755. // edge resolved (resolveModuleImportToFile falls back to a dotted-module
  756. // file lookup for absolute package paths), but resolvePythonModuleMember
  757. // had no such fallback — resolveImportPath returns null for an absolute
  758. // package path like `pkg.module`, so the member never resolved and
  759. // callers/callees/impact on the target came back empty. Same root-cause
  760. // class as the Go cross-package qualified call (#388).
  761. fs.mkdirSync(path.join(tempDir, 'pkg'));
  762. fs.writeFileSync(path.join(tempDir, 'pkg', '__init__.py'), '');
  763. fs.writeFileSync(
  764. path.join(tempDir, 'pkg', 'module.py'),
  765. 'def func():\n return 1\n'
  766. );
  767. fs.writeFileSync(
  768. path.join(tempDir, 'main.py'),
  769. `from pkg import module
  770. import os
  771. def caller():
  772. return module.func()
  773. def external_caller():
  774. return os.getcwd()
  775. `
  776. );
  777. cg = await CodeGraph.init(tempDir, { index: true });
  778. const caller = cg.getNodesByKind('function').filter((n) => n.name === 'caller')[0];
  779. expect(caller).toBeDefined();
  780. const calls = cg.getOutgoingEdges(caller!.id).filter((e) => e.kind === 'calls');
  781. // module.func() must resolve to the real function in the submodule file.
  782. expect(calls).toHaveLength(1);
  783. const target = cg.getNode(calls[0]!.target);
  784. expect(target?.name).toBe('func');
  785. expect(target?.filePath.replace(/\\/g, '/')).toBe('pkg/module.py');
  786. // The flip side of the fix: an attribute call through a *stdlib* module
  787. // (`os.getcwd()`) must still create no edge — the fallback only matches
  788. // real in-repo module files.
  789. const externalCaller = cg.getNodesByKind('function').filter((n) => n.name === 'external_caller')[0];
  790. expect(externalCaller).toBeDefined();
  791. const externalCalls = cg.getOutgoingEdges(externalCaller!.id).filter((e) => e.kind === 'calls');
  792. expect(externalCalls).toHaveLength(0);
  793. });
  794. it('attaches Go methods to their receiver type across files (#583, cross-file half)', async () => {
  795. // In Go a type's methods are commonly declared in a different file from the
  796. // `type` declaration (`type Box` in box.go, `func (b *Box) Get()` in
  797. // box_methods.go). Extraction only attaches the struct→method `contains`
  798. // edge when the type is in the SAME file (the owner lookup is file-scoped),
  799. // so a cross-file method was orphaned from its struct — breaking member
  800. // outlines and any callers/callees/impact traversal through `contains`. A
  801. // resolution-phase pass now links them within the package (= directory).
  802. fs.writeFileSync(
  803. path.join(tempDir, 'box.go'),
  804. 'package main\n\ntype Box struct{ v int }\n'
  805. );
  806. fs.writeFileSync(
  807. path.join(tempDir, 'box_methods.go'),
  808. 'package main\n\nfunc (b *Box) Get() int { return b.v }\nfunc (b *Box) Set(x int) { b.v = x }\n'
  809. );
  810. // Generic receiver declared cross-file too — exercises #583 half A
  811. // (generic `*Stack[T]` receiver parsing) and half B (cross-file) together.
  812. fs.writeFileSync(
  813. path.join(tempDir, 'stack.go'),
  814. 'package main\n\ntype Stack[T any] struct {\n\titems []T\n}\n'
  815. );
  816. fs.writeFileSync(
  817. path.join(tempDir, 'stack_push.go'),
  818. 'package main\n\nfunc (s *Stack[T]) Push(v T) { s.items = append(s.items, v) }\n'
  819. );
  820. // A same-named type in another package must NOT capture this package's
  821. // methods — the link is scoped to the receiver type's own directory.
  822. fs.mkdirSync(path.join(tempDir, 'other'));
  823. fs.writeFileSync(
  824. path.join(tempDir, 'other', 'box.go'),
  825. 'package other\n\ntype Box struct{ w int }\n'
  826. );
  827. cg = await CodeGraph.init(tempDir, { index: true });
  828. const methodsOf = (typeName: string, file: string): string[] => {
  829. const node = cg
  830. .getNodesByKind('struct')
  831. .find((n) => n.name === typeName && n.filePath.replace(/\\/g, '/') === file);
  832. expect(node, `${typeName} @ ${file}`).toBeDefined();
  833. return cg
  834. .getOutgoingEdges(node!.id)
  835. .filter((e) => e.kind === 'contains')
  836. .map((e) => cg.getNode(e.target))
  837. .filter((n) => !!n && n.kind === 'method')
  838. .map((n) => n!.name)
  839. .sort();
  840. };
  841. // Cross-file (non-generic) methods now attach to their struct.
  842. expect(methodsOf('Box', 'box.go')).toEqual(['Get', 'Set']);
  843. // Generic + cross-file.
  844. expect(methodsOf('Stack', 'stack.go')).toEqual(['Push']);
  845. // Cross-package isolation: other/Box defines no methods of its own.
  846. expect(methodsOf('Box', 'other/box.go')).toEqual([]);
  847. });
  848. it('TS type_alias object-shape members resolve method calls (#359)', async () => {
  849. // Pre-#359, `recorder.stop()` (recorder: RecorderHandle) attached
  850. // to `StdioMcpClient.stop` in a sibling directory via path-proximity
  851. // because the type_alias had no `stop` node — only the unrelated
  852. // class did. Now type_alias produces member nodes (property/method),
  853. // so the camelCase receiver↔type word overlap pulls the call to
  854. // `RecorderHandle::stop` instead of the look-alike class.
  855. fs.mkdirSync(path.join(tempDir, 'voice'));
  856. fs.mkdirSync(path.join(tempDir, 'codegraph'));
  857. fs.writeFileSync(
  858. path.join(tempDir, 'voice', 'recorder.ts'),
  859. `export type RecorderHandle = {
  860. wavPath: string;
  861. stop: () => Promise<{ ok: true }>;
  862. };
  863. `
  864. );
  865. fs.writeFileSync(
  866. path.join(tempDir, 'voice', 'controller.ts'),
  867. `import type { RecorderHandle } from "./recorder";
  868. export async function finaliseRecording(recorder: RecorderHandle) {
  869. return await recorder.stop();
  870. }
  871. `
  872. );
  873. fs.writeFileSync(
  874. path.join(tempDir, 'codegraph', 'stdio-client.ts'),
  875. `export class StdioMcpClient {
  876. private stopped = false;
  877. async stop(): Promise<void> { this.stopped = true; }
  878. }
  879. `
  880. );
  881. cg = await CodeGraph.init(tempDir, { index: true });
  882. const handleStop = cg
  883. .getNodesByKind('method')
  884. .find((n) => n.qualifiedName === 'RecorderHandle::stop');
  885. expect(handleStop).toBeDefined();
  886. const clientStop = cg
  887. .getNodesByKind('method')
  888. .find((n) => n.qualifiedName === 'StdioMcpClient::stop');
  889. expect(clientStop).toBeDefined();
  890. const handleCallers = cg.getIncomingEdges(handleStop!.id).filter((e) => e.kind === 'calls');
  891. const clientCallers = cg.getIncomingEdges(clientStop!.id).filter((e) => e.kind === 'calls');
  892. expect(handleCallers.length).toBeGreaterThanOrEqual(1);
  893. // The class method must have NO callers — voice/'s call must NOT
  894. // mis-attribute. A non-empty list would mean the false-positive
  895. // path is still firing.
  896. expect(clientCallers).toHaveLength(0);
  897. // Function-typed property surfaces as a `method` node, not `property`,
  898. // because `stop()` semantics at the call site are method semantics.
  899. expect(handleStop!.kind).toBe('method');
  900. });
  901. it('Java import disambiguates same-name classes across modules (#314)', async () => {
  902. // Pre-#314 the import resolver had no Java branch at all, so a
  903. // multi-module Maven repo where `dao/converter/FooConverter` and
  904. // `service/converter/FooConverter` both export a `convert` method
  905. // resolved by file-path proximity — picking whichever class was
  906. // closer to the caller, which is wrong any time the caller lives
  907. // in an equidistant cross-cutting module.
  908. const daoDir = path.join(tempDir, 'dao/src/main/java/com/example/dao/converter');
  909. const serviceDir = path.join(tempDir, 'service/src/main/java/com/example/service/converter');
  910. const webDir = path.join(tempDir, 'web/src/main/java/com/example/web');
  911. fs.mkdirSync(daoDir, { recursive: true });
  912. fs.mkdirSync(serviceDir, { recursive: true });
  913. fs.mkdirSync(webDir, { recursive: true });
  914. fs.writeFileSync(
  915. path.join(daoDir, 'FooConverter.java'),
  916. `package com.example.dao.converter;
  917. public class FooConverter { public String convert(String x) { return "dao:" + x; } }
  918. `
  919. );
  920. fs.writeFileSync(
  921. path.join(serviceDir, 'FooConverter.java'),
  922. `package com.example.service.converter;
  923. public class FooConverter { public String convert(String x) { return "svc:" + x; } }
  924. `
  925. );
  926. // The caller imports the SERVICE version — even though dao is
  927. // alphabetically/lexically first in the candidate list, the
  928. // import must trump that order.
  929. fs.writeFileSync(
  930. path.join(webDir, 'Handler.java'),
  931. `package com.example.web;
  932. import com.example.service.converter.FooConverter;
  933. public class Handler {
  934. private FooConverter fooConverter;
  935. public String use() { return fooConverter.convert("input"); }
  936. }
  937. `
  938. );
  939. cg = await CodeGraph.init(tempDir, { index: true });
  940. const use = cg
  941. .getNodesByKind('method')
  942. .find((n) => n.qualifiedName === 'com.example.web::Handler::use');
  943. expect(use).toBeDefined();
  944. const calls = cg.getOutgoingEdges(use!.id).filter((e) => e.kind === 'calls');
  945. expect(calls.length).toBeGreaterThanOrEqual(1);
  946. const target = cg.getNode(calls[0]!.target);
  947. expect(target?.name).toBe('convert');
  948. expect(target?.filePath.replace(/\\/g, '/')).toBe(
  949. 'service/src/main/java/com/example/service/converter/FooConverter.java'
  950. );
  951. });
  952. it('C# extracts references from method/property/field types (#381)', async () => {
  953. // Pre-#381, every C# project produced ZERO `references` edges:
  954. // csharp.ts was missing returnField, and the type-leaf walker
  955. // only recognized TS/Java's `type_identifier` nodes — C# uses
  956. // `identifier`/`predefined_type`/`qualified_name`/`generic_name`.
  957. const srcDir = path.join(tempDir, 'src');
  958. fs.mkdirSync(srcDir, { recursive: true });
  959. fs.writeFileSync(
  960. path.join(srcDir, 'Dtos.cs'),
  961. `namespace MyApp;
  962. public class SessionInfoDto { public string Id { get; set; } = ""; }
  963. public class UserDto { public string Name { get; set; } = ""; }
  964. `
  965. );
  966. fs.writeFileSync(
  967. path.join(srcDir, 'Service.cs'),
  968. `using System.Threading.Tasks;
  969. namespace MyApp;
  970. public class DataExporter
  971. {
  972. public SessionInfoDto Build(UserDto user, SessionInfoDto session) { return session; }
  973. public Task<SessionInfoDto> BuildAsync(UserDto user) { return Task.FromResult(new SessionInfoDto()); }
  974. public SessionInfoDto Latest { get; set; } = new();
  975. private UserDto _cached;
  976. }
  977. `
  978. );
  979. cg = await CodeGraph.init(tempDir, { index: true });
  980. const sessionDto = cg
  981. .getNodesByKind('class')
  982. .find((n) => n.name === 'SessionInfoDto');
  983. const userDto = cg
  984. .getNodesByKind('class')
  985. .find((n) => n.name === 'UserDto');
  986. expect(sessionDto).toBeDefined();
  987. expect(userDto).toBeDefined();
  988. const sessionIncoming = cg
  989. .getIncomingEdges(sessionDto!.id)
  990. .filter((e) => e.kind === 'references');
  991. const userIncoming = cg
  992. .getIncomingEdges(userDto!.id)
  993. .filter((e) => e.kind === 'references');
  994. // SessionInfoDto: Build return, Build param, BuildAsync return (inside Task<>), Latest property.
  995. // UserDto: Build param, BuildAsync param, _cached field.
  996. expect(sessionIncoming.length).toBeGreaterThanOrEqual(4);
  997. expect(userIncoming.length).toBeGreaterThanOrEqual(3);
  998. });
  999. it('Go: leaves stdlib calls (fmt.Println, etc.) external', async () => {
  1000. fs.writeFileSync(
  1001. path.join(tempDir, 'go.mod'),
  1002. 'module github.com/example/myproject\n\ngo 1.21\n'
  1003. );
  1004. fs.writeFileSync(
  1005. path.join(tempDir, 'main.go'),
  1006. `package main
  1007. import "fmt"
  1008. func main() {
  1009. fmt.Println("hi")
  1010. }
  1011. `
  1012. );
  1013. cg = await CodeGraph.init(tempDir, { index: true });
  1014. const mainFn = cg.getNodesByKind('function').filter((n) => n.name ==='main')[0];
  1015. const calls = cg.getOutgoingEdges(mainFn!.id).filter((e) => e.kind === 'calls');
  1016. // No spurious in-project edge — fmt.* must stay unresolved/external.
  1017. expect(calls).toHaveLength(0);
  1018. });
  1019. });
  1020. describe('Name Matcher: kind bias for new ref kinds', () => {
  1021. const baseContext = (candidates: Node[]): ResolutionContext => ({
  1022. getNodesInFile: () => [],
  1023. getNodesByName: (name) => candidates.filter((c) => c.name === name),
  1024. getNodesByQualifiedName: () => [],
  1025. getNodesByKind: () => [],
  1026. fileExists: () => true,
  1027. readFile: () => null,
  1028. getProjectRoot: () => '/test',
  1029. getAllFiles: () => [],
  1030. getNodesByLowerName: () => [],
  1031. getImportMappings: () => [],
  1032. });
  1033. it('prefers a class candidate over a function for `instantiates` refs', () => {
  1034. // A class and a function share a name across the codebase.
  1035. // Without the kind bias, the function (which gets the +25 `calls`
  1036. // bonus historically applied to all candidates of that kind) would
  1037. // win. Now the instantiates branch reverses it.
  1038. const fn: Node = {
  1039. id: 'func:utils.ts:Logger:5', kind: 'function', name: 'Logger',
  1040. qualifiedName: 'utils.ts::Logger', filePath: 'utils.ts', language: 'typescript',
  1041. startLine: 5, endLine: 7, startColumn: 0, endColumn: 0, updatedAt: Date.now(),
  1042. };
  1043. const cls: Node = {
  1044. id: 'class:logger.ts:Logger:10', kind: 'class', name: 'Logger',
  1045. qualifiedName: 'logger.ts::Logger', filePath: 'logger.ts', language: 'typescript',
  1046. startLine: 10, endLine: 30, startColumn: 0, endColumn: 0, updatedAt: Date.now(),
  1047. };
  1048. const ref = {
  1049. fromNodeId: 'func:main.ts:bootstrap:1',
  1050. referenceName: 'Logger',
  1051. referenceKind: 'instantiates' as const,
  1052. line: 5, column: 0, filePath: 'main.ts', language: 'typescript' as const,
  1053. };
  1054. const result = matchReference(ref, baseContext([fn, cls]));
  1055. expect(result?.targetNodeId).toBe('class:logger.ts:Logger:10');
  1056. });
  1057. it('prefers a function candidate over a non-function for `decorates` refs', () => {
  1058. const variable: Node = {
  1059. id: 'var:config.ts:Inject:5', kind: 'variable', name: 'Inject',
  1060. qualifiedName: 'config.ts::Inject', filePath: 'config.ts', language: 'typescript',
  1061. startLine: 5, endLine: 5, startColumn: 0, endColumn: 0, updatedAt: Date.now(),
  1062. };
  1063. const decorator: Node = {
  1064. id: 'func:di.ts:Inject:10', kind: 'function', name: 'Inject',
  1065. qualifiedName: 'di.ts::Inject', filePath: 'di.ts', language: 'typescript',
  1066. startLine: 10, endLine: 20, startColumn: 0, endColumn: 0, updatedAt: Date.now(),
  1067. };
  1068. const ref = {
  1069. fromNodeId: 'class:svc.ts:UserService:1',
  1070. referenceName: 'Inject',
  1071. referenceKind: 'decorates' as const,
  1072. line: 5, column: 0, filePath: 'svc.ts', language: 'typescript' as const,
  1073. };
  1074. const result = matchReference(ref, baseContext([variable, decorator]));
  1075. expect(result?.targetNodeId).toBe('func:di.ts:Inject:10');
  1076. });
  1077. });
  1078. describe('tsconfig path aliases', () => {
  1079. it('resolves an aliased import to the alias-mapped file (not a same-named file elsewhere)', async () => {
  1080. // Two same-named exports in different directories. Without alias
  1081. // resolution, name-matcher would pick whichever it finds first;
  1082. // with alias resolution, the import path uniquely picks one.
  1083. fs.mkdirSync(path.join(tempDir, 'src/utils'), { recursive: true });
  1084. fs.mkdirSync(path.join(tempDir, 'src/legacy'), { recursive: true });
  1085. fs.writeFileSync(
  1086. path.join(tempDir, 'src/utils/format.ts'),
  1087. `export function pickMe(): number { return 1; }\n`
  1088. );
  1089. fs.writeFileSync(
  1090. path.join(tempDir, 'src/legacy/format.ts'),
  1091. `export function pickMe(): number { return 99; }\n`
  1092. );
  1093. fs.writeFileSync(
  1094. path.join(tempDir, 'src/main.ts'),
  1095. `import { pickMe } from '@utils/format';\nexport function go(): number { return pickMe(); }\n`
  1096. );
  1097. fs.writeFileSync(
  1098. path.join(tempDir, 'tsconfig.json'),
  1099. JSON.stringify({
  1100. compilerOptions: {
  1101. baseUrl: './src',
  1102. paths: { '@utils/*': ['utils/*'] },
  1103. },
  1104. })
  1105. );
  1106. cg = await CodeGraph.init(tempDir, { index: true });
  1107. cg.resolveReferences();
  1108. // The two pickMe nodes live in different files. The aliased
  1109. // import should attach the call edge to the @utils-mapped one,
  1110. // not the legacy duplicate.
  1111. const all = cg.getNodesByKind('function').filter((n) => n.name === 'pickMe');
  1112. const utilsNode = all.find((n) => n.filePath === 'src/utils/format.ts');
  1113. const legacyNode = all.find((n) => n.filePath === 'src/legacy/format.ts');
  1114. expect(utilsNode).toBeDefined();
  1115. expect(legacyNode).toBeDefined();
  1116. const utilsCallers = cg.getCallers(utilsNode!.id);
  1117. const legacyCallers = cg.getCallers(legacyNode!.id);
  1118. expect(utilsCallers.length).toBeGreaterThan(0);
  1119. expect(utilsCallers.some((c) => c.node.filePath === 'src/main.ts')).toBe(true);
  1120. // The legacy node should NOT have a caller from src/main.ts —
  1121. // the alias correctly picked the utils version.
  1122. expect(legacyCallers.some((c) => c.node.filePath === 'src/main.ts')).toBe(false);
  1123. });
  1124. it('falls back gracefully when tsconfig is absent', async () => {
  1125. fs.mkdirSync(path.join(tempDir, 'src'), { recursive: true });
  1126. fs.writeFileSync(
  1127. path.join(tempDir, 'src/a.ts'),
  1128. `export function aFn(): void {}\n`
  1129. );
  1130. fs.writeFileSync(
  1131. path.join(tempDir, 'src/b.ts'),
  1132. `import { aFn } from './a';\nexport function bFn(): void { aFn(); }\n`
  1133. );
  1134. cg = await CodeGraph.init(tempDir, { index: true });
  1135. // No tsconfig present — index should still complete and the
  1136. // relative-import-based call edge should be created.
  1137. const aFn = cg.getNodesByKind('function').find((n) => n.name === 'aFn');
  1138. expect(aFn).toBeDefined();
  1139. const callers = cg.getCallers(aFn!.id);
  1140. expect(callers.some((c) => c.node.filePath === 'src/b.ts')).toBe(true);
  1141. });
  1142. });
  1143. describe('re-export chain following', () => {
  1144. it('chases a 3-hop barrel chain (wildcard → named → declaration)', async () => {
  1145. // main.ts → all.ts (wildcard) → index.ts (named) → auth.ts (declaration).
  1146. // Without chain following, `signIn` resolves to nothing because
  1147. // none of the barrel files declare it directly.
  1148. fs.mkdirSync(path.join(tempDir, 'src/services'), { recursive: true });
  1149. fs.writeFileSync(
  1150. path.join(tempDir, 'src/services/auth.ts'),
  1151. `export function signIn(): void {}\n`
  1152. );
  1153. fs.writeFileSync(
  1154. path.join(tempDir, 'src/services/index.ts'),
  1155. `export { signIn } from './auth';\n`
  1156. );
  1157. fs.writeFileSync(
  1158. path.join(tempDir, 'src/all.ts'),
  1159. `export * from './services/index';\n`
  1160. );
  1161. fs.writeFileSync(
  1162. path.join(tempDir, 'src/main.ts'),
  1163. `import { signIn } from './all';\nexport function go(): void { signIn(); }\n`
  1164. );
  1165. cg = await CodeGraph.init(tempDir, { index: true });
  1166. cg.resolveReferences();
  1167. const signInNode = cg
  1168. .getNodesByKind('function')
  1169. .find((n) => n.name === 'signIn' && n.filePath === 'src/services/auth.ts');
  1170. expect(signInNode).toBeDefined();
  1171. const callers = cg.getCallers(signInNode!.id);
  1172. expect(callers.some((c) => c.node.filePath === 'src/main.ts')).toBe(true);
  1173. });
  1174. it('follows a renamed named re-export (export { foo as bar } from ...)', async () => {
  1175. // The chase has to look up `foo` in the upstream module even
  1176. // though the importer asked for `bar` — exercises the rename
  1177. // branch of findExportedSymbol.
  1178. fs.mkdirSync(path.join(tempDir, 'src'), { recursive: true });
  1179. fs.writeFileSync(
  1180. path.join(tempDir, 'src/auth.ts'),
  1181. `export function signIn(): void {}\n`
  1182. );
  1183. fs.writeFileSync(
  1184. path.join(tempDir, 'src/index.ts'),
  1185. `export { signIn as login } from './auth';\n`
  1186. );
  1187. fs.writeFileSync(
  1188. path.join(tempDir, 'src/main.ts'),
  1189. `import { login } from './index';\nexport function go(): void { login(); }\n`
  1190. );
  1191. cg = await CodeGraph.init(tempDir, { index: true });
  1192. cg.resolveReferences();
  1193. const signInNode = cg
  1194. .getNodesByKind('function')
  1195. .find((n) => n.name === 'signIn' && n.filePath === 'src/auth.ts');
  1196. expect(signInNode).toBeDefined();
  1197. const callers = cg.getCallers(signInNode!.id);
  1198. expect(callers.some((c) => c.node.filePath === 'src/main.ts')).toBe(true);
  1199. });
  1200. it('follows a default re-export of a .svelte component (export { default as Foo } from ./RealButton.svelte) (#629)', async () => {
  1201. // The ubiquitous Svelte/React component-barrel form. The leaf is a
  1202. // .svelte component (extracted as kind 'component', the default
  1203. // export). The re-export ALIAS (`Foo`) deliberately differs from the
  1204. // component's real name (`RealButton`) so the name-matcher fallback
  1205. // can't coincidentally connect them — the only path to the edge is
  1206. // the import-chase, which must match a `component` (not just
  1207. // function/class) for the default export. Otherwise the
  1208. // consumer↔component edge is never created and `callers` returns a
  1209. // false 0.
  1210. fs.mkdirSync(path.join(tempDir, 'src/lib'), { recursive: true });
  1211. fs.writeFileSync(
  1212. path.join(tempDir, 'src/lib/RealButton.svelte'),
  1213. `<script lang="ts">\n export let label: string = '';\n</script>\n\n<button>{label}</button>\n`
  1214. );
  1215. fs.writeFileSync(
  1216. path.join(tempDir, 'src/lib/index.ts'),
  1217. `export { default as Foo } from './RealButton.svelte';\n`
  1218. );
  1219. fs.writeFileSync(
  1220. path.join(tempDir, 'src/Bar.svelte'),
  1221. `<script lang="ts">\n import { Foo } from './lib';\n</script>\n\n<Foo />\n`
  1222. );
  1223. cg = await CodeGraph.init(tempDir, { index: true });
  1224. cg.resolveReferences();
  1225. const fooNode = cg
  1226. .getNodesByKind('component')
  1227. .find((n) => n.name === 'RealButton' && n.filePath === 'src/lib/RealButton.svelte');
  1228. expect(fooNode).toBeDefined();
  1229. const callers = cg.getCallers(fooNode!.id);
  1230. expect(callers.some((c) => c.node.filePath === 'src/Bar.svelte')).toBe(true);
  1231. });
  1232. it('resolves a bare directory import (import { x } from "." / "./") to index.ts (#629)', async () => {
  1233. // `import { helper } from '.'` (or './') must map to the
  1234. // directory's index.ts before the re-export chase can run. The
  1235. // barrel renames `realHelper` → `helper` so the name-matcher can't
  1236. // mask a path-resolution failure: only the bare-dir resolution +
  1237. // rename chase can connect the edge.
  1238. fs.mkdirSync(path.join(tempDir, 'src'), { recursive: true });
  1239. fs.writeFileSync(
  1240. path.join(tempDir, 'src/util.ts'),
  1241. `export function realHelper(): void {}\n`
  1242. );
  1243. fs.writeFileSync(
  1244. path.join(tempDir, 'src/index.ts'),
  1245. `export { realHelper as helper } from './util';\n`
  1246. );
  1247. fs.writeFileSync(
  1248. path.join(tempDir, 'src/main.ts'),
  1249. `import { helper } from '.';\nexport function go(): void { helper(); }\n`
  1250. );
  1251. fs.writeFileSync(
  1252. path.join(tempDir, 'src/main2.ts'),
  1253. `import { helper } from './';\nexport function go2(): void { helper(); }\n`
  1254. );
  1255. cg = await CodeGraph.init(tempDir, { index: true });
  1256. cg.resolveReferences();
  1257. const helperNode = cg
  1258. .getNodesByKind('function')
  1259. .find((n) => n.name === 'realHelper' && n.filePath === 'src/util.ts');
  1260. expect(helperNode).toBeDefined();
  1261. const callers = cg.getCallers(helperNode!.id);
  1262. expect(callers.some((c) => c.node.filePath === 'src/main.ts')).toBe(true);
  1263. expect(callers.some((c) => c.node.filePath === 'src/main2.ts')).toBe(true);
  1264. });
  1265. it('resolves a workspace package-subpath barrel (@scope/pkg/sub) to its index (#629)', async () => {
  1266. // bun/npm/pnpm workspace: `@scope/ui/widgets` → the `ui` package's
  1267. // `widgets/` subdir index, which re-exports a .svelte component.
  1268. // Alias `Thing` ≠ component `Widget` defeats the name-matcher, so
  1269. // only workspace-package resolution can connect the edge.
  1270. fs.mkdirSync(path.join(tempDir, 'packages/ui/widgets'), { recursive: true });
  1271. fs.writeFileSync(
  1272. path.join(tempDir, 'package.json'),
  1273. JSON.stringify({ name: 'root', private: true, workspaces: ['packages/*'] }, null, 2)
  1274. );
  1275. fs.writeFileSync(
  1276. path.join(tempDir, 'packages/ui/package.json'),
  1277. JSON.stringify({ name: '@scope/ui', version: '1.0.0' }, null, 2)
  1278. );
  1279. fs.writeFileSync(
  1280. path.join(tempDir, 'packages/ui/widgets/Widget.svelte'),
  1281. `<script lang="ts">\n export let label: string = '';\n</script>\n\n<button>{label}</button>\n`
  1282. );
  1283. fs.writeFileSync(
  1284. path.join(tempDir, 'packages/ui/widgets/index.ts'),
  1285. `export { default as Thing } from './Widget.svelte';\n`
  1286. );
  1287. fs.mkdirSync(path.join(tempDir, 'app'), { recursive: true });
  1288. fs.writeFileSync(
  1289. path.join(tempDir, 'app/App.svelte'),
  1290. `<script lang="ts">\n import { Thing } from '@scope/ui/widgets';\n</script>\n\n<Thing />\n`
  1291. );
  1292. cg = await CodeGraph.init(tempDir, { index: true });
  1293. cg.resolveReferences();
  1294. const buttonNode = cg
  1295. .getNodesByKind('component')
  1296. .find((n) => n.name === 'Widget' && n.filePath === 'packages/ui/widgets/Widget.svelte');
  1297. expect(buttonNode).toBeDefined();
  1298. const callers = cg.getCallers(buttonNode!.id);
  1299. expect(callers.some((c) => c.node.filePath === 'app/App.svelte')).toBe(true);
  1300. });
  1301. it('resolves a barrel import from a Vue SFC <script> block (#629)', async () => {
  1302. // The same import-resolution gaps (no SFC import mappings, no SFC
  1303. // extension list, barrel parsed in the consumer's language) broke
  1304. // Vue SFCs too. Guards the resolver-side generalization to `.vue`.
  1305. // The barrel renames `realRun` → `run` so only the import-chase (not
  1306. // the name-matcher) can connect the call.
  1307. fs.mkdirSync(path.join(tempDir, 'src'), { recursive: true });
  1308. fs.writeFileSync(
  1309. path.join(tempDir, 'src/util.ts'),
  1310. `export function realRun(): void {}\n`
  1311. );
  1312. fs.writeFileSync(
  1313. path.join(tempDir, 'src/index.ts'),
  1314. `export { realRun as run } from './util';\n`
  1315. );
  1316. fs.writeFileSync(
  1317. path.join(tempDir, 'src/App.vue'),
  1318. `<script lang="ts">\nimport { run } from './';\nexport default { mounted() { run(); } };\n</script>\n<template><div/></template>\n`
  1319. );
  1320. cg = await CodeGraph.init(tempDir, { index: true });
  1321. cg.resolveReferences();
  1322. const runNode = cg
  1323. .getNodesByKind('function')
  1324. .find((n) => n.name === 'realRun' && n.filePath === 'src/util.ts');
  1325. expect(runNode).toBeDefined();
  1326. const callers = cg.getCallers(runNode!.id);
  1327. expect(callers.some((c) => c.node.filePath === 'src/App.vue')).toBe(true);
  1328. });
  1329. it('follows a Vue component used in a <template> through a default re-export barrel (#629)', async () => {
  1330. // End-to-end Vue analogue of the Svelte case: the leaf is a `.vue`
  1331. // component re-exported under an alias (`Thing`) that differs from its
  1332. // real name (`Widget`), and the consumer uses it ONLY in markup
  1333. // (`<Thing />`). Requires both the new template-tag extraction AND the
  1334. // barrel default-export chase to connect the edge.
  1335. fs.mkdirSync(path.join(tempDir, 'src/lib'), { recursive: true });
  1336. fs.writeFileSync(
  1337. path.join(tempDir, 'src/lib/Widget.vue'),
  1338. `<script setup lang="ts">\ndefineProps<{ label?: string }>();\n</script>\n<template><button>x</button></template>\n`
  1339. );
  1340. fs.writeFileSync(
  1341. path.join(tempDir, 'src/lib/index.ts'),
  1342. `export { default as Thing } from './Widget.vue';\n`
  1343. );
  1344. fs.writeFileSync(
  1345. path.join(tempDir, 'src/App.vue'),
  1346. `<script setup lang="ts">\nimport { Thing } from './lib';\n</script>\n<template>\n <Thing />\n</template>\n`
  1347. );
  1348. cg = await CodeGraph.init(tempDir, { index: true });
  1349. cg.resolveReferences();
  1350. const widgetNode = cg
  1351. .getNodesByKind('component')
  1352. .find((n) => n.name === 'Widget' && n.filePath === 'src/lib/Widget.vue');
  1353. expect(widgetNode).toBeDefined();
  1354. const callers = cg.getCallers(widgetNode!.id);
  1355. expect(callers.some((c) => c.node.filePath === 'src/App.vue')).toBe(true);
  1356. });
  1357. });
  1358. describe('C/C++ Import Resolution', () => {
  1359. afterEach(() => {
  1360. clearCppIncludeDirCache();
  1361. });
  1362. it('should resolve C include to header in same directory', () => {
  1363. const context: ResolutionContext = {
  1364. getNodesInFile: () => [],
  1365. getNodesByName: () => [],
  1366. getNodesByQualifiedName: () => [],
  1367. getNodesByKind: () => [],
  1368. fileExists: (p) => p === 'utils.h',
  1369. readFile: () => null,
  1370. getProjectRoot: () => '',
  1371. getAllFiles: () => ['utils.h', 'main.c'],
  1372. };
  1373. const result = resolveImportPath(
  1374. 'utils.h',
  1375. 'main.c',
  1376. 'c',
  1377. context
  1378. );
  1379. expect(result).toBe('utils.h');
  1380. });
  1381. it('should resolve C++ include with .hpp extension', () => {
  1382. const context: ResolutionContext = {
  1383. getNodesInFile: () => [],
  1384. getNodesByName: () => [],
  1385. getNodesByQualifiedName: () => [],
  1386. getNodesByKind: () => [],
  1387. fileExists: (p) => p === 'include/myclass.hpp',
  1388. readFile: () => null,
  1389. getProjectRoot: () => '',
  1390. getAllFiles: () => ['include/myclass.hpp', 'src/main.cpp'],
  1391. getCppIncludeDirs: () => ['include'],
  1392. };
  1393. const result = resolveImportPath(
  1394. 'myclass.hpp',
  1395. 'src/main.cpp',
  1396. 'cpp',
  1397. context
  1398. );
  1399. expect(result).toBe('include/myclass.hpp');
  1400. });
  1401. it('should resolve include with subdirectory path', () => {
  1402. const context: ResolutionContext = {
  1403. getNodesInFile: () => [],
  1404. getNodesByName: () => [],
  1405. getNodesByQualifiedName: () => [],
  1406. getNodesByKind: () => [],
  1407. fileExists: (p) => p === 'utils/helpers.h',
  1408. readFile: () => null,
  1409. getProjectRoot: () => '',
  1410. getAllFiles: () => ['utils/helpers.h', 'main.c'],
  1411. };
  1412. const result = resolveImportPath(
  1413. 'utils/helpers.h',
  1414. 'main.c',
  1415. 'c',
  1416. context
  1417. );
  1418. expect(result).toBe('utils/helpers.h');
  1419. });
  1420. it('should resolve include via include directories', () => {
  1421. const context: ResolutionContext = {
  1422. getNodesInFile: () => [],
  1423. getNodesByName: () => [],
  1424. getNodesByQualifiedName: () => [],
  1425. getNodesByKind: () => [],
  1426. fileExists: (p) => p === 'include/myheader.h',
  1427. readFile: () => null,
  1428. getProjectRoot: () => '',
  1429. getAllFiles: () => ['include/myheader.h', 'src/main.cpp'],
  1430. getCppIncludeDirs: () => ['include'],
  1431. };
  1432. const result = resolveImportPath(
  1433. 'myheader.h',
  1434. 'src/main.cpp',
  1435. 'cpp',
  1436. context
  1437. );
  1438. expect(result).toBe('include/myheader.h');
  1439. });
  1440. it('should resolve include trying multiple extensions', () => {
  1441. const context: ResolutionContext = {
  1442. getNodesInFile: () => [],
  1443. getNodesByName: () => [],
  1444. getNodesByQualifiedName: () => [],
  1445. getNodesByKind: () => [],
  1446. // myclass.h does not exist, but myclass.hpp does
  1447. fileExists: (p) => p === 'include/myclass.hpp',
  1448. readFile: () => null,
  1449. getProjectRoot: () => '',
  1450. getAllFiles: () => ['include/myclass.hpp', 'src/main.cpp'],
  1451. getCppIncludeDirs: () => ['include'],
  1452. };
  1453. const result = resolveImportPath(
  1454. 'myclass',
  1455. 'src/main.cpp',
  1456. 'cpp',
  1457. context
  1458. );
  1459. expect(result).toBe('include/myclass.hpp');
  1460. });
  1461. it('should return null for system headers', () => {
  1462. const context: ResolutionContext = {
  1463. getNodesInFile: () => [],
  1464. getNodesByName: () => [],
  1465. getNodesByQualifiedName: () => [],
  1466. getNodesByKind: () => [],
  1467. fileExists: () => true,
  1468. readFile: () => null,
  1469. getProjectRoot: () => '',
  1470. getAllFiles: () => [],
  1471. };
  1472. // C standard library header
  1473. expect(resolveImportPath('stdio.h', 'main.c', 'c', context)).toBeNull();
  1474. // C++ standard library header
  1475. expect(resolveImportPath('vector', 'main.cpp', 'cpp', context)).toBeNull();
  1476. // C++ C-wrapper header
  1477. expect(resolveImportPath('cstdio', 'main.cpp', 'cpp', context)).toBeNull();
  1478. });
  1479. it('should return null for single-component third-party paths that cannot be resolved', () => {
  1480. const context: ResolutionContext = {
  1481. getNodesInFile: () => [],
  1482. getNodesByName: () => [],
  1483. getNodesByQualifiedName: () => [],
  1484. getNodesByKind: () => [],
  1485. fileExists: () => false,
  1486. readFile: () => null,
  1487. getProjectRoot: () => '',
  1488. getAllFiles: () => [],
  1489. getCppIncludeDirs: () => [],
  1490. };
  1491. // Third-party bare header without path — not resolvable, returns null
  1492. const result = resolveImportPath(
  1493. 'openssl/ssl.h',
  1494. 'main.cpp',
  1495. 'cpp',
  1496. context
  1497. );
  1498. expect(result).toBeNull();
  1499. });
  1500. it('should not filter project headers with path separators', () => {
  1501. const context: ResolutionContext = {
  1502. getNodesInFile: () => [],
  1503. getNodesByName: () => [],
  1504. getNodesByQualifiedName: () => [],
  1505. getNodesByKind: () => [],
  1506. fileExists: (p) => p === 'mylib/utils.h',
  1507. readFile: () => null,
  1508. getProjectRoot: () => '',
  1509. getAllFiles: () => ['mylib/utils.h'],
  1510. };
  1511. // Path with separator should NOT be filtered as external
  1512. const result = resolveImportPath(
  1513. 'mylib/utils.h',
  1514. 'main.c',
  1515. 'c',
  1516. context
  1517. );
  1518. expect(result).toBe('mylib/utils.h');
  1519. });
  1520. it('should extract C/C++ import mappings from #include directives', () => {
  1521. const code = `#include <iostream>
  1522. #include "myheader.h"
  1523. #include "utils/helpers.hpp"`;
  1524. const mappings = extractImportMappings('main.cpp', code, 'cpp');
  1525. expect(mappings.length).toBe(3);
  1526. expect(mappings[0]).toEqual({
  1527. localName: 'iostream',
  1528. exportedName: '*',
  1529. source: 'iostream',
  1530. isDefault: false,
  1531. isNamespace: true,
  1532. });
  1533. expect(mappings[1]).toEqual({
  1534. localName: 'myheader',
  1535. exportedName: '*',
  1536. source: 'myheader.h',
  1537. isDefault: false,
  1538. isNamespace: true,
  1539. });
  1540. expect(mappings[2]).toEqual({
  1541. localName: 'helpers',
  1542. exportedName: '*',
  1543. source: 'utils/helpers.hpp',
  1544. isDefault: false,
  1545. isNamespace: true,
  1546. });
  1547. });
  1548. it('should discover include directories from compile_commands.json', () => {
  1549. // Create a temp project with compile_commands.json
  1550. const tempProject = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-cpp-test-'));
  1551. try {
  1552. const compileDb = [
  1553. {
  1554. directory: tempProject,
  1555. command: 'g++ -Iinclude -Isrc/lib -isystem /usr/include -c src/main.cpp',
  1556. file: 'src/main.cpp',
  1557. },
  1558. ];
  1559. fs.writeFileSync(
  1560. path.join(tempProject, 'compile_commands.json'),
  1561. JSON.stringify(compileDb)
  1562. );
  1563. // Create the include dirs so they exist
  1564. fs.mkdirSync(path.join(tempProject, 'include'), { recursive: true });
  1565. fs.mkdirSync(path.join(tempProject, 'src', 'lib'), { recursive: true });
  1566. clearCppIncludeDirCache();
  1567. const dirs = loadCppIncludeDirs(tempProject);
  1568. // Should find include and src/lib (relative to project root)
  1569. // /usr/include is absolute and outside project, should be excluded
  1570. expect(dirs).toContain('include');
  1571. expect(dirs).toContain('src/lib');
  1572. expect(dirs.some(d => d.includes('usr'))).toBe(false);
  1573. } finally {
  1574. fs.rmSync(tempProject, { recursive: true });
  1575. }
  1576. });
  1577. it('should fall back to heuristic include dirs when no compile_commands.json', () => {
  1578. const tempProject = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-cpp-test-'));
  1579. try {
  1580. // Create include/ and src/ directories with headers
  1581. fs.mkdirSync(path.join(tempProject, 'include'), { recursive: true });
  1582. fs.writeFileSync(path.join(tempProject, 'include', 'types.h'), '');
  1583. fs.mkdirSync(path.join(tempProject, 'src'), { recursive: true });
  1584. fs.writeFileSync(path.join(tempProject, 'src', 'main.cpp'), '');
  1585. // Create a directory without headers — should not be included
  1586. fs.mkdirSync(path.join(tempProject, 'docs'), { recursive: true });
  1587. clearCppIncludeDirCache();
  1588. const dirs = loadCppIncludeDirs(tempProject);
  1589. expect(dirs).toContain('include');
  1590. expect(dirs).toContain('src');
  1591. expect(dirs).not.toContain('docs');
  1592. } finally {
  1593. fs.rmSync(tempProject, { recursive: true });
  1594. }
  1595. });
  1596. // Documents the cross-language `.h` behavior. Objective-C and C++ share
  1597. // the `.h` extension, so in a mixed iOS-style project an Obj-C header
  1598. // dir gets claimed as a C/C++ include dir too. That's intentional — a
  1599. // C++ file legitimately can `#include "Foo.h"` against an Obj-C header
  1600. // (Obj-C++ / .mm callers), and false-positive inclusion is far cheaper
  1601. // than missing real resolutions. The test pins this so a later
  1602. // "exclude objc dirs" refactor breaks loudly and reviewers see the
  1603. // trade-off explicitly.
  1604. it('heuristic claims any top-level dir containing .h files, including Obj-C', () => {
  1605. const tempProject = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-cpp-test-'));
  1606. try {
  1607. // C++ side: an `cppmod` dir with a .hpp (C++-only extension)
  1608. fs.mkdirSync(path.join(tempProject, 'cppmod'), { recursive: true });
  1609. fs.writeFileSync(path.join(tempProject, 'cppmod', 'shared.hpp'), '');
  1610. // Obj-C side: an `iosmod` dir with .h + .m (no .cpp/.hpp).
  1611. fs.mkdirSync(path.join(tempProject, 'iosmod'), { recursive: true });
  1612. fs.writeFileSync(path.join(tempProject, 'iosmod', 'View.h'), '');
  1613. fs.writeFileSync(path.join(tempProject, 'iosmod', 'View.m'), '');
  1614. clearCppIncludeDirCache();
  1615. const dirs = loadCppIncludeDirs(tempProject);
  1616. // Both included — Obj-C dirs are intentionally allowed.
  1617. expect(dirs).toContain('cppmod');
  1618. expect(dirs).toContain('iosmod');
  1619. } finally {
  1620. fs.rmSync(tempProject, { recursive: true });
  1621. }
  1622. });
  1623. // End-to-end: ensure `#include "X.h"` produces a file→file `imports` edge
  1624. // in the actual indexing pipeline (not just a phantom file→import-node
  1625. // edge). This pins the include-dir resolution path so the headline PR
  1626. // feature can't silently regress to a no-op in the indexing flow.
  1627. it('connects #include to the real header file via include-dir scan (end-to-end)', async () => {
  1628. const tempProject = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-cpp-e2e-'));
  1629. try {
  1630. fs.mkdirSync(path.join(tempProject, 'include'), { recursive: true });
  1631. fs.mkdirSync(path.join(tempProject, 'src'), { recursive: true });
  1632. fs.writeFileSync(
  1633. path.join(tempProject, 'include', 'utils.h'),
  1634. `#ifndef UTILS_H\n#define UTILS_H\nint add(int, int);\n#endif\n`
  1635. );
  1636. fs.writeFileSync(
  1637. path.join(tempProject, 'src', 'main.cpp'),
  1638. `#include "utils.h"\n#include <vector>\nint main(){ return add(1,2); }\n`
  1639. );
  1640. clearCppIncludeDirCache();
  1641. cg = await CodeGraph.init(tempProject, { index: true });
  1642. // Sanity: file nodes exist for the header and the cpp.
  1643. const allFiles = cg.getStats();
  1644. expect(allFiles.fileCount).toBe(2);
  1645. // The `#include "utils.h"` edge should target the real
  1646. // `include/utils.h` file node — not a floating `import` node
  1647. // living inside main.cpp.
  1648. const db = DatabaseConnection.open(path.join(tempProject, '.codegraph', 'codegraph.db'));
  1649. const rows = db.getDb().prepare(`
  1650. select dst.kind as dstKind, dst.file_path as dstPath
  1651. from edges e
  1652. join nodes src on e.source = src.id
  1653. join nodes dst on e.target = dst.id
  1654. where e.kind = 'imports'
  1655. and src.kind = 'file'
  1656. and src.file_path = 'src/main.cpp'
  1657. `).all() as Array<{ dstKind: string; dstPath: string }>;
  1658. const resolvedToHeader = rows.find(
  1659. (r) => r.dstKind === 'file' && r.dstPath === 'include/utils.h'
  1660. );
  1661. expect(resolvedToHeader, 'main.cpp → include/utils.h imports edge missing').toBeDefined();
  1662. // `<vector>` should NOT produce a file edge — it's a stdlib header.
  1663. const stdlibFile = rows.find(
  1664. (r) => r.dstKind === 'file' && r.dstPath && r.dstPath.endsWith('vector')
  1665. );
  1666. expect(stdlibFile).toBeUndefined();
  1667. } finally {
  1668. fs.rmSync(tempProject, { recursive: true, force: true });
  1669. }
  1670. });
  1671. });
  1672. });