Skip to content
53 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import { buildTree, canDo, coverStyle, cursorColour, flatten, pageIdOf, pagePath, pathTo, readingTime, 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});