-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathanalyzer.test.ts
More file actions
180 lines (148 loc) · 5.29 KB
/
analyzer.test.ts
File metadata and controls
180 lines (148 loc) · 5.29 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
import * as assert from "node:assert"
import { analyzeFile, analyzeTree } from "../../core/analyzer"
import { Parser } from "../../core/parser"
import { fixtures, nodeFileSystem, wasmBinaries } from "../testUtils"
suite("analyzer", () => {
let parser: Parser
const parse = (code: string) => {
const tree = parser.parse(code)
assert.ok(tree, "Failed to parse code")
return tree
}
suiteSetup(async () => {
parser = new Parser()
await parser.init(wasmBinaries)
})
suiteTeardown(() => {
parser.dispose()
})
suite("analyzeTree", () => {
test("extracts routes from decorated functions", () => {
const code = `
from fastapi import APIRouter
router = APIRouter()
@router.get("/")
def list_items():
pass
@router.post("/")
def create_item():
pass
`
const tree = parse(code)
const result = analyzeTree(tree, "/test/file.py")
assert.strictEqual(result.routes.length, 2)
assert.strictEqual(result.routes[0].method, "get")
assert.strictEqual(result.routes[0].path, "/")
assert.strictEqual(result.routes[1].method, "post")
})
test("extracts routers from assignments", () => {
const code = `
from fastapi import FastAPI, APIRouter
app = FastAPI()
router = APIRouter(prefix="/api")
`
const tree = parse(code)
const result = analyzeTree(tree, "/test/file.py")
assert.strictEqual(result.routers.length, 2)
assert.strictEqual(result.routers[0].variableName, "app")
assert.strictEqual(result.routers[0].type, "FastAPI")
assert.strictEqual(result.routers[1].variableName, "router")
assert.strictEqual(result.routers[1].type, "APIRouter")
assert.strictEqual(result.routers[1].prefix, "/api")
})
test("extracts include_router calls", () => {
const code = `
app.include_router(users.router, prefix="/users")
app.include_router(items.router, prefix="/items")
`
const tree = parse(code)
const result = analyzeTree(tree, "/test/file.py")
assert.strictEqual(result.includeRouters.length, 2)
assert.strictEqual(result.includeRouters[0].router, "users.router")
assert.strictEqual(result.includeRouters[0].prefix, "/users")
assert.strictEqual(result.includeRouters[1].router, "items.router")
assert.strictEqual(result.includeRouters[1].prefix, "/items")
})
test("extracts imports", () => {
const code = `
from fastapi import FastAPI
from .routes import users, items
import os
`
const tree = parse(code)
const result = analyzeTree(tree, "/test/file.py")
assert.strictEqual(result.imports.length, 3)
const fastapiImport = result.imports.find(
(i) => i.modulePath === "fastapi",
)
assert.ok(fastapiImport)
assert.deepStrictEqual(fastapiImport.names, ["FastAPI"])
const routesImport = result.imports.find((i) => i.modulePath === "routes")
assert.ok(routesImport)
assert.strictEqual(routesImport.isRelative, true)
})
test("sets filePath correctly", () => {
const code = "x = 1"
const tree = parse(code)
const result = analyzeTree(tree, "/custom/path.py")
assert.strictEqual(result.filePath, "/custom/path.py")
})
})
suite("analyzeFile", () => {
test("analyzes main.py fixture", async () => {
const result = await analyzeFile(
fixtures.standard.mainPy,
parser,
nodeFileSystem,
)
assert.ok(result)
assert.strictEqual(result.filePath, fixtures.standard.mainPy)
// Should find FastAPI app
const fastApiRouter = result.routers.find((r) => r.type === "FastAPI")
assert.ok(fastApiRouter)
assert.strictEqual(fastApiRouter.variableName, "app")
// Should find include_router calls
assert.ok(result.includeRouters.length > 0)
// Should find health check route
const healthRoute = result.routes.find((r) => r.path === "/health")
assert.ok(healthRoute)
assert.strictEqual(healthRoute.method, "get")
})
test("analyzes users.py fixture", async () => {
const result = await analyzeFile(
fixtures.standard.usersPy,
parser,
nodeFileSystem,
)
assert.ok(result)
// Should find APIRouter
const apiRouter = result.routers.find((r) => r.type === "APIRouter")
assert.ok(apiRouter)
// Should find routes (users.py has 3 routes: list, get, create)
assert.ok(result.routes.length >= 3)
// Check specific routes exist
const methods = result.routes.map((r) => r.method)
assert.ok(methods.includes("get"))
assert.ok(methods.includes("post"))
})
test("returns null when parser fails to parse", async () => {
const nullParser = { parse: () => null } as unknown as Parser
const mockFs = {
readFile: async () => new TextEncoder().encode("x = 1"),
exists: async () => true,
joinPath: (...parts: string[]) => parts.join("/"),
dirname: (p: string) => p.split("/").slice(0, -1).join("/"),
}
const result = await analyzeFile("file:///test.py", nullParser, mockFs)
assert.strictEqual(result, null)
})
test("returns null for non-existent file", async () => {
const result = await analyzeFile(
"file:///nonexistent/file.py",
parser,
nodeFileSystem,
)
assert.strictEqual(result, null)
})
})
})