char/sorcery

static-files based git repo viewer

git clone https://git.t4t.associates/char/sorcery

Charlotte Somexperiment: support sha-256 oids in git repos42f80d8

main
3.9 KiB70 linesraw
1import { assertEquals } from "jsr:@std/assert@1";
2import { canonical, pageAt, viewAt } from "./route.ts";
3
4const tip = "a".repeat(40);
5const old = "b".repeat(40);
6
7Deno.test("accepts SHA-1 and SHA-256 object ids", () => {
8  const page = pageAt("/u/r/", "main", tip);
9  const sha256 = "c".repeat(64);
10  assertEquals(viewAt(page, `#commit/${sha256}`), { kind: "commit", oid: sha256, path: [] });
11  assertEquals(viewAt(page, `#commit/${"c".repeat(48)}`), null);
12});
13
14Deno.test("pathname parses to a page", () => {
15  assertEquals(pageAt("/u/r/", "main", tip), { base: "/u/r", rootBase: "/u/r/", refBase: "/u/r/ref/main/", tip, kind: "tree", path: [] });
16  assertEquals(pageAt("/u/r/refs", "main", tip).rootBase, "/u/r/");
17  assertEquals(pageAt("/u/r/ref/main/", "main", tip).refBase, "/u/r/ref/main/");
18  assertEquals(pageAt("/u/r/ref/feat/x/tree/src/a%20b/", "feat/x", tip), {
19    base: "/u/r",
20    rootBase: "/u/r/ref/feat/x/",
21    refBase: "/u/r/ref/feat/x/",
22    tip,
23    kind: "tree",
24    path: ["src", "a b"],
25  });
26  assertEquals(pageAt("/u/r/ref/main/blob/src/lib.rs", "main", tip).kind, "blob");
27});
28
29Deno.test("hash is a delta on the page", () => {
30  const page = pageAt("/u/r/ref/main/blob/src/lib.rs", "main", tip);
31  assertEquals(viewAt(page, ""), null);
32  assertEquals(viewAt(page, "#L12"), null);
33  assertEquals(viewAt(page, `#${old}`), { kind: "blob", oid: old, path: ["src", "lib.rs"] });
34  assertEquals(viewAt(page, "#history"), { kind: "history", oid: tip, path: ["src", "lib.rs"] });
35  assertEquals(viewAt(page, `#history/${old}`), { kind: "history", oid: old, path: ["src", "lib.rs"] });
36  assertEquals(viewAt(page, `#commit/${old}`), { kind: "commit", oid: old, path: [] });
37  assertEquals(viewAt(page, "#language/rust"), { kind: "language", oid: tip, path: ["rust"] });
38  assertEquals(viewAt(page, `#tree/${old}/a%20b/c`), { kind: "tree", oid: old, path: ["a b", "c"] });
39  assertEquals(viewAt(page, `#tree/${old}`), { kind: "tree", oid: old, path: [] });
40  assertEquals(viewAt(pageAt("/u/r/", "main", null), "#history"), null);
41});
42
43Deno.test("canonical picks the shortest valid form", () => {
44  const page = pageAt("/u/r/ref/main/tree/src/", "main", tip);
45  const lib = ["src", "lib.rs"];
46  assertEquals(canonical(page, { kind: "blob", oid: tip, path: lib }, "blob"), "/u/r/ref/main/blob/src/lib.rs");
47  assertEquals(canonical(page, { kind: "blob", oid: old, path: lib }, "blob"), `/u/r/ref/main/blob/src/lib.rs#${old}`);
48  assertEquals(canonical(page, { kind: "blob", oid: old, path: lib }, null), `#blob/${old}/src/lib.rs`);
49  assertEquals(canonical(page, { kind: "blob", oid: old, path: lib }, "tree"), `#blob/${old}/src/lib.rs`);
50  assertEquals(canonical(page, { kind: "tree", oid: old, path: [] }, "tree"), `/u/r/ref/main/#${old}`);
51  assertEquals(canonical(page, { kind: "history", oid: tip, path: lib }, "blob"), "/u/r/ref/main/blob/src/lib.rs#history");
52  assertEquals(canonical(page, { kind: "history", oid: old, path: [] }, "tree"), `/u/r/ref/main/#history/${old}`);
53  assertEquals(canonical(page, { kind: "history", oid: old, path: lib }, null), `#history/${old}/src/lib.rs`);
54  assertEquals(canonical(page, { kind: "commit", oid: tip, path: [] }, null), `#commit/${tip}`);
55  assertEquals(canonical(page, { kind: "language", oid: tip, path: ["rust"] }, null), "/u/r/ref/main/#language/rust");
56  assertEquals(canonical(page, { kind: "language", oid: old, path: ["rust"] }, null), `/u/r/ref/main/#language/rust/${old}`);
57});
58
59Deno.test("canonical round-trips through viewAt", () => {
60  const page = pageAt("/u/r/", "main", tip);
61  for (const view of [
62    { kind: "blob" as const, oid: old, path: ["a b", "c.rs"] },
63    { kind: "history" as const, oid: old, path: ["a b"] },
64    { kind: "tree" as const, oid: old, path: ["a b"] },
65  ]) {
66    const at = view.kind === "blob" ? "blob" : "tree";
67    const url = new URL(canonical(page, view, at), "http://x/u/r/");
68    assertEquals(viewAt(pageAt(url.pathname, "main", tip), url.hash), view);
69  }
70});