Annotate Wikipedia articles with the same keys as nodes

An article's text is drawn from its sanitised HTML as selectable text
(articleText.ts): paragraphs, list items and quotes with their inline
elements; a formula is one unit; tables and preformatted blocks are shown
but carry no offsets. The selection ladder, Tab, Enter, e d y x c :, D and
the rest go through the same store paths as on a node; a note on article
words is a note (Enter steps into it; c, f, u, the editor and history work
on it as on any other) and shows wherever the article is opened, in the
library's own note style. Detached annotations are listed at the top of
their section with a link to the old revision. Keys that only make sense on
a node say why in the status line. Tab on a link now selects its words, so
Esc widens before h leaves, as in a node.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014yB6q4BHMwptTX3YFtkRab
This commit is contained in:
thalladaandClaude Opus 5.5 committed 2026-11-15 21:53:00 -05:00
1 parent f63fc10dc0
commit 2c1ee048a4
18 files changed
+1134 -126

No files matched your search

+215
View File
@@ -0,0 +1,215 @@
/**
* Annotating Wikipedia articles, keyboard first, against the mock Wikipedia and the mock model:
* words of an article are selected with the same keys as words of a node, a lens writes a note on
* them, the note belongs to the article (it shows wherever the article is opened), and it is found
* again in a later revision, or listed as detached when its words are gone.
*/
import { expect, test, type Page } from "@playwright/test";
import { command, idle, jump, keys, seedLinks, selected, settle, state } from "./helpers";
const panes = (page: Page) => page.locator(".strip > .pane");
async function wide(page: Page, path = "/doc/fixed-points") {
await page.setViewportSize({ width: 1920, height: 1080 });
await page.goto(path);
await page.waitForSelector(".strip > .pane");
await page.evaluate(() => document.fonts.ready);
}
async function column(page: Page, path: string) {
await page.setViewportSize({ width: 1440, height: 900 });
await page.goto(path);
await page.waitForFunction(() => (globalThis as any).__ie?.ready);
}
async function tabTo(page: Page, words: string) {
for (let i = 0; i < 16; i++) {
await page.keyboard.press("Tab");
if ((await selected(page)) === words || (await articleSelection(page)) === words) return;
}
throw new Error(`Tab never reached “${words}”`);
}
/** The words selected in an article's text. */
const articleSelection = (page: Page) => state(page, (s) => s.selectedText() as string | null);
/** The library's annotations: their words and text. */
const annotations = (page: Page) => state(page, (s) => Object.values(s.snap.nodes).filter((n: any) => n.wanchor).map((n: any) => `${n.wanchor.q} | ${n.body}`));
test.describe.configure({ mode: "serial" });
test.beforeAll(async ({ request }) => {
await seedLinks(request);
});
test.beforeEach(async ({ context }) => {
await context.route(/wikipedia\.org|wikimedia\.org/, (r) => r.abort());
});
test.afterEach(({ page }) => settle(page));
test("words of an article are selected with the node keys, and a lens writes a note on them that the library keeps", async ({ page }) => {
await wide(page);
const docs = await state(page, (s) => s.menuItems("").length);
await jump(page, "Recursion without names");
await tabTo(page, "fixed-point combinator");
await keys(page, "Enter");
const art = page.locator(".wiki-pane").last();
await expect(art.locator(".wiki-head h2")).toHaveText("Fixed-point combinator");
// i: the first sentence; i again: its first word; j moves by word, Esc widens back.
// (Pressed while the article is still arriving, i waits for it.)
await keys(page, "i");
await expect.poll(() => articleSelection(page)).toMatch(/^In mathematics and computer science, a fixed-point combinator .* of f: a value x with f\(x\) = x\.$/);
await expect(art.locator(".wiki-text .sel")).not.toHaveCount(0);
await keys(page, "i");
expect(await articleSelection(page)).toBe("In");
await keys(page, "j L");
expect(await articleSelection(page)).toBe("mathematics and");
await keys(page, "Escape");
// A sentence: y asks why, as a margin note set after its paragraph.
await keys(page, "j");
const sentence = await articleSelection(page);
expect(sentence).toMatch(/^In the untyped lambda calculus/);
await keys(page, "y");
await idle(page);
const note = art.locator(".wiki-text .inset");
await expect(note).toContainText("through the why lens");
expect(await note.evaluate((el) => getComputedStyle(el).fontFamily)).toContain("Serif");
// Tab reaches links and the note's words; d on a link's words writes a gloss.
await keys(page, "Escape");
await tabTo(page, "Haskell Curry");
await expect(art.locator("a.wfocus")).toHaveText("Haskell Curry");
await keys(page, "d");
await idle(page);
await expect(art.locator(".wiki-text .gloss")).toContainText("mock gloss of Haskell Curry");
await expect(art.locator(".wiki-text .anchor.a-gloss")).toHaveText("Haskell Curry");
expect((await annotations(page)).sort()).toEqual([`Haskell Curry | mock gloss of Haskell Curry`, `${sentence} | Mock note on “${sentence!.slice(0, 47)}…” through the why lens. It is two sentences long.`].sort());
// The article's node is in the library's Wikipedia container, which is not a document.
expect(await state(page, (s) => s.menuItems("").length)).toBe(docs);
expect(await state(page, (s) => s.menuItems("").some((d: any) => d.title === "Wikipedia"))).toBe(false);
// Keys that only make sense on a node say why not.
await keys(page, "Escape Escape");
await keys(page, "c");
await expect(page.locator(".status .message")).toContainText("cannot be edited");
await keys(page, "r");
await expect(page.locator(".status .message")).toContainText("shown as it is");
});
test("an annotation is a note: Enter steps into it, and it is edited, undone, deleted and given to the editor like any other", async ({ page }) => {
await wide(page);
await jump(page, "Recursion without names");
await tabTo(page, "fixed-point combinator");
await keys(page, "Enter");
await keys(page, "i");
await tabTo(page, "Haskell Curry");
const art = page.locator(".wiki-pane").last();
// Its words carry the dotted underline; e opens and closes it, as on a node; Enter opens it, and Enter again steps into it.
await expect(art.locator(".wiki-text .anchor.a-gloss")).toHaveText("Haskell Curry");
await expect(art.locator(".wiki-text .gloss")).toHaveCount(0);
await keys(page, "e");
await expect(art.locator(".wiki-text .gloss")).toHaveCount(1);
await keys(page, "e");
await expect(art.locator(".wiki-text .gloss")).toHaveCount(0);
await keys(page, "Enter Enter");
const id = await state(page, (s) => s.focus.node);
expect(await state(page, (s) => !!s.snap.nodes[s.focus.node].wanchor && s.aux === null)).toBe(true);
await expect(art.locator(".wiki-text .gloss.focused")).toHaveCount(1);
// c edits it in place.
await keys(page, "c");
await page.keyboard.press("Control+a");
await page.keyboard.type("the logician the Y combinator is named after");
await keys(page, "Escape");
await expect(art.locator(".wiki-text .gloss")).toContainText("the logician the Y combinator is named after");
await keys(page, "u");
await expect(art.locator(".wiki-text .gloss")).toContainText("mock gloss of Haskell Curry");
// The editor agent works on it like on any node.
await command(page, 'rename to "Curry"');
await idle(page);
expect(await state(page, (s) => s.snap.nodes[s.focus.node]?.title)).toBe("Curry");
// h returns to its words; D there deletes it, u brings it back.
await keys(page, "h");
expect(await articleSelection(page)).toBe("Haskell Curry");
await keys(page, "D");
expect(await page.evaluate((x) => !!(globalThis as any).__ie.snap.nodes[x], id)).toBe(false);
await expect(art.locator(".wiki-text .gloss")).toHaveCount(0);
await expect(art.locator(".wiki-text .anchor.a-gloss")).toHaveCount(0);
await keys(page, "u");
await expect(art.locator(".wiki-text .anchor.a-gloss")).toHaveText("Haskell Curry");
});
test("an article's annotations show wherever it is opened: from another link, from another article, by its address", async ({ page }) => {
// Annotate Lambda calculus under the note that links to it (the article in full beneath the note).
await wide(page);
await jump(page, "Recursion without names");
await tabTo(page, "lambda calculus");
await keys(page, "Enter j");
const under = panes(page).last().locator(".wiki.embedded");
await expect(under.locator(".wiki-head h2")).toHaveText("Lambda calculus");
await keys(page, "i");
await expect.poll(() => articleSelection(page)).toMatch(/^Lambda calculus \(also written λ-calculus\) is a formal system/);
await keys(page, "x");
await idle(page);
await expect(under.locator(".wiki-text .inset")).toContainText("through the example lens");
// From another document's link to it.
await settle(page);
await wide(page, "/doc/what-a-proof-is");
await jump(page, "Proofs as programs");
await tabTo(page, "a function turning proofs");
await keys(page, "Enter");
await expect(page.locator(".wiki-pane").last().locator(".wiki-head h2")).toHaveText("Lambda calculus");
await expect(page.locator(".wiki-pane").last().locator(".wiki-text .inset")).toContainText("through the example lens");
// From another article's link to it.
await jump(page, "Recursion without names");
await tabTo(page, "fixed-point combinator");
await keys(page, "Enter");
await tabTo(page, "lambda calculus");
await keys(page, "Enter");
await expect(page.locator(".wiki-pane").last().locator(".wiki-head h2")).toHaveText("Lambda calculus");
await expect(page.locator(".wiki-pane").last().locator(".wiki-text .inset")).toContainText("through the example lens");
// By its address, as the column's article page.
await settle(page);
await column(page, "/w/en/Lambda_calculus");
await expect(page.locator(".wiki-page .wiki-text .inset")).toContainText("through the example lens");
});
test("in a new revision an annotation is found where its words moved; one whose words are gone is kept as detached", async ({ page }) => {
await column(page, "/w/en/Octopus");
const pageEl = page.locator(".wiki-page");
const openSection = async (title: string) => {
const focused = () => page.evaluate(() => document.querySelector(".wiki-page .wrow.focused > .row .title")?.textContent ?? "");
await expect(pageEl.locator(".wrow").first()).toBeVisible();
for (let i = 0; i < 12 && (await focused()) !== title; i++) await keys(page, "j");
await keys(page, "l");
};
// A note on a sentence of Description, and one on a sentence of Intelligence.
await openSection("Description");
await keys(page, "i j");
expect(await articleSelection(page)).toBe("Its skin contains specialised cells that let it change colour and texture within a fraction of a second.");
await keys(page, "y");
await idle(page);
await keys(page, "Escape Escape");
await openSection("Intelligence");
await keys(page, "i j");
expect(await articleSelection(page)).toBe("In laboratory studies they have solved mazes, unscrewed jar lids and learned by watching other octopuses.");
await keys(page, "x");
await idle(page);
await expect(pageEl.locator(".wiki-text .inset")).toHaveCount(2);
const rev = await state(page, (s) => (Object.values(s.snap.nodes) as any[]).find((n) => n.wanchor?.q.startsWith("Its skin")).wanchor.rev as number);
await settle(page);
// The next revision: the Intelligence paragraph moved into Description and edited around the
// annotated sentence, and the skin sentence rewritten away.
await page.route(/\/api\/wiki\/article\?title=Octopus/, async (route) => {
const res = await route.fetch();
const body = await res.json();
const a = body.article;
a.revisionId += 1;
const desc = a.sections.find((x: any) => x.heading === "Description");
const intel = a.sections.find((x: any) => x.heading === "Intelligence");
desc.html = desc.html.replace(/Its skin contains specialised cells[^.]*\./, "Its skin can change colour in an instant.") + intel.html.replace("Some have been seen carrying coconut shells to use later as shelter.", "Some carry coconut shells about.").replace("widely regarded as", "often called");
intel.html = "<p>See above.</p>";
await route.fulfill({ response: res, json: body });
});
await page.reload();
await page.waitForFunction(() => (globalThis as any).__ie?.ready);
await openSection("Description");
const desc = pageEl.locator(".wrow.open").first();
await expect(desc.locator(".wdetached .inset")).toContainText("Its skin contains specialised cells");
await expect(desc.locator(".wdetached a")).toHaveAttribute("href", new RegExp(`oldid=${rev}$`));
await expect(desc.locator(".wiki-text .anchor")).toHaveText("In laboratory studies they have solved mazes, unscrewed jar lids and learned by watching other octopuses.");
await expect(desc.locator(".wiki-text .inset")).toContainText("through the example lens");
// Nothing was lost or rewritten: both annotations are still in the library as they were.
expect((await annotations(page)).filter((a) => /^(Its skin|In laboratory)/.test(a))).toHaveLength(2);
});
+1 -1
View File
@@ -103,7 +103,7 @@ test("past the pre-generated text, a selection is expanded by the model, in a fo
await expect(page.locator(".inset").last()).toContainText("through the how does this fail? lens");
const notes = await state(page, (s) =>
Object.values(s.snap.nodes).filter((c: any) => c.kind === "note" && /^[Mm]ock/.test(c.body)).map((c: any) => c.form).sort(),
Object.values(s.snap.nodes).filter((c: any) => c.kind === "note" && !c.wanchor && /^[Mm]ock/.test(c.body)).map((c: any) => c.form).sort(),
);
expect(notes).toEqual(["gloss", "note", "passage"]);
+11 -5
View File
@@ -139,17 +139,19 @@ test("panes: Tab and Enter open an article in the next pane; its sections open a
await expect(sec.locator(".wattr")).toBeVisible();
await keys(page, "l");
expect(await state(page, (s) => s.aux)).toMatchObject({ kind: "wiki", host: "p4", row: { t: "head" } });
// Tab reaches the section's link to Lambda calculus; Enter opens it as a further pane.
// Tab selects the section's link to Lambda calculus, as words of a node; Enter opens it as a further pane.
await keys(page, "Tab");
await expect(sec.locator("a.wfocus")).toHaveText("lambda calculus");
expect(await state(page, (s) => s.selectedText())).toBe("lambda calculus");
await keys(page, "Enter");
await expect(panes(page)).toHaveCount(6);
await expect(panes(page).last().locator(".wiki-head h2")).toHaveText("Lambda calculus");
// Back left, step by step, to the words the trail started from; nothing to the right closes.
await keys(page, "h");
expect(await state(page, (s) => s.paneAt)).toBe(4);
// From the link it was opened from: off the link, up to the section's row, the article's head, the words.
await keys(page, "h h h h");
// From the link it was opened from: Esc widens the selection to its sentence, then to the text's
// head; h goes up to the section's row in the pane before, its head, the words.
await keys(page, "Escape Escape h h h");
expect(await selected(page)).toBe("fixed-point combinator");
expect(await trail(page)).toEqual(["Library", "Fixed points", "Recursion without names", "W:Fixed-point combinator", "W:Fixed-point combinator#2", "W:Lambda calculus"]);
await expect(panes(page)).toHaveCount(6);
@@ -167,7 +169,10 @@ test("panes: Tab and Enter open an article in the next pane; its sections open a
// Spines of covered article panes carry the W and the tint.
await keys(page, "j j l l Tab Enter");
await expect(panes(page).last().locator(".wiki-head h2")).toHaveText("Lambda calculus");
await keys(page, "Tab Enter");
// (Tab also stops on notes the library keeps on the article, if an earlier test wrote one.)
await expect(panes(page).last().locator(".wiki-text a[data-wiki]").first()).toBeVisible();
for (let i = 0; i < 4 && (await state(page, (s) => s.selectedText())) !== "Alonzo Church"; i++) await keys(page, "Tab");
await keys(page, "Enter");
await expect(panes(page).last().locator(".wiki-head h2")).toHaveText("Alonzo Church");
const spine = page.locator(".wiki-pane[data-covered] .spine").first();
await expect(spine.locator(".wmark")).toHaveText("W");
@@ -437,7 +442,8 @@ test("column: Enter on a link shows a preview, Enter again opens the article as
await expect(pageEl.locator(".wiki-head h2")).toHaveText("Lambda calculus");
await keys(page, "h");
await expect(pageEl.locator(".wiki-head h2")).toHaveText("Fixed-point combinator");
await keys(page, "h h h");
// Back on the link's words: Esc widens to the sentence, then to the section's row; h to the head, then out.
await keys(page, "Escape Escape h h");
await expect(page.locator(".wiki-page")).toHaveCount(0);
expect(await selected(page)).toBe("fixed-point combinator");
// Back works too.
+3 -3
View File
@@ -56,7 +56,7 @@ test.afterEach(({ page }) => settle(page));
test("the library is the first page: its documents are its sections, and opening one needs no closing step", async ({ page }) => {
await open(page, "/");
await expect(page.locator(".top h1")).toHaveText("Library");
const docs = await state(page, (s) => s.snap.nodes[s.snap.root].children.length);
const docs = await state(page, (s) => s.snap.nodes[s.snap.root].children.filter((c: string) => !s.snap.nodes[c].articles).length);
await expect(page.locator("article > .outline > .node")).toHaveCount(docs);
await expect(page.locator("article > .outline > .node.open")).toHaveCount(0);
await keys(page, "j Enter"); // the first document's row: it opens in place of the list
@@ -81,7 +81,7 @@ test("m opens the menu, in library order, and the title in the top bar opens it
await expect(panel.locator(".doc.current")).toContainText("Infinispand");
await expect(panel.locator(".doc.current")).toContainText(/open · (saved|saving…)/);
const listed = await panel.locator(".doc .title").allTextContents();
expect(listed).toEqual(await state(page, (s) => s.snap.nodes[s.snap.root].children.map((c: string) => s.snap.nodes[c].title)));
expect(listed).toEqual(await state(page, (s) => s.snap.nodes[s.snap.root].children.filter((c: string) => !s.snap.nodes[c].articles).map((c: string) => s.snap.nodes[c].title)));
await keys(page, "Escape Escape");
await expect(panel).toHaveCount(0);
await page.locator(".docbar .doc-title").click();
@@ -234,7 +234,7 @@ test("create, rename and duplicate from the menu", async ({ page }) => {
await expect(page).toHaveURL(/\/doc\/scratch-copy$/);
// The copy sits after the original in the library, and is one commit in its history.
expect(await state(page, (s) => s.commits()[0].message)).toBe("Duplicate “Scratch pad, renamed” as “Scratch copy”");
const order = await state(page, (s) => s.snap.nodes[s.snap.root].children.map((c: string) => s.snap.nodes[c].slug));
const order = await state(page, (s) => s.snap.nodes[s.snap.root].children.filter((c: string) => !s.snap.nodes[c].articles).map((c: string) => s.snap.nodes[c].slug));
expect(order.indexOf("scratch-copy")).toBe(order.indexOf("scratch-pad") + 1);
});
+1 -1
View File
@@ -33,7 +33,7 @@ test("everything is reachable with the mouse: titles, marked spans, and a dragge
expect(focus.text.length).toBeGreaterThan(3);
await page.keyboard.press("e");
await idle(page);
const made = await state(page, (s) => Object.values(s.snap.nodes).filter((n: any) => n.kind === "note" && /^[Mm]ock/.test(n.body)).map((n: any) => n.anchors[0].text));
const made = await state(page, (s) => Object.values(s.snap.nodes).filter((n: any) => n.kind === "note" && !n.wanchor && /^[Mm]ock/.test(n.body)).map((n: any) => n.anchors[0].text));
expect(made).toContain(focus.text);
});
+1 -1
View File
@@ -37,7 +37,7 @@ test("the library is the first pane, and a document opens in the next; reload an
await page.goto("/");
await page.waitForSelector(".strip > .pane");
expect(await paneTitles(page)).toEqual(["Library"]);
await expect(page.locator(".pane.root .rows > .node")).toHaveCount(await state(page, (s) => s.snap.nodes[s.snap.root].children.length));
await expect(page.locator(".pane.root .rows > .node")).toHaveCount(await state(page, (s) => s.snap.nodes[s.snap.root].children.filter((c: string) => !s.snap.nodes[c].articles).length));
await keys(page, "j l"); // the first document's row, then open it
expect(await paneTitles(page)).toEqual(["Library", "Infinispand"]);
await expect(page.locator(".pane.doc .abstract.seed.folded")).toBeVisible();