Radish alpha
r
Radicle web interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
Test that route building and parsing leave routes invariant
Thomas Scholtes committed 2 years ago
commit c6afc56beb57f8ab327be38032be392d3c429901
parent 9f40fd85b77976aa66a7df01cadd56dce65bc030
2 files changed +39 -26
modified 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);
  }
}

modified 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" } },
+
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,
      },
-
      output: "/seeds/willow.radicle.garden",
-
      description: "Seed View Route",
-
    },
-
    {
-
      input: {
-
        resource: "projects",
-
        params: {
-
          view: { resource: "tree" },
-
          baseUrl: { hostname: "willow.radicle.garden" },
-
          id: "rad:zKtT7DmF9H34KkvcKj9PHW19WzjT",
-
        },
+
    });
+
  });
+
  test("projects.tree", () => {
+
    return expectParsingInvariant({
+
      resource: "projects",
+
      params: {
+
        view: { resource: "tree" },
+
        baseUrl,
+
        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);
+
    });
  });
+

+
  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", () => {