Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

DesktopBrowserHost.test.ts

1,175 lines45.2 KB

1// @effect-diagnostics nodeBuiltinImport:off - Stands in for an Electron debugger.
2import { describe, expect, it, vi } from "@effect/vitest";
3import { DesktopBrowserEvent } from "@t3tools/contracts";
4import * as Effect from "effect/Effect";
5import * as Fiber from "effect/Fiber";
6import * as Queue from "effect/Queue";
7import * as Schema from "effect/Schema";
8import * as Stream from "effect/Stream";
9import * as TestClock from "effect/testing/TestClock";
10import * as NodeEvents from "node:events";
11​
12import * as DesktopBrowserHost from "./DesktopBrowserHost.ts";
13​
14const decodeEvent = Schema.decodeUnknownSync(Schema.fromJsonString(DesktopBrowserEvent));
15const encodeJson = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
16const decodeCdpReply = Schema.decodeUnknownSync(
17 Schema.fromJsonString(Schema.Struct({ id: Schema.Number })),
18);
19const key = { threadId: "thread-1", tabId: "tab-1" };
20/** Sends a page command whose id the fake debugger records as it receives it. */
21const command = (
22 host: DesktopBrowserHost.DesktopBrowserHost["Service"],
23 id: number,
24 method: string,
25) =>
26 host.handleCommandLine(
27 encodeJson({
28 type: "cdp",
29 ...key,
30 message: encodeJson({ id, method, params: { id }, sessionId: "t3-preview-page" }),
31 }),
32 );
33​
34const makeDebuggee = (
35 options: {
36 readonly deferFocus?: boolean;
37 /** The guest is still loading its first page. */
38 readonly loading?: boolean;
39 readonly refuse?: (method: string, sessionId: string | undefined) => boolean;
40 } = {},
41) => {
42 const emitter = new NodeEvents.EventEmitter();
43 const pending: Array<() => void> = [];
44 const focusChanges: Array<() => void> = [];
45 const sent: Array<unknown> = [];
46 const sendWaiters: Array<() => void> = [];
47 const focusEmulation: Array<boolean> = [];
48 const debuggee = Object.assign(emitter, {
49 sendCommand: (
50 method: string,
51 params?: { id?: number; enabled?: boolean },
52 sessionId?: string,
53 ) =>
54 new Promise((resolve, reject) => {
55 if (options.refuse?.(method, sessionId)) {
56 reject(new Error("Refused"));
57 return;
58 }
59 if (method === "Target.getTargetInfo") {
60 resolve({ targetInfo: { targetId: "GUEST" } });
61 return;
62 }
63 if (method === "Emulation.setFocusEmulationEnabled") {
64 focusEmulation.push(params?.enabled === true);
65 if (options.deferFocus) focusChanges.push(() => resolve({}));
66 else resolve({});
67 return;
68 }
69 sent.push(params?.id);
70 pending.push(() => resolve({ method }));
71 sendWaiters.shift()?.();
72 }),
73 });
74 let destroyed = false;
75 const webContents = Object.assign(new NodeEvents.EventEmitter(), {
76 getURL: () => "http://localhost/",
77 getTitle: () => "Page",
78 getUserAgent: () => "Electron",
79 isLoadingMainFrame: () => options.loading === true,
80 isDestroyed: () => destroyed,
81 });
82 return {
83 tab: {
84 webContents: webContents as unknown as Electron.WebContents,
85 debugger: debuggee as unknown as Electron.Debugger,
86 },
87 emit: (method: string, params: unknown) => emitter.emit("message", {}, method, params, ""),
88 navigate: () => webContents.emit("did-navigate"),
89 domReady: () => webContents.emit("dom-ready"),
90 destroy: () => {
91 destroyed = true;
92 },
93 failLoad: (code: number) =>
94 webContents.emit("did-fail-load", {}, code, "Failed", "http://localhost/", true),
95 /** Ids of the commands the debugger received, in order. */
96 sent,
97 focusEmulation,
98 nextSend: () => new Promise<void>((resolve) => sendWaiters.push(resolve)),
99 release: () => pending.splice(0).forEach((resolve) => resolve()),
100 releaseFirst: () => pending.shift()?.(),
101 settleFocus: () => focusChanges.splice(0).forEach((resolve) => resolve()),
102 };
103};
104​
105/** Commands wait behind a focus emulation change; it settles within a macrotask. */
106// oxlint-disable-next-line t3code/no-real-time-wait-in-tests -- a zero-delay macrotask boundary, not a wait for time
107const settle = Effect.promise(() => new Promise<void>((resolve) => setImmediate(resolve)));
108​
109const takeEvents = (host: DesktopBrowserHost.DesktopBrowserHost["Service"], count: number) =>
110 host.events.pipe(
111 Stream.take(count),
112 Stream.runCollect,
113 Effect.map((lines) => lines.map((line) => decodeEvent(new TextDecoder().decode(line)))),
114 );
115​
116/**
117 * Ids of the replies the host sends, in order. A screenshot's reply leaves after its turn
118 * has passed on, so by then the next waiting screenshot, if any, has been sent. Subscribe
119 * before sending commands: a new subscriber releases the tab's relay.
120 */
121const watchReplies = (host: DesktopBrowserHost.DesktopBrowserHost["Service"]) =>
122 Effect.gen(function* () {
123 const replies = yield* Queue.unbounded<number>();
124 yield* host.events.pipe(
125 Stream.map((line) => decodeEvent(new TextDecoder().decode(line))),
126 Stream.runForEach((event) =>
127 event.type === "cdp" ? Queue.offer(replies, decodeCdpReply(event.message).id) : Effect.void,
128 ),
129 Effect.forkScoped,
130 );
131 yield* Effect.yieldNow;
132 return replies;
133 });
134​
135/** Whether the client should paint the guest, each time that changes. */
136const watchPainting = (host: DesktopBrowserHost.DesktopBrowserHost["Service"]) =>
137 Effect.gen(function* () {
138 const painting: Array<boolean> = [];
139 yield* host.painting.pipe(
140 Stream.runForEach(({ active }) => Effect.sync(() => painting.push(active))),
141 Effect.forkScoped,
142 );
143 yield* Effect.yieldNow;
144 return painting;
145 });
146​
147describe("DesktopBrowserHost", () => {
148 it.effect("announces tabs already attached to a backend that starts later", () =>
149 Effect.gen(function* () {
150 const host = yield* DesktopBrowserHost.make;
151 host.attach(key, makeDebuggee().tab);
152​
153 expect(yield* takeEvents(host, 1)).toEqual([{ type: "attached", ...key }]);
154 expect(yield* takeEvents(host, 1)).toEqual([{ type: "attached", ...key }]);
155 }),
156 );
157​
158 it.effect("announces a guest still loading its first page only once that page is up", () =>
159 Effect.gen(function* () {
160 const host = yield* DesktopBrowserHost.make;
161 const loading = makeDebuggee({ loading: true });
162 const other = { ...key, tabId: "tab-2" };
163 const reader = yield* takeEvents(host, 3).pipe(Effect.forkScoped);
164​
165 yield* Effect.yieldNow;
166 host.attach(key, loading.tab);
167 host.setShown(key, true);
168 host.attach(other, makeDebuggee().tab);
169 loading.domReady();
170​
171 expect(yield* Fiber.join(reader)).toEqual([
172 { type: "attached", ...other },
173 { type: "attached", ...key },
174 { type: "shown", ...key, shown: true },
175 ]);
176 }),
177 );
178​
179 it.effect(
180 "keeps a loading guest unannounced across a replaced navigation and a new backend",
181 () =>
182 Effect.gen(function* () {
183 const host = yield* DesktopBrowserHost.make;
184 const loading = makeDebuggee({ loading: true });
185 const other = { ...key, tabId: "tab-2" };
186 host.attach(key, loading.tab);
187 loading.failLoad(-3);
188 const reader = yield* takeEvents(host, 2).pipe(Effect.forkScoped);
189​
190 yield* Effect.yieldNow;
191 host.attach(other, makeDebuggee().tab);
192 loading.domReady();
193​
194 expect(yield* Fiber.join(reader)).toEqual([
195 { type: "attached", ...other },
196 { type: "attached", ...key },
197 ]);
198 }),
199 );
200​
201 it.effect(
202 "announces a first page that never comes up, but never a tab closed or destroyed first",
203 () =>
204 Effect.gen(function* () {
205 const host = yield* DesktopBrowserHost.make;
206 const closed = { ...key, tabId: "tab-2" };
207 const destroyed = { ...key, tabId: "tab-3" };
208 const destroyedGuest = makeDebuggee({ loading: true });
209 const reader = yield* takeEvents(host, 1).pipe(Effect.forkScoped);
210​
211 yield* Effect.yieldNow;
212 host.attach(closed, makeDebuggee({ loading: true }).tab);
213 host.detach(closed);
214 host.attach(destroyed, destroyedGuest.tab);
215 destroyedGuest.destroy();
216 host.attach(key, makeDebuggee({ loading: true }).tab);
217 yield* TestClock.adjust("5 seconds");
218​
219 expect(yield* Fiber.join(reader)).toEqual([{ type: "attached", ...key }]);
220 }),
221 );
222​
223 it.effect("re-sends shown to a backend that starts later and when a tab attaches again", () =>
224 Effect.gen(function* () {
225 const host = yield* DesktopBrowserHost.make;
226 host.attach(key, makeDebuggee().tab);
227 host.setShown(key, true);
228 const reader = yield* takeEvents(host, 5).pipe(Effect.forkScoped);
229​
230 yield* Effect.yieldNow;
231 host.detach(key);
232 host.attach(key, makeDebuggee().tab);
233​
234 expect(yield* Fiber.join(reader)).toEqual([
235 { type: "attached", ...key },
236 { type: "shown", ...key, shown: true },
237 { type: "detached", ...key },
238 { type: "attached", ...key },
239 { type: "shown", ...key, shown: true },
240 ]);
241 }),
242 );
243​
244 it.effect("replays an explicit hidden report before announcing another tab", () =>
245 Effect.gen(function* () {
246 const host = yield* DesktopBrowserHost.make;
247 const other = { ...key, tabId: "tab-2" };
248 host.setShown(key, false);
249 host.attach(key, makeDebuggee().tab);
250 host.setShown(other, true);
251 host.attach(other, makeDebuggee().tab);
252​
253 expect(yield* takeEvents(host, 3)).toEqual([
254 { type: "attached", ...key },
255 { type: "shown", ...key, shown: false },
256 { type: "attached", ...other },
257 ]);
258 }),
259 );
260​
261 it.effect("drops replies from a relay the server released", () =>
262 Effect.gen(function* () {
263 const host = yield* DesktopBrowserHost.make;
264 const debuggee = makeDebuggee();
265 host.attach(key, debuggee.tab);
266 const reader = yield* takeEvents(host, 2).pipe(Effect.forkScoped);
267​
268 yield* Effect.yieldNow;
269 const command = (id: number, method: string) =>
270 host.handleCommandLine(
271 encodeJson({
272 type: "cdp",
273 ...key,
274 message: encodeJson({ id, method, sessionId: "t3-preview-page" }),
275 }),
276 );
277 yield* command(1, "Page.captureScreenshot");
278 yield* host.handleCommandLine(encodeJson({ type: "release", ...key }));
279 yield* command(2, "DOM.enable");
280 yield* settle;
281 debuggee.release();
282 const [, reply] = yield* Fiber.join(reader);
283​
284 expect(reply).toMatchObject({ type: "cdp" });
285 expect(decodeCdpReply((reply as { message: string }).message).id).toBe(2);
286 }).pipe(Effect.scoped),
287 );
288​
289 it.effect("sends a page one screenshot at a time", () =>
290 Effect.gen(function* () {
291 const host = yield* DesktopBrowserHost.make;
292 const debuggee = makeDebuggee();
293 host.attach(key, debuggee.tab);
294 const replies = yield* watchReplies(host);
295​
296 yield* command(host, 1, "Page.captureScreenshot");
297 yield* command(host, 2, "Page.captureScreenshot");
298 yield* command(host, 3, "DOM.enable");
299 yield* settle;
300 expect(debuggee.sent).toEqual([1, 3]);
301​
302 debuggee.releaseFirst();
303 expect(yield* Queue.take(replies)).toBe(1);
304 expect(debuggee.sent).toEqual([1, 3, 2]);
305 }).pipe(Effect.scoped),
306 );
307​
308 it.effect("stops waiting on a screenshot the page navigated away from", () =>
309 Effect.gen(function* () {
310 const host = yield* DesktopBrowserHost.make;
311 const debuggee = makeDebuggee();
312 host.attach(key, debuggee.tab);
313 const replies = yield* watchReplies(host);
314​
315 yield* command(host, 1, "Page.captureScreenshot");
316 yield* command(host, 2, "Page.captureScreenshot");
317 yield* command(host, 3, "Page.captureScreenshot");
318 yield* settle;
319 debuggee.navigate();
320 expect(debuggee.sent).toEqual([1, 2]);
321​
322 // The superseded screenshot answering late must not start a third beside the second.
323 debuggee.releaseFirst();
324 expect(yield* Queue.take(replies)).toBe(1);
325 expect(debuggee.sent).toEqual([1, 2]);
326​
327 debuggee.releaseFirst();
328 expect(yield* Queue.take(replies)).toBe(2);
329 expect(debuggee.sent).toEqual([1, 2, 3]);
330 }).pipe(Effect.scoped),
331 );
332​
333 it.effect("drops screenshots a released relay left waiting", () =>
334 Effect.gen(function* () {
335 const host = yield* DesktopBrowserHost.make;
336 const debuggee = makeDebuggee();
337 host.attach(key, debuggee.tab);
338​
339 yield* command(host, 1, "Page.captureScreenshot");
340 yield* command(host, 2, "Page.captureScreenshot");
341 yield* settle;
342 yield* host.handleCommandLine(encodeJson({ type: "release", ...key }));
343 yield* command(host, 3, "Page.captureScreenshot");
344 expect(debuggee.sent).toEqual([1]);
345​
346 const sent = debuggee.nextSend();
347 debuggee.release();
348 yield* Effect.promise(() => sent);
349 expect(debuggee.sent).toEqual([1, 3]);
350 }),
351 );
352​
353 it.effect("emulates focus while the server drives a guest, then lets a hidden one throttle", () =>
354 Effect.gen(function* () {
355 const host = yield* DesktopBrowserHost.make;
356 const debuggee = makeDebuggee();
357 host.attach(key, debuggee.tab);
358​
359 yield* command(host, 1, "DOM.enable");
360 yield* settle;
361 expect(debuggee.focusEmulation).toEqual([true]);
362 expect(debuggee.sent).toEqual([1]);
363 debuggee.release();
364 yield* settle;
365​
366 yield* TestClock.adjust("4 seconds");
367 yield* command(host, 2, "Input.dispatchMouseEvent");
368 yield* settle;
369 debuggee.release();
370 yield* settle;
371 yield* TestClock.adjust("4 seconds");
372 expect(debuggee.focusEmulation).toEqual([true]);
373​
374 yield* TestClock.adjust("10 seconds");
375 expect(debuggee.focusEmulation).toEqual([true, false]);
376​
377 yield* command(host, 3, "Page.captureScreenshot");
378 yield* settle;
379 expect(debuggee.focusEmulation).toEqual([true, false, true]);
380 expect(debuggee.sent).toEqual([1, 2, 3]);
381 }),
382 );
383​
384 it.effect("keeps a guest focused while a command it is running is still pending", () =>
385 Effect.gen(function* () {
386 const host = yield* DesktopBrowserHost.make;
387 const debuggee = makeDebuggee();
388 host.attach(key, debuggee.tab);
389 const replies = yield* watchReplies(host);
390​
391 // An evaluation awaiting the page's next frame, which a throttled guest never paints.
392 yield* command(host, 1, "Runtime.evaluate");
393 yield* settle;
394 expect(debuggee.sent).toEqual([1]);
395 yield* TestClock.adjust("1 minute");
396 expect(debuggee.focusEmulation).toEqual([true]);
397​
398 debuggee.release();
399 expect(yield* Queue.take(replies)).toBe(1);
400 yield* TestClock.adjust("1 minute");
401 expect(debuggee.focusEmulation).toEqual([true, false]);
402 }).pipe(Effect.scoped),
403 );
404​
405 it.effect("lets a guest throttle once Chromium can no longer answer its pending command", () =>
406 Effect.gen(function* () {
407 const host = yield* DesktopBrowserHost.make;
408 const debuggee = makeDebuggee();
409 host.attach(key, debuggee.tab);
410​
411 yield* command(host, 1, "Runtime.evaluate");
412 yield* settle;
413 yield* host.handleCommandLine(encodeJson({ type: "release", ...key }));
414 yield* TestClock.adjust("1 minute");
415 expect(debuggee.focusEmulation).toEqual([true, false]);
416​
417 // A command the old document never answers.
418 yield* command(host, 2, "Page.captureScreenshot");
419 yield* settle;
420 expect(debuggee.sent).toEqual([1, 2]);
421 debuggee.navigate();
422 yield* TestClock.adjust("1 minute");
423 expect(debuggee.focusEmulation).toEqual([true, false, true, false]);
424 }),
425 );
426​
427 it.effect("keeps a guest focused for the screenshot a navigation lets start", () =>
428 Effect.gen(function* () {
429 const host = yield* DesktopBrowserHost.make;
430 const debuggee = makeDebuggee();
431 host.attach(key, debuggee.tab);
432​
433 yield* command(host, 1, "Page.captureScreenshot");
434 yield* command(host, 2, "Page.captureScreenshot");
435 yield* settle;
436 debuggee.navigate();
437 expect(debuggee.sent).toEqual([1, 2]);
438 yield* TestClock.adjust("1 minute");
439 expect(debuggee.focusEmulation).toEqual([true]);
440 }),
441 );
442​
443 it.effect("never sends a screenshot a navigation superseded while focus was changing", () =>
444 Effect.gen(function* () {
445 const host = yield* DesktopBrowserHost.make;
446 const debuggee = makeDebuggee({ deferFocus: true });
447 host.attach(key, debuggee.tab);
448​
449 yield* command(host, 1, "Page.captureScreenshot");
450 yield* command(host, 2, "Page.captureScreenshot");
451 yield* settle;
452 debuggee.navigate();
453 debuggee.settleFocus();
454 yield* settle;
455 // Two captures in flight would undo the one-at-a-time guard.
456 expect(debuggee.sent).toEqual([2]);
457 }),
458 );
459​
460 it.effect("keeps a guest focused when its turn starts after the agent's first command", () =>
461 Effect.gen(function* () {
462 const host = yield* DesktopBrowserHost.make;
463 const debuggee = makeDebuggee();
464 host.attach(key, debuggee.tab);
465 const turn = (active: boolean) =>
466 host.handleCommandLine(encodeJson({ type: "turn", threadId: key.threadId, active }));
467​
468 yield* command(host, 1, "Input.dispatchKeyEvent");
469 yield* settle;
470 yield* turn(true);
471 debuggee.release();
472 yield* settle;
473 yield* TestClock.adjust("1 minute");
474 expect(debuggee.focusEmulation).toEqual([true]);
475​
476 yield* turn(false);
477 yield* TestClock.adjust("1 minute");
478 expect(debuggee.focusEmulation).toEqual([true, false]);
479 }),
480 );
481​
482 it.effect("lets a guest throttle through a turn that only sent it housekeeping", () =>
483 Effect.gen(function* () {
484 const host = yield* DesktopBrowserHost.make;
485 const debuggee = makeDebuggee();
486 host.attach(key, debuggee.tab);
487 yield* host.handleCommandLine(
488 encodeJson({ type: "turn", threadId: key.threadId, active: true }),
489 );
490​
491 // Playwright re-enabling a domain after a reload is not the agent using the tab.
492 yield* command(host, 1, "Runtime.enable");
493 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;
497 debuggee.release();
498 yield* settle;
499 yield* TestClock.adjust("1 minute");
500 expect(debuggee.focusEmulation).toEqual([true, false]);
501​
502 yield* command(host, 2, "Runtime.evaluate");
503 yield* settle;
504 debuggee.release();
505 yield* settle;
506 yield* TestClock.adjust("1 minute");
507 expect(debuggee.focusEmulation).toEqual([true, false, true]);
508 }),
509 );
510​
511 it.effect("keeps a guest the agent used focused through its turn's pauses", () =>
512 Effect.gen(function* () {
513 const host = yield* DesktopBrowserHost.make;
514 const debuggee = makeDebuggee();
515 host.attach(key, debuggee.tab);
516 const turn = (active: boolean) =>
517 host.handleCommandLine(encodeJson({ type: "turn", threadId: key.threadId, active }));
518​
519 yield* turn(true);
520 yield* TestClock.adjust("1 minute");
521 expect(debuggee.focusEmulation).toEqual([]);
522​
523 yield* command(host, 1, "Input.dispatchKeyEvent");
524 yield* settle;
525 debuggee.release();
526 yield* settle;
527 // The model thinks between steps; a focus-sensitive menu must not blur meanwhile.
528 yield* TestClock.adjust("1 minute");
529 expect(debuggee.focusEmulation).toEqual([true]);
530​
531 yield* turn(false);
532 yield* TestClock.adjust("1 minute");
533 expect(debuggee.focusEmulation).toEqual([true, false]);
534 }),
535 );
536​
537 it.effect(
538 "ends a command's hold with the turn that sent it, even if Chromium never answers",
539 () =>
540 Effect.gen(function* () {
541 const host = yield* DesktopBrowserHost.make;
542 const debuggee = makeDebuggee();
543 host.attach(key, debuggee.tab);
544 yield* host.handleCommandLine(
545 encodeJson({ type: "turn", threadId: key.threadId, active: true }),
546 );
547​
548 // A screenshot of a hidden guest the server already gave up on.
549 yield* command(host, 1, "Page.captureScreenshot");
550 yield* settle;
551 expect(debuggee.sent).toEqual([1]);
552 yield* host.handleCommandLine(
553 encodeJson({ type: "turn", threadId: key.threadId, active: false }),
554 );
555 yield* TestClock.adjust("1 minute");
556 expect(debuggee.focusEmulation).toEqual([true, false]);
557 }),
558 );
559​
560 it.effect("ends the agent's own session's holds with its turn, but not a viewer's stream", () =>
561 Effect.gen(function* () {
562 const host = yield* DesktopBrowserHost.make;
563 const debuggee = makeDebuggee();
564 host.attach(key, debuggee.tab);
565 const replies = yield* watchReplies(host);
566 const raw = (message: Record<string, unknown>) =>
567 host.handleCommandLine(encodeJson({ type: "cdp", ...key, message: encodeJson(message) }));
568 const turn = (active: boolean) =>
569 host.handleCommandLine(encodeJson({ type: "turn", threadId: key.threadId, active }));
570​
571 // The server's own CDP session, evaluating a promise that never settles.
572 yield* turn(true);
573 yield* raw({ id: 1, method: "Target.attachToTarget", params: { targetId: "GUEST" } });
574 expect(yield* Queue.take(replies)).toBe(1);
575 yield* raw({ id: 2, method: "Runtime.evaluate", sessionId: "t3-preview-page-1" });
576 yield* settle;
577 yield* turn(false);
578 yield* TestClock.adjust("1 minute");
579 expect(debuggee.focusEmulation).toEqual([true, false]);
580​
581 // A viewer streaming the guest keeps it focused through a turn's end.
582 yield* turn(true);
583 yield* raw({ id: 3, method: "Target.attachToTarget", params: { targetId: "GUEST" } });
584 expect(yield* Queue.take(replies)).toBe(3);
585 yield* raw({ id: 4, method: "Page.startScreencast", sessionId: "t3-preview-page-2" });
586 yield* settle;
587 yield* turn(false);
588 yield* TestClock.adjust("1 minute");
589 expect(debuggee.focusEmulation).toEqual([true, false, true]);
590 }).pipe(Effect.scoped),
591 );
592​
593 it.effect("paints for a screenshot queued behind one its viewer abandoned mid-capture", () =>
594 Effect.gen(function* () {
595 const host = yield* DesktopBrowserHost.make;
596 const debuggee = makeDebuggee();
597 host.attach(key, debuggee.tab);
598 const replies = yield* watchReplies(host);
599 const painting = yield* watchPainting(host);
600 const raw = (message: Record<string, unknown>) =>
601 host.handleCommandLine(encodeJson({ type: "cdp", ...key, message: encodeJson(message) }));
602​
603 yield* raw({ id: 1, method: "Target.attachToTarget", params: { targetId: "GUEST" } });
604 expect(yield* Queue.take(replies)).toBe(1);
605 yield* raw({ id: 2, method: "Page.captureScreenshot", sessionId: "t3-preview-page-1" });
606 yield* settle;
607 yield* raw({
608 id: 3,
609 method: "Target.detachFromTarget",
610 params: { sessionId: "t3-preview-page-1" },
611 });
612 expect(yield* Queue.take(replies)).toBe(3);
613 expect(painting).toEqual([true]);
614​
615 // The agent's screenshot waits its turn; painting lets the abandoned one finish first.
616 yield* command(host, 4, "Page.captureScreenshot");
617 yield* settle;
618 expect(painting).toEqual([true]);
619 debuggee.release();
620 expect(yield* Queue.take(replies)).toBe(2);
621 yield* settle;
622 expect(debuggee.sent).toEqual([undefined, 4]);
623 expect(painting).toEqual([true]);
624 debuggee.release();
625 expect(yield* Queue.take(replies)).toBe(4);
626 yield* settle;
627 expect(painting).toEqual([true, false]);
628 }).pipe(Effect.scoped),
629 );
630​
631 it.effect("stops painting for a queued screenshot once its session detaches", () =>
632 Effect.gen(function* () {
633 const host = yield* DesktopBrowserHost.make;
634 const debuggee = makeDebuggee();
635 host.attach(key, debuggee.tab);
636 const replies = yield* watchReplies(host);
637 const painting = yield* watchPainting(host);
638 const raw = (message: Record<string, unknown>) =>
639 host.handleCommandLine(encodeJson({ type: "cdp", ...key, message: encodeJson(message) }));
640 const turn = (active: boolean) =>
641 host.handleCommandLine(encodeJson({ type: "turn", threadId: key.threadId, active }));
642​
643 // The agent's screenshot is abandoned at turn end; a viewer's waits behind it, then leaves.
644 yield* turn(true);
645 yield* command(host, 1, "Page.captureScreenshot");
646 yield* settle;
647 yield* turn(false);
648 yield* raw({ id: 2, method: "Target.attachToTarget", params: { targetId: "GUEST" } });
649 expect(yield* Queue.take(replies)).toBe(2);
650 yield* raw({ id: 3, method: "Page.captureScreenshot", sessionId: "t3-preview-page-1" });
651 yield* settle;
652 expect(painting).toEqual([true]);
653 yield* raw({
654 id: 4,
655 method: "Target.detachFromTarget",
656 params: { sessionId: "t3-preview-page-1" },
657 });
658 expect(yield* Queue.take(replies)).toBe(4);
659​
660 // The agent's answers late; the viewer's is dropped unsent, so nothing is left to paint.
661 debuggee.release();
662 yield* settle;
663 expect(debuggee.sent).toEqual([1]);
664 expect(painting).toEqual([true, false]);
665 yield* TestClock.adjust("1 minute");
666 expect(debuggee.focusEmulation).toEqual([true, false]);
667 }).pipe(Effect.scoped),
668 );
669​
670 it.effect("never sends a screenshot whose session detached while focus was changing", () =>
671 Effect.gen(function* () {
672 const host = yield* DesktopBrowserHost.make;
673 const debuggee = makeDebuggee({ deferFocus: true });
674 host.attach(key, debuggee.tab);
675 const replies = yield* watchReplies(host);
676 const raw = (message: Record<string, unknown>) =>
677 host.handleCommandLine(encodeJson({ type: "cdp", ...key, message: encodeJson(message) }));
678​
679 yield* raw({ id: 1, method: "Target.attachToTarget", params: { targetId: "GUEST" } });
680 expect(yield* Queue.take(replies)).toBe(1);
681 yield* raw({ id: 2, method: "Page.captureScreenshot", sessionId: "t3-preview-page-1" });
682 yield* raw({
683 id: 3,
684 method: "Target.detachFromTarget",
685 params: { sessionId: "t3-preview-page-1" },
686 });
687 expect(yield* Queue.take(replies)).toBe(3);
688 debuggee.settleFocus();
689 yield* settle;
690 expect(debuggee.sent).toEqual([]);
691 }).pipe(Effect.scoped),
692 );
693​
694 it.effect("holds no focus for a screenshot still queued when the agent's turn ends", () =>
695 Effect.gen(function* () {
696 const host = yield* DesktopBrowserHost.make;
697 const debuggee = makeDebuggee();
698 host.attach(key, debuggee.tab);
699 const painting = yield* watchPainting(host);
700 const turn = (active: boolean) =>
701 host.handleCommandLine(encodeJson({ type: "turn", threadId: key.threadId, active }));
702​
703 yield* turn(true);
704 yield* command(host, 1, "Page.captureScreenshot");
705 yield* command(host, 2, "Page.captureScreenshot");
706 yield* settle;
707 yield* turn(false);
708 yield* settle;
709 expect(painting).toEqual([true]);
710​
711 // The first answers late; the second still goes in turn, painting but holding no focus.
712 debuggee.releaseFirst();
713 yield* settle;
714 expect(debuggee.sent).toEqual([1, 2]);
715 yield* TestClock.adjust("1 minute");
716 expect(debuggee.focusEmulation).toEqual([true, false]);
717 expect(painting).toEqual([true]);
718 debuggee.release();
719 yield* settle;
720 expect(painting).toEqual([true, false]);
721 }).pipe(Effect.scoped),
722 );
723​
724 it.effect("lets a guest throttle when Chromium refuses a viewer's screencast", () =>
725 Effect.gen(function* () {
726 const host = yield* DesktopBrowserHost.make;
727 const debuggee = makeDebuggee({ refuse: (method) => method === "Page.startScreencast" });
728 host.attach(key, debuggee.tab);
729 const replies = yield* watchReplies(host);
730 const painting = yield* watchPainting(host);
731 const raw = (message: Record<string, unknown>) =>
732 host.handleCommandLine(encodeJson({ type: "cdp", ...key, message: encodeJson(message) }));
733​
734 yield* raw({ id: 1, method: "Target.attachToTarget", params: { targetId: "GUEST" } });
735 expect(yield* Queue.take(replies)).toBe(1);
736 yield* raw({ id: 2, method: "Page.startScreencast", sessionId: "t3-preview-page-1" });
737 expect(yield* Queue.take(replies)).toBe(2);
738 yield* settle;
739 expect(painting).toEqual([true, false]);
740 yield* TestClock.adjust("1 minute");
741 expect(debuggee.focusEmulation).toEqual([true, false]);
742 }).pipe(Effect.scoped),
743 );
744​
745 it.effect("lets a guest throttle after a person's input through a viewer that then closed", () =>
746 Effect.gen(function* () {
747 const host = yield* DesktopBrowserHost.make;
748 const debuggee = makeDebuggee();
749 host.attach(key, debuggee.tab);
750 const replies = yield* watchReplies(host);
751 const raw = (message: Record<string, unknown>) =>
752 host.handleCommandLine(encodeJson({ type: "cdp", ...key, message: encodeJson(message) }));
753​
754 yield* host.handleCommandLine(
755 encodeJson({ type: "turn", threadId: key.threadId, active: true }),
756 );
757 yield* raw({ id: 1, method: "Target.attachToTarget", params: { targetId: "GUEST" } });
758 expect(yield* Queue.take(replies)).toBe(1);
759 yield* raw({ id: 2, method: "Page.startScreencast", sessionId: "t3-preview-page-1" });
760 yield* raw({ id: 3, method: "Input.dispatchMouseEvent", sessionId: "t3-preview-page-1" });
761 yield* settle;
762 debuggee.release();
763 yield* raw({
764 id: 4,
765 method: "Target.detachFromTarget",
766 params: { sessionId: "t3-preview-page-1" },
767 });
768 yield* settle;
769 yield* TestClock.adjust("1 minute");
770 expect(debuggee.focusEmulation).toEqual([true, false]);
771 }).pipe(Effect.scoped),
772 );
773​
774 it.effect("keeps a page's screencast when a child session's screencast is refused", () =>
775 Effect.gen(function* () {
776 const host = yield* DesktopBrowserHost.make;
777 const debuggee = makeDebuggee({
778 refuse: (method, sessionId) => method === "Page.startScreencast" && sessionId !== undefined,
779 });
780 host.attach(key, debuggee.tab);
781 const replies = yield* watchReplies(host);
782 const raw = (message: Record<string, unknown>) =>
783 host.handleCommandLine(encodeJson({ type: "cdp", ...key, message: encodeJson(message) }));
784​
785 yield* command(host, 1, "Page.startScreencast");
786 yield* settle;
787 debuggee.release();
788 expect(yield* Queue.take(replies)).toBe(1);
789 yield* raw({ id: 2, method: "Page.startScreencast", sessionId: "child-frame" });
790 expect(yield* Queue.take(replies)).toBe(2);
791 yield* TestClock.adjust("1 minute");
792 expect(debuggee.focusEmulation).toEqual([true]);
793 }).pipe(Effect.scoped),
794 );
795​
796 it.effect("ignores a screencast that arrives on a session after it detached", () =>
797 Effect.gen(function* () {
798 const host = yield* DesktopBrowserHost.make;
799 const debuggee = makeDebuggee();
800 host.attach(key, debuggee.tab);
801 const replies = yield* watchReplies(host);
802 const raw = (message: Record<string, unknown>) =>
803 host.handleCommandLine(encodeJson({ type: "cdp", ...key, message: encodeJson(message) }));
804​
805 yield* raw({ id: 1, method: "Target.attachToTarget", params: { targetId: "GUEST" } });
806 expect(yield* Queue.take(replies)).toBe(1);
807 yield* raw({
808 id: 2,
809 method: "Target.detachFromTarget",
810 params: { sessionId: "t3-preview-page-1" },
811 });
812 expect(yield* Queue.take(replies)).toBe(2);
813 yield* raw({ id: 3, method: "Page.startScreencast", sessionId: "t3-preview-page-1" });
814 yield* settle;
815 debuggee.release();
816 yield* settle;
817 yield* TestClock.adjust("1 minute");
818 expect(debuggee.focusEmulation).toEqual([true, false]);
819 }).pipe(Effect.scoped),
820 );
821​
822 it.effect("lets a guest throttle once the session waiting on its command detaches", () =>
823 Effect.gen(function* () {
824 const host = yield* DesktopBrowserHost.make;
825 const debuggee = makeDebuggee();
826 host.attach(key, debuggee.tab);
827 const replies = yield* watchReplies(host);
828 const raw = (message: Record<string, unknown>) =>
829 host.handleCommandLine(encodeJson({ type: "cdp", ...key, message: encodeJson(message) }));
830​
831 // A viewer's own CDP session, closed while its still of a hidden guest is pending.
832 yield* raw({ id: 1, method: "Target.attachToTarget", params: { targetId: "GUEST" } });
833 expect(yield* Queue.take(replies)).toBe(1);
834 yield* raw({ id: 2, method: "Page.captureScreenshot", sessionId: "t3-preview-page-1" });
835 yield* settle;
836 yield* TestClock.adjust("1 minute");
837 expect(debuggee.focusEmulation).toEqual([true]);
838​
839 yield* raw({
840 id: 3,
841 method: "Target.detachFromTarget",
842 params: { sessionId: "t3-preview-page-1" },
843 });
844 yield* TestClock.adjust("1 minute");
845 expect(debuggee.focusEmulation).toEqual([true, false]);
846 }).pipe(Effect.scoped),
847 );
848​
849 it.effect("never sends a screenshot queued by a session that has since detached", () =>
850 Effect.gen(function* () {
851 const host = yield* DesktopBrowserHost.make;
852 const debuggee = makeDebuggee();
853 host.attach(key, debuggee.tab);
854 const replies = yield* watchReplies(host);
855 const raw = (message: Record<string, unknown>) =>
856 host.handleCommandLine(encodeJson({ type: "cdp", ...key, message: encodeJson(message) }));
857​
858 yield* raw({ id: 1, method: "Target.attachToTarget", params: { targetId: "GUEST" } });
859 expect(yield* Queue.take(replies)).toBe(1);
860 yield* command(host, 2, "Page.captureScreenshot");
861 yield* raw({ id: 3, method: "Page.captureScreenshot", sessionId: "t3-preview-page-1" });
862 yield* raw({
863 id: 4,
864 method: "Target.detachFromTarget",
865 params: { sessionId: "t3-preview-page-1" },
866 });
867 yield* settle;
868 debuggee.release();
869 expect(yield* Queue.take(replies)).toBe(4);
870 // The dropped screenshot's error reply leaves as the first frees its turn.
871 expect([yield* Queue.take(replies), yield* Queue.take(replies)]).toEqual([3, 2]);
872 yield* settle;
873 expect(debuggee.sent).toEqual([2]);
874​
875 yield* TestClock.adjust("1 minute");
876 expect(debuggee.focusEmulation).toEqual([true, false]);
877 }).pipe(Effect.scoped),
878 );
879​
880 it.effect("keeps a streamed guest focused until its screencast ends", () =>
881 Effect.gen(function* () {
882 const host = yield* DesktopBrowserHost.make;
883 const debuggee = makeDebuggee();
884 host.attach(key, debuggee.tab);
885​
886 yield* command(host, 1, "Page.startScreencast");
887 yield* TestClock.adjust("1 minute");
888 expect(debuggee.focusEmulation).toEqual([true]);
889​
890 yield* command(host, 2, "Page.stopScreencast");
891 yield* settle;
892 debuggee.release();
893 yield* settle;
894 yield* TestClock.adjust("1 minute");
895 expect(debuggee.focusEmulation).toEqual([true, false]);
896​
897 // A viewer whose connection went away without stopping its screencast.
898 yield* command(host, 3, "Page.startScreencast");
899 yield* settle;
900 debuggee.release();
901 yield* settle;
902 yield* TestClock.adjust("1 minute");
903 yield* host.handleCommandLine(encodeJson({ type: "release", ...key }));
904 yield* TestClock.adjust("1 minute");
905 expect(debuggee.focusEmulation).toEqual([true, false, true, false]);
906 }),
907 );
908​
909 it.effect("keeps a guest focused while any of its screencasts runs", () =>
910 Effect.gen(function* () {
911 const host = yield* DesktopBrowserHost.make;
912 const debuggee = makeDebuggee();
913 host.attach(key, debuggee.tab);
914 const replies = yield* watchReplies(host);
915 const raw = (message: Record<string, unknown>) =>
916 host.handleCommandLine(encodeJson({ type: "cdp", ...key, message: encodeJson(message) }));
917​
918 // Two viewers, or a viewer and a recording, each on its own session.
919 yield* raw({ id: 1, method: "Target.attachToTarget", params: { targetId: "GUEST" } });
920 yield* raw({ id: 2, method: "Target.attachToTarget", params: { targetId: "GUEST" } });
921 expect([yield* Queue.take(replies), yield* Queue.take(replies)]).toEqual([1, 2]);
922 yield* raw({ id: 3, method: "Page.startScreencast", sessionId: "t3-preview-page-1" });
923 yield* raw({ id: 4, method: "Page.startScreencast", sessionId: "t3-preview-page-2" });
924 yield* raw({ id: 5, method: "Page.stopScreencast", sessionId: "t3-preview-page-1" });
925 yield* settle;
926 debuggee.release();
927 yield* settle;
928 yield* TestClock.adjust("1 minute");
929 expect(debuggee.focusEmulation).toEqual([true]);
930​
931 yield* raw({
932 id: 6,
933 method: "Target.detachFromTarget",
934 params: { sessionId: "t3-preview-page-2" },
935 });
936 yield* TestClock.adjust("1 minute");
937 expect(debuggee.focusEmulation).toEqual([true, false]);
938 }).pipe(Effect.scoped),
939 );
940​
941 it.effect("paints a hidden guest only while the screenshot it is taking is pending", () =>
942 Effect.gen(function* () {
943 const host = yield* DesktopBrowserHost.make;
944 const debuggee = makeDebuggee();
945 host.attach(key, debuggee.tab);
946 const painting = yield* watchPainting(host);
947​
948 yield* command(host, 1, "DOM.enable");
949 yield* settle;
950 expect(painting).toEqual([]);
951​
952 yield* command(host, 2, "Page.captureScreenshot");
953 yield* command(host, 3, "Page.captureScreenshot");
954 yield* settle;
955 expect(painting).toEqual([true]);
956​
957 // The first answers once the guest paints; the queued one then takes its turn.
958 debuggee.release();
959 yield* settle;
960 expect(debuggee.sent).toEqual([1, 2, 3]);
961 debuggee.release();
962 yield* settle;
963 expect(painting).toEqual([true, false]);
964 }).pipe(Effect.scoped),
965 );
966​
967 it.effect("stops painting for a screenshot Chromium never answers once its document goes", () =>
968 Effect.gen(function* () {
969 const host = yield* DesktopBrowserHost.make;
970 const debuggee = makeDebuggee();
971 host.attach(key, debuggee.tab);
972 const painting = yield* watchPainting(host);
973 const turn = (active: boolean) =>
974 host.handleCommandLine(encodeJson({ type: "turn", threadId: key.threadId, active }));
975​
976 // The server gave up on it, and the agent's turn ends; Chromium still holds the capture.
977 yield* turn(true);
978 yield* command(host, 1, "Page.captureScreenshot");
979 yield* settle;
980 yield* turn(false);
981 yield* settle;
982 expect(painting).toEqual([true]);
983​
984 // The page loads a new document, which also frees the screenshot turn.
985 debuggee.navigate();
986 expect(painting).toEqual([true, false]);
987 yield* command(host, 2, "Page.captureScreenshot");
988 yield* settle;
989 debuggee.navigate();
990 yield* settle;
991 expect(painting).toEqual([true, false, true, false]);
992​
993 // The server releases the relay; Chromium still holds the capture.
994 yield* command(host, 3, "Page.captureScreenshot");
995 yield* settle;
996 yield* host.handleCommandLine(encodeJson({ type: "release", ...key }));
997 yield* settle;
998 expect(painting).toEqual([true, false, true, false, true]);
999​
1000 // The guest detaches; a replacement starts with no screenshot pending.
1001 const replacement = makeDebuggee();
1002 host.attach(key, replacement.tab);
1003 expect(painting).toEqual([true, false, true, false, true, false]);
1004 yield* command(host, 4, "Page.captureScreenshot");
1005 yield* settle;
1006 host.detach(key);
1007 yield* settle;
1008 expect(painting).toEqual([true, false, true, false, true, false, true, false]);
1009 }).pipe(Effect.scoped),
1010 );
1011​
1012 it.effect("paints a streamed guest until its last screencast ends", () =>
1013 Effect.gen(function* () {
1014 const host = yield* DesktopBrowserHost.make;
1015 const debuggee = makeDebuggee();
1016 host.attach(key, debuggee.tab);
1017 const replies = yield* watchReplies(host);
1018 const painting = yield* watchPainting(host);
1019 const raw = (message: Record<string, unknown>) =>
1020 host.handleCommandLine(encodeJson({ type: "cdp", ...key, message: encodeJson(message) }));
1021​
1022 yield* raw({ id: 1, method: "Target.attachToTarget", params: { targetId: "GUEST" } });
1023 yield* raw({ id: 2, method: "Target.attachToTarget", params: { targetId: "GUEST" } });
1024 expect([yield* Queue.take(replies), yield* Queue.take(replies)]).toEqual([1, 2]);
1025 yield* raw({ id: 3, method: "Page.startScreencast", sessionId: "t3-preview-page-1" });
1026 yield* raw({ id: 4, method: "Page.startScreencast", sessionId: "t3-preview-page-2" });
1027 yield* raw({ id: 5, method: "Page.stopScreencast", sessionId: "t3-preview-page-1" });
1028 yield* settle;
1029 debuggee.release();
1030 yield* settle;
1031 expect(painting).toEqual([true]);
1032​
1033 yield* raw({
1034 id: 6,
1035 method: "Target.detachFromTarget",
1036 params: { sessionId: "t3-preview-page-2" },
1037 });
1038 yield* settle;
1039 expect(painting).toEqual([true, false]);
1040 }).pipe(Effect.scoped),
1041 );
1042​
1043 it.effect("saves a server tab's download under its CDP guid where the server asked", () =>
1044 Effect.gen(function* () {
1045 const host = yield* DesktopBrowserHost.make;
1046 const debuggee = makeDebuggee();
1047 host.attach(key, debuggee.tab);
1048 const replies = yield* watchReplies(host);
1049 const paths: Array<string> = [];
1050 const item = {
1051 setSavePath: (path: string) => void paths.push(path),
1052 } as unknown as Electron.DownloadItem;
1053​
1054 expect(host.placeDownload(debuggee.tab.webContents, item)).toBe(false);
1055 const sent = debuggee.nextSend();
1056 yield* host.handleCommandLine(
1057 encodeJson({
1058 type: "cdp",
1059 ...key,
1060 message: encodeJson({
1061 id: 1,
1062 method: "Browser.setDownloadBehavior",
1063 params: { behavior: "allowAndName", downloadPath: "/srv/downloads" },
1064 }),
1065 }),
1066 );
1067 yield* Effect.promise(() => sent);
1068 debuggee.release();
1069 expect(yield* Queue.take(replies)).toBe(1);
1070 debuggee.emit("Page.downloadWillBegin", { guid: "guid-1", suggestedFilename: "r.csv" });
1071 expect(host.placeDownload(makeDebuggee().tab.webContents, item)).toBe(false);
1072 expect(host.placeDownload(debuggee.tab.webContents, item)).toBe(true);
1073 expect(paths).toEqual(["/srv/downloads/guid-1"]);
1074 expect(host.placeDownload(debuggee.tab.webContents, item)).toBe(false);
1075 }).pipe(Effect.scoped),
1076 );
1077​
1078 it.effect("tells a download the person clicked from one the agent's input started", () =>
1079 Effect.gen(function* () {
1080 const host = yield* DesktopBrowserHost.make;
1081 const debuggee = makeDebuggee();
1082 host.attach(key, debuggee.tab);
1083​
1084 yield* host.handleCommandLine(
1085 encodeJson({
1086 type: "cdp",
1087 ...key,
1088 message: encodeJson({ id: 1, method: "DOM.getDocument", params: {} }),
1089 }),
1090 );
1091 expect(host.humanStartedDownload(debuggee.tab.webContents)).toBe(true);
1092 yield* host.handleCommandLine(
1093 encodeJson({
1094 type: "cdp",
1095 ...key,
1096 message: encodeJson({
1097 id: 2,
1098 method: "Input.dispatchMouseEvent",
1099 params: { type: "mousePressed", x: 1, y: 1 },
1100 }),
1101 }),
1102 );
1103 expect(host.humanStartedDownload(debuggee.tab.webContents)).toBe(false);
1104 }),
1105 );
1106​
1107 it.effect("detaches only the tab whose CDP event is too large to relay", () =>
1108 Effect.gen(function* () {
1109 const host = yield* DesktopBrowserHost.make;
1110 const tabs = [makeDebuggee({ deferFocus: true }), makeDebuggee()];
1111 const send = vi.spyOn(tabs[0]!.tab.debugger, "sendCommand");
1112 const keys = [key, { threadId: "thread-2", tabId: "tab-2" }];
1113 const events = yield* Queue.unbounded<DesktopBrowserEvent>();
1114 yield* host.events.pipe(
1115 Stream.runForEach((line) =>
1116 Queue.offer(events, decodeEvent(new TextDecoder().decode(line))),
1117 ),
1118 Effect.forkScoped,
1119 );
1120 for (const [index, tabKey] of keys.entries()) {
1121 host.attach(tabKey, tabs[index]!.tab);
1122 expect(yield* Queue.take(events)).toEqual({ type: "attached", ...tabKey });
1123 yield* host.handleCommandLine(
1124 encodeJson({
1125 type: "cdp",
1126 ...tabKey,
1127 message: encodeJson({ id: 1, method: "Target.setAutoAttach" }),
1128 }),
1129 );
1130 // The command's reply and the page announcement.
1131 yield* Queue.takeN(events, 2);
1133​
1134 yield* host.handleCommandLine(
1135 encodeJson({
1136 type: "cdp",
1137 ...key,
1138 message: encodeJson({
1139 id: 2,
1140 method: "Target.setAutoAttach",
1141 params: { autoAttach: true, waitForDebuggerOnStart: true, flatten: true },
1142 sessionId: "t3-preview-page",
1143 }),
1144 }),
1145 );
1146 yield* command(host, 3, "Page.startScreencast");
1147 tabs[0]!.emit("Runtime.consoleAPICalled", {
1148 args: Array(2048).fill("x".repeat(40 * 1024)),
1149 });
1150 tabs[1]!.emit("Page.loadEventFired", { timestamp: 1 });
1151 const [detached, next] = yield* Queue.takeN(events, 2);
1152​
1153 const { type, threadId, tabId } = detached!;
1154 expect({ type, threadId, tabId }).toEqual({ type: "detached", ...key });
1155 expect(send).toHaveBeenCalledWith("Target.setAutoAttach", {
1156 autoAttach: false,
1157 waitForDebuggerOnStart: false,
1158 flatten: true,
1159 });
1160 tabs[0]!.settleFocus();
1161 yield* settle;
1162 expect(send).not.toHaveBeenCalledWith("Target.setAutoAttach", {
1163 autoAttach: true,
1164 waitForDebuggerOnStart: true,
1165 flatten: true,
1166 });
1167 expect(send).toHaveBeenCalledWith("Page.stopScreencast");
1168 expect(tabs[0]!.focusEmulation).toEqual([true, false]);
1169 expect(next).toMatchObject({ type: "cdp", ...keys[1] });
1170 expect(JSON.parse((next as { message: string }).message)).toMatchObject({
1171 method: "Page.loadEventFired",
1172 });
1173 }).pipe(Effect.scoped),
1174 );
1175});