import { assertEquals } from "jsr:@std/assert@1"; import { canonical, pageAt, viewAt } from "./route.ts"; const tip = "a".repeat(40); const old = "b".repeat(40); Deno.test("accepts SHA-1 and SHA-256 object ids", () => { const page = pageAt("/u/r/", "main", tip); const sha256 = "c".repeat(64); assertEquals(viewAt(page, `#commit/${sha256}`), { kind: "commit", oid: sha256, path: [] }); assertEquals(viewAt(page, `#commit/${"c".repeat(48)}`), null); }); Deno.test("pathname parses to a page", () => { assertEquals(pageAt("/u/r/", "main", tip), { base: "/u/r", rootBase: "/u/r/", refBase: "/u/r/ref/main/", tip, kind: "tree", path: [] }); assertEquals(pageAt("/u/r/refs", "main", tip).rootBase, "/u/r/"); assertEquals(pageAt("/u/r/ref/main/", "main", tip).refBase, "/u/r/ref/main/"); assertEquals(pageAt("/u/r/ref/feat/x/tree/src/a%20b/", "feat/x", tip), { base: "/u/r", rootBase: "/u/r/ref/feat/x/", refBase: "/u/r/ref/feat/x/", tip, kind: "tree", path: ["src", "a b"], }); assertEquals(pageAt("/u/r/ref/main/blob/src/lib.rs", "main", tip).kind, "blob"); }); Deno.test("hash is a delta on the page", () => { const page = pageAt("/u/r/ref/main/blob/src/lib.rs", "main", tip); assertEquals(viewAt(page, ""), null); assertEquals(viewAt(page, "#L12"), null); assertEquals(viewAt(page, `#${old}`), { kind: "blob", oid: old, path: ["src", "lib.rs"] }); assertEquals(viewAt(page, "#history"), { kind: "history", oid: tip, path: ["src", "lib.rs"] }); assertEquals(viewAt(page, `#history/${old}`), { kind: "history", oid: old, path: ["src", "lib.rs"] }); assertEquals(viewAt(page, `#commit/${old}`), { kind: "commit", oid: old, path: [] }); assertEquals(viewAt(page, "#language/rust"), { kind: "language", oid: tip, path: ["rust"] }); assertEquals(viewAt(page, `#tree/${old}/a%20b/c`), { kind: "tree", oid: old, path: ["a b", "c"] }); assertEquals(viewAt(page, `#tree/${old}`), { kind: "tree", oid: old, path: [] }); assertEquals(viewAt(pageAt("/u/r/", "main", null), "#history"), null); }); Deno.test("canonical picks the shortest valid form", () => { const page = pageAt("/u/r/ref/main/tree/src/", "main", tip); const lib = ["src", "lib.rs"]; assertEquals(canonical(page, { kind: "blob", oid: tip, path: lib }, "blob"), "/u/r/ref/main/blob/src/lib.rs"); assertEquals(canonical(page, { kind: "blob", oid: old, path: lib }, "blob"), `/u/r/ref/main/blob/src/lib.rs#${old}`); assertEquals(canonical(page, { kind: "blob", oid: old, path: lib }, null), `#blob/${old}/src/lib.rs`); assertEquals(canonical(page, { kind: "blob", oid: old, path: lib }, "tree"), `#blob/${old}/src/lib.rs`); assertEquals(canonical(page, { kind: "tree", oid: old, path: [] }, "tree"), `/u/r/ref/main/#${old}`); assertEquals(canonical(page, { kind: "history", oid: tip, path: lib }, "blob"), "/u/r/ref/main/blob/src/lib.rs#history"); assertEquals(canonical(page, { kind: "history", oid: old, path: [] }, "tree"), `/u/r/ref/main/#history/${old}`); assertEquals(canonical(page, { kind: "history", oid: old, path: lib }, null), `#history/${old}/src/lib.rs`); assertEquals(canonical(page, { kind: "commit", oid: tip, path: [] }, null), `#commit/${tip}`); assertEquals(canonical(page, { kind: "language", oid: tip, path: ["rust"] }, null), "/u/r/ref/main/#language/rust"); assertEquals(canonical(page, { kind: "language", oid: old, path: ["rust"] }, null), `/u/r/ref/main/#language/rust/${old}`); }); Deno.test("canonical round-trips through viewAt", () => { const page = pageAt("/u/r/", "main", tip); for (const view of [ { kind: "blob" as const, oid: old, path: ["a b", "c.rs"] }, { kind: "history" as const, oid: old, path: ["a b"] }, { kind: "tree" as const, oid: old, path: ["a b"] }, ]) { const at = view.kind === "blob" ? "blob" : "tree"; const url = new URL(canonical(page, view, at), "http://x/u/r/"); assertEquals(viewAt(pageAt(url.pathname, "main", tip), url.hash), view); } });