frameworks-integration.test.ts 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. import { describe, it, expect, beforeAll, afterEach } from 'vitest';
  2. import * as fs from 'fs';
  3. import * as path from 'path';
  4. import * as os from 'os';
  5. import { CodeGraph } from '../src';
  6. import { initGrammars, loadAllGrammars } from '../src/extraction/grammars';
  7. beforeAll(async () => {
  8. await initGrammars();
  9. await loadAllGrammars();
  10. });
  11. describe('Django end-to-end framework extraction', () => {
  12. let tmpDir: string | undefined;
  13. afterEach(() => {
  14. if (tmpDir) fs.rmSync(tmpDir, { recursive: true, force: true });
  15. tmpDir = undefined;
  16. });
  17. it('creates a route->view edge from urls.py to view class', async () => {
  18. tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-django-'));
  19. fs.writeFileSync(path.join(tmpDir, 'manage.py'), '# marker\n');
  20. fs.writeFileSync(path.join(tmpDir, 'requirements.txt'), 'django==4.2\n');
  21. fs.mkdirSync(path.join(tmpDir, 'users'));
  22. fs.writeFileSync(path.join(tmpDir, 'users/__init__.py'), '');
  23. fs.writeFileSync(
  24. path.join(tmpDir, 'users/views.py'),
  25. 'class UserListView:\n def get(self, request): pass\n'
  26. );
  27. fs.writeFileSync(
  28. path.join(tmpDir, 'users/urls.py'),
  29. 'from django.urls import path\n' +
  30. 'from users.views import UserListView\n' +
  31. 'urlpatterns = [path("users/", UserListView.as_view(), name="user-list")]\n'
  32. );
  33. const cg = CodeGraph.initSync(tmpDir);
  34. await cg.indexAll();
  35. // Route node exists
  36. const routes = cg.getNodesByKind('route');
  37. expect(routes.length).toBeGreaterThan(0);
  38. const route = routes.find((n) => n.name === 'users/');
  39. expect(route).toBeDefined();
  40. // View class exists
  41. const classNodes = cg.getNodesByKind('class');
  42. const view = classNodes.find((n) => n.name === 'UserListView');
  43. expect(view).toBeDefined();
  44. // Edge route -> view exists
  45. const edges = cg.getOutgoingEdges(route!.id);
  46. const toView = edges.find((e) => e.target === view!.id);
  47. expect(toView).toBeDefined();
  48. expect(toView!.kind).toBe('references');
  49. cg.close();
  50. });
  51. });
  52. describe('Flask end-to-end framework extraction', () => {
  53. let tmpDir: string | undefined;
  54. afterEach(() => {
  55. if (tmpDir) fs.rmSync(tmpDir, { recursive: true, force: true });
  56. tmpDir = undefined;
  57. });
  58. it('resolves stacked routes across @login_required to a view named after a builtin (index)', async () => {
  59. tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-flask-'));
  60. fs.writeFileSync(path.join(tmpDir, 'requirements.txt'), 'flask==3.0\n');
  61. fs.writeFileSync(
  62. path.join(tmpDir, 'app.py'),
  63. 'from flask import Blueprint, render_template\n' +
  64. 'from flask_login import login_required\n' +
  65. 'bp = Blueprint("main", __name__)\n' +
  66. '\n' +
  67. '@bp.route("/", methods=["GET", "POST"])\n' +
  68. '@bp.route("/index", methods=["GET", "POST"])\n' +
  69. '@login_required\n' +
  70. 'def index():\n' +
  71. ' return render_template("index.html")\n'
  72. );
  73. const cg = CodeGraph.initSync(tmpDir);
  74. await cg.indexAll();
  75. // Both stacked @bp.route decorators are extracted (the second was previously
  76. // dropped because @login_required broke the "def must follow" assumption).
  77. const routes = cg.getNodesByKind('route');
  78. expect(routes.map((r) => r.name).sort()).toEqual(['GET /', 'GET /index']);
  79. // The view function exists even though its name is a Python builtin method.
  80. const fn = cg.getNodesByKind('function').find((n) => n.name === 'index');
  81. expect(fn).toBeDefined();
  82. // Both routes resolve to it — exercises the bare-name builtin guard, which
  83. // previously filtered the `index` reference as a builtin method.
  84. for (const route of routes) {
  85. const edges = cg.getOutgoingEdges(route.id);
  86. const toView = edges.find((e) => e.target === fn!.id && e.kind === 'references');
  87. expect(toView, `route ${route.name} should resolve to index()`).toBeDefined();
  88. }
  89. cg.close();
  90. });
  91. });
  92. describe('Flutter end-to-end — setState→build synthesis', () => {
  93. let tmpDir: string | undefined;
  94. afterEach(() => {
  95. if (tmpDir) fs.rmSync(tmpDir, { recursive: true, force: true });
  96. tmpDir = undefined;
  97. });
  98. it('synthesizes a handler→build edge when a State method calls setState', async () => {
  99. tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-flutter-'));
  100. fs.writeFileSync(
  101. path.join(tmpDir, 'main.dart'),
  102. 'import "package:flutter/material.dart";\n' +
  103. 'class CounterPage extends StatefulWidget {\n' +
  104. ' @override\n' +
  105. ' State<CounterPage> createState() => _CounterPageState();\n' +
  106. '}\n' +
  107. 'class _CounterPageState extends State<CounterPage> {\n' +
  108. ' int _count = 0;\n' +
  109. ' void _increment() {\n' +
  110. ' setState(() {\n' +
  111. ' _count++;\n' +
  112. ' });\n' +
  113. ' }\n' +
  114. ' @override\n' +
  115. ' Widget build(BuildContext context) {\n' +
  116. ' return Text("$_count");\n' +
  117. ' }\n' +
  118. '}\n'
  119. );
  120. const cg = CodeGraph.initSync(tmpDir);
  121. await cg.indexAll();
  122. const methods = cg.getNodesByKind('method');
  123. const increment = methods.find((n) => n.name === '_increment');
  124. const build = methods.find((n) => n.name === 'build');
  125. expect(increment).toBeDefined();
  126. expect(build).toBeDefined();
  127. // setState re-runs build (Flutter-internal, no static edge). The synthesizer
  128. // bridges the handler → build so the "tap → setState → rebuilt UI" flow connects.
  129. const edges = cg.getOutgoingEdges(increment!.id);
  130. const toBuild = edges.find((e) => e.target === build!.id && e.kind === 'calls');
  131. expect(toBuild, '_increment should reach build via setState synthesis').toBeDefined();
  132. cg.close();
  133. });
  134. });
  135. describe('C++ end-to-end — virtual override synthesis', () => {
  136. let tmpDir: string | undefined;
  137. afterEach(() => {
  138. if (tmpDir) fs.rmSync(tmpDir, { recursive: true, force: true });
  139. tmpDir = undefined;
  140. });
  141. it('resolves callers through typed object pointers', async () => {
  142. tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-cpp-'));
  143. let cg: CodeGraph | undefined;
  144. try {
  145. fs.writeFileSync(
  146. path.join(tmpDir, 'detect.hpp'),
  147. 'class CDetect {\n' +
  148. ' public:\n' +
  149. ' int Processing();\n' +
  150. '};\n' +
  151. 'class CDetector {\n' +
  152. ' private:\n' +
  153. ' CDetect* m_cpAlg = nullptr;\n' +
  154. ' public:\n' +
  155. ' int Run();\n' +
  156. ' int Flush();\n' +
  157. '};\n'
  158. );
  159. fs.writeFileSync(
  160. path.join(tmpDir, 'detect.cpp'),
  161. '#include "detect.hpp"\n' +
  162. 'int CDetector::Run() { return m_cpAlg->Processing(); }\n' +
  163. 'int CDetector::Flush() { return m_cpAlg->Processing(); }\n' +
  164. 'int CDetect::Processing() { return 0; }\n'
  165. );
  166. cg = CodeGraph.initSync(tmpDir);
  167. await cg.indexAll();
  168. const processing = cg
  169. .getNodesByKind('method')
  170. .find((n) => n.qualifiedName.endsWith('CDetect::Processing'));
  171. expect(processing).toBeDefined();
  172. const callers = cg.getCallers(processing!.id).map((c) => c.node.qualifiedName);
  173. expect(callers).toContain('CDetector::Run');
  174. expect(callers).toContain('CDetector::Flush');
  175. const runMethod = cg
  176. .getNodesByKind('method')
  177. .find((n) => n.qualifiedName.endsWith('CDetector::Run'));
  178. expect(runMethod).toBeDefined();
  179. const callees = cg.getCallees(runMethod!.id).map((c) => c.node.qualifiedName);
  180. expect(callees).toContain('CDetect::Processing');
  181. } finally {
  182. cg?.close();
  183. }
  184. });
  185. it('resolves typed pointer callers when the method name is ambiguous and the call sits inside a return/declaration', async () => {
  186. // Regression: an earlier version of the C++ receiver-type inference matched
  187. // the call line itself (`return m_cpAlg->Processing()`) and treated `return`
  188. // as the type, OR grabbed `int r =` as a type from the prefix. With Strategy
  189. // 3's "unique method name" fallback, the original issue example resolved
  190. // anyway — but as soon as two classes share a method name (very common in
  191. // real C++), both calls go unresolved.
  192. tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-cpp-'));
  193. let cg: CodeGraph | undefined;
  194. try {
  195. fs.writeFileSync(
  196. path.join(tmpDir, 'detect.hpp'),
  197. 'class CDetect { public: int Processing(); };\n' +
  198. 'class CWidget { public: int Processing(); };\n' +
  199. 'class CDetector {\n' +
  200. ' private:\n' +
  201. ' CDetect* m_cpAlg = nullptr;\n' +
  202. ' public:\n' +
  203. ' int RunReturn();\n' +
  204. ' int RunAssign();\n' +
  205. '};\n'
  206. );
  207. fs.writeFileSync(
  208. path.join(tmpDir, 'detect.cpp'),
  209. '#include "detect.hpp"\n' +
  210. 'int CDetector::RunReturn() { return m_cpAlg->Processing(); }\n' +
  211. 'int CDetector::RunAssign() { int r = m_cpAlg->Processing(); return r; }\n' +
  212. 'int CDetect::Processing() { return 0; }\n' +
  213. 'int CWidget::Processing() { return 0; }\n'
  214. );
  215. cg = CodeGraph.initSync(tmpDir);
  216. await cg.indexAll();
  217. const detectProc = cg
  218. .getNodesByKind('method')
  219. .find((n) => n.qualifiedName === 'CDetect::Processing');
  220. const widgetProc = cg
  221. .getNodesByKind('method')
  222. .find((n) => n.qualifiedName === 'CWidget::Processing');
  223. expect(detectProc).toBeDefined();
  224. expect(widgetProc).toBeDefined();
  225. const detectCallers = cg.getCallers(detectProc!.id).map((c) => c.node.qualifiedName);
  226. expect(detectCallers).toContain('CDetector::RunReturn');
  227. expect(detectCallers).toContain('CDetector::RunAssign');
  228. // CWidget::Processing is never called — calls must NOT misroute here.
  229. const widgetCallers = cg.getCallers(widgetProc!.id).map((c) => c.node.qualifiedName);
  230. expect(widgetCallers).not.toContain('CDetector::RunReturn');
  231. expect(widgetCallers).not.toContain('CDetector::RunAssign');
  232. } finally {
  233. cg?.close();
  234. }
  235. });
  236. it('bridges a base virtual method to the subclass override', async () => {
  237. tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-cpp-'));
  238. fs.writeFileSync(
  239. path.join(tmpDir, 'iter.cpp'),
  240. 'class Iterator {\n' +
  241. ' public:\n' +
  242. ' virtual void Next() { }\n' +
  243. '};\n' +
  244. 'class DBIter : public Iterator {\n' +
  245. ' public:\n' +
  246. ' void Next() override { advance(); }\n' +
  247. ' void advance() { }\n' +
  248. '};\n'
  249. );
  250. const cg = CodeGraph.initSync(tmpDir);
  251. await cg.indexAll();
  252. // Two methods named Next: the base virtual (lower line) and the override.
  253. const nexts = cg
  254. .getNodesByKind('method')
  255. .filter((n) => n.name === 'Next')
  256. .sort((a, b) => a.startLine - b.startLine);
  257. expect(nexts.length).toBe(2);
  258. const [baseNext, overrideNext] = nexts;
  259. // A vtable call to Iterator::Next dispatches to DBIter::Next — bridge it so
  260. // trace/callees from the interface method reaches the implementation.
  261. const edge = cg
  262. .getOutgoingEdges(baseNext!.id)
  263. .find((e) => e.target === overrideNext!.id && e.kind === 'calls');
  264. expect(edge, 'Iterator::Next should reach DBIter::Next via override synthesis').toBeDefined();
  265. cg.close();
  266. });
  267. });
  268. describe('Java end-to-end — field-injected bean trace (issue #389)', () => {
  269. let tmpDir: string | undefined;
  270. afterEach(() => {
  271. if (tmpDir) fs.rmSync(tmpDir, { recursive: true, force: true });
  272. tmpDir = undefined;
  273. });
  274. // Mirrors the issue's Spring MVC pattern:
  275. // UserAction(@Resource UserBO userbo).toLogin2() -> this.userbo.toLogin2()
  276. // -> UserBO.toLogin2() -> userService.toLogin() -> UserService.toLogin (iface)
  277. // -> UserServiceImpl.toLogin() via interface→impl synthesis.
  278. // Without the extractor `this.` strip + field-typed receiver lookup, the very
  279. // first hop (controller -> bean) was missing entirely, breaking trace.
  280. it('connects controller -> @Resource bean -> interface -> impl end-to-end', async () => {
  281. tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-spring-bean-'));
  282. const javaDir = path.join(tmpDir, 'src/main/java/com/example/user');
  283. fs.mkdirSync(path.join(javaDir, 'action'), { recursive: true });
  284. fs.mkdirSync(path.join(javaDir, 'bo'), { recursive: true });
  285. fs.mkdirSync(path.join(javaDir, 'service'), { recursive: true });
  286. fs.mkdirSync(path.join(javaDir, 'service/impl'), { recursive: true });
  287. fs.writeFileSync(
  288. path.join(tmpDir, 'pom.xml'),
  289. '<project><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies></project>\n'
  290. );
  291. fs.writeFileSync(
  292. path.join(javaDir, 'action/UserAction.java'),
  293. 'package com.example.user.action;\n' +
  294. 'import com.example.user.bo.UserBO;\n' +
  295. 'import javax.annotation.Resource;\n' +
  296. '@org.springframework.stereotype.Controller\n' +
  297. 'public class UserAction {\n' +
  298. ' @Resource(name = "userBO") private UserBO userbo;\n' +
  299. ' public void toLogin2() { this.userbo.toLogin2(); }\n' +
  300. '}\n'
  301. );
  302. fs.writeFileSync(
  303. path.join(javaDir, 'bo/UserBO.java'),
  304. 'package com.example.user.bo;\n' +
  305. 'import com.example.user.service.UserService;\n' +
  306. 'import javax.annotation.Resource;\n' +
  307. '@org.springframework.stereotype.Component("userBO")\n' +
  308. 'public class UserBO {\n' +
  309. ' @Resource private UserService userService;\n' +
  310. ' public void toLogin2() { userService.toLogin(); }\n' +
  311. '}\n'
  312. );
  313. fs.writeFileSync(
  314. path.join(javaDir, 'service/UserService.java'),
  315. 'package com.example.user.service;\n' +
  316. 'public interface UserService { void toLogin(); }\n'
  317. );
  318. fs.writeFileSync(
  319. path.join(javaDir, 'service/impl/UserServiceImpl.java'),
  320. 'package com.example.user.service.impl;\n' +
  321. 'import com.example.user.service.UserService;\n' +
  322. '@org.springframework.stereotype.Service("userService")\n' +
  323. 'public class UserServiceImpl implements UserService {\n' +
  324. ' public void toLogin() { }\n' +
  325. '}\n'
  326. );
  327. const cg = CodeGraph.initSync(tmpDir);
  328. await cg.indexAll();
  329. const methods = cg.getNodesByKind('method');
  330. const find = (cls: string, name: string) =>
  331. methods.find((m) => m.name === name && m.filePath.endsWith(`${cls}.java`));
  332. const action = find('UserAction', 'toLogin2');
  333. const bo = find('UserBO', 'toLogin2');
  334. const svc = find('UserService', 'toLogin');
  335. const impl = find('UserServiceImpl', 'toLogin');
  336. expect(action).toBeDefined();
  337. expect(bo).toBeDefined();
  338. expect(svc).toBeDefined();
  339. expect(impl).toBeDefined();
  340. // UserAction.toLogin2 -> UserBO.toLogin2 (the regressed hop — `this.userbo`
  341. // receiver was emitted verbatim and the field-type lookup didn't exist).
  342. const actionToBo = cg.getOutgoingEdges(action!.id).find((e) => e.target === bo!.id);
  343. expect(actionToBo, 'controller `this.userbo.toLogin2()` should reach UserBO.toLogin2').toBeDefined();
  344. expect(actionToBo!.kind).toBe('calls');
  345. // UserBO.toLogin2 -> UserService.toLogin (plain identifier receiver, works pre-fix).
  346. const boToSvc = cg.getOutgoingEdges(bo!.id).find((e) => e.target === svc!.id);
  347. expect(boToSvc).toBeDefined();
  348. // UserService.toLogin -> UserServiceImpl.toLogin (interface->impl synth).
  349. const svcToImpl = cg.getOutgoingEdges(svc!.id).find((e) => e.target === impl!.id);
  350. expect(svcToImpl).toBeDefined();
  351. cg.close();
  352. });
  353. it('bridges a Java mapper interface method to its MyBatis XML statement (incl. SQL fragments)', async () => {
  354. tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-mybatis-'));
  355. const javaDir = path.join(tmpDir, 'src/main/java/com/example/dao');
  356. const xmlDir = path.join(tmpDir, 'src/main/resources/mappers');
  357. fs.mkdirSync(javaDir, { recursive: true });
  358. fs.mkdirSync(xmlDir, { recursive: true });
  359. fs.writeFileSync(
  360. path.join(tmpDir, 'pom.xml'),
  361. '<project><dependencies><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId></dependency></dependencies></project>\n'
  362. );
  363. fs.writeFileSync(
  364. path.join(javaDir, 'UserDAOMapper.java'),
  365. 'package com.example.dao;\n' +
  366. 'public interface UserDAOMapper {\n' +
  367. ' Object getById(int id);\n' +
  368. ' int updateUser(Object u);\n' +
  369. '}\n'
  370. );
  371. fs.writeFileSync(
  372. path.join(xmlDir, 'UserDAOMapper.xml'),
  373. '<?xml version="1.0" encoding="UTF-8"?>\n' +
  374. '<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">\n' +
  375. '<mapper namespace="com.example.dao.UserDAOMapper">\n' +
  376. ' <sql id="userCols">id, name, email</sql>\n' +
  377. ' <select id="getById" parameterType="int" resultType="User">\n' +
  378. ' SELECT <include refid="userCols"/> FROM users WHERE id = #{id}\n' +
  379. ' </select>\n' +
  380. ' <update id="updateUser" parameterType="User">\n' +
  381. ' UPDATE users SET name=#{name}, email=#{email} WHERE id=#{id}\n' +
  382. ' </update>\n' +
  383. '</mapper>\n'
  384. );
  385. const cg = CodeGraph.initSync(tmpDir);
  386. await cg.indexAll();
  387. const methods = cg.getNodesByKind('method');
  388. const getByIdJava = methods.find((m) => m.name === 'getById' && m.language === 'java');
  389. const getByIdXml = methods.find((m) => m.name === 'getById' && m.language === 'xml');
  390. const updateJava = methods.find((m) => m.name === 'updateUser' && m.language === 'java');
  391. const updateXml = methods.find((m) => m.name === 'updateUser' && m.language === 'xml');
  392. const sqlFrag = methods.find((m) => m.name === 'userCols' && m.language === 'xml');
  393. expect(getByIdJava).toBeDefined();
  394. expect(getByIdXml).toBeDefined();
  395. expect(updateJava).toBeDefined();
  396. expect(updateXml).toBeDefined();
  397. expect(sqlFrag).toBeDefined();
  398. // XML statement qualified name must be `<namespace>::<id>` so the
  399. // synthesizer can match against the Java method's `<Class>::<method>`
  400. // suffix — this is the load-bearing contract between extractor + synthesis.
  401. expect(getByIdXml!.qualifiedName).toBe('com.example.dao.UserDAOMapper::getById');
  402. // Bridge: Java mapper method -> XML statement, kind 'calls'.
  403. const j2xGet = cg.getOutgoingEdges(getByIdJava!.id).find((e) => e.target === getByIdXml!.id);
  404. expect(j2xGet, 'Java getById should reach the XML <select id="getById">').toBeDefined();
  405. expect(j2xGet!.kind).toBe('calls');
  406. const j2xUpd = cg.getOutgoingEdges(updateJava!.id).find((e) => e.target === updateXml!.id);
  407. expect(j2xUpd, 'Java updateUser should reach the XML <update id="updateUser">').toBeDefined();
  408. // <include refid="userCols"/> inside <select> -> <sql id="userCols"> in same mapper.
  409. const incEdge = cg.getOutgoingEdges(getByIdXml!.id).find((e) => e.target === sqlFrag!.id);
  410. expect(incEdge, '<include refid="userCols"/> should reach the <sql> fragment').toBeDefined();
  411. cg.close();
  412. });
  413. it('binds @Value / @ConfigurationProperties to YAML + .properties keys (incl. relaxed binding)', async () => {
  414. tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-spring-config-'));
  415. const javaDir = path.join(tmpDir, 'src/main/java/com/example');
  416. const resDir = path.join(tmpDir, 'src/main/resources');
  417. fs.mkdirSync(javaDir, { recursive: true });
  418. fs.mkdirSync(resDir, { recursive: true });
  419. fs.writeFileSync(
  420. path.join(tmpDir, 'pom.xml'),
  421. '<project><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency></dependencies></project>\n'
  422. );
  423. fs.writeFileSync(
  424. path.join(resDir, 'application.yml'),
  425. 'app:\n' +
  426. ' cache:\n' +
  427. ' name:\n' +
  428. ' user-token: "example-service:auth:token"\n' +
  429. ' enabled: true\n' +
  430. 'db:\n' +
  431. ' url: "jdbc:mysql://localhost/x"\n'
  432. );
  433. fs.writeFileSync(
  434. path.join(resDir, 'application.properties'),
  435. 'app.retry-count=3\n'
  436. );
  437. fs.writeFileSync(
  438. path.join(javaDir, 'CacheConfig.java'),
  439. 'package com.example;\n' +
  440. 'import org.springframework.beans.factory.annotation.Value;\n' +
  441. 'public class CacheConfig {\n' +
  442. ' @Value("${app.cache.name.user-token}") private String tokenCacheName;\n' +
  443. ' @Value("${app.cache.enabled:true}") private boolean enabled;\n' +
  444. ' // relaxed binding: java camelCase, properties kebab-case\n' +
  445. ' @Value("${app.retryCount}") private int retry;\n' +
  446. '}\n'
  447. );
  448. fs.writeFileSync(
  449. path.join(javaDir, 'CacheProperties.java'),
  450. 'package com.example;\n' +
  451. 'import org.springframework.boot.context.properties.ConfigurationProperties;\n' +
  452. '@ConfigurationProperties(prefix = "app.cache")\n' +
  453. 'public class CacheProperties { private boolean enabled; }\n'
  454. );
  455. const cg = CodeGraph.initSync(tmpDir);
  456. await cg.indexAll();
  457. // YAML/properties leaf keys: one constant node per dotted path.
  458. const cfgKeys = cg
  459. .getNodesByKind('constant')
  460. .filter((n) => n.language === 'yaml' || n.language === 'properties');
  461. const cfgByQn = (qn: string) => cfgKeys.find((n) => n.qualifiedName === qn);
  462. expect(cfgByQn('app.cache.name.user-token')).toBeDefined();
  463. expect(cfgByQn('app.cache.enabled')).toBeDefined();
  464. expect(cfgByQn('db.url')).toBeDefined();
  465. expect(cfgByQn('app.retry-count')).toBeDefined();
  466. // @Value("${app.cache.name.user-token}") -> the YAML leaf key.
  467. const valueBindings = cg
  468. .getNodesByKind('constant')
  469. .filter((n) => n.id.startsWith('spring-value:'));
  470. const userToken = valueBindings.find((n) => n.name === 'app.cache.name.user-token');
  471. expect(userToken).toBeDefined();
  472. const userTokenEdges = cg.getOutgoingEdges(userToken!.id);
  473. const userTokenTarget = userTokenEdges.find((e) =>
  474. cfgKeys.some((c) => c.id === e.target && c.qualifiedName === 'app.cache.name.user-token'),
  475. );
  476. expect(userTokenTarget, '@Value should reference the YAML leaf key').toBeDefined();
  477. // Default-value form `${k:default}` — strip the `:default` and bind the key.
  478. const enabledBind = valueBindings.find((n) => n.name === 'app.cache.enabled');
  479. expect(enabledBind).toBeDefined();
  480. expect(cg.getOutgoingEdges(enabledBind!.id).some((e) => {
  481. const t = cfgByQn('app.cache.enabled');
  482. return t && e.target === t.id;
  483. })).toBe(true);
  484. // Relaxed binding: `app.retryCount` (camel) -> `app.retry-count` (kebab).
  485. const retryBind = valueBindings.find((n) => n.name === 'app.retryCount');
  486. expect(retryBind).toBeDefined();
  487. expect(cg.getOutgoingEdges(retryBind!.id).some((e) => {
  488. const t = cfgByQn('app.retry-count');
  489. return t && e.target === t.id;
  490. })).toBe(true);
  491. // @ConfigurationProperties(prefix="app.cache") -> a key under that prefix.
  492. const cpBindings = cg
  493. .getNodesByKind('constant')
  494. .filter((n) => n.id.startsWith('spring-cp:'));
  495. const cpAppCache = cpBindings.find((n) => n.name === 'app.cache');
  496. expect(cpAppCache).toBeDefined();
  497. const cpEdges = cg.getOutgoingEdges(cpAppCache!.id);
  498. expect(cpEdges.length).toBeGreaterThan(0);
  499. cg.close();
  500. });
  501. it('emits only a file node for non-MyBatis XML (pom.xml, beans.xml, log4j.xml)', async () => {
  502. tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-xml-non-mybatis-'));
  503. fs.writeFileSync(
  504. path.join(tmpDir, 'pom.xml'),
  505. '<project><groupId>x</groupId><artifactId>y</artifactId></project>\n'
  506. );
  507. fs.writeFileSync(
  508. path.join(tmpDir, 'log4j.xml'),
  509. '<?xml version="1.0"?><Configuration><Loggers><Root level="info"/></Loggers></Configuration>\n'
  510. );
  511. const cg = CodeGraph.initSync(tmpDir);
  512. await cg.indexAll();
  513. // No method nodes — non-mapper XML produces no symbols (just file rows).
  514. expect(cg.getNodesByKind('method').filter((n) => n.language === 'xml').length).toBe(0);
  515. cg.close();
  516. });
  517. it('resolves a `this.field.method()` call to a unique implementation class', async () => {
  518. // Standalone test of the extractor `this.` strip: even without Spring annotations,
  519. // `this.svc.run()` where `svc` is typed as a concrete class should route to that
  520. // class's method. This is the general Java fix, Spring is only one consumer.
  521. tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-java-this-field-'));
  522. fs.writeFileSync(
  523. path.join(tmpDir, 'App.java'),
  524. 'class Svc { public void run() { } }\n' +
  525. 'class App {\n' +
  526. ' private Svc svc;\n' +
  527. ' public void go() { this.svc.run(); }\n' +
  528. '}\n'
  529. );
  530. const cg = CodeGraph.initSync(tmpDir);
  531. await cg.indexAll();
  532. const methods = cg.getNodesByKind('method');
  533. const go = methods.find((m) => m.name === 'go');
  534. const run = methods.find((m) => m.name === 'run');
  535. expect(go && run).toBeTruthy();
  536. const edge = cg.getOutgoingEdges(go!.id).find((e) => e.target === run!.id);
  537. expect(edge, '`this.svc.run()` should resolve to Svc.run').toBeDefined();
  538. cg.close();
  539. });
  540. });
  541. describe('JVM FQN imports — end-to-end', () => {
  542. let tmpDir: string | undefined;
  543. afterEach(() => {
  544. if (tmpDir) fs.rmSync(tmpDir, { recursive: true, force: true });
  545. tmpDir = undefined;
  546. });
  547. it('resolves a Kotlin import when the file name differs from the class name', async () => {
  548. // Bar lives in Models.kt — the filesystem-based Java-style path lookup
  549. // (com/example/Bar.kt) misses this; only FQN-via-qualifiedName finds it.
  550. tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-jvm-imp-'));
  551. fs.writeFileSync(
  552. path.join(tmpDir, 'Models.kt'),
  553. 'package com.example\n\nclass Bar {\n fun greet(): String = "hi"\n}\n'
  554. );
  555. fs.writeFileSync(
  556. path.join(tmpDir, 'Caller.kt'),
  557. 'package com.example.app\n\nimport com.example.Bar\n\nclass App {\n fun run() { Bar().greet() }\n}\n'
  558. );
  559. const cg = CodeGraph.initSync(tmpDir);
  560. await cg.indexAll();
  561. const bar = cg.getNodesByKind('class').find((n) => n.qualifiedName === 'com.example::Bar');
  562. expect(bar, 'Bar should be extracted with package-qualified name').toBeDefined();
  563. const importNode = cg.getNodesByKind('import').find((n) => n.name === 'com.example.Bar');
  564. expect(importNode, 'import statement node should exist').toBeDefined();
  565. // The imports edge may originate from the import node OR from a parent
  566. // scope (file / namespace) — accept either, but require that an
  567. // imports-kind edge to Bar exists.
  568. const reachesBar = cg
  569. .getIncomingEdges(bar!.id)
  570. .find((e) => e.kind === 'imports');
  571. expect(reachesBar, 'an imports edge should resolve to Bar via FQN').toBeDefined();
  572. cg.close();
  573. });
  574. it('resolves a Kotlin top-level function import', async () => {
  575. tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-jvm-imp-'));
  576. fs.writeFileSync(
  577. path.join(tmpDir, 'Utils.kt'),
  578. 'package com.example\n\nfun util(): Int = 42\n'
  579. );
  580. fs.writeFileSync(
  581. path.join(tmpDir, 'Caller.kt'),
  582. 'package com.example.app\n\nimport com.example.util\n\nfun main() { util() }\n'
  583. );
  584. const cg = CodeGraph.initSync(tmpDir);
  585. await cg.indexAll();
  586. const util = cg.getNodesByKind('function').find((n) => n.qualifiedName === 'com.example::util');
  587. expect(util, 'top-level util() should be extracted under com.example').toBeDefined();
  588. const edge = cg.getIncomingEdges(util!.id).find((e) => e.kind === 'imports');
  589. expect(edge, 'imports edge should reach the top-level function by FQN').toBeDefined();
  590. });
  591. it('resolves cross-language: Kotlin importing a Java class', async () => {
  592. tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-jvm-imp-'));
  593. fs.writeFileSync(
  594. path.join(tmpDir, 'JavaBar.java'),
  595. 'package com.example;\n\npublic class JavaBar {\n public String greet() { return "hi"; }\n}\n'
  596. );
  597. fs.writeFileSync(
  598. path.join(tmpDir, 'Caller.kt'),
  599. 'package com.example.app\n\nimport com.example.JavaBar\n\nfun main() { JavaBar().greet() }\n'
  600. );
  601. const cg = CodeGraph.initSync(tmpDir);
  602. await cg.indexAll();
  603. const javaBar = cg.getNodesByKind('class').find((n) => n.qualifiedName === 'com.example::JavaBar');
  604. expect(javaBar, 'JavaBar should be extracted under com.example regardless of language').toBeDefined();
  605. const edge = cg.getIncomingEdges(javaBar!.id).find((e) => e.kind === 'imports');
  606. expect(edge, 'Kotlin caller should resolve its import to the Java class').toBeDefined();
  607. });
  608. it('disambiguates a class-name collision across packages', async () => {
  609. // Two `Bar` classes in different packages — each importer should reach
  610. // ITS Bar, not the other one. This is the central failure mode that
  611. // name-matcher alone cannot disambiguate.
  612. tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-jvm-imp-'));
  613. fs.writeFileSync(
  614. path.join(tmpDir, 'AlphaBar.kt'),
  615. 'package com.example.alpha\n\nclass Bar { fun who() = "alpha" }\n'
  616. );
  617. fs.writeFileSync(
  618. path.join(tmpDir, 'BetaBar.kt'),
  619. 'package com.example.beta\n\nclass Bar { fun who() = "beta" }\n'
  620. );
  621. fs.writeFileSync(
  622. path.join(tmpDir, 'CallerA.kt'),
  623. 'package app\n\nimport com.example.alpha.Bar\n\nfun a() { Bar().who() }\n'
  624. );
  625. fs.writeFileSync(
  626. path.join(tmpDir, 'CallerB.kt'),
  627. 'package app\n\nimport com.example.beta.Bar\n\nfun b() { Bar().who() }\n'
  628. );
  629. const cg = CodeGraph.initSync(tmpDir);
  630. await cg.indexAll();
  631. const alphaBar = cg.getNodesByKind('class').find((n) => n.qualifiedName === 'com.example.alpha::Bar');
  632. const betaBar = cg.getNodesByKind('class').find((n) => n.qualifiedName === 'com.example.beta::Bar');
  633. expect(alphaBar).toBeDefined();
  634. expect(betaBar).toBeDefined();
  635. expect(alphaBar!.id).not.toBe(betaBar!.id);
  636. // Each Bar receives exactly one imports edge — from its own caller.
  637. const alphaIncoming = cg.getIncomingEdges(alphaBar!.id).filter((e) => e.kind === 'imports');
  638. const betaIncoming = cg.getIncomingEdges(betaBar!.id).filter((e) => e.kind === 'imports');
  639. expect(alphaIncoming.length).toBeGreaterThan(0);
  640. expect(betaIncoming.length).toBeGreaterThan(0);
  641. // Sanity: the edges don't cross — alpha's incoming sources don't include
  642. // beta's filePath and vice versa.
  643. const sourceFiles = (edges: typeof alphaIncoming) =>
  644. edges.map((e) => cg.getNode(e.source)?.filePath).filter(Boolean);
  645. expect(sourceFiles(alphaIncoming).some((p) => p?.includes('CallerA.kt'))).toBe(true);
  646. expect(sourceFiles(betaIncoming).some((p) => p?.includes('CallerB.kt'))).toBe(true);
  647. });
  648. });
  649. describe('Java anonymous-class override synthesis — end-to-end', () => {
  650. let tmpDir: string | undefined;
  651. afterEach(() => {
  652. if (tmpDir) fs.rmSync(tmpDir, { recursive: true, force: true });
  653. tmpDir = undefined;
  654. });
  655. it('bridges an abstract base method to overrides inside `new Base() { ... }`', async () => {
  656. // Mirrors guava Splitter: a factory returns `new BaseIter() {
  657. // @Override int separatorStart(...) { ... } }`. Without anon-class
  658. // extraction the override is invisible — Phase 5.5 interface-impl
  659. // has no class to bridge — and an agent investigating `BaseIter.separatorStart`
  660. // can't see its real implementation without reading the file.
  661. tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-anon-java-'));
  662. fs.writeFileSync(
  663. path.join(tmpDir, 'Splitter.java'),
  664. 'package com.example;\n' +
  665. '\n' +
  666. 'abstract class BaseIter {\n' +
  667. ' abstract int separatorStart(int start);\n' +
  668. '}\n' +
  669. '\n' +
  670. 'public class Splitter {\n' +
  671. ' public BaseIter make() {\n' +
  672. ' return new BaseIter() {\n' +
  673. ' @Override\n' +
  674. ' int separatorStart(int start) { return start + 1; }\n' +
  675. ' };\n' +
  676. ' }\n' +
  677. '}\n'
  678. );
  679. const cg = CodeGraph.initSync(tmpDir);
  680. await cg.indexAll();
  681. // The anon class is extracted and contains the override.
  682. const anonClass = cg
  683. .getNodesByKind('class')
  684. .find((n) => /BaseIter\$anon@/.test(n.name));
  685. expect(anonClass, 'anonymous BaseIter subclass should be a class node').toBeDefined();
  686. const baseAbstract = cg
  687. .getNodesByKind('method')
  688. .find((n) => n.qualifiedName === 'com.example::BaseIter::separatorStart');
  689. const anonOverride = cg
  690. .getNodesByKind('method')
  691. .find(
  692. (n) =>
  693. n.name === 'separatorStart' &&
  694. n.qualifiedName.includes('$anon@') &&
  695. n.qualifiedName.startsWith('com.example::Splitter::make::')
  696. );
  697. expect(baseAbstract, 'base abstract method should be in the graph').toBeDefined();
  698. expect(anonOverride, 'anon-class override should be in the graph').toBeDefined();
  699. // Phase 5.5 interface-impl: the abstract method has a synthesized
  700. // `calls` edge to the anon override. Without this hop the agent
  701. // would have to Read the file to discover the implementation.
  702. const synthEdge = cg
  703. .getOutgoingEdges(baseAbstract!.id)
  704. .find((e) => e.target === anonOverride!.id && e.kind === 'calls');
  705. expect(synthEdge, 'BaseIter.separatorStart should bridge to anon.separatorStart').toBeDefined();
  706. expect(synthEdge!.provenance).toBe('heuristic');
  707. expect((synthEdge!.metadata as { synthesizedBy?: string } | undefined)?.synthesizedBy).toBe(
  708. 'interface-impl'
  709. );
  710. cg.close();
  711. });
  712. });
  713. describe('Go gRPC stub→impl synthesis', () => {
  714. let tmpDir: string | undefined;
  715. afterEach(() => {
  716. if (tmpDir) fs.rmSync(tmpDir, { recursive: true, force: true });
  717. tmpDir = undefined;
  718. });
  719. it('bridges UnimplementedMsgServer methods to the hand-written keeper impl', async () => {
  720. tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-go-grpc-'));
  721. // Mimic protoc-gen-go-grpc output: `*_grpc.pb.go` carrying the
  722. // UnimplementedMsgServer stub.
  723. fs.writeFileSync(
  724. path.join(tmpDir, 'tx_grpc.pb.go'),
  725. 'package banktypes\n\n' +
  726. 'type UnimplementedMsgServer struct{}\n\n' +
  727. 'func (UnimplementedMsgServer) Send(ctx context.Context, req *MsgSend) (*MsgSendResponse, error) { return nil, nil }\n' +
  728. 'func (UnimplementedMsgServer) MultiSend(ctx context.Context, req *MsgMultiSend) (*MsgMultiSendResponse, error) { return nil, nil }\n' +
  729. 'func (UnimplementedMsgServer) mustEmbedUnimplementedMsgServer() {}\n' +
  730. 'func (UnimplementedMsgServer) testEmbeddedByValue() {}\n'
  731. );
  732. // Hand-written impl in a non-generated file — what an agent actually
  733. // wants the trace to land on.
  734. fs.writeFileSync(
  735. path.join(tmpDir, 'msg_server.go'),
  736. 'package keeper\n\n' +
  737. 'type msgServer struct{ k Keeper }\n\n' +
  738. 'func (m msgServer) Send(ctx context.Context, req *MsgSend) (*MsgSendResponse, error) {\n' +
  739. ' return m.k.SendCoins(ctx, req.From, req.To, req.Amount)\n' +
  740. '}\n' +
  741. 'func (m msgServer) MultiSend(ctx context.Context, req *MsgMultiSend) (*MsgMultiSendResponse, error) {\n' +
  742. ' return nil, nil\n' +
  743. '}\n'
  744. );
  745. let cg: CodeGraph | undefined;
  746. try {
  747. cg = CodeGraph.initSync(tmpDir);
  748. await cg.indexAll();
  749. const stubSend = cg
  750. .getNodesByKind('method')
  751. .find((n) => n.qualifiedName.endsWith('UnimplementedMsgServer::Send'));
  752. const implSend = cg
  753. .getNodesByKind('method')
  754. .find((n) => n.qualifiedName.endsWith('msgServer::Send'));
  755. expect(stubSend, 'UnimplementedMsgServer.Send should be indexed').toBeDefined();
  756. expect(implSend, 'msgServer.Send should be indexed').toBeDefined();
  757. const bridge = cg
  758. .getOutgoingEdges(stubSend!.id)
  759. .find((e) => e.target === implSend!.id && e.kind === 'calls');
  760. expect(bridge, 'stub Send should bridge to impl Send').toBeDefined();
  761. expect(bridge!.provenance).toBe('heuristic');
  762. expect((bridge!.metadata as { synthesizedBy?: string } | undefined)?.synthesizedBy).toBe(
  763. 'go-grpc-stub-impl'
  764. );
  765. } finally {
  766. cg?.close();
  767. }
  768. });
  769. it('does not bridge to candidates living in another generated file', async () => {
  770. tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-go-grpc-sib-'));
  771. // `*_grpc.pb.go` also contains a sibling `msgClient` struct that
  772. // happens to satisfy the same method set. We must NOT bridge to it —
  773. // it's not the hand-written impl, just the gRPC client wrapper.
  774. fs.writeFileSync(
  775. path.join(tmpDir, 'tx_grpc.pb.go'),
  776. 'package banktypes\n\n' +
  777. 'type UnimplementedMsgServer struct{}\n' +
  778. 'func (UnimplementedMsgServer) Send() {}\n' +
  779. 'func (UnimplementedMsgServer) MultiSend() {}\n\n' +
  780. 'type msgClient struct{}\n' +
  781. 'func (m msgClient) Send() {}\n' +
  782. 'func (m msgClient) MultiSend() {}\n'
  783. );
  784. let cg: CodeGraph | undefined;
  785. try {
  786. cg = CodeGraph.initSync(tmpDir);
  787. await cg.indexAll();
  788. const stub = cg
  789. .getNodesByKind('struct')
  790. .find((n) => n.name === 'UnimplementedMsgServer');
  791. expect(stub).toBeDefined();
  792. const bridges = cg
  793. .getNodesByKind('method')
  794. .filter((n) => n.qualifiedName.endsWith('UnimplementedMsgServer::Send'))
  795. .flatMap((stubSend) => cg!.getOutgoingEdges(stubSend.id))
  796. .filter(
  797. (e) =>
  798. e.kind === 'calls' &&
  799. (e.metadata as { synthesizedBy?: string } | undefined)?.synthesizedBy ===
  800. 'go-grpc-stub-impl',
  801. );
  802. expect(bridges, 'no bridge to msgClient (also generated)').toHaveLength(0);
  803. } finally {
  804. cg?.close();
  805. }
  806. });
  807. });
  808. describe('React Router end-to-end route extraction (.tsx/.jsx)', () => {
  809. let tmpDir: string | undefined;
  810. afterEach(() => {
  811. if (tmpDir) fs.rmSync(tmpDir, { recursive: true, force: true });
  812. tmpDir = undefined;
  813. });
  814. // Regression for the resolver language-gate bug: the `react` resolver's
  815. // `extract()` was filtered out of the .tsx/.jsx grammars, so `<Route>` routes
  816. // — which only live in JSX files — were never indexed through the real
  817. // indexing path (the unit tests call extract() directly and so missed this).
  818. it('indexes <Route element={<X/>}> routes from a .tsx file and links them to the component', async () => {
  819. tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-rr-'));
  820. fs.writeFileSync(
  821. path.join(tmpDir, 'package.json'),
  822. '{"dependencies":{"react":"^18.0.0","react-router-dom":"^6.0.0"}}'
  823. );
  824. fs.writeFileSync(
  825. path.join(tmpDir, 'Home.tsx'),
  826. 'export function Home() { return null; }\n'
  827. );
  828. fs.writeFileSync(
  829. path.join(tmpDir, 'routes.tsx'),
  830. `import { Routes, Route } from 'react-router-dom';
  831. import { Home } from './Home';
  832. export function AppRoutes() {
  833. return (
  834. <Routes>
  835. <Route path="/home" element={<Home/>} />
  836. </Routes>
  837. );
  838. }
  839. `
  840. );
  841. const cg = CodeGraph.initSync(tmpDir);
  842. await cg.indexAll();
  843. try {
  844. // The route node from the .tsx file exists (the bug: it didn't).
  845. const route = cg.getNodesByKind('route').find((n) => n.name === '/home');
  846. expect(route, '/home route from .tsx should be indexed').toBeDefined();
  847. // ...and it links to the Home component.
  848. const home = cg.getNodesByName('Home').find((n) => n.kind === 'function');
  849. expect(home).toBeDefined();
  850. const toHome = cg.getOutgoingEdges(route!.id).find((e) => e.target === home!.id);
  851. expect(toHome, 'route → Home component edge').toBeDefined();
  852. } finally {
  853. cg.close();
  854. }
  855. });
  856. });