-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathhandlers.test.ts
More file actions
160 lines (145 loc) · 6.19 KB
/
Copy pathhandlers.test.ts
File metadata and controls
160 lines (145 loc) · 6.19 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
// ---------------------------------------------------------------------------
// Handler-level integration test for the MCP group.
//
// Verifies the layer wiring stays coherent end-to-end: the handlers
// pull the wrapped extension from the service, and any un-caught cause
// lands in the observability middleware — producing a 500 whose body is
// the opaque `InternalError` schema (no internal leakage).
// ---------------------------------------------------------------------------
import { HttpApiBuilder } from "effect/unstable/httpapi";
import { HttpRouter, HttpServer } from "effect/unstable/http";
import { describe, expect, it } from "@effect/vitest";
import { Effect, Layer, Schema } from "effect";
import { addGroup, observabilityMiddleware } from "@executor-js/api";
import { CoreHandlers, ExecutionEngineService, ExecutorService } from "@executor-js/api/server";
import { IntegrationNotFoundError, IntegrationSlug } from "@executor-js/sdk/shared";
import type { McpPluginExtension } from "../sdk/plugin";
import { McpConnectionError } from "../sdk/errors";
import { McpExtensionService, McpHandlers } from "./handlers";
import { McpGroup } from "./group";
const unused = Effect.die("unused");
const failingExtension: McpPluginExtension = {
// oxlint-disable-next-line executor/no-error-constructor -- boundary: test injects a defect to verify opaque handler error responses
probeEndpoint: () => Effect.die(new Error("Not implemented")),
addServer: () => unused,
removeServer: () => unused,
reconcileStdioConnections: () => unused,
getServer: () => Effect.succeed(null),
configureServer: () => unused,
configureAuth: () => unused,
};
const Api = addGroup(McpGroup);
const UnusedExecutor = Layer.succeed(ExecutorService)({} as ExecutorService["Service"]);
const UnusedExecutionEngine = Layer.succeed(ExecutionEngineService)(
{} as ExecutionEngineService["Service"],
);
const webHandlerFor = (extension: McpPluginExtension) =>
Effect.acquireRelease(
Effect.sync(() =>
HttpRouter.toWebHandler(
HttpApiBuilder.layer(Api).pipe(
Layer.provide(CoreHandlers),
Layer.provide(McpHandlers),
Layer.provide(observabilityMiddleware(Api)),
Layer.provide(UnusedExecutor),
Layer.provide(UnusedExecutionEngine),
Layer.provide(Layer.succeed(McpExtensionService, extension)),
Layer.provideMerge(HttpServer.layerServices),
Layer.provideMerge(Layer.succeed(HttpRouter.RouterConfig)({ maxParamLength: 1000 })),
),
),
),
(web) => Effect.promise(() => web.dispose()),
);
// `acquireRelease` keeps disposal inside the Effect scope — no
// try/finally, no per-test cleanup plumbing. `it.scoped` closes the
// scope for us. Each `Layer.provide` satisfies a piece of the api
// builder's dependency graph; `provideMerge` at the bottom keeps
// framework services available to the router itself.
const WebHandler = webHandlerFor(failingExtension);
const McpConnectionErrorResponse = Schema.Struct({
_tag: Schema.Literal("McpConnectionError"),
message: Schema.String,
});
const IntegrationNotFoundErrorResponse = Schema.Struct({
_tag: Schema.Literal("IntegrationNotFoundError"),
slug: Schema.String,
});
describe("McpHandlers", () => {
it.effect("defect-returning methods produce an opaque InternalError, no leakage", () =>
Effect.gen(function* () {
const web = yield* WebHandler;
const response = yield* Effect.promise(() =>
(web.handler as (request: Request) => Promise<Response>)(
new Request("http://localhost/mcp/probe", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ endpoint: "https://example.com/mcp" }),
}),
),
);
expect(response.status).toBe(500);
const body = yield* Effect.promise(() => response.text());
expect(body).not.toContain("Not implemented");
}),
);
it.effect("domain MCP connection errors are encoded as 400 responses", () =>
Effect.gen(function* () {
const web = yield* webHandlerFor({
...failingExtension,
probeEndpoint: () =>
Effect.fail(
new McpConnectionError({
transport: "remote",
message:
"Failed to connect to MCP endpoint and no OAuth was detected. Do you need to provide an API key, header, or query parameter?",
}),
),
});
const response = yield* Effect.promise(() =>
(web.handler as (request: Request) => Promise<Response>)(
new Request("http://localhost/mcp/probe", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ endpoint: "https://ui.sh/mcp" }),
}),
),
);
expect(response.status).toBe(400);
const body = yield* Schema.decodeUnknownEffect(McpConnectionErrorResponse)(
yield* Effect.promise(() => response.json()),
);
expect(body.message).toContain("Do you need to provide an API key");
}),
);
it.effect("configureServer IntegrationNotFoundError is encoded as a 404 response", () =>
Effect.gen(function* () {
const slug = IntegrationSlug.make("missing_mcp");
const web = yield* webHandlerFor({
...failingExtension,
configureServer: () => Effect.fail(new IntegrationNotFoundError({ slug })),
});
const response = yield* Effect.promise(() =>
(web.handler as (request: Request) => Promise<Response>)(
new Request("http://localhost/mcp/servers/missing_mcp/config", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
config: {
transport: "remote",
endpoint: "https://example.com/mcp",
remoteTransport: "auto",
authenticationTemplate: [{ slug: "none", kind: "none" }],
},
}),
}),
),
);
expect(response.status).toBe(404);
const body = yield* Schema.decodeUnknownEffect(IntegrationNotFoundErrorResponse)(
yield* Effect.promise(() => response.json()),
);
expect(body.slug).toBe("missing_mcp");
}),
);
});