Skip to content

Commit ec3d2fd

Browse files
fix(server): viewport capture test waits for the requested size, not any resize
CI showed the capture failing in 166ms with metrics and frame agreeing at 1280x800, so the snapshot was right for the page it saw: the test's one-shot resize listener had fired before the window reached 390x844. Drop the capture retry and wait in the test until the layout reports the requested size. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 9f17f9e commit ec3d2fd

2 files changed

Lines changed: 7 additions & 34 deletions

File tree

‎apps/server/src/preview/ServerBrowserPage.test.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,13 @@ it("captures the native display viewport after page script reassigns the globals
409409
offset: { x: 0, y: 0 },
410410
});
411411
const { windowId } = await cdp.send("Browser.getWindowForTarget");
412-
// CDP acknowledges the bounds change before the renderer applies the resize.
412+
// CDP acknowledges the bounds change before the renderer applies it, and a resize event can
413+
// fire for a size on the way there. Page script replaced innerWidth, so read the layout.
413414
const resize = await page.evaluateHandle(`({
414-
done: new Promise(resolve => window.addEventListener("resize", () => resolve(), { once: true })),
415+
done: new Promise(resolve => window.addEventListener("resize", () => {
416+
const { clientWidth, clientHeight } = document.documentElement;
417+
if (clientWidth === 390 && clientHeight === 844) resolve();
418+
})),
415419
})`);
416420
await cdp.send("Browser.setWindowBounds", {
417421
windowId,

‎apps/server/src/preview/ServerBrowserPage.ts‎

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ const MAX_EVALUATION_BYTES = 64_000;
2626
const MAX_VISIBLE_TEXT_LENGTH = 20_000;
2727
const MAX_SCREENSHOT_WIDTH = 1280;
2828
const WAIT_POLL_MS = 100;
29-
const RESIZED_FRAME_MS = 2_000;
3029
export const DIAGNOSTIC_BUFFER_LIMIT = 200;
3130

3231
export class ServerBrowserOperationError extends Error {
@@ -485,36 +484,6 @@ const renderingOf = async (input: SnapshotInput) => {
485484
};
486485
};
487486

488-
// A desktop window reports a resize before its compositor draws a frame at the new size, so an
489-
// unclipped capture can still return the old frame. Bounded: a frame that never matches is kept.
490-
// Headless pages are sized by the server, which awaits each resize.
491-
const captureResized = async (
492-
input: SnapshotInput,
493-
options: {
494-
readonly scale: number;
495-
readonly viewport: Viewport;
496-
readonly offset: { readonly x: number; readonly y: number } | undefined;
497-
readonly renderScale: number;
498-
},
499-
deadline: number,
500-
) => {
501-
if (input.renderScale !== "display")
502-
return captureViewport(input.page, input.cdp, { format: "png", ...options });
503-
const expected = {
504-
width: options.viewport.width * options.renderScale * options.scale,
505-
height: options.viewport.height * options.renderScale * options.scale,
506-
};
507-
const giveUpAt = Math.min(Date.now() + RESIZED_FRAME_MS, deadline - 2_000);
508-
for (;;) {
509-
const data = await captureViewport(input.page, input.cdp, { format: "png", ...options });
510-
const size = pngSize(data);
511-
const matches =
512-
Math.abs(size.width - expected.width) <= 1 && Math.abs(size.height - expected.height) <= 1;
513-
if (matches || Date.now() + WAIT_POLL_MS >= giveUpAt) return data;
514-
await new Promise((resolve) => setTimeout(resolve, WAIT_POLL_MS));
515-
}
516-
};
517-
518487
const capture = async (
519488
input: SnapshotInput,
520489
deadline: number,
@@ -537,7 +506,7 @@ const capture = async (
537506
"url" | "title" | "loading" | "visibleText" | "interactiveElements"
538507
>
539508
>,
540-
captureResized(input, { scale, viewport, offset, renderScale }, deadline),
509+
captureViewport(input.page, input.cdp, { format: "png", scale, viewport, offset }),
541510
]);
542511
void pageAndScreenshot.catch(constVoid);
543512
// Leaves time for the second read; a frame that never loads holds the tree until then.

0 commit comments

Comments
 (0)