import { test } from "node:test";import assert from "node:assert/strict";import { h, load } from "../../lib/harness.mjs";const { matchRoute } = await load();const run = (routes, path) => { const r = h.res(() => matchRoute(routes, path)); if (!r.ok) return r; const o = h.opt(r.value); return o.some ? { ok: true, value: { some: true, value: { route: o.value.route, params: { ...o.value.params } } } } : { ok: true, value: o };};const hit = (route, params = {}) => ({ ok: true, value: { some: true, value: { route, params } } });const miss = { ok: true, value: { some: false } };const invalid = (route) => ({ ok: false, error: { kind: "invalid_route", route } });test("static routes", () => { const routes = ["/", "/about", "/about/team"]; assert.deepEqual(run(routes, "/about"), hit("/about")); assert.deepEqual(run(routes, "/about/team"), hit("/about/team")); assert.deepEqual(run(routes, "/"), hit("/")); assert.deepEqual(run(routes, "/contact"), miss); assert.deepEqual(run(routes, "/About"), miss); assert.deepEqual(run([], "/"), miss);});test("params are extracted and decoded", () => { assert.deepEqual(run(["/users/:id/posts/:post"], "/users/42/posts/hello%20world"), hit("/users/:id/posts/:post", { id: "42", post: "hello world" })); assert.deepEqual(run(["/t/:tag"], "/t/caf%C3%A9"), hit("/t/:tag", { tag: "café" })); assert.deepEqual(run(["/t/:tag"], "/t/0"), hit("/t/:tag", { tag: "0" }));});test("segment counts must line up", () => { assert.deepEqual(run(["/a/:x"], "/a/1/2"), miss); assert.deepEqual(run(["/a/:x"], "/a"), miss); assert.deepEqual(run(["/a/:x"], "/a/"), miss); assert.deepEqual(run(["/"], "/a"), miss);});test("static beats param regardless of order", () => { for (const routes of [["/users/:id", "/users/me"], ["/users/me", "/users/:id"]]) { assert.deepEqual(run(routes, "/users/me"), hit("/users/me")); assert.deepEqual(run(routes, "/users/5"), hit("/users/:id", { id: "5" })); } assert.deepEqual(run(["/:a/b", "/a/:b"], "/a/b"), hit("/a/:b", { b: "b" }));});test("splats", () => { const routes = ["/files/*path"]; assert.deepEqual(run(routes, "/files/a/b/c.txt"), hit("/files/*path", { path: "a/b/c.txt" })); assert.deepEqual(run(routes, "/files"), hit("/files/*path", { path: "" })); assert.deepEqual(run(routes, "/files/"), hit("/files/*path", { path: "" })); assert.deepEqual(run(routes, "/files/a%2Fb/c%20d"), hit("/files/*path", { path: "a/b/c d" })); assert.deepEqual(run(["/*all"], "/"), hit("/*all", { all: "" })); assert.deepEqual(run(routes, "/filesx/a"), miss);});test("param beats splat, and an exact end beats a splat", () => { for (const routes of [["/files/*rest", "/files/:name"], ["/files/:name", "/files/*rest"]]) { assert.deepEqual(run(routes, "/files/x"), hit("/files/:name", { name: "x" })); assert.deepEqual(run(routes, "/files/x/y"), hit("/files/*rest", { rest: "x/y" })); } assert.deepEqual(run(["/files/*rest", "/files"], "/files"), hit("/files")); assert.deepEqual(run(["/*all", "/:a/:b"], "/x/y"), hit("/:a/:b", { a: "x", b: "y" }));});test("ties go to the earlier route", () => { assert.deepEqual(run(["/u/:id", "/u/:uid"], "/u/1"), hit("/u/:id", { id: "1" })); assert.deepEqual(run(["/u/:uid", "/u/:id"], "/u/1"), hit("/u/:uid", { uid: "1" }));});test("query string and trailing slash are ignored", () => { const routes = ["/", "/search/:q"]; assert.deepEqual(run(routes, "/search/cats?page=2&x=/y"), hit("/search/:q", { q: "cats" })); assert.deepEqual(run(routes, "/search/cats/"), hit("/search/:q", { q: "cats" })); assert.deepEqual(run(routes, "/search/cats/?a=1"), hit("/search/:q", { q: "cats" })); assert.deepEqual(run(routes, "/?a=1"), hit("/")); assert.deepEqual(run(routes, "/search/?q=cats"), miss);});test("malformed escapes make the route not match", () => { assert.deepEqual(run(["/q/:term"], "/q/%E0%A4%A"), miss); assert.deepEqual(run(["/q/:term", "/*all"], "/q/%zz"), miss); assert.deepEqual(run(["/q/:term", "/q/%zz"], "/q/%zz"), hit("/q/%zz")); assert.deepEqual(run(["/q/:term", "/:a/:b"], "/q/100%"), miss); assert.deepEqual(run(["/x/:bad/a", "/x/*rest"], "/x/%ZZ/b"), miss); assert.deepEqual(run(["/x/:id/a", "/x/:id/:b"], "/x/1/%E0%A4%A8"), hit("/x/:id/:b", { id: "1", b: "न" }));});test("invalid patterns", () => { for (const route of ["users", "", "/a//b", "/a/", "/a/:", "/a/*", "/a/*rest/b", "/a/:id/:id", "/a/:x/*x", "//"]) { assert.deepEqual(run([route], "/a"), invalid(route), route); }});test("all patterns are validated before matching, first invalid wins", () => { assert.deepEqual(run(["/a", "/b/:", "/c//"], "/a"), invalid("/b/:")); assert.deepEqual(run(["/a/*rest", "/b/*x/y"], "/a/1"), invalid("/b/*x/y"));});test("param names that exist on Object.prototype", () => { const r = run(["/:constructor/:toString"], "/a/b"); assert.deepEqual(r, hit("/:constructor/:toString", { constructor: "a", toString: "b" }));});