Files
hyperframes/packages/cli/src/capture/lazyScrollForCapture.test.ts
T
Xuanru LiandCursor 43dba22057 fix(capture): bound scroll/evaluate timeouts so heavy pages keep capturing (#3236)
* fix(capture): bound scroll/evaluate timeouts so heavy pages keep capturing

Fonts/Vercel-class sites were failing after navigation when a single in-page
scroll/evaluate hung until protocolTimeout. Drive lazy scroll from Node,
degrade on evaluate timeouts, and stop labeling those failures as bot blocks.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(capture): enforce stage budgets and surface evaluate degradation

Bound each scroll/content evaluate with remaining stage time so a wedged
page.evaluate cannot outlive the advertised 15s/8s budgets, skip recovery
CDP calls after expiry, and return/propagate timed-out animation and
screenshot work so caller warnings are reachable.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(capture): prefer TimeoutError instanceof for timeout classification

Use puppeteer-core TimeoutError as the primary signal for navigation vs
evaluate/protocol timeouts, with message checks only as a fallback for
string formatting and non-TimeoutError cases.

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-11 18:26:22 -07:00

62 lines
2.1 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import { lazyScrollForCapture } from "./lazyScrollForCapture.js";
describe("lazyScrollForCapture", () => {
it("no-ops when budget is empty", async () => {
const evaluate = vi.fn();
const result = await lazyScrollForCapture({ evaluate }, 0);
expect(result).toEqual({ steps: 0, timedOut: false, degraded: false });
expect(evaluate).not.toHaveBeenCalled();
});
it("scrolls in node-driven steps until the bottom", async () => {
const heights = [{ atBottom: false }, { atBottom: false }, { atBottom: true }];
const evaluate = vi.fn(async (expr: string) => {
if (expr.includes("atBottom")) {
return heights.shift() ?? { atBottom: true };
}
return 0;
});
const sleep = vi.fn(async () => undefined);
const result = await lazyScrollForCapture({ evaluate }, 15_000, { stepDelayMs: 10, sleep });
expect(result.steps).toBe(3);
expect(result.timedOut).toBe(false);
expect(result.degraded).toBe(false);
});
it("returns within the stage budget when evaluate never resolves", async () => {
const evaluate = vi.fn(() => new Promise(() => undefined));
const started = Date.now();
const warnings: string[] = [];
const result = await lazyScrollForCapture({ evaluate }, 25, {
onWarning: (message) => warnings.push(message),
sleep: async () => undefined,
});
expect(result.degraded).toBe(true);
expect(result.timedOut).toBe(true);
expect(Date.now() - started).toBeLessThan(250);
expect(warnings[0]).toMatch(/lazy-scroll evaluate timed out/i);
});
it("does not issue a recovery evaluate after the budget is exhausted", async () => {
let now = 1_000_000;
const dateSpy = vi.spyOn(Date, "now").mockImplementation(() => now);
const evaluate = vi.fn(() => {
now += 100;
return new Promise(() => undefined);
});
try {
const result = await lazyScrollForCapture({ evaluate }, 30, { sleep: async () => undefined });
expect(result.degraded).toBe(true);
expect(evaluate).toHaveBeenCalledTimes(1);
} finally {
dateSpy.mockRestore();
}
});
});