The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 00:45 UTC
fix(server): preview_evaluate accepts top-level await (#31925)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Nov 9, 2026 at 23:59 UTC
4f570443c68e0250ac66b123f44c5405ef963a4d
2 files changed
+121
-2
apps/server/src/preview/ServerBrowserPage.test.ts
+105
-1
@@ -5,7 +5,16 @@ import {
5
type CDPSession,
6
type Page,
7
} from "playwright-core";
8
-import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from "vite-plus/test";
8
+import {
9
+ afterAll,
10
+ afterEach,
11
+ beforeAll,
12
+ beforeEach,
13
+ describe,
14
+ expect,
15
+ it,
16
+ vi,
17
+} from "vite-plus/test";
18
19
import * as ServerBrowserPage from "./ServerBrowserPage.ts";
20
@@ -269,6 +278,67 @@ describe("server browser element refs", () => {
278
await ServerBrowserPage.click(page, { selector: 'button[data-example=" >> aria-ref=e1"]' });
279
expect(await page.locator("button").textContent()).toBe("saved");
280
});
281
+
282
+ it("evaluates top-level await and returns the last expression's value", async () => {
283
+ await page.setContent("<title>Ready</title>");
284
+ const result = await ServerBrowserPage.evaluate(
285
+ cdp,
286
+ { expression: "await Promise.resolve(); document.title" },
287
+ 5_000,
288
+ );
289
+ expect(result).toBe("Ready");
290
+ });
291
+
292
+ it("still awaits the promise an async function returns", async () => {
293
+ const result = await ServerBrowserPage.evaluate(
294
+ cdp,
295
+ { expression: "(async () => { await 0; return 1 })()" },
296
+ 5_000,
297
+ );
298
+ expect(result).toBe(1);
299
+ });
300
+
301
+ it("does not rerun an expression whose SyntaxError is thrown at runtime", async () => {
302
+ const evaluation = ServerBrowserPage.evaluate(
303
+ cdp,
304
+ { expression: "window.runs = (window.runs ?? 0) + 1; new Function('await 0')" },
305
+ 5_000,
306
+ );
307
+ await expect(evaluation).rejects.toThrow("await is only valid");
308
+ expect(await page.evaluate("window.runs")).toBe(1);
309
+ });
310
+
311
+ it("does not rerun an expression whose awaited browser call rejects with a SyntaxError", async () => {
312
+ let posts = 0;
313
+ await page.route("http://preview.test/**", (route) => {
314
+ if (route.request().method() === "POST") posts++;
315
+ return route.fulfill({ contentType: "text/html", body: "<title>Ready</title>" });
316
+ });
317
+ await page.goto("http://preview.test/");
318
+ const evaluation = ServerBrowserPage.evaluate(
319
+ cdp,
320
+ {
321
+ expression:
322
+ "(async () => { const r = await fetch('/api', { method: 'POST' }); return r.json() })()",
323
+ },
324
+ 5_000,
325
+ );
326
+ await expect(evaluation).rejects.toThrow("is not valid JSON");
327
+ expect(posts).toBe(1);
328
+ });
329
+
330
+ it("returns the result of a top-level await expression that reloads the page", async () => {
331
+ await page.route("http://preview.test/", (route) =>
332
+ route.fulfill({ contentType: "text/html", body: "<title>Ready</title>" }),
333
+ );
334
+ await page.goto("http://preview.test/");
335
+ const result = await ServerBrowserPage.evaluate(
336
+ cdp,
337
+ { expression: "await 0; location.reload(); ({ clicked: true })" },
338
+ 5_000,
339
+ );
340
+ expect(result).toEqual({ clicked: true });
341
+ });
342
});
343
344
describe("server browser snapshot screenshot", () => {
@@ -417,6 +487,40 @@ describe("server browser drag", () => {
487
});
488
});
489
490
+describe("server browser evaluate", () => {
491
+ it("does not retry a parse error that arrives after the timeout", async () => {
492
+ vi.useFakeTimers();
493
+ try {
494
+ const classic = Promise.withResolvers<unknown>();
495
+ const send = vi.fn((method: string) =>
496
+ method === "Runtime.evaluate" ? classic.promise : Promise.resolve({}),
497
+ );
498
+ const cdp = { send } as unknown as CDPSession;
499
+ const evaluation = ServerBrowserPage.evaluate(cdp, { expression: "await 0; 1" }, 10);
500
+ const rejected = expect(evaluation).rejects.toMatchObject({
501
+ tag: "PreviewAutomationTimeoutError",
502
+ });
503
+ await vi.advanceTimersByTimeAsync(10);
504
+ await rejected;
505
+ classic.resolve({
506
+ result: { type: "object" },
507
+ exceptionDetails: {
508
+ text: "Uncaught",
509
+ scriptId: "4",
510
+ exception: { type: "object", className: "SyntaxError" },
511
+ },
512
+ });
513
+ await classic.promise;
514
+ expect(send.mock.calls.map(([method]) => method)).toEqual([
515
+ "Runtime.evaluate",
516
+ "Runtime.terminateExecution",
517
+ ]);
518
+ } finally {
519
+ vi.useRealTimers();
520
+ }
521
+ });
522
+});
523
+
524
describe("server browser net errors", () => {
525
it.each([
526
["net::ERR_CONNECTION_REFUSED", "ERR_CONNECTION_REFUSED", -102],
apps/server/src/preview/ServerBrowserPage.ts
+16
-1
@@ -892,8 +892,10 @@ export const evaluate = async (
892
timeoutMs: number,
893
) => {
894
let timer: ReturnType<typeof setTimeout> | undefined;
895
+ let timedOut = false;
896
const expired = new Promise<never>((_, reject) => {
897
timer = setTimeout(() => {
898
+ timedOut = true;
899
// Stops a busy script; an awaited promise is abandoned and settles unread.
900
void cdp.send("Runtime.terminateExecution").catch(constVoid);
901
reject(
@@ -904,10 +906,23 @@ export const evaluate = async (
906
);
907
}, timeoutMs);
908
});
907
- const evaluation = cdp.send("Runtime.evaluate", {
909
+ const params = {
910
expression: input.expression,
911
awaitPromise: input.awaitPromise ?? true,
912
returnByValue: input.returnByValue ?? true,
913
+ };
914
+ // The devtools console's replMode allows top-level await but returns a trailing promise unawaited,
915
+ // so it only retries what the page's parser rejected. A parse error has a scriptId but no stack and
916
+ // ran nothing; a browser-internal rejection has neither.
917
+ const evaluation = cdp.send("Runtime.evaluate", params).then((classic) => {
918
+ const failure = classic.exceptionDetails;
919
+ const unparsed =
920
+ failure?.exception?.className === "SyntaxError" &&
921
+ failure.scriptId !== undefined &&
922
+ failure.stackTrace === undefined;
923
+ return unparsed && !timedOut && input.expression.includes("await")
924
+ ? cdp.send("Runtime.evaluate", { ...params, replMode: true })
925
+ : classic;
926
});
927
void evaluation.catch(constVoid);
928
const result = await Promise.race([evaluation, expired]).finally(() => clearTimeout(timer));