test(mcp): migrate headers tests to Effect runner (#27237)
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { test, expect, mock, beforeEach } from "bun:test"
|
import { describe, expect, mock, beforeEach } from "bun:test"
|
||||||
import { Effect } from "effect"
|
import { Effect } from "effect"
|
||||||
import type { MCP as MCPNS } from "../../src/mcp/index"
|
import { testEffect } from "../lib/effect"
|
||||||
|
|
||||||
// Track what options were passed to each transport constructor
|
// Track what options were passed to each transport constructor
|
||||||
const transportCalls: Array<{
|
const transportCalls: Array<{
|
||||||
@@ -46,53 +46,22 @@ beforeEach(() => {
|
|||||||
|
|
||||||
// Import MCP after mocking
|
// Import MCP after mocking
|
||||||
const { MCP } = await import("../../src/mcp/index")
|
const { MCP } = await import("../../src/mcp/index")
|
||||||
const { AppRuntime } = await import("../../src/effect/app-runtime")
|
const it = testEffect(MCP.defaultLayer)
|
||||||
const { Instance } = await import("../../src/project/instance")
|
|
||||||
const { WithInstance } = await import("../../src/project/with-instance")
|
|
||||||
const { tmpdir } = await import("../fixture/fixture")
|
|
||||||
const service = MCP.Service as unknown as Effect.Effect<MCPNS.Interface, never, never>
|
|
||||||
|
|
||||||
test("headers are passed to transports when oauth is enabled (default)", async () => {
|
describe("mcp.headers", () => {
|
||||||
await using tmp = await tmpdir({
|
it.instance("headers are passed to transports when oauth is enabled (default)", () =>
|
||||||
init: async (dir) => {
|
Effect.gen(function* () {
|
||||||
await Bun.write(
|
const mcp = yield* MCP.Service
|
||||||
`${dir}/opencode.json`,
|
yield* mcp
|
||||||
JSON.stringify({
|
.add("test-server", {
|
||||||
$schema: "https://opencode.ai/config.json",
|
type: "remote",
|
||||||
mcp: {
|
url: "https://example.com/mcp",
|
||||||
"test-server": {
|
headers: {
|
||||||
type: "remote",
|
Authorization: "Bearer test-token",
|
||||||
url: "https://example.com/mcp",
|
"X-Custom-Header": "custom-value",
|
||||||
headers: {
|
|
||||||
Authorization: "Bearer test-token",
|
|
||||||
"X-Custom-Header": "custom-value",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}),
|
})
|
||||||
)
|
.pipe(Effect.catch(() => Effect.void))
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
await WithInstance.provide({
|
|
||||||
directory: tmp.path,
|
|
||||||
fn: async () => {
|
|
||||||
// Trigger MCP initialization - it will fail to connect but we can check the transport options
|
|
||||||
await AppRuntime.runPromise(
|
|
||||||
Effect.gen(function* () {
|
|
||||||
const mcp = yield* service
|
|
||||||
yield* mcp
|
|
||||||
.add("test-server", {
|
|
||||||
type: "remote",
|
|
||||||
url: "https://example.com/mcp",
|
|
||||||
headers: {
|
|
||||||
Authorization: "Bearer test-token",
|
|
||||||
"X-Custom-Header": "custom-value",
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.pipe(Effect.catch(() => Effect.void))
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
|
|
||||||
// Both transports should have been created with headers
|
// Both transports should have been created with headers
|
||||||
expect(transportCalls.length).toBeGreaterThanOrEqual(1)
|
expect(transportCalls.length).toBeGreaterThanOrEqual(1)
|
||||||
@@ -106,33 +75,22 @@ test("headers are passed to transports when oauth is enabled (default)", async (
|
|||||||
// OAuth should be enabled by default, so authProvider should exist
|
// OAuth should be enabled by default, so authProvider should exist
|
||||||
expect(call.options.authProvider).toBeDefined()
|
expect(call.options.authProvider).toBeDefined()
|
||||||
}
|
}
|
||||||
},
|
}),
|
||||||
})
|
)
|
||||||
})
|
|
||||||
|
|
||||||
test("headers are passed to transports when oauth is explicitly disabled", async () => {
|
it.instance("headers are passed to transports when oauth is explicitly disabled", () =>
|
||||||
await using tmp = await tmpdir()
|
Effect.gen(function* () {
|
||||||
|
const mcp = yield* MCP.Service
|
||||||
await WithInstance.provide({
|
yield* mcp
|
||||||
directory: tmp.path,
|
.add("test-server-no-oauth", {
|
||||||
fn: async () => {
|
type: "remote",
|
||||||
transportCalls.length = 0
|
url: "https://example.com/mcp",
|
||||||
|
oauth: false,
|
||||||
await AppRuntime.runPromise(
|
headers: {
|
||||||
Effect.gen(function* () {
|
Authorization: "Bearer test-token",
|
||||||
const mcp = yield* service
|
},
|
||||||
yield* mcp
|
})
|
||||||
.add("test-server-no-oauth", {
|
.pipe(Effect.catch(() => Effect.void))
|
||||||
type: "remote",
|
|
||||||
url: "https://example.com/mcp",
|
|
||||||
oauth: false,
|
|
||||||
headers: {
|
|
||||||
Authorization: "Bearer test-token",
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.pipe(Effect.catch(() => Effect.void))
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
|
|
||||||
expect(transportCalls.length).toBeGreaterThanOrEqual(1)
|
expect(transportCalls.length).toBeGreaterThanOrEqual(1)
|
||||||
|
|
||||||
@@ -144,29 +102,18 @@ test("headers are passed to transports when oauth is explicitly disabled", async
|
|||||||
// OAuth is disabled, so no authProvider
|
// OAuth is disabled, so no authProvider
|
||||||
expect(call.options.authProvider).toBeUndefined()
|
expect(call.options.authProvider).toBeUndefined()
|
||||||
}
|
}
|
||||||
},
|
}),
|
||||||
})
|
)
|
||||||
})
|
|
||||||
|
|
||||||
test("no requestInit when headers are not provided", async () => {
|
it.instance("no requestInit when headers are not provided", () =>
|
||||||
await using tmp = await tmpdir()
|
Effect.gen(function* () {
|
||||||
|
const mcp = yield* MCP.Service
|
||||||
await WithInstance.provide({
|
yield* mcp
|
||||||
directory: tmp.path,
|
.add("test-server-no-headers", {
|
||||||
fn: async () => {
|
type: "remote",
|
||||||
transportCalls.length = 0
|
url: "https://example.com/mcp",
|
||||||
|
})
|
||||||
await AppRuntime.runPromise(
|
.pipe(Effect.catch(() => Effect.void))
|
||||||
Effect.gen(function* () {
|
|
||||||
const mcp = yield* service
|
|
||||||
yield* mcp
|
|
||||||
.add("test-server-no-headers", {
|
|
||||||
type: "remote",
|
|
||||||
url: "https://example.com/mcp",
|
|
||||||
})
|
|
||||||
.pipe(Effect.catch(() => Effect.void))
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
|
|
||||||
expect(transportCalls.length).toBeGreaterThanOrEqual(1)
|
expect(transportCalls.length).toBeGreaterThanOrEqual(1)
|
||||||
|
|
||||||
@@ -174,6 +121,6 @@ test("no requestInit when headers are not provided", async () => {
|
|||||||
// No headers means requestInit should be undefined
|
// No headers means requestInit should be undefined
|
||||||
expect(call.options.requestInit).toBeUndefined()
|
expect(call.options.requestInit).toBeUndefined()
|
||||||
}
|
}
|
||||||
},
|
}),
|
||||||
})
|
)
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user