/** server/pageHtml.ts: metadata, container, units, sections, sanitising, figures; the fixtures of server/pageMock.ts through the real client. */ import fc from "fast-check"; import { parseFragment } from "parse5"; import { describe, expect, it } from "vitest"; import { PageClient, type WebArticle } from "../server/page"; import { embedOf, embedSrc, validId } from "../src/core/video"; import { extractPage, firstSentence, videoOf, type Extraction, type Plan } from "../server/pageHtml"; import { MOCK_PAGES, MOCK_VIMEO_ID, MOCK_YOUTUBE_ID, makeMockPageFetch, prose } from "../server/pageMock"; import { textOf } from "../server/wikiHtml"; const client = () => new PageClient({ fetch: makeMockPageFetch(), spacingMs: 0, persist: false }); async function article(url: string): Promise { const r = await client().article(url); if (!r.ok) throw new Error(`${url}: ${r.reason}`); return r.value; } const doc = (body: string, head = "") => `${head}${body}`; const ok = (e: Extraction) => { if (!e.ok) throw new Error(`${e.reason}: ${e.message}`); return e; }; const flat = (html: string) => parseFragment(html).childNodes.map(textOf).join("").replace(/\s+/g, ""); /** Every unit once, in order, across title, lead, sections and dropped; and the html shows exactly the units' text. */ function checkInvariant(e: Extraction & { ok: true }) { const p: Plan = e.plan; const all = [...(p.title !== null ? [p.title] : []), ...p.lead, ...p.sections.flatMap((s) => [...(s.heading !== null ? [s.heading] : []), ...s.body]), ...p.dropped]; expect([...all].sort((a, b) => a - b)).toEqual(p.units.map((_, i) => i)); const shown = [...p.lead, ...p.sections.flatMap((s) => [...(s.heading !== null ? [s.heading] : []), ...s.body])]; expect(shown).toEqual([...shown].sort((a, b) => a - b)); const text = (idx: number[]) => idx.map((i) => p.units[i].text).join("").replace(/\s+/g, ""); const desc = e.leadFrom === "page-description" ? (e.meta.description ?? "").replace(/\s+/g, "") : ""; expect(flat(e.lead)).toBe(desc + text(p.lead)); for (const [k, s] of p.sections.entries()) { expect(flat(e.sections[k].html)).toBe(text(s.body)); if (s.heading !== null) expect(e.sections[k].heading).toBe(p.units[s.heading].text); } } describe("the fixtures", () => { it("h3-only blog post: headings outline, meta author,