import * as NodeEvents from "node:events"; import * as NodeServices from "@effect/platform-node/NodeServices"; import { it } from "@effect/vitest"; import { EnvironmentId, PreviewTabId, ProviderInstanceId, ThreadId, type PreviewAutomationSnapshot, type PreviewAutomationStatus, } from "@t3tools/contracts"; import * as Clock from "effect/Clock"; import * as Effect from "effect/Effect"; import * as Fiber from "effect/Fiber"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as PubSub from "effect/PubSub"; import * as Queue from "effect/Queue"; import * as Stream from "effect/Stream"; import type { BrowserContext, Page } from "playwright-core"; import { beforeEach, expect, vi } from "vite-plus/test"; import * as ServerConfig from "../config.ts"; import * as ServerEnvironment from "../environment/ServerEnvironment.ts"; import * as Broker from "../mcp/PreviewAutomationBroker.ts"; import * as DesktopChannel from "./DesktopBrowserChannel.ts"; import * as Manager from "./Manager.ts"; import * as ServerBrowser from "./ServerBrowser.ts"; import * as PreviewBrowser from "./PreviewBrowser.ts"; // Keep the manager, broker, ownership, refs, and viewer paths real; replace Chromium I/O only. vi.mock("./ServerBrowserContexts.ts", () => ({ presentAsChrome: async () => {}, ServerBrowserContexts: class { private readonly onClose: ((context: BrowserContext) => void) | undefined; constructor(options: { onContextClose?: (context: BrowserContext) => void }) { this.onClose = options.onContextClose; } async contextFor() { if (contextFailure) throw contextFailure; await contextGate?.promise; const context = makeContext(this.onClose); contexts.push(context); return context as unknown as BrowserContext; } async scratchPage() { return makeContext().page as unknown as Page; } async connectDesktopPage(endpoint: string) { const context = makeContext(); desktopConnections.push({ endpoint, context }); return { browser: { close: async () => {} }, page: context.page as unknown as Page }; } async close() { for (const context of contexts) await context.close(); } }, })); function makeSession() { return { on: vi.fn(), detach: vi.fn(async () => {}), send: vi.fn(async (method: string, _input?: unknown): Promise> => { if (method === "Page.getNavigationHistory") return { currentIndex: 0, entries: [{}] }; if (method === "Page.getLayoutMetrics") return { cssVisualViewport: { pageX: 0, pageY: 0 } }; if (method === "Page.captureScreenshot") return { data: "ZnJhbWU=" }; return { result: { value: "evaluated" } }; }), }; } function makeContext(onClose?: (context: BrowserContext) => void) { const events = new NodeEvents.EventEmitter(); const sessions: ReturnType[] = []; let url = "about:blank"; let viewport = { width: 1280, height: 800 }; let closed = false; let contextClosed = false; const page = { on: (name: string, callback: (...args: unknown[]) => void) => events.on(name, callback), once: (name: string, callback: (...args: unknown[]) => void) => events.once(name, callback), off: (name: string, callback: (...args: unknown[]) => void) => events.off(name, callback), emit: (name: string, ...args: unknown[]) => events.emit(name, ...args), emitAsync: (name: string, ...args: unknown[]) => Promise.all(events.listeners(name).map((listener) => listener(...args))), setDefaultTimeout: vi.fn(), setDefaultNavigationTimeout: vi.fn(), context: () => context, mainFrame: () => page, url: () => url, title: vi.fn(async () => "test page"), viewportSize: () => viewport, setViewportSize: vi.fn(async (size: typeof viewport) => { viewport = size; }), goto: vi.fn(async (next: string) => { url = next; events.emit("load"); }), goBack: vi.fn(async () => {}), goForward: vi.fn(async () => {}), reload: vi.fn(async () => {}), emulateMedia: vi.fn(async () => {}), waitForLoadState: vi.fn(async () => {}), evaluate: vi.fn(async () => ({ url, title: "test page", loading: false, visibleText: "delete", interactiveElements: [], })), ariaSnapshot: vi.fn(async () => '- button "delete" [ref=e1]'), locator: vi.fn(() => { throw new Error("Unexpected locator action"); }), isClosed: () => closed, close: vi.fn(async () => { if (!closed) { closed = true; events.emit("close"); } }), }; const context = { page, sessions, newPage: async () => page as unknown as Page, grantPermissions: vi.fn(async () => {}), exposeBinding: vi.fn(async (_name: string, binding: ClipboardBinding) => { clipboardBinding = binding; }), addInitScript: vi.fn(async () => {}), newCDPSession: async () => { const session = makeSession(); sessions.push(session); return session; }, close: vi.fn(async () => { if (contextClosed) return; contextClosed = true; await page.close(); onClose?.(context as unknown as BrowserContext); }), }; return context; } const contexts: ReturnType[] = []; let contextGate: PromiseWithResolvers | null = null; type ClipboardBinding = (source: { page: unknown }, text: unknown) => void; let clipboardBinding: ClipboardBinding | null = null; let contextFailure: Error | null = null; /** Server tabs the fake desktop renders, and the endpoints the server connected to. */ let desktopRendersNext = false; /** Pages the fake desktop takes back or returns; the channel's streams emit them. */ const desktopDetaches = new NodeEvents.EventEmitter(); const desktopTabs = new Set(); const desktopRenders = (tabId: string) => { if (desktopRendersNext) { desktopRendersNext = false; desktopTabs.add(tabId); } return desktopTabs.has(tabId); }; const releasedDesktopTabs: Array = []; const desktopConnections: Array<{ endpoint: string; context: ReturnType }> = []; const testThread = { threadId: ThreadId.make("browser-test-thread"), providerSessionId: "agent-a", providerInstanceId: ProviderInstanceId.make("codex"), }; const scope = { environmentId: EnvironmentId.make("browser-test-environment"), thread: testThread, client: undefined, requestNamespace: "browser-test", capabilities: new Set(["preview"] as const), issuedAt: 1, }; /** The same thread, as another of its agent sessions. */ const asSession = (providerSessionId: string) => ({ ...scope, thread: { ...testThread, providerSessionId }, }); const dependencies = Layer.mergeAll( Broker.layer, Manager.layer, Layer.succeed(ServerEnvironment.ServerEnvironment, { getEnvironmentId: Effect.succeed(scope.environmentId), getDescriptor: Effect.die("unused descriptor"), }), Layer.succeed(PreviewBrowser.PreviewBrowser, { executable: Effect.die("mock Chromium does not need an executable"), installed: Effect.die("mock Chromium does not need an executable"), }), Layer.succeed(DesktopChannel.DesktopBrowserChannel, { // Only tabs a test marks render on the desktop; the rest stay headless. available: true, awaitAttached: (key) => Effect.sync(() => desktopRenders(key.tabId)), detached: Stream.callback<{ threadId: string; tabId: string }>((queue) => Effect.acquireRelease( Effect.sync(() => { const onDetach = (key: { threadId: string; tabId: string }) => Queue.offerUnsafe(queue, key); desktopDetaches.on("detach", onDetach); return onDetach; }), (onDetach) => Effect.sync(() => desktopDetaches.off("detach", onDetach)), ), ), attached: Stream.callback<{ threadId: string; tabId: string }>((queue) => Effect.acquireRelease( Effect.sync(() => { const onAttach = (key: { threadId: string; tabId: string }) => Queue.offerUnsafe(queue, key); desktopDetaches.on("attach", onAttach); return onAttach; }), (onAttach) => Effect.sync(() => desktopDetaches.off("attach", onAttach)), ), ), isAttached: (key) => Effect.sync(() => desktopRenders(key.tabId)), endpoint: (key) => Effect.acquireRelease(Effect.succeed(`ws://desktop/${key.tabId}`), () => Effect.sync(() => releasedDesktopTabs.push(key.tabId)), ), pointer: () => Effect.void, }), ).pipe( Layer.provideMerge(ServerConfig.layerTest(process.cwd(), { prefix: "t3-server-browser-" })), Layer.provideMerge(NodeServices.layer), ); const layer = ServerBrowser.layer.pipe(Layer.provideMerge(dependencies)); const ready = Effect.gen(function* () { const browser = yield* ServerBrowser.ServerBrowser; const broker = yield* Broker.PreviewAutomationBroker; yield* Effect.yieldNow; const opened = yield* broker.invoke({ scope, operation: "open", input: { reuseExistingTab: false, show: false }, }); const tabId = PreviewTabId.make(opened.tabId!); return { browser, broker, tabId }; }); /** Fills a viewer's output the way a viewer that stopped reading leaves it. */ const stallViewer = ( viewer: ServerBrowser.ServerBrowserViewer, item: ServerBrowser.ServerBrowserViewerOutput, ) => { // The service hands out the read side of a queue it also writes to. const output = viewer.output as unknown as Queue.Queue; while (Queue.offerUnsafe(output, item)); }; const viewerInput = (tabId: string, canOperate: boolean) => ({ threadId: scope.thread.threadId, tabId, canOperate, maxWidth: 1280, maxHeight: 800, quality: 70, }); beforeEach(() => { contexts.length = 0; contextGate = null; contextFailure = null; desktopTabs.clear(); desktopRendersNext = false; releasedDesktopTabs.length = 0; desktopConnections.length = 0; }); it.live("readiness none responds immediately but takeover input waits for navigation commit", () => Effect.scoped( Effect.gen(function* () { const { browser, broker, tabId } = yield* ready; const viewer = yield* browser.attachViewer(viewerInput(tabId, true)); const committed = Promise.withResolvers(); const events: string[] = []; contexts[0]!.page.goto.mockImplementationOnce(async () => { await committed.promise; events.push("navigation committed"); }); yield* Effect.addFinalizer(() => Effect.sync(() => committed.resolve())); const response = yield* broker.invoke({ scope, tabId, operation: "navigate", input: { url: "http://localhost:5173/next", readiness: "none" }, }); expect(response.available).toBe(true); expect(events).toEqual([]); yield* Queue.clear(viewer.output); const takeover = yield* viewer.input({ type: "takeControl" }).pipe(Effect.forkScoped); let control = yield* Queue.take(viewer.output); while (control._tag !== "control" || control.controller !== "you") { control = yield* Queue.take(viewer.output); } const cdp = contexts[0]!.sessions.at(-1)!; const send = cdp.send.getMockImplementation()!; cdp.send.mockImplementation(async (operation, input) => { if (operation === "Input.insertText") events.push("human typed"); return send(operation, input); }); const typing = yield* viewer.input({ type: "text", text: "hello" }).pipe(Effect.forkScoped); yield* broker.invoke({ scope, tabId, operation: "status", input: {} }); expect(events).toEqual([]); committed.resolve(); yield* Fiber.join(takeover); yield* Fiber.join(typing); expect(events).toEqual(["navigation committed", "human typed"]); }), ).pipe(Effect.provide(layer)), ); it.live.each([ { method: "goto" as const, message: { type: "navigate", url: "http://localhost:5173/next" } }, { method: "goBack" as const, message: { type: "history", delta: -1 } }, { method: "goForward" as const, message: { type: "history", delta: 1 } }, { method: "reload" as const, message: { type: "reload" } }, ])("release waits for viewer $method to commit before agent actions", ({ method, message }) => Effect.scoped( Effect.gen(function* () { const { browser, broker, tabId } = yield* ready; const viewer = yield* browser.attachViewer(viewerInput(tabId, true)); yield* viewer.input({ type: "takeControl" }); yield* Queue.clear(viewer.output); const started = Promise.withResolvers(); const committed = Promise.withResolvers(); const events: string[] = []; contexts[0]!.page[method].mockImplementationOnce(async () => { started.resolve(); await committed.promise; events.push("navigation committed"); }); const navigate = yield* viewer.input(message).pipe(Effect.forkScoped); yield* Effect.addFinalizer(() => Effect.sync(() => committed.resolve())); yield* Effect.promise(() => started.promise); const releasing = yield* viewer.input({ type: "releaseControl" }).pipe(Effect.forkScoped); let control = yield* Queue.take(viewer.output); while (control._tag !== "control" || control.controller !== "agent") { control = yield* Queue.take(viewer.output); } const cdp = contexts[0]!.sessions[0]!; const send = cdp.send.getMockImplementation()!; cdp.send.mockImplementation(async (operation, input) => { if (operation === "Runtime.evaluate") events.push("agent acted"); return send(operation, input); }); const resumed = yield* broker .invoke({ scope, tabId, operation: "evaluate", input: { expression: "resumed()" }, }) .pipe(Effect.forkScoped); yield* broker.invoke({ scope, tabId, operation: "status", input: {} }); expect(events).toEqual([]); committed.resolve(); yield* Fiber.join(navigate); yield* Fiber.join(releasing); yield* Fiber.join(resumed); expect(events).toEqual(["navigation committed", "agent acted"]); const calls = contexts[0]!.page[method].mock.calls; expect(calls[0]?.at(-1)).toMatchObject({ waitUntil: "commit", timeout: 15_000 }); }), ).pipe(Effect.provide(layer)), ); it.live("enforces provider ownership and explicit targets when a session has multiple tabs", () => Effect.scoped( Effect.gen(function* () { const { broker, tabId } = yield* ready; const foreign = yield* broker .invoke({ scope: asSession("agent-b"), tabId, operation: "evaluate", input: { expression: "foreign()" }, }) .pipe(Effect.flip); expect(foreign).toMatchObject({ _tag: "PreviewAutomationControlInterruptedError", reason: "agentMismatch", }); expect(contexts[0]!.sessions[0]!.send).not.toHaveBeenCalledWith( "Runtime.evaluate", expect.anything(), ); yield* broker.invoke({ scope, operation: "open", input: { reuseExistingTab: false, show: false }, }); const ambiguous = yield* broker .invoke({ scope, operation: "evaluate", input: { expression: "ambiguous()" } }) .pipe(Effect.flip); expect(ambiguous).toMatchObject({ _tag: "PreviewAutomationControlInterruptedError", reason: "tabRequired", }); const ambiguousStop = yield* broker .invoke({ scope, operation: "recordingStop", input: {} }) .pipe(Effect.flip); expect(ambiguousStop).toMatchObject({ _tag: "PreviewAutomationControlInterruptedError", reason: "tabRequired", }); const explicitStop = yield* broker .invoke({ scope, tabId, operation: "recordingStop", input: {} }) .pipe(Effect.flip); expect(explicitStop).toMatchObject({ _tag: "PreviewAutomationExecutionError", cause: { _tag: "PreviewAutomationRecordingNotActiveError" }, }); const result = yield* broker.invoke({ scope, tabId, operation: "evaluate", input: { expression: "owned()" }, }); expect(result).toBe("evaluated"); expect(contexts).toHaveLength(2); }), ).pipe(Effect.provide(layer)), ); it.live("streams to a read-only viewer without allowing takeover, input, or viewport changes", () => Effect.scoped( Effect.gen(function* () { const { browser, tabId } = yield* ready; const viewer = yield* browser.attachViewer(viewerInput(tabId, false)); const page = contexts[0]!.page; page.setViewportSize.mockClear(); const session = contexts[0]!.sessions.at(-1)!; session.send.mockClear(); for (const message of [ { type: "takeControl" }, { type: "key", action: "down", key: "a", text: "a" }, { type: "resize", width: 390, height: 844 }, { type: "viewport", setting: { _tag: "freeform", width: 390, height: 844 } }, ]) yield* viewer.input(message); expect(page.setViewportSize).not.toHaveBeenCalled(); expect(session.send.mock.calls.some(([method]) => method.startsWith("Input."))).toBe(false); const outputs = yield* Queue.takeAll(viewer.output); expect(outputs).toContainEqual( expect.objectContaining({ _tag: "frame", data: Buffer.from("frame") }), ); expect(outputs).toContainEqual(expect.objectContaining({ _tag: "viewport" })); expect(outputs).toContainEqual( expect.objectContaining({ _tag: "control", canOperate: false, controller: "agent" }), ); const operator = yield* browser.attachViewer(viewerInput(tabId, true)); yield* operator.input({ type: "takeControl" }); yield* operator.input({ type: "text", text: "typed" }); yield* operator.input({ type: "resize", width: 390, height: 844 }); expect(contexts[0]!.sessions.at(-1)!.send).toHaveBeenCalledWith("Input.insertText", { text: "typed", }); expect(page.setViewportSize).toHaveBeenCalledWith({ width: 390, height: 844 }); yield* operator.input({ type: "releaseControl" }); }), ).pipe(Effect.provide(layer)), ); it.live("applies any client's viewport, appearance, and zoom to a headless tab", () => Effect.scoped( Effect.gen(function* () { const { tabId } = yield* ready; const manager = yield* Manager.PreviewManager; const page = contexts[0]!.page; const session = contexts[0]!.sessions[0]!; const target = { threadId: scope.thread.threadId, tabId }; // The agent owns this tab, and the client sends no takeover first. yield* manager.resize({ ...target, viewport: { _tag: "freeform", width: 390, height: 844 } }); yield* manager.adjust({ ...target, colorScheme: "dark", zoomFactor: 1.25 }); // Settings apply in order, so the reload landing means the earlier ones did too. const reloaded = Promise.withResolvers(); const send = session.send.getMockImplementation()!; session.send.mockImplementation(async (method, input) => { if (method === "Page.reload") reloaded.resolve(); return send(method, input); }); yield* manager.adjust({ ...target, hardReload: true }); yield* Effect.promise(() => reloaded.promise); expect(session.send).toHaveBeenCalledWith("Page.reload", { ignoreCache: true }); expect(page.setViewportSize).toHaveBeenCalledWith({ width: 390, height: 844 }); expect(page.emulateMedia).toHaveBeenCalledWith({ colorScheme: "dark" }); // Zoom lays the page out in fewer CSS pixels and draws each one larger. expect(session.send).toHaveBeenCalledWith("Emulation.setDeviceMetricsOverride", { width: 312, height: 675, deviceScaleFactor: 2.5, mobile: false, }); }), ).pipe(Effect.provide(layer)), ); it.live( "takeover waits for the running agent and revokes snapshot refs before returning control", () => Effect.scoped( Effect.gen(function* () { const { browser, broker, tabId } = yield* ready; const snapshot = yield* broker.invoke({ scope, tabId, operation: "snapshot", input: {}, }); const ref = /\[ref=([^\]]+)\]/.exec(String(snapshot.accessibilityTree))![1]!; const viewer = yield* browser.attachViewer(viewerInput(tabId, true)); const started = Promise.withResolvers(); const finish = Promise.withResolvers>(); const session = contexts[0]!.sessions[0]!; session.send.mockImplementationOnce(async () => { started.resolve(); return finish.promise; }); const running = yield* broker .invoke({ scope, tabId, operation: "evaluate", input: { expression: "pending()" } }) .pipe(Effect.forkScoped); yield* Effect.addFinalizer(() => Effect.sync(() => finish.resolve({ result: { value: "finished" } })), ); yield* Effect.promise(() => started.promise); const takeover = yield* viewer.input({ type: "takeControl" }).pipe(Effect.forkScoped); let control = yield* Queue.take(viewer.output); while (control._tag !== "control" || control.controller !== "you") { control = yield* Queue.take(viewer.output); } const rejected = yield* broker .invoke({ scope, tabId, operation: "evaluate", input: { expression: "racing()" } }) .pipe(Effect.flip); expect(rejected).toMatchObject({ _tag: "PreviewAutomationControlInterruptedError", reason: "humanControl", }); finish.resolve({ result: { value: "finished" } }); expect(yield* Fiber.join(running)).toBe("finished"); yield* Fiber.join(takeover); yield* viewer.input({ type: "releaseControl" }); const stale = yield* broker .invoke({ scope, tabId, operation: "click", input: { locator: `aria-ref=${ref}` } }) .pipe(Effect.flip); expect(stale._tag).toBe("PreviewAutomationInvalidSelectorError"); expect(contexts[0]!.page.locator).not.toHaveBeenCalled(); expect( yield* broker.invoke({ scope, tabId, operation: "evaluate", input: { expression: "resumed()" }, }), ).toBe("evaluated"); }), ).pipe(Effect.provide(layer)), ); it.live("reports a pending dialog without evaluating the page and resolves it explicitly", () => Effect.scoped( Effect.gen(function* () { const { broker, tabId } = yield* ready; const page = contexts[0]!.page; yield* broker.invoke({ scope, tabId, operation: "navigate", input: { url: "http://localhost:5173" }, }); const started = Promise.withResolvers(); const resolvedEvaluation = Promise.withResolvers>(); const session = contexts[0]!.sessions[0]!; const send = session.send.getMockImplementation()!; session.send.mockImplementation(async (method, input) => { if (method !== "Runtime.evaluate") return send(method, input); started.resolve(); return resolvedEvaluation.promise; }); const blockedAction = yield* broker .invoke({ scope, tabId, operation: "evaluate", input: { expression: 'confirm("Delete row?")' }, }) .pipe(Effect.forkScoped); yield* Effect.addFinalizer(() => Effect.sync(() => resolvedEvaluation.resolve({ result: { value: true } })), ); yield* Effect.promise(() => started.promise); const dialog = { type: () => "confirm", message: () => "Delete row?", defaultValue: () => "", accept: vi.fn(async () => { resolvedEvaluation.resolve({ result: { value: true } }); }), dismiss: vi.fn(async () => {}), }; page.emit("dialog", dialog); page.title.mockClear(); const status = yield* broker.invoke({ scope, tabId, operation: "status", input: {}, }); expect(status.dialog).toMatchObject({ type: "confirm", message: "Delete row?" }); expect(page.title).not.toHaveBeenCalled(); expect(dialog.dismiss).not.toHaveBeenCalled(); const reuse = yield* broker .invoke({ scope, tabId, operation: "open", input: { url: "http://localhost:5173/other" }, }) .pipe(Effect.flip); expect(reuse).toMatchObject({ _tag: "PreviewAutomationControlInterruptedError", reason: "dialogPending", }); expect(page.goto).toHaveBeenCalledTimes(1); yield* broker.invoke({ scope, tabId, operation: "dialog", input: { accept: true } }); expect(yield* Fiber.join(blockedAction)).toBe(true); expect(dialog.accept).toHaveBeenCalledTimes(1); const resolved = yield* broker.invoke({ scope, tabId, operation: "status", input: {}, }); expect(resolved.dialog).toBeNull(); }), ).pipe(Effect.provide(layer)), ); it.live("the owner can close a tab while an agent action waits on its dialog", () => Effect.scoped( Effect.gen(function* () { const { broker, tabId } = yield* ready; const page = contexts[0]!.page; const started = Promise.withResolvers(); const finish = Promise.withResolvers>(); contexts[0]!.sessions[0]!.send.mockImplementationOnce(async () => { started.resolve(); return finish.promise; }); page.on("close", () => finish.resolve({ result: { value: "closed" } })); const running = yield* broker .invoke({ scope, tabId, operation: "evaluate", input: { expression: 'confirm("Delete?")' }, }) .pipe(Effect.forkScoped); yield* Effect.addFinalizer(() => Effect.sync(() => finish.resolve({ result: { value: "cleanup" } })), ); yield* Effect.promise(() => started.promise); page.emit("dialog", { type: () => "confirm", message: () => "Delete?", defaultValue: () => "", accept: vi.fn(), dismiss: vi.fn(), }); const foreign = yield* broker .invoke({ scope: asSession("agent-b"), tabId, operation: "close", input: {}, }) .pipe(Effect.flip); expect(foreign).toMatchObject({ _tag: "PreviewAutomationControlInterruptedError", reason: "agentMismatch", }); expect(page.close).not.toHaveBeenCalled(); yield* broker.invoke({ scope, tabId, operation: "close", input: {} }); expect(yield* Fiber.join(running)).toBe("closed"); expect(page.close).toHaveBeenCalled(); const manager = yield* Manager.PreviewManager; expect((yield* manager.list({ threadId: scope.thread.threadId })).sessions).toHaveLength(0); const afterClose = yield* broker .invoke({ scope, tabId, operation: "evaluate", input: { expression: "late()" } }) .pipe(Effect.flip); expect(afterClose._tag).toBe("PreviewAutomationTabNotFoundError"); }), ).pipe(Effect.provide(layer)), ); it.live("a popup becomes the agent's own tab and keeps its opener page", () => Effect.scoped( Effect.gen(function* () { const { broker, tabId } = yield* ready; const opener = contexts[0]!.page; const popup = makeContext(); opener.emit("popup", popup.page); const manager = yield* Manager.PreviewManager; let sessions = (yield* manager.list({ threadId: scope.thread.threadId })).sessions; while (sessions.length < 2) { yield* Effect.sleep("5 millis"); sessions = (yield* manager.list({ threadId: scope.thread.threadId })).sessions; } const popupTab = sessions.find((session) => session.tabId !== tabId)!; const opened = sessions.find((session) => session.tabId === tabId)!; expect(popupTab).toMatchObject({ automationOwner: opened.automationOwner, reveal: false }); const status = yield* broker.invoke({ scope, tabId, operation: "status", input: {}, }); expect(status.tabs).toEqual( expect.arrayContaining([ expect.objectContaining({ tabId }), expect.objectContaining({ tabId: popupTab.tabId, openerTabId: tabId }), ]), ); expect(opener.goto).not.toHaveBeenCalled(); expect(opener.close).not.toHaveBeenCalled(); // The page the popup script holds is the tab, so closing it ends the tab. yield* Effect.promise(() => popup.page.close()); while ((yield* manager.list({ threadId: scope.thread.threadId })).sessions.length > 1) { yield* Effect.sleep("5 millis"); } }), ).pipe(Effect.provide(layer)), ); it.live("a tab opened to a file the browser cannot show reports the file to download", () => Effect.scoped( Effect.gen(function* () { const browser = yield* ServerBrowser.ServerBrowser; const manager = yield* Manager.PreviewManager; const events = yield* manager.subscribeEvents; yield* Effect.yieldNow; const opened = yield* manager.open({ threadId: scope.thread.threadId, runtime: "server" }); const viewer = yield* browser.attachViewer(viewerInput(opened.tabId, true)); const page = contexts[0]!.page; const pdf = "https://example.com/paper.pdf"; const request = { url: () => pdf, method: () => "GET", isNavigationRequest: () => true }; // What Chromium reports when a navigation turns into a download. page.emit("requestfailed", { ...request, frame: () => page, failure: () => ({ errorText: "net::ERR_ABORTED" }), }); // Stands in for Chromium, which writes the file itself. const saved = yield* Queue.unbounded(); const written = Promise.withResolvers(); page.emit("download", { failure: async () => null, saveAs: (path: string) => { Queue.offerUnsafe(saved, path); return written.promise; }, suggestedFilename: () => "paper.pdf", url: () => pdf, }); const fs = yield* FileSystem.FileSystem; yield* fs.writeFileString(yield* Queue.take(saved), "%PDF"); written.resolve(); let status = yield* PubSub.take(events); while (status.type !== "failed") status = yield* PubSub.take(events); expect(status).toMatchObject({ url: pdf, download: { fileName: "paper.pdf" } }); // The tab shows the file, so no separate download toast is offered. expect((yield* Queue.clear(viewer.output)).some((item) => item._tag === "download")).toBe( false, ); }), ).pipe(Effect.provide(layer)), ); it.live("a file a blank tab opened is offered to download if the tab moves on while it saves", () => Effect.scoped( Effect.gen(function* () { const browser = yield* ServerBrowser.ServerBrowser; const manager = yield* Manager.PreviewManager; yield* Effect.yieldNow; const opened = yield* manager.open({ threadId: scope.thread.threadId, runtime: "server" }); const viewer = yield* browser.attachViewer(viewerInput(opened.tabId, true)); yield* viewer.input({ type: "takeControl" }); const page = contexts[0]!.page; const pdf = "https://example.com/paper.pdf"; const navigation = { url: () => pdf, method: () => "GET", isNavigationRequest: () => true, frame: () => page, failure: () => ({ errorText: "net::ERR_ABORTED" }), }; page.emit("request", navigation); page.emit("requestfailed", navigation); const saved = yield* Queue.unbounded(); const written = Promise.withResolvers(); page.emit("download", { failure: async () => null, saveAs: (path: string) => { Queue.offerUnsafe(saved, path); return written.promise; }, suggestedFilename: () => "paper.pdf", url: () => pdf, }); const fs = yield* FileSystem.FileSystem; yield* fs.writeFileString(yield* Queue.take(saved), "%PDF"); // The person navigates elsewhere while the file is still being written. page.emit("request", { url: () => "https://example.com/next", method: () => "GET", isNavigationRequest: () => true, frame: () => page, }); written.resolve(); let offered = yield* Queue.take(viewer.output); while (offered._tag !== "download") offered = yield* Queue.take(viewer.output); expect(offered).toMatchObject({ _tag: "download", fileName: "paper.pdf" }); }), ).pipe(Effect.provide(layer)), ); it.live("a download from a superseded navigation leaves the newer navigation loading", () => Effect.scoped( Effect.gen(function* () { const { broker, tabId } = yield* ready; const page = contexts[0]!.page; const pdf = "https://example.com/paper.pdf"; const navigation = { url: () => pdf, method: () => "GET", isNavigationRequest: () => true, frame: () => page, failure: () => ({ errorText: "net::ERR_ABORTED" }), }; page.emit("request", navigation); page.emit("requestfailed", navigation); const saved = yield* Queue.unbounded(); const written = Promise.withResolvers(); page.emit("download", { failure: async () => null, saveAs: (path: string) => { Queue.offerUnsafe(saved, path); return written.promise; }, suggestedFilename: () => "paper.pdf", url: () => pdf, }); const fs = yield* FileSystem.FileSystem; yield* fs.writeFileString(yield* Queue.take(saved), "%PDF"); // The person navigates elsewhere while the file is still being written. page.emit("request", { url: () => "https://example.com/next", method: () => "GET", isNavigationRequest: () => true, frame: () => page, }); written.resolve(); const status = () => broker.invoke({ scope, tabId, operation: "status", input: {} }); while ((yield* status()).downloads?.length !== 1) yield* Effect.sleep("5 millis"); expect((yield* status()).loading).toBe(true); }), ).pipe(Effect.provide(layer)), ); it.live( "a late failure from a request that predates tracking leaves a newer navigation loading", () => Effect.scoped( Effect.gen(function* () { const { broker, tabId } = yield* ready; const page = contexts[0]!.page; const navigation = (url: string) => ({ url: () => url, method: () => "GET", isNavigationRequest: () => true, frame: () => page, failure: () => ({ errorText: "net::ERR_CONNECTION_REFUSED" }), }); page.emit("request", navigation("https://example.com/next")); // A popup's first request can start before the tab listens for requests. page.emit("requestfailed", navigation("https://example.com/first")); const status = yield* broker.invoke({ scope, tabId, operation: "status", input: {}, }); expect(status.loading).toBe(true); }), ).pipe(Effect.provide(layer)), ); it.live("an aborted navigation that no download explains stops loading", () => Effect.scoped( Effect.gen(function* () { const { broker, tabId } = yield* ready; const page = contexts[0]!.page; const navigation = { url: () => "https://example.com/cancelled", method: () => "GET", isNavigationRequest: () => true, frame: () => page, failure: () => ({ errorText: "net::ERR_ABORTED" }), }; const status = () => broker.invoke({ scope, tabId, operation: "status", input: {} }); vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] }); try { page.emit("request", navigation); page.emit("requestfailed", navigation); expect((yield* status()).loading).toBe(true); vi.advanceTimersByTime(5_000); } finally { vi.useRealTimers(); } expect((yield* status()).loading).toBe(false); }), ).pipe(Effect.provide(layer)), ); it.live("a popup from a person's click is shown to that person, with its opener kept", () => Effect.scoped( Effect.gen(function* () { const browser = yield* ServerBrowser.ServerBrowser; const manager = yield* Manager.PreviewManager; yield* Effect.yieldNow; const opened = yield* manager.open({ threadId: scope.thread.threadId, runtime: "server" }); const watcher = yield* browser.attachViewer(viewerInput(opened.tabId, false)); const viewer = yield* browser.attachViewer(viewerInput(opened.tabId, true)); yield* viewer.input({ type: "takeControl" }); const opener = contexts[0]!.page; const popup = makeContext(); opener.emit("popup", popup.page); let shown = yield* Queue.take(viewer.output); while (shown._tag !== "popup") shown = yield* Queue.take(viewer.output); const sessions = (yield* manager.list({ threadId: scope.thread.threadId })).sessions; expect(sessions.map((session) => session.tabId)).toEqual( expect.arrayContaining([opened.tabId, shown.tabId]), ); expect((yield* Queue.clear(watcher.output)).some((item) => item._tag === "popup")).toBe( false, ); expect(opener.close).not.toHaveBeenCalled(); }), ).pipe(Effect.provide(layer)), ); it.live("closing a tab while a viewer is still opening it does not leave its page behind", () => Effect.scoped( Effect.gen(function* () { const browser = yield* ServerBrowser.ServerBrowser; const manager = yield* Manager.PreviewManager; yield* Effect.yieldNow; // The background open fails, so the tab exists only as a session. contextFailure = new Error("first launch failed"); const snapshot = yield* manager.open({ threadId: scope.thread.threadId, runtime: "server" }); yield* Effect.sleep("10 millis"); contextFailure = null; contextGate = Promise.withResolvers(); const attaching = yield* browser .attachViewer(viewerInput(snapshot.tabId, false)) .pipe(Effect.flip, Effect.forkScoped); yield* Effect.sleep("10 millis"); yield* manager.close({ threadId: scope.thread.threadId, tabId: snapshot.tabId }); yield* Effect.sleep("10 millis"); contextGate.resolve(); expect((yield* Fiber.join(attaching))._tag).toBe("ServerBrowserTabNotFoundError"); expect(contexts).toHaveLength(1); expect(contexts[0]!.page.close).toHaveBeenCalled(); }), ).pipe(Effect.provide(layer)), ); it.live("page copies reach only the controlling viewer right after its input", () => Effect.scoped( Effect.gen(function* () { const { browser, tabId } = yield* ready; const page = contexts[0]!.page; const watcher = yield* browser.attachViewer(viewerInput(tabId, false)); const viewer = yield* browser.attachViewer(viewerInput(tabId, true)); yield* viewer.input({ type: "takeControl" }); const clipboard = (queue: typeof viewer.output) => Queue.clear(queue).pipe( Effect.map((items) => items.filter((item) => item._tag === "clipboard")), ); // A page writing on its own, without a recent gesture, stays on the server. clipboardBinding!({ page }, "unprompted"); expect(yield* clipboard(viewer.output)).toEqual([]); yield* viewer.input({ type: "key", action: "down", key: "c", code: "KeyC", modifiers: 4 }); const cdp = contexts[0]!.sessions.at(-1)!; expect(cdp.send).toHaveBeenCalledWith( "Input.dispatchKeyEvent", expect.objectContaining({ key: "c", commands: ["copy"] }), ); clipboardBinding!({ page }, "copied"); expect(yield* clipboard(viewer.output)).toEqual([{ _tag: "clipboard", text: "copied" }]); expect(yield* clipboard(watcher.output)).toEqual([]); }), ).pipe(Effect.provide(layer)), ); it.live("a page download is saved, offered to the controller, and listed for the agent", () => Effect.scoped( Effect.gen(function* () { const { browser, broker, tabId } = yield* ready; const page = contexts[0]!.page; const viewer = yield* browser.attachViewer(viewerInput(tabId, true)); yield* viewer.input({ type: "takeControl" }); yield* Queue.clear(viewer.output); // Stands in for Chromium, which writes the file itself. const saved = yield* Queue.unbounded(); const written = Promise.withResolvers(); page.emit("download", { failure: async () => null, saveAs: (path: string) => { Queue.offerUnsafe(saved, path); return written.promise; }, suggestedFilename: () => "report.csv", url: () => "blob:http://localhost:5173/1", }); const fs = yield* FileSystem.FileSystem; const path = yield* Queue.take(saved); yield* fs.writeFileString(path, "a,b"); written.resolve(); let offered = yield* Queue.take(viewer.output); while (offered._tag !== "download") offered = yield* Queue.take(viewer.output); expect(offered).toMatchObject({ _tag: "download", fileName: "report.csv", sizeBytes: 3 }); const file = yield* browser.openDownload({ threadId: scope.thread.threadId, tabId, downloadId: offered.id, }); expect(Option.isSome(file) && file.value.fileName).toBe("report.csv"); const status = yield* broker.invoke({ scope, tabId, operation: "status", input: {}, }); expect(status.downloads).toEqual([ expect.objectContaining({ fileName: "report.csv", sizeBytes: 3 }), ]); expect( Option.isNone( yield* browser.openDownload({ threadId: scope.thread.threadId, tabId, downloadId: "guess", }), ), ).toBe(true); }), ).pipe(Effect.provide(layer)), ); it.live("a page's file picker goes to the controller and takes its uploaded files", () => Effect.scoped( Effect.gen(function* () { const { browser, tabId } = yield* ready; const page = contexts[0]!.page; const viewer = yield* browser.attachViewer(viewerInput(tabId, true)); yield* viewer.input({ type: "takeControl" }); const setFiles = vi.fn(async (_files: unknown) => {}); page.emit("filechooser", { isMultiple: () => false, element: () => ({ getAttribute: async () => ".csv" }), setFiles, }); let offered = yield* Queue.take(viewer.output); while (offered._tag !== "fileChooser") offered = yield* Queue.take(viewer.output); expect(offered).toMatchObject({ multiple: false, accept: ".csv" }); const file = (name: string) => ({ name, mimeType: "text/csv", buffer: Buffer.from(name) }); const answer = (chooserId: string) => browser.answerFileChooser({ threadId: scope.thread.threadId, tabId, chooserId, files: [file("a.csv"), file("b.csv")], }); expect(yield* answer("other")).toBe(false); expect(yield* answer(offered.id)).toBe(true); // A single-file input only receives the first file. expect(setFiles).toHaveBeenCalledExactlyOnceWith([file("a.csv")]); let closed = yield* Queue.take(viewer.output); while (closed._tag !== "fileChooserClosed") closed = yield* Queue.take(viewer.output); expect(closed.id).toBe(offered.id); expect(yield* answer(offered.id)).toBe(false); }), ).pipe(Effect.provide(layer)), ); it.live("a file picker replaces a stalled viewer backlog instead of being dropped", () => Effect.scoped( Effect.gen(function* () { const { browser, tabId } = yield* ready; const page = contexts[0]!.page; const viewer = yield* browser.attachViewer(viewerInput(tabId, true)); yield* viewer.input({ type: "takeControl" }); const accept = Promise.withResolvers(); page.emit("filechooser", { isMultiple: () => false, element: () => ({ getAttribute: () => accept.promise }), setFiles: async () => {}, }); // The viewer stopped reading: its output is full when the picker opens. yield* Queue.clear(viewer.output); stallViewer(viewer, { _tag: "viewport", width: 1, height: 1 }); accept.resolve(".csv"); // The picker is offered within the microtasks that follow its accept attribute. yield* Effect.promise(() => new Promise((resolve) => setImmediate(resolve))); expect(yield* Queue.clear(viewer.output)).toEqual([ expect.objectContaining({ _tag: "fileChooser", accept: ".csv" }), ]); }), ).pipe(Effect.provide(layer)), ); it.live("control updates replace a stalled viewer backlog in the order they happen", () => Effect.scoped( Effect.gen(function* () { const { browser, tabId } = yield* ready; const viewer = yield* browser.attachViewer(viewerInput(tabId, true)); yield* Queue.clear(viewer.output); // Chromium is slow to take back the frames the replacement drops. const acked = Promise.withResolvers(); yield* Effect.addFinalizer(() => Effect.sync(() => acked.resolve())); stallViewer(viewer, { _tag: "frame", data: Buffer.alloc(0), ack: Effect.promise(() => acked.promise), }); yield* viewer.input({ type: "takeControl" }); yield* viewer.input({ type: "releaseControl" }); const controls = (yield* Queue.clear(viewer.output)).flatMap((item) => item._tag === "control" ? [item.controller] : [], ); expect(controls[0]).toBe("you"); expect(controls.at(-1)).not.toBe("you"); }), ).pipe(Effect.provide(layer)), ); it.live("an agent's tabs stop at the limit until an unwatched idle tab closes", () => Effect.scoped( Effect.gen(function* () { const { broker, tabId } = yield* ready; const open = broker.invoke({ scope, operation: "open", input: { reuseExistingTab: false, show: false }, }); for (let count = 1; count < 8; count += 1) yield* open; expect(yield* Effect.flip(open)).toMatchObject({ _tag: "PreviewAutomationControlInterruptedError", reason: "tabLimit", }); // Another agent session keeps its own budget. yield* broker.invoke({ scope: asSession("agent-b"), operation: "open", input: { reuseExistingTab: false, show: false }, }); const later = (yield* Clock.currentTimeMillis) + 31 * 60 * 1000; const clock = vi.spyOn(Date, "now").mockReturnValue(later); yield* Effect.addFinalizer(() => Effect.sync(() => clock.mockRestore())); // The first tab is watched, so it stays open while the agent's other idle tabs close. const browser = yield* ServerBrowser.ServerBrowser; yield* browser.attachViewer(viewerInput(tabId, false)); const reopened = yield* open; const manager = yield* Manager.PreviewManager; const { sessions } = yield* manager.list({ threadId: scope.thread.threadId }); expect(sessions.map((session) => session.tabId).toSorted()).toEqual( [tabId, reopened.tabId].toSorted(), ); }), ).pipe(Effect.provide(layer)), ); it.live("an agent answers the page's file picker or sets files on a file input", () => Effect.scoped( Effect.gen(function* () { const { broker, tabId } = yield* ready; const page = contexts[0]!.page; const upload = (input: Record) => broker.invoke({ scope, tabId, operation: "upload", input }); expect(yield* Effect.flip(upload({ paths: ["/tmp/a.csv"] }))).toMatchObject({ _tag: "PreviewAutomationExecutionError", }); const setFiles = vi.fn(async (_files: unknown) => {}); page.emit("filechooser", { isMultiple: () => false, element: () => ({ getAttribute: async () => ".csv" }), setFiles, }); const status = broker.invoke({ scope, tabId, operation: "status", input: {}, }); let opened = yield* status; while (!opened.fileChooser) { yield* Effect.sleep("5 millis"); opened = yield* status; } expect(opened.fileChooser).toEqual({ multiple: false, accept: ".csv" }); // A single-file picker rejects several files instead of dropping some. yield* Effect.flip(upload({ paths: ["/tmp/a.csv", "/tmp/b.csv"] })); yield* upload({ paths: ["/tmp/a.csv"] }); expect(setFiles).toHaveBeenCalledExactlyOnceWith(["/tmp/a.csv"], expect.anything()); expect((yield* status).fileChooser).toBeNull(); const setInputFiles = vi.fn(async () => {}); page.locator.mockReturnValue({ setInputFiles } as never); yield* upload({ paths: ["/tmp/b.csv"], locator: "input[type=file]" }); expect(page.locator).toHaveBeenLastCalledWith("input[type=file]"); expect(setInputFiles).toHaveBeenCalledWith(["/tmp/b.csv"], expect.anything()); }), ).pipe(Effect.provide(layer)), ); it.live("viewers see the agent's pointer move to its target and click there", () => Effect.scoped( Effect.gen(function* () { const { browser, broker, tabId } = yield* ready; const page = contexts[0]!.page; const viewer = yield* browser.attachViewer(viewerInput(tabId, false)); const click = vi.fn(async () => {}); page.locator.mockReturnValue({ scrollIntoViewIfNeeded: async () => {}, boundingBox: async () => ({ x: 100, y: 40, width: 80, height: 20 }), click, } as never); yield* broker.invoke({ scope, tabId, operation: "click", input: { locator: "#go" } }); const pointers = (yield* Queue.clear(viewer.output)).filter( (item) => item._tag === "pointer", ); expect(pointers).toEqual([ expect.objectContaining({ phase: "move", x: 140, y: 50 }), expect.objectContaining({ phase: "click", x: 140, y: 50 }), ]); expect(click).toHaveBeenCalledOnce(); }), ).pipe(Effect.provide(layer)), ); it.live("drives the desktop's own page for a tab the desktop renders", () => Effect.scoped( Effect.gen(function* () { const browser = yield* ServerBrowser.ServerBrowser; const broker = yield* Broker.PreviewAutomationBroker; const manager = yield* Manager.PreviewManager; yield* Effect.yieldNow; desktopRendersNext = true; const opened = yield* manager.open({ threadId: scope.thread.threadId, runtime: "server" }); const viewer = yield* browser.attachViewer(viewerInput(opened.tabId, true)); expect(desktopConnections.map((connection) => connection.endpoint)).toEqual([ `ws://desktop/${opened.tabId}`, ]); // No headless context was launched for it. expect(contexts).toEqual([]); const page = desktopConnections[0]!.context.page; // The desktop panel sizes its page, so a viewer resize leaves it alone. yield* viewer.input({ type: "takeControl" }); yield* viewer.input({ type: "resize", width: 390, height: 844 }); expect(page.setViewportSize).not.toHaveBeenCalled(); yield* viewer.input({ type: "releaseControl" }); // Agents reach it through the same engine as a headless tab. const evaluated = yield* broker.invoke({ scope: asSession("agent-desktop"), operation: "status", input: {}, tabId: PreviewTabId.make(opened.tabId), }); expect(evaluated).toMatchObject({ tabId: opened.tabId }); // Closing the session lets go of the desktop's page without closing it. yield* manager.close({ threadId: scope.thread.threadId, tabId: opened.tabId }); while (releasedDesktopTabs.length === 0) yield* Effect.yieldNow; expect(releasedDesktopTabs).toEqual([opened.tabId]); expect(page.close).not.toHaveBeenCalled(); }), ).pipe(Effect.provide(layer)), ); it.live("a desktop page the desktop takes back reconnects instead of closing", () => Effect.scoped( Effect.gen(function* () { const browser = yield* ServerBrowser.ServerBrowser; const manager = yield* Manager.PreviewManager; yield* Effect.yieldNow; desktopRendersNext = true; const opened = yield* manager.open({ threadId: scope.thread.threadId, runtime: "server" }); const viewer = yield* browser.attachViewer(viewerInput(opened.tabId, false)); // Devtools opened on the desktop, so it withdrew the page's debugger. // The server's listener subscribes in its own fiber; detach once it is there. while (desktopDetaches.listenerCount("detach") === 0) yield* Effect.yieldNow; desktopDetaches.emit("detach", { threadId: scope.thread.threadId, tabId: opened.tabId }); let end = yield* Queue.take(viewer.output); while (end._tag !== "reconnect" && end._tag !== "gone") end = yield* Queue.take(viewer.output); expect(end._tag).toBe("reconnect"); expect(releasedDesktopTabs).toEqual([opened.tabId]); // The session survives, and the next viewer reaches the page again. const { sessions } = yield* manager.list({ threadId: scope.thread.threadId }); expect(sessions.map((session) => session.tabId)).toContain(opened.tabId); yield* browser.attachViewer(viewerInput(opened.tabId, false)); expect(desktopConnections).toHaveLength(2); }), ).pipe(Effect.provide(layer)), ); it.live("a desktop page that comes back reconnects without waiting for a viewer", () => Effect.scoped( Effect.gen(function* () { const manager = yield* Manager.PreviewManager; yield* ServerBrowser.ServerBrowser; yield* Effect.yieldNow; desktopRendersNext = true; const opened = yield* manager.open({ threadId: scope.thread.threadId, runtime: "server" }); while (desktopConnections.length === 0) yield* Effect.yieldNow; while (desktopDetaches.listenerCount("detach") === 0) yield* Effect.yieldNow; // DevTools opened, then closed: the desktop withdraws the page and returns it. desktopDetaches.emit("detach", { threadId: scope.thread.threadId, tabId: opened.tabId }); while (releasedDesktopTabs.length === 0) yield* Effect.yieldNow; desktopDetaches.emit("attach", { threadId: scope.thread.threadId, tabId: opened.tabId }); // Without a viewer or agent, the server drives the page again, so its URL keeps reaching clients. while (desktopConnections.length < 2) yield* Effect.yieldNow; expect(desktopConnections).toHaveLength(2); }), ).pipe(Effect.provide(layer)), );