Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 108 additions & 36 deletions apps/writer/scripts/browser-a11y-test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ assert.ok(["127.0.0.1", "localhost", "[::1]"].includes(new URL(base).hostname));
await mkdir("out", { recursive: true });
const browser = await chromium.launch({ executablePath: process.env.CHROME });
let failures = 0;
let audits = 0;
const wcag = ["wcag2a", "wcag2aa", "wcag21a", "wcag21aa", "wcag22a", "wcag22aa"];

async function audit(page, name) {
await page.evaluate(async () => {
Expand All @@ -19,17 +21,27 @@ async function audit(page, name) {
).map(animation => animation.finished.catch(() => {})));
});
await page.evaluate(axe.source);
const results = await page.evaluate(() => window.axe.run(document, {
const results = await page.evaluate(tags => window.axe.run(document, {
runOnly: { type: "tag", values: tags },
// This speech-input rule is opt-in in axe, including the production sweep.
rules: { "label-content-name-mismatch": { enabled: true } },
}));
await writeFile(`out/a11y-${name}.json`, JSON.stringify(results.violations, null, 2));
const serious = results.violations.filter(({ impact }) => impact === "serious" || impact === "critical");
if (serious.length) {
}), wcag);
audits++;
const contrastNeedsReview = results.incomplete.filter(({ id }) => id === "color-contrast");
// Axe leaves single characters (including real numeric badges) undecided.
// Use its measured ratio when it can identify both colors.
const shortText = contrastNeedsReview.flatMap(({ nodes }) => nodes.filter(node =>
node.any.some(({ data }) => data?.messageKey === "shortTextContent" &&
data.fgColor && data.bgColor && data.contrastRatio < Number.parseFloat(data.expectedContrastRatio))
));
await writeFile(`out/a11y-${name}.json`, JSON.stringify({ violations: results.violations, shortText, contrastNeedsReview }, null, 2));
const violations = results.violations.map(({ id, nodes }) => ({ id, targets: nodes.map(n => n.target) }));
if (shortText.length) violations.push({ id: "color-contrast (short text)", targets: shortText.map(n => n.target) });
if (violations.length) {
failures++;
await page.screenshot({ path: `out/a11y-${name}.png`, fullPage: true });
console.error(`FAIL ${name}: ${JSON.stringify(serious.map(v => ({ id: v.id, nodes: v.nodes.map(n => n.target) })))}`);
} else console.log(`PASS ${name}: no serious or critical axe violations`);
console.error(`FAIL ${name}: ${JSON.stringify(violations)}`);
} else console.log(`PASS ${name}: no WCAG AA axe violations`);
}

async function escapeTo(page, dialog, opener) {
Expand All @@ -41,8 +53,13 @@ async function escapeTo(page, dialog, opener) {
}

try {
for (const width of [375, 1280]) {
const context = await browser.newContext({ viewport: { width, height: 812 }, colorScheme: "light" });
for (const [width, colorScheme] of [390, 1280].flatMap(width => ["light", "dark"].map(theme => [width, theme]))) {
const scenario = `${width}-${colorScheme}`;
const context = await browser.newContext({
viewport: { width, height: width === 390 ? 844 : 900 },
colorScheme,
hasTouch: width === 390,
});
await context.route("**/*", route => new URL(route.request().url()).origin === origin ? route.continue() : route.abort());
const page = await context.newPage();
page.setDefaultTimeout(15_000);
Expand All @@ -51,14 +68,20 @@ try {
await page.getByRole("heading", { name: "Manuscripts", exact: true }).waitFor();
await page.evaluate(async () => (await import("/scripts/browser-a11y-fixture.tsx")).mountSignIn());
await page.getByRole("button", { name: "Sign in", exact: true }).waitFor();
await audit(page, `signin-${width}`);
await audit(page, `signin-${scenario}`);

await page.goto(`${base}?demo`);
await page.getByRole("heading", { name: "Manuscripts", exact: true }).waitFor();
await audit(page, `home-${width}`);
await audit(page, `home-${scenario}`);
const create = page.getByRole("button", { name: "New manuscript", exact: true });
await create.click();
await audit(page, `new-manuscript-${width}`);
await audit(page, `new-manuscript-${scenario}`);
await page.getByRole("button", { name: "Use a note you already have…" }).click();
const note = page.locator(".note-option").first();
await note.focus();
await page.keyboard.press("Enter");
assert.equal(await note.getAttribute("aria-pressed"), "true");
await audit(page, `adopt-note-${scenario}`);
await escapeTo(page, page.getByRole("dialog", { name: "New manuscript", exact: true }), create);

await page.locator(".manuscripts > li", { hasText: "Small Agents, Large Effects" }).locator("[data-path]").click();
Expand All @@ -68,7 +91,7 @@ try {
}, null, { timeout: 60_000 });
if (width < 900) {
assert.equal(await page.locator(".preview").evaluate(element => element.checkVisibility({ visibilityProperty: true })), false);
await audit(page, `editor-${width}`);
await audit(page, `editor-${scenario}`);
await page.getByRole("navigation", { name: "View", exact: true }).getByRole("button", { name: /Preview/ }).click();
}
await page.waitForFunction(() => {
Expand All @@ -77,27 +100,52 @@ try {
return snap?.artifact && !snap.compiling && Number(document.querySelector(".preview-pages")?.dataset.renderedRevision) === snap.artifactRevision && canvas?.checkVisibility({ visibilityProperty: true });
}, null, { timeout: 60_000 });
if (width < 900) {
await audit(page, `preview-${width}`);
await audit(page, `preview-${scenario}`);
await page.getByRole("navigation", { name: "View", exact: true }).getByRole("button", { name: /Sidebar/ }).click();
} else await audit(page, `editor-${width}`);
for (const name of ["Sources", "Comments"]) {
} else await audit(page, `editor-${scenario}`);
for (const name of ["Outline", "Sources", "Comments"]) {
await page.getByRole("tab", { name: new RegExp(name) }).click();
if (name === "Sources") await page.locator(".source-row").first().waitFor();
if (name === "Comments") await page.waitForFunction(() => window.writer.workspace.getSnapshot().commentsLoad.phase === "ready");
await audit(page, `${name.toLowerCase()}-${width}`);
await audit(page, `${name.toLowerCase()}-${scenario}`);
}
const outlineSearch = page.getByRole("searchbox", { name: "Search outline", exact: true });
await page.getByRole("tab", { name: "Outline", exact: true }).click();
await outlineSearch.fill("Darwin");
await page.locator(".search-hit").first().waitFor();
await audit(page, `outline-search-${scenario}`);
await outlineSearch.clear();
await page.getByRole("tab", { name: /Sources/ }).click();
const sourceSearch = page.locator(".sources input[type=search]");
await sourceSearch.fill("darwinFormation81");
await page.locator('.source-row[data-key="darwinFormation81"]').click();
await page.getByRole("textbox", { name: /^Page or locator for / }).waitFor();
await audit(page, `citation-details-${scenario}`);
await sourceSearch.clear();
await page.getByRole("tab", { name: /Comments/ }).click();

await page.getByRole("button", { name: "Split sidebar", exact: true }).click();
await audit(page, `split-sidebar-${width}`);
await audit(page, `split-sidebar-${scenario}`);
await page.getByRole("button", { name: "Close the lower panel", exact: true }).click();
if (width < 900) await page.getByRole("navigation", { name: "View", exact: true }).getByRole("button", { name: /Write/ }).click();
const settings = page.getByRole("button", { name: "Settings", exact: true });
if (width < 900) {
await page.getByRole("button", { name: "More", exact: true }).click();
await page.getByRole("menuitem", { name: "Settings", exact: true }).click();
} else await settings.click();
await audit(page, `settings-${width}`);
await audit(page, `settings-${scenario}`);
await escapeTo(page, page.getByRole("dialog", { name: "Manuscript settings", exact: true }), page.locator(".cm-content"));

const more = page.getByRole("button", { name: "More", exact: true });
await page.locator(".cm-content").focus();
await page.keyboard.press("Control+End");
await page.keyboard.press("Enter");
await page.keyboard.insertText("[@darwin");
const completion = page.locator(".cm-tooltip-autocomplete [role=option]").first();
await completion.waitFor();
await audit(page, `citation-completion-${scenario}`);
await page.keyboard.press("Escape");
await completion.waitFor({ state: "hidden" });
for (const name of ["Keyboard shortcuts", "Commands"]) {
if (width < 900) await page.keyboard.press(name === "Commands" ? "Control+k" : "Control+/");
else {
Expand All @@ -106,26 +154,23 @@ try {
}
const dialog = page.getByRole("dialog", { name: name === "Commands" ? "Writer commands" : name, exact: true });
await dialog.waitFor();
await audit(page, `${name.toLowerCase().replaceAll(" ", "-")}-${width}`);
await audit(page, `${name.toLowerCase().replaceAll(" ", "-")}-${scenario}`);
await escapeTo(page, dialog, width < 900 ? page.locator(".cm-content") : more);
}
await page.evaluate(() => {
const { workspace } = window.writer;
workspace.setBody(workspace.main, "# Export check\n\nMissing citation [@a11y-missing].\n");
});
let exportOpener;
if (width < 900) {
await more.click();
await page.getByRole("menuitem", { name: "PDF", exact: true }).click();
exportOpener = more;
} else {
exportOpener = page.getByRole("button", { name: "Export PDF", exact: true });
await exportOpener.click();
}
const exportMenu = width < 900 ? more : page.getByRole("button", { name: "Export formats" });
await exportMenu.click();
const word = page.getByRole("menuitem").filter({ has: page.getByText("Word (DOCX)", { exact: true }) });
await word.waitFor();
await audit(page, `export-menu-${scenario}`);
await word.click();
const exportReview = page.getByRole("dialog", { name: "Review export problems", exact: true });
await exportReview.waitFor({ timeout: 60_000 });
await audit(page, `export-review-${width}`);
await escapeTo(page, exportReview, exportOpener);
await audit(page, `export-review-${scenario}`);
await escapeTo(page, exportReview, exportMenu);
await page.evaluate(() => {
const { workspace, backend } = window.writer;
const snapshot = workspace.getSnapshot().records.get(workspace.main).snapshot;
Expand All @@ -134,11 +179,11 @@ try {
});
const compare = page.getByRole("button", { name: "Compare versions", exact: true });
await compare.click();
await audit(page, `compare-${width}`);
await audit(page, `compare-${scenario}`);
await escapeTo(page, page.getByRole("dialog", { name: "Compare conflicting versions", exact: true }), compare);
const home = page.getByRole("button", { name: "Manuscripts", exact: true });
await home.click();
await audit(page, `unsaved-changes-${width}`);
await audit(page, `unsaved-changes-${scenario}`);
await page.evaluate(() => {
const { workspace } = window.writer;
const retry = workspace.retrySave.bind(workspace);
Expand All @@ -152,12 +197,39 @@ try {
await page.waitForFunction(() => !document.querySelector('.compare-actions [aria-disabled="true"]'));
assert.ok(await retry.evaluate(el => el === document.activeElement), "A failed retry keeps focus");
await escapeTo(page, page.getByRole("dialog", { name: "Some changes are not saved", exact: true }), home);
console.log(`PASS ${width}px: dialogs close with Escape and restore focus`);
// The browser-local checkpoint survives a reload. Audit its home row,
// the editor banner, and the comparison before choosing a version.
await page.goto(`${base}?demo`);
await page.locator(".manuscripts > li", { hasText: "Patient Observation" }).locator("[data-path]").click();
await page.waitForFunction(() => window.writer?.workspace?.getSnapshot().phase === "ready");
await page.evaluate(async () => {
const { workspace } = window.writer;
const snap = workspace.getSnapshot().records.get(workspace.main).snapshot;
const { DraftStore } = await import("/src/workspace/drafts.ts");
new DraftStore("demo").write(workspace.main, {
version: 1, body: "# Recovered local draft\n", frontmatter: snap.frontmatter,
baseBody: snap.body, baseFrontmatter: snap.record.frontmatter,
revision: snap.record.revision, updated: new Date().toISOString(),
});
});
await page.goto(`${base}?demo`);
const drafts = page.getByRole("region", { name: "Unsaved drafts" });
await drafts.waitFor();
await audit(page, `offline-drafts-${scenario}`);
await drafts.getByRole("listitem").filter({ hasText: "Patient Observation" }).getByRole("button", { name: "Review", exact: true }).click();
const reviewDraft = page.getByRole("button", { name: "Review Patient Observation", exact: true });
await reviewDraft.waitFor();
await audit(page, `recovery-banner-${scenario}`);
await reviewDraft.click();
await page.getByRole("button", { name: "Restore local draft", exact: true }).waitFor();
await audit(page, `recovery-dialog-${scenario}`);
console.log(`PASS ${scenario}: dialogs close with Escape and restore focus`);
} catch (error) {
failures++;
await page.screenshot({ path: `out/a11y-failure-${width}.png`, fullPage: true });
console.error(`FAIL ${width}px: ${error.stack}`);
await page.screenshot({ path: `out/a11y-failure-${scenario}.png`, fullPage: true });
console.error(`FAIL ${scenario}: ${error.stack}`);
} finally { await context.close(); }
}
} finally { await browser.close(); }
console.log(`Audited ${audits} Writer surfaces across viewport and theme combinations`);
assert.equal(failures, 0, `${failures} accessibility checks failed`);
42 changes: 41 additions & 1 deletion apps/writer/scripts/browser-reliability-test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Run with the development server: node scripts/browser-reliability-test.mjs [base URL]
import assert from "node:assert/strict";
import { mkdirSync } from "node:fs";
import { execFileSync } from "node:child_process";
import { chromium } from "playwright";

const base = process.argv[2] ?? process.env.BASE ?? "http://127.0.0.1:5320/";
Expand Down Expand Up @@ -479,7 +480,7 @@ try {
assert.equal(await page.locator(".toast.tone-busy").count(), 0);
});

await scenario("new manuscripts can include an actionable worked example", async (page) => {
await scenario("the worked example exports its collection image and cited footnote to Word", async (page) => {
await page.goto(`${base}?demo`);
await page.getByRole("button", { name: "New manuscript" }).click();
await page.getByRole("textbox", { name: "Title", exact: true }).fill("[test] Worked example");
Expand All @@ -488,6 +489,45 @@ try {
await page.waitForFunction(() => window.writer?.workspace?.getSnapshot().phase === "ready");
const text = await page.evaluate(() => { const workspace = window.writer.workspace; return workspace.getSnapshot().records.get(workspace.main).snapshot.body; });
assert.match(text, /fig-example/); assert.match(text, /Add chapter/); assert.match(text, /A cited claim \[@/);

const png = Buffer.from("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==", "base64");
await page.evaluate(async bytes => {
const { backend, workspace } = window.writer;
// A collection client models the peer that adds the file. Export reads
// it through Writer's native descriptor and download checks.
const uploaded = await backend.fixture.client().files.upload("figures/example.png", new Uint8Array(bytes));
await uploaded.confirmed;
const snapshot = workspace.getSnapshot().records.get(workspace.main).snapshot;
const citekey = /A cited claim \[@([^,]+),/.exec(snapshot.body)[1];
workspace.setBody(workspace.main, `${snapshot.body}\nA worked note.[^example]\n\n[^example]: Footnote evidence [@${citekey}, p. 21].\n`);
}, [...png]);

const [outcome] = await Promise.all([
Promise.race([
page.waitForEvent("download", { timeout: 120_000 }).then(download => ({ download })),
page.getByRole("dialog", { name: "Review export problems", exact: true }).waitFor({ timeout: 120_000 })
.then(async () => ({ problems: await page.getByRole("dialog", { name: "Review export problems", exact: true }).innerText() })),
]),
format(page, "Word (DOCX)"),
]);
assert.ok(outcome.download, outcome.problems);
const path = "out/worked-example.docx";
await outcome.download.saveAs(path);
const part = name => execFileSync("unzip", ["-p", path, name]);
const relationships = part("word/_rels/document.xml.rels").toString();
const image = /relationships\/image" Id="([^"]+)" Target="(media\/[^"]+\.png)"/.exec(relationships);
assert.ok(image, "document contains an image relationship");
const doc = part("word/document.xml").toString();
assert.ok(doc.includes(`r:embed="${image[1]}"`));
assert.deepEqual(part(`word/${image[2]}`), png);
assert.match(doc, /w:footnoteReference/);
assert.match(doc, /Bibliography/);
const footnotes = part("word/footnotes.xml").toString();
assert.match(footnotes, /Footnote evidence/);
assert.match(footnotes, /21/);
assert.match(footnotes, /<w:hyperlink\b/);
assert.doesNotMatch(footnotes, /\[@|citation-not-found/);

});
} finally { await browser.close(); }
process.exitCode = failures ? 1 : 0;
Loading