|
| 1 | +import { expect, mock, test } from "bun:test" |
| 2 | +import { spawn } from "node:child_process" |
| 3 | +import { once } from "node:events" |
| 4 | +import { LayerNode } from "@opencode-ai/core/effect/layer-node" |
| 5 | +import { Effect } from "effect" |
| 6 | + |
| 7 | +await mock.module("@opencode-ai/core/open", () => ({ |
| 8 | + openUrl: async (url: string) => { |
| 9 | + const code = new URL(url).pathname === "/successful" ? 0 : 23 |
| 10 | + const subprocess = spawn(process.execPath, ["-e", `process.exit(${code})`], { stdio: "ignore" }) |
| 11 | + await once(subprocess, "close") |
| 12 | + return subprocess |
| 13 | + }, |
| 14 | +})) |
| 15 | + |
| 16 | +const { McpBrowser } = await import("../../src/mcp/browser") |
| 17 | + |
| 18 | +test("reports a browser launcher that exited before openUrl returned", async () => { |
| 19 | + const error = await Effect.runPromise( |
| 20 | + Effect.gen(function* () { |
| 21 | + const browser = yield* McpBrowser.Service |
| 22 | + return yield* Effect.flip(browser.open("https://example.com/authorize")) |
| 23 | + }).pipe(Effect.provide(LayerNode.compile(McpBrowser.node))), |
| 24 | + ) |
| 25 | + |
| 26 | + expect(error.message).toBe("Browser open failed with exit code 23") |
| 27 | +}) |
| 28 | + |
| 29 | +test("accepts a browser launcher that exited successfully before openUrl returned", async () => { |
| 30 | + await Effect.runPromise( |
| 31 | + Effect.gen(function* () { |
| 32 | + const browser = yield* McpBrowser.Service |
| 33 | + yield* browser.open("https://example.com/successful") |
| 34 | + }).pipe(Effect.provide(LayerNode.compile(McpBrowser.node))), |
| 35 | + ) |
| 36 | +}) |
0 commit comments