One component, PageIntro, for the text under a page's title: shown as it is when it fits in two lines, otherwise folded with a Show more, remembered per page in this browser, opened by tabbing to a link in it, unfolded in print. Used on 41 pages. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
69 lines
3.8 KiB
TypeScript
69 lines
3.8 KiB
TypeScript
/**
|
|
* The folded introductions under page titles: what is remembered about which ones the person has opened out, kept as a short list of page paths — and that every page that has such a text
|
|
* uses the one component for it, so they all fold the same way.
|
|
*
|
|
* Run with: npm test --workspace server
|
|
*/
|
|
import { test, describe } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { readdirSync, readFileSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
|
|
const { parseOpen, withOpen } = await import("../../web/src/utils/pageIntro.js");
|
|
|
|
describe("what is remembered", () => {
|
|
test("a list of paths as stored; anything else is an empty list", () => {
|
|
assert.deepEqual(parseOpen('["/duplicati","/technitium"]'), ["/duplicati", "/technitium"]);
|
|
for (const junk of [null, "", "nope", "{}", "5", '"x"']) assert.deepEqual(parseOpen(junk), [], String(junk));
|
|
assert.deepEqual(parseOpen('["/a", 5, null, "/b"]'), ["/a", "/b"], "what is not a path is dropped");
|
|
});
|
|
|
|
test("opening a page adds it once; folding it takes it away; the others are left as they were", () => {
|
|
const one = withOpen([], "/duplicati", true);
|
|
assert.deepEqual(one, ["/duplicati"]);
|
|
assert.deepEqual(withOpen(one, "/duplicati", true), ["/duplicati"], "not twice");
|
|
assert.deepEqual(withOpen(withOpen(one, "/technitium", true), "/duplicati", false), ["/technitium"]);
|
|
});
|
|
|
|
test("the list never grows past 200: the newest are kept", () => {
|
|
let list: string[] = [];
|
|
for (let i = 0; i < 250; i++) list = withOpen(list, `/page-${i}`, true);
|
|
assert.equal(list.length, 200);
|
|
assert.equal(list[0], "/page-50");
|
|
assert.equal(list.at(-1), "/page-249");
|
|
});
|
|
});
|
|
|
|
describe("the pages", () => {
|
|
const pages = join(import.meta.dirname, "..", "..", "web", "src", "pages");
|
|
const files = readdirSync(pages).filter((f) => f.endsWith(".tsx"));
|
|
const read = (f: string) => readFileSync(join(pages, f), "utf8");
|
|
|
|
test("the long introductions that were converted use the component, with its import, and no page is left with a long one in a bare paragraph", () => {
|
|
const using = files.filter((f) => read(f).includes("<PageIntro"));
|
|
assert.ok(using.length >= 38, `${using.length} pages use it`);
|
|
for (const f of using) assert.match(read(f), /import PageIntro from "\.\.\/components\/PageIntro";/, f);
|
|
// After a page's title, the first block of secondary text of any length (but a short note) is the component's.
|
|
const bare: string[] = [];
|
|
for (const f of files) {
|
|
const s = read(f).replace(/\r\n/g, "\n");
|
|
const t = s.indexOf("page-title");
|
|
if (t < 0 || s.slice(t, t + 3500).includes("<PageIntro")) continue;
|
|
const m = /<(p|div) className="(?:[^"]*\s)?text-secondary(?:\s[^"]*)?">([\s\S]*?)<\/\1>/.exec(s.slice(t, t + 3500));
|
|
const text = m ? m[2].replace(/<[^>]+>/g, "").replace(/\{[^}]*\}/g, "x").replace(/\s+/g, " ").trim() : "";
|
|
if (text.length >= 200 && !/card-body/.test(m![0].slice(0, 80))) bare.push(`${f} (${text.length})`);
|
|
}
|
|
// The few that follow their title with a note of another kind (a card, a field's hint) are not introductions.
|
|
assert.deepEqual(bare.filter((b) => !/^(Ipam|Servers|InventoryLabels|Privacy)\.tsx/.test(b)), []);
|
|
});
|
|
|
|
test("the component keeps all of the text (it is folded with the styles, never cut from the page) and is printed unfolded", () => {
|
|
const component = readFileSync(join(import.meta.dirname, "..", "..", "web", "src", "components", "PageIntro.tsx"), "utf8");
|
|
assert.match(component, /aria-expanded=\{open\}/);
|
|
assert.match(component, /\{children\}/);
|
|
const css = readFileSync(join(import.meta.dirname, "..", "..", "web", "src", "styles.css"), "utf8");
|
|
assert.match(css, /\.page-intro-clamp[\s\S]*?-webkit-line-clamp:\s*2/);
|
|
assert.match(css, /@media print[\s\S]*?\.page-intro-clamp[\s\S]*?overflow:\s*visible/);
|
|
});
|
|
});
|