import { EventEmitter } from "node:events"; import { Writable } from "node:stream"; import { afterEach, describe, expect, it } from "vitest"; import { openerCandidates } from "../open"; import { createUiRuntime, type UiRuntime } from "../runtime"; describe("ui browser opener", () => { it("uses platform openers", () => { expect( openerCandidates("http://x", { platform: "win32", env: {} })[0], ).toEqual({ command: "cmd.exe", args: ["/c", "start", "", "http://x"], }); expect( openerCandidates("http://x", { platform: "darwin", env: {} })[0], ).toEqual({ command: "open", args: ["http://x"], }); expect( openerCandidates("http://x", { platform: "linux", env: {} })[0], ).toEqual({ command: "xdg-open", args: ["http://x"], }); }); it("uses termux opener when available", () => { expect( openerCandidates("http://x", { platform: "linux", env: { TERMUX_VERSION: "1" }, })[0], ).toEqual({ command: "termux-open-url", args: ["http://x"], }); }); }); const runtimes: UiRuntime[] = []; afterEach(async () => { await Promise.all( runtimes.splice(0).map((runtime) => runtime.stop("test cleanup")), ); }); describe("ui runtime", () => { it("starts, reports status, redraws QR, and stops", async () => { const runtime = await createUiRuntime({ sessionLabel: "ui: test", address: "127.0.0.1", openBrowser: false, }); runtimes.push(runtime); expect(runtime.status().active).toBe(true); expect(new URL(runtime.status().url ?? "").searchParams.has("token")).toBe( true, ); expect(runtime.widgetLines().join("\n")).toContain("ui: test"); await runtime.redrawQr(); expect(runtime.widgetLines().join("\n")).toContain("mobile qr"); await runtime.stop("test"); expect(runtime.status().active).toBe(false); }); it("is safe to stop twice", async () => { const runtime = await createUiRuntime({ sessionLabel: "ui: test", address: "127.0.0.1", openBrowser: false, }); await runtime.stop("first"); await runtime.stop("second"); expect(runtime.status().active).toBe(false); }); it("seeds an active branch and starts rpc when a session manager is provided", async () => { const sent: unknown[] = []; const runtime = await createUiRuntime({ sessionLabel: "ui: test", address: "127.0.0.1", cwd: "C:/repo", openBrowser: false, sessionManager: fakeSessionManager(), startRpcBridge: ({ sessionPath, onState }) => { onState?.("connected"); return fakeRpc(sent, sessionPath); }, }); runtimes.push(runtime); expect(runtime.status()).toMatchObject({ active: true, rpcState: "connected", sessionPath: "C:/sessions/ui-branch.jsonl", }); expect(runtime.widgetLines().join("\n")).toContain("rpc: connected"); }); it("forwards browser prompts to rpc and publishes stream events", async () => { const sent: unknown[] = []; const runtime = await createUiRuntime({ sessionLabel: "ui: test", address: "127.0.0.1", cwd: "C:/repo", openBrowser: false, sessionManager: fakeSessionManager(), startRpcBridge: ({ sessionPath }) => fakeRpc(sent, sessionPath), }); runtimes.push(runtime); const url = new URL(runtime.status().url ?? ""); const socketUrl = new URL("/ws", url); socketUrl.protocol = "ws:"; socketUrl.searchParams.set("token", url.searchParams.get("token") ?? ""); const socket = new WebSocket(socketUrl); await new Promise((resolve) => socket.addEventListener("open", resolve, { once: true }), ); socket.send(JSON.stringify({ type: "prompt", text: "hello" })); await new Promise((resolve) => setTimeout(resolve, 20)); socket.close(); expect(sent).toContainEqual( expect.objectContaining({ type: "prompt", message: "hello" }), ); }); it("routes bash and browser control actions to rpc", async () => { const sent: unknown[] = []; const runtime = await createUiRuntime({ sessionLabel: "ui: test", address: "127.0.0.1", cwd: "C:/repo", openBrowser: false, sessionManager: fakeSessionManager(), startRpcBridge: ({ sessionPath }) => fakeRpc(sent, sessionPath), }); runtimes.push(runtime); const url = new URL(runtime.status().url ?? ""); const socketUrl = new URL("/ws", url); socketUrl.protocol = "ws:"; socketUrl.searchParams.set("token", url.searchParams.get("token") ?? ""); const socket = new WebSocket(socketUrl); await new Promise((resolve) => socket.addEventListener("open", resolve, { once: true }), ); socket.send(JSON.stringify({ type: "prompt", text: "!!git status" })); socket.send(JSON.stringify({ type: "cycle_model" })); socket.send(JSON.stringify({ type: "abort" })); await new Promise((resolve) => setTimeout(resolve, 20)); socket.close(); expect(sent).toContainEqual({ type: "bash", command: "git status", excludeFromContext: true, }); expect(sent).toContainEqual({ type: "cycle_model" }); expect(sent).toContainEqual({ type: "abort" }); }); it("attaches telemetry digest once to browser prompt", async () => { const sent: unknown[] = []; const runtime = await createUiRuntime({ sessionLabel: "ui: test", address: "127.0.0.1", cwd: "C:/repo", openBrowser: false, sessionManager: fakeSessionManager(), startRpcBridge: ({ sessionPath }) => fakeRpc(sent, sessionPath), }); runtimes.push(runtime); const url = new URL(runtime.status().url ?? ""); const socketUrl = new URL("/ws", url); socketUrl.protocol = "ws:"; socketUrl.searchParams.set("token", url.searchParams.get("token") ?? ""); const socket = new WebSocket(socketUrl); await new Promise((resolve) => socket.addEventListener("open", resolve, { once: true }), ); socket.send( JSON.stringify({ type: "telemetry_batch", events: [{ type: "click", target: "SEND" }], }), ); socket.send(JSON.stringify({ type: "prompt", text: "hello" })); socket.send(JSON.stringify({ type: "prompt", text: "again" })); await new Promise((resolve) => setTimeout(resolve, 20)); socket.close(); expect(sent).toContainEqual({ type: "prompt", message: "Browser digest:\n- click SEND\n\nUser prompt:\nhello", }); expect(sent).toContainEqual({ type: "prompt", message: "again" }); }); it("routes extension UI responses to rpc", async () => { const sent: unknown[] = []; const runtime = await createUiRuntime({ sessionLabel: "ui: test", address: "127.0.0.1", cwd: "C:/repo", openBrowser: false, sessionManager: fakeSessionManager(), startRpcBridge: ({ sessionPath }) => fakeRpc(sent, sessionPath), }); runtimes.push(runtime); const url = new URL(runtime.status().url ?? ""); const socketUrl = new URL("/ws", url); socketUrl.protocol = "ws:"; socketUrl.searchParams.set("token", url.searchParams.get("token") ?? ""); const socket = new WebSocket(socketUrl); await new Promise((resolve) => socket.addEventListener("open", resolve, { once: true }), ); socket.send( JSON.stringify({ type: "ui_response", id: "u1", value: "Allow" }), ); await new Promise((resolve) => setTimeout(resolve, 20)); socket.close(); expect(sent).toContainEqual({ type: "extension_ui_response", id: "u1", value: "Allow", }); }); }); function fakeSessionManager() { return { getLeafId: () => "leaf-1", getSessionFile: () => "C:/sessions/source.jsonl", createBranchedSession: () => "C:/sessions/ui-branch.jsonl", }; } function fakeRpc(sent: unknown[], sessionPath: string) { return { child: Object.assign(new EventEmitter(), { stdout: new EventEmitter(), stderr: new EventEmitter(), stdin: new Writable({ write(_chunk, _encoding, callback) { callback(); }, }), kill() {}, }), sessionPath, send(request: unknown) { sent.push(request); return Promise.resolve({ type: "response", success: true, data: {} }); }, stderr: () => "", close: () => Promise.resolve(), }; }