diff --git a/skills/renderdiff/reference.md b/skills/renderdiff/reference.md index fd83b0c..294546e 100644 --- a/skills/renderdiff/reference.md +++ b/skills/renderdiff/reference.md @@ -106,6 +106,7 @@ Report (`--open`, for a human): green from 99.5%, amber to 97%, red below. | Error | Cause and fix | | --- | --- | | `HTTP 404 at …` (any ≥ 400) | That side answered an error: a route missing on one origin, or a server not ready. | +| `not an HTML page: server sent … at …` | The route is a file (PDF, image, JSON, download), not a page. Drop it from the routes. Never retried. | | `page did not render on either side (…)` | Both sides under 200 chars of text and no visible image, SVG, canvas, video, iframe or form control. Usually a failed asset or an app that never mounted. One blank side isn't refused; it scores low. | | `page did not settle: Npx unstable at …` | Over 25% of the page kept changing. Mask or stop the moving part. | | `page is Npx tall at Wpx wide, past the …px sanity ceiling …` | Taller than 57,600,000px ÷ width (never under 40,000px): a runaway layout, not a page. | diff --git a/src/capture.ts b/src/capture.ts index b8ebb9e..26a5a0d 100644 --- a/src/capture.ts +++ b/src/capture.ts @@ -1,5 +1,5 @@ import { setTimeout as sleep } from "node:timers/promises"; -import type { Locator, Page, Request } from "playwright"; +import type { Locator, Page, Request, Response } from "playwright"; import sharp from "sharp"; import { label, MASK_COLOR, masksFor, type ResolvedConfig, type Route } from "./config.ts"; @@ -69,6 +69,14 @@ const MAX_PAGE_AREA = 1440 * 40_000; /** Requests still open per page, when the last one opened or closed, and stylesheets that failed. */ const network = new WeakMap; last: number; failed: Set }>(); +/** Each page's last main-frame document response; redirect hops come first, so it is the final one. */ +const doc = new WeakMap(); + +const html = (r: Response) => { + const type = r.headers()["content-type"]; + return !type || /^\s*(text\/html|application\/xhtml\+xml)\b/i.test(type); +}; + /** The step each page's capture is on, for the shot deadline to name. */ export const step = new WeakMap(); @@ -113,6 +121,7 @@ export async function prepare(page: Page, cfg: ResolvedConfig, now: number): Pro }); page.on("response", (r) => { if (r.status() >= 400) sheet(r.request()); + if (r.request().isNavigationRequest() && r.frame() === page.mainFrame()) doc.set(page, r); }); // A request the old document left open never finishes or fails, so it can't hold the next load. page.on("framenavigated", (f) => { @@ -167,13 +176,21 @@ export async function capture( await page.clock.setSystemTime(now); // Here, not on framenavigated, which a pushState during hydration fires too. network.get(page)!.failed.clear(); - const res = await page.goto(url, { - waitUntil: "domcontentloaded", - timeout: 45_000, - }); + doc.delete(page); + const res = await page + .goto(url, { waitUntil: "domcontentloaded", timeout: 45_000 }) + .catch((e: unknown) => { + const d = doc.get(page); + if (!d || html(d)) throw e; + return d; + }); if (res && res.status() >= 400) { throw new Error(`HTTP ${res.status()} at ${url}`); } + if (res && !html(res)) { + const type = res.headers()["content-type"]!.split(";")[0]!.trim(); + throw new Error(`not an HTML page: server sent ${type} at ${url}`); + } if (route.before) { step.set(page, "before hook"); try { diff --git a/tests/fixtures/shared/doc.pdf b/tests/fixtures/shared/doc.pdf new file mode 100644 index 0000000..7524650 Binary files /dev/null and b/tests/fixtures/shared/doc.pdf differ diff --git a/tests/helpers/server.ts b/tests/helpers/server.ts index 4d28f9c..c220693 100644 --- a/tests/helpers/server.ts +++ b/tests/helpers/server.ts @@ -35,6 +35,7 @@ const CONTENT_TYPES: Record = { ".png": "image/png", ".webm": "video/webm", ".html": "text/html; charset=utf-8", + ".pdf": "application/pdf", }; /** `/` -> `home`, `/flex-vs-grid` -> `flex-vs-grid`. Rejects anything with a separator. */ diff --git a/tests/visual/guardrails.test.ts b/tests/visual/guardrails.test.ts index 9066614..0bf4cc1 100644 --- a/tests/visual/guardrails.test.ts +++ b/tests/visual/guardrails.test.ts @@ -136,6 +136,21 @@ describe("the render gate", () => { assert.match(report.errors[0]!.message, /HTTP 404/); }); + test("a route that is not HTML is an error that names its content type", async () => { + const run = await capture(["/shared/doc.pdf", "/shared/img/azure.svg"]); + assert.equal(run.code, 3, context(run)); + const messages = parseReport(run.report()!).errors.map((e) => e.message); + assert.equal(messages.length, 2, context(run)); + assert.ok( + messages.some((m) => /not an HTML page: server sent application\/pdf at /.test(m)), + context(run), + ); + assert.ok( + messages.some((m) => /not an HTML page: server sent image\/svg\+xml at /.test(m)), + context(run), + ); + }); + test("a document with no is refused at once, not after a wait for one", async () => { const start = Date.now(); const run = await capture(["/frameset"]);