frameworks-integration.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. });