Skip to content

Commit bbd6077

Browse files
committed
fix(cli): tolerate missing waitForNetworkIdle in consent settle
waitForPageReady called page.waitForNetworkIdle unconditionally; a Page-shaped double without it throws a synchronous TypeError that captureScrollScreenshots' catch-all reports as internal-error with zero screenshots, breaking the partial-budget contract upstream's coreExtractionPhase.test.ts exercises with a thin mock page. The idle wait is best-effort, so make the hook optional and swallow its failures; also only keep the readyState poll alive while the page affirmatively reports "loading" so a non-boolean result cannot burn the settle deadline.
1 parent ed8f377 commit bbd6077

2 files changed

Lines changed: 37 additions & 6 deletions

File tree

‎packages/cli/src/capture/navigateForCapture.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,30 @@ describe("waitForPageReady", () => {
252252
expect(evaluate).not.toHaveBeenCalled();
253253
expect(waitForNetworkIdle).not.toHaveBeenCalled();
254254
});
255+
256+
it("settles without waitForNetworkIdle on the page object", async () => {
257+
const evaluate = vi.fn(async () => false);
258+
259+
await expect(waitForPageReady({ evaluate })).resolves.toBeUndefined();
260+
expect(evaluate).toHaveBeenCalledTimes(1);
261+
});
262+
263+
it("stops polling when readyState does not evaluate to a boolean", async () => {
264+
vi.useFakeTimers();
265+
try {
266+
const waitForNetworkIdle = vi.fn(async () => undefined);
267+
const evaluate = vi.fn(async () => []);
268+
269+
const start = Date.now();
270+
const done = waitForPageReady({ waitForNetworkIdle, evaluate }, { timeoutMs: 15_000 });
271+
await vi.runAllTimersAsync();
272+
await done;
273+
274+
expect(evaluate).toHaveBeenCalledTimes(1);
275+
expect(Date.now() - start).toBeLessThan(15_000);
276+
expect(waitForNetworkIdle).toHaveBeenCalledTimes(1);
277+
} finally {
278+
vi.useRealTimers();
279+
}
280+
});
255281
});

‎packages/cli/src/capture/navigateForCapture.ts‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export async function navigateForCapture<TResponse>(
6363
}
6464

6565
export interface PageReadyPage {
66-
waitForNetworkIdle(options: {
66+
waitForNetworkIdle?(options: {
6767
idleTime: number;
6868
timeout: number;
6969
concurrency?: number;
@@ -83,10 +83,15 @@ export async function waitForPageReady(
8383
const { timeoutMs = 15_000, networkIdle = "always" } = options;
8484
const deadline = Date.now() + timeoutMs;
8585
const remaining = () => Math.max(0, deadline - Date.now());
86-
const idle = () =>
87-
page
88-
.waitForNetworkIdle({ idleTime: 500, timeout: remaining(), concurrency: 2 })
89-
.catch(() => {});
86+
const idle = async () => {
87+
try {
88+
await page.waitForNetworkIdle?.({
89+
idleTime: 500,
90+
timeout: remaining(),
91+
concurrency: 2,
92+
});
93+
} catch {}
94+
};
9095

9196
if (networkIdle === "always" && remaining() > 0) await idle();
9297

@@ -106,7 +111,7 @@ export async function waitForPageReady(
106111
]).finally(() => {
107112
if (timer !== undefined) clearTimeout(timer);
108113
});
109-
if (!loading) break;
114+
if (loading !== true) break;
110115
waitedOutLoad = true;
111116
if (remaining() > 0) {
112117
await new Promise((resolve) => setTimeout(resolve, Math.min(500, remaining())));

0 commit comments

Comments
 (0)