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

McpSessionRegistry.test.ts

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