Skip to content

Commit 2ed69de

Browse files
fix(desktop): browser snapshots work after relay reconnects (pingdotgg#24829)
1 parent 7271ea0 commit 2ed69de

3 files changed

Lines changed: 58 additions & 1 deletion

File tree

‎apps/desktop/src/preview/CdpRelay.test.ts‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { describe, expect, it, vi } from "vite-plus/test";
22

3-
import { createCdpRelayConnection, type CdpRelayTarget } from "./CdpRelay.ts";
3+
import {
4+
createCdpRelayConnection,
5+
type CdpRelayConnection,
6+
type CdpRelayTarget,
7+
} from "./CdpRelay.ts";
48

59
const makeTarget = (
610
send: CdpRelayTarget["send"],
@@ -354,3 +358,43 @@ describe("CDP relay payload budget", () => {
354358
expect(written.map((message) => message["method"])).toEqual([]);
355359
});
356360
});
361+
362+
describe("CDP relay reconnects", () => {
363+
it("reports the page's default execution context to every connection", async () => {
364+
// Electron keeps one debugger attached, so Runtime stays enabled after a client drops,
365+
// and the native Runtime.enable announces contexts again only after Runtime.disable.
366+
let runtimeEnabled = false;
367+
let current: CdpRelayConnection | undefined;
368+
const context = {
369+
context: { id: 1, origin: "null", name: "", auxData: { isDefault: true, frameId: "F" } },
370+
};
371+
const send = vi.fn(async (method: string) => {
372+
if (method === "Runtime.disable") runtimeEnabled = false;
373+
if (method === "Runtime.enable" && !runtimeEnabled) {
374+
runtimeEnabled = true;
375+
current!.event("Runtime.executionContextCreated", context, "");
376+
}
377+
return {};
378+
});
379+
const connect = async () => {
380+
const written: Array<Record<string, unknown>> = [];
381+
current = makeRelay(makeTarget(send), (raw) => written.push(JSON.parse(raw)));
382+
current.receive(JSON.stringify({ id: 1, method: "Target.setAutoAttach", params: {} }));
383+
await settle();
384+
current.receive(
385+
JSON.stringify({ id: 2, method: "Runtime.enable", sessionId: "t3-preview-page" }),
386+
);
387+
await settle();
388+
return written.filter((m) => m["method"] === "Runtime.executionContextCreated");
389+
};
390+
const expected = [
391+
{
392+
method: "Runtime.executionContextCreated",
393+
params: context,
394+
sessionId: "t3-preview-page",
395+
},
396+
];
397+
expect(await connect()).toEqual(expected);
398+
expect(await connect()).toEqual(expected);
399+
});
400+
});

‎apps/desktop/src/preview/CdpRelay.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ export function createCdpRelayConnection(
188188
}
189189
};
190190

191+
let runtimeReset: Promise<unknown> | undefined;
192+
191193
const pageCommand = (command: CdpCommand): Promise<unknown> => {
192194
const child = sessions.get(command.sessionId!) === "page" ? undefined : command.sessionId;
193195
const owner =
@@ -198,6 +200,14 @@ export function createCdpRelayConnection(
198200
}
199201
// Playwright asks for this on every page; the host emulates focus only while it is in use.
200202
if (command.method === "Emulation.setFocusEmulationEnabled") return Promise.resolve({});
203+
// The shared debugger keeps Runtime enabled after an earlier connection, and a repeated
204+
// enable reports no existing contexts; Playwright waits for them, so reset once per connection.
205+
if (command.method === "Runtime.enable" && child === undefined) {
206+
runtimeReset ??= target.send("Runtime.disable", {}, undefined, owner).catch(() => undefined);
207+
return runtimeReset.then(() =>
208+
target.send(command.method, command.params ?? {}, undefined, owner),
209+
);
210+
}
201211
return target.send(command.method, command.params ?? {}, child, owner);
202212
};
203213

‎apps/desktop/src/preview/DesktopBrowserHost.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,9 @@ describe("DesktopBrowserHost", () => {
491491
// Playwright re-enabling a domain after a reload is not the agent using the tab.
492492
yield* command(host, 1, "Runtime.enable");
493493
yield* settle;
494+
// The relay resets Runtime before a connection's first enable: answer the disable, then the enable.
495+
debuggee.release();
496+
yield* settle;
494497
debuggee.release();
495498
yield* settle;
496499
yield* TestClock.adjust("1 minute");

0 commit comments

Comments
 (0)