forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpapi-mcp.test.ts
More file actions
195 lines (177 loc) · 5.66 KB
/
httpapi-mcp.test.ts
File metadata and controls
195 lines (177 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { describe, expect } from "bun:test"
import { Context, Effect, Layer } from "effect"
import { HttpApiApp } from "../../src/server/routes/instance/httpapi/server"
import { McpPaths } from "../../src/server/routes/instance/httpapi/groups/mcp"
import { Server } from "../../src/server/server"
import * as Log from "@opencode-ai/core/util/log"
import { resetDatabase } from "../fixture/db"
import { TestInstance } from "../fixture/fixture"
import { testEffect } from "../lib/effect"
void Log.init({ print: false })
const context = Context.empty() as Context.Context<unknown>
const testStateLayer = Layer.effectDiscard(
Effect.gen(function* () {
yield* Effect.promise(() => resetDatabase())
yield* Effect.addFinalizer(() => Effect.promise(() => resetDatabase()).pipe(Effect.ignore))
}),
)
const it = testEffect(testStateLayer)
function app() {
return Server.Default().app
}
type TestApp = ReturnType<typeof app>
type TestHandler = ReturnType<typeof HttpApiApp.webHandler>
const handlerScoped = Effect.acquireRelease(
Effect.sync(() => HttpApiApp.webHandler()),
(handler) => Effect.promise(() => handler.dispose()).pipe(Effect.ignore),
)
const request = Effect.fnUntraced(function* (
handler: TestHandler,
route: string,
directory: string,
init?: RequestInit,
) {
const headers = new Headers(init?.headers)
headers.set("x-opencode-directory", directory)
return yield* Effect.promise(() =>
Promise.resolve(
handler.handler(
new Request(`http://localhost${route}`, {
...init,
headers,
}),
context,
),
),
)
})
const json = <A>(response: Response) => Effect.promise(() => response.json() as Promise<A>)
const readResponse = Effect.fnUntraced(function* (input: { app: TestApp; path: string; headers: HeadersInit }) {
const response = yield* Effect.promise(() =>
Promise.resolve(input.app.request(input.path, { method: "POST", headers: input.headers })),
)
return {
status: response.status,
body: yield* Effect.promise(() => response.text()),
}
})
describe("mcp HttpApi", () => {
it.instance(
"serves status endpoint",
() =>
Effect.gen(function* () {
const tmp = yield* TestInstance
const handler = yield* handlerScoped
const response = yield* request(handler, McpPaths.status, tmp.directory)
expect(response.status).toBe(200)
expect(yield* json(response)).toEqual({ demo: { status: "disabled" } })
}),
{
config: {
mcp: {
demo: {
type: "local",
command: ["echo", "demo"],
enabled: false,
},
},
},
},
)
it.instance(
"serves add, connect, and disconnect endpoints",
() =>
Effect.gen(function* () {
const tmp = yield* TestInstance
const handler = yield* handlerScoped
const added = yield* request(handler, McpPaths.status, tmp.directory, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
name: "added",
config: {
type: "local",
command: ["echo", "added"],
enabled: false,
},
}),
})
expect(added.status).toBe(200)
expect(yield* json(added)).toMatchObject({ added: { status: "disabled" } })
const connected = yield* request(handler, "/mcp/demo/connect", tmp.directory, { method: "POST" })
expect(connected.status).toBe(200)
expect(yield* json(connected)).toBe(true)
const disconnected = yield* request(handler, "/mcp/demo/disconnect", tmp.directory, { method: "POST" })
expect(disconnected.status).toBe(200)
expect(yield* json(disconnected)).toBe(true)
}),
{
config: {
mcp: {
demo: {
type: "local",
command: ["echo", "demo"],
enabled: false,
},
},
},
},
)
it.instance(
"serves deterministic OAuth endpoints",
() =>
Effect.gen(function* () {
const tmp = yield* TestInstance
const handler = yield* handlerScoped
const start = yield* request(handler, "/mcp/demo/auth", tmp.directory, { method: "POST" })
expect(start.status).toBe(400)
const authenticate = yield* request(handler, "/mcp/demo/auth/authenticate", tmp.directory, { method: "POST" })
expect(authenticate.status).toBe(400)
const removed = yield* request(handler, "/mcp/demo/auth", tmp.directory, { method: "DELETE" })
expect(removed.status).toBe(200)
expect(yield* json(removed)).toEqual({ success: true })
}),
{
config: {
mcp: {
demo: {
type: "local",
command: ["echo", "demo"],
enabled: false,
},
},
},
},
)
it.instance(
"returns unsupported OAuth error responses",
() =>
Effect.gen(function* () {
const tmp = yield* TestInstance
const dir = tmp.directory
const headers = { "x-opencode-directory": dir }
yield* Effect.forEach(["/mcp/demo/auth", "/mcp/demo/auth/authenticate"], (path) =>
Effect.gen(function* () {
const response = yield* readResponse({ app: app(), path, headers })
expect(response).toEqual({
status: 400,
body: JSON.stringify({ error: "MCP server demo does not support OAuth" }),
})
}),
)
}),
{
config: {
formatter: false,
lsp: false,
mcp: {
demo: {
type: "local",
command: ["echo", "demo"],
enabled: false,
},
},
},
},
)
})