// The report's "How do I…?" help (pivotkit-react's help-topics.js): each built-in topic links to a // docs page and a heading that exist, and the docs state the help's limits and ids as the code has // them. pivotkit-react's test/help.test.js checks the topics' texts and what Show me opens; the // browser test (tests/browser/help.spec.js) checks every topic in the rendered playground. import { test } from "node:test"; import assert from "node:assert/strict"; import { existsSync, readFileSync } from "node:fs"; import { HELP_LIST, HELP_RESERVED, HELP_TOPICS, OWN_LIMITS } from "../packages/pivotkit-react/src/help-topics.js"; import { assertStates } from "./docs-facts.js"; const docs = new URL("../docs/", import.meta.url); /** * A heading's anchor as the docs site writes it: an explicit `{ #id }`, else Python-Markdown's toc * slug (ASCII letters, digits, "_" and "-"; spaces and dashes become one "-"; lower case). JavaScript's * \w is ASCII only, so other letters drop out as in Python-Markdown's ASCII encoding. * @param {string} line a Markdown heading line */ function anchorOf(line) { const explicit = line.match(/\{\s*#([\w-]+)[^}]*\}\s*$/); if (explicit) return explicit[1]; const text = line.replace(/^#+\s*/, "").replace(/\s*\{[^}]*\}\s*$/, ""); return text .normalize("NFKD") .replace(/[^\w\s-]/g, "") .trim() .toLowerCase() .replace(/[-\s]+/g, "-"); } /** The anchors of a page's headings, outside code blocks. */ function anchors(page) { let fenced = false; const found = new Set(); for (const line of readFileSync(new URL(page, docs), "utf8").split("\n")) { if (/^\s*(```|~~~)/.test(line)) fenced = !fenced; else if (!fenced && /^#{1,6}\s/.test(line)) found.add(anchorOf(line)); } return found; } test("the slug follows Python-Markdown's toc", () => { assert.equal(anchorOf("## Filter by values"), "filter-by-values"); assert.equal(anchorOf("## Top N"), "top-n"); assert.equal(anchorOf("## The row count under the result { #row-count }"), "row-count"); assert.equal(anchorOf("## Quick choices for dates"), "quick-choices-for-dates"); assert.equal(anchorOf("## 6. Switch to the chart"), "6-switch-to-the-chart"); }); test("every topic links to a docs page and heading that exist", () => { for (const topic of HELP_TOPICS) { const [page, anchor] = topic.docs.split("#"); assert.ok(existsSync(new URL(page, docs)), `${topic.id}: docs/${page} does not exist`); if (anchor) assert.ok(anchors(page).has(anchor), `${topic.id}: docs/${page} has no heading #${anchor}`); } }); test("the docs state the help's limits as the code has them", () => { const read = (page) => readFileSync(new URL(page, docs), "utf8"); const layout = read("reference/react-layout.md"); assertStates(layout, "docs/reference/react-layout.md", `first ${HELP_LIST} questions`); assertStates(layout, "docs/reference/react-layout.md", `1 to ${OWN_LIMITS.id} letters`); assertStates(layout, "docs/reference/react-layout.md", `at most ${OWN_LIMITS.question} characters`, `at most ${OWN_LIMITS.answer.toLocaleString("en-US")}`); assertStates(layout, "docs/reference/react-layout.md", `at most ${OWN_LIMITS.group} characters`); assertStates(layout, "docs/reference/react-layout.md", "`target` is an id like `id`", `\`text\` has at most ${OWN_LIMITS.question}`); assertStates(layout, "docs/reference/react-layout.md", `At most ${OWN_LIMITS.count} own questions`); // every built-in id and every reserved one is listed assertStates(layout, "docs/reference/react-layout.md", "help's own parts", ...HELP_RESERVED.map((id) => `\`${id}\``)); assertStates(layout, "docs/reference/react-layout.md", ...HELP_TOPICS.map((t) => `\`${t.id}\``)); });