forks/sorcery

git clone https://git.gae.moe/forks/sorcery

Charlotte Somrender tip tree/blob views on clientside (big rework)7e9177b

darwin-flake
4.8 KiB85 linesraw
1import { assertEquals } from "jsr:@std/assert@1";
2import { canonical, pageAt, pageView, viewAt, withinRef } 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)}`), pageView(page));
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/refs", "main", tip).tip, null);
18  assertEquals(pageAt("/u/r/ref/main/", "main", tip).refBase, "/u/r/ref/main/");
19  assertEquals(pageAt("/u/r/ref/feat/x/tree/src/a%20b/", "feat/x", tip), {
20    base: "/u/r",
21    rootBase: "/u/r/ref/feat/x/",
22    refBase: "/u/r/ref/feat/x/",
23    tip,
24    kind: "tree",
25    path: ["src", "a b"],
26  });
27  assertEquals(pageAt("/u/r/ref/main/blob/src/lib.rs", "main", tip).kind, "blob");
28});
29
30Deno.test("hash is a delta on the page", () => {
31  const page = pageAt("/u/r/ref/main/blob/src/lib.rs", "main", tip);
32  const own = { kind: "blob", oid: tip, path: ["src", "lib.rs"] };
33  assertEquals(viewAt(page, ""), own);
34  assertEquals(viewAt(page, "#L12"), own);
35  assertEquals(viewAt(pageAt("/u/r/ref/main/tree/src/", "main", tip), ""), { kind: "tree", oid: tip, path: ["src"] });
36  assertEquals(viewAt(pageAt("/u/r/", "main", null), ""), null);
37  assertEquals(viewAt(page, `#${old}`), { kind: "blob", oid: old, path: ["src", "lib.rs"] });
38  assertEquals(viewAt(page, "#history"), { kind: "history", oid: tip, path: ["src", "lib.rs"] });
39  assertEquals(viewAt(page, `#history/${old}`), { kind: "history", oid: old, path: ["src", "lib.rs"] });
40  assertEquals(viewAt(page, `#commit/${old}`), { kind: "commit", oid: old, path: [] });
41  assertEquals(viewAt(page, "#language/rust"), { kind: "language", oid: tip, path: ["rust"] });
42  assertEquals(viewAt(page, `#tree/${old}/a%20b/c`), { kind: "tree", oid: old, path: ["a b", "c"] });
43  assertEquals(viewAt(page, `#tree/${old}`), { kind: "tree", oid: old, path: [] });
44  assertEquals(viewAt(pageAt("/u/r/", "main", null), "#history"), null);
45});
46
47Deno.test("the tip speaks for the ref's pages only", () => {
48  const root = pageAt("/u/r/", "main", tip);
49  assertEquals(withinRef(root, "/u/r/"), true);
50  assertEquals(withinRef(root, "/u/r/ref/main/tree/src/"), true);
51  assertEquals(withinRef(root, "/u/r/ref/other/"), false);
52  assertEquals(withinRef(root, "/u/r/refs"), false);
53  // a ref page cannot know whether the root page is its own
54  assertEquals(withinRef(pageAt("/u/r/ref/main/", "main", tip), "/u/r/"), false);
55  assertEquals(withinRef(pageAt("/u/r/", "main", null), "/u/r/"), false);
56});
57
58Deno.test("canonical picks the shortest valid form", () => {
59  const page = pageAt("/u/r/ref/main/tree/src/", "main", tip);
60  const lib = ["src", "lib.rs"];
61  assertEquals(canonical(page, { kind: "blob", oid: tip, path: lib }, "blob"), "/u/r/ref/main/blob/src/lib.rs");
62  assertEquals(canonical(page, { kind: "blob", oid: old, path: lib }, "blob"), `/u/r/ref/main/blob/src/lib.rs#${old}`);
63  assertEquals(canonical(page, { kind: "blob", oid: old, path: lib }, null), `#blob/${old}/src/lib.rs`);
64  assertEquals(canonical(page, { kind: "blob", oid: old, path: lib }, "tree"), `#blob/${old}/src/lib.rs`);
65  assertEquals(canonical(page, { kind: "tree", oid: old, path: [] }, "tree"), `/u/r/ref/main/#${old}`);
66  assertEquals(canonical(page, { kind: "history", oid: tip, path: lib }, "blob"), "/u/r/ref/main/blob/src/lib.rs#history");
67  assertEquals(canonical(page, { kind: "history", oid: old, path: [] }, "tree"), `/u/r/ref/main/#history/${old}`);
68  assertEquals(canonical(page, { kind: "history", oid: old, path: lib }, null), `#history/${old}/src/lib.rs`);
69  assertEquals(canonical(page, { kind: "commit", oid: tip, path: [] }, null), `#commit/${tip}`);
70  assertEquals(canonical(page, { kind: "language", oid: tip, path: ["rust"] }, null), "/u/r/ref/main/#language/rust");
71  assertEquals(canonical(page, { kind: "language", oid: old, path: ["rust"] }, null), `/u/r/ref/main/#language/rust/${old}`);
72});
73
74Deno.test("canonical round-trips through viewAt", () => {
75  const page = pageAt("/u/r/", "main", tip);
76  for (const view of [
77    { kind: "blob" as const, oid: old, path: ["a b", "c.rs"] },
78    { kind: "history" as const, oid: old, path: ["a b"] },
79    { kind: "tree" as const, oid: old, path: ["a b"] },
80  ]) {
81    const at = view.kind === "blob" ? "blob" : "tree";
82    const url = new URL(canonical(page, view, at), "http://x/u/r/");
83    assertEquals(viewAt(pageAt(url.pathname, "main", tip), url.hash), view);
84  }
85});