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
1 change: 1 addition & 0 deletions skills/renderdiff/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
27 changes: 22 additions & 5 deletions src/capture.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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<Page, { open: Set<Request>; last: number; failed: Set<string> }>();
/** Each page's last main-frame document response; redirect hops come first, so it is the final one. */
const doc = new WeakMap<Page, Response>();

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<Page, string>();

Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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 {
Expand Down
Binary file added tests/fixtures/shared/doc.pdf
Binary file not shown.
1 change: 1 addition & 0 deletions tests/helpers/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const CONTENT_TYPES: Record<string, string> = {
".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. */
Expand Down
15 changes: 15 additions & 0 deletions tests/visual/guardrails.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <body> is refused at once, not after a wait for one", async () => {
const start = Date.now();
const run = await capture(["/frameset"]);
Expand Down
Loading