@mandujs/core 0.13.0 → 0.13.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.ko.md +4 -4
- package/README.md +653 -653
- package/package.json +1 -1
- package/src/bundler/build.ts +91 -91
- package/src/bundler/css.ts +302 -302
- package/src/client/Link.tsx +227 -227
- package/src/client/globals.ts +44 -44
- package/src/client/hooks.ts +267 -267
- package/src/client/index.ts +5 -5
- package/src/client/island.ts +8 -8
- package/src/client/router.ts +435 -435
- package/src/client/runtime.ts +23 -23
- package/src/client/serialize.ts +404 -404
- package/src/client/window-state.ts +101 -101
- package/src/config/mandu.ts +9 -0
- package/src/config/validate.ts +12 -0
- package/src/config/watcher.ts +311 -311
- package/src/constants.ts +40 -40
- package/src/content/content-layer.ts +314 -314
- package/src/content/content.test.ts +433 -433
- package/src/content/data-store.ts +245 -245
- package/src/content/digest.ts +133 -133
- package/src/content/index.ts +164 -164
- package/src/content/loader-context.ts +172 -172
- package/src/content/loaders/api.ts +216 -216
- package/src/content/loaders/file.ts +169 -169
- package/src/content/loaders/glob.ts +252 -252
- package/src/content/loaders/index.ts +34 -34
- package/src/content/loaders/types.ts +137 -137
- package/src/content/meta-store.ts +209 -209
- package/src/content/types.ts +282 -282
- package/src/content/watcher.ts +135 -135
- package/src/contract/client-safe.test.ts +42 -42
- package/src/contract/client-safe.ts +114 -114
- package/src/contract/client.ts +16 -16
- package/src/contract/define.ts +459 -459
- package/src/contract/handler.ts +10 -10
- package/src/contract/normalize.test.ts +276 -276
- package/src/contract/normalize.ts +404 -404
- package/src/contract/registry.test.ts +206 -206
- package/src/contract/registry.ts +568 -568
- package/src/contract/schema.ts +48 -48
- package/src/contract/types.ts +58 -58
- package/src/contract/validator.ts +32 -32
- package/src/devtools/ai/context-builder.ts +375 -375
- package/src/devtools/ai/index.ts +25 -25
- package/src/devtools/ai/mcp-connector.ts +465 -465
- package/src/devtools/client/catchers/error-catcher.ts +327 -327
- package/src/devtools/client/catchers/index.ts +18 -18
- package/src/devtools/client/catchers/network-proxy.ts +363 -363
- package/src/devtools/client/components/index.ts +39 -39
- package/src/devtools/client/components/kitchen-root.tsx +362 -362
- package/src/devtools/client/components/mandu-character.tsx +241 -241
- package/src/devtools/client/components/overlay.tsx +368 -368
- package/src/devtools/client/components/panel/errors-panel.tsx +259 -259
- package/src/devtools/client/components/panel/guard-panel.tsx +244 -244
- package/src/devtools/client/components/panel/index.ts +32 -32
- package/src/devtools/client/components/panel/islands-panel.tsx +304 -304
- package/src/devtools/client/components/panel/network-panel.tsx +292 -292
- package/src/devtools/client/components/panel/panel-container.tsx +259 -259
- package/src/devtools/client/filters/context-filters.ts +282 -282
- package/src/devtools/client/filters/index.ts +16 -16
- package/src/devtools/client/index.ts +63 -63
- package/src/devtools/client/persistence.ts +335 -335
- package/src/devtools/client/state-manager.ts +478 -478
- package/src/devtools/design-tokens.ts +263 -263
- package/src/devtools/hook/create-hook.ts +207 -207
- package/src/devtools/hook/index.ts +13 -13
- package/src/devtools/index.ts +439 -439
- package/src/devtools/init.ts +266 -266
- package/src/devtools/protocol.ts +237 -237
- package/src/devtools/server/index.ts +17 -17
- package/src/devtools/server/source-context.ts +444 -444
- package/src/devtools/types.ts +319 -319
- package/src/devtools/worker/index.ts +25 -25
- package/src/devtools/worker/redaction-worker.ts +222 -222
- package/src/devtools/worker/worker-manager.ts +409 -409
- package/src/error/domains.ts +265 -265
- package/src/error/result.ts +46 -46
- package/src/error/types.ts +6 -6
- package/src/errors/extractor.ts +409 -409
- package/src/errors/index.ts +19 -19
- package/src/filling/auth.ts +308 -308
- package/src/filling/context.ts +24 -1
- package/src/filling/deps.ts +238 -238
- package/src/filling/index.ts +2 -0
- package/src/filling/sse.test.ts +168 -0
- package/src/filling/sse.ts +162 -0
- package/src/generator/index.ts +3 -3
- package/src/guard/analyzer.ts +360 -360
- package/src/guard/ast-analyzer.ts +806 -806
- package/src/guard/contract-guard.ts +9 -9
- package/src/guard/file-type.test.ts +24 -24
- package/src/guard/presets/atomic.ts +70 -70
- package/src/guard/presets/clean.ts +77 -77
- package/src/guard/presets/fsd.ts +79 -79
- package/src/guard/presets/hexagonal.ts +68 -68
- package/src/guard/presets/index.ts +291 -291
- package/src/guard/reporter.ts +445 -445
- package/src/guard/rules.ts +12 -12
- package/src/guard/statistics.ts +578 -578
- package/src/guard/suggestions.ts +358 -358
- package/src/guard/types.ts +348 -348
- package/src/guard/validator.ts +834 -834
- package/src/guard/watcher.ts +404 -404
- package/src/index.ts +6 -1
- package/src/intent/index.ts +310 -310
- package/src/island/index.ts +304 -304
- package/src/logging/index.ts +22 -22
- package/src/logging/transports.ts +365 -365
- package/src/plugins/index.ts +38 -38
- package/src/plugins/registry.ts +377 -377
- package/src/plugins/types.ts +363 -363
- package/src/report/index.ts +1 -1
- package/src/router/fs-patterns.ts +387 -387
- package/src/router/fs-scanner.ts +497 -497
- package/src/runtime/boundary.tsx +232 -232
- package/src/runtime/compose.ts +222 -222
- package/src/runtime/escape.ts +44 -0
- package/src/runtime/lifecycle.ts +381 -381
- package/src/runtime/logger.test.ts +345 -345
- package/src/runtime/logger.ts +677 -677
- package/src/runtime/router.test.ts +476 -476
- package/src/runtime/router.ts +105 -105
- package/src/runtime/security.ts +155 -155
- package/src/runtime/server.ts +257 -0
- package/src/runtime/session-key.ts +328 -328
- package/src/runtime/ssr.ts +16 -21
- package/src/runtime/streaming-ssr.ts +24 -33
- package/src/runtime/trace.ts +144 -144
- package/src/seo/index.ts +214 -214
- package/src/seo/integration/ssr.ts +307 -307
- package/src/seo/render/basic.ts +427 -427
- package/src/seo/render/index.ts +143 -143
- package/src/seo/render/jsonld.ts +539 -539
- package/src/seo/render/opengraph.ts +191 -191
- package/src/seo/render/robots.ts +116 -116
- package/src/seo/render/sitemap.ts +137 -137
- package/src/seo/render/twitter.ts +126 -126
- package/src/seo/resolve/index.ts +353 -353
- package/src/seo/resolve/opengraph.ts +143 -143
- package/src/seo/resolve/robots.ts +73 -73
- package/src/seo/resolve/title.ts +94 -94
- package/src/seo/resolve/twitter.ts +73 -73
- package/src/seo/resolve/url.ts +97 -97
- package/src/seo/routes/index.ts +290 -290
- package/src/seo/types.ts +575 -575
- package/src/slot/validator.ts +39 -39
- package/src/spec/index.ts +3 -3
- package/src/spec/load.ts +76 -76
- package/src/spec/lock.ts +56 -56
- package/src/utils/bun.ts +8 -8
- package/src/utils/lru-cache.ts +75 -75
- package/src/utils/safe-io.ts +188 -188
- package/src/utils/string-safe.ts +298 -298
|
@@ -1,476 +1,476 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Router v5 Tests
|
|
3
|
-
*
|
|
4
|
-
* Test cases:
|
|
5
|
-
* 1. Static vs Dynamic Priority
|
|
6
|
-
* 2. Parameter Matching
|
|
7
|
-
* 3. Wildcard Matching
|
|
8
|
-
* 4. Security (URI encoding)
|
|
9
|
-
* 5. Validation Errors
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
import { describe, test, expect } from "bun:test";
|
|
13
|
-
import {
|
|
14
|
-
Router,
|
|
15
|
-
RouterError,
|
|
16
|
-
createRouter,
|
|
17
|
-
WILDCARD_PARAM_KEY,
|
|
18
|
-
} from "./router";
|
|
19
|
-
import type { RouteSpec } from "../spec/schema";
|
|
20
|
-
|
|
21
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
22
|
-
// Test Fixtures
|
|
23
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
24
|
-
|
|
25
|
-
function makeRoute(id: string, pattern: string, kind: "page" | "api" = "api"): RouteSpec {
|
|
26
|
-
return {
|
|
27
|
-
id,
|
|
28
|
-
pattern,
|
|
29
|
-
kind,
|
|
30
|
-
module: `generated/${id}.route.ts`,
|
|
31
|
-
...(kind === "page" ? { componentModule: `generated/${id}.route.tsx` } : {}),
|
|
32
|
-
} as RouteSpec;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
36
|
-
// 1. Static vs Dynamic Priority
|
|
37
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
38
|
-
|
|
39
|
-
describe("Static vs Dynamic Priority", () => {
|
|
40
|
-
test("static route takes precedence over param route", () => {
|
|
41
|
-
const router = createRouter([
|
|
42
|
-
makeRoute("todos-item", "/api/todos/:id"),
|
|
43
|
-
makeRoute("todos-stats", "/api/todos/stats"),
|
|
44
|
-
]);
|
|
45
|
-
|
|
46
|
-
const result = router.match("/api/todos/stats");
|
|
47
|
-
|
|
48
|
-
expect(result).not.toBeNull();
|
|
49
|
-
expect(result!.route.id).toBe("todos-stats");
|
|
50
|
-
expect(result!.params).toEqual({});
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
test("static route precedence regardless of registration order", () => {
|
|
54
|
-
// Register static AFTER dynamic
|
|
55
|
-
const router = createRouter([
|
|
56
|
-
makeRoute("users-item", "/users/:id"),
|
|
57
|
-
makeRoute("users-me", "/users/me"),
|
|
58
|
-
]);
|
|
59
|
-
|
|
60
|
-
expect(router.match("/users/me")!.route.id).toBe("users-me");
|
|
61
|
-
expect(router.match("/users/123")!.route.id).toBe("users-item");
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
test("root path matching", () => {
|
|
65
|
-
const router = createRouter([
|
|
66
|
-
makeRoute("home", "/", "page"),
|
|
67
|
-
makeRoute("api", "/api"),
|
|
68
|
-
]);
|
|
69
|
-
|
|
70
|
-
expect(router.match("/")!.route.id).toBe("home");
|
|
71
|
-
expect(router.match("/api")!.route.id).toBe("api");
|
|
72
|
-
});
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
76
|
-
// 2. Parameter Matching
|
|
77
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
78
|
-
|
|
79
|
-
describe("Parameter Matching", () => {
|
|
80
|
-
test("extracts single param correctly", () => {
|
|
81
|
-
const router = createRouter([
|
|
82
|
-
makeRoute("todos-item", "/api/todos/:id"),
|
|
83
|
-
]);
|
|
84
|
-
|
|
85
|
-
const result = router.match("/api/todos/123");
|
|
86
|
-
|
|
87
|
-
expect(result).not.toBeNull();
|
|
88
|
-
expect(result!.route.id).toBe("todos-item");
|
|
89
|
-
expect(result!.params).toEqual({ id: "123" });
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
test("extracts multiple params correctly", () => {
|
|
93
|
-
const router = createRouter([
|
|
94
|
-
makeRoute("user-post", "/users/:userId/posts/:postId"),
|
|
95
|
-
]);
|
|
96
|
-
|
|
97
|
-
const result = router.match("/users/42/posts/99");
|
|
98
|
-
|
|
99
|
-
expect(result).not.toBeNull();
|
|
100
|
-
expect(result!.params).toEqual({ userId: "42", postId: "99" });
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
test("decodes UTF-8 encoded params", () => {
|
|
104
|
-
const router = createRouter([
|
|
105
|
-
makeRoute("user", "/user/:name"),
|
|
106
|
-
]);
|
|
107
|
-
|
|
108
|
-
// café encoded as caf%C3%A9
|
|
109
|
-
const result = router.match("/user/caf%C3%A9");
|
|
110
|
-
|
|
111
|
-
expect(result).not.toBeNull();
|
|
112
|
-
expect(result!.params).toEqual({ name: "café" });
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
test("handles non-ASCII static routes", () => {
|
|
116
|
-
const router = createRouter([
|
|
117
|
-
makeRoute("cafe", "/café", "page"),
|
|
118
|
-
]);
|
|
119
|
-
|
|
120
|
-
expect(router.match("/café")!.route.id).toBe("cafe");
|
|
121
|
-
});
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
125
|
-
// 3. Wildcard Matching
|
|
126
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
127
|
-
|
|
128
|
-
describe("Wildcard Matching", () => {
|
|
129
|
-
test("matches wildcard with remaining path", () => {
|
|
130
|
-
const router = createRouter([
|
|
131
|
-
makeRoute("files", "/files/*"),
|
|
132
|
-
]);
|
|
133
|
-
|
|
134
|
-
const result = router.match("/files/a/b/c");
|
|
135
|
-
|
|
136
|
-
expect(result).not.toBeNull();
|
|
137
|
-
expect(result!.route.id).toBe("files");
|
|
138
|
-
expect(result!.params).toEqual({ [WILDCARD_PARAM_KEY]: "a/b/c" });
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
test("matches named wildcard with remaining path", () => {
|
|
142
|
-
const router = createRouter([
|
|
143
|
-
makeRoute("docs", "/docs/:path*"),
|
|
144
|
-
]);
|
|
145
|
-
|
|
146
|
-
const result = router.match("/docs/a/b/c");
|
|
147
|
-
|
|
148
|
-
expect(result).not.toBeNull();
|
|
149
|
-
expect(result!.route.id).toBe("docs");
|
|
150
|
-
expect(result!.params).toEqual({ path: "a/b/c" });
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
test("optional wildcard matches base path without param", () => {
|
|
154
|
-
const router = createRouter([
|
|
155
|
-
makeRoute("docs", "/docs/:path*?"),
|
|
156
|
-
]);
|
|
157
|
-
|
|
158
|
-
const result = router.match("/docs");
|
|
159
|
-
|
|
160
|
-
expect(result).not.toBeNull();
|
|
161
|
-
expect(result!.route.id).toBe("docs");
|
|
162
|
-
expect(result!.params).toEqual({});
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
test("optional wildcard matches with remaining path", () => {
|
|
166
|
-
const router = createRouter([
|
|
167
|
-
makeRoute("docs", "/docs/:path*?"),
|
|
168
|
-
]);
|
|
169
|
-
|
|
170
|
-
const result = router.match("/docs/intro");
|
|
171
|
-
|
|
172
|
-
expect(result).not.toBeNull();
|
|
173
|
-
expect(result!.params).toEqual({ path: "intro" });
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
test("wildcard with single segment", () => {
|
|
177
|
-
const router = createRouter([
|
|
178
|
-
makeRoute("docs", "/docs/*"),
|
|
179
|
-
]);
|
|
180
|
-
|
|
181
|
-
const result = router.match("/docs/readme");
|
|
182
|
-
|
|
183
|
-
expect(result!.params).toEqual({ [WILDCARD_PARAM_KEY]: "readme" });
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
test("Policy A: wildcard does NOT match base path", () => {
|
|
187
|
-
const router = createRouter([
|
|
188
|
-
makeRoute("files", "/files/*"),
|
|
189
|
-
]);
|
|
190
|
-
|
|
191
|
-
// /files/* should NOT match /files
|
|
192
|
-
expect(router.match("/files")).toBeNull();
|
|
193
|
-
expect(router.match("/files/")).toBeNull(); // normalized to /files
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
test("static route takes precedence over wildcard", () => {
|
|
197
|
-
const router = createRouter([
|
|
198
|
-
makeRoute("files-wildcard", "/files/*"),
|
|
199
|
-
makeRoute("files-readme", "/files/readme"),
|
|
200
|
-
]);
|
|
201
|
-
|
|
202
|
-
expect(router.match("/files/readme")!.route.id).toBe("files-readme");
|
|
203
|
-
expect(router.match("/files/other")!.route.id).toBe("files-wildcard");
|
|
204
|
-
});
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
208
|
-
// 4. Security (URI Encoding)
|
|
209
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
210
|
-
|
|
211
|
-
describe("Security", () => {
|
|
212
|
-
test("blocks %2F (encoded slash) in path segments", () => {
|
|
213
|
-
const router = createRouter([
|
|
214
|
-
makeRoute("user", "/user/:name"),
|
|
215
|
-
]);
|
|
216
|
-
|
|
217
|
-
// a%2Fb = a/b encoded
|
|
218
|
-
const result = router.match("/user/a%2Fb");
|
|
219
|
-
|
|
220
|
-
expect(result).toBeNull();
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
test("blocks double-encoded slash (%252F)", () => {
|
|
224
|
-
const router = createRouter([
|
|
225
|
-
makeRoute("user", "/user/:name"),
|
|
226
|
-
]);
|
|
227
|
-
|
|
228
|
-
// %252F decodes to %2F
|
|
229
|
-
const result = router.match("/user/%252F");
|
|
230
|
-
|
|
231
|
-
expect(result).toBeNull();
|
|
232
|
-
});
|
|
233
|
-
|
|
234
|
-
test("blocks malformed UTF-8 encoding", () => {
|
|
235
|
-
const router = createRouter([
|
|
236
|
-
makeRoute("user", "/user/:name"),
|
|
237
|
-
]);
|
|
238
|
-
|
|
239
|
-
// Invalid UTF-8 sequence
|
|
240
|
-
const result = router.match("/user/%C0%AE");
|
|
241
|
-
|
|
242
|
-
expect(result).toBeNull();
|
|
243
|
-
});
|
|
244
|
-
|
|
245
|
-
test("allows valid percent-encoded characters", () => {
|
|
246
|
-
const router = createRouter([
|
|
247
|
-
makeRoute("search", "/search/:query"),
|
|
248
|
-
]);
|
|
249
|
-
|
|
250
|
-
// hello%20world = "hello world"
|
|
251
|
-
const result = router.match("/search/hello%20world");
|
|
252
|
-
|
|
253
|
-
expect(result).not.toBeNull();
|
|
254
|
-
expect(result!.params).toEqual({ query: "hello world" });
|
|
255
|
-
});
|
|
256
|
-
});
|
|
257
|
-
|
|
258
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
259
|
-
// 5. Validation Errors
|
|
260
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
261
|
-
|
|
262
|
-
describe("Validation Errors", () => {
|
|
263
|
-
test("throws DUPLICATE_PATTERN for same pattern", () => {
|
|
264
|
-
expect(() => {
|
|
265
|
-
createRouter([
|
|
266
|
-
makeRoute("route1", "/api/users"),
|
|
267
|
-
makeRoute("route2", "/api/users"),
|
|
268
|
-
]);
|
|
269
|
-
}).toThrow(RouterError);
|
|
270
|
-
|
|
271
|
-
try {
|
|
272
|
-
createRouter([
|
|
273
|
-
makeRoute("route1", "/api/users"),
|
|
274
|
-
makeRoute("route2", "/api/users"),
|
|
275
|
-
]);
|
|
276
|
-
} catch (e) {
|
|
277
|
-
expect(e).toBeInstanceOf(RouterError);
|
|
278
|
-
expect((e as RouterError).code).toBe("DUPLICATE_PATTERN");
|
|
279
|
-
expect((e as RouterError).routeId).toBe("route2");
|
|
280
|
-
expect((e as RouterError).conflictsWith).toBe("route1");
|
|
281
|
-
}
|
|
282
|
-
});
|
|
283
|
-
|
|
284
|
-
test("throws DUPLICATE_PATTERN for normalized duplicates (trailing slash)", () => {
|
|
285
|
-
expect(() => {
|
|
286
|
-
createRouter([
|
|
287
|
-
makeRoute("route1", "/api/users"),
|
|
288
|
-
makeRoute("route2", "/api/users/"),
|
|
289
|
-
]);
|
|
290
|
-
}).toThrow(RouterError);
|
|
291
|
-
|
|
292
|
-
try {
|
|
293
|
-
createRouter([
|
|
294
|
-
makeRoute("route1", "/api/users"),
|
|
295
|
-
makeRoute("route2", "/api/users/"),
|
|
296
|
-
]);
|
|
297
|
-
} catch (e) {
|
|
298
|
-
expect((e as RouterError).code).toBe("DUPLICATE_PATTERN");
|
|
299
|
-
}
|
|
300
|
-
});
|
|
301
|
-
|
|
302
|
-
test("throws PARAM_NAME_CONFLICT for same-depth param mismatch", () => {
|
|
303
|
-
expect(() => {
|
|
304
|
-
createRouter([
|
|
305
|
-
makeRoute("users", "/users/:id"),
|
|
306
|
-
makeRoute("users-by-name", "/users/:name"),
|
|
307
|
-
]);
|
|
308
|
-
}).toThrow(RouterError);
|
|
309
|
-
|
|
310
|
-
try {
|
|
311
|
-
createRouter([
|
|
312
|
-
makeRoute("users", "/users/:id"),
|
|
313
|
-
makeRoute("users-by-name", "/users/:name"),
|
|
314
|
-
]);
|
|
315
|
-
} catch (e) {
|
|
316
|
-
expect((e as RouterError).code).toBe("PARAM_NAME_CONFLICT");
|
|
317
|
-
}
|
|
318
|
-
});
|
|
319
|
-
|
|
320
|
-
test("allows same param name across different paths", () => {
|
|
321
|
-
// These should NOT conflict - different parent paths
|
|
322
|
-
const router = createRouter([
|
|
323
|
-
makeRoute("users", "/users/:id"),
|
|
324
|
-
makeRoute("posts", "/posts/:id"),
|
|
325
|
-
]);
|
|
326
|
-
|
|
327
|
-
expect(router.match("/users/1")!.params).toEqual({ id: "1" });
|
|
328
|
-
expect(router.match("/posts/2")!.params).toEqual({ id: "2" });
|
|
329
|
-
});
|
|
330
|
-
|
|
331
|
-
test("throws WILDCARD_NOT_LAST for non-terminal wildcard", () => {
|
|
332
|
-
expect(() => {
|
|
333
|
-
createRouter([
|
|
334
|
-
makeRoute("invalid", "/files/*/more"),
|
|
335
|
-
]);
|
|
336
|
-
}).toThrow(RouterError);
|
|
337
|
-
|
|
338
|
-
try {
|
|
339
|
-
createRouter([
|
|
340
|
-
makeRoute("invalid", "/files/*/more"),
|
|
341
|
-
]);
|
|
342
|
-
} catch (e) {
|
|
343
|
-
expect((e as RouterError).code).toBe("WILDCARD_NOT_LAST");
|
|
344
|
-
}
|
|
345
|
-
});
|
|
346
|
-
|
|
347
|
-
test("throws ROUTE_CONFLICT for wildcard conflicts", () => {
|
|
348
|
-
expect(() => {
|
|
349
|
-
createRouter([
|
|
350
|
-
makeRoute("files-legacy", "/files/*"),
|
|
351
|
-
makeRoute("files-named", "/files/:path*"),
|
|
352
|
-
]);
|
|
353
|
-
}).toThrow(RouterError);
|
|
354
|
-
|
|
355
|
-
try {
|
|
356
|
-
createRouter([
|
|
357
|
-
makeRoute("files-legacy", "/files/*"),
|
|
358
|
-
makeRoute("files-named", "/files/:path*"),
|
|
359
|
-
]);
|
|
360
|
-
} catch (e) {
|
|
361
|
-
expect((e as RouterError).code).toBe("ROUTE_CONFLICT");
|
|
362
|
-
}
|
|
363
|
-
});
|
|
364
|
-
});
|
|
365
|
-
|
|
366
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
367
|
-
// 6. Router API
|
|
368
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
369
|
-
|
|
370
|
-
describe("Router API", () => {
|
|
371
|
-
test("getStats returns correct counts", () => {
|
|
372
|
-
const router = createRouter([
|
|
373
|
-
makeRoute("home", "/"),
|
|
374
|
-
makeRoute("health", "/api/health"),
|
|
375
|
-
makeRoute("todos-item", "/api/todos/:id"),
|
|
376
|
-
makeRoute("files", "/files/*"),
|
|
377
|
-
]);
|
|
378
|
-
|
|
379
|
-
const stats = router.getStats();
|
|
380
|
-
|
|
381
|
-
expect(stats.staticCount).toBe(2); // / and /api/health
|
|
382
|
-
expect(stats.dynamicCount).toBe(2); // /api/todos/:id and /files/*
|
|
383
|
-
expect(stats.totalRoutes).toBe(4);
|
|
384
|
-
});
|
|
385
|
-
|
|
386
|
-
test("getRoutes returns all registered routes", () => {
|
|
387
|
-
const routes = [
|
|
388
|
-
makeRoute("home", "/"),
|
|
389
|
-
makeRoute("users", "/users/:id"),
|
|
390
|
-
];
|
|
391
|
-
const router = createRouter(routes);
|
|
392
|
-
|
|
393
|
-
const retrieved = router.getRoutes();
|
|
394
|
-
|
|
395
|
-
expect(retrieved.length).toBe(2);
|
|
396
|
-
expect(retrieved.map((r) => r.id).sort()).toEqual(["home", "users"]);
|
|
397
|
-
});
|
|
398
|
-
|
|
399
|
-
test("addRoute adds route to existing router", () => {
|
|
400
|
-
const router = createRouter([
|
|
401
|
-
makeRoute("home", "/"),
|
|
402
|
-
]);
|
|
403
|
-
|
|
404
|
-
router.addRoute(makeRoute("about", "/about"));
|
|
405
|
-
|
|
406
|
-
expect(router.match("/about")).not.toBeNull();
|
|
407
|
-
expect(router.getStats().totalRoutes).toBe(2);
|
|
408
|
-
});
|
|
409
|
-
|
|
410
|
-
test("addRoute validates against existing routes", () => {
|
|
411
|
-
const router = createRouter([
|
|
412
|
-
makeRoute("home", "/"),
|
|
413
|
-
]);
|
|
414
|
-
|
|
415
|
-
expect(() => {
|
|
416
|
-
router.addRoute(makeRoute("home2", "/"));
|
|
417
|
-
}).toThrow(RouterError);
|
|
418
|
-
});
|
|
419
|
-
});
|
|
420
|
-
|
|
421
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
422
|
-
// 7. Edge Cases
|
|
423
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
424
|
-
|
|
425
|
-
describe("Edge Cases", () => {
|
|
426
|
-
test("empty routes", () => {
|
|
427
|
-
const router = createRouter([]);
|
|
428
|
-
|
|
429
|
-
expect(router.match("/")).toBeNull();
|
|
430
|
-
expect(router.getStats().totalRoutes).toBe(0);
|
|
431
|
-
});
|
|
432
|
-
|
|
433
|
-
test("deep nested paths", () => {
|
|
434
|
-
const router = createRouter([
|
|
435
|
-
makeRoute("deep", "/a/b/c/d/e/:id"),
|
|
436
|
-
]);
|
|
437
|
-
|
|
438
|
-
const result = router.match("/a/b/c/d/e/123");
|
|
439
|
-
|
|
440
|
-
expect(result!.params).toEqual({ id: "123" });
|
|
441
|
-
});
|
|
442
|
-
|
|
443
|
-
test("consecutive params", () => {
|
|
444
|
-
const router = createRouter([
|
|
445
|
-
makeRoute("date", "/calendar/:year/:month/:day"),
|
|
446
|
-
]);
|
|
447
|
-
|
|
448
|
-
const result = router.match("/calendar/2025/01/30");
|
|
449
|
-
|
|
450
|
-
expect(result!.params).toEqual({
|
|
451
|
-
year: "2025",
|
|
452
|
-
month: "01",
|
|
453
|
-
day: "30",
|
|
454
|
-
});
|
|
455
|
-
});
|
|
456
|
-
|
|
457
|
-
test("param followed by static", () => {
|
|
458
|
-
const router = createRouter([
|
|
459
|
-
makeRoute("user-posts", "/users/:id/posts"),
|
|
460
|
-
]);
|
|
461
|
-
|
|
462
|
-
const result = router.match("/users/42/posts");
|
|
463
|
-
|
|
464
|
-
expect(result!.route.id).toBe("user-posts");
|
|
465
|
-
expect(result!.params).toEqual({ id: "42" });
|
|
466
|
-
});
|
|
467
|
-
|
|
468
|
-
test("trailing slash normalization", () => {
|
|
469
|
-
const router = createRouter([
|
|
470
|
-
makeRoute("api", "/api"),
|
|
471
|
-
]);
|
|
472
|
-
|
|
473
|
-
expect(router.match("/api")).not.toBeNull();
|
|
474
|
-
expect(router.match("/api/")).not.toBeNull(); // normalized to /api
|
|
475
|
-
});
|
|
476
|
-
});
|
|
1
|
+
/**
|
|
2
|
+
* Router v5 Tests
|
|
3
|
+
*
|
|
4
|
+
* Test cases:
|
|
5
|
+
* 1. Static vs Dynamic Priority
|
|
6
|
+
* 2. Parameter Matching
|
|
7
|
+
* 3. Wildcard Matching
|
|
8
|
+
* 4. Security (URI encoding)
|
|
9
|
+
* 5. Validation Errors
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { describe, test, expect } from "bun:test";
|
|
13
|
+
import {
|
|
14
|
+
Router,
|
|
15
|
+
RouterError,
|
|
16
|
+
createRouter,
|
|
17
|
+
WILDCARD_PARAM_KEY,
|
|
18
|
+
} from "./router";
|
|
19
|
+
import type { RouteSpec } from "../spec/schema";
|
|
20
|
+
|
|
21
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
22
|
+
// Test Fixtures
|
|
23
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
24
|
+
|
|
25
|
+
function makeRoute(id: string, pattern: string, kind: "page" | "api" = "api"): RouteSpec {
|
|
26
|
+
return {
|
|
27
|
+
id,
|
|
28
|
+
pattern,
|
|
29
|
+
kind,
|
|
30
|
+
module: `generated/${id}.route.ts`,
|
|
31
|
+
...(kind === "page" ? { componentModule: `generated/${id}.route.tsx` } : {}),
|
|
32
|
+
} as RouteSpec;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
36
|
+
// 1. Static vs Dynamic Priority
|
|
37
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
38
|
+
|
|
39
|
+
describe("Static vs Dynamic Priority", () => {
|
|
40
|
+
test("static route takes precedence over param route", () => {
|
|
41
|
+
const router = createRouter([
|
|
42
|
+
makeRoute("todos-item", "/api/todos/:id"),
|
|
43
|
+
makeRoute("todos-stats", "/api/todos/stats"),
|
|
44
|
+
]);
|
|
45
|
+
|
|
46
|
+
const result = router.match("/api/todos/stats");
|
|
47
|
+
|
|
48
|
+
expect(result).not.toBeNull();
|
|
49
|
+
expect(result!.route.id).toBe("todos-stats");
|
|
50
|
+
expect(result!.params).toEqual({});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("static route precedence regardless of registration order", () => {
|
|
54
|
+
// Register static AFTER dynamic
|
|
55
|
+
const router = createRouter([
|
|
56
|
+
makeRoute("users-item", "/users/:id"),
|
|
57
|
+
makeRoute("users-me", "/users/me"),
|
|
58
|
+
]);
|
|
59
|
+
|
|
60
|
+
expect(router.match("/users/me")!.route.id).toBe("users-me");
|
|
61
|
+
expect(router.match("/users/123")!.route.id).toBe("users-item");
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test("root path matching", () => {
|
|
65
|
+
const router = createRouter([
|
|
66
|
+
makeRoute("home", "/", "page"),
|
|
67
|
+
makeRoute("api", "/api"),
|
|
68
|
+
]);
|
|
69
|
+
|
|
70
|
+
expect(router.match("/")!.route.id).toBe("home");
|
|
71
|
+
expect(router.match("/api")!.route.id).toBe("api");
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
76
|
+
// 2. Parameter Matching
|
|
77
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
78
|
+
|
|
79
|
+
describe("Parameter Matching", () => {
|
|
80
|
+
test("extracts single param correctly", () => {
|
|
81
|
+
const router = createRouter([
|
|
82
|
+
makeRoute("todos-item", "/api/todos/:id"),
|
|
83
|
+
]);
|
|
84
|
+
|
|
85
|
+
const result = router.match("/api/todos/123");
|
|
86
|
+
|
|
87
|
+
expect(result).not.toBeNull();
|
|
88
|
+
expect(result!.route.id).toBe("todos-item");
|
|
89
|
+
expect(result!.params).toEqual({ id: "123" });
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test("extracts multiple params correctly", () => {
|
|
93
|
+
const router = createRouter([
|
|
94
|
+
makeRoute("user-post", "/users/:userId/posts/:postId"),
|
|
95
|
+
]);
|
|
96
|
+
|
|
97
|
+
const result = router.match("/users/42/posts/99");
|
|
98
|
+
|
|
99
|
+
expect(result).not.toBeNull();
|
|
100
|
+
expect(result!.params).toEqual({ userId: "42", postId: "99" });
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test("decodes UTF-8 encoded params", () => {
|
|
104
|
+
const router = createRouter([
|
|
105
|
+
makeRoute("user", "/user/:name"),
|
|
106
|
+
]);
|
|
107
|
+
|
|
108
|
+
// café encoded as caf%C3%A9
|
|
109
|
+
const result = router.match("/user/caf%C3%A9");
|
|
110
|
+
|
|
111
|
+
expect(result).not.toBeNull();
|
|
112
|
+
expect(result!.params).toEqual({ name: "café" });
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test("handles non-ASCII static routes", () => {
|
|
116
|
+
const router = createRouter([
|
|
117
|
+
makeRoute("cafe", "/café", "page"),
|
|
118
|
+
]);
|
|
119
|
+
|
|
120
|
+
expect(router.match("/café")!.route.id).toBe("cafe");
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
125
|
+
// 3. Wildcard Matching
|
|
126
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
127
|
+
|
|
128
|
+
describe("Wildcard Matching", () => {
|
|
129
|
+
test("matches wildcard with remaining path", () => {
|
|
130
|
+
const router = createRouter([
|
|
131
|
+
makeRoute("files", "/files/*"),
|
|
132
|
+
]);
|
|
133
|
+
|
|
134
|
+
const result = router.match("/files/a/b/c");
|
|
135
|
+
|
|
136
|
+
expect(result).not.toBeNull();
|
|
137
|
+
expect(result!.route.id).toBe("files");
|
|
138
|
+
expect(result!.params).toEqual({ [WILDCARD_PARAM_KEY]: "a/b/c" });
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test("matches named wildcard with remaining path", () => {
|
|
142
|
+
const router = createRouter([
|
|
143
|
+
makeRoute("docs", "/docs/:path*"),
|
|
144
|
+
]);
|
|
145
|
+
|
|
146
|
+
const result = router.match("/docs/a/b/c");
|
|
147
|
+
|
|
148
|
+
expect(result).not.toBeNull();
|
|
149
|
+
expect(result!.route.id).toBe("docs");
|
|
150
|
+
expect(result!.params).toEqual({ path: "a/b/c" });
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test("optional wildcard matches base path without param", () => {
|
|
154
|
+
const router = createRouter([
|
|
155
|
+
makeRoute("docs", "/docs/:path*?"),
|
|
156
|
+
]);
|
|
157
|
+
|
|
158
|
+
const result = router.match("/docs");
|
|
159
|
+
|
|
160
|
+
expect(result).not.toBeNull();
|
|
161
|
+
expect(result!.route.id).toBe("docs");
|
|
162
|
+
expect(result!.params).toEqual({});
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
test("optional wildcard matches with remaining path", () => {
|
|
166
|
+
const router = createRouter([
|
|
167
|
+
makeRoute("docs", "/docs/:path*?"),
|
|
168
|
+
]);
|
|
169
|
+
|
|
170
|
+
const result = router.match("/docs/intro");
|
|
171
|
+
|
|
172
|
+
expect(result).not.toBeNull();
|
|
173
|
+
expect(result!.params).toEqual({ path: "intro" });
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
test("wildcard with single segment", () => {
|
|
177
|
+
const router = createRouter([
|
|
178
|
+
makeRoute("docs", "/docs/*"),
|
|
179
|
+
]);
|
|
180
|
+
|
|
181
|
+
const result = router.match("/docs/readme");
|
|
182
|
+
|
|
183
|
+
expect(result!.params).toEqual({ [WILDCARD_PARAM_KEY]: "readme" });
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
test("Policy A: wildcard does NOT match base path", () => {
|
|
187
|
+
const router = createRouter([
|
|
188
|
+
makeRoute("files", "/files/*"),
|
|
189
|
+
]);
|
|
190
|
+
|
|
191
|
+
// /files/* should NOT match /files
|
|
192
|
+
expect(router.match("/files")).toBeNull();
|
|
193
|
+
expect(router.match("/files/")).toBeNull(); // normalized to /files
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
test("static route takes precedence over wildcard", () => {
|
|
197
|
+
const router = createRouter([
|
|
198
|
+
makeRoute("files-wildcard", "/files/*"),
|
|
199
|
+
makeRoute("files-readme", "/files/readme"),
|
|
200
|
+
]);
|
|
201
|
+
|
|
202
|
+
expect(router.match("/files/readme")!.route.id).toBe("files-readme");
|
|
203
|
+
expect(router.match("/files/other")!.route.id).toBe("files-wildcard");
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
208
|
+
// 4. Security (URI Encoding)
|
|
209
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
210
|
+
|
|
211
|
+
describe("Security", () => {
|
|
212
|
+
test("blocks %2F (encoded slash) in path segments", () => {
|
|
213
|
+
const router = createRouter([
|
|
214
|
+
makeRoute("user", "/user/:name"),
|
|
215
|
+
]);
|
|
216
|
+
|
|
217
|
+
// a%2Fb = a/b encoded
|
|
218
|
+
const result = router.match("/user/a%2Fb");
|
|
219
|
+
|
|
220
|
+
expect(result).toBeNull();
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
test("blocks double-encoded slash (%252F)", () => {
|
|
224
|
+
const router = createRouter([
|
|
225
|
+
makeRoute("user", "/user/:name"),
|
|
226
|
+
]);
|
|
227
|
+
|
|
228
|
+
// %252F decodes to %2F
|
|
229
|
+
const result = router.match("/user/%252F");
|
|
230
|
+
|
|
231
|
+
expect(result).toBeNull();
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
test("blocks malformed UTF-8 encoding", () => {
|
|
235
|
+
const router = createRouter([
|
|
236
|
+
makeRoute("user", "/user/:name"),
|
|
237
|
+
]);
|
|
238
|
+
|
|
239
|
+
// Invalid UTF-8 sequence
|
|
240
|
+
const result = router.match("/user/%C0%AE");
|
|
241
|
+
|
|
242
|
+
expect(result).toBeNull();
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
test("allows valid percent-encoded characters", () => {
|
|
246
|
+
const router = createRouter([
|
|
247
|
+
makeRoute("search", "/search/:query"),
|
|
248
|
+
]);
|
|
249
|
+
|
|
250
|
+
// hello%20world = "hello world"
|
|
251
|
+
const result = router.match("/search/hello%20world");
|
|
252
|
+
|
|
253
|
+
expect(result).not.toBeNull();
|
|
254
|
+
expect(result!.params).toEqual({ query: "hello world" });
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
259
|
+
// 5. Validation Errors
|
|
260
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
261
|
+
|
|
262
|
+
describe("Validation Errors", () => {
|
|
263
|
+
test("throws DUPLICATE_PATTERN for same pattern", () => {
|
|
264
|
+
expect(() => {
|
|
265
|
+
createRouter([
|
|
266
|
+
makeRoute("route1", "/api/users"),
|
|
267
|
+
makeRoute("route2", "/api/users"),
|
|
268
|
+
]);
|
|
269
|
+
}).toThrow(RouterError);
|
|
270
|
+
|
|
271
|
+
try {
|
|
272
|
+
createRouter([
|
|
273
|
+
makeRoute("route1", "/api/users"),
|
|
274
|
+
makeRoute("route2", "/api/users"),
|
|
275
|
+
]);
|
|
276
|
+
} catch (e) {
|
|
277
|
+
expect(e).toBeInstanceOf(RouterError);
|
|
278
|
+
expect((e as RouterError).code).toBe("DUPLICATE_PATTERN");
|
|
279
|
+
expect((e as RouterError).routeId).toBe("route2");
|
|
280
|
+
expect((e as RouterError).conflictsWith).toBe("route1");
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
test("throws DUPLICATE_PATTERN for normalized duplicates (trailing slash)", () => {
|
|
285
|
+
expect(() => {
|
|
286
|
+
createRouter([
|
|
287
|
+
makeRoute("route1", "/api/users"),
|
|
288
|
+
makeRoute("route2", "/api/users/"),
|
|
289
|
+
]);
|
|
290
|
+
}).toThrow(RouterError);
|
|
291
|
+
|
|
292
|
+
try {
|
|
293
|
+
createRouter([
|
|
294
|
+
makeRoute("route1", "/api/users"),
|
|
295
|
+
makeRoute("route2", "/api/users/"),
|
|
296
|
+
]);
|
|
297
|
+
} catch (e) {
|
|
298
|
+
expect((e as RouterError).code).toBe("DUPLICATE_PATTERN");
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
test("throws PARAM_NAME_CONFLICT for same-depth param mismatch", () => {
|
|
303
|
+
expect(() => {
|
|
304
|
+
createRouter([
|
|
305
|
+
makeRoute("users", "/users/:id"),
|
|
306
|
+
makeRoute("users-by-name", "/users/:name"),
|
|
307
|
+
]);
|
|
308
|
+
}).toThrow(RouterError);
|
|
309
|
+
|
|
310
|
+
try {
|
|
311
|
+
createRouter([
|
|
312
|
+
makeRoute("users", "/users/:id"),
|
|
313
|
+
makeRoute("users-by-name", "/users/:name"),
|
|
314
|
+
]);
|
|
315
|
+
} catch (e) {
|
|
316
|
+
expect((e as RouterError).code).toBe("PARAM_NAME_CONFLICT");
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
test("allows same param name across different paths", () => {
|
|
321
|
+
// These should NOT conflict - different parent paths
|
|
322
|
+
const router = createRouter([
|
|
323
|
+
makeRoute("users", "/users/:id"),
|
|
324
|
+
makeRoute("posts", "/posts/:id"),
|
|
325
|
+
]);
|
|
326
|
+
|
|
327
|
+
expect(router.match("/users/1")!.params).toEqual({ id: "1" });
|
|
328
|
+
expect(router.match("/posts/2")!.params).toEqual({ id: "2" });
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
test("throws WILDCARD_NOT_LAST for non-terminal wildcard", () => {
|
|
332
|
+
expect(() => {
|
|
333
|
+
createRouter([
|
|
334
|
+
makeRoute("invalid", "/files/*/more"),
|
|
335
|
+
]);
|
|
336
|
+
}).toThrow(RouterError);
|
|
337
|
+
|
|
338
|
+
try {
|
|
339
|
+
createRouter([
|
|
340
|
+
makeRoute("invalid", "/files/*/more"),
|
|
341
|
+
]);
|
|
342
|
+
} catch (e) {
|
|
343
|
+
expect((e as RouterError).code).toBe("WILDCARD_NOT_LAST");
|
|
344
|
+
}
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
test("throws ROUTE_CONFLICT for wildcard conflicts", () => {
|
|
348
|
+
expect(() => {
|
|
349
|
+
createRouter([
|
|
350
|
+
makeRoute("files-legacy", "/files/*"),
|
|
351
|
+
makeRoute("files-named", "/files/:path*"),
|
|
352
|
+
]);
|
|
353
|
+
}).toThrow(RouterError);
|
|
354
|
+
|
|
355
|
+
try {
|
|
356
|
+
createRouter([
|
|
357
|
+
makeRoute("files-legacy", "/files/*"),
|
|
358
|
+
makeRoute("files-named", "/files/:path*"),
|
|
359
|
+
]);
|
|
360
|
+
} catch (e) {
|
|
361
|
+
expect((e as RouterError).code).toBe("ROUTE_CONFLICT");
|
|
362
|
+
}
|
|
363
|
+
});
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
367
|
+
// 6. Router API
|
|
368
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
369
|
+
|
|
370
|
+
describe("Router API", () => {
|
|
371
|
+
test("getStats returns correct counts", () => {
|
|
372
|
+
const router = createRouter([
|
|
373
|
+
makeRoute("home", "/"),
|
|
374
|
+
makeRoute("health", "/api/health"),
|
|
375
|
+
makeRoute("todos-item", "/api/todos/:id"),
|
|
376
|
+
makeRoute("files", "/files/*"),
|
|
377
|
+
]);
|
|
378
|
+
|
|
379
|
+
const stats = router.getStats();
|
|
380
|
+
|
|
381
|
+
expect(stats.staticCount).toBe(2); // / and /api/health
|
|
382
|
+
expect(stats.dynamicCount).toBe(2); // /api/todos/:id and /files/*
|
|
383
|
+
expect(stats.totalRoutes).toBe(4);
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
test("getRoutes returns all registered routes", () => {
|
|
387
|
+
const routes = [
|
|
388
|
+
makeRoute("home", "/"),
|
|
389
|
+
makeRoute("users", "/users/:id"),
|
|
390
|
+
];
|
|
391
|
+
const router = createRouter(routes);
|
|
392
|
+
|
|
393
|
+
const retrieved = router.getRoutes();
|
|
394
|
+
|
|
395
|
+
expect(retrieved.length).toBe(2);
|
|
396
|
+
expect(retrieved.map((r) => r.id).sort()).toEqual(["home", "users"]);
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
test("addRoute adds route to existing router", () => {
|
|
400
|
+
const router = createRouter([
|
|
401
|
+
makeRoute("home", "/"),
|
|
402
|
+
]);
|
|
403
|
+
|
|
404
|
+
router.addRoute(makeRoute("about", "/about"));
|
|
405
|
+
|
|
406
|
+
expect(router.match("/about")).not.toBeNull();
|
|
407
|
+
expect(router.getStats().totalRoutes).toBe(2);
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
test("addRoute validates against existing routes", () => {
|
|
411
|
+
const router = createRouter([
|
|
412
|
+
makeRoute("home", "/"),
|
|
413
|
+
]);
|
|
414
|
+
|
|
415
|
+
expect(() => {
|
|
416
|
+
router.addRoute(makeRoute("home2", "/"));
|
|
417
|
+
}).toThrow(RouterError);
|
|
418
|
+
});
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
422
|
+
// 7. Edge Cases
|
|
423
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
424
|
+
|
|
425
|
+
describe("Edge Cases", () => {
|
|
426
|
+
test("empty routes", () => {
|
|
427
|
+
const router = createRouter([]);
|
|
428
|
+
|
|
429
|
+
expect(router.match("/")).toBeNull();
|
|
430
|
+
expect(router.getStats().totalRoutes).toBe(0);
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
test("deep nested paths", () => {
|
|
434
|
+
const router = createRouter([
|
|
435
|
+
makeRoute("deep", "/a/b/c/d/e/:id"),
|
|
436
|
+
]);
|
|
437
|
+
|
|
438
|
+
const result = router.match("/a/b/c/d/e/123");
|
|
439
|
+
|
|
440
|
+
expect(result!.params).toEqual({ id: "123" });
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
test("consecutive params", () => {
|
|
444
|
+
const router = createRouter([
|
|
445
|
+
makeRoute("date", "/calendar/:year/:month/:day"),
|
|
446
|
+
]);
|
|
447
|
+
|
|
448
|
+
const result = router.match("/calendar/2025/01/30");
|
|
449
|
+
|
|
450
|
+
expect(result!.params).toEqual({
|
|
451
|
+
year: "2025",
|
|
452
|
+
month: "01",
|
|
453
|
+
day: "30",
|
|
454
|
+
});
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
test("param followed by static", () => {
|
|
458
|
+
const router = createRouter([
|
|
459
|
+
makeRoute("user-posts", "/users/:id/posts"),
|
|
460
|
+
]);
|
|
461
|
+
|
|
462
|
+
const result = router.match("/users/42/posts");
|
|
463
|
+
|
|
464
|
+
expect(result!.route.id).toBe("user-posts");
|
|
465
|
+
expect(result!.params).toEqual({ id: "42" });
|
|
466
|
+
});
|
|
467
|
+
|
|
468
|
+
test("trailing slash normalization", () => {
|
|
469
|
+
const router = createRouter([
|
|
470
|
+
makeRoute("api", "/api"),
|
|
471
|
+
]);
|
|
472
|
+
|
|
473
|
+
expect(router.match("/api")).not.toBeNull();
|
|
474
|
+
expect(router.match("/api/")).not.toBeNull(); // normalized to /api
|
|
475
|
+
});
|
|
476
|
+
});
|