| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { cleanCover, cleanIcon, cleanSource, cleanTarget, cleanTitle, decodeCursor, depthOf, encodeCursor, listLimit, sharedTops, subtreeHeight, treeNodes } from "./list.ts"; |
| 5 | |
| 6 | test("titles, icons, covers and sources are cleaned", () => { |
| 7 | assert.equal(cleanTitle(" Q4 roadmap \n"), "Q4 roadmap"); |
| 8 | assert.equal(cleanTitle("x".repeat(300)).length, 200); |
| 9 | assert.equal(cleanIcon(""), null); |
| 10 | assert.equal(cleanIcon("📐 extra words"), "📐 ex"); |
| 11 | assert.equal(cleanCover("gradient:3"), "gradient:3"); |
| 12 | assert.equal(cleanCover("javascript:alert(1)"), null); |
| 13 | assert.deepEqual(cleanSource({ title: "Launch thread", href: "/acme/-/chat/general?thread=1" }), { title: "Launch thread", href: "/acme/-/chat/general?thread=1" }); |
| 14 | assert.equal(cleanSource({ title: "x", href: "https://evil.example" }), null); |
| 15 | assert.equal(cleanSource({ title: "x", href: "//evil.example" }), null); |
| 16 | assert.deepEqual(cleanTarget({ kind: "section", heading: " Risks " }), { kind: "section", heading: "Risks" }); |
| 17 | assert.equal(cleanTarget({ kind: "nope" }), null); |
| 18 | }); |
| 19 | |
| 20 | test("the cursor goes there and back, and nonsense is no cursor", () => { |
| 21 | const c = { k: "2026-10-09T03:00:00.000Z", id: "fol_01jb2k7x9hfq0b3zj0f5s2m8ra" }; |
| 22 | assert.deepEqual(decodeCursor(encodeCursor(c)), c); |
| 23 | assert.equal(decodeCursor("not a cursor"), null); |
| 24 | assert.equal(decodeCursor(null), null); |
| 25 | assert.equal(listLimit(undefined), 30); |
| 26 | assert.equal(listLimit(500), 100); |
| 27 | assert.equal(listLimit(0), 30); |
| 28 | }); |
| 29 | |
| 30 | test("a tree puts a row under its parent only when the parent is shown", () => { |
| 31 | const rows = [ |
| 32 | { id: "b", kind: "doc" as const, parent_id: "a", position: 2, title: "B", icon: null, inherit: 1 }, |
| 33 | { id: "a", kind: "doc" as const, parent_id: null, position: 1, title: "A", icon: null, inherit: 1 }, |
| 34 | { id: "c", kind: "doc" as const, parent_id: "hidden", position: 3, title: "C", icon: null, inherit: 0 }, |
| 35 | ]; |
| 36 | const tree = treeNodes(rows, new Set(["b"])); |
| 37 | assert.deepEqual( |
| 38 | tree.map((n) => [n.id, n.parent_id, n.restricted, n.stale ?? false]), |
| 39 | [ |
| 40 | ["a", null, false, false], |
| 41 | ["b", "a", false, true], |
| 42 | ["c", null, true, false], |
| 43 | ], |
| 44 | ); |
| 45 | assert.equal(tree[0]!.slug, "a-a"); |
| 46 | }); |
| 47 | |
| 48 | test("shared tops leave out children of what is shown and anything shown elsewhere", () => { |
| 49 | const readable = [ |
| 50 | { id: "top", parent_id: "secret" }, |
| 51 | { id: "kid", parent_id: "top" }, |
| 52 | { id: "loose", parent_id: null }, |
| 53 | { id: "inspace", parent_id: null }, |
| 54 | { id: "underspace", parent_id: "spacedoc" }, |
| 55 | ]; |
| 56 | assert.deepEqual( |
| 57 | sharedTops(readable, new Set(["inspace", "spacedoc"])).map((r) => r.id), |
| 58 | ["top", "loose"], |
| 59 | ); |
| 60 | }); |
| 61 | |
| 62 | test("depth and height come from paths", () => { |
| 63 | assert.equal(depthOf("/a/"), 1); |
| 64 | assert.equal(depthOf("/a/b/c/"), 3); |
| 65 | assert.equal(subtreeHeight({ path: "/a/b/" }, [{ path: "/a/b/" }, { path: "/a/b/c/" }, { path: "/a/b/c/d/" }]), 2); |
| 66 | }); |