11import { it as effectIt } from "@effect/vitest";
22import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
3- import { DESKTOP_PREVIEW_RECORDING_CAPTURE_TRIGGER } from "@t3tools/contracts";
3+ import {
4+ DesktopBrowserEvent,
5+ DESKTOP_PREVIEW_RECORDING_CAPTURE_TRIGGER,
6+ DESKTOP_PREVIEW_SCREENSHOT_ACTIVITY,
7+ } from "@t3tools/contracts";
48import type {
59 DesktopPreviewRecordingFrame,
610 DesktopPreviewRecordingInputEvent,
@@ -16,6 +20,7 @@ import * as Exit from "effect/Exit";
1620import * as FileSystem from "effect/FileSystem";
1721import * as Fiber from "effect/Fiber";
1822import * as Queue from "effect/Queue";
23+ import * as Schema from "effect/Schema";
1924import * as Stream from "effect/Stream";
2025import * as Layer from "effect/Layer";
2126import * as Logger from "effect/Logger";
@@ -411,6 +416,100 @@ const makeTestPreviewWebContents = (
411416 } as unknown as TestPreviewWebContents;
412417};
413418
419+ const decodeBrowserEvent = Schema.decodeUnknownSync(Schema.fromJsonString(DesktopBrowserEvent));
420+
421+ const setupAgentScreenshot = Effect.fnUntraced(function* () {
422+ const manager = yield* PreviewManager.PreviewManager;
423+ const host = yield* DesktopBrowserHost.DesktopBrowserHost;
424+ const serverTab = { threadId: "thread-capture", tabId: "server-capture" };
425+ const replies = yield* Queue.unbounded<string>();
426+ const attached = yield* Queue.unbounded<void>();
427+ yield* host.events.pipe(
428+ Stream.runForEach((line) => {
429+ const event = decodeBrowserEvent(new TextDecoder().decode(line));
430+ if (event.type === "attached") return Queue.offer(attached, undefined);
431+ return event.type === "cdp" ? Queue.offer(replies, event.message) : Effect.void;
432+ }),
433+ Effect.forkScoped,
434+ );
435+ const preparing = Promise.withResolvers<void>();
436+ const ready = Promise.withResolvers<boolean>();
437+ const copying = Promise.withResolvers<void>();
438+ const copy = Promise.withResolvers<TestCapturedPreviewImage>();
439+ const capture = Promise.withResolvers<{ data: string }>();
440+ const leases = new Set<string>();
441+ const hostWebContents = makeTestHostWebContents();
442+ hostWebContents.executeJavaScript.mockImplementation(async (script: string) => {
443+ let acquiring = false;
444+ const prepared = new Function("globalThis", `return ${script}`)({
445+ [DESKTOP_PREVIEW_SCREENSHOT_ACTIVITY]: (_tabId: string, token: string, active: boolean) => {
446+ if (active) {
447+ acquiring = true;
448+ leases.add(token);
449+ preparing.resolve();
450+ } else {
451+ leases.delete(token);
452+ }
453+ return true;
454+ },
455+ });
456+ return acquiring ? ready.promise : prepared;
457+ });
458+ const capturePage = vi.fn(() => {
459+ copying.resolve();
460+ return copy.promise;
461+ });
462+ const wc = Object.assign(makeTestPreviewWebContents(capturePage, 42, hostWebContents), {
463+ isDevToolsOpened: () => false,
464+ getUserAgent: () => "Electron",
465+ loadURL: vi.fn(async () => {}),
466+ });
467+ vi.mocked(wc.debugger.sendCommand).mockImplementation(async (method: string) =>
468+ method === "Page.captureScreenshot" ? capture.promise : {},
469+ );
470+ fromId.mockReturnValue(wc);
471+ const setBackgroundThrottling = vi.fn();
472+ yield* manager.setMainWindow({
473+ isDestroyed: () => false,
474+ once: vi.fn(),
475+ webContents: Object.assign(hostWebContents, { setBackgroundThrottling }),
476+ } as never);
477+ yield* manager.createTab("tab_capture", { serverTab });
478+ yield* manager.registerWebview("tab_capture", wc.id);
479+ yield* Queue.take(attached);
480+ const send = (id: number) =>
481+ host.handleCommandLine(
482+ JSON.stringify({
483+ type: "cdp",
484+ ...serverTab,
485+ message: JSON.stringify({
486+ id,
487+ method: "Page.captureScreenshot",
488+ params: { format: "png" },
489+ sessionId: "t3-preview-page",
490+ }),
491+ }),
492+ );
493+ return {
494+ manager,
495+ host,
496+ wc,
497+ serverTab,
498+ replies,
499+ attached,
500+ preparing,
501+ ready,
502+ copying,
503+ copy,
504+ capture,
505+ leases,
506+ hostWebContents,
507+ capturePage,
508+ setBackgroundThrottling,
509+ send,
510+ };
511+ });
512+
414513const setupRecordingRaceTabs = (manager: PreviewManager.PreviewManager["Service"]) =>
415514 Effect.gen(function* () {
416515 const capturePage = vi.fn(async () => ({
@@ -3011,6 +3110,180 @@ describe("PreviewManager", () => {
30113110 ),
30123111 );
30133112
3113+ effectIt.effect.each(["preparing", "capturing"] as const)(
3114+ "keeps agent capture ownership across navigation while %s and a later guest replacement",
3115+ (phase) =>
3116+ Effect.gen(function* () {
3117+ const {
3118+ manager,
3119+ wc,
3120+ replies,
3121+ attached,
3122+ preparing,
3123+ ready,
3124+ copying,
3125+ copy,
3126+ capture,
3127+ leases,
3128+ hostWebContents,
3129+ capturePage,
3130+ setBackgroundThrottling,
3131+ send,
3132+ } = yield* setupAgentScreenshot();
3133+ yield* send(1);
3134+ yield* TestClock.adjust(0);
3135+ yield* Effect.promise(() => preparing.promise);
3136+ expect(setBackgroundThrottling).toHaveBeenLastCalledWith(false);
3137+ if (phase === "capturing") {
3138+ ready.resolve(true);
3139+ yield* Effect.promise(() => copying.promise);
3140+ }
3141+ yield* manager.navigate("tab_capture", "https://example.com/next");
3142+ expect(leases.size).toBe(1);
3143+ ready.resolve(true);
3144+ yield* Effect.promise(() => copying.promise);
3145+ capture.resolve({ data: "fresh-frame" });
3146+ expect(JSON.parse(yield* Queue.take(replies))).toMatchObject({
3147+ id: 1,
3148+ result: { data: "fresh-frame" },
3149+ });
3150+ expect(leases.size).toBe(0);
3151+ expect(capturePage).toHaveBeenCalledExactlyOnceWith(undefined, { stayHidden: true });
3152+ expect(setBackgroundThrottling).toHaveBeenLastCalledWith(true);
3153+ const replacement = Object.assign(
3154+ makeTestPreviewWebContents(capturePage, 43, hostWebContents),
3155+ {
3156+ isDevToolsOpened: () => false,
3157+ getUserAgent: () => "Electron",
3158+ },
3159+ );
3160+ vi.mocked(replacement.debugger.sendCommand).mockResolvedValue({
3161+ data: "replacement-frame",
3162+ });
3163+ fromId.mockImplementation((id) => (id === replacement.id ? replacement : wc));
3164+ yield* manager.registerWebview("tab_capture", replacement.id);
3165+ yield* Queue.take(attached);
3166+ yield* send(2);
3167+ expect(JSON.parse(yield* Queue.take(replies))).toMatchObject({
3168+ id: 2,
3169+ result: { data: "replacement-frame" },
3170+ });
3171+ expect(leases.size).toBe(0);
3172+ const copies = capturePage.mock.calls.length;
3173+ copy.resolve({
3174+ toJPEG: () => Buffer.from("unused"),
3175+ getSize: () => ({ width: 800, height: 600 }),
3176+ });
3177+ yield* TestClock.adjust(100);
3178+ expect(capturePage).toHaveBeenCalledTimes(copies);
3179+ }).pipe(Effect.provide(managerLayer()), Effect.scoped),
3180+ );
3181+
3182+ effectIt.effect(
3183+ "expires queued agent screenshots before starting their renderer preparation",
3184+ () =>
3185+ Effect.gen(function* () {
3186+ const h = yield* setupAgentScreenshot();
3187+ const release = Promise.withResolvers<boolean>();
3188+ h.hostWebContents.executeJavaScript
3189+ .mockImplementationOnce(async () => {
3190+ h.preparing.resolve();
3191+ return h.ready.promise;
3192+ })
3193+ .mockImplementation(async () => release.promise);
3194+ yield* h.send(1);
3195+ yield* Effect.promise(() => h.preparing.promise);
3196+ yield* h.send(2);
3197+ yield* TestClock.adjust("4 seconds");
3198+ const replies = [
3199+ JSON.parse(yield* Queue.take(h.replies)),
3200+ JSON.parse(yield* Queue.take(h.replies)),
3201+ ];
3202+ expect(replies.map((reply) => reply.id).sort()).toEqual([1, 2]);
3203+ expect(replies.every((reply) => reply.error)).toBe(true);
3204+ expect(h.hostWebContents.executeJavaScript).toHaveBeenCalledTimes(2);
3205+ expect(h.wc.debugger.sendCommand).not.toHaveBeenCalledWith(
3206+ "Page.captureScreenshot",
3207+ expect.anything(),
3208+ );
3209+ expect(h.capturePage).not.toHaveBeenCalled();
3210+ h.ready.resolve(true);
3211+ release.resolve(true);
3212+ yield* TestClock.adjust(0);
3213+ expect(h.hostWebContents.executeJavaScript).toHaveBeenCalledTimes(2);
3214+ expect(h.setBackgroundThrottling).toHaveBeenLastCalledWith(true);
3215+ }).pipe(Effect.provide(managerLayer()), Effect.scoped),
3216+ );
3217+
3218+ effectIt.effect("preserves native screenshot errors through the Manager and CDP relay", () =>
3219+ Effect.gen(function* () {
3220+ const h = yield* setupAgentScreenshot();
3221+ h.ready.resolve(true);
3222+ yield* h.send(1);
3223+ yield* Effect.promise(() => h.copying.promise);
3224+ h.capture.reject(new Error("compositor unavailable"));
3225+ expect(JSON.parse(yield* Queue.take(h.replies))).toMatchObject({
3226+ id: 1,
3227+ error: { code: -32000, message: "compositor unavailable" },
3228+ });
3229+ expect(h.leases.size).toBe(0);
3230+ expect(h.wc.setBackgroundThrottling).not.toHaveBeenCalled();
3231+ expect(h.setBackgroundThrottling).toHaveBeenLastCalledWith(true);
3232+ h.copy.resolve({
3233+ toJPEG: () => Buffer.from("unused"),
3234+ getSize: () => ({ width: 800, height: 600 }),
3235+ });
3236+ yield* TestClock.adjust(100);
3237+ expect(h.capturePage).toHaveBeenCalledTimes(1);
3238+ }).pipe(Effect.provide(managerLayer()), Effect.scoped),
3239+ );
3240+
3241+ effectIt.effect.each(["preparing", "capturing"] as const)(
3242+ "cancels a replaced guest while %s and ignores its late completion",
3243+ (phase) =>
3244+ Effect.gen(function* () {
3245+ const h = yield* setupAgentScreenshot();
3246+ yield* h.send(1);
3247+ yield* Effect.promise(() => h.preparing.promise);
3248+ if (phase === "capturing") {
3249+ h.ready.resolve(true);
3250+ yield* Effect.promise(() => h.copying.promise);
3251+ }
3252+ const replacement = Object.assign(
3253+ makeTestPreviewWebContents(h.capturePage, 43, h.hostWebContents),
3254+ {
3255+ isDevToolsOpened: () => false,
3256+ getUserAgent: () => "Electron",
3257+ },
3258+ );
3259+ vi.mocked(replacement.debugger.sendCommand).mockResolvedValue({
3260+ data: "replacement-frame",
3261+ });
3262+ fromId.mockImplementation((id) => (id === replacement.id ? replacement : h.wc));
3263+ yield* h.manager.registerWebview("tab_capture", replacement.id);
3264+ yield* Queue.take(h.attached);
3265+ expect(h.leases.size).toBe(0);
3266+ expect(replacement.setBackgroundThrottling).not.toHaveBeenCalled();
3267+ h.ready.resolve(true);
3268+ h.capture.resolve({ data: "late-old-frame" });
3269+ h.copy.resolve({
3270+ toJPEG: () => Buffer.from("unused"),
3271+ getSize: () => ({ width: 800, height: 600 }),
3272+ });
3273+ yield* TestClock.adjust(100);
3274+ expect(yield* Queue.poll(h.replies)).toEqual(Option.none());
3275+ expect(h.capturePage).toHaveBeenCalledTimes(phase === "capturing" ? 1 : 0);
3276+ yield* h.send(2);
3277+ expect(JSON.parse(yield* Queue.take(h.replies))).toMatchObject({
3278+ id: 2,
3279+ result: { data: "replacement-frame" },
3280+ });
3281+ expect(h.leases.size).toBe(0);
3282+ expect(h.wc.setBackgroundThrottling).not.toHaveBeenCalled();
3283+ expect(h.setBackgroundThrottling).toHaveBeenLastCalledWith(true);
3284+ }).pipe(Effect.provide(managerLayer()), Effect.scoped),
3285+ );
3286+
30143287 effectIt.effect("tells the server when a tab it renders natively closes", () =>
30153288 Effect.gen(function* () {
30163289 const host = yield* DesktopBrowserHost.DesktopBrowserHost;
0 commit comments