extraction.test.ts 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987
  1. /**
  2. * Extraction Tests
  3. *
  4. * Tests for the tree-sitter extraction system.
  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 { extractFromSource, scanDirectory, shouldIncludeFile } from '../src/extraction';
  12. import { detectLanguage, isLanguageSupported, getSupportedLanguages } from '../src/extraction/grammars';
  13. import { normalizePath } from '../src/utils';
  14. import { DEFAULT_CONFIG } from '../src/types';
  15. // Create a temporary directory for each test
  16. function createTempDir(): string {
  17. return fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-test-'));
  18. }
  19. // Clean up temporary directory
  20. function cleanupTempDir(dir: string): void {
  21. if (fs.existsSync(dir)) {
  22. fs.rmSync(dir, { recursive: true, force: true });
  23. }
  24. }
  25. describe('Language Detection', () => {
  26. it('should detect TypeScript files', () => {
  27. expect(detectLanguage('src/index.ts')).toBe('typescript');
  28. expect(detectLanguage('components/Button.tsx')).toBe('tsx');
  29. });
  30. it('should detect JavaScript files', () => {
  31. expect(detectLanguage('index.js')).toBe('javascript');
  32. expect(detectLanguage('App.jsx')).toBe('jsx');
  33. expect(detectLanguage('config.mjs')).toBe('javascript');
  34. });
  35. it('should detect Python files', () => {
  36. expect(detectLanguage('main.py')).toBe('python');
  37. });
  38. it('should detect Go files', () => {
  39. expect(detectLanguage('main.go')).toBe('go');
  40. });
  41. it('should detect Rust files', () => {
  42. expect(detectLanguage('lib.rs')).toBe('rust');
  43. });
  44. it('should detect Java files', () => {
  45. expect(detectLanguage('Main.java')).toBe('java');
  46. });
  47. it('should detect C files', () => {
  48. expect(detectLanguage('main.c')).toBe('c');
  49. expect(detectLanguage('utils.h')).toBe('c');
  50. });
  51. it('should detect C++ files', () => {
  52. expect(detectLanguage('main.cpp')).toBe('cpp');
  53. expect(detectLanguage('class.hpp')).toBe('cpp');
  54. });
  55. it('should detect C# files', () => {
  56. expect(detectLanguage('Program.cs')).toBe('csharp');
  57. });
  58. it('should detect PHP files', () => {
  59. expect(detectLanguage('index.php')).toBe('php');
  60. });
  61. it('should detect Ruby files', () => {
  62. expect(detectLanguage('app.rb')).toBe('ruby');
  63. });
  64. it('should detect Swift files', () => {
  65. expect(detectLanguage('ViewController.swift')).toBe('swift');
  66. });
  67. it('should detect Kotlin files', () => {
  68. expect(detectLanguage('MainActivity.kt')).toBe('kotlin');
  69. expect(detectLanguage('build.gradle.kts')).toBe('kotlin');
  70. });
  71. it('should detect Dart files', () => {
  72. expect(detectLanguage('main.dart')).toBe('dart');
  73. });
  74. it('should return unknown for unsupported extensions', () => {
  75. expect(detectLanguage('styles.css')).toBe('unknown');
  76. expect(detectLanguage('data.json')).toBe('unknown');
  77. });
  78. });
  79. describe('Language Support', () => {
  80. it('should report supported languages', () => {
  81. expect(isLanguageSupported('typescript')).toBe(true);
  82. expect(isLanguageSupported('python')).toBe(true);
  83. expect(isLanguageSupported('go')).toBe(true);
  84. expect(isLanguageSupported('unknown')).toBe(false);
  85. });
  86. it('should list all supported languages', () => {
  87. const languages = getSupportedLanguages();
  88. expect(languages).toContain('typescript');
  89. expect(languages).toContain('javascript');
  90. expect(languages).toContain('python');
  91. expect(languages).toContain('go');
  92. expect(languages).toContain('rust');
  93. expect(languages).toContain('java');
  94. expect(languages).toContain('csharp');
  95. expect(languages).toContain('php');
  96. expect(languages).toContain('ruby');
  97. expect(languages).toContain('swift');
  98. expect(languages).toContain('kotlin');
  99. expect(languages).toContain('dart');
  100. });
  101. });
  102. describe('TypeScript Extraction', () => {
  103. it('should extract function declarations', () => {
  104. const code = `
  105. export function processPayment(amount: number): Promise<Receipt> {
  106. return stripe.charge(amount);
  107. }
  108. `;
  109. const result = extractFromSource('payment.ts', code);
  110. expect(result.nodes).toHaveLength(1);
  111. expect(result.nodes[0]).toMatchObject({
  112. kind: 'function',
  113. name: 'processPayment',
  114. language: 'typescript',
  115. isExported: true,
  116. });
  117. expect(result.nodes[0]?.signature).toContain('amount: number');
  118. });
  119. it('should extract class declarations', () => {
  120. const code = `
  121. export class PaymentService {
  122. private stripe: StripeClient;
  123. constructor(apiKey: string) {
  124. this.stripe = new StripeClient(apiKey);
  125. }
  126. async charge(amount: number): Promise<Receipt> {
  127. return this.stripe.charge(amount);
  128. }
  129. }
  130. `;
  131. const result = extractFromSource('service.ts', code);
  132. const classNode = result.nodes.find((n) => n.kind === 'class');
  133. const methodNodes = result.nodes.filter((n) => n.kind === 'method');
  134. expect(classNode).toBeDefined();
  135. expect(classNode?.name).toBe('PaymentService');
  136. expect(classNode?.isExported).toBe(true);
  137. expect(methodNodes.length).toBeGreaterThanOrEqual(1);
  138. const chargeMethod = methodNodes.find((m) => m.name === 'charge');
  139. expect(chargeMethod).toBeDefined();
  140. });
  141. it('should extract interfaces', () => {
  142. const code = `
  143. export interface User {
  144. id: string;
  145. name: string;
  146. email: string;
  147. }
  148. `;
  149. const result = extractFromSource('types.ts', code);
  150. expect(result.nodes).toHaveLength(1);
  151. expect(result.nodes[0]).toMatchObject({
  152. kind: 'interface',
  153. name: 'User',
  154. isExported: true,
  155. });
  156. });
  157. it('should track function calls', () => {
  158. const code = `
  159. function main() {
  160. const result = processData();
  161. console.log(result);
  162. }
  163. `;
  164. const result = extractFromSource('main.ts', code);
  165. expect(result.unresolvedReferences.length).toBeGreaterThan(0);
  166. const calls = result.unresolvedReferences.filter((r) => r.referenceKind === 'calls');
  167. expect(calls.some((c) => c.referenceName === 'processData')).toBe(true);
  168. });
  169. });
  170. describe('Arrow Function Export Extraction', () => {
  171. it('should extract exported arrow functions assigned to const', () => {
  172. const code = `
  173. export const useAuth = (): AuthContextValue => {
  174. return useContext(AuthContext);
  175. };
  176. `;
  177. const result = extractFromSource('hooks.ts', code);
  178. expect(result.nodes).toHaveLength(1);
  179. expect(result.nodes[0]).toMatchObject({
  180. kind: 'function',
  181. name: 'useAuth',
  182. isExported: true,
  183. });
  184. });
  185. it('should extract exported function expressions assigned to const', () => {
  186. const code = `
  187. export const processData = function(input: string): string {
  188. return input.trim();
  189. };
  190. `;
  191. const result = extractFromSource('utils.ts', code);
  192. expect(result.nodes).toHaveLength(1);
  193. expect(result.nodes[0]).toMatchObject({
  194. kind: 'function',
  195. name: 'processData',
  196. isExported: true,
  197. });
  198. });
  199. it('should not extract non-exported arrow functions as exported', () => {
  200. const code = `
  201. const internalHelper = () => {
  202. return 42;
  203. };
  204. `;
  205. const result = extractFromSource('internal.ts', code);
  206. const helperNode = result.nodes.find((n) => n.name === 'internalHelper');
  207. expect(helperNode).toBeDefined();
  208. expect(helperNode?.isExported).toBeFalsy();
  209. });
  210. it('should still skip truly anonymous arrow functions', () => {
  211. const code = `
  212. const items = [1, 2, 3].map((x) => x * 2);
  213. `;
  214. const result = extractFromSource('anon.ts', code);
  215. // The inline arrow function passed to .map() has no variable_declarator parent
  216. // and should remain anonymous (skipped)
  217. const anonFunctions = result.nodes.filter(
  218. (n) => n.kind === 'function' && n.name === '<anonymous>'
  219. );
  220. expect(anonFunctions).toHaveLength(0);
  221. });
  222. it('should extract multiple exported arrow functions from the same file', () => {
  223. const code = `
  224. export const add = (a: number, b: number): number => a + b;
  225. export const subtract = (a: number, b: number): number => a - b;
  226. const internal = () => 'not exported';
  227. `;
  228. const result = extractFromSource('math.ts', code);
  229. const exported = result.nodes.filter((n) => n.kind === 'function' && n.isExported);
  230. expect(exported).toHaveLength(2);
  231. expect(exported.map((n) => n.name).sort()).toEqual(['add', 'subtract']);
  232. const internalNode = result.nodes.find((n) => n.name === 'internal');
  233. expect(internalNode).toBeDefined();
  234. expect(internalNode?.isExported).toBeFalsy();
  235. });
  236. it('should extract arrow functions in JavaScript files', () => {
  237. const code = `
  238. export const fetchData = async () => {
  239. const response = await fetch('/api/data');
  240. return response.json();
  241. };
  242. `;
  243. const result = extractFromSource('api.js', code);
  244. expect(result.nodes).toHaveLength(1);
  245. expect(result.nodes[0]).toMatchObject({
  246. kind: 'function',
  247. name: 'fetchData',
  248. isExported: true,
  249. });
  250. });
  251. });
  252. describe('Type Alias Extraction', () => {
  253. it('should extract exported type aliases in TypeScript', () => {
  254. const code = `
  255. export type AuthContextValue = {
  256. user: User | null;
  257. login: () => void;
  258. logout: () => void;
  259. };
  260. `;
  261. const result = extractFromSource('types.ts', code);
  262. expect(result.nodes).toHaveLength(1);
  263. expect(result.nodes[0]).toMatchObject({
  264. kind: 'type_alias',
  265. name: 'AuthContextValue',
  266. isExported: true,
  267. });
  268. });
  269. it('should extract non-exported type aliases', () => {
  270. const code = `
  271. type InternalState = {
  272. loading: boolean;
  273. error: string | null;
  274. };
  275. `;
  276. const result = extractFromSource('internal.ts', code);
  277. expect(result.nodes).toHaveLength(1);
  278. expect(result.nodes[0]).toMatchObject({
  279. kind: 'type_alias',
  280. name: 'InternalState',
  281. isExported: false,
  282. });
  283. });
  284. it('should extract multiple type aliases from the same file', () => {
  285. const code = `
  286. export type UnitSystem = 'metric' | 'imperial';
  287. export type DateFormat = 'ISO' | 'US' | 'EU';
  288. type Internal = string;
  289. `;
  290. const result = extractFromSource('config.ts', code);
  291. const typeAliases = result.nodes.filter((n) => n.kind === 'type_alias');
  292. expect(typeAliases).toHaveLength(3);
  293. const exported = typeAliases.filter((n) => n.isExported);
  294. expect(exported).toHaveLength(2);
  295. expect(exported.map((n) => n.name).sort()).toEqual(['DateFormat', 'UnitSystem']);
  296. });
  297. });
  298. describe('Exported Variable Extraction', () => {
  299. it('should extract exported const with call expression (Zustand store)', () => {
  300. const code = `
  301. export const useUIStore = create<UIState>((set) => ({
  302. isOpen: false,
  303. toggle: () => set((s) => ({ isOpen: !s.isOpen })),
  304. }));
  305. `;
  306. const result = extractFromSource('store.ts', code);
  307. const varNode = result.nodes.find((n) => n.kind === 'variable' && n.name === 'useUIStore');
  308. expect(varNode).toBeDefined();
  309. expect(varNode?.isExported).toBe(true);
  310. });
  311. it('should extract exported const with object literal', () => {
  312. const code = `
  313. export const config = {
  314. apiUrl: 'https://api.example.com',
  315. timeout: 5000,
  316. };
  317. `;
  318. const result = extractFromSource('config.ts', code);
  319. const varNode = result.nodes.find((n) => n.kind === 'variable' && n.name === 'config');
  320. expect(varNode).toBeDefined();
  321. expect(varNode?.isExported).toBe(true);
  322. });
  323. it('should extract exported const with array literal', () => {
  324. const code = `
  325. export const SCREEN_NAMES = ['home', 'settings', 'profile'] as const;
  326. `;
  327. const result = extractFromSource('constants.ts', code);
  328. const varNode = result.nodes.find((n) => n.kind === 'variable' && n.name === 'SCREEN_NAMES');
  329. expect(varNode).toBeDefined();
  330. expect(varNode?.isExported).toBe(true);
  331. });
  332. it('should extract exported const with primitive value', () => {
  333. const code = `
  334. export const MAX_RETRIES = 3;
  335. export const API_VERSION = "v2";
  336. `;
  337. const result = extractFromSource('constants.ts', code);
  338. const variables = result.nodes.filter((n) => n.kind === 'variable');
  339. expect(variables).toHaveLength(2);
  340. expect(variables.map((n) => n.name).sort()).toEqual(['API_VERSION', 'MAX_RETRIES']);
  341. });
  342. it('should NOT duplicate arrow functions as both function and variable', () => {
  343. const code = `
  344. export const useAuth = () => {
  345. return useContext(AuthContext);
  346. };
  347. `;
  348. const result = extractFromSource('hooks.ts', code);
  349. // Should be extracted as function (from arrow function handler), NOT as variable
  350. const funcNodes = result.nodes.filter((n) => n.kind === 'function' && n.name === 'useAuth');
  351. const varNodes = result.nodes.filter((n) => n.kind === 'variable' && n.name === 'useAuth');
  352. expect(funcNodes).toHaveLength(1);
  353. expect(varNodes).toHaveLength(0);
  354. });
  355. it('should not extract non-exported const as exported variable', () => {
  356. const code = `
  357. const internalConfig = {
  358. debug: true,
  359. };
  360. `;
  361. const result = extractFromSource('internal.ts', code);
  362. // Non-exported const should NOT create a variable node
  363. // (only export_statement triggers extractExportedVariables)
  364. const varNodes = result.nodes.filter((n) => n.kind === 'variable' && n.name === 'internalConfig');
  365. expect(varNodes).toHaveLength(0);
  366. });
  367. it('should extract Zod schema exports', () => {
  368. const code = `
  369. export const userSchema = z.object({
  370. id: z.string(),
  371. name: z.string(),
  372. email: z.string().email(),
  373. });
  374. `;
  375. const result = extractFromSource('schemas.ts', code);
  376. const varNode = result.nodes.find((n) => n.kind === 'variable' && n.name === 'userSchema');
  377. expect(varNode).toBeDefined();
  378. expect(varNode?.isExported).toBe(true);
  379. });
  380. it('should extract XState machine exports', () => {
  381. const code = `
  382. export const authMachine = createMachine({
  383. id: "auth",
  384. initial: "idle",
  385. states: {
  386. idle: {},
  387. authenticated: {},
  388. },
  389. });
  390. `;
  391. const result = extractFromSource('machine.ts', code);
  392. const varNode = result.nodes.find((n) => n.kind === 'variable' && n.name === 'authMachine');
  393. expect(varNode).toBeDefined();
  394. expect(varNode?.isExported).toBe(true);
  395. });
  396. });
  397. describe('Python Extraction', () => {
  398. it('should extract function definitions', () => {
  399. const code = `
  400. def calculate_total(items: list, tax_rate: float) -> float:
  401. """Calculate total with tax."""
  402. subtotal = sum(item.price for item in items)
  403. return subtotal * (1 + tax_rate)
  404. `;
  405. const result = extractFromSource('calc.py', code);
  406. expect(result.nodes).toHaveLength(1);
  407. expect(result.nodes[0]).toMatchObject({
  408. kind: 'function',
  409. name: 'calculate_total',
  410. language: 'python',
  411. });
  412. });
  413. it('should extract class definitions', () => {
  414. const code = `
  415. class UserService:
  416. """Service for managing users."""
  417. def __init__(self, db):
  418. self.db = db
  419. def get_user(self, user_id: str) -> User:
  420. return self.db.find_user(user_id)
  421. `;
  422. const result = extractFromSource('service.py', code);
  423. const classNode = result.nodes.find((n) => n.kind === 'class');
  424. expect(classNode).toBeDefined();
  425. expect(classNode?.name).toBe('UserService');
  426. });
  427. });
  428. describe('Go Extraction', () => {
  429. it('should extract function declarations', () => {
  430. const code = `
  431. package main
  432. func ProcessOrder(order Order) (Receipt, error) {
  433. // Process the order
  434. return Receipt{}, nil
  435. }
  436. `;
  437. const result = extractFromSource('main.go', code);
  438. const funcNode = result.nodes.find((n) => n.kind === 'function');
  439. expect(funcNode).toBeDefined();
  440. expect(funcNode?.name).toBe('ProcessOrder');
  441. });
  442. it('should extract method declarations', () => {
  443. const code = `
  444. package main
  445. type Service struct {
  446. db *Database
  447. }
  448. func (s *Service) GetUser(id string) (*User, error) {
  449. return s.db.FindUser(id)
  450. }
  451. `;
  452. const result = extractFromSource('service.go', code);
  453. const methodNode = result.nodes.find((n) => n.kind === 'method');
  454. expect(methodNode).toBeDefined();
  455. expect(methodNode?.name).toBe('GetUser');
  456. });
  457. });
  458. describe('Rust Extraction', () => {
  459. it('should extract function declarations', () => {
  460. const code = `
  461. pub fn process_data(input: &str) -> Result<Output, Error> {
  462. // Process data
  463. Ok(Output::new())
  464. }
  465. `;
  466. const result = extractFromSource('lib.rs', code);
  467. const funcNode = result.nodes.find((n) => n.kind === 'function');
  468. expect(funcNode).toBeDefined();
  469. expect(funcNode?.name).toBe('process_data');
  470. expect(funcNode?.visibility).toBe('public');
  471. });
  472. it('should extract struct declarations', () => {
  473. const code = `
  474. pub struct User {
  475. pub id: String,
  476. pub name: String,
  477. email: String,
  478. }
  479. `;
  480. const result = extractFromSource('models.rs', code);
  481. const structNode = result.nodes.find((n) => n.kind === 'struct');
  482. expect(structNode).toBeDefined();
  483. expect(structNode?.name).toBe('User');
  484. });
  485. it('should extract trait declarations', () => {
  486. const code = `
  487. pub trait Repository {
  488. fn find(&self, id: &str) -> Option<Entity>;
  489. fn save(&mut self, entity: Entity) -> Result<(), Error>;
  490. }
  491. `;
  492. const result = extractFromSource('traits.rs', code);
  493. const traitNode = result.nodes.find((n) => n.kind === 'trait');
  494. expect(traitNode).toBeDefined();
  495. expect(traitNode?.name).toBe('Repository');
  496. });
  497. });
  498. describe('Java Extraction', () => {
  499. it('should extract class declarations', () => {
  500. const code = `
  501. public class UserService {
  502. private final UserRepository repository;
  503. public UserService(UserRepository repository) {
  504. this.repository = repository;
  505. }
  506. public User getUser(String id) {
  507. return repository.findById(id);
  508. }
  509. }
  510. `;
  511. const result = extractFromSource('UserService.java', code);
  512. const classNode = result.nodes.find((n) => n.kind === 'class');
  513. expect(classNode).toBeDefined();
  514. expect(classNode?.name).toBe('UserService');
  515. expect(classNode?.visibility).toBe('public');
  516. });
  517. it('should extract method declarations', () => {
  518. const code = `
  519. public class Calculator {
  520. public static int add(int a, int b) {
  521. return a + b;
  522. }
  523. }
  524. `;
  525. const result = extractFromSource('Calculator.java', code);
  526. const methodNode = result.nodes.find((n) => n.kind === 'method' && n.name === 'add');
  527. expect(methodNode).toBeDefined();
  528. expect(methodNode?.isStatic).toBe(true);
  529. });
  530. });
  531. describe('C# Extraction', () => {
  532. it('should extract class declarations', () => {
  533. const code = `
  534. public class OrderService
  535. {
  536. private readonly IOrderRepository _repository;
  537. public OrderService(IOrderRepository repository)
  538. {
  539. _repository = repository;
  540. }
  541. public async Task<Order> GetOrderAsync(string id)
  542. {
  543. return await _repository.FindByIdAsync(id);
  544. }
  545. }
  546. `;
  547. const result = extractFromSource('OrderService.cs', code);
  548. const classNode = result.nodes.find((n) => n.kind === 'class');
  549. expect(classNode).toBeDefined();
  550. expect(classNode?.name).toBe('OrderService');
  551. expect(classNode?.visibility).toBe('public');
  552. });
  553. });
  554. describe('PHP Extraction', () => {
  555. it('should extract class declarations', () => {
  556. const code = `<?php
  557. class UserController
  558. {
  559. private UserService $userService;
  560. public function __construct(UserService $userService)
  561. {
  562. $this->userService = $userService;
  563. }
  564. public function show(string $id): User
  565. {
  566. return $this->userService->find($id);
  567. }
  568. }
  569. `;
  570. const result = extractFromSource('UserController.php', code);
  571. const classNode = result.nodes.find((n) => n.kind === 'class');
  572. expect(classNode).toBeDefined();
  573. expect(classNode?.name).toBe('UserController');
  574. });
  575. });
  576. describe('Swift Extraction', () => {
  577. it('should extract class declarations', () => {
  578. const code = `
  579. public class NetworkManager {
  580. private let session: URLSession
  581. public init(session: URLSession = .shared) {
  582. self.session = session
  583. }
  584. public func fetchData(from url: URL) async throws -> Data {
  585. let (data, _) = try await session.data(from: url)
  586. return data
  587. }
  588. }
  589. `;
  590. const result = extractFromSource('NetworkManager.swift', code);
  591. const classNode = result.nodes.find((n) => n.kind === 'class');
  592. expect(classNode).toBeDefined();
  593. expect(classNode?.name).toBe('NetworkManager');
  594. });
  595. it('should extract function declarations', () => {
  596. const code = `
  597. func calculateSum(_ numbers: [Int]) -> Int {
  598. return numbers.reduce(0, +)
  599. }
  600. public func formatCurrency(amount: Double) -> String {
  601. return String(format: "$%.2f", amount)
  602. }
  603. `;
  604. const result = extractFromSource('utils.swift', code);
  605. const functions = result.nodes.filter((n) => n.kind === 'function');
  606. expect(functions.length).toBeGreaterThanOrEqual(1);
  607. });
  608. it('should extract struct declarations', () => {
  609. const code = `
  610. public struct User {
  611. let id: UUID
  612. var name: String
  613. var email: String
  614. func displayName() -> String {
  615. return name
  616. }
  617. }
  618. `;
  619. const result = extractFromSource('User.swift', code);
  620. const structNode = result.nodes.find((n) => n.kind === 'struct');
  621. expect(structNode).toBeDefined();
  622. expect(structNode?.name).toBe('User');
  623. });
  624. it('should extract protocol declarations', () => {
  625. const code = `
  626. public protocol Repository {
  627. associatedtype Entity
  628. func find(id: String) async throws -> Entity?
  629. func save(_ entity: Entity) async throws
  630. }
  631. `;
  632. const result = extractFromSource('Repository.swift', code);
  633. const protocolNode = result.nodes.find((n) => n.kind === 'interface');
  634. expect(protocolNode).toBeDefined();
  635. expect(protocolNode?.name).toBe('Repository');
  636. });
  637. });
  638. describe('Kotlin Extraction', () => {
  639. it('should extract class declarations', () => {
  640. const code = `
  641. class UserRepository(private val database: Database) {
  642. fun findById(id: String): User? {
  643. return database.query("SELECT * FROM users WHERE id = ?", id)
  644. }
  645. suspend fun save(user: User) {
  646. database.insert(user)
  647. }
  648. }
  649. `;
  650. const result = extractFromSource('UserRepository.kt', code);
  651. const classNode = result.nodes.find((n) => n.kind === 'class');
  652. expect(classNode).toBeDefined();
  653. expect(classNode?.name).toBe('UserRepository');
  654. });
  655. it('should extract function declarations', () => {
  656. const code = `
  657. fun calculateTotal(items: List<Item>): Double {
  658. return items.sumOf { it.price }
  659. }
  660. suspend fun fetchUserData(userId: String): User {
  661. return api.getUser(userId)
  662. }
  663. `;
  664. const result = extractFromSource('utils.kt', code);
  665. const functions = result.nodes.filter((n) => n.kind === 'function');
  666. expect(functions.length).toBeGreaterThanOrEqual(1);
  667. });
  668. it('should detect suspend functions as async', () => {
  669. const code = `
  670. suspend fun loadData(): List<String> {
  671. delay(1000)
  672. return listOf("a", "b", "c")
  673. }
  674. `;
  675. const result = extractFromSource('loader.kt', code);
  676. const funcNode = result.nodes.find((n) => n.kind === 'function');
  677. expect(funcNode).toBeDefined();
  678. expect(funcNode?.isAsync).toBe(true);
  679. });
  680. });
  681. describe('Dart Extraction', () => {
  682. it('should extract class declarations', () => {
  683. const code = `
  684. class UserService {
  685. final Database _db;
  686. Future<User> findById(String id) async {
  687. return await _db.query(id);
  688. }
  689. void _privateMethod() {}
  690. }
  691. `;
  692. const result = extractFromSource('service.dart', code);
  693. const classNode = result.nodes.find((n) => n.kind === 'class');
  694. expect(classNode).toBeDefined();
  695. expect(classNode?.name).toBe('UserService');
  696. expect(classNode?.visibility).toBe('public');
  697. const methodNodes = result.nodes.filter((n) => n.kind === 'method');
  698. expect(methodNodes.length).toBeGreaterThanOrEqual(2);
  699. const findById = methodNodes.find((m) => m.name === 'findById');
  700. expect(findById).toBeDefined();
  701. expect(findById?.isAsync).toBe(true);
  702. const privateMethod = methodNodes.find((m) => m.name === '_privateMethod');
  703. expect(privateMethod).toBeDefined();
  704. expect(privateMethod?.visibility).toBe('private');
  705. });
  706. it('should extract top-level function declarations', () => {
  707. const code = `
  708. void topLevelFunction(String name) {
  709. print(name);
  710. }
  711. `;
  712. const result = extractFromSource('utils.dart', code);
  713. const funcNode = result.nodes.find((n) => n.kind === 'function');
  714. expect(funcNode).toBeDefined();
  715. expect(funcNode?.name).toBe('topLevelFunction');
  716. expect(funcNode?.language).toBe('dart');
  717. });
  718. it('should extract enum declarations', () => {
  719. const code = `
  720. enum Status { active, inactive, pending }
  721. `;
  722. const result = extractFromSource('models.dart', code);
  723. const enumNode = result.nodes.find((n) => n.kind === 'enum');
  724. expect(enumNode).toBeDefined();
  725. expect(enumNode?.name).toBe('Status');
  726. });
  727. it('should extract mixin declarations', () => {
  728. const code = `
  729. mixin LoggerMixin {
  730. void log(String message) {}
  731. }
  732. `;
  733. const result = extractFromSource('mixins.dart', code);
  734. const classNode = result.nodes.find((n) => n.kind === 'class');
  735. expect(classNode).toBeDefined();
  736. expect(classNode?.name).toBe('LoggerMixin');
  737. const methodNode = result.nodes.find((n) => n.kind === 'method');
  738. expect(methodNode).toBeDefined();
  739. expect(methodNode?.name).toBe('log');
  740. });
  741. it('should extract extension declarations', () => {
  742. const code = `
  743. extension StringExt on String {
  744. bool get isBlank => trim().isEmpty;
  745. }
  746. `;
  747. const result = extractFromSource('extensions.dart', code);
  748. const classNode = result.nodes.find((n) => n.kind === 'class');
  749. expect(classNode).toBeDefined();
  750. expect(classNode?.name).toBe('StringExt');
  751. });
  752. it('should detect static methods', () => {
  753. const code = `
  754. class Utils {
  755. static void doWork() {}
  756. }
  757. `;
  758. const result = extractFromSource('utils.dart', code);
  759. const methodNode = result.nodes.find((n) => n.kind === 'method');
  760. expect(methodNode).toBeDefined();
  761. expect(methodNode?.name).toBe('doWork');
  762. expect(methodNode?.isStatic).toBe(true);
  763. });
  764. it('should detect async functions', () => {
  765. const code = `
  766. Future<String> fetchData() async {
  767. return await http.get('/data');
  768. }
  769. `;
  770. const result = extractFromSource('api.dart', code);
  771. const funcNode = result.nodes.find((n) => n.kind === 'function');
  772. expect(funcNode).toBeDefined();
  773. expect(funcNode?.name).toBe('fetchData');
  774. expect(funcNode?.isAsync).toBe(true);
  775. });
  776. it('should detect private visibility via underscore convention', () => {
  777. const code = `
  778. void _privateHelper() {}
  779. void publicFunction() {}
  780. `;
  781. const result = extractFromSource('helpers.dart', code);
  782. const functions = result.nodes.filter((n) => n.kind === 'function');
  783. const privateFunc = functions.find((f) => f.name === '_privateHelper');
  784. const publicFunc = functions.find((f) => f.name === 'publicFunction');
  785. expect(privateFunc?.visibility).toBe('private');
  786. expect(publicFunc?.visibility).toBe('public');
  787. });
  788. });
  789. describe('Import Extraction', () => {
  790. describe('TypeScript/JavaScript imports', () => {
  791. it('should extract default imports', () => {
  792. const code = `import React from 'react';`;
  793. const result = extractFromSource('app.tsx', code);
  794. const importNode = result.nodes.find((n) => n.kind === 'import');
  795. expect(importNode).toBeDefined();
  796. expect(importNode?.name).toBe('react');
  797. expect(importNode?.signature).toBe("import React from 'react';");
  798. });
  799. it('should extract named imports', () => {
  800. const code = `import { Bug, Database } from '@phosphor-icons/react';`;
  801. const result = extractFromSource('icons.tsx', code);
  802. const importNode = result.nodes.find((n) => n.kind === 'import');
  803. expect(importNode).toBeDefined();
  804. expect(importNode?.name).toBe('@phosphor-icons/react');
  805. expect(importNode?.signature).toContain('Bug');
  806. expect(importNode?.signature).toContain('Database');
  807. });
  808. it('should extract namespace imports', () => {
  809. const code = `import * as Icons from '@phosphor-icons/react';`;
  810. const result = extractFromSource('icons.tsx', code);
  811. const importNode = result.nodes.find((n) => n.kind === 'import');
  812. expect(importNode).toBeDefined();
  813. expect(importNode?.name).toBe('@phosphor-icons/react');
  814. expect(importNode?.signature).toContain('* as Icons');
  815. });
  816. it('should extract side-effect imports', () => {
  817. const code = `import './styles.css';`;
  818. const result = extractFromSource('app.tsx', code);
  819. const importNode = result.nodes.find((n) => n.kind === 'import');
  820. expect(importNode).toBeDefined();
  821. expect(importNode?.name).toBe('./styles.css');
  822. });
  823. it('should extract mixed imports (default + named)', () => {
  824. const code = `import React, { useState, useEffect } from 'react';`;
  825. const result = extractFromSource('app.tsx', code);
  826. const importNode = result.nodes.find((n) => n.kind === 'import');
  827. expect(importNode).toBeDefined();
  828. expect(importNode?.name).toBe('react');
  829. expect(importNode?.signature).toContain('React');
  830. expect(importNode?.signature).toContain('useState');
  831. expect(importNode?.signature).toContain('useEffect');
  832. });
  833. it('should extract multiple import statements', () => {
  834. const code = `
  835. import React from 'react';
  836. import { Button } from './components';
  837. import './styles.css';
  838. `;
  839. const result = extractFromSource('app.tsx', code);
  840. const importNodes = result.nodes.filter((n) => n.kind === 'import');
  841. expect(importNodes.length).toBe(3);
  842. const names = importNodes.map((n) => n.name);
  843. expect(names).toContain('react');
  844. expect(names).toContain('./components');
  845. expect(names).toContain('./styles.css');
  846. });
  847. it('should extract type imports', () => {
  848. const code = `import type { FC, ReactNode } from 'react';`;
  849. const result = extractFromSource('types.ts', code);
  850. const importNode = result.nodes.find((n) => n.kind === 'import');
  851. expect(importNode).toBeDefined();
  852. expect(importNode?.name).toBe('react');
  853. expect(importNode?.signature).toContain('type');
  854. expect(importNode?.signature).toContain('FC');
  855. });
  856. it('should extract aliased named imports', () => {
  857. const code = `import { useState as useStateAlias } from 'react';`;
  858. const result = extractFromSource('hooks.ts', code);
  859. const importNode = result.nodes.find((n) => n.kind === 'import');
  860. expect(importNode).toBeDefined();
  861. expect(importNode?.name).toBe('react');
  862. expect(importNode?.signature).toContain('useState');
  863. expect(importNode?.signature).toContain('useStateAlias');
  864. });
  865. it('should extract relative path imports', () => {
  866. const code = `import { helper } from '../utils/helper';`;
  867. const result = extractFromSource('components/Button.tsx', code);
  868. const importNode = result.nodes.find((n) => n.kind === 'import');
  869. expect(importNode).toBeDefined();
  870. expect(importNode?.name).toBe('../utils/helper');
  871. expect(importNode?.signature).toContain('helper');
  872. });
  873. });
  874. describe('Python imports', () => {
  875. it('should extract simple import statement', () => {
  876. const code = `import json`;
  877. const result = extractFromSource('utils.py', code);
  878. const importNode = result.nodes.find((n) => n.kind === 'import');
  879. expect(importNode).toBeDefined();
  880. expect(importNode?.name).toBe('json');
  881. });
  882. it('should extract from import statement', () => {
  883. const code = `from os import path`;
  884. const result = extractFromSource('utils.py', code);
  885. const importNode = result.nodes.find((n) => n.kind === 'import');
  886. expect(importNode).toBeDefined();
  887. expect(importNode?.name).toBe('os');
  888. expect(importNode?.signature).toContain('path');
  889. });
  890. it('should extract multiple imports from same module', () => {
  891. const code = `from typing import List, Dict, Optional`;
  892. const result = extractFromSource('types.py', code);
  893. const importNode = result.nodes.find((n) => n.kind === 'import');
  894. expect(importNode).toBeDefined();
  895. expect(importNode?.name).toBe('typing');
  896. expect(importNode?.signature).toContain('List');
  897. expect(importNode?.signature).toContain('Dict');
  898. });
  899. it('should extract multiple import statements', () => {
  900. const code = `
  901. import os
  902. import sys
  903. `;
  904. const result = extractFromSource('main.py', code);
  905. const importNodes = result.nodes.filter((n) => n.kind === 'import');
  906. expect(importNodes.length).toBe(2);
  907. const names = importNodes.map((n) => n.name);
  908. expect(names).toContain('os');
  909. expect(names).toContain('sys');
  910. });
  911. it('should extract aliased import', () => {
  912. const code = `import numpy as np`;
  913. const result = extractFromSource('data.py', code);
  914. const importNode = result.nodes.find((n) => n.kind === 'import');
  915. expect(importNode).toBeDefined();
  916. expect(importNode?.name).toBe('numpy');
  917. expect(importNode?.signature).toContain('as np');
  918. });
  919. it('should extract relative import', () => {
  920. const code = `from .utils import helper`;
  921. const result = extractFromSource('module.py', code);
  922. const importNode = result.nodes.find((n) => n.kind === 'import');
  923. expect(importNode).toBeDefined();
  924. expect(importNode?.name).toBe('.utils');
  925. expect(importNode?.signature).toContain('helper');
  926. });
  927. it('should extract wildcard import', () => {
  928. const code = `from typing import *`;
  929. const result = extractFromSource('types.py', code);
  930. const importNode = result.nodes.find((n) => n.kind === 'import');
  931. expect(importNode).toBeDefined();
  932. expect(importNode?.name).toBe('typing');
  933. expect(importNode?.signature).toContain('*');
  934. });
  935. });
  936. describe('Rust imports', () => {
  937. it('should extract simple use declaration', () => {
  938. const code = `use std::io;`;
  939. const result = extractFromSource('main.rs', code);
  940. const importNode = result.nodes.find((n) => n.kind === 'import');
  941. expect(importNode).toBeDefined();
  942. expect(importNode?.name).toBe('std');
  943. expect(importNode?.signature).toBe('use std::io;');
  944. });
  945. it('should extract scoped use list', () => {
  946. const code = `use std::{ffi::OsStr, io, path::Path};`;
  947. const result = extractFromSource('main.rs', code);
  948. const importNode = result.nodes.find((n) => n.kind === 'import');
  949. expect(importNode).toBeDefined();
  950. expect(importNode?.name).toBe('std');
  951. expect(importNode?.signature).toContain('ffi::OsStr');
  952. expect(importNode?.signature).toContain('path::Path');
  953. });
  954. it('should extract crate imports', () => {
  955. const code = `use crate::error::Error;`;
  956. const result = extractFromSource('lib.rs', code);
  957. const importNode = result.nodes.find((n) => n.kind === 'import');
  958. expect(importNode).toBeDefined();
  959. expect(importNode?.name).toBe('crate');
  960. });
  961. it('should extract super imports', () => {
  962. const code = `use super::utils;`;
  963. const result = extractFromSource('submod.rs', code);
  964. const importNode = result.nodes.find((n) => n.kind === 'import');
  965. expect(importNode).toBeDefined();
  966. expect(importNode?.name).toBe('super');
  967. });
  968. it('should extract external crate imports', () => {
  969. const code = `use serde::{Serialize, Deserialize};`;
  970. const result = extractFromSource('types.rs', code);
  971. const importNode = result.nodes.find((n) => n.kind === 'import');
  972. expect(importNode).toBeDefined();
  973. expect(importNode?.name).toBe('serde');
  974. expect(importNode?.signature).toContain('Serialize');
  975. expect(importNode?.signature).toContain('Deserialize');
  976. });
  977. });
  978. describe('Go imports', () => {
  979. it('should extract single import', () => {
  980. const code = `
  981. package main
  982. import "fmt"
  983. `;
  984. const result = extractFromSource('main.go', code);
  985. const importNode = result.nodes.find((n) => n.kind === 'import');
  986. expect(importNode).toBeDefined();
  987. expect(importNode?.name).toBe('fmt');
  988. });
  989. it('should extract grouped imports', () => {
  990. const code = `
  991. package main
  992. import (
  993. "fmt"
  994. "os"
  995. "encoding/json"
  996. )
  997. `;
  998. const result = extractFromSource('main.go', code);
  999. const importNodes = result.nodes.filter((n) => n.kind === 'import');
  1000. expect(importNodes.length).toBe(3);
  1001. const names = importNodes.map((n) => n.name);
  1002. expect(names).toContain('fmt');
  1003. expect(names).toContain('os');
  1004. expect(names).toContain('encoding/json');
  1005. });
  1006. it('should extract aliased import', () => {
  1007. const code = `
  1008. package main
  1009. import f "fmt"
  1010. `;
  1011. const result = extractFromSource('main.go', code);
  1012. const importNode = result.nodes.find((n) => n.kind === 'import');
  1013. expect(importNode).toBeDefined();
  1014. expect(importNode?.name).toBe('fmt');
  1015. expect(importNode?.signature).toContain('f');
  1016. });
  1017. it('should extract dot import', () => {
  1018. const code = `
  1019. package main
  1020. import . "math"
  1021. `;
  1022. const result = extractFromSource('main.go', code);
  1023. const importNode = result.nodes.find((n) => n.kind === 'import');
  1024. expect(importNode).toBeDefined();
  1025. expect(importNode?.name).toBe('math');
  1026. expect(importNode?.signature).toContain('.');
  1027. });
  1028. it('should extract blank import', () => {
  1029. const code = `
  1030. package main
  1031. import _ "github.com/go-sql-driver/mysql"
  1032. `;
  1033. const result = extractFromSource('main.go', code);
  1034. const importNode = result.nodes.find((n) => n.kind === 'import');
  1035. expect(importNode).toBeDefined();
  1036. expect(importNode?.name).toBe('github.com/go-sql-driver/mysql');
  1037. expect(importNode?.signature).toContain('_');
  1038. });
  1039. });
  1040. describe('Swift imports', () => {
  1041. it('should extract simple import', () => {
  1042. const code = `import Foundation`;
  1043. const result = extractFromSource('main.swift', code);
  1044. const importNode = result.nodes.find((n) => n.kind === 'import');
  1045. expect(importNode).toBeDefined();
  1046. expect(importNode?.name).toBe('Foundation');
  1047. expect(importNode?.signature).toBe('import Foundation');
  1048. });
  1049. it('should extract @testable import', () => {
  1050. const code = `@testable import Alamofire`;
  1051. const result = extractFromSource('Tests.swift', code);
  1052. const importNode = result.nodes.find((n) => n.kind === 'import');
  1053. expect(importNode).toBeDefined();
  1054. expect(importNode?.name).toBe('Alamofire');
  1055. expect(importNode?.signature).toContain('@testable');
  1056. });
  1057. it('should extract @preconcurrency import', () => {
  1058. const code = `@preconcurrency import Security`;
  1059. const result = extractFromSource('Auth.swift', code);
  1060. const importNode = result.nodes.find((n) => n.kind === 'import');
  1061. expect(importNode).toBeDefined();
  1062. expect(importNode?.name).toBe('Security');
  1063. });
  1064. it('should extract multiple imports', () => {
  1065. const code = `
  1066. import Foundation
  1067. import UIKit
  1068. import Alamofire
  1069. `;
  1070. const result = extractFromSource('App.swift', code);
  1071. const importNodes = result.nodes.filter((n) => n.kind === 'import');
  1072. expect(importNodes.length).toBe(3);
  1073. const names = importNodes.map((n) => n.name);
  1074. expect(names).toContain('Foundation');
  1075. expect(names).toContain('UIKit');
  1076. expect(names).toContain('Alamofire');
  1077. });
  1078. });
  1079. describe('Kotlin imports', () => {
  1080. it('should extract simple import', () => {
  1081. const code = `import java.io.IOException`;
  1082. const result = extractFromSource('Main.kt', code);
  1083. const importNode = result.nodes.find((n) => n.kind === 'import');
  1084. expect(importNode).toBeDefined();
  1085. expect(importNode?.name).toBe('java.io.IOException');
  1086. expect(importNode?.signature).toBe('import java.io.IOException');
  1087. });
  1088. it('should extract aliased import', () => {
  1089. const code = `import okhttp3.Request.Builder as RequestBuilder`;
  1090. const result = extractFromSource('Utils.kt', code);
  1091. const importNode = result.nodes.find((n) => n.kind === 'import');
  1092. expect(importNode).toBeDefined();
  1093. expect(importNode?.name).toBe('okhttp3.Request.Builder');
  1094. expect(importNode?.signature).toContain('as RequestBuilder');
  1095. });
  1096. it('should extract wildcard import', () => {
  1097. const code = `import java.util.concurrent.TimeUnit.*`;
  1098. const result = extractFromSource('Time.kt', code);
  1099. const importNode = result.nodes.find((n) => n.kind === 'import');
  1100. expect(importNode).toBeDefined();
  1101. expect(importNode?.name).toBe('java.util.concurrent.TimeUnit');
  1102. expect(importNode?.signature).toContain('.*');
  1103. });
  1104. it('should extract multiple imports', () => {
  1105. const code = `
  1106. import java.io.IOException
  1107. import kotlin.test.assertFailsWith
  1108. import okhttp3.OkHttpClient
  1109. `;
  1110. const result = extractFromSource('Test.kt', code);
  1111. const importNodes = result.nodes.filter((n) => n.kind === 'import');
  1112. expect(importNodes.length).toBe(3);
  1113. const names = importNodes.map((n) => n.name);
  1114. expect(names).toContain('java.io.IOException');
  1115. expect(names).toContain('kotlin.test.assertFailsWith');
  1116. expect(names).toContain('okhttp3.OkHttpClient');
  1117. });
  1118. });
  1119. describe('Java imports', () => {
  1120. it('should extract simple import', () => {
  1121. const code = `import java.util.List;`;
  1122. const result = extractFromSource('Main.java', code);
  1123. const importNode = result.nodes.find((n) => n.kind === 'import');
  1124. expect(importNode).toBeDefined();
  1125. expect(importNode?.name).toBe('java.util.List');
  1126. expect(importNode?.signature).toBe('import java.util.List;');
  1127. });
  1128. it('should extract static import', () => {
  1129. const code = `import static java.util.Collections.emptyList;`;
  1130. const result = extractFromSource('Utils.java', code);
  1131. const importNode = result.nodes.find((n) => n.kind === 'import');
  1132. expect(importNode).toBeDefined();
  1133. expect(importNode?.name).toBe('java.util.Collections.emptyList');
  1134. expect(importNode?.signature).toContain('static');
  1135. });
  1136. it('should extract wildcard import', () => {
  1137. const code = `import java.util.*;`;
  1138. const result = extractFromSource('App.java', code);
  1139. const importNode = result.nodes.find((n) => n.kind === 'import');
  1140. expect(importNode).toBeDefined();
  1141. expect(importNode?.name).toBe('java.util');
  1142. expect(importNode?.signature).toContain('.*');
  1143. });
  1144. it('should extract nested class import', () => {
  1145. const code = `import java.util.Map.Entry;`;
  1146. const result = extractFromSource('MapUtil.java', code);
  1147. const importNode = result.nodes.find((n) => n.kind === 'import');
  1148. expect(importNode).toBeDefined();
  1149. expect(importNode?.name).toBe('java.util.Map.Entry');
  1150. });
  1151. it('should extract multiple imports', () => {
  1152. const code = `
  1153. import java.util.List;
  1154. import java.util.Map;
  1155. import java.io.IOException;
  1156. `;
  1157. const result = extractFromSource('Service.java', code);
  1158. const importNodes = result.nodes.filter((n) => n.kind === 'import');
  1159. expect(importNodes.length).toBe(3);
  1160. const names = importNodes.map((n) => n.name);
  1161. expect(names).toContain('java.util.List');
  1162. expect(names).toContain('java.util.Map');
  1163. expect(names).toContain('java.io.IOException');
  1164. });
  1165. });
  1166. describe('C# imports', () => {
  1167. it('should extract simple using', () => {
  1168. const code = `using System;`;
  1169. const result = extractFromSource('Program.cs', code);
  1170. const importNode = result.nodes.find((n) => n.kind === 'import');
  1171. expect(importNode).toBeDefined();
  1172. expect(importNode?.name).toBe('System');
  1173. expect(importNode?.signature).toBe('using System;');
  1174. });
  1175. it('should extract qualified using', () => {
  1176. const code = `using System.Collections.Generic;`;
  1177. const result = extractFromSource('Utils.cs', code);
  1178. const importNode = result.nodes.find((n) => n.kind === 'import');
  1179. expect(importNode).toBeDefined();
  1180. expect(importNode?.name).toBe('System.Collections.Generic');
  1181. });
  1182. it('should extract static using', () => {
  1183. const code = `using static System.Console;`;
  1184. const result = extractFromSource('App.cs', code);
  1185. const importNode = result.nodes.find((n) => n.kind === 'import');
  1186. expect(importNode).toBeDefined();
  1187. expect(importNode?.name).toBe('System.Console');
  1188. expect(importNode?.signature).toContain('static');
  1189. });
  1190. it('should extract alias using', () => {
  1191. const code = `using MyList = System.Collections.Generic.List<int>;`;
  1192. const result = extractFromSource('Types.cs', code);
  1193. const importNode = result.nodes.find((n) => n.kind === 'import');
  1194. expect(importNode).toBeDefined();
  1195. expect(importNode?.name).toBe('System.Collections.Generic.List<int>');
  1196. expect(importNode?.signature).toContain('MyList =');
  1197. });
  1198. it('should extract multiple usings', () => {
  1199. const code = `
  1200. using System;
  1201. using System.Threading.Tasks;
  1202. using Microsoft.Extensions.DependencyInjection;
  1203. `;
  1204. const result = extractFromSource('Service.cs', code);
  1205. const importNodes = result.nodes.filter((n) => n.kind === 'import');
  1206. expect(importNodes.length).toBe(3);
  1207. const names = importNodes.map((n) => n.name);
  1208. expect(names).toContain('System');
  1209. expect(names).toContain('System.Threading.Tasks');
  1210. expect(names).toContain('Microsoft.Extensions.DependencyInjection');
  1211. });
  1212. });
  1213. describe('PHP imports', () => {
  1214. it('should extract simple use', () => {
  1215. const code = `<?php use PHPUnit\\Framework\\TestCase;`;
  1216. const result = extractFromSource('Test.php', code);
  1217. const importNode = result.nodes.find((n) => n.kind === 'import');
  1218. expect(importNode).toBeDefined();
  1219. expect(importNode?.name).toBe('PHPUnit\\Framework\\TestCase');
  1220. });
  1221. it('should extract aliased use', () => {
  1222. const code = `<?php use Mockery as m;`;
  1223. const result = extractFromSource('Test.php', code);
  1224. const importNode = result.nodes.find((n) => n.kind === 'import');
  1225. expect(importNode).toBeDefined();
  1226. expect(importNode?.name).toBe('Mockery');
  1227. expect(importNode?.signature).toContain('as m');
  1228. });
  1229. it('should extract function use', () => {
  1230. const code = `<?php use function Illuminate\\Support\\env;`;
  1231. const result = extractFromSource('helpers.php', code);
  1232. const importNode = result.nodes.find((n) => n.kind === 'import');
  1233. expect(importNode).toBeDefined();
  1234. expect(importNode?.name).toBe('Illuminate\\Support\\env');
  1235. expect(importNode?.signature).toContain('function');
  1236. });
  1237. it('should extract grouped use', () => {
  1238. const code = `<?php use Illuminate\\Database\\{Model, Builder};`;
  1239. const result = extractFromSource('Models.php', code);
  1240. const importNodes = result.nodes.filter((n) => n.kind === 'import');
  1241. expect(importNodes.length).toBe(2);
  1242. const names = importNodes.map((n) => n.name);
  1243. expect(names).toContain('Illuminate\\Database\\Model');
  1244. expect(names).toContain('Illuminate\\Database\\Builder');
  1245. });
  1246. it('should extract multiple uses', () => {
  1247. const code = `<?php
  1248. use Illuminate\\Support\\Collection;
  1249. use Illuminate\\Support\\Str;
  1250. use Closure;
  1251. `;
  1252. const result = extractFromSource('Service.php', code);
  1253. const importNodes = result.nodes.filter((n) => n.kind === 'import');
  1254. expect(importNodes.length).toBe(3);
  1255. const names = importNodes.map((n) => n.name);
  1256. expect(names).toContain('Illuminate\\Support\\Collection');
  1257. expect(names).toContain('Illuminate\\Support\\Str');
  1258. expect(names).toContain('Closure');
  1259. });
  1260. });
  1261. describe('Ruby imports', () => {
  1262. it('should extract require', () => {
  1263. const code = `require 'json'`;
  1264. const result = extractFromSource('app.rb', code);
  1265. const importNode = result.nodes.find((n) => n.kind === 'import');
  1266. expect(importNode).toBeDefined();
  1267. expect(importNode?.name).toBe('json');
  1268. expect(importNode?.signature).toBe("require 'json'");
  1269. });
  1270. it('should extract require with path', () => {
  1271. const code = `require 'active_support/core_ext/string'`;
  1272. const result = extractFromSource('config.rb', code);
  1273. const importNode = result.nodes.find((n) => n.kind === 'import');
  1274. expect(importNode).toBeDefined();
  1275. expect(importNode?.name).toBe('active_support/core_ext/string');
  1276. });
  1277. it('should extract require_relative', () => {
  1278. const code = `require_relative '../test_helper'`;
  1279. const result = extractFromSource('test/my_test.rb', code);
  1280. const importNode = result.nodes.find((n) => n.kind === 'import');
  1281. expect(importNode).toBeDefined();
  1282. expect(importNode?.name).toBe('../test_helper');
  1283. expect(importNode?.signature).toContain('require_relative');
  1284. });
  1285. it('should not extract non-require calls', () => {
  1286. const code = `puts 'hello'`;
  1287. const result = extractFromSource('app.rb', code);
  1288. const importNode = result.nodes.find((n) => n.kind === 'import');
  1289. expect(importNode).toBeUndefined();
  1290. });
  1291. it('should extract multiple requires', () => {
  1292. const code = `
  1293. require 'json'
  1294. require 'yaml'
  1295. require_relative 'helper'
  1296. `;
  1297. const result = extractFromSource('lib.rb', code);
  1298. const importNodes = result.nodes.filter((n) => n.kind === 'import');
  1299. expect(importNodes.length).toBe(3);
  1300. const names = importNodes.map((n) => n.name);
  1301. expect(names).toContain('json');
  1302. expect(names).toContain('yaml');
  1303. expect(names).toContain('helper');
  1304. });
  1305. });
  1306. describe('C/C++ imports', () => {
  1307. it('should extract system include', () => {
  1308. const code = `#include <iostream>`;
  1309. const result = extractFromSource('main.cpp', code);
  1310. const importNode = result.nodes.find((n) => n.kind === 'import');
  1311. expect(importNode).toBeDefined();
  1312. expect(importNode?.name).toBe('iostream');
  1313. expect(importNode?.signature).toBe('#include <iostream>');
  1314. });
  1315. it('should extract system include with path', () => {
  1316. const code = `#include <nlohmann/json.hpp>`;
  1317. const result = extractFromSource('app.cpp', code);
  1318. const importNode = result.nodes.find((n) => n.kind === 'import');
  1319. expect(importNode).toBeDefined();
  1320. expect(importNode?.name).toBe('nlohmann/json.hpp');
  1321. });
  1322. it('should extract local include', () => {
  1323. const code = `#include "myheader.h"`;
  1324. const result = extractFromSource('main.cpp', code);
  1325. const importNode = result.nodes.find((n) => n.kind === 'import');
  1326. expect(importNode).toBeDefined();
  1327. expect(importNode?.name).toBe('myheader.h');
  1328. });
  1329. it('should extract C header', () => {
  1330. const code = `#include <stdio.h>`;
  1331. const result = extractFromSource('main.c', code);
  1332. const importNode = result.nodes.find((n) => n.kind === 'import');
  1333. expect(importNode).toBeDefined();
  1334. expect(importNode?.name).toBe('stdio.h');
  1335. });
  1336. it('should extract multiple includes', () => {
  1337. const code = `
  1338. #include <iostream>
  1339. #include <vector>
  1340. #include "config.h"
  1341. `;
  1342. const result = extractFromSource('app.cpp', code);
  1343. const importNodes = result.nodes.filter((n) => n.kind === 'import');
  1344. expect(importNodes.length).toBe(3);
  1345. const names = importNodes.map((n) => n.name);
  1346. expect(names).toContain('iostream');
  1347. expect(names).toContain('vector');
  1348. expect(names).toContain('config.h');
  1349. });
  1350. });
  1351. describe('Dart imports', () => {
  1352. it('should extract dart: import', () => {
  1353. const code = `import 'dart:async';`;
  1354. const result = extractFromSource('main.dart', code);
  1355. const importNode = result.nodes.find((n) => n.kind === 'import');
  1356. expect(importNode).toBeDefined();
  1357. expect(importNode?.name).toBe('dart:async');
  1358. expect(importNode?.signature).toBe("import 'dart:async';");
  1359. });
  1360. it('should extract package import', () => {
  1361. const code = `import 'package:flutter/material.dart';`;
  1362. const result = extractFromSource('app.dart', code);
  1363. const importNode = result.nodes.find((n) => n.kind === 'import');
  1364. expect(importNode).toBeDefined();
  1365. expect(importNode?.name).toBe('package:flutter/material.dart');
  1366. });
  1367. it('should extract aliased import', () => {
  1368. const code = `import 'package:http/http.dart' as http;`;
  1369. const result = extractFromSource('api.dart', code);
  1370. const importNode = result.nodes.find((n) => n.kind === 'import');
  1371. expect(importNode).toBeDefined();
  1372. expect(importNode?.name).toBe('package:http/http.dart');
  1373. expect(importNode?.signature).toContain('as http');
  1374. });
  1375. it('should extract multiple imports', () => {
  1376. const code = `
  1377. import 'dart:async';
  1378. import 'dart:convert';
  1379. import 'package:flutter/material.dart';
  1380. `;
  1381. const result = extractFromSource('main.dart', code);
  1382. const importNodes = result.nodes.filter((n) => n.kind === 'import');
  1383. expect(importNodes.length).toBe(3);
  1384. const names = importNodes.map((n) => n.name);
  1385. expect(names).toContain('dart:async');
  1386. expect(names).toContain('dart:convert');
  1387. expect(names).toContain('package:flutter/material.dart');
  1388. });
  1389. it('should extract relative import', () => {
  1390. const code = `import '../utils/helpers.dart';`;
  1391. const result = extractFromSource('lib/main.dart', code);
  1392. const importNode = result.nodes.find((n) => n.kind === 'import');
  1393. expect(importNode).toBeDefined();
  1394. expect(importNode?.name).toBe('../utils/helpers.dart');
  1395. });
  1396. });
  1397. describe('Liquid imports', () => {
  1398. it('should extract render tag', () => {
  1399. const code = `{% render 'loading-spinner' %}`;
  1400. const result = extractFromSource('template.liquid', code);
  1401. const importNode = result.nodes.find((n) => n.kind === 'import');
  1402. expect(importNode).toBeDefined();
  1403. expect(importNode?.name).toBe('loading-spinner');
  1404. expect(importNode?.signature).toContain('render');
  1405. });
  1406. it('should extract section tag', () => {
  1407. const code = `{% section 'header' %}`;
  1408. const result = extractFromSource('layout/theme.liquid', code);
  1409. const importNode = result.nodes.find((n) => n.kind === 'import');
  1410. expect(importNode).toBeDefined();
  1411. expect(importNode?.name).toBe('header');
  1412. expect(importNode?.signature).toContain('section');
  1413. });
  1414. it('should extract include tag', () => {
  1415. const code = `{% include 'icon-cart' %}`;
  1416. const result = extractFromSource('snippets/header.liquid', code);
  1417. const importNode = result.nodes.find((n) => n.kind === 'import');
  1418. expect(importNode).toBeDefined();
  1419. expect(importNode?.name).toBe('icon-cart');
  1420. expect(importNode?.signature).toContain('include');
  1421. });
  1422. it('should extract render with whitespace control', () => {
  1423. const code = `{%- render 'price' -%}`;
  1424. const result = extractFromSource('snippets/product.liquid', code);
  1425. const importNode = result.nodes.find((n) => n.kind === 'import');
  1426. expect(importNode).toBeDefined();
  1427. expect(importNode?.name).toBe('price');
  1428. });
  1429. it('should extract multiple imports', () => {
  1430. const code = `
  1431. {% section 'header' %}
  1432. {% render 'loading-spinner' %}
  1433. {% render 'cart-drawer' %}
  1434. `;
  1435. const result = extractFromSource('layout/theme.liquid', code);
  1436. const importNodes = result.nodes.filter((n) => n.kind === 'import');
  1437. expect(importNodes.length).toBe(3);
  1438. const names = importNodes.map((n) => n.name);
  1439. expect(names).toContain('header');
  1440. expect(names).toContain('loading-spinner');
  1441. expect(names).toContain('cart-drawer');
  1442. });
  1443. });
  1444. });
  1445. describe('Full Indexing', () => {
  1446. let tempDir: string;
  1447. beforeEach(() => {
  1448. tempDir = createTempDir();
  1449. });
  1450. afterEach(() => {
  1451. cleanupTempDir(tempDir);
  1452. });
  1453. it('should index a TypeScript file', async () => {
  1454. // Create test file
  1455. const srcDir = path.join(tempDir, 'src');
  1456. fs.mkdirSync(srcDir);
  1457. fs.writeFileSync(
  1458. path.join(srcDir, 'utils.ts'),
  1459. `
  1460. export function add(a: number, b: number): number {
  1461. return a + b;
  1462. }
  1463. export function multiply(a: number, b: number): number {
  1464. return a * b;
  1465. }
  1466. `
  1467. );
  1468. // Initialize and index
  1469. const cg = CodeGraph.initSync(tempDir);
  1470. const result = await cg.indexAll();
  1471. expect(result.success).toBe(true);
  1472. expect(result.filesIndexed).toBe(1);
  1473. expect(result.nodesCreated).toBeGreaterThanOrEqual(2);
  1474. // Check nodes were stored
  1475. const nodes = cg.getNodesInFile('src/utils.ts');
  1476. expect(nodes.length).toBeGreaterThanOrEqual(2);
  1477. const addFunc = nodes.find((n) => n.name === 'add');
  1478. expect(addFunc).toBeDefined();
  1479. expect(addFunc?.kind).toBe('function');
  1480. cg.close();
  1481. });
  1482. it('should index multiple files', async () => {
  1483. // Create test files
  1484. const srcDir = path.join(tempDir, 'src');
  1485. fs.mkdirSync(srcDir);
  1486. fs.writeFileSync(
  1487. path.join(srcDir, 'math.ts'),
  1488. `export function add(a: number, b: number) { return a + b; }`
  1489. );
  1490. fs.writeFileSync(
  1491. path.join(srcDir, 'string.ts'),
  1492. `export function capitalize(s: string) { return s.toUpperCase(); }`
  1493. );
  1494. // Initialize and index
  1495. const cg = CodeGraph.initSync(tempDir);
  1496. const result = await cg.indexAll();
  1497. expect(result.success).toBe(true);
  1498. expect(result.filesIndexed).toBe(2);
  1499. const files = cg.getFiles();
  1500. expect(files.length).toBe(2);
  1501. cg.close();
  1502. });
  1503. it('should track file hashes for incremental updates', async () => {
  1504. // Create initial file
  1505. const srcDir = path.join(tempDir, 'src');
  1506. fs.mkdirSync(srcDir);
  1507. fs.writeFileSync(path.join(srcDir, 'main.ts'), `export const x = 1;`);
  1508. // Initialize and index
  1509. const cg = CodeGraph.initSync(tempDir);
  1510. await cg.indexAll();
  1511. // Check file is tracked
  1512. const file = cg.getFile('src/main.ts');
  1513. expect(file).toBeDefined();
  1514. expect(file?.contentHash).toBeDefined();
  1515. // Modify file
  1516. fs.writeFileSync(path.join(srcDir, 'main.ts'), `export const x = 2;`);
  1517. // Check for changes
  1518. const changes = cg.getChangedFiles();
  1519. expect(changes.modified).toContain('src/main.ts');
  1520. cg.close();
  1521. });
  1522. it('should sync and detect changes', async () => {
  1523. // Create initial file
  1524. const srcDir = path.join(tempDir, 'src');
  1525. fs.mkdirSync(srcDir);
  1526. fs.writeFileSync(
  1527. path.join(srcDir, 'main.ts'),
  1528. `export function original() { return 1; }`
  1529. );
  1530. // Initialize and index
  1531. const cg = CodeGraph.initSync(tempDir);
  1532. await cg.indexAll();
  1533. const initialNodes = cg.getNodesInFile('src/main.ts');
  1534. expect(initialNodes.some((n) => n.name === 'original')).toBe(true);
  1535. // Modify file
  1536. fs.writeFileSync(
  1537. path.join(srcDir, 'main.ts'),
  1538. `export function updated() { return 2; }`
  1539. );
  1540. // Sync
  1541. const syncResult = await cg.sync();
  1542. expect(syncResult.filesModified).toBe(1);
  1543. // Check nodes were updated
  1544. const updatedNodes = cg.getNodesInFile('src/main.ts');
  1545. expect(updatedNodes.some((n) => n.name === 'updated')).toBe(true);
  1546. expect(updatedNodes.some((n) => n.name === 'original')).toBe(false);
  1547. cg.close();
  1548. });
  1549. });
  1550. describe('Path Normalization', () => {
  1551. it('should convert backslashes to forward slashes', () => {
  1552. expect(normalizePath('gui\\node_modules\\foo')).toBe('gui/node_modules/foo');
  1553. expect(normalizePath('src\\components\\Button.tsx')).toBe('src/components/Button.tsx');
  1554. });
  1555. it('should leave forward-slash paths unchanged', () => {
  1556. expect(normalizePath('src/components/Button.tsx')).toBe('src/components/Button.tsx');
  1557. });
  1558. it('should handle empty string', () => {
  1559. expect(normalizePath('')).toBe('');
  1560. });
  1561. });
  1562. describe('Directory Exclusion', () => {
  1563. let tempDir: string;
  1564. beforeEach(() => {
  1565. tempDir = createTempDir();
  1566. });
  1567. afterEach(() => {
  1568. cleanupTempDir(tempDir);
  1569. });
  1570. it('should exclude node_modules directories', () => {
  1571. // Create structure: src/index.ts + node_modules/pkg/index.js
  1572. const srcDir = path.join(tempDir, 'src');
  1573. const nmDir = path.join(tempDir, 'node_modules', 'pkg');
  1574. fs.mkdirSync(srcDir, { recursive: true });
  1575. fs.mkdirSync(nmDir, { recursive: true });
  1576. fs.writeFileSync(path.join(srcDir, 'index.ts'), 'export const x = 1;');
  1577. fs.writeFileSync(path.join(nmDir, 'index.js'), 'module.exports = {};');
  1578. const config = { ...DEFAULT_CONFIG, rootDir: tempDir };
  1579. const files = scanDirectory(tempDir, config);
  1580. expect(files).toContain('src/index.ts');
  1581. expect(files.every((f) => !f.includes('node_modules'))).toBe(true);
  1582. });
  1583. it('should exclude nested node_modules directories', () => {
  1584. // Create structure: packages/app/node_modules/pkg/index.js
  1585. const srcDir = path.join(tempDir, 'packages', 'app', 'src');
  1586. const nmDir = path.join(tempDir, 'packages', 'app', 'node_modules', 'pkg');
  1587. fs.mkdirSync(srcDir, { recursive: true });
  1588. fs.mkdirSync(nmDir, { recursive: true });
  1589. fs.writeFileSync(path.join(srcDir, 'index.ts'), 'export const x = 1;');
  1590. fs.writeFileSync(path.join(nmDir, 'index.js'), 'module.exports = {};');
  1591. const config = { ...DEFAULT_CONFIG, rootDir: tempDir };
  1592. const files = scanDirectory(tempDir, config);
  1593. expect(files).toContain('packages/app/src/index.ts');
  1594. expect(files.every((f) => !f.includes('node_modules'))).toBe(true);
  1595. });
  1596. it('should exclude .git directories', () => {
  1597. const srcDir = path.join(tempDir, 'src');
  1598. const gitDir = path.join(tempDir, '.git', 'objects');
  1599. fs.mkdirSync(srcDir, { recursive: true });
  1600. fs.mkdirSync(gitDir, { recursive: true });
  1601. fs.writeFileSync(path.join(srcDir, 'index.ts'), 'export const x = 1;');
  1602. fs.writeFileSync(path.join(gitDir, 'pack.ts'), 'export const y = 2;');
  1603. const config = { ...DEFAULT_CONFIG, rootDir: tempDir };
  1604. const files = scanDirectory(tempDir, config);
  1605. expect(files).toContain('src/index.ts');
  1606. expect(files.every((f) => !f.includes('.git'))).toBe(true);
  1607. });
  1608. it('should return forward-slash paths on all platforms', () => {
  1609. const srcDir = path.join(tempDir, 'src', 'components');
  1610. fs.mkdirSync(srcDir, { recursive: true });
  1611. fs.writeFileSync(path.join(srcDir, 'Button.tsx'), 'export function Button() {}');
  1612. const config = { ...DEFAULT_CONFIG, rootDir: tempDir };
  1613. const files = scanDirectory(tempDir, config);
  1614. expect(files.length).toBe(1);
  1615. expect(files[0]).toBe('src/components/Button.tsx');
  1616. expect(files[0]).not.toContain('\\');
  1617. });
  1618. it('should respect .codegraphignore marker', () => {
  1619. const srcDir = path.join(tempDir, 'src');
  1620. const vendorDir = path.join(tempDir, 'vendor');
  1621. fs.mkdirSync(srcDir, { recursive: true });
  1622. fs.mkdirSync(vendorDir, { recursive: true });
  1623. fs.writeFileSync(path.join(srcDir, 'index.ts'), 'export const x = 1;');
  1624. fs.writeFileSync(path.join(vendorDir, 'lib.ts'), 'export const y = 2;');
  1625. fs.writeFileSync(path.join(vendorDir, '.codegraphignore'), '');
  1626. const config = { ...DEFAULT_CONFIG, rootDir: tempDir };
  1627. const files = scanDirectory(tempDir, config);
  1628. expect(files).toContain('src/index.ts');
  1629. expect(files.every((f) => !f.includes('vendor'))).toBe(true);
  1630. });
  1631. });