|
1 | 1 | import { describe, expect, it, vi } from "vite-plus/test"; |
2 | 2 |
|
3 | | -import { createCdpRelayConnection, type CdpRelayTarget } from "./CdpRelay.ts"; |
| 3 | +import { |
| 4 | + createCdpRelayConnection, |
| 5 | + type CdpRelayConnection, |
| 6 | + type CdpRelayTarget, |
| 7 | +} from "./CdpRelay.ts"; |
4 | 8 |
|
5 | 9 | const makeTarget = ( |
6 | 10 | send: CdpRelayTarget["send"], |
@@ -354,3 +358,43 @@ describe("CDP relay payload budget", () => { |
354 | 358 | expect(written.map((message) => message["method"])).toEqual([]); |
355 | 359 | }); |
356 | 360 | }); |
| 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 | +}); |
0 commit comments