-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathpathUtils.test.ts
More file actions
342 lines (298 loc) · 9.4 KB
/
pathUtils.test.ts
File metadata and controls
342 lines (298 loc) · 9.4 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import * as assert from "node:assert"
import {
countSegments,
findProjectRoot,
getPathSegments,
isWithinDirectory,
pathMatchesPathOperation,
stripLeadingDynamicSegments,
} from "../../core/pathUtils"
import { fixtures, nodeFileSystem } from "../testUtils"
const standardRoot = fixtures.standard.root
suite("pathUtils", () => {
suite("stripLeadingDynamicSegments", () => {
test("strips single dynamic segment", () => {
assert.strictEqual(
stripLeadingDynamicSegments("{settings.API_V1_STR}/users/{id}"),
"/users/{id}",
)
})
test("strips multiple dynamic segments", () => {
assert.strictEqual(
stripLeadingDynamicSegments("{BASE}{VERSION}/api/items"),
"/api/items",
)
})
test("leaves path parameters unchanged", () => {
assert.strictEqual(
stripLeadingDynamicSegments("/users/{id}/posts"),
"/users/{id}/posts",
)
})
test("returns / for only dynamic segment", () => {
assert.strictEqual(
stripLeadingDynamicSegments("{settings.API_V1_STR}"),
"/",
)
})
test("leaves static paths unchanged", () => {
assert.strictEqual(
stripLeadingDynamicSegments("/api/users"),
"/api/users",
)
})
test("handles empty string", () => {
assert.strictEqual(stripLeadingDynamicSegments(""), "/")
})
test("handles root path", () => {
assert.strictEqual(stripLeadingDynamicSegments("/"), "/")
})
})
suite("getPathSegments", () => {
test("gets first N segments", () => {
assert.strictEqual(
getPathSegments("/integrations/neon/foo", 2),
"/integrations/neon",
)
})
test("gets single segment", () => {
assert.strictEqual(getPathSegments("/users/123/posts", 1), "/users")
})
test("returns full path if count exceeds segments", () => {
assert.strictEqual(getPathSegments("/a/b/c", 5), "/a/b/c")
})
test("returns full path if count equals segments", () => {
assert.strictEqual(getPathSegments("/a/b/c", 3), "/a/b/c")
})
test("handles root path", () => {
assert.strictEqual(getPathSegments("/", 1), "/")
})
test("handles zero count", () => {
assert.strictEqual(getPathSegments("/users/posts", 0), "/")
})
})
suite("countSegments", () => {
test("counts multiple segments", () => {
assert.strictEqual(countSegments("/integrations/neon"), 2)
})
test("counts single segment", () => {
assert.strictEqual(countSegments("/users"), 1)
})
test("returns 0 for root path", () => {
assert.strictEqual(countSegments("/"), 0)
})
test("handles path without leading slash", () => {
assert.strictEqual(countSegments("users/posts"), 2)
})
test("handles empty string", () => {
assert.strictEqual(countSegments(""), 0)
})
test("ignores trailing slashes", () => {
assert.strictEqual(countSegments("/users/posts/"), 2)
})
})
suite("isWithinDirectory", () => {
test("returns true for path inside directory", () => {
assert.strictEqual(
isWithinDirectory("file:///foo/bar/baz", "file:///foo/bar"),
true,
)
})
test("returns true for path equal to directory", () => {
assert.strictEqual(
isWithinDirectory("file:///foo/bar", "file:///foo/bar"),
true,
)
})
test("returns false for path outside directory", () => {
assert.strictEqual(
isWithinDirectory("file:///foo/baz", "file:///foo/bar"),
false,
)
})
test("returns false for sibling with similar prefix", () => {
// This is the key test - /foo/ba is NOT a parent of /foo/bar
assert.strictEqual(
isWithinDirectory("file:///foo/bar", "file:///foo/ba"),
false,
)
})
test("returns false for parent directory", () => {
assert.strictEqual(
isWithinDirectory("file:///foo", "file:///foo/bar"),
false,
)
})
})
suite("findProjectRoot", () => {
test("returns entry dir when no __init__.py present", async () => {
// main.py is at fixtures/standard/main.py, and fixtures/standard has no __init__.py
const mainPyUri = nodeFileSystem.joinPath(standardRoot, "main.py")
const result = await findProjectRoot(
mainPyUri,
standardRoot,
nodeFileSystem,
)
assert.strictEqual(result, standardRoot)
})
test("walks up to find project root from nested package", async () => {
// users.py is in app/routes/users.py
// app has __init__.py, routes has __init__.py
// but fixtures/standard does not, so project root should be fixtures/standard
const usersUri = nodeFileSystem.joinPath(
standardRoot,
"app",
"routes",
"users.py",
)
const result = await findProjectRoot(
usersUri,
standardRoot,
nodeFileSystem,
)
assert.strictEqual(result, standardRoot)
})
test("returns workspace root when all dirs have __init__.py", async () => {
// If we pretend the workspace root is app, it should return that
const appRootUri = nodeFileSystem.joinPath(standardRoot, "app")
const usersUri = nodeFileSystem.joinPath(
standardRoot,
"app",
"routes",
"users.py",
)
const result = await findProjectRoot(usersUri, appRootUri, nodeFileSystem)
assert.strictEqual(result, appRootUri)
})
test("returns pyproject.toml dir for namespace packages in a monorepo", async () => {
// myapp/ has no __init__.py (namespace package), but service/ has pyproject.toml
const result = await findProjectRoot(
fixtures.monorepo.mainPy,
fixtures.monorepo.workspaceRoot,
nodeFileSystem,
)
assert.strictEqual(result, fixtures.monorepo.projectRoot)
})
})
suite("pathMatchesPathOperation", () => {
test("matches exact static paths", () => {
assert.strictEqual(pathMatchesPathOperation("/items", "/items"), true)
assert.strictEqual(
pathMatchesPathOperation("/api/users", "/api/users"),
true,
)
})
test("matches path with single parameter", () => {
assert.strictEqual(
pathMatchesPathOperation("/items/123", "/items/{item_id}"),
true,
)
assert.strictEqual(
pathMatchesPathOperation("/items/abc", "/items/{item_id}"),
true,
)
})
test("matches path with multiple parameters", () => {
assert.strictEqual(
pathMatchesPathOperation(
"/users/abc/posts/456",
"/users/{user_id}/posts/{post_id}",
),
true,
)
})
test("rejects when segment count differs", () => {
assert.strictEqual(
pathMatchesPathOperation("/items/123/details", "/items/{item_id}"),
false,
)
assert.strictEqual(
pathMatchesPathOperation("/items", "/items/{item_id}"),
false,
)
})
test("rejects when static segments differ", () => {
assert.strictEqual(
pathMatchesPathOperation("/users/123", "/items/{item_id}"),
false,
)
assert.strictEqual(
pathMatchesPathOperation("/api/v1/items", "/api/v2/items"),
false,
)
})
test("handles trailing slashes", () => {
assert.strictEqual(pathMatchesPathOperation("/items/", "/items"), true)
assert.strictEqual(pathMatchesPathOperation("/items", "/items/"), true)
assert.strictEqual(
pathMatchesPathOperation("/items/123/", "/items/{id}"),
true,
)
})
test("handles root path", () => {
assert.strictEqual(pathMatchesPathOperation("/", "/"), true)
assert.strictEqual(pathMatchesPathOperation("", ""), true)
})
test("rejects empty path against non-empty", () => {
assert.strictEqual(
pathMatchesPathOperation("/items/123", "/items/{item_id}"),
true,
)
assert.strictEqual(
pathMatchesPathOperation("/items/", "/items/{item_id}"),
false,
)
})
test("matches paths with dynamic prefix in path operation", () => {
assert.strictEqual(
pathMatchesPathOperation(
"/items/123",
"{settings.API_V1_STR}/items/{item_id}",
),
true,
)
assert.strictEqual(
pathMatchesPathOperation("/api/v2/users", "{BASE}/users"),
false,
)
assert.strictEqual(
pathMatchesPathOperation("/users", "{BASE}/users"),
true,
)
assert.strictEqual(
pathMatchesPathOperation("/items", "{BASE}/users"),
false,
)
})
test("matches paths with dynamic prefix in test path (f-strings)", () => {
assert.strictEqual(
pathMatchesPathOperation(
"{settings.API_V1_STR}/apps/{app.id}/environment-variables",
"/apps/{app_id}/environment-variables",
),
true,
)
assert.strictEqual(
pathMatchesPathOperation(
"{settings.API}/items/{item_id}",
"{BASE}/items/{id}",
),
true,
)
assert.strictEqual(
pathMatchesPathOperation("{BASE}/users/{id}", "/items/{item_id}"),
false,
)
})
test("strips query strings from test path", () => {
assert.strictEqual(
pathMatchesPathOperation(
"/teams/?owner=true&order_by=created_at",
"/teams",
),
true,
)
assert.strictEqual(pathMatchesPathOperation("/?page=1", "/"), true)
})
})
})