Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

McpSessionRegistry.test.ts

169 lines6.3 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { expect, it } from "@effect/vitest";
3import { EnvironmentId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";
4import * as Effect from "effect/Effect";
5import { HttpServer } from "effect/unstable/http";
6import * as NetAddress from "effect/unstable/net/NetAddress";
7​
8import * as ServerEnvironment from "../environment/ServerEnvironment.ts";
9import * as McpSessionRegistry from "./McpSessionRegistry.ts";
10​
11const environmentId = EnvironmentId.make("environment-1");
12const makeFakeHttpServer = (hostname: string, port = 43123) =>
13 HttpServer.HttpServer.of({
14 address: NetAddress.inetAddressFromIpStringUnsafe(hostname, port),
15 serve: (() => Effect.void) as HttpServer.HttpServer["Service"]["serve"],
16 });
17const fakeHttpServer = makeFakeHttpServer("127.0.0.1");
18const fakeEnvironment = ServerEnvironment.ServerEnvironment.of({
19 getEnvironmentId: Effect.succeed(environmentId),
20 getDescriptor: Effect.die("unused"),
21});
22​
23const makeRegistry = (now: () => number, httpServer = fakeHttpServer) =>
24 McpSessionRegistry.__testing
25 .make({
26 now,
27 livenessWindowMs: 100,
28 })
29 .pipe(
30 Effect.provideService(HttpServer.HttpServer, httpServer),
31 Effect.provideService(ServerEnvironment.ServerEnvironment, fakeEnvironment),
32 Effect.provide(NodeServices.layer),
33 );
34​
35it.effect("stores only a token hash, resolves the bearer token, and revokes by thread", () =>
36 Effect.gen(function* () {
37 let timestamp = 1_000;
38 const registry = yield* makeRegistry(() => timestamp);
39 const threadId = ThreadId.make("thread-1");
40 const issued = yield* registry.issue({
41 threadId,
42 providerInstanceId: ProviderInstanceId.make("codex"),
43 capabilities: new Set(["preview"]),
44 });
45 expect(issued.config.endpoint).toBe("http://127.0.0.1:43123/mcp");
46 const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");
47 expect(token.length).toBeGreaterThan(20);
48​
49 const resolved = yield* registry.resolve(token);
50 expect(resolved?.threadId).toBe(threadId);
51​
52 yield* registry.revokeThread(threadId);
53 expect(yield* registry.resolve(token)).toBeUndefined();
54​
55 timestamp += 2_000;
56 }),
57);
58​
59it.effect("always grants pull-requests and gates browser and device access independently", () =>
60 Effect.gen(function* () {
61 const registry = yield* makeRegistry(() => 1_000);
62 const withPreview = yield* registry.issue({
63 threadId: ThreadId.make("thread-preview"),
64 providerInstanceId: ProviderInstanceId.make("codex"),
65 capabilities: new Set(["preview"]),
66 });
67 const withoutPreview = yield* registry.issue({
68 threadId: ThreadId.make("thread-no-preview"),
69 providerInstanceId: ProviderInstanceId.make("codex"),
70 capabilities: new Set(),
71 });
72 const withDevice = yield* registry.issue({
73 threadId: ThreadId.make("thread-device"),
74 providerInstanceId: ProviderInstanceId.make("codex"),
75 capabilities: new Set(["device"]),
76 });
77 const capabilitiesOf = (issued: typeof withPreview) =>
78 registry
79 .resolve(issued.config.authorizationHeader.replace(/^Bearer\s+/, ""))
80 .pipe(Effect.map((scope) => [...(scope?.capabilities ?? [])].sort()));
81​
82 expect(yield* capabilitiesOf(withPreview)).toEqual([
83 "orchestration",
84 "preview",
85 "pull-requests",
86 ]);
87 expect(yield* capabilitiesOf(withoutPreview)).toEqual(["orchestration", "pull-requests"]);
88 expect(yield* capabilitiesOf(withDevice)).toEqual(["device", "orchestration", "pull-requests"]);
89 }),
90);
91​
92it.effect("builds MCP endpoints from the bound server host", () =>
93 Effect.gen(function* () {
94 const cases = [
95 ["100.64.0.40", "http://100.64.0.40:43123/mcp"],
96 ["0.0.0.0", "http://127.0.0.1:43123/mcp"],
97 ["::", "http://127.0.0.1:43123/mcp"],
98 ["::1", "http://[::1]:43123/mcp"],
99 ["127.0.0.1", "http://127.0.0.1:43123/mcp"],
100 ] as const;
101​
102 for (const [hostname, expectedEndpoint] of cases) {
103 const registry = yield* makeRegistry(() => 1_000, makeFakeHttpServer(hostname));
104 const issued = yield* registry.issue({
105 threadId: ThreadId.make(`thread-${hostname}`),
106 providerInstanceId: ProviderInstanceId.make("codex"),
107 capabilities: new Set(["preview"]),
108 });
109 expect(issued.config.endpoint).toBe(expectedEndpoint);
110 }
111 }),
112);
113​
114it.effect("expires credentials once their session stops showing signs of life", () =>
115 Effect.gen(function* () {
116 let timestamp = 1_000;
117 const registry = yield* makeRegistry(() => timestamp);
118 const issued = yield* registry.issue({
119 threadId: ThreadId.make("thread-2"),
120 providerInstanceId: ProviderInstanceId.make("claude"),
121 capabilities: new Set(["preview"]),
122 });
123 const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");
124 timestamp += 101;
125 expect(yield* registry.resolve(token)).toBeUndefined();
126 }),
127);
128​
129it.effect("keeps a credential alive across turns that never touch an MCP tool", () =>
130 Effect.gen(function* () {
131 let timestamp = 1_000;
132 const registry = yield* makeRegistry(() => timestamp);
133 const threadId = ThreadId.make("thread-3");
134 const issued = yield* registry.issue({
135 threadId,
136 providerInstanceId: ProviderInstanceId.make("claude"),
137 capabilities: new Set(["preview"]),
138 });
139 const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");
140​
141 // Well past the liveness window in total, but each turn reports in before
142 // it lapses — this is the long-session case that used to lose the toolkit.
143 for (let turn = 0; turn < 10; turn += 1) {
144 timestamp += 99;
145 yield* registry.touch(threadId);
146 }
147​
148 expect((yield* registry.resolve(token))?.threadId).toBe(threadId);
149 }),
150);
151​
152it.effect("does not keep credentials of other threads alive", () =>
153 Effect.gen(function* () {
154 let timestamp = 1_000;
155 const registry = yield* makeRegistry(() => timestamp);
156 const issued = yield* registry.issue({
157 threadId: ThreadId.make("thread-4"),
158 providerInstanceId: ProviderInstanceId.make("codex"),
159 capabilities: new Set(["preview"]),
160 });
161 const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");
162​
163 timestamp += 99;
164 yield* registry.touch(ThreadId.make("thread-unrelated"));
165 timestamp += 2;
166​
167 expect(yield* registry.resolve(token)).toBeUndefined();
168 }),
169);