|
1 | 1 | import { test } from "@fedify/fixture"; |
2 | 2 | import { deepEqual, equal, throws } from "node:assert/strict"; |
| 3 | +import type Template from "../template/template.ts"; |
3 | 4 | import { |
4 | 5 | createRouterAddTest, |
5 | 6 | createRouterBuildTest, |
@@ -64,11 +65,22 @@ const createCountingPattern = ( |
64 | 65 | ): RouterPathPattern => { |
65 | 66 | const pattern = Router.compile(path); |
66 | 67 | const match = pattern.template.match; |
67 | | - pattern.template.match = (uri: string): ExpandContext | null => { |
68 | | - calls.set(path, (calls.get(path) ?? 0) + 1); |
69 | | - return match(uri); |
| 68 | + const template = { |
| 69 | + get tokens(): typeof pattern.template.tokens { |
| 70 | + return pattern.template.tokens; |
| 71 | + }, |
| 72 | + expand: pattern.template.expand, |
| 73 | + match: (uri: string): ExpandContext | null => { |
| 74 | + calls.set(path, (calls.get(path) ?? 0) + 1); |
| 75 | + return match(uri); |
| 76 | + }, |
| 77 | + toString: pattern.template.toString, |
| 78 | + } as unknown as Template; |
| 79 | + return { |
| 80 | + path: pattern.path, |
| 81 | + template, |
| 82 | + variables: pattern.variables, |
70 | 83 | }; |
71 | | - return pattern; |
72 | 84 | }; |
73 | 85 |
|
74 | 86 | test("Router indexes shared dynamic prefixes before template matching", () => { |
@@ -219,6 +231,93 @@ test("Router accepts pre-parsed RouterPathPattern", async (t) => { |
219 | 231 | }); |
220 | 232 | }); |
221 | 233 |
|
| 234 | +test("Router.compile() returns immutable path patterns", async (t) => { |
| 235 | + const pattern = Router.compile("/items/{id}"); |
| 236 | + const router = new Router([[pattern, "item"]]); |
| 237 | + |
| 238 | + await t.step("blocks Template entry point reassignment", () => { |
| 239 | + throws(() => { |
| 240 | + (pattern.template as { |
| 241 | + match: (uri: string) => ExpandContext | null; |
| 242 | + }).match = () => null; |
| 243 | + }, TypeError); |
| 244 | + throws(() => { |
| 245 | + (pattern.template as { |
| 246 | + expand: (context: ExpandContext) => string; |
| 247 | + }).expand = () => "/changed"; |
| 248 | + }, TypeError); |
| 249 | + }); |
| 250 | + |
| 251 | + await t.step("blocks RouterPathPattern wrapper reassignment", () => { |
| 252 | + const otherTemplate = Router.compile("/other/{id}").template; |
| 253 | + throws(() => { |
| 254 | + (pattern as { path: Path }).path = "/other/{id}"; |
| 255 | + }, TypeError); |
| 256 | + throws(() => { |
| 257 | + (pattern as { template: typeof otherTemplate }).template = otherTemplate; |
| 258 | + }, TypeError); |
| 259 | + }); |
| 260 | + |
| 261 | + await t.step("blocks variables mutation", () => { |
| 262 | + throws(() => { |
| 263 | + (pattern.variables as Set<string>).add("unexpected"); |
| 264 | + }, TypeError); |
| 265 | + equal(pattern.variables.has("unexpected"), false); |
| 266 | + equal(pattern.variables.size, 1); |
| 267 | + }); |
| 268 | + |
| 269 | + await t.step("keeps registered routing behavior unchanged", () => { |
| 270 | + deepEqual(router.route("/items/9"), { |
| 271 | + name: "item", |
| 272 | + template: "/items/{id}", |
| 273 | + values: { id: "9" }, |
| 274 | + }); |
| 275 | + equal(router.build("item", { id: "9" }), "/items/9"); |
| 276 | + }); |
| 277 | +}); |
| 278 | + |
| 279 | +test("Router.clone() isolates route sets with shared pre-parsed patterns", () => { |
| 280 | + const pattern = Router.compile("/items/{id}"); |
| 281 | + const original = new Router([[pattern, "item"]]); |
| 282 | + const clone = original.clone(); |
| 283 | + const itemRoute = { |
| 284 | + name: "item", |
| 285 | + template: "/items/{id}", |
| 286 | + values: { id: "9" }, |
| 287 | + }; |
| 288 | + |
| 289 | + deepEqual(original.route("/items/9"), itemRoute); |
| 290 | + deepEqual(clone.route("/items/9"), itemRoute); |
| 291 | + |
| 292 | + original.add("/posts/{postId}", "post"); |
| 293 | + equal(clone.route("/posts/3"), null); |
| 294 | + deepEqual(original.route("/posts/3"), { |
| 295 | + name: "post", |
| 296 | + template: "/posts/{postId}", |
| 297 | + values: { postId: "3" }, |
| 298 | + }); |
| 299 | + |
| 300 | + clone.add("/users/{userId}", "user"); |
| 301 | + equal(original.route("/users/5"), null); |
| 302 | + deepEqual(clone.route("/users/5"), { |
| 303 | + name: "user", |
| 304 | + template: "/users/{userId}", |
| 305 | + values: { userId: "5" }, |
| 306 | + }); |
| 307 | + |
| 308 | + original.add("/people/{id}", "item"); |
| 309 | + equal(original.route("/items/9"), null); |
| 310 | + deepEqual(original.route("/people/9"), { |
| 311 | + name: "item", |
| 312 | + template: "/people/{id}", |
| 313 | + values: { id: "9" }, |
| 314 | + }); |
| 315 | + equal(original.build("item", { id: "9" }), "/people/9"); |
| 316 | + deepEqual(clone.route("/items/9"), itemRoute); |
| 317 | + equal(clone.route("/people/9"), null); |
| 318 | + equal(clone.build("item", { id: "9" }), "/items/9"); |
| 319 | +}); |
| 320 | + |
222 | 321 | test("Router trailing slash retry accepts empty root path", () => { |
223 | 322 | const router = new Router([["", "root"]], { |
224 | 323 | trailingSlashInsensitive: true, |
|
0 commit comments