| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495 |
- /**
- * Resolution Module Tests
- *
- * Tests for Phase 3: Reference Resolution
- */
- import { describe, it, expect, beforeEach, afterEach } from 'vitest';
- import * as fs from 'fs';
- import * as path from 'path';
- import * as os from 'os';
- import { CodeGraph } from '../src';
- import { Node, UnresolvedReference } from '../src/types';
- import { ReferenceResolver, createResolver, ResolutionContext } from '../src/resolution';
- import { matchReference } from '../src/resolution/name-matcher';
- import { resolveImportPath, extractImportMappings, loadCppIncludeDirs, clearCppIncludeDirCache } from '../src/resolution/import-resolver';
- import { detectFrameworks, getAllFrameworkResolvers } from '../src/resolution/frameworks';
- import { QueryBuilder } from '../src/db/queries';
- import { DatabaseConnection } from '../src/db';
- describe('Resolution Module', () => {
- let tempDir: string;
- let cg: CodeGraph;
- beforeEach(() => {
- // Create temp directory
- tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-resolution-test-'));
- });
- afterEach(() => {
- // Clean up
- if (cg) {
- cg.destroy();
- } else if (fs.existsSync(tempDir)) {
- fs.rmSync(tempDir, { recursive: true });
- }
- });
- describe('Name Matcher', () => {
- it('should match exact name references', () => {
- // Create a mock context
- const mockNodes: Node[] = [
- {
- id: 'func:test.ts:myFunction:10',
- kind: 'function',
- name: 'myFunction',
- qualifiedName: 'test.ts::myFunction',
- filePath: 'test.ts',
- language: 'typescript',
- startLine: 10,
- endLine: 20,
- startColumn: 0,
- endColumn: 0,
- updatedAt: Date.now(),
- },
- ];
- const context: ResolutionContext = {
- getNodesInFile: () => mockNodes,
- getNodesByName: (name) => mockNodes.filter((n) => n.name === name),
- getNodesByQualifiedName: () => [],
- getNodesByKind: () => [],
- fileExists: () => true,
- readFile: () => null,
- getProjectRoot: () => '/test',
- getAllFiles: () => ['test.ts'],
- };
- const ref = {
- fromNodeId: 'caller:main.ts:caller:5',
- referenceName: 'myFunction',
- referenceKind: 'calls' as const,
- line: 5,
- column: 10,
- filePath: 'main.ts',
- language: 'typescript' as const,
- };
- const result = matchReference(ref, context);
- expect(result).not.toBeNull();
- expect(result?.targetNodeId).toBe('func:test.ts:myFunction:10');
- expect(result?.resolvedBy).toBe('exact-match');
- });
- it('should prefer same-module candidates over cross-module matches', () => {
- // Simulates a Python monorepo where multiple apps define navigate()
- const candidateA: Node = {
- id: 'func:apps/app_a/src/server.py:navigate:10',
- kind: 'function',
- name: 'navigate',
- qualifiedName: 'apps/app_a/src/server.py::navigate',
- filePath: 'apps/app_a/src/server.py',
- language: 'python',
- startLine: 10,
- endLine: 20,
- startColumn: 0,
- endColumn: 0,
- updatedAt: Date.now(),
- };
- const candidateB: Node = {
- id: 'func:apps/app_b/src/server.py:navigate:15',
- kind: 'function',
- name: 'navigate',
- qualifiedName: 'apps/app_b/src/server.py::navigate',
- filePath: 'apps/app_b/src/server.py',
- language: 'python',
- startLine: 15,
- endLine: 25,
- startColumn: 0,
- endColumn: 0,
- updatedAt: Date.now(),
- };
- const context: ResolutionContext = {
- getNodesInFile: () => [],
- getNodesByName: (name) => name === 'navigate' ? [candidateA, candidateB] : [],
- getNodesByQualifiedName: () => [],
- getNodesByKind: () => [],
- fileExists: () => true,
- readFile: () => null,
- getProjectRoot: () => '/test',
- getAllFiles: () => [],
- getNodesByLowerName: () => [],
- getImportMappings: () => [],
- };
- // Reference from app_a should resolve to app_a's navigate, not app_b's
- const ref = {
- fromNodeId: 'func:apps/app_a/src/handler.py:handler:5',
- referenceName: 'navigate',
- referenceKind: 'calls' as const,
- line: 5,
- column: 10,
- filePath: 'apps/app_a/src/handler.py',
- language: 'python' as const,
- };
- const result = matchReference(ref, context);
- expect(result).not.toBeNull();
- expect(result?.targetNodeId).toBe('func:apps/app_a/src/server.py:navigate:10');
- expect(result?.resolvedBy).toBe('exact-match');
- });
- it('should lower confidence for cross-module exact matches', () => {
- // Only one candidate but in a completely different module
- const candidates: Node[] = [
- {
- id: 'func:apps/app_b/src/server.py:navigate:10',
- kind: 'function',
- name: 'navigate',
- qualifiedName: 'apps/app_b/src/server.py::navigate',
- filePath: 'apps/app_b/src/server.py',
- language: 'python',
- startLine: 10,
- endLine: 20,
- startColumn: 0,
- endColumn: 0,
- updatedAt: Date.now(),
- },
- {
- id: 'func:apps/app_c/src/server.py:navigate:10',
- kind: 'function',
- name: 'navigate',
- qualifiedName: 'apps/app_c/src/server.py::navigate',
- filePath: 'apps/app_c/src/server.py',
- language: 'python',
- startLine: 10,
- endLine: 20,
- startColumn: 0,
- endColumn: 0,
- updatedAt: Date.now(),
- },
- ];
- const context: ResolutionContext = {
- getNodesInFile: () => [],
- getNodesByName: (name) => name === 'navigate' ? candidates : [],
- getNodesByQualifiedName: () => [],
- getNodesByKind: () => [],
- fileExists: () => true,
- readFile: () => null,
- getProjectRoot: () => '/test',
- getAllFiles: () => [],
- getNodesByLowerName: () => [],
- getImportMappings: () => [],
- };
- // Reference from app_a — neither candidate is in the same module
- const ref = {
- fromNodeId: 'func:apps/app_a/src/handler.py:handler:5',
- referenceName: 'navigate',
- referenceKind: 'calls' as const,
- line: 5,
- column: 10,
- filePath: 'apps/app_a/src/handler.py',
- language: 'python' as const,
- };
- const result = matchReference(ref, context);
- // Should still resolve but with low confidence
- expect(result).not.toBeNull();
- expect(result?.confidence).toBeLessThanOrEqual(0.4);
- });
- it('should match qualified name references', () => {
- const mockClassNode: Node = {
- id: 'class:user.ts:User:5',
- kind: 'class',
- name: 'User',
- qualifiedName: 'user.ts::User',
- filePath: 'user.ts',
- language: 'typescript',
- startLine: 5,
- endLine: 30,
- startColumn: 0,
- endColumn: 0,
- updatedAt: Date.now(),
- };
- const mockMethodNode: Node = {
- id: 'method:user.ts:User.save:15',
- kind: 'method',
- name: 'save',
- qualifiedName: 'user.ts::User::save',
- filePath: 'user.ts',
- language: 'typescript',
- startLine: 15,
- endLine: 25,
- startColumn: 0,
- endColumn: 0,
- updatedAt: Date.now(),
- };
- const context: ResolutionContext = {
- getNodesInFile: (fp) => fp === 'user.ts' ? [mockClassNode, mockMethodNode] : [],
- getNodesByName: (name) => {
- if (name === 'User') return [mockClassNode];
- if (name === 'save') return [mockMethodNode];
- return [];
- },
- getNodesByQualifiedName: (qn) => {
- if (qn === 'user.ts::User::save') return [mockMethodNode];
- return [];
- },
- getNodesByKind: () => [],
- fileExists: () => true,
- readFile: () => null,
- getProjectRoot: () => '/test',
- getAllFiles: () => ['user.ts'],
- };
- const ref = {
- fromNodeId: 'caller:main.ts:main:5',
- referenceName: 'User.save',
- referenceKind: 'calls' as const,
- line: 5,
- column: 10,
- filePath: 'main.ts',
- language: 'typescript' as const,
- };
- const result = matchReference(ref, context);
- expect(result).not.toBeNull();
- expect(result?.targetNodeId).toBe('method:user.ts:User.save:15');
- });
- });
- describe('Import Resolver', () => {
- it('should resolve relative import paths', () => {
- const context: ResolutionContext = {
- getNodesInFile: () => [],
- getNodesByName: () => [],
- getNodesByQualifiedName: () => [],
- getNodesByKind: () => [],
- fileExists: (p) => p === 'src/components/utils.ts' || p === 'src/components/utils/index.ts',
- readFile: () => null,
- getProjectRoot: () => '',
- getAllFiles: () => ['src/components/utils.ts', 'src/components/utils/index.ts'],
- };
- const result = resolveImportPath(
- './utils',
- 'src/components/Button.ts',
- 'typescript',
- context
- );
- expect(result).toBe('src/components/utils.ts');
- });
- it('should resolve parent directory imports', () => {
- const context: ResolutionContext = {
- getNodesInFile: () => [],
- getNodesByName: () => [],
- getNodesByQualifiedName: () => [],
- getNodesByKind: () => [],
- fileExists: (p) => p === 'src/helpers.ts' || p === 'src/helpers/index.ts',
- readFile: () => null,
- getProjectRoot: () => '',
- getAllFiles: () => ['src/helpers.ts', 'src/helpers/index.ts'],
- };
- const result = resolveImportPath(
- '../helpers',
- 'src/components/Button.ts',
- 'typescript',
- context
- );
- expect(result).toBe('src/helpers.ts');
- });
- it('should extract JS/TS import mappings', () => {
- const content = `
- import { foo } from './foo';
- import bar from '../bar';
- import * as utils from './utils';
- import { baz, qux } from './baz';
- `;
- const mappings = extractImportMappings(
- 'src/index.ts',
- content,
- 'typescript'
- );
- expect(mappings.length).toBeGreaterThan(0);
- expect(mappings.some((m) => m.localName === 'foo')).toBe(true);
- expect(mappings.some((m) => m.localName === 'bar')).toBe(true);
- });
- it('should extract Python import mappings', () => {
- const content = `
- from utils import helper
- from .models import User
- import os
- from ..services import auth_service
- `;
- const mappings = extractImportMappings(
- 'src/main.py',
- content,
- 'python'
- );
- expect(mappings.length).toBeGreaterThan(0);
- expect(mappings.some((m) => m.localName === 'helper')).toBe(true);
- expect(mappings.some((m) => m.localName === 'User')).toBe(true);
- });
- });
- describe('Framework Detection', () => {
- it('should detect React framework', () => {
- const context: ResolutionContext = {
- getNodesInFile: () => [],
- getNodesByName: () => [],
- getNodesByQualifiedName: () => [],
- getNodesByKind: () => [],
- fileExists: () => false,
- readFile: (p) => {
- if (p === 'package.json') {
- return JSON.stringify({
- dependencies: { react: '^18.0.0' },
- });
- }
- return null;
- },
- getProjectRoot: () => '/test',
- getAllFiles: () => ['package.json', 'src/App.tsx'],
- };
- const frameworks = detectFrameworks(context);
- expect(frameworks.some((f) => f.name === 'react')).toBe(true);
- });
- it('should detect Express framework', () => {
- const context: ResolutionContext = {
- getNodesInFile: () => [],
- getNodesByName: () => [],
- getNodesByQualifiedName: () => [],
- getNodesByKind: () => [],
- fileExists: () => false,
- readFile: (p) => {
- if (p === 'package.json') {
- return JSON.stringify({
- dependencies: { express: '^4.18.0' },
- });
- }
- return null;
- },
- getProjectRoot: () => '/test',
- getAllFiles: () => ['package.json', 'src/app.js'],
- };
- const frameworks = detectFrameworks(context);
- expect(frameworks.some((f) => f.name === 'express')).toBe(true);
- });
- it('should detect Laravel framework', () => {
- const context: ResolutionContext = {
- getNodesInFile: () => [],
- getNodesByName: () => [],
- getNodesByQualifiedName: () => [],
- getNodesByKind: () => [],
- fileExists: (p) => p === 'artisan',
- readFile: () => null,
- getProjectRoot: () => '/test',
- getAllFiles: () => ['artisan', 'app/Http/Kernel.php'],
- };
- const frameworks = detectFrameworks(context);
- expect(frameworks.some((f) => f.name === 'laravel')).toBe(true);
- });
- it('should return all framework resolvers', () => {
- const resolvers = getAllFrameworkResolvers();
- expect(resolvers.length).toBeGreaterThan(0);
- expect(resolvers.some((r) => r.name === 'react')).toBe(true);
- expect(resolvers.some((r) => r.name === 'express')).toBe(true);
- expect(resolvers.some((r) => r.name === 'laravel')).toBe(true);
- });
- });
- describe('React Framework Resolver', () => {
- it('should resolve React component references', () => {
- const mockNodes: Node[] = [
- {
- id: 'component:src/Button.tsx:Button:5',
- kind: 'component',
- name: 'Button',
- qualifiedName: 'src/Button.tsx::Button',
- filePath: 'src/Button.tsx',
- language: 'tsx',
- startLine: 5,
- endLine: 20,
- startColumn: 0,
- endColumn: 0,
- updatedAt: Date.now(),
- },
- ];
- const context: ResolutionContext = {
- getNodesInFile: (fp) => (fp === 'src/Button.tsx' ? mockNodes : []),
- getNodesByName: () => mockNodes,
- getNodesByQualifiedName: () => [],
- getNodesByKind: () => [],
- fileExists: () => false,
- readFile: (p) => {
- if (p === 'package.json') {
- return JSON.stringify({ dependencies: { react: '^18.0.0' } });
- }
- return null;
- },
- getProjectRoot: () => '/test',
- getAllFiles: () => ['package.json', 'src/Button.tsx', 'src/App.tsx'],
- };
- const frameworks = detectFrameworks(context);
- const reactResolver = frameworks.find((f) => f.name === 'react');
- expect(reactResolver).toBeDefined();
- const ref = {
- fromNodeId: 'component:src/App.tsx:App:1',
- referenceName: 'Button',
- referenceKind: 'renders' as const,
- line: 10,
- column: 5,
- filePath: 'src/App.tsx',
- language: 'typescript' as const,
- };
- const result = reactResolver!.resolve(ref, context);
- expect(result).not.toBeNull();
- expect(result?.targetNodeId).toBe('component:src/Button.tsx:Button:5');
- });
- it('should resolve custom hook references', () => {
- const mockNodes: Node[] = [
- {
- id: 'hook:src/hooks/useAuth.ts:useAuth:1',
- kind: 'function',
- name: 'useAuth',
- qualifiedName: 'src/hooks/useAuth.ts::useAuth',
- filePath: 'src/hooks/useAuth.ts',
- language: 'typescript',
- startLine: 1,
- endLine: 20,
- startColumn: 0,
- endColumn: 0,
- updatedAt: Date.now(),
- },
- ];
- const context: ResolutionContext = {
- getNodesInFile: (fp) => (fp.includes('useAuth') ? mockNodes : []),
- getNodesByName: () => mockNodes,
- getNodesByQualifiedName: () => [],
- getNodesByKind: () => [],
- fileExists: () => false,
- readFile: (p) => {
- if (p === 'package.json') {
- return JSON.stringify({ dependencies: { react: '^18.0.0' } });
- }
- return null;
- },
- getProjectRoot: () => '/test',
- getAllFiles: () => ['package.json', 'src/hooks/useAuth.ts'],
- };
- const frameworks = detectFrameworks(context);
- const reactResolver = frameworks.find((f) => f.name === 'react');
- const ref = {
- fromNodeId: 'component:src/App.tsx:App:1',
- referenceName: 'useAuth',
- referenceKind: 'calls' as const,
- line: 5,
- column: 10,
- filePath: 'src/App.tsx',
- language: 'typescript' as const,
- };
- const result = reactResolver!.resolve(ref, context);
- expect(result).not.toBeNull();
- expect(result?.targetNodeId).toBe('hook:src/hooks/useAuth.ts:useAuth:1');
- });
- });
- describe('Integration Tests', () => {
- it('should create resolver from CodeGraph instance', async () => {
- // Create a simple TypeScript project
- fs.writeFileSync(
- path.join(tempDir, 'package.json'),
- JSON.stringify({ name: 'test', dependencies: { react: '^18.0.0' } })
- );
- const srcDir = path.join(tempDir, 'src');
- fs.mkdirSync(srcDir);
- // Create utility file
- fs.writeFileSync(
- path.join(srcDir, 'utils.ts'),
- `export function formatDate(date: Date): string {
- return date.toISOString();
- }
- export function parseDate(str: string): Date {
- return new Date(str);
- }`
- );
- // Create main file that uses utils
- fs.writeFileSync(
- path.join(srcDir, 'main.ts'),
- `import { formatDate, parseDate } from './utils';
- function processDate(input: string): string {
- const date = parseDate(input);
- return formatDate(date);
- }`
- );
- // Initialize and index
- cg = await CodeGraph.init(tempDir, { index: true });
- // Check that resolver detected React framework
- const frameworks = cg.getDetectedFrameworks();
- expect(frameworks).toContain('react');
- // Get stats to verify indexing worked
- const stats = cg.getStats();
- expect(stats.fileCount).toBe(2);
- expect(stats.nodeCount).toBeGreaterThan(0);
- });
- it('should resolve references after indexing', async () => {
- // Create a project with references
- const srcDir = path.join(tempDir, 'src');
- fs.mkdirSync(srcDir, { recursive: true });
- fs.writeFileSync(
- path.join(srcDir, 'helper.ts'),
- `export function helperFunction(): void {
- console.log('helper');
- }`
- );
- fs.writeFileSync(
- path.join(srcDir, 'main.ts'),
- `import { helperFunction } from './helper';
- function main(): void {
- helperFunction();
- }`
- );
- cg = await CodeGraph.init(tempDir, { index: true });
- // Run reference resolution
- const result = cg.resolveReferences();
- // Should have attempted resolution
- expect(result.stats.total).toBeGreaterThanOrEqual(0);
- });
- it('promotes calls→instantiates when target resolves to a class (Python)', async () => {
- // Python has no `new` keyword — `Foo()` is the standard
- // instantiation syntax. Extraction can't tell that apart from
- // a function call without symbol info, so it emits a `calls`
- // ref. Resolution promotes it to `instantiates` once the
- // target is known to be a class.
- const srcDir = path.join(tempDir, 'src');
- fs.mkdirSync(srcDir, { recursive: true });
- fs.writeFileSync(
- path.join(srcDir, 'app.py'),
- `class UserService:
- def __init__(self):
- self.db = None
- def bootstrap():
- return UserService()
- `
- );
- cg = await CodeGraph.init(tempDir, { index: true });
- cg.resolveReferences();
- const bootstrap = cg
- .getNodesByKind('function')
- .find((n) => n.name === 'bootstrap');
- expect(bootstrap).toBeDefined();
- const outgoing = cg.getOutgoingEdges(bootstrap!.id);
- const instantiates = outgoing.find((e) => e.kind === 'instantiates');
- expect(instantiates).toBeDefined();
- // Same edge must NOT also appear as a `calls` edge — promotion
- // replaces the kind, doesn't duplicate.
- const callsToUserService = outgoing.filter(
- (e) => e.kind === 'calls' && e.target === instantiates!.target
- );
- expect(callsToUserService).toHaveLength(0);
- });
- it('resolves Go cross-package qualified calls via go.mod module path (#388)', async () => {
- // Pre-#388, every `pkga.FuncX(...)` call in a Go monorepo was flagged
- // external (isExternalImport returned true for any non-`/internal/`
- // import without `.`-prefix) and resolution fell through to name-match
- // with path proximity — recall on cross-package callers was ~<1%.
- fs.writeFileSync(
- path.join(tempDir, 'go.mod'),
- 'module github.com/example/myproject\n\ngo 1.21\n'
- );
- const pkgaDir = path.join(tempDir, 'pkga');
- const pkgbDir = path.join(tempDir, 'pkgb');
- const pkgcDir = path.join(tempDir, 'pkgc');
- fs.mkdirSync(pkgaDir);
- fs.mkdirSync(pkgbDir);
- fs.mkdirSync(pkgcDir);
- // Same-name exported function in two packages — only the imported one
- // should resolve. Exercises disambiguation, not just connectivity.
- fs.writeFileSync(
- path.join(pkgaDir, 'conv.go'),
- 'package pkga\nfunc Convert(x int) int { return x * 2 }\n'
- );
- fs.writeFileSync(
- path.join(pkgbDir, 'conv.go'),
- 'package pkgb\nfunc Convert(x int) int { return x + 1 }\n'
- );
- fs.writeFileSync(
- path.join(pkgcDir, 'use.go'),
- `package pkgc
- import "github.com/example/myproject/pkga"
- func UsePkga() {
- pkga.Convert(5)
- }
- `
- );
- cg = await CodeGraph.init(tempDir, { index: true });
- const usePkga = cg.getNodesByKind('function').filter((n) => n.name ==='UsePkga')[0];
- expect(usePkga).toBeDefined();
- const outgoing = cg.getOutgoingEdges(usePkga!.id);
- const callEdges = outgoing.filter((e) => e.kind === 'calls');
- expect(callEdges).toHaveLength(1);
- const target = cg.getNode(callEdges[0]!.target);
- expect(target?.name).toBe('Convert');
- // Critical: the resolver must pick the imported pkga's Convert,
- // not pkgb's. With the broken (pre-fix) resolver this lands on
- // whichever Convert happens to be cheaper under path proximity.
- expect(target?.filePath.replace(/\\/g, '/')).toBe('pkga/conv.go');
- });
- it('resolves Go aliased imports across packages (#388)', async () => {
- fs.writeFileSync(
- path.join(tempDir, 'go.mod'),
- 'module github.com/example/myproject\n\ngo 1.21\n'
- );
- fs.mkdirSync(path.join(tempDir, 'pkgb'));
- fs.mkdirSync(path.join(tempDir, 'pkgd'));
- fs.writeFileSync(
- path.join(tempDir, 'pkgb', 'lib.go'),
- 'package pkgb\nfunc Compute(x int) int { return x }\n'
- );
- fs.writeFileSync(
- path.join(tempDir, 'pkgd', 'use.go'),
- `package pkgd
- import (
- "fmt"
- alias "github.com/example/myproject/pkgb"
- )
- func UseAliased() {
- fmt.Println("hi")
- alias.Compute(3)
- }
- `
- );
- cg = await CodeGraph.init(tempDir, { index: true });
- const useAliased = cg.getNodesByKind('function').filter((n) => n.name ==='UseAliased')[0];
- expect(useAliased).toBeDefined();
- const calls = cg.getOutgoingEdges(useAliased!.id).filter((e) => e.kind === 'calls');
- // fmt.Println is stdlib — must stay external. alias.Compute must resolve.
- expect(calls).toHaveLength(1);
- const target = cg.getNode(calls[0]!.target);
- expect(target?.name).toBe('Compute');
- expect(target?.filePath.replace(/\\/g, '/')).toBe('pkgb/lib.go');
- });
- it('TS type_alias object-shape members resolve method calls (#359)', async () => {
- // Pre-#359, `recorder.stop()` (recorder: RecorderHandle) attached
- // to `StdioMcpClient.stop` in a sibling directory via path-proximity
- // because the type_alias had no `stop` node — only the unrelated
- // class did. Now type_alias produces member nodes (property/method),
- // so the camelCase receiver↔type word overlap pulls the call to
- // `RecorderHandle::stop` instead of the look-alike class.
- fs.mkdirSync(path.join(tempDir, 'voice'));
- fs.mkdirSync(path.join(tempDir, 'codegraph'));
- fs.writeFileSync(
- path.join(tempDir, 'voice', 'recorder.ts'),
- `export type RecorderHandle = {
- wavPath: string;
- stop: () => Promise<{ ok: true }>;
- };
- `
- );
- fs.writeFileSync(
- path.join(tempDir, 'voice', 'controller.ts'),
- `import type { RecorderHandle } from "./recorder";
- export async function finaliseRecording(recorder: RecorderHandle) {
- return await recorder.stop();
- }
- `
- );
- fs.writeFileSync(
- path.join(tempDir, 'codegraph', 'stdio-client.ts'),
- `export class StdioMcpClient {
- private stopped = false;
- async stop(): Promise<void> { this.stopped = true; }
- }
- `
- );
- cg = await CodeGraph.init(tempDir, { index: true });
- const handleStop = cg
- .getNodesByKind('method')
- .find((n) => n.qualifiedName === 'RecorderHandle::stop');
- expect(handleStop).toBeDefined();
- const clientStop = cg
- .getNodesByKind('method')
- .find((n) => n.qualifiedName === 'StdioMcpClient::stop');
- expect(clientStop).toBeDefined();
- const handleCallers = cg.getIncomingEdges(handleStop!.id).filter((e) => e.kind === 'calls');
- const clientCallers = cg.getIncomingEdges(clientStop!.id).filter((e) => e.kind === 'calls');
- expect(handleCallers.length).toBeGreaterThanOrEqual(1);
- // The class method must have NO callers — voice/'s call must NOT
- // mis-attribute. A non-empty list would mean the false-positive
- // path is still firing.
- expect(clientCallers).toHaveLength(0);
- // Function-typed property surfaces as a `method` node, not `property`,
- // because `stop()` semantics at the call site are method semantics.
- expect(handleStop!.kind).toBe('method');
- });
- it('Java import disambiguates same-name classes across modules (#314)', async () => {
- // Pre-#314 the import resolver had no Java branch at all, so a
- // multi-module Maven repo where `dao/converter/FooConverter` and
- // `service/converter/FooConverter` both export a `convert` method
- // resolved by file-path proximity — picking whichever class was
- // closer to the caller, which is wrong any time the caller lives
- // in an equidistant cross-cutting module.
- const daoDir = path.join(tempDir, 'dao/src/main/java/com/example/dao/converter');
- const serviceDir = path.join(tempDir, 'service/src/main/java/com/example/service/converter');
- const webDir = path.join(tempDir, 'web/src/main/java/com/example/web');
- fs.mkdirSync(daoDir, { recursive: true });
- fs.mkdirSync(serviceDir, { recursive: true });
- fs.mkdirSync(webDir, { recursive: true });
- fs.writeFileSync(
- path.join(daoDir, 'FooConverter.java'),
- `package com.example.dao.converter;
- public class FooConverter { public String convert(String x) { return "dao:" + x; } }
- `
- );
- fs.writeFileSync(
- path.join(serviceDir, 'FooConverter.java'),
- `package com.example.service.converter;
- public class FooConverter { public String convert(String x) { return "svc:" + x; } }
- `
- );
- // The caller imports the SERVICE version — even though dao is
- // alphabetically/lexically first in the candidate list, the
- // import must trump that order.
- fs.writeFileSync(
- path.join(webDir, 'Handler.java'),
- `package com.example.web;
- import com.example.service.converter.FooConverter;
- public class Handler {
- private FooConverter fooConverter;
- public String use() { return fooConverter.convert("input"); }
- }
- `
- );
- cg = await CodeGraph.init(tempDir, { index: true });
- const use = cg
- .getNodesByKind('method')
- .find((n) => n.qualifiedName === 'Handler::use');
- expect(use).toBeDefined();
- const calls = cg.getOutgoingEdges(use!.id).filter((e) => e.kind === 'calls');
- expect(calls.length).toBeGreaterThanOrEqual(1);
- const target = cg.getNode(calls[0]!.target);
- expect(target?.name).toBe('convert');
- expect(target?.filePath.replace(/\\/g, '/')).toBe(
- 'service/src/main/java/com/example/service/converter/FooConverter.java'
- );
- });
- it('C# extracts references from method/property/field types (#381)', async () => {
- // Pre-#381, every C# project produced ZERO `references` edges:
- // csharp.ts was missing returnField, and the type-leaf walker
- // only recognized TS/Java's `type_identifier` nodes — C# uses
- // `identifier`/`predefined_type`/`qualified_name`/`generic_name`.
- const srcDir = path.join(tempDir, 'src');
- fs.mkdirSync(srcDir, { recursive: true });
- fs.writeFileSync(
- path.join(srcDir, 'Dtos.cs'),
- `namespace MyApp;
- public class SessionInfoDto { public string Id { get; set; } = ""; }
- public class UserDto { public string Name { get; set; } = ""; }
- `
- );
- fs.writeFileSync(
- path.join(srcDir, 'Service.cs'),
- `using System.Threading.Tasks;
- namespace MyApp;
- public class DataExporter
- {
- public SessionInfoDto Build(UserDto user, SessionInfoDto session) { return session; }
- public Task<SessionInfoDto> BuildAsync(UserDto user) { return Task.FromResult(new SessionInfoDto()); }
- public SessionInfoDto Latest { get; set; } = new();
- private UserDto _cached;
- }
- `
- );
- cg = await CodeGraph.init(tempDir, { index: true });
- const sessionDto = cg
- .getNodesByKind('class')
- .find((n) => n.name === 'SessionInfoDto');
- const userDto = cg
- .getNodesByKind('class')
- .find((n) => n.name === 'UserDto');
- expect(sessionDto).toBeDefined();
- expect(userDto).toBeDefined();
- const sessionIncoming = cg
- .getIncomingEdges(sessionDto!.id)
- .filter((e) => e.kind === 'references');
- const userIncoming = cg
- .getIncomingEdges(userDto!.id)
- .filter((e) => e.kind === 'references');
- // SessionInfoDto: Build return, Build param, BuildAsync return (inside Task<>), Latest property.
- // UserDto: Build param, BuildAsync param, _cached field.
- expect(sessionIncoming.length).toBeGreaterThanOrEqual(4);
- expect(userIncoming.length).toBeGreaterThanOrEqual(3);
- });
- it('Go: leaves stdlib calls (fmt.Println, etc.) external', async () => {
- fs.writeFileSync(
- path.join(tempDir, 'go.mod'),
- 'module github.com/example/myproject\n\ngo 1.21\n'
- );
- fs.writeFileSync(
- path.join(tempDir, 'main.go'),
- `package main
- import "fmt"
- func main() {
- fmt.Println("hi")
- }
- `
- );
- cg = await CodeGraph.init(tempDir, { index: true });
- const mainFn = cg.getNodesByKind('function').filter((n) => n.name ==='main')[0];
- const calls = cg.getOutgoingEdges(mainFn!.id).filter((e) => e.kind === 'calls');
- // No spurious in-project edge — fmt.* must stay unresolved/external.
- expect(calls).toHaveLength(0);
- });
- });
- describe('Name Matcher: kind bias for new ref kinds', () => {
- const baseContext = (candidates: Node[]): ResolutionContext => ({
- getNodesInFile: () => [],
- getNodesByName: (name) => candidates.filter((c) => c.name === name),
- getNodesByQualifiedName: () => [],
- getNodesByKind: () => [],
- fileExists: () => true,
- readFile: () => null,
- getProjectRoot: () => '/test',
- getAllFiles: () => [],
- getNodesByLowerName: () => [],
- getImportMappings: () => [],
- });
- it('prefers a class candidate over a function for `instantiates` refs', () => {
- // A class and a function share a name across the codebase.
- // Without the kind bias, the function (which gets the +25 `calls`
- // bonus historically applied to all candidates of that kind) would
- // win. Now the instantiates branch reverses it.
- const fn: Node = {
- id: 'func:utils.ts:Logger:5', kind: 'function', name: 'Logger',
- qualifiedName: 'utils.ts::Logger', filePath: 'utils.ts', language: 'typescript',
- startLine: 5, endLine: 7, startColumn: 0, endColumn: 0, updatedAt: Date.now(),
- };
- const cls: Node = {
- id: 'class:logger.ts:Logger:10', kind: 'class', name: 'Logger',
- qualifiedName: 'logger.ts::Logger', filePath: 'logger.ts', language: 'typescript',
- startLine: 10, endLine: 30, startColumn: 0, endColumn: 0, updatedAt: Date.now(),
- };
- const ref = {
- fromNodeId: 'func:main.ts:bootstrap:1',
- referenceName: 'Logger',
- referenceKind: 'instantiates' as const,
- line: 5, column: 0, filePath: 'main.ts', language: 'typescript' as const,
- };
- const result = matchReference(ref, baseContext([fn, cls]));
- expect(result?.targetNodeId).toBe('class:logger.ts:Logger:10');
- });
- it('prefers a function candidate over a non-function for `decorates` refs', () => {
- const variable: Node = {
- id: 'var:config.ts:Inject:5', kind: 'variable', name: 'Inject',
- qualifiedName: 'config.ts::Inject', filePath: 'config.ts', language: 'typescript',
- startLine: 5, endLine: 5, startColumn: 0, endColumn: 0, updatedAt: Date.now(),
- };
- const decorator: Node = {
- id: 'func:di.ts:Inject:10', kind: 'function', name: 'Inject',
- qualifiedName: 'di.ts::Inject', filePath: 'di.ts', language: 'typescript',
- startLine: 10, endLine: 20, startColumn: 0, endColumn: 0, updatedAt: Date.now(),
- };
- const ref = {
- fromNodeId: 'class:svc.ts:UserService:1',
- referenceName: 'Inject',
- referenceKind: 'decorates' as const,
- line: 5, column: 0, filePath: 'svc.ts', language: 'typescript' as const,
- };
- const result = matchReference(ref, baseContext([variable, decorator]));
- expect(result?.targetNodeId).toBe('func:di.ts:Inject:10');
- });
- });
- describe('tsconfig path aliases', () => {
- it('resolves an aliased import to the alias-mapped file (not a same-named file elsewhere)', async () => {
- // Two same-named exports in different directories. Without alias
- // resolution, name-matcher would pick whichever it finds first;
- // with alias resolution, the import path uniquely picks one.
- fs.mkdirSync(path.join(tempDir, 'src/utils'), { recursive: true });
- fs.mkdirSync(path.join(tempDir, 'src/legacy'), { recursive: true });
- fs.writeFileSync(
- path.join(tempDir, 'src/utils/format.ts'),
- `export function pickMe(): number { return 1; }\n`
- );
- fs.writeFileSync(
- path.join(tempDir, 'src/legacy/format.ts'),
- `export function pickMe(): number { return 99; }\n`
- );
- fs.writeFileSync(
- path.join(tempDir, 'src/main.ts'),
- `import { pickMe } from '@utils/format';\nexport function go(): number { return pickMe(); }\n`
- );
- fs.writeFileSync(
- path.join(tempDir, 'tsconfig.json'),
- JSON.stringify({
- compilerOptions: {
- baseUrl: './src',
- paths: { '@utils/*': ['utils/*'] },
- },
- })
- );
- cg = await CodeGraph.init(tempDir, { index: true });
- cg.resolveReferences();
- // The two pickMe nodes live in different files. The aliased
- // import should attach the call edge to the @utils-mapped one,
- // not the legacy duplicate.
- const all = cg.getNodesByKind('function').filter((n) => n.name === 'pickMe');
- const utilsNode = all.find((n) => n.filePath === 'src/utils/format.ts');
- const legacyNode = all.find((n) => n.filePath === 'src/legacy/format.ts');
- expect(utilsNode).toBeDefined();
- expect(legacyNode).toBeDefined();
- const utilsCallers = cg.getCallers(utilsNode!.id);
- const legacyCallers = cg.getCallers(legacyNode!.id);
- expect(utilsCallers.length).toBeGreaterThan(0);
- expect(utilsCallers.some((c) => c.node.filePath === 'src/main.ts')).toBe(true);
- // The legacy node should NOT have a caller from src/main.ts —
- // the alias correctly picked the utils version.
- expect(legacyCallers.some((c) => c.node.filePath === 'src/main.ts')).toBe(false);
- });
- it('falls back gracefully when tsconfig is absent', async () => {
- fs.mkdirSync(path.join(tempDir, 'src'), { recursive: true });
- fs.writeFileSync(
- path.join(tempDir, 'src/a.ts'),
- `export function aFn(): void {}\n`
- );
- fs.writeFileSync(
- path.join(tempDir, 'src/b.ts'),
- `import { aFn } from './a';\nexport function bFn(): void { aFn(); }\n`
- );
- cg = await CodeGraph.init(tempDir, { index: true });
- // No tsconfig present — index should still complete and the
- // relative-import-based call edge should be created.
- const aFn = cg.getNodesByKind('function').find((n) => n.name === 'aFn');
- expect(aFn).toBeDefined();
- const callers = cg.getCallers(aFn!.id);
- expect(callers.some((c) => c.node.filePath === 'src/b.ts')).toBe(true);
- });
- });
- describe('re-export chain following', () => {
- it('chases a 3-hop barrel chain (wildcard → named → declaration)', async () => {
- // main.ts → all.ts (wildcard) → index.ts (named) → auth.ts (declaration).
- // Without chain following, `signIn` resolves to nothing because
- // none of the barrel files declare it directly.
- fs.mkdirSync(path.join(tempDir, 'src/services'), { recursive: true });
- fs.writeFileSync(
- path.join(tempDir, 'src/services/auth.ts'),
- `export function signIn(): void {}\n`
- );
- fs.writeFileSync(
- path.join(tempDir, 'src/services/index.ts'),
- `export { signIn } from './auth';\n`
- );
- fs.writeFileSync(
- path.join(tempDir, 'src/all.ts'),
- `export * from './services/index';\n`
- );
- fs.writeFileSync(
- path.join(tempDir, 'src/main.ts'),
- `import { signIn } from './all';\nexport function go(): void { signIn(); }\n`
- );
- cg = await CodeGraph.init(tempDir, { index: true });
- cg.resolveReferences();
- const signInNode = cg
- .getNodesByKind('function')
- .find((n) => n.name === 'signIn' && n.filePath === 'src/services/auth.ts');
- expect(signInNode).toBeDefined();
- const callers = cg.getCallers(signInNode!.id);
- expect(callers.some((c) => c.node.filePath === 'src/main.ts')).toBe(true);
- });
- it('follows a renamed named re-export (export { foo as bar } from ...)', async () => {
- // The chase has to look up `foo` in the upstream module even
- // though the importer asked for `bar` — exercises the rename
- // branch of findExportedSymbol.
- fs.mkdirSync(path.join(tempDir, 'src'), { recursive: true });
- fs.writeFileSync(
- path.join(tempDir, 'src/auth.ts'),
- `export function signIn(): void {}\n`
- );
- fs.writeFileSync(
- path.join(tempDir, 'src/index.ts'),
- `export { signIn as login } from './auth';\n`
- );
- fs.writeFileSync(
- path.join(tempDir, 'src/main.ts'),
- `import { login } from './index';\nexport function go(): void { login(); }\n`
- );
- cg = await CodeGraph.init(tempDir, { index: true });
- cg.resolveReferences();
- const signInNode = cg
- .getNodesByKind('function')
- .find((n) => n.name === 'signIn' && n.filePath === 'src/auth.ts');
- expect(signInNode).toBeDefined();
- const callers = cg.getCallers(signInNode!.id);
- expect(callers.some((c) => c.node.filePath === 'src/main.ts')).toBe(true);
- });
- });
- describe('C/C++ Import Resolution', () => {
- afterEach(() => {
- clearCppIncludeDirCache();
- });
- it('should resolve C include to header in same directory', () => {
- const context: ResolutionContext = {
- getNodesInFile: () => [],
- getNodesByName: () => [],
- getNodesByQualifiedName: () => [],
- getNodesByKind: () => [],
- fileExists: (p) => p === 'utils.h',
- readFile: () => null,
- getProjectRoot: () => '',
- getAllFiles: () => ['utils.h', 'main.c'],
- };
- const result = resolveImportPath(
- 'utils.h',
- 'main.c',
- 'c',
- context
- );
- expect(result).toBe('utils.h');
- });
- it('should resolve C++ include with .hpp extension', () => {
- const context: ResolutionContext = {
- getNodesInFile: () => [],
- getNodesByName: () => [],
- getNodesByQualifiedName: () => [],
- getNodesByKind: () => [],
- fileExists: (p) => p === 'include/myclass.hpp',
- readFile: () => null,
- getProjectRoot: () => '',
- getAllFiles: () => ['include/myclass.hpp', 'src/main.cpp'],
- getCppIncludeDirs: () => ['include'],
- };
- const result = resolveImportPath(
- 'myclass.hpp',
- 'src/main.cpp',
- 'cpp',
- context
- );
- expect(result).toBe('include/myclass.hpp');
- });
- it('should resolve include with subdirectory path', () => {
- const context: ResolutionContext = {
- getNodesInFile: () => [],
- getNodesByName: () => [],
- getNodesByQualifiedName: () => [],
- getNodesByKind: () => [],
- fileExists: (p) => p === 'utils/helpers.h',
- readFile: () => null,
- getProjectRoot: () => '',
- getAllFiles: () => ['utils/helpers.h', 'main.c'],
- };
- const result = resolveImportPath(
- 'utils/helpers.h',
- 'main.c',
- 'c',
- context
- );
- expect(result).toBe('utils/helpers.h');
- });
- it('should resolve include via include directories', () => {
- const context: ResolutionContext = {
- getNodesInFile: () => [],
- getNodesByName: () => [],
- getNodesByQualifiedName: () => [],
- getNodesByKind: () => [],
- fileExists: (p) => p === 'include/myheader.h',
- readFile: () => null,
- getProjectRoot: () => '',
- getAllFiles: () => ['include/myheader.h', 'src/main.cpp'],
- getCppIncludeDirs: () => ['include'],
- };
- const result = resolveImportPath(
- 'myheader.h',
- 'src/main.cpp',
- 'cpp',
- context
- );
- expect(result).toBe('include/myheader.h');
- });
- it('should resolve include trying multiple extensions', () => {
- const context: ResolutionContext = {
- getNodesInFile: () => [],
- getNodesByName: () => [],
- getNodesByQualifiedName: () => [],
- getNodesByKind: () => [],
- // myclass.h does not exist, but myclass.hpp does
- fileExists: (p) => p === 'include/myclass.hpp',
- readFile: () => null,
- getProjectRoot: () => '',
- getAllFiles: () => ['include/myclass.hpp', 'src/main.cpp'],
- getCppIncludeDirs: () => ['include'],
- };
- const result = resolveImportPath(
- 'myclass',
- 'src/main.cpp',
- 'cpp',
- context
- );
- expect(result).toBe('include/myclass.hpp');
- });
- it('should return null for system headers', () => {
- const context: ResolutionContext = {
- getNodesInFile: () => [],
- getNodesByName: () => [],
- getNodesByQualifiedName: () => [],
- getNodesByKind: () => [],
- fileExists: () => true,
- readFile: () => null,
- getProjectRoot: () => '',
- getAllFiles: () => [],
- };
- // C standard library header
- expect(resolveImportPath('stdio.h', 'main.c', 'c', context)).toBeNull();
- // C++ standard library header
- expect(resolveImportPath('vector', 'main.cpp', 'cpp', context)).toBeNull();
- // C++ C-wrapper header
- expect(resolveImportPath('cstdio', 'main.cpp', 'cpp', context)).toBeNull();
- });
- it('should return null for single-component third-party paths that cannot be resolved', () => {
- const context: ResolutionContext = {
- getNodesInFile: () => [],
- getNodesByName: () => [],
- getNodesByQualifiedName: () => [],
- getNodesByKind: () => [],
- fileExists: () => false,
- readFile: () => null,
- getProjectRoot: () => '',
- getAllFiles: () => [],
- getCppIncludeDirs: () => [],
- };
- // Third-party bare header without path — not resolvable, returns null
- const result = resolveImportPath(
- 'openssl/ssl.h',
- 'main.cpp',
- 'cpp',
- context
- );
- expect(result).toBeNull();
- });
- it('should not filter project headers with path separators', () => {
- const context: ResolutionContext = {
- getNodesInFile: () => [],
- getNodesByName: () => [],
- getNodesByQualifiedName: () => [],
- getNodesByKind: () => [],
- fileExists: (p) => p === 'mylib/utils.h',
- readFile: () => null,
- getProjectRoot: () => '',
- getAllFiles: () => ['mylib/utils.h'],
- };
- // Path with separator should NOT be filtered as external
- const result = resolveImportPath(
- 'mylib/utils.h',
- 'main.c',
- 'c',
- context
- );
- expect(result).toBe('mylib/utils.h');
- });
- it('should extract C/C++ import mappings from #include directives', () => {
- const code = `#include <iostream>
- #include "myheader.h"
- #include "utils/helpers.hpp"`;
- const mappings = extractImportMappings('main.cpp', code, 'cpp');
- expect(mappings.length).toBe(3);
- expect(mappings[0]).toEqual({
- localName: 'iostream',
- exportedName: '*',
- source: 'iostream',
- isDefault: false,
- isNamespace: true,
- });
- expect(mappings[1]).toEqual({
- localName: 'myheader',
- exportedName: '*',
- source: 'myheader.h',
- isDefault: false,
- isNamespace: true,
- });
- expect(mappings[2]).toEqual({
- localName: 'helpers',
- exportedName: '*',
- source: 'utils/helpers.hpp',
- isDefault: false,
- isNamespace: true,
- });
- });
- it('should discover include directories from compile_commands.json', () => {
- // Create a temp project with compile_commands.json
- const tempProject = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-cpp-test-'));
- try {
- const compileDb = [
- {
- directory: tempProject,
- command: 'g++ -Iinclude -Isrc/lib -isystem /usr/include -c src/main.cpp',
- file: 'src/main.cpp',
- },
- ];
- fs.writeFileSync(
- path.join(tempProject, 'compile_commands.json'),
- JSON.stringify(compileDb)
- );
- // Create the include dirs so they exist
- fs.mkdirSync(path.join(tempProject, 'include'), { recursive: true });
- fs.mkdirSync(path.join(tempProject, 'src', 'lib'), { recursive: true });
- clearCppIncludeDirCache();
- const dirs = loadCppIncludeDirs(tempProject);
- // Should find include and src/lib (relative to project root)
- // /usr/include is absolute and outside project, should be excluded
- expect(dirs).toContain('include');
- expect(dirs).toContain('src/lib');
- expect(dirs.some(d => d.includes('usr'))).toBe(false);
- } finally {
- fs.rmSync(tempProject, { recursive: true });
- }
- });
- it('should fall back to heuristic include dirs when no compile_commands.json', () => {
- const tempProject = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-cpp-test-'));
- try {
- // Create include/ and src/ directories with headers
- fs.mkdirSync(path.join(tempProject, 'include'), { recursive: true });
- fs.writeFileSync(path.join(tempProject, 'include', 'types.h'), '');
- fs.mkdirSync(path.join(tempProject, 'src'), { recursive: true });
- fs.writeFileSync(path.join(tempProject, 'src', 'main.cpp'), '');
- // Create a directory without headers — should not be included
- fs.mkdirSync(path.join(tempProject, 'docs'), { recursive: true });
- clearCppIncludeDirCache();
- const dirs = loadCppIncludeDirs(tempProject);
- expect(dirs).toContain('include');
- expect(dirs).toContain('src');
- expect(dirs).not.toContain('docs');
- } finally {
- fs.rmSync(tempProject, { recursive: true });
- }
- });
- // Documents the cross-language `.h` behavior. Objective-C and C++ share
- // the `.h` extension, so in a mixed iOS-style project an Obj-C header
- // dir gets claimed as a C/C++ include dir too. That's intentional — a
- // C++ file legitimately can `#include "Foo.h"` against an Obj-C header
- // (Obj-C++ / .mm callers), and false-positive inclusion is far cheaper
- // than missing real resolutions. The test pins this so a later
- // "exclude objc dirs" refactor breaks loudly and reviewers see the
- // trade-off explicitly.
- it('heuristic claims any top-level dir containing .h files, including Obj-C', () => {
- const tempProject = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-cpp-test-'));
- try {
- // C++ side: an `cppmod` dir with a .hpp (C++-only extension)
- fs.mkdirSync(path.join(tempProject, 'cppmod'), { recursive: true });
- fs.writeFileSync(path.join(tempProject, 'cppmod', 'shared.hpp'), '');
- // Obj-C side: an `iosmod` dir with .h + .m (no .cpp/.hpp).
- fs.mkdirSync(path.join(tempProject, 'iosmod'), { recursive: true });
- fs.writeFileSync(path.join(tempProject, 'iosmod', 'View.h'), '');
- fs.writeFileSync(path.join(tempProject, 'iosmod', 'View.m'), '');
- clearCppIncludeDirCache();
- const dirs = loadCppIncludeDirs(tempProject);
- // Both included — Obj-C dirs are intentionally allowed.
- expect(dirs).toContain('cppmod');
- expect(dirs).toContain('iosmod');
- } finally {
- fs.rmSync(tempProject, { recursive: true });
- }
- });
- // End-to-end: ensure `#include "X.h"` produces a file→file `imports` edge
- // in the actual indexing pipeline (not just a phantom file→import-node
- // edge). This pins the include-dir resolution path so the headline PR
- // feature can't silently regress to a no-op in the indexing flow.
- it('connects #include to the real header file via include-dir scan (end-to-end)', async () => {
- const tempProject = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-cpp-e2e-'));
- try {
- fs.mkdirSync(path.join(tempProject, 'include'), { recursive: true });
- fs.mkdirSync(path.join(tempProject, 'src'), { recursive: true });
- fs.writeFileSync(
- path.join(tempProject, 'include', 'utils.h'),
- `#ifndef UTILS_H\n#define UTILS_H\nint add(int, int);\n#endif\n`
- );
- fs.writeFileSync(
- path.join(tempProject, 'src', 'main.cpp'),
- `#include "utils.h"\n#include <vector>\nint main(){ return add(1,2); }\n`
- );
- clearCppIncludeDirCache();
- cg = await CodeGraph.init(tempProject, { index: true });
- // Sanity: file nodes exist for the header and the cpp.
- const allFiles = cg.getStats();
- expect(allFiles.fileCount).toBe(2);
- // The `#include "utils.h"` edge should target the real
- // `include/utils.h` file node — not a floating `import` node
- // living inside main.cpp.
- const db = DatabaseConnection.open(path.join(tempProject, '.codegraph', 'codegraph.db'));
- const rows = db.getDb().prepare(`
- select dst.kind as dstKind, dst.file_path as dstPath
- from edges e
- join nodes src on e.source = src.id
- join nodes dst on e.target = dst.id
- where e.kind = 'imports'
- and src.kind = 'file'
- and src.file_path = 'src/main.cpp'
- `).all() as Array<{ dstKind: string; dstPath: string }>;
- const resolvedToHeader = rows.find(
- (r) => r.dstKind === 'file' && r.dstPath === 'include/utils.h'
- );
- expect(resolvedToHeader, 'main.cpp → include/utils.h imports edge missing').toBeDefined();
- // `<vector>` should NOT produce a file edge — it's a stdlib header.
- const stdlibFile = rows.find(
- (r) => r.dstKind === 'file' && r.dstPath && r.dstPath.endsWith('vector')
- );
- expect(stdlibFile).toBeUndefined();
- } finally {
- fs.rmSync(tempProject, { recursive: true, force: true });
- }
- });
- });
- });
|