Files
explicata/tests/articleText.test.ts
T
thalladaandClaude Fable 5.2 f8c6576ea1 Web pages: a link to any address opens as an article, as Wikipedia's do
A url link now opens in the app instead of the browser. The server fetches
the page (robots.txt honoured, public addresses only, size and time caps),
keeps a snapshot on disk until it is reloaded, and cuts it into a lead and
sections: by its headings, else by pseudo-headings, else in chunks, or by a
model when asked (s), within a daily budget. The pane has its own amber
ground, the author and date in its head when the page gives them, and the
source in its foot. W opens the original, R reads it again. See
docs/pages.md.

Pictures and videos are kept, in web pages and in Wikipedia articles:
pictures through the server (only addresses seen in a page that was read),
videos as a still that loads the player on a click.

A section that is only a link (a target, no text, no sections) opens as
its article at once, and L opens a target to the right of the node's own
pane, never under it.

Article text is sized to read like the reader's own.

Panes: a pane that already has all its column can give beside the others'
tabs no longer peeks over them on hover, and the curves of tabs under a
peeking pane are no longer drawn over it. The keypress timing test judges
each of its two passes on its own, so a stall from outside the app does
not fail it.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014yB6q4BHMwptTX3YFtkRab
2026-11-18 13:44:13 -05:00

108 lines
6.9 KiB
TypeScript

/**
* Article HTML as selectable text (src/ui/articleText.ts), on the mock articles as the server
* sanitises them, parsed with parse5 in place of the browser's DOMParser: what is text, what is
* not, links and formulas, and that every sentence anchors and re-anchors to itself.
*/
import { parseFragment, serializeOuter, type DefaultTreeAdapterTypes as T } from "parse5";
import { describe, expect, it } from "vitest";
import { makeAnchor, reanchor } from "../src/core/article";
import { sentences } from "../src/core/text";
import { buildText, type Block, type DomLike } from "../src/ui/articleText";
import { WikiClient } from "../server/wiki";
import { mockWikiFetch } from "../server/wikiMock";
/** A parse5 node as the little of the DOM the walk uses. */
function adapt(n: T.Node): DomLike {
if (n.nodeName === "#text") return { nodeType: 3, data: (n as T.TextNode).value, textContent: (n as T.TextNode).value, childNodes: [] };
if (!("tagName" in n)) return { nodeType: 8, textContent: "", childNodes: "childNodes" in n ? (n as T.ParentNode).childNodes.map(adapt) : [] };
const el = n as T.Element;
const text = (x: T.Node): string => (x.nodeName === "#text" ? (x as T.TextNode).value : "childNodes" in x ? (x as T.Element).childNodes.map(text).join("") : "");
return { nodeType: 1, localName: el.tagName, textContent: text(el), childNodes: el.childNodes.map(adapt), attributes: el.attrs, outerHTML: serializeOuter(el) };
}
const build = (html: string) => buildText({ nodeType: 11, textContent: "", childNodes: parseFragment(html).childNodes.map(adapt) });
async function article(title: string) {
const r = await new WikiClient({ fetch: mockWikiFetch, spacingMs: 0, persist: false, offlineHoldMs: 0 }).article(title, { sections: true });
if (!r.ok) throw new Error(r.message);
return r.value;
}
const kinds = (bs: Block[]): string[] => bs.flatMap((b) => (b.k === "el" ? [b.tag, ...kinds(b.kids)] : [b.k]));
describe("article text", () => {
it("is the paragraphs' visible text, links as ranges of it, a formula as one run", async () => {
const a = await article("Fixed-point combinator");
const lead = build(a.lead);
expect(lead.text).toMatch(/^In mathematics and computer science, a fixed-point combinator \(or fixpoint combinator\) is a higher-order function that, given a function f, returns a fixed point of f: a value x with f\(x\) = x\. In the untyped lambda calculus/);
expect(lead.text).not.toMatch(/ {2}|\n(?!\n)|\[1\]/);
expect(lead.links.map((l) => [lead.text.slice(...l.range), l.title])).toEqual([
["lambda calculus", "Lambda calculus"],
["Haskell Curry", "Haskell Curry"],
["recursion", "Recursion (computer science)"],
]);
const y = build(a.sections.find((s) => s.heading === "Y combinator")!.html);
const runs = y.blocks.flatMap((b) => (b.k === "para" ? b.runs : []));
const math = runs.filter((r) => r.math);
expect(math).toHaveLength(1);
expect(y.text.slice(math[0].s, math[0].e)).toBe("Y=λf.(λx.f(xx))(λx.f(xx))");
expect(math[0].math).toMatch(/^<math/);
// Code and italics are inline runs of the same text.
expect(runs.some((r) => r.marks.some((m) => m.tag === "code") && y.text.slice(r.s, r.e) === "(λx.x x)(λx.x x)")).toBe(true);
});
it("makes a paragraph of each list item; a table is shown but carries no text", async () => {
const m = build((await article("Mercury")).lead);
expect(kinds(m.blocks)).toEqual(["para", "ul", "li", "para", "li", "para", "li", "para"]);
expect(m.text.split("\n\n")).toEqual(["Mercury commonly refers to:", "Mercury (planet), the planet closest to the Sun", "Mercury (element), a metallic chemical element with the symbol Hg", "Mercury (mythology), a Roman god"]);
const t = build(`<p>Before.</p><table><tbody><tr><td>cell text</td></tr></tbody></table><p>After <i>it</i>.</p><pre>code block</pre>`);
expect(t.text).toBe("Before.\n\nAfter it.");
expect(kinds(t.blocks)).toEqual(["para", "raw", "para", "raw"]);
});
it("keeps every paragraph's offsets in its runs, and every sentence of every section anchors back to itself", async () => {
for (const title of ["Fixed-point combinator", "Octopus", "Hoyle state", "Mercury", "Carbon-12"]) {
const a = await article(title);
const secs = [{ path: [] as string[], html: a.lead }, ...a.sections.map((s) => ({ path: [s.heading], html: s.html }))].map((s) => ({ path: s.path, t: build(s.html) }));
for (const [i, s] of secs.entries()) {
const paras = s.t.blocks.flatMap(function all(b: Block): Block[] {
return b.k === "el" ? b.kids.flatMap(all) : [b];
}).filter((b) => b.k === "para");
for (const p of paras) if (p.k === "para") expect(p.runs.map((r) => s.t.text.slice(r.s, r.e)).join("")).toBe(s.t.text.slice(p.s, p.e));
for (const r of sentences(s.t.text)) {
const got = reanchor(makeAnchor(s.t.text, r, s.path, a.revisionId), secs.map((x) => ({ path: x.path, text: x.t.text })));
expect(got).toEqual({ sec: i, range: r });
}
}
}
});
it("an annotation made on an article's text before pictures were kept lands on the same words once its figures' captions are text too", async () => {
// Grey heron as the sanitiser now makes it, and as the old one did (the same text, no figures).
const a = await article("Grey heron");
const parts = [{ path: [] as string[], html: a.lead }, ...a.sections.map((s) => ({ path: [s.heading], html: s.html }))];
const now = parts.map((p) => ({ path: p.path, t: build(p.html) }));
const before = parts.map((p) => ({ path: p.path, t: build(p.html.replace(/<figure[^]*?<\/figure>/g, "").replace(/<p>Herons at the weir<\/p>/, "")) }));
expect(now[0].t.text.startsWith("An adult at a weir\n\nThe grey heron")).toBe(true);
expect(before[0].t.text.startsWith("The grey heron")).toBe(true);
const secsNow = now.map((x) => ({ path: x.path, text: x.t.text }));
let checked = 0;
for (const [i, s] of before.entries()) {
// Every sentence, and a word the captions also use ("weir", "heron"), wherever it occurs.
const ranges = [...sentences(s.t.text), ...[...s.t.text.matchAll(/\b(weir|heron)\b/g)].map((m): [number, number] => [m.index!, m.index! + m[0].length])];
for (const r of ranges) {
const anchor = makeAnchor(s.t.text, r, s.path, a.revisionId);
const got = reanchor(anchor, secsNow);
if ("detached" in got) throw new Error(`detached: ${anchor.q}`);
expect(got.sec).toBe(i);
// The same words, in the same place: the text around them is the text that was around them.
const t = secsNow[got.sec].text;
expect(t.slice(...got.range)).toBe(anchor.q);
expect(t.slice(Math.max(0, got.range[0] - anchor.pre.length), got.range[0])).toBe(anchor.pre);
expect(t.slice(got.range[1], got.range[1] + anchor.post.length)).toBe(anchor.post);
checked++;
}
}
expect(checked).toBeGreaterThan(10);
});
});