Skip to content
74 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import { buildTree, canDo, citationHref, coverStyle, cursorColour, flatten, pageIdOf, pagePath, pathTo, readingTime, repoFilePath, repoFolders, snippetParts } from "./docs.ts";
5
6const id = "pag_01jb2k7x9hfq0b3zj0f5s2m8ra";
7
8const nodes = [
9 { id: "b", parent_id: null, position: 2, title: "B", icon: null, slug: "b" },
10 { id: "a", parent_id: null, position: 1, title: "A", icon: null, slug: "a" },
11 { id: "c", parent_id: "a", position: 1, title: "C", icon: null, slug: "c" },
12 { id: "orphan", parent_id: "gone", position: 3, title: "O", icon: null, slug: "o" },
13];
14
15test("the tree orders each level and lifts pages whose parent is gone", () => {
16 const tree = buildTree(nodes);
17 assert.deepEqual(
18 tree.map((n) => n.id),
19 ["a", "b", "orphan"],
20 );
21 assert.equal(tree[0]!.children[0]!.id, "c");
22 assert.equal(tree[0]!.children[0]!.depth, 1);
23 assert.deepEqual(
24 flatten(tree).map((n) => n.id),
25 ["a", "c", "b", "orphan"],
26 );
27 assert.deepEqual(pathTo(nodes, "c"), ["a"]);
28});
29
30test("page addresses carry the id, read back from the end", () => {
31 assert.equal(pagePath("acme", "eng", "Release plan!", id), `/acme/-/docs/eng/release-plan-${id}`);
32 assert.equal(pagePath("acme", "eng", "", id), `/acme/-/docs/eng/${id}`);
33 assert.equal(pageIdOf(`old-title-${id}`), id);
34 assert.equal(pageIdOf("settings"), null);
35});
36
37test("snippets mark their matches", () => {
38 assert.deepEqual(snippetParts("a [[b]] c"), [
39 { text: "a ", match: false },
40 { text: "b", match: true },
41 { text: " c", match: false },
42 ]);
43});
44
45test("covers, colours, roles, reading time", () => {
46 assert.match(coverStyle("gradient:1")!, /^linear-gradient/);
47 assert.match(coverStyle("https://g1tusercontent.com/docs-files/x")!, /url\("https:/);
48 assert.equal(coverStyle("javascript:alert(1)"), null);
49 assert.equal(cursorColour("ana"), cursorColour("ana"));
50 assert.equal(canDo("edit", "comment"), true);
51 assert.equal(canDo("comment", "edit"), false);
52 assert.deepEqual(readingTime("one two three"), { words: 3, minutes: 1 });
53});
54
55test("a citation links to its code, a glob to the folder it starts from", () => {
56 assert.equal(citationHref({ repo: "acme/web", path: "src/export.ts", ref: "abc1234" }), "/acme/web/blob/abc1234/src/export.ts");
57 assert.equal(citationHref({ repo: "acme/web", path: "src/jobs", ref: null }), "/acme/web/tree/HEAD/src/jobs");
58 assert.equal(citationHref({ repo: "acme/web", path: "src/**/*.sql", ref: "abc" }), "/acme/web/tree/abc/src");
59});
60
61test("a project's docs become folders under the space", () => {
62 const root = repoFolders([
63 { path: "README.md", title: "Acme" },
64 { path: "docs/setup.md", title: "Setup" },
65 { path: "docs/guides/deploy.md", title: "Deploy" },
66 ]);
67 assert.deepEqual(
68 root.files.map((f) => f.path),
69 ["README.md", "docs/setup.md"],
70 );
71 assert.equal(root.folders[0]!.name, "guides");
72 assert.deepEqual(root.folders[0]!.files.map((f) => f.title), ["Deploy"]);
73 assert.equal(repoFilePath("acme", "acme/web", "docs/a b.md"), "/acme/-/docs/repo/acme/web/docs/a%20b.md");
74});