forked from colbymchenry/codegraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframeworks-integration.test.ts
More file actions
199 lines (172 loc) · 7.11 KB
/
Copy pathframeworks-integration.test.ts
File metadata and controls
199 lines (172 loc) · 7.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { describe, it, expect, beforeAll, afterEach } from 'vitest';
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import { CodeGraph } from '../src';
import { initGrammars, loadAllGrammars } from '../src/extraction/grammars';
beforeAll(async () => {
await initGrammars();
await loadAllGrammars();
});
describe('Django end-to-end framework extraction', () => {
let tmpDir: string | undefined;
afterEach(() => {
if (tmpDir) fs.rmSync(tmpDir, { recursive: true, force: true });
tmpDir = undefined;
});
it('creates a route->view edge from urls.py to view class', async () => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-django-'));
fs.writeFileSync(path.join(tmpDir, 'manage.py'), '# marker\n');
fs.writeFileSync(path.join(tmpDir, 'requirements.txt'), 'django==4.2\n');
fs.mkdirSync(path.join(tmpDir, 'users'));
fs.writeFileSync(path.join(tmpDir, 'users/__init__.py'), '');
fs.writeFileSync(
path.join(tmpDir, 'users/views.py'),
'class UserListView:\n def get(self, request): pass\n'
);
fs.writeFileSync(
path.join(tmpDir, 'users/urls.py'),
'from django.urls import path\n' +
'from users.views import UserListView\n' +
'urlpatterns = [path("users/", UserListView.as_view(), name="user-list")]\n'
);
const cg = CodeGraph.initSync(tmpDir);
await cg.indexAll();
// Route node exists
const routes = cg.getNodesByKind('route');
expect(routes.length).toBeGreaterThan(0);
const route = routes.find((n) => n.name === 'users/');
expect(route).toBeDefined();
// View class exists
const classNodes = cg.getNodesByKind('class');
const view = classNodes.find((n) => n.name === 'UserListView');
expect(view).toBeDefined();
// Edge route -> view exists
const edges = cg.getOutgoingEdges(route!.id);
const toView = edges.find((e) => e.target === view!.id);
expect(toView).toBeDefined();
expect(toView!.kind).toBe('references');
cg.close();
});
});
describe('Flask end-to-end framework extraction', () => {
let tmpDir: string | undefined;
afterEach(() => {
if (tmpDir) fs.rmSync(tmpDir, { recursive: true, force: true });
tmpDir = undefined;
});
it('resolves stacked routes across @login_required to a view named after a builtin (index)', async () => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-flask-'));
fs.writeFileSync(path.join(tmpDir, 'requirements.txt'), 'flask==3.0\n');
fs.writeFileSync(
path.join(tmpDir, 'app.py'),
'from flask import Blueprint, render_template\n' +
'from flask_login import login_required\n' +
'bp = Blueprint("main", __name__)\n' +
'\n' +
'@bp.route("/", methods=["GET", "POST"])\n' +
'@bp.route("/index", methods=["GET", "POST"])\n' +
'@login_required\n' +
'def index():\n' +
' return render_template("index.html")\n'
);
const cg = CodeGraph.initSync(tmpDir);
await cg.indexAll();
// Both stacked @bp.route decorators are extracted (the second was previously
// dropped because @login_required broke the "def must follow" assumption).
const routes = cg.getNodesByKind('route');
expect(routes.map((r) => r.name).sort()).toEqual(['GET /', 'GET /index']);
// The view function exists even though its name is a Python builtin method.
const fn = cg.getNodesByKind('function').find((n) => n.name === 'index');
expect(fn).toBeDefined();
// Both routes resolve to it — exercises the bare-name builtin guard, which
// previously filtered the `index` reference as a builtin method.
for (const route of routes) {
const edges = cg.getOutgoingEdges(route.id);
const toView = edges.find((e) => e.target === fn!.id && e.kind === 'references');
expect(toView, `route ${route.name} should resolve to index()`).toBeDefined();
}
cg.close();
});
});
describe('Flutter end-to-end — setState→build synthesis', () => {
let tmpDir: string | undefined;
afterEach(() => {
if (tmpDir) fs.rmSync(tmpDir, { recursive: true, force: true });
tmpDir = undefined;
});
it('synthesizes a handler→build edge when a State method calls setState', async () => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-flutter-'));
fs.writeFileSync(
path.join(tmpDir, 'main.dart'),
'import "package:flutter/material.dart";\n' +
'class CounterPage extends StatefulWidget {\n' +
' @override\n' +
' State<CounterPage> createState() => _CounterPageState();\n' +
'}\n' +
'class _CounterPageState extends State<CounterPage> {\n' +
' int _count = 0;\n' +
' void _increment() {\n' +
' setState(() {\n' +
' _count++;\n' +
' });\n' +
' }\n' +
' @override\n' +
' Widget build(BuildContext context) {\n' +
' return Text("$_count");\n' +
' }\n' +
'}\n'
);
const cg = CodeGraph.initSync(tmpDir);
await cg.indexAll();
const methods = cg.getNodesByKind('method');
const increment = methods.find((n) => n.name === '_increment');
const build = methods.find((n) => n.name === 'build');
expect(increment).toBeDefined();
expect(build).toBeDefined();
// setState re-runs build (Flutter-internal, no static edge). The synthesizer
// bridges the handler → build so the "tap → setState → rebuilt UI" flow connects.
const edges = cg.getOutgoingEdges(increment!.id);
const toBuild = edges.find((e) => e.target === build!.id && e.kind === 'calls');
expect(toBuild, '_increment should reach build via setState synthesis').toBeDefined();
cg.close();
});
});
describe('C++ end-to-end — virtual override synthesis', () => {
let tmpDir: string | undefined;
afterEach(() => {
if (tmpDir) fs.rmSync(tmpDir, { recursive: true, force: true });
tmpDir = undefined;
});
it('bridges a base virtual method to the subclass override', async () => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-cpp-'));
fs.writeFileSync(
path.join(tmpDir, 'iter.cpp'),
'class Iterator {\n' +
' public:\n' +
' virtual void Next() { }\n' +
'};\n' +
'class DBIter : public Iterator {\n' +
' public:\n' +
' void Next() override { advance(); }\n' +
' void advance() { }\n' +
'};\n'
);
const cg = CodeGraph.initSync(tmpDir);
await cg.indexAll();
// Two methods named Next: the base virtual (lower line) and the override.
const nexts = cg
.getNodesByKind('method')
.filter((n) => n.name === 'Next')
.sort((a, b) => a.startLine - b.startLine);
expect(nexts.length).toBe(2);
const [baseNext, overrideNext] = nexts;
// A vtable call to Iterator::Next dispatches to DBIter::Next — bridge it so
// trace/callees from the interface method reaches the implementation.
const edge = cg
.getOutgoingEdges(baseNext!.id)
.find((e) => e.target === overrideNext!.id && e.kind === 'calls');
expect(edge, 'Iterator::Next should reach DBIter::Next via override synthesis').toBeDefined();
cg.close();
});
});