+21

![opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)



![opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)



James Long
Brendan Allan
Kit Langton
opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>
Affan Ali
affanali2k3
Frank
opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com>
𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴
Aiden Cline
Jay V
Dax Raad
Aarav Sareen
OpeOginni
Luke Parker
Ben Guthrie
Dax
Filip
Max Anderson
Brendan Allan
Jack
Shoubhit Dash
Dustin Deus
starptech
Aiden Cline
usrnk1
Jay
runvip
opencode
Julian Coy
Vladimir Glafirov
8c94e9005f
Co-authored-by: Brendan Allan <14191578+Brendonovich@users.noreply.github.com> Co-authored-by: Kit Langton <kit.langton@gmail.com> Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: Affan Ali <93028901+affanali2k3@users.noreply.github.com> Co-authored-by: affanali2k3 <affanalikhanxx@gmail.com> Co-authored-by: Frank <frank@anoma.ly> Co-authored-by: opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com> Co-authored-by: 𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴 <little-frank@opencord.local> Co-authored-by: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Co-authored-by: Jay V <air@live.ca> Co-authored-by: Dax Raad <d@ironbay.co> Co-authored-by: Aarav Sareen <96787824+arvsrn@users.noreply.github.com> Co-authored-by: OpeOginni <107570612+OpeOginni@users.noreply.github.com> Co-authored-by: Luke Parker <10430890+Hona@users.noreply.github.com> Co-authored-by: Ben Guthrie <benjee.012@gmail.com> Co-authored-by: Dax <mail@thdxr.com> Co-authored-by: Filip <34747899+neriousy@users.noreply.github.com> Co-authored-by: Max Anderson <max.a.anderson95@gmail.com> Co-authored-by: Brendan Allan <git@brendonovich.dev> Co-authored-by: Jack <jack@anoma.ly> Co-authored-by: Shoubhit Dash <shoubhit2005@gmail.com> Co-authored-by: Dustin Deus <deusdustin@gmail.com> Co-authored-by: starptech <starptech@starptechs-MBP.fritz.box> Co-authored-by: Aiden Cline <aidenpcline@gmail.com> Co-authored-by: usrnk1 <7547651+usrnk1@users.noreply.github.com> Co-authored-by: Jay <53023+jayair@users.noreply.github.com> Co-authored-by: runvip <164729189+runvip@users.noreply.github.com> Co-authored-by: opencode <opencode@sst.dev> Co-authored-by: Julian Coy <julian@ex-machina.co> Co-authored-by: Vladimir Glafirov <vglafirov@gitlab.com>
244 lines
7.9 KiB
TypeScript
244 lines
7.9 KiB
TypeScript
import { expect, mock, beforeEach } from "bun:test"
|
|
import { EventEmitter } from "events"
|
|
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
|
|
import { Deferred, Effect, Layer, Option } from "effect"
|
|
import { awaitWithTimeout, testEffect } from "../lib/effect"
|
|
import type { MCP as MCPNS } from "../../src/mcp/index"
|
|
|
|
// Track open() calls and control failure behavior
|
|
let openShouldFail = false
|
|
let openCalledWith: string | undefined
|
|
let openDeferred: Deferred.Deferred<string> | undefined
|
|
|
|
void mock.module("open", () => ({
|
|
default: async (url: string) => {
|
|
openCalledWith = url
|
|
if (openDeferred) Effect.runSync(Deferred.succeed(openDeferred, url).pipe(Effect.ignore))
|
|
|
|
// Return a mock subprocess that emits an error if openShouldFail is true
|
|
const subprocess = new EventEmitter()
|
|
if (openShouldFail) {
|
|
// Emit error asynchronously like a real subprocess would
|
|
setTimeout(() => {
|
|
subprocess.emit("error", new Error("spawn xdg-open ENOENT"))
|
|
}, 10)
|
|
}
|
|
return subprocess
|
|
},
|
|
}))
|
|
|
|
// Mock UnauthorizedError
|
|
class MockUnauthorizedError extends Error {
|
|
constructor() {
|
|
super("Unauthorized")
|
|
this.name = "UnauthorizedError"
|
|
}
|
|
}
|
|
|
|
// Track what options were passed to each transport constructor
|
|
const transportCalls: Array<{
|
|
type: "streamable" | "sse"
|
|
url: string
|
|
options: { authProvider?: unknown; requestInit?: RequestInit }
|
|
}> = []
|
|
|
|
// Mock the transport constructors
|
|
void mock.module("@modelcontextprotocol/sdk/client/streamableHttp.js", () => ({
|
|
StreamableHTTPClientTransport: class MockStreamableHTTP {
|
|
url: string
|
|
authProvider: { redirectToAuthorization?: (url: URL) => Promise<void> } | undefined
|
|
constructor(
|
|
url: URL,
|
|
options?: { authProvider?: { redirectToAuthorization?: (url: URL) => Promise<void> }; requestInit?: RequestInit },
|
|
) {
|
|
this.url = url.toString()
|
|
this.authProvider = options?.authProvider
|
|
transportCalls.push({
|
|
type: "streamable",
|
|
url: url.toString(),
|
|
options: options ?? {},
|
|
})
|
|
}
|
|
async start() {
|
|
// Simulate OAuth redirect by calling the authProvider's redirectToAuthorization
|
|
if (this.authProvider?.redirectToAuthorization) {
|
|
await this.authProvider.redirectToAuthorization(new URL("https://auth.example.com/authorize?client_id=test"))
|
|
}
|
|
throw new MockUnauthorizedError()
|
|
}
|
|
async finishAuth(_code: string) {
|
|
// Mock successful auth completion
|
|
}
|
|
},
|
|
}))
|
|
|
|
void mock.module("@modelcontextprotocol/sdk/client/sse.js", () => ({
|
|
SSEClientTransport: class MockSSE {
|
|
constructor(url: URL) {
|
|
transportCalls.push({
|
|
type: "sse",
|
|
url: url.toString(),
|
|
options: {},
|
|
})
|
|
}
|
|
async start() {
|
|
throw new Error("Mock SSE transport cannot connect")
|
|
}
|
|
},
|
|
}))
|
|
|
|
// Mock the MCP SDK Client to trigger OAuth flow
|
|
void mock.module("@modelcontextprotocol/sdk/client/index.js", () => ({
|
|
Client: class MockClient {
|
|
setRequestHandler() {}
|
|
|
|
async connect(transport: { start: () => Promise<void> }) {
|
|
await transport.start()
|
|
}
|
|
|
|
getServerCapabilities() {
|
|
return { tools: {} }
|
|
}
|
|
},
|
|
}))
|
|
|
|
// Mock UnauthorizedError in the auth module
|
|
void mock.module("@modelcontextprotocol/sdk/client/auth.js", () => ({
|
|
UnauthorizedError: MockUnauthorizedError,
|
|
}))
|
|
|
|
beforeEach(() => {
|
|
openShouldFail = false
|
|
openCalledWith = undefined
|
|
openDeferred = undefined
|
|
transportCalls.length = 0
|
|
})
|
|
|
|
// Import modules after mocking
|
|
const { MCP } = await import("../../src/mcp/index")
|
|
const { EventV2Bridge } = await import("../../src/event-v2-bridge")
|
|
const { Config } = await import("../../src/config/config")
|
|
const { McpAuth } = await import("../../src/mcp/auth")
|
|
const { McpOAuthCallback } = await import("../../src/mcp/oauth-callback")
|
|
const { FSUtil } = await import("@opencode-ai/core/fs-util")
|
|
const { CrossSpawnSpawner } = await import("@opencode-ai/core/cross-spawn-spawner")
|
|
const mcpTest = testEffect(
|
|
LayerNode.compile(
|
|
LayerNode.group([MCP.node, McpAuth.node, EventV2Bridge.node, Config.node, CrossSpawnSpawner.node, FSUtil.node]),
|
|
),
|
|
)
|
|
const service = MCP.Service as unknown as Effect.Effect<MCPNS.Interface, never, never>
|
|
|
|
const config = (name: string, headers?: Record<string, string>) => ({
|
|
mcp: {
|
|
[name]: {
|
|
type: "remote" as const,
|
|
url: "https://example.com/mcp",
|
|
headers,
|
|
},
|
|
},
|
|
})
|
|
|
|
const withCallbackStop = Effect.addFinalizer(() => Effect.promise(() => McpOAuthCallback.stop()).pipe(Effect.ignore))
|
|
|
|
const trackBrowserOpen = Effect.gen(function* () {
|
|
const opened = yield* Deferred.make<string>()
|
|
openDeferred = opened
|
|
yield* Effect.addFinalizer(() => Effect.sync(() => (openDeferred = undefined)))
|
|
return opened
|
|
})
|
|
|
|
const trackBrowserOpenFailed = Effect.gen(function* () {
|
|
const events = yield* EventV2Bridge.Service
|
|
const event = yield* Deferred.make<{ mcpName: string; url: string }>()
|
|
const unsubscribe = yield* events.listen((evt) => {
|
|
if (evt.type === MCP.BrowserOpenFailed.type)
|
|
Deferred.doneUnsafe(event, Effect.succeed(evt.data as { mcpName: string; url: string }))
|
|
return Effect.void
|
|
})
|
|
yield* Effect.addFinalizer(() => unsubscribe)
|
|
return event
|
|
})
|
|
|
|
const authenticateScoped = (name: string, onAuthorization?: (authorizationUrl: string) => void) =>
|
|
Effect.gen(function* () {
|
|
const mcp = yield* service
|
|
yield* mcp.authenticate(name, onAuthorization).pipe(
|
|
Effect.ignore,
|
|
Effect.catchCause(() => Effect.void),
|
|
Effect.forkScoped,
|
|
)
|
|
})
|
|
|
|
mcpTest.instance(
|
|
"BrowserOpenFailed event is published when open() throws",
|
|
() =>
|
|
Effect.gen(function* () {
|
|
yield* withCallbackStop
|
|
openShouldFail = true
|
|
|
|
const event = yield* trackBrowserOpenFailed
|
|
yield* authenticateScoped("test-oauth-server")
|
|
|
|
const failure = yield* awaitWithTimeout(
|
|
Deferred.await(event),
|
|
"Timed out waiting for BrowserOpenFailed event",
|
|
"5 seconds",
|
|
)
|
|
|
|
expect(failure.mcpName).toBe("test-oauth-server")
|
|
expect(failure.url).toContain("https://")
|
|
}),
|
|
{ config: config("test-oauth-server") },
|
|
)
|
|
|
|
mcpTest.instance(
|
|
"BrowserOpenFailed event is NOT published when open() succeeds",
|
|
() =>
|
|
Effect.gen(function* () {
|
|
yield* withCallbackStop
|
|
openShouldFail = false
|
|
|
|
const opened = yield* trackBrowserOpen
|
|
const event = yield* trackBrowserOpenFailed
|
|
yield* authenticateScoped("test-oauth-server-2")
|
|
|
|
yield* awaitWithTimeout(Deferred.await(opened), "Timed out waiting for open()", "5 seconds")
|
|
const failure = yield* Deferred.await(event).pipe(Effect.timeoutOption("700 millis"))
|
|
|
|
expect(failure).toEqual(Option.none())
|
|
expect(openCalledWith).toBeDefined()
|
|
}),
|
|
{ config: config("test-oauth-server-2") },
|
|
)
|
|
|
|
mcpTest.instance(
|
|
"open() is called with the authorization URL",
|
|
() =>
|
|
Effect.gen(function* () {
|
|
yield* withCallbackStop
|
|
openShouldFail = false
|
|
openCalledWith = undefined
|
|
|
|
const opened = yield* trackBrowserOpen
|
|
const event = yield* trackBrowserOpenFailed
|
|
const authorization = yield* Deferred.make<string>()
|
|
yield* authenticateScoped("test-oauth-server-3", (url) => Deferred.doneUnsafe(authorization, Effect.succeed(url)))
|
|
|
|
const url = yield* awaitWithTimeout(Deferred.await(opened), "Timed out waiting for open()", "5 seconds")
|
|
const authorizationUrl = yield* awaitWithTimeout(
|
|
Deferred.await(authorization),
|
|
"Timed out waiting for authorization URL",
|
|
"5 seconds",
|
|
)
|
|
const failure = yield* Deferred.await(event).pipe(Effect.timeoutOption("700 millis"))
|
|
|
|
expect(failure).toEqual(Option.none())
|
|
expect(authorizationUrl).toBe(url)
|
|
expect(typeof url).toBe("string")
|
|
expect(url).toContain("https://")
|
|
expect(transportCalls.at(-1)?.options.requestInit?.headers).toEqual({ "X-Custom-Header": "custom-value" })
|
|
}),
|
|
{ config: config("test-oauth-server-3", { "X-Custom-Header": "custom-value" }) },
|
|
)
|