diff --git a/src/lib/router.ts b/src/lib/router.ts index d91e108ca..d502ec546 100644 --- a/src/lib/router.ts +++ b/src/lib/router.ts @@ -12,6 +12,8 @@ import { } from "@app/views/projects/router"; import { loadRoute } from "@app/lib/router/definitions"; +export { type Route }; + // Only used by Safari. const DOCUMENT_TITLE = "Radicle Interface"; @@ -227,7 +229,7 @@ function seedPath(baseUrl: BaseUrl) { } } -export function routeToPath(route: Route) { +export function routeToPath(route: Route): string { if (route.resource === "home") { return "/"; } else if (route.resource === "session") { @@ -302,7 +304,7 @@ export function routeToPath(route: Route) { } else if (route.resource === "notFound") { return route.params.url; } else { - utils.unreachable(route); + return utils.unreachable(route); } } diff --git a/tests/unit/router.test.ts b/tests/unit/router.test.ts index 498757fba..e0f0ced78 100644 --- a/tests/unit/router.test.ts +++ b/tests/unit/router.test.ts @@ -1,35 +1,46 @@ import { describe, expect, test } from "vitest"; -import { testExports } from "@app/lib/router"; +import { testExports, type Route } from "@app/lib/router"; // Defining the window.origin value, since vitest doesn't provide one. window.origin = "http://localhost:3000"; -describe("routeToPath", () => { - test.each([ - { input: { resource: "home" }, output: "/", description: "Home Route" }, - { - input: { - resource: "seeds", - params: { baseUrl: { hostname: "willow.radicle.garden" } }, - }, - output: "/seeds/willow.radicle.garden", - description: "Seed View Route", - }, - { - input: { - resource: "projects", - params: { - view: { resource: "tree" }, - baseUrl: { hostname: "willow.radicle.garden" }, - id: "rad:zKtT7DmF9H34KkvcKj9PHW19WzjT", - }, - }, - output: "/seeds/willow.radicle.garden/rad:zKtT7DmF9H34KkvcKj9PHW19WzjT", - description: "Seed Project Route", - }, - ])("$description", (route: any) => { - expect(testExports.routeToPath(route.input)).toEqual(route.output); +describe("route invariant when parsed", () => { + const baseUrl = { + hostname: "willow.radicle.garden", + port: 8000, + scheme: "http", + }; + + test("home", () => { + return expectParsingInvariant({ resource: "home" }); }); + test("seeds", () => { + expectParsingInvariant({ + resource: "seeds", + params: { + // TODO: This only works with the value 0. The value is not actually + // extract. + projectPageIndex: 0, + baseUrl, + }, + }); + }); + test("projects.tree", () => { + return expectParsingInvariant({ + resource: "projects", + params: { + view: { resource: "tree" }, + baseUrl, + id: "rad:zKtT7DmF9H34KkvcKj9PHW19WzjT", + }, + }); + }); + + function expectParsingInvariant(route: Route) { + const origin = "http://localhost:3000"; + const path = testExports.routeToPath(route); + expect(testExports.pathToRoute(new URL(path, origin))).toEqual(route); + } }); describe("pathToRoute", () => {