Files
t3code/apps/desktop/src/preview/Manager.test.ts
T

2072 lines
75 KiB
TypeScript

import { it as effectIt } from "@effect/vitest";
import type { DesktopPreviewRecordingFrame } from "@t3tools/contracts";
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
import * as Cause from "effect/Cause";
import * as Deferred from "effect/Deferred";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as FileSystem from "effect/FileSystem";
import * as Fiber from "effect/Fiber";
import * as Layer from "effect/Layer";
import * as Logger from "effect/Logger";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
import * as Schema from "effect/Schema";
import type * as Scope from "effect/Scope";
import { TestClock } from "effect/testing";
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
import * as ElectronWindow from "../electron/ElectronWindow.ts";
import * as BrowserSession from "./BrowserSession.ts";
import * as PreviewManager from "./Manager.ts";
describe("fitPictureInPictureContentSize", () => {
it("preserves the PiP content area across aspect-ratio changes", () => {
expect(PreviewManager.fitPictureInPictureContentSize([480, 320], 16 / 9)).toEqual([523, 294]);
expect(PreviewManager.fitPictureInPictureContentSize([480, 320], 9 / 16)).toEqual([294, 523]);
});
it("does not collapse toward the minimum size when orientation changes repeatedly", () => {
const portrait = PreviewManager.fitPictureInPictureContentSize([523, 294], 9 / 16);
const landscape = PreviewManager.fitPictureInPictureContentSize(portrait, 16 / 9);
expect(portrait).toEqual([294, 523]);
expect(landscape).toEqual([523, 294]);
});
});
describe("isPreviewRefreshShortcut", () => {
const input = (overrides: Partial<Electron.Input> = {}) =>
({
type: "keyDown",
key: "r",
meta: true,
control: false,
shift: false,
alt: false,
...overrides,
}) as Electron.Input;
it("recognizes the platform refresh chord without matching modified variants", () => {
expect(PreviewManager.isPreviewRefreshShortcut(input())).toBe(true);
expect(PreviewManager.isPreviewRefreshShortcut(input({ meta: false, control: true }))).toBe(
true,
);
expect(PreviewManager.isPreviewRefreshShortcut(input({ shift: true }))).toBe(false);
expect(PreviewManager.isPreviewRefreshShortcut(input({ type: "keyUp" }))).toBe(false);
});
});
const {
browserWindowConstructor,
createFromPath,
fromId,
getFocusedWebContents,
mkdir,
showItemInFolder,
webviewSend,
writeFile,
writeImage,
} = vi.hoisted(() => ({
browserWindowConstructor: vi.fn(),
createFromPath: vi.fn((): { readonly isEmpty: () => boolean } => ({ isEmpty: () => false })),
fromId: vi.fn((_id?: number) => null),
getFocusedWebContents: vi.fn(() => null),
mkdir: vi.fn((_path: string) => undefined),
showItemInFolder: vi.fn(),
webviewSend: vi.fn(),
writeFile: vi.fn((_path: string, _data: Uint8Array) => undefined),
writeImage: vi.fn(),
}));
vi.mock("electron", () => ({
BrowserWindow: browserWindowConstructor,
clipboard: {
writeImage,
},
nativeImage: {
createFromPath,
},
shell: {
showItemInFolder,
},
session: {
fromPartition: vi.fn(),
},
webContents: {
fromId,
getFocusedWebContents,
},
}));
const browserSessionLayer = Layer.succeed(
BrowserSession.BrowserSession,
BrowserSession.BrowserSession.of({
getPartition: () => Effect.succeed("persist:t3code-preview-test"),
isPartition: (partition) => partition.startsWith("persist:t3code-preview-"),
getSession: () => Effect.die("unexpected getSession"),
clearCookies: () => Effect.void,
clearCache: () => Effect.void,
}),
);
const environmentLayer = Layer.succeed(
DesktopEnvironment.DesktopEnvironment,
DesktopEnvironment.DesktopEnvironment.of({
browserArtifactsDir: "/tmp/t3/dev/browser-artifacts",
dirname: "/tmp/t3/desktop",
path: {
join: (...parts: ReadonlyArray<string>) => parts.join("/"),
},
} as DesktopEnvironment.DesktopEnvironment["Service"]),
);
const fileSystemLayer = FileSystem.layerNoop({
makeDirectory: (path) =>
Effect.sync(() => {
mkdir(path);
}),
writeFile: (path, data) =>
Effect.sync(() => {
writeFile(path, data);
}),
});
const layer = PreviewManager.layer.pipe(
Layer.provideMerge(browserSessionLayer),
Layer.provideMerge(environmentLayer),
Layer.provideMerge(fileSystemLayer),
Layer.provideMerge(Path.layer),
Layer.provideMerge(Layer.succeed(HostProcessPlatform, "darwin")),
);
const encodePreviewManagerError = Schema.encodeSync(PreviewManager.PreviewManagerError);
const withManager = <A>(
use: (
manager: PreviewManager.PreviewManager["Service"],
) => Effect.Effect<A, PreviewManager.PreviewManagerError, Scope.Scope>,
) =>
Effect.gen(function* () {
const manager = yield* PreviewManager.PreviewManager;
return yield* use(manager);
}).pipe(Effect.provide(layer), Effect.scoped);
interface TestCapturedPreviewImage {
readonly toJPEG: () => Buffer;
readonly getSize: () => { readonly width: number; readonly height: number };
}
const makeTestPreviewWebContents = (
capturePage: () => Promise<TestCapturedPreviewImage>,
id = 42,
) =>
({
id,
isDestroyed: () => false,
getType: () => "webview",
getURL: () => "https://example.com",
getTitle: () => "Example",
isLoading: () => false,
getZoomFactor: () => 1,
setZoomFactor: vi.fn(),
on: vi.fn(),
off: vi.fn(),
ipc: { on: vi.fn(), off: vi.fn() },
send: webviewSend,
navigationHistory: { canGoBack: () => false, canGoForward: () => false },
setWindowOpenHandler: vi.fn(),
debugger: {
isAttached: () => false,
attach: vi.fn(),
sendCommand: vi.fn(async () => undefined),
on: vi.fn(),
off: vi.fn(),
},
capturePage,
}) as never;
const makeTestPictureInPictureWindow = (loadURL: () => Promise<void> = async () => undefined) => {
const listeners = new Map<string, () => void>();
const send = vi.fn();
let destroyed = false;
const pictureInPictureWindow = {
isDestroyed: vi.fn(() => destroyed),
once: vi.fn((event: string, listener: () => void) => {
listeners.set(event, listener);
}),
setAlwaysOnTop: vi.fn(),
setVisibleOnAllWorkspaces: vi.fn(),
setAspectRatio: vi.fn(),
getContentSize: vi.fn(() => [480, 320]),
setContentSize: vi.fn(),
loadURL: vi.fn(loadURL),
showInactive: vi.fn(() => {
if (destroyed) throw new Error("Picture-in-picture window is closed.");
}),
close: vi.fn(() => {
if (destroyed) return;
destroyed = true;
listeners.get("closed")?.();
}),
webContents: {
send,
},
};
return { pictureInPictureWindow, send };
};
describe("PreviewManager", () => {
beforeEach(() => {
browserWindowConstructor.mockReset();
fromId.mockClear();
getFocusedWebContents.mockReset();
getFocusedWebContents.mockReturnValue(null);
mkdir.mockClear();
writeFile.mockClear();
showItemInFolder.mockClear();
writeImage.mockClear();
createFromPath.mockClear();
webviewSend.mockClear();
});
effectIt.effect("reports an unregistered webview as temporarily unavailable", () =>
withManager((manager) =>
Effect.gen(function* () {
expect(yield* manager.automationStatus("tab_1")).toEqual({
available: false,
visible: true,
tabId: "tab_1",
url: null,
title: null,
loading: false,
});
yield* manager.createTab("tab_1");
expect(yield* manager.automationStatus("tab_1")).toEqual({
available: false,
visible: true,
tabId: "tab_1",
url: null,
title: null,
loading: false,
});
expect(fromId).not.toHaveBeenCalled();
}),
),
);
effectIt.effect("isolates failed state listeners and continues delivery", () => {
const loggedErrors: Array<unknown> = [];
const logger = Logger.make(({ message }) => {
for (const value of Array.isArray(message) ? message : [message]) {
if (typeof value === "object" && value !== null && "cause" in value) {
loggedErrors.push(Cause.squash(value.cause as Cause.Cause<never>));
}
}
});
const deliveryError = new ElectronWindow.ElectronWindowOperationError({
operation: "send-window-message",
platform: "darwin",
windowId: 42,
channel: "preview:state-change",
cause: new Error("renderer unavailable"),
});
const delivered = vi.fn();
return withManager((manager) =>
Effect.gen(function* () {
yield* manager.subscribeStateChanges(() => Effect.die(deliveryError));
yield* manager.subscribeStateChanges((tabId, state) =>
Effect.sync(() => {
delivered(tabId, state);
}),
);
const state = yield* manager.createTab("tab_listener_failure");
expect(delivered).toHaveBeenCalledOnce();
expect(delivered).toHaveBeenCalledWith("tab_listener_failure", state);
expect(loggedErrors).toHaveLength(1);
expect(loggedErrors[0]).toBeInstanceOf(ElectronWindow.ElectronWindowOperationError);
expect(loggedErrors[0]).toMatchObject({
operation: "send-window-message",
windowId: 42,
channel: "preview:state-change",
});
}),
).pipe(
Effect.provide(
Logger.layer([logger], {
mergeWithExisting: false,
}),
),
);
});
effectIt.effect("does not swallow state listener interruption", () =>
withManager((manager) =>
Effect.gen(function* () {
const exit = yield* Effect.scoped(
Effect.gen(function* () {
yield* manager.subscribeStateChanges(() => Effect.interrupt);
return yield* Effect.exit(manager.createTab("tab_interrupted_listener"));
}),
);
expect(Exit.isFailure(exit)).toBe(true);
if (Exit.isFailure(exit)) {
expect(Cause.hasInterrupts(exit.cause)).toBe(true);
}
}),
),
);
effectIt.effect("queues navigation until the webview registers", () =>
withManager((manager) =>
Effect.gen(function* () {
const loadURL = vi.fn(async () => undefined);
const listeners = new Map<string, (...args: never[]) => void>();
fromId.mockReturnValue({
id: 42,
isDestroyed: () => false,
getType: () => "webview",
getURL: () => "about:blank",
getTitle: () => "",
isLoading: () => false,
getZoomFactor: () => 1,
setZoomFactor: vi.fn(),
loadURL,
on: vi.fn((event: string, listener: (...args: never[]) => void) => {
listeners.set(event, listener);
}),
off: vi.fn(),
ipc: { on: vi.fn(), off: vi.fn() },
send: webviewSend,
navigationHistory: { canGoBack: () => false, canGoForward: () => false },
setWindowOpenHandler: vi.fn(),
debugger: {
isAttached: () => false,
attach: vi.fn(),
sendCommand: vi.fn(async () => undefined),
on: vi.fn(),
off: vi.fn(),
},
} as never);
yield* manager.navigate("tab_pending", "localhost:3200");
expect(yield* manager.automationStatus("tab_pending")).toEqual({
available: false,
visible: true,
tabId: "tab_pending",
url: "http://localhost:3200/",
title: "",
loading: true,
});
yield* manager.registerWebview("tab_pending", 42);
yield* Effect.yieldNow;
expect(loadURL).toHaveBeenCalledOnce();
expect(loadURL).toHaveBeenCalledWith("http://localhost:3200/");
}),
),
);
effectIt.effect("mirrors Electron's effective zoom across registration and navigation", () =>
withManager((manager) =>
Effect.gen(function* () {
let effectiveZoom = 0.9;
let zoomReadable = true;
let url = "https://example.com";
const listeners = new Map<string, (...args: unknown[]) => void>();
const setZoomFactor = vi.fn();
fromId.mockReturnValue({
id: 42,
isDestroyed: () => false,
getType: () => "webview",
getURL: () => url,
getTitle: () => "Example",
isLoading: () => false,
getZoomFactor: () => {
if (!zoomReadable) throw new Error("zoom unavailable");
return effectiveZoom;
},
setZoomFactor,
on: vi.fn((event: string, listener: (...args: unknown[]) => void) => {
listeners.set(event, listener);
}),
off: vi.fn(),
ipc: { on: vi.fn(), off: vi.fn() },
send: webviewSend,
navigationHistory: { canGoBack: () => false, canGoForward: () => false },
setWindowOpenHandler: vi.fn(),
debugger: {
isAttached: () => false,
attach: vi.fn(),
sendCommand: vi.fn(async () => undefined),
on: vi.fn(),
off: vi.fn(),
},
} as never);
const states: PreviewManager.PreviewTabState[] = [];
yield* manager.subscribeStateChanges((_tabId, state) =>
Effect.sync(() => {
states.push(state);
}),
);
yield* manager.createTab("tab_zoom");
yield* manager.registerWebview("tab_zoom", 42);
expect(states.at(-1)?.zoomFactor).toBe(0.9);
expect(setZoomFactor).not.toHaveBeenCalled();
effectiveZoom = 1.25;
listeners.get("did-navigate")?.();
yield* Effect.yieldNow;
expect(states.at(-1)?.zoomFactor).toBe(1.25);
expect(setZoomFactor).not.toHaveBeenCalled();
zoomReadable = false;
url = "https://example.com/after-zoom-read-failed";
listeners.get("did-navigate")?.();
yield* Effect.yieldNow;
expect(states.at(-1)?.navStatus).toEqual({
kind: "Success",
url,
title: "Example",
});
expect(states.at(-1)?.zoomFactor).toBe(1.25);
const replacementSetZoomFactor = vi.fn();
fromId.mockReturnValue({
id: 43,
isDestroyed: () => false,
getType: () => "webview",
getURL: () => url,
getTitle: () => "Example",
isLoading: () => false,
getZoomFactor: () => 1,
setZoomFactor: replacementSetZoomFactor,
on: vi.fn(),
off: vi.fn(),
ipc: { on: vi.fn(), off: vi.fn() },
send: webviewSend,
navigationHistory: { canGoBack: () => false, canGoForward: () => false },
setWindowOpenHandler: vi.fn(),
debugger: {
isAttached: () => false,
attach: vi.fn(),
sendCommand: vi.fn(async () => undefined),
on: vi.fn(),
off: vi.fn(),
},
} as never);
yield* manager.registerWebview("tab_zoom", 43);
expect(replacementSetZoomFactor).toHaveBeenCalledWith(1.25);
expect(states.at(-1)?.zoomFactor).toBe(1.25);
}),
),
);
effectIt.effect("emulates prefers-color-scheme and re-applies it across webview swaps", () =>
withManager((manager) =>
Effect.gen(function* () {
const makeWebContents = (id: number) => {
const sendCommand = vi.fn(async () => undefined);
return {
sendCommand,
wc: {
id,
isDestroyed: () => false,
isDevToolsOpened: () => false,
getType: () => "webview",
getURL: () => "https://example.com",
getTitle: () => "Example",
isLoading: () => false,
getZoomFactor: () => 1,
setZoomFactor: vi.fn(),
on: vi.fn(),
off: vi.fn(),
ipc: { on: vi.fn(), off: vi.fn() },
send: webviewSend,
navigationHistory: { canGoBack: () => false, canGoForward: () => false },
setWindowOpenHandler: vi.fn(),
debugger: {
isAttached: () => false,
attach: vi.fn(),
sendCommand,
on: vi.fn(),
off: vi.fn(),
},
} as never,
};
};
const first = makeWebContents(42);
fromId.mockReturnValue(first.wc);
const states: PreviewManager.PreviewTabState[] = [];
yield* manager.subscribeStateChanges((_tabId, state) =>
Effect.sync(() => {
states.push(state);
}),
);
yield* manager.createTab("tab_scheme");
yield* manager.registerWebview("tab_scheme", 42);
yield* Effect.yieldNow;
yield* manager.setColorScheme("tab_scheme", "dark");
expect(first.sendCommand).toHaveBeenCalledWith("Emulation.setEmulatedMedia", {
features: [{ name: "prefers-color-scheme", value: "dark" }],
});
expect(states.at(-1)?.colorScheme).toBe("dark");
const replacement = makeWebContents(43);
fromId.mockReturnValue(replacement.wc);
yield* manager.registerWebview("tab_scheme", 43);
yield* Effect.yieldNow;
expect(replacement.sendCommand).toHaveBeenCalledWith("Emulation.setEmulatedMedia", {
features: [{ name: "prefers-color-scheme", value: "dark" }],
});
expect(states.at(-1)?.colorScheme).toBe("dark");
yield* manager.setColorScheme("tab_scheme", "system");
expect(replacement.sendCommand).toHaveBeenCalledWith("Emulation.setEmulatedMedia", {
features: [{ name: "prefers-color-scheme", value: "" }],
});
expect(states.at(-1)?.colorScheme).toBe("system");
}),
),
);
effectIt.effect("blocks late webview and capture starts during tab close", () =>
withManager((manager) =>
Effect.gen(function* () {
const capturePage = vi.fn(async () => ({
toJPEG: () => Buffer.from("close-race-frame"),
getSize: () => ({ width: 1280, height: 720 }),
}));
const firstWebContents = makeTestPreviewWebContents(capturePage, 42);
const replacementWebContents = makeTestPreviewWebContents(capturePage, 43);
const replacementListenerSpies = replacementWebContents as unknown as {
readonly on: ReturnType<typeof vi.fn>;
readonly off: ReturnType<typeof vi.fn>;
readonly ipc: { readonly off: ReturnType<typeof vi.fn> };
};
fromId.mockImplementation((id) => {
if (id === 42) return firstWebContents;
if (id === 43) return replacementWebContents;
return null;
});
const { pictureInPictureWindow } = makeTestPictureInPictureWindow();
browserWindowConstructor.mockImplementation(function () {
return pictureInPictureWindow;
});
yield* manager.createTab("tab_close_register_race");
yield* manager.registerWebview("tab_close_register_race", 42);
yield* manager.openPictureInPicture("tab_close_register_race");
const closeCleanupPaused = yield* Deferred.make<void>();
const continueCloseCleanup = yield* Deferred.make<void>();
yield* manager.subscribeStateChanges((_tabId, state) =>
!state.pictureInPicture && state.webContentsId === 42
? Deferred.succeed(closeCleanupPaused, undefined).pipe(
Effect.andThen(Deferred.await(continueCloseCleanup)),
)
: Effect.void,
);
const closeFiber = yield* manager
.closeTab("tab_close_register_race")
.pipe(Effect.forkChild({ startImmediately: true }));
yield* Deferred.await(closeCleanupPaused);
const recreateFiber = yield* manager
.createTab("tab_close_register_race")
.pipe(Effect.forkChild({ startImmediately: true }));
const registrationFiber = yield* manager
.registerWebview("tab_close_register_race", 43)
.pipe(Effect.forkChild({ startImmediately: true }));
yield* Effect.yieldNow;
expect(replacementListenerSpies.on).not.toHaveBeenCalled();
yield* manager.closeTab("tab_close_register_race");
const recordingExit = yield* Effect.exit(manager.startRecording("tab_close_register_race"));
yield* Deferred.succeed(continueCloseCleanup, undefined);
yield* Fiber.join(closeFiber);
const recreated = yield* Fiber.join(recreateFiber);
const registrationExit = yield* Fiber.await(registrationFiber);
for (const exit of [registrationExit, recordingExit]) {
expect(Exit.isFailure(exit)).toBe(true);
if (Exit.isSuccess(exit)) continue;
expect(Option.getOrThrow(Cause.findErrorOption(exit.cause))).toMatchObject({
_tag: "PreviewTabNotFoundError",
tabId: "tab_close_register_race",
});
}
expect(replacementListenerSpies.on).not.toHaveBeenCalled();
expect(replacementListenerSpies.off).not.toHaveBeenCalled();
expect(replacementListenerSpies.ipc.off).not.toHaveBeenCalled();
expect(capturePage).toHaveBeenCalledOnce();
expect(recreated.webContentsId).toBeNull();
}),
),
);
effectIt.effect("keeps a main-frame load failure visible until a retry starts", () =>
withManager((manager) =>
Effect.gen(function* () {
const url = "http://localhost:5733/";
let loading = false;
const listeners = new Map<string, (...args: unknown[]) => void>();
fromId.mockReturnValue({
id: 42,
isDestroyed: () => false,
getType: () => "webview",
getURL: () => url,
getTitle: () => "localhost:5733",
isLoading: () => loading,
getZoomFactor: () => 1,
setZoomFactor: vi.fn(),
on: vi.fn((event: string, listener: (...args: unknown[]) => void) => {
listeners.set(event, listener);
}),
off: vi.fn(),
ipc: { on: vi.fn(), off: vi.fn() },
send: webviewSend,
navigationHistory: { canGoBack: () => false, canGoForward: () => false },
setWindowOpenHandler: vi.fn(),
debugger: {
isAttached: () => false,
attach: vi.fn(),
sendCommand: vi.fn(async () => undefined),
on: vi.fn(),
off: vi.fn(),
},
} as never);
const statuses: PreviewManager.PreviewNavStatus[] = [];
yield* manager.subscribeStateChanges((_tabId, state) =>
Effect.sync(() => {
statuses.push(state.navStatus);
}),
);
yield* manager.createTab("tab_failed");
yield* manager.registerWebview("tab_failed", 42);
listeners.get("did-fail-load")?.(
{},
-105,
"ERR_NAME_NOT_RESOLVED",
"https://missing-frame.example/",
false,
);
yield* Effect.yieldNow;
expect(statuses.at(-1)?.kind).toBe("Success");
loading = true;
listeners.get("did-start-loading")?.();
yield* Effect.yieldNow;
expect(statuses.at(-1)?.kind).toBe("Loading");
loading = false;
listeners.get("did-fail-load")?.({}, -102, "ERR_CONNECTION_REFUSED", url, true);
listeners.get("did-stop-loading")?.();
listeners.get("page-title-updated")?.();
yield* Effect.yieldNow;
expect(statuses.at(-1)).toEqual({
kind: "LoadFailed",
url,
title: "localhost:5733",
code: -102,
description: "ERR_CONNECTION_REFUSED",
});
loading = true;
listeners.get("did-start-loading")?.();
yield* Effect.yieldNow;
expect(statuses.at(-1)?.kind).toBe("Loading");
loading = false;
listeners.get("did-stop-loading")?.();
yield* Effect.yieldNow;
expect(statuses.at(-1)?.kind).toBe("Success");
listeners.get("did-fail-load")?.({}, -102, "ERR_CONNECTION_REFUSED", url, true);
yield* Effect.yieldNow;
expect(statuses.at(-1)?.kind).toBe("LoadFailed");
listeners.get("did-navigate")?.();
yield* Effect.yieldNow;
expect(statuses.at(-1)?.kind).toBe("Success");
}),
),
);
effectIt.effect("captures a PNG screenshot into browser artifacts", () =>
withManager((manager) =>
Effect.gen(function* () {
const png = Buffer.from("preview-png");
const capturePage = vi.fn(async () => ({ toPNG: () => png }));
const listeners = new Map<string, (...args: never[]) => void>();
fromId.mockReturnValue({
id: 42,
isDestroyed: () => false,
getType: () => "webview",
getURL: () => "https://example.com:8443/path?query=value",
getTitle: () => "Example",
isLoading: () => false,
getZoomFactor: () => 1,
setZoomFactor: vi.fn(),
on: vi.fn((event: string, listener: (...args: never[]) => void) => {
listeners.set(event, listener);
}),
off: vi.fn(),
ipc: { on: vi.fn(), off: vi.fn() },
send: webviewSend,
navigationHistory: { canGoBack: () => false, canGoForward: () => false },
setWindowOpenHandler: vi.fn(),
debugger: {
isAttached: () => false,
attach: vi.fn(),
sendCommand: vi.fn(async () => undefined),
on: vi.fn(),
off: vi.fn(),
},
capturePage,
} as never);
yield* manager.createTab("tab_1");
yield* manager.registerWebview("tab_1", 42);
expect(webviewSend).toHaveBeenCalledWith(
"preview:annotation-theme",
expect.objectContaining({
colorScheme: "light",
primary: "oklch(0.488 0.217 264)",
}),
);
const artifact = yield* manager.captureScreenshot("tab_1");
expect(capturePage).toHaveBeenCalledOnce();
expect(mkdir).toHaveBeenCalledWith("/tmp/t3/dev/browser-artifacts");
expect(writeFile).toHaveBeenCalledWith(artifact.path, png);
expect(artifact).toMatchObject({
tabId: "tab_1",
mimeType: "image/png",
sizeBytes: png.byteLength,
});
expect(artifact.path).toMatch(
/\/browser-artifacts\/browser-screenshot-example-com-[^.]+\.png$/,
);
const captureCause = new Error("capture failed");
capturePage.mockRejectedValueOnce(captureCause);
const exit = yield* Effect.exit(manager.captureScreenshot("tab_1"));
expect(Exit.isFailure(exit)).toBe(true);
if (Exit.isSuccess(exit)) return;
const error = Option.getOrThrow(Cause.findErrorOption(exit.cause));
expect(error).toMatchObject({
_tag: "PreviewOperationError",
operation: "captureScreenshot.capturePage",
tabId: "tab_1",
webContentsId: 42,
cause: captureCause,
});
}),
),
);
effectIt.effect("captures hidden preview recordings independently for concurrent tabs", () =>
withManager((manager) =>
Effect.gen(function* () {
const firstJpeg = Buffer.from("first-recording-frame");
const secondJpeg = Buffer.from("second-recording-frame");
const firstCapturePage = vi.fn(async () => ({
toJPEG: () => firstJpeg,
getSize: () => ({ width: 800, height: 600 }),
}));
const secondCapturePage = vi.fn(async () => ({
toJPEG: () => secondJpeg,
getSize: () => ({ width: 390, height: 844 }),
}));
const firstSendCommand = vi.fn(async () => undefined);
const secondSendCommand = vi.fn(async () => undefined);
const makeWebContents = (
id: number,
capturePage: typeof firstCapturePage,
sendCommand: typeof firstSendCommand,
) =>
({
id,
isDestroyed: () => false,
getType: () => "webview",
getURL: () => `https://example.com/${id}`,
getTitle: () => `Example ${id}`,
isLoading: () => false,
getZoomFactor: () => 1,
setZoomFactor: vi.fn(),
on: vi.fn(),
off: vi.fn(),
ipc: { on: vi.fn(), off: vi.fn() },
send: webviewSend,
navigationHistory: { canGoBack: () => false, canGoForward: () => false },
setWindowOpenHandler: vi.fn(),
debugger: {
isAttached: () => false,
attach: vi.fn(),
sendCommand,
on: vi.fn(),
off: vi.fn(),
},
capturePage,
}) as never;
const webContentsById = new Map([
[41, makeWebContents(41, firstCapturePage, firstSendCommand)],
[42, makeWebContents(42, secondCapturePage, secondSendCommand)],
]);
fromId.mockImplementation((id) =>
id === undefined ? null : (webContentsById.get(id) ?? null),
);
const frames: DesktopPreviewRecordingFrame[] = [];
yield* manager.subscribeRecordingFrames((frame) =>
Effect.sync(() => {
frames.push(frame);
}),
);
yield* manager.createTab("tab_1");
yield* manager.createTab("tab_2");
yield* manager.registerWebview("tab_1", 41);
yield* manager.registerWebview("tab_2", 42);
yield* Effect.all([manager.startRecording("tab_1"), manager.startRecording("tab_2")], {
concurrency: 2,
discard: true,
});
expect(firstCapturePage).toHaveBeenCalledOnce();
expect(secondCapturePage).toHaveBeenCalledOnce();
expect(frames).toHaveLength(2);
expect(frames).toEqual(
expect.arrayContaining([
expect.objectContaining({
tabId: "tab_1",
data: firstJpeg.toString("base64"),
width: 800,
height: 600,
}),
expect.objectContaining({
tabId: "tab_2",
data: secondJpeg.toString("base64"),
width: 390,
height: 844,
}),
]),
);
expect(firstSendCommand).not.toHaveBeenCalledWith(
"Page.startScreencast",
expect.anything(),
);
expect(secondSendCommand).not.toHaveBeenCalledWith(
"Page.startScreencast",
expect.anything(),
);
yield* Effect.all([manager.stopRecording("tab_1"), manager.stopRecording("tab_2")], {
concurrency: 2,
discard: true,
});
}),
),
);
effectIt.effect("drops a captured frame when the tab webview changes during capture", () =>
withManager((manager) =>
Effect.gen(function* () {
const staleImage: TestCapturedPreviewImage = {
toJPEG: vi.fn(() => Buffer.from("stale-recording-frame")),
getSize: vi.fn(() => ({ width: 1280, height: 720 })),
};
let markCaptureStarted!: () => void;
const captureStarted = new Promise<void>((resolve) => {
markCaptureStarted = resolve;
});
let resolveCapture: ((image: TestCapturedPreviewImage) => void) | undefined;
const staleCapturePage = vi.fn(() => {
markCaptureStarted();
return new Promise<TestCapturedPreviewImage>((resolve) => {
resolveCapture = resolve;
});
});
const replacementCapturePage = vi.fn(async () => ({
toJPEG: () => Buffer.from("replacement-recording-frame"),
getSize: () => ({ width: 1280, height: 720 }),
}));
const initialWebContents = makeTestPreviewWebContents(staleCapturePage, 42);
const replacementWebContents = makeTestPreviewWebContents(replacementCapturePage, 43);
fromId.mockImplementation((webContentsId?: number) => {
if (webContentsId === 42) return initialWebContents;
if (webContentsId === 43) return replacementWebContents;
return null;
});
const frames: DesktopPreviewRecordingFrame[] = [];
yield* manager.subscribeRecordingFrames((frame) =>
Effect.sync(() => {
frames.push(frame);
}),
);
yield* manager.createTab("tab_capture_replaced");
yield* manager.registerWebview("tab_capture_replaced", 42);
const recordingFiber = yield* manager
.startRecording("tab_capture_replaced")
.pipe(Effect.forkChild({ startImmediately: true }));
yield* Effect.promise(() => captureStarted);
yield* manager.registerWebview("tab_capture_replaced", 43);
resolveCapture?.(staleImage);
yield* Fiber.join(recordingFiber);
expect(staleImage.getSize).not.toHaveBeenCalled();
expect(staleImage.toJPEG).not.toHaveBeenCalled();
expect(frames).toHaveLength(0);
expect(replacementCapturePage).not.toHaveBeenCalled();
yield* manager.stopRecording("tab_capture_replaced");
}),
),
);
effectIt.effect("keeps an in-flight frame when a capture consumer is added", () =>
withManager((manager) =>
Effect.gen(function* () {
const image: TestCapturedPreviewImage = {
toJPEG: vi.fn(() => Buffer.from("shared-in-flight-frame")),
getSize: vi.fn(() => ({ width: 1280, height: 720 })),
};
let markCaptureStarted!: () => void;
const captureStarted = new Promise<void>((resolve) => {
markCaptureStarted = resolve;
});
let resolveCapture: ((captured: TestCapturedPreviewImage) => void) | undefined;
const capturePage = vi.fn(() => {
markCaptureStarted();
return new Promise<TestCapturedPreviewImage>((resolve) => {
resolveCapture = resolve;
});
});
fromId.mockReturnValue(makeTestPreviewWebContents(capturePage));
const { pictureInPictureWindow, send } = makeTestPictureInPictureWindow();
browserWindowConstructor.mockImplementation(function () {
return pictureInPictureWindow;
});
const recordingFrames: DesktopPreviewRecordingFrame[] = [];
yield* manager.subscribeRecordingFrames((frame) =>
Effect.sync(() => {
recordingFrames.push(frame);
}),
);
yield* manager.createTab("tab_capture_consumer_added");
yield* manager.registerWebview("tab_capture_consumer_added", 42);
const recordingFiber = yield* manager
.startRecording("tab_capture_consumer_added")
.pipe(Effect.forkChild({ startImmediately: true }));
yield* Effect.promise(() => captureStarted);
yield* manager.openPictureInPicture("tab_capture_consumer_added");
resolveCapture?.(image);
yield* Fiber.join(recordingFiber);
expect(recordingFrames).toHaveLength(1);
expect(send).toHaveBeenCalledWith(
"desktop:preview-pip-frame",
expect.objectContaining({
tabId: "tab_capture_consumer_added",
data: Buffer.from("shared-in-flight-frame").toString("base64"),
}),
);
yield* manager.stopRecording("tab_capture_consumer_added");
yield* manager.closePictureInPicture("tab_capture_consumer_added");
}),
),
);
effectIt.effect("emits debugger screencast frames only while recording is active", () =>
withManager((manager) =>
Effect.gen(function* () {
let debuggerMessage:
| ((event: unknown, method: string, params: Record<string, unknown>) => void)
| undefined;
const capturePage = vi.fn(async () => ({
toJPEG: () => Buffer.from("scheduled-recording-frame"),
getSize: () => ({ width: 1280, height: 720 }),
}));
const sendCommand = vi.fn(async (method: string) =>
method === "Runtime.evaluate" ? { result: { value: null } } : undefined,
);
fromId.mockReturnValue({
id: 42,
isDestroyed: () => false,
getType: () => "webview",
getURL: () => "https://example.com",
getTitle: () => "Example",
isLoading: () => false,
isDevToolsOpened: () => false,
getZoomFactor: () => 1,
setZoomFactor: vi.fn(),
on: vi.fn(),
off: vi.fn(),
ipc: { on: vi.fn(), off: vi.fn() },
send: webviewSend,
navigationHistory: { canGoBack: () => false, canGoForward: () => false },
setWindowOpenHandler: vi.fn(),
debugger: {
isAttached: () => false,
attach: vi.fn(),
sendCommand,
on: vi.fn(
(
event: string,
listener: (event: unknown, method: string, params: Record<string, unknown>) => void,
) => {
if (event === "message") debuggerMessage = listener;
},
),
off: vi.fn(),
},
capturePage,
} as never);
const recordingFrames: DesktopPreviewRecordingFrame[] = [];
yield* manager.subscribeRecordingFrames((frame) =>
Effect.sync(() => {
recordingFrames.push(frame);
}),
);
yield* manager.createTab("tab_screencast_guard");
yield* manager.registerWebview("tab_screencast_guard", 42);
yield* manager.automationEvaluate("tab_screencast_guard", { expression: "null" });
debuggerMessage?.({}, "Page.screencastFrame", {
sessionId: 1,
data: "inactive-frame",
metadata: { deviceWidth: 1280, deviceHeight: 720 },
});
yield* Effect.yieldNow;
expect(recordingFrames).toHaveLength(0);
yield* manager.startRecording("tab_screencast_guard");
recordingFrames.length = 0;
debuggerMessage?.({}, "Page.screencastFrame", {
sessionId: 2,
data: "active-frame",
metadata: { deviceWidth: 1280, deviceHeight: 720 },
});
yield* Effect.yieldNow;
expect(recordingFrames).toEqual([
expect.objectContaining({
tabId: "tab_screencast_guard",
data: "active-frame",
width: 1280,
height: 720,
}),
]);
yield* manager.stopRecording("tab_screencast_guard");
}),
),
);
effectIt.effect("shares background frame capture between recording and picture-in-picture", () =>
withManager((manager) =>
Effect.gen(function* () {
const jpeg = Buffer.from("shared-preview-frame");
const capturePage = vi.fn(async () => ({
toJPEG: () => jpeg,
getSize: () => ({ width: 1280, height: 720 }),
}));
fromId.mockReturnValue({
id: 42,
isDestroyed: () => false,
getType: () => "webview",
getURL: () => "https://example.com",
getTitle: () => "Example",
isLoading: () => false,
getZoomFactor: () => 1,
setZoomFactor: vi.fn(),
on: vi.fn(),
off: vi.fn(),
ipc: { on: vi.fn(), off: vi.fn() },
send: webviewSend,
navigationHistory: { canGoBack: () => false, canGoForward: () => false },
setWindowOpenHandler: vi.fn(),
debugger: {
isAttached: () => false,
attach: vi.fn(),
sendCommand: vi.fn(async () => undefined),
on: vi.fn(),
off: vi.fn(),
},
capturePage,
} as never);
const pictureInPictureListeners = new Map<string, () => void>();
const pictureInPictureSend = vi.fn();
const pictureInPictureWindow = {
isDestroyed: vi.fn(() => false),
once: vi.fn((event: string, listener: () => void) => {
pictureInPictureListeners.set(event, listener);
}),
setAlwaysOnTop: vi.fn(),
setVisibleOnAllWorkspaces: vi.fn(),
setAspectRatio: vi.fn(),
getContentSize: vi.fn(() => [480, 320] as [number, number]),
setContentSize: vi.fn(),
loadURL: vi.fn(async () => undefined),
showInactive: vi.fn(),
close: vi.fn(() => {
pictureInPictureListeners.get("closed")?.();
}),
webContents: {
send: pictureInPictureSend,
},
};
browserWindowConstructor.mockImplementation(function () {
return pictureInPictureWindow;
});
const states: PreviewManager.PreviewTabState[] = [];
const recordingFrames: DesktopPreviewRecordingFrame[] = [];
yield* manager.subscribeStateChanges((_tabId, state) =>
Effect.sync(() => {
states.push(state);
}),
);
yield* manager.subscribeRecordingFrames((frame) =>
Effect.sync(() => {
recordingFrames.push(frame);
}),
);
yield* manager.createTab("tab_pip");
yield* manager.registerWebview("tab_pip", 42);
yield* manager.openPictureInPicture("tab_pip");
expect(browserWindowConstructor).toHaveBeenCalledWith(
expect.objectContaining({
alwaysOnTop: true,
show: false,
skipTaskbar: true,
webPreferences: expect.objectContaining({
preload: "/tmp/t3/desktop/preview-pip-preload.cjs",
backgroundThrottling: false,
}),
}),
);
expect(pictureInPictureWindow.showInactive).toHaveBeenCalledOnce();
expect(pictureInPictureWindow.setVisibleOnAllWorkspaces).toHaveBeenCalledWith(true, {
visibleOnFullScreen: true,
skipTransformProcessType: true,
});
expect(pictureInPictureWindow.setAspectRatio.mock.calls).toEqual([[0], [1280 / 720]]);
expect(pictureInPictureWindow.setContentSize).toHaveBeenCalledWith(523, 294, false);
expect(pictureInPictureWindow.setAspectRatio.mock.invocationCallOrder[0]).toBeLessThan(
pictureInPictureWindow.setContentSize.mock.invocationCallOrder[0] ?? 0,
);
expect(pictureInPictureWindow.setContentSize.mock.invocationCallOrder[0]).toBeLessThan(
pictureInPictureWindow.setAspectRatio.mock.invocationCallOrder[1] ?? 0,
);
expect(pictureInPictureSend).toHaveBeenCalledWith(
"desktop:preview-pip-frame",
expect.objectContaining({
tabId: "tab_pip",
data: jpeg.toString("base64"),
width: 1280,
height: 720,
}),
);
expect(states.at(-1)?.pictureInPicture).toBe(true);
expect(capturePage).toHaveBeenCalledOnce();
yield* manager.startRecording("tab_pip");
expect(capturePage).toHaveBeenCalledOnce();
expect(recordingFrames).toHaveLength(0);
yield* TestClock.adjust(100);
expect(capturePage).toHaveBeenCalledTimes(2);
expect(recordingFrames).toHaveLength(1);
yield* manager.stopRecording("tab_pip");
const framesBeforePictureInPictureOnlyTick = pictureInPictureSend.mock.calls.length;
yield* TestClock.adjust(100);
expect(capturePage).toHaveBeenCalledTimes(3);
expect(pictureInPictureSend.mock.calls.length).toBeGreaterThan(
framesBeforePictureInPictureOnlyTick,
);
expect(recordingFrames).toHaveLength(1);
yield* manager.closePictureInPicture("tab_pip");
expect(pictureInPictureWindow.close).toHaveBeenCalledOnce();
expect(states.at(-1)?.pictureInPicture).toBe(false);
const capturesAfterClose = capturePage.mock.calls.length;
yield* TestClock.adjust(200);
expect(capturePage).toHaveBeenCalledTimes(capturesAfterClose);
}),
),
);
effectIt.effect("retries a cold hidden-tab capture without dropping recording", () =>
withManager((manager) =>
Effect.gen(function* () {
const jpeg = Buffer.from("recovered-preview-frame");
const capturePage = vi.fn(async () => ({
toJPEG: () => jpeg,
getSize: () => ({ width: 1280, height: 720 }),
}));
capturePage.mockRejectedValueOnce(new Error("UnknownVizError"));
fromId.mockReturnValue(makeTestPreviewWebContents(capturePage));
const frames: DesktopPreviewRecordingFrame[] = [];
yield* manager.subscribeRecordingFrames((frame) =>
Effect.sync(() => {
frames.push(frame);
}),
);
yield* manager.createTab("tab_cold_capture");
yield* manager.registerWebview("tab_cold_capture", 42);
yield* manager.startRecording("tab_cold_capture");
expect(capturePage).toHaveBeenCalledOnce();
expect(frames).toHaveLength(0);
yield* TestClock.adjust(100);
expect(capturePage).toHaveBeenCalledTimes(2);
expect(frames).toEqual([
expect.objectContaining({
tabId: "tab_cold_capture",
data: jpeg.toString("base64"),
width: 1280,
height: 720,
}),
]);
yield* manager.stopRecording("tab_cold_capture");
}),
),
);
effectIt.effect("drops empty frames before picture-in-picture delivery", () =>
withManager((manager) =>
Effect.gen(function* () {
const validImage: TestCapturedPreviewImage = {
toJPEG: () => Buffer.from("valid-preview-frame"),
getSize: () => ({ width: 1280, height: 720 }),
};
const capturePage = vi.fn(async () => validImage);
capturePage.mockResolvedValueOnce({
toJPEG: () => Buffer.from("empty-preview-frame"),
getSize: () => ({ width: 0, height: 0 }),
});
fromId.mockReturnValue(makeTestPreviewWebContents(capturePage));
const { pictureInPictureWindow, send } = makeTestPictureInPictureWindow();
browserWindowConstructor.mockImplementation(function () {
return pictureInPictureWindow;
});
yield* manager.createTab("tab_empty_frame");
yield* manager.registerWebview("tab_empty_frame", 42);
yield* manager.openPictureInPicture("tab_empty_frame");
expect(capturePage).toHaveBeenCalledOnce();
expect(pictureInPictureWindow.setAspectRatio).not.toHaveBeenCalled();
expect(send).not.toHaveBeenCalled();
yield* TestClock.adjust(100);
expect(pictureInPictureWindow.setAspectRatio.mock.calls).toEqual([[0], [1280 / 720]]);
expect(send).toHaveBeenCalledOnce();
yield* manager.closePictureInPicture("tab_empty_frame");
}),
),
);
effectIt.effect("does not publish picture-in-picture readiness after window teardown", () =>
withManager((manager) =>
Effect.gen(function* () {
const capturePage = vi.fn(async () => ({
toJPEG: () => Buffer.from("closing-preview-frame"),
getSize: () => ({ width: 1280, height: 720 }),
}));
fromId.mockReturnValue(makeTestPreviewWebContents(capturePage));
const { pictureInPictureWindow } = makeTestPictureInPictureWindow();
pictureInPictureWindow.showInactive.mockImplementationOnce(() => {
pictureInPictureWindow.close();
});
browserWindowConstructor.mockImplementation(function () {
return pictureInPictureWindow;
});
const states: PreviewManager.PreviewTabState[] = [];
yield* manager.subscribeStateChanges((_tabId, state) =>
Effect.sync(() => {
states.push(state);
}),
);
yield* manager.createTab("tab_pip_teardown");
yield* manager.registerWebview("tab_pip_teardown", 42);
const openExit = yield* Effect.exit(manager.openPictureInPicture("tab_pip_teardown"));
expect(Exit.hasInterrupts(openExit)).toBe(true);
expect(pictureInPictureWindow.close).toHaveBeenCalledOnce();
expect(states.some((state) => state.pictureInPicture)).toBe(false);
expect(states.at(-1)?.pictureInPicture).toBe(false);
}),
),
);
effectIt.effect("closes an initializing picture-in-picture without blocking later opens", () =>
withManager((manager) =>
Effect.gen(function* () {
const capturePage = vi.fn(async () => ({
toJPEG: () => Buffer.from("serialized-preview-frame"),
getSize: () => ({ width: 1280, height: 720 }),
}));
fromId.mockReturnValue(makeTestPreviewWebContents(capturePage));
const { pictureInPictureWindow: initializingWindow } = makeTestPictureInPictureWindow(
() =>
new Promise<void>(() => {
// Simulate a renderer load that never settles.
}),
);
const { pictureInPictureWindow: reopenedWindow } = makeTestPictureInPictureWindow();
browserWindowConstructor
.mockImplementationOnce(function () {
return initializingWindow;
})
.mockImplementationOnce(function () {
return reopenedWindow;
});
const states: PreviewManager.PreviewTabState[] = [];
yield* manager.subscribeStateChanges((_tabId, state) =>
Effect.sync(() => {
states.push(state);
}),
);
yield* manager.createTab("tab_concurrent_pip");
yield* manager.registerWebview("tab_concurrent_pip", 42);
const firstOpen = yield* manager
.openPictureInPicture("tab_concurrent_pip")
.pipe(Effect.forkChild);
yield* Effect.yieldNow;
const secondOpen = yield* manager
.openPictureInPicture("tab_concurrent_pip")
.pipe(Effect.forkChild);
yield* Effect.yieldNow;
const close = yield* manager
.closePictureInPicture("tab_concurrent_pip")
.pipe(Effect.forkChild);
yield* Effect.yieldNow;
expect(browserWindowConstructor).toHaveBeenCalledOnce();
expect(initializingWindow.loadURL).toHaveBeenCalledOnce();
expect(initializingWindow.close).toHaveBeenCalledOnce();
const [firstOpenExit, secondOpenExit] = yield* Effect.all([
Fiber.await(firstOpen),
Fiber.await(secondOpen),
]);
yield* Fiber.join(close);
expect(Exit.hasInterrupts(firstOpenExit)).toBe(true);
expect(Exit.hasInterrupts(secondOpenExit)).toBe(true);
expect(initializingWindow.showInactive).not.toHaveBeenCalled();
expect(capturePage).not.toHaveBeenCalled();
expect(states.at(-1)?.pictureInPicture).toBe(false);
yield* manager.openPictureInPicture("tab_concurrent_pip");
expect(browserWindowConstructor).toHaveBeenCalledTimes(2);
expect(reopenedWindow.showInactive).toHaveBeenCalledOnce();
expect(capturePage).toHaveBeenCalledOnce();
expect(states.at(-1)?.pictureInPicture).toBe(true);
yield* manager.closePictureInPicture("tab_concurrent_pip");
expect(browserWindowConstructor).toHaveBeenCalledTimes(2);
expect(reopenedWindow.close).toHaveBeenCalledOnce();
expect(states.at(-1)?.pictureInPicture).toBe(false);
const capturesAfterClose = capturePage.mock.calls.length;
yield* TestClock.adjust(200);
expect(capturePage).toHaveBeenCalledTimes(capturesAfterClose);
}),
),
);
effectIt.effect("rejects picture-in-picture when its webview changes during initialization", () =>
withManager((manager) =>
Effect.gen(function* () {
const initialCapturePage = vi.fn(async () => ({
toJPEG: () => Buffer.from("stale-preview-frame"),
getSize: () => ({ width: 1280, height: 720 }),
}));
const replacementCapturePage = vi.fn(async () => ({
toJPEG: () => Buffer.from("replacement-preview-frame"),
getSize: () => ({ width: 1280, height: 720 }),
}));
const initialWebContents = makeTestPreviewWebContents(initialCapturePage, 42);
const replacementWebContents = makeTestPreviewWebContents(replacementCapturePage, 43);
fromId.mockImplementation((webContentsId?: number) => {
if (webContentsId === 42) return initialWebContents;
if (webContentsId === 43) return replacementWebContents;
return null;
});
let resolveLoad: (() => void) | undefined;
const { pictureInPictureWindow } = makeTestPictureInPictureWindow(
() =>
new Promise<void>((resolve) => {
resolveLoad = resolve;
}),
);
browserWindowConstructor.mockImplementation(function () {
return pictureInPictureWindow;
});
yield* manager.createTab("tab_replaced_webview");
yield* manager.registerWebview("tab_replaced_webview", 42);
const open = yield* manager
.openPictureInPicture("tab_replaced_webview")
.pipe(Effect.forkChild({ startImmediately: true }));
yield* Effect.yieldNow;
expect(pictureInPictureWindow.loadURL).toHaveBeenCalledOnce();
expect(resolveLoad).toBeDefined();
const concurrentOpen = yield* manager
.openPictureInPicture("tab_replaced_webview")
.pipe(Effect.forkChild({ startImmediately: true }));
yield* Effect.yieldNow;
yield* manager.registerWebview("tab_replaced_webview", 43);
resolveLoad?.();
const openExits = yield* Effect.all([Fiber.await(open), Fiber.await(concurrentOpen)]);
for (const openExit of openExits) {
expect(Exit.isFailure(openExit)).toBe(true);
if (Exit.isSuccess(openExit)) continue;
const error = Option.getOrThrow(Cause.findErrorOption(openExit.cause));
expect(error).toMatchObject({
_tag: "PreviewOperationError",
operation: "pictureInPicture.validateWebContents",
tabId: "tab_replaced_webview",
webContentsId: 42,
});
}
expect(browserWindowConstructor).toHaveBeenCalledOnce();
expect(pictureInPictureWindow.close).toHaveBeenCalledOnce();
expect(pictureInPictureWindow.showInactive).not.toHaveBeenCalled();
expect(initialCapturePage).not.toHaveBeenCalled();
expect(replacementCapturePage).not.toHaveBeenCalled();
}),
),
);
effectIt.effect("keeps element picking active during subframe navigation", () =>
withManager((manager) =>
Effect.gen(function* () {
const listeners = new Map<string, (...args: unknown[]) => void>();
fromId.mockReturnValue({
id: 42,
isDestroyed: () => false,
getType: () => "webview",
getURL: () => "https://example.com",
getTitle: () => "Example",
isLoading: () => false,
isFocused: () => true,
getZoomFactor: () => 1,
setZoomFactor: vi.fn(),
on: vi.fn((event: string, listener: (...args: unknown[]) => void) => {
listeners.set(event, listener);
}),
once: vi.fn((event: string, listener: (...args: unknown[]) => void) => {
listeners.set(event, listener);
}),
off: vi.fn(),
ipc: { on: vi.fn(), off: vi.fn(), removeListener: vi.fn() },
send: webviewSend,
navigationHistory: { canGoBack: () => false, canGoForward: () => false },
setWindowOpenHandler: vi.fn(),
debugger: {
isAttached: () => false,
attach: vi.fn(),
sendCommand: vi.fn(async () => undefined),
on: vi.fn(),
off: vi.fn(),
},
} as never);
yield* manager.createTab("tab_1");
yield* manager.registerWebview("tab_1", 42);
const pick = yield* manager.pickElement("tab_1").pipe(Effect.forkChild);
yield* Effect.yieldNow;
listeners.get("did-start-navigation")?.({}, "about:blank", false, false);
yield* Effect.yieldNow;
expect(pick.pollUnsafe()).toBeUndefined();
listeners.get("did-start-navigation")?.({}, "https://example.com/next", false, true);
expect(yield* Fiber.join(pick)).toBeNull();
}),
),
);
effectIt.effect("reveals only files inside the configured browser artifact directory", () =>
withManager((manager) =>
Effect.gen(function* () {
yield* manager.revealArtifact("/tmp/t3/dev/browser-artifacts/browser-screenshot-test.png");
expect(showItemInFolder).toHaveBeenCalledWith(
"/tmp/t3/dev/browser-artifacts/browser-screenshot-test.png",
);
const exit = yield* Effect.exit(manager.revealArtifact("/tmp/t3/dev/settings.json"));
expect(Exit.isFailure(exit)).toBe(true);
if (Exit.isSuccess(exit)) return;
const error = Option.getOrThrow(Cause.findErrorOption(exit.cause));
expect(error).toMatchObject({
_tag: "PreviewArtifactPathOutsideDirectoryError",
artifactPath: "/tmp/t3/dev/settings.json",
artifactDirectory: "/tmp/t3/dev/browser-artifacts",
});
expect("cause" in error).toBe(false);
}),
),
);
effectIt.effect("copies screenshot artifacts to the system clipboard", () =>
withManager((manager) =>
Effect.gen(function* () {
const artifactPath = "/tmp/t3/dev/browser-artifacts/browser-screenshot-test.png";
yield* manager.copyArtifactToClipboard(artifactPath);
expect(createFromPath).toHaveBeenCalledWith(artifactPath);
expect(writeImage).toHaveBeenCalledOnce();
const exit = yield* Effect.exit(
manager.copyArtifactToClipboard("/tmp/t3/dev/settings.json"),
);
expect(Exit.isFailure(exit)).toBe(true);
if (Exit.isSuccess(exit)) return;
const error = Option.getOrThrow(Cause.findErrorOption(exit.cause));
expect(error).toMatchObject({
_tag: "PreviewArtifactPathOutsideDirectoryError",
artifactPath: "/tmp/t3/dev/settings.json",
artifactDirectory: "/tmp/t3/dev/browser-artifacts",
});
expect("cause" in error).toBe(false);
createFromPath.mockReturnValueOnce({ isEmpty: () => true });
const invalidImageExit = yield* Effect.exit(manager.copyArtifactToClipboard(artifactPath));
expect(Exit.isFailure(invalidImageExit)).toBe(true);
if (Exit.isSuccess(invalidImageExit)) return;
expect(Option.getOrThrow(Cause.findErrorOption(invalidImageExit.cause))).toMatchObject({
_tag: "PreviewArtifactImageLoadError",
artifactPath,
});
}),
),
);
effectIt.effect("emits the resolved pointer target before dispatching an automation click", () =>
withManager((manager) =>
Effect.gen(function* () {
let humanInput: ((_event: unknown, signal: unknown) => void) | undefined;
const activity: string[] = [];
const sendCommand = vi.fn(async (method: string, params?: Record<string, unknown>) => {
if (method === "Runtime.evaluate") {
return {
result: {
value: { width: 800, height: 600 },
},
};
}
if (method === "Input.dispatchMouseEvent" && params?.type === "mousePressed") {
activity.push("mousePressed");
humanInput?.({}, { kind: "pointer", x: params.x, y: params.y, button: 0 });
}
return undefined;
});
fromId.mockReturnValue({
id: 42,
isDestroyed: () => false,
getType: () => "webview",
getURL: () => "https://example.com",
getTitle: () => "Example",
isLoading: () => false,
isDevToolsOpened: () => false,
getZoomFactor: () => 1,
setZoomFactor: vi.fn(),
on: vi.fn(),
off: vi.fn(),
ipc: {
on: vi.fn((channel: string, listener: typeof humanInput) => {
if (channel === "preview:human-input") humanInput = listener;
}),
off: vi.fn(),
},
send: webviewSend,
navigationHistory: { canGoBack: () => false, canGoForward: () => false },
setWindowOpenHandler: vi.fn(),
debugger: {
isAttached: () => false,
attach: vi.fn(),
sendCommand,
on: vi.fn(),
off: vi.fn(),
},
} as never);
yield* manager.subscribePointerEvents((event) =>
Effect.sync(() => {
activity.push(event.phase);
}),
);
yield* manager.createTab("tab_1");
yield* manager.registerWebview("tab_1", 42);
const click = yield* manager
.automationClick("tab_1", { x: 120, y: 80 })
.pipe(Effect.forkChild({ startImmediately: true }));
yield* TestClock.adjust(200);
yield* Fiber.join(click);
expect(activity).toEqual(["move", "click", "mousePressed"]);
expect(sendCommand).toHaveBeenCalledWith("Input.dispatchMouseEvent", {
type: "mousePressed",
x: 120,
y: 80,
button: "left",
clickCount: 1,
});
expect(sendCommand).toHaveBeenCalledWith("Input.dispatchMouseEvent", {
type: "mouseReleased",
x: 120,
y: 80,
button: "left",
clickCount: 1,
});
}),
),
);
effectIt.effect("types in background webviews and enables native key input", () =>
withManager((manager) =>
Effect.gen(function* () {
let failKeyDown = false;
let humanInput: ((_event: unknown, signal: unknown) => void) | undefined;
const sendCommand = vi.fn(async (method: string, params?: Record<string, unknown>) => {
if (
failKeyDown &&
method === "Input.dispatchKeyEvent" &&
(params?.["type"] === "keyDown" || params?.["type"] === "rawKeyDown")
) {
throw new Error("key dispatch failed");
}
if (
method === "Input.dispatchKeyEvent" &&
(params?.["type"] === "keyDown" || params?.["type"] === "rawKeyDown")
) {
humanInput?.(
{},
{
kind: "key",
key: params["key"],
code: params["code"] ?? "Digit1",
},
);
}
return method === "Runtime.evaluate" ? { result: { value: { ok: true } } } : undefined;
});
const restoreFocus = vi.fn();
const focus = vi.fn();
getFocusedWebContents.mockReturnValue({
id: 7,
isDestroyed: () => false,
focus: restoreFocus,
} as never);
fromId.mockReturnValue({
id: 42,
isDestroyed: () => false,
getType: () => "webview",
getURL: () => "https://example.com",
getTitle: () => "Example",
isLoading: () => false,
isDevToolsOpened: () => false,
focus,
getZoomFactor: () => 1,
setZoomFactor: vi.fn(),
on: vi.fn(),
off: vi.fn(),
ipc: {
on: vi.fn((channel: string, listener: typeof humanInput) => {
if (channel === "preview:human-input") humanInput = listener;
}),
off: vi.fn(),
},
send: webviewSend,
navigationHistory: { canGoBack: () => false, canGoForward: () => false },
setWindowOpenHandler: vi.fn(),
debugger: {
isAttached: () => false,
attach: vi.fn(),
sendCommand,
on: vi.fn(),
off: vi.fn(),
},
} as never);
yield* manager.createTab("tab_input");
yield* manager.registerWebview("tab_input", 42);
yield* manager.automationType("tab_input", { text: "hello", clear: true });
yield* manager.automationType("tab_input", { text: "", clear: true });
yield* manager.automationPress("tab_input", { key: "x" });
const calls = sendCommand.mock.calls;
const methods = calls.map(([method]) => method);
const enableIndex = methods.indexOf("Input.setIgnoreInputEvents");
const focusOnIndex = calls.findIndex(
([method, params]) =>
method === "Emulation.setFocusEmulationEnabled" && params?.["enabled"] === true,
);
const keyDownIndex = calls.findIndex(
([method, params]) =>
method === "Input.dispatchKeyEvent" && params?.["type"] === "keyDown",
);
const keyUpIndex = calls.findIndex(
([method, params]) => method === "Input.dispatchKeyEvent" && params?.["type"] === "keyUp",
);
const focusOffIndex = calls.findIndex(
([method, params]) =>
method === "Emulation.setFocusEmulationEnabled" && params?.["enabled"] === false,
);
const typeEvaluation = sendCommand.mock.calls.find(
([method, params]) =>
method === "Runtime.evaluate" &&
typeof params === "object" &&
params !== null &&
"expression" in params &&
typeof params.expression === "string" &&
params.expression.includes('document.execCommand("insertText"'),
);
expect(typeEvaluation).toBeDefined();
const clearOnlyEvaluation = sendCommand.mock.calls.find(
([method, params]) =>
method === "Runtime.evaluate" &&
typeof params === "object" &&
params !== null &&
"expression" in params &&
typeof params.expression === "string" &&
params.expression.includes('const text = ""') &&
params.expression.includes("Object.getOwnPropertyDescriptor"),
);
expect(clearOnlyEvaluation).toBeDefined();
expect(methods).not.toContain("Input.insertText");
expect(enableIndex).toBeGreaterThanOrEqual(0);
expect(focus).toHaveBeenCalledOnce();
expect(restoreFocus).toHaveBeenCalledOnce();
expect(methods).toContain("Page.bringToFront");
expect(enableIndex).toBeLessThan(focusOnIndex);
expect(focusOnIndex).toBeLessThan(keyDownIndex);
expect(keyDownIndex).toBeLessThan(keyUpIndex);
expect(keyUpIndex).toBeLessThan(focusOffIndex);
expect(
calls.filter(
([method, params]) =>
method === "Input.dispatchKeyEvent" && params?.["type"] === "keyUp",
),
).toHaveLength(1);
expect(sendCommand).toHaveBeenCalledWith("Input.setIgnoreInputEvents", { ignore: false });
sendCommand.mockClear();
failKeyDown = true;
const failedPress = yield* Effect.exit(manager.automationPress("tab_input", { key: "y" }));
expect(Exit.isFailure(failedPress)).toBe(true);
expect(sendCommand).toHaveBeenCalledWith("Input.dispatchKeyEvent", {
type: "keyUp",
key: "y",
code: "KeyY",
modifiers: 0,
windowsVirtualKeyCode: 89,
location: 0,
isKeypad: false,
});
expect(sendCommand).toHaveBeenCalledWith("Emulation.setFocusEmulationEnabled", {
enabled: false,
});
expect(restoreFocus).toHaveBeenCalledTimes(2);
expect(
sendCommand.mock.calls.filter(
([method, params]) =>
method === "Input.dispatchKeyEvent" && params?.["type"] === "keyUp",
),
).toHaveLength(1);
sendCommand.mockClear();
failKeyDown = false;
yield* manager.automationPress("tab_input", { key: "!" });
expect(sendCommand).toHaveBeenCalledWith("Input.dispatchKeyEvent", {
type: "keyDown",
key: "!",
code: "Digit1",
modifiers: 0,
windowsVirtualKeyCode: 49,
location: 0,
isKeypad: false,
text: "!",
unmodifiedText: "!",
});
expect(restoreFocus).toHaveBeenCalledTimes(3);
}),
),
);
effectIt.effect("still interrupts agent control for a different human pointer event", () =>
withManager((manager) =>
Effect.gen(function* () {
let humanInput: ((_event: unknown, signal: unknown) => void) | undefined;
const sendCommand = vi.fn(async (method: string) => {
if (method === "Runtime.evaluate") {
return {
result: {
value: { width: 800, height: 600 },
},
};
}
if (method === "Input.dispatchMouseEvent") {
humanInput?.({}, { kind: "pointer", x: 400, y: 300, button: 0 });
}
return undefined;
});
fromId.mockReturnValue({
id: 42,
isDestroyed: () => false,
getType: () => "webview",
getURL: () => "https://example.com",
getTitle: () => "Example",
isLoading: () => false,
isDevToolsOpened: () => false,
getZoomFactor: () => 1,
setZoomFactor: vi.fn(),
on: vi.fn(),
off: vi.fn(),
ipc: {
on: vi.fn((channel: string, listener: typeof humanInput) => {
if (channel === "preview:human-input") humanInput = listener;
}),
off: vi.fn(),
},
send: webviewSend,
navigationHistory: { canGoBack: () => false, canGoForward: () => false },
setWindowOpenHandler: vi.fn(),
debugger: {
isAttached: () => false,
attach: vi.fn(),
sendCommand,
on: vi.fn(),
off: vi.fn(),
},
} as never);
yield* manager.createTab("tab_1");
yield* manager.registerWebview("tab_1", 42);
const click = yield* manager
.automationClick("tab_1", { x: 120, y: 80 })
.pipe(Effect.forkChild({ startImmediately: true }));
yield* TestClock.adjust(200);
const exit = yield* Fiber.await(click);
expect(Exit.isFailure(exit)).toBe(true);
if (Exit.isSuccess(exit)) return;
const error = Option.getOrThrow(Cause.findErrorOption(exit.cause));
expect(error).toMatchObject({
_tag: "PreviewAutomationControlInterruptedError",
operation: "click",
tabId: "tab_1",
webContentsId: 42,
});
expect(error).toBeInstanceOf(Error);
if (error instanceof Error) {
expect(error.name).toBe("PreviewAutomationControlInterruptedError");
}
expect("cause" in error).toBe(false);
}),
),
);
effectIt.effect("derives evaluation detail kind and length from the same non-empty source", () =>
withManager((manager) =>
Effect.gen(function* () {
const text = "ReferenceError: fallbackDetail is not defined";
const exceptionDetails = {
text,
exception: { description: "" },
};
const sendCommand = vi.fn(async (method: string) =>
method === "Runtime.evaluate" ? { exceptionDetails } : undefined,
);
fromId.mockReturnValue({
id: 42,
isDestroyed: () => false,
getType: () => "webview",
getURL: () => "https://example.com",
getTitle: () => "Example",
isLoading: () => false,
isDevToolsOpened: () => false,
getZoomFactor: () => 1,
setZoomFactor: vi.fn(),
on: vi.fn(),
off: vi.fn(),
ipc: { on: vi.fn(), off: vi.fn() },
send: webviewSend,
navigationHistory: { canGoBack: () => false, canGoForward: () => false },
setWindowOpenHandler: vi.fn(),
debugger: {
isAttached: () => false,
attach: vi.fn(),
sendCommand,
on: vi.fn(),
off: vi.fn(),
},
} as never);
yield* manager.createTab("tab_1");
yield* manager.registerWebview("tab_1", 42);
const exit = yield* Effect.exit(
manager.automationEvaluate("tab_1", { expression: "fallbackDetail" }),
);
expect(Exit.isFailure(exit)).toBe(true);
if (Exit.isSuccess(exit)) return;
const error = Option.getOrThrow(Cause.findErrorOption(exit.cause));
expect(error).toMatchObject({
_tag: "PreviewAutomationEvaluationError",
detailKind: "exception-text",
detailLength: text.length,
cause: exceptionDetails,
});
}),
),
);
});
describe("PreviewOperationError", () => {
it("keeps timeline detail separate from its structured message", () => {
const cause = new Error("CDP command failed with an invalid node id");
const error = new PreviewManager.PreviewOperationError({
operation: "click.DOM.resolveNode",
tabId: "tab_1",
webContentsId: 42,
cause,
});
expect(error.message).not.toContain(cause.message);
expect(PreviewManager.PreviewOperationError.toTimelineMessage(error)).toBe(cause.message);
});
});
describe("Preview automation diagnostics", () => {
it("keeps browser exception detail out of structural diagnostics", () => {
const secret = "unrelated-browser-payload-secret";
const detail = "ReferenceError: missingValue is not defined";
const cause = {
text: "Uncaught Error",
exception: { description: detail },
unsafePayload: secret,
};
const error = new PreviewManager.PreviewAutomationEvaluationError({
tabId: "tab_1",
detailKind: "exception-description",
detailLength: detail.length,
cause,
});
const encoded = encodePreviewManagerError(error);
const { cause: encodedCause, ...encodedDiagnostics } = encoded as typeof encoded & {
readonly cause?: unknown;
};
expect(error.cause).toBe(cause);
expect(encodedCause).toStrictEqual(cause);
expect(error.message).toBe("Preview JavaScript evaluation failed in tab tab_1");
expect(error.message).not.toContain(secret);
expect(JSON.stringify(encodedDiagnostics)).not.toContain(secret);
expect("detail" in error).toBe(false);
expect(PreviewManager.PreviewAutomationEvaluationError.toTimelineMessage(error)).toBe(detail);
expect(PreviewManager.PreviewAutomationEvaluationError.toTimelineMessage(error)).not.toContain(
secret,
);
});
it("retains bounded selector diagnostics without exposing selector or reason text", () => {
const selector = "role=button[name='selector-secret']";
const reason = "Unexpected token near reason-secret";
const cause = { invalidSelector: true as const, message: reason };
const error = new PreviewManager.PreviewAutomationInvalidSelectorError({
operation: "click",
tabId: "tab_1",
selectorKind: "locator",
selectorLength: selector.length,
reasonLength: reason.length,
cause,
});
const encoded = encodePreviewManagerError(error);
const { cause: encodedCause, ...encodedDiagnostics } = encoded as typeof encoded & {
readonly cause?: unknown;
};
expect(error.cause).toBe(cause);
expect(encodedCause).toStrictEqual(cause);
expect(error).toMatchObject({
selectorKind: "locator",
selectorLength: selector.length,
reasonLength: reason.length,
});
expect(error.detail).toEqual({
selectorKind: "locator",
selectorLength: selector.length,
});
expect(error.message).not.toContain("secret");
expect(JSON.stringify(encodedDiagnostics)).not.toContain("secret");
expect("selector" in error).toBe(false);
expect("reason" in error).toBe(false);
expect(PreviewManager.PreviewAutomationInvalidSelectorError.toTimelineMessage(error)).toBe(
reason,
);
});
it("does not retain a missing target locator", () => {
const selector = "[data-token='target-secret']";
const error = new PreviewManager.PreviewAutomationTargetNotFoundError({
operation: "scroll",
tabId: "tab_1",
selectorKind: "selector",
selectorLength: selector.length,
});
expect(error.message).not.toContain(selector);
expect(JSON.stringify(error)).not.toContain(selector);
expect("locator" in error).toBe(false);
});
});