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

McpSessionRegistry.test.ts

165 lines6.2 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(["preview", "pull-requests"]);
83 expect(yield* capabilitiesOf(withoutPreview)).toEqual(["pull-requests"]);
84 expect(yield* capabilitiesOf(withDevice)).toEqual(["device", "pull-requests"]);
85 }),
86);
87​
88it.effect("builds MCP endpoints from the bound server host", () =>
89 Effect.gen(function* () {
90 const cases = [
91 ["100.64.0.40", "http://100.64.0.40:43123/mcp"],
92 ["0.0.0.0", "http://127.0.0.1:43123/mcp"],
93 ["::", "http://127.0.0.1:43123/mcp"],
94 ["::1", "http://[::1]:43123/mcp"],
95 ["127.0.0.1", "http://127.0.0.1:43123/mcp"],
96 ] as const;
97​
98 for (const [hostname, expectedEndpoint] of cases) {
99 const registry = yield* makeRegistry(() => 1_000, makeFakeHttpServer(hostname));
100 const issued = yield* registry.issue({
101 threadId: ThreadId.make(`thread-${hostname}`),
102 providerInstanceId: ProviderInstanceId.make("codex"),
103 capabilities: new Set(["preview"]),
104 });
105 expect(issued.config.endpoint).toBe(expectedEndpoint);
106 }
107 }),
108);
109​
110it.effect("expires credentials once their session stops showing signs of life", () =>
111 Effect.gen(function* () {
112 let timestamp = 1_000;
113 const registry = yield* makeRegistry(() => timestamp);
114 const issued = yield* registry.issue({
115 threadId: ThreadId.make("thread-2"),
116 providerInstanceId: ProviderInstanceId.make("claude"),
117 capabilities: new Set(["preview"]),
118 });
119 const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");
120 timestamp += 101;
121 expect(yield* registry.resolve(token)).toBeUndefined();
122 }),
123);
124​
125it.effect("keeps a credential alive across turns that never touch an MCP tool", () =>
126 Effect.gen(function* () {
127 let timestamp = 1_000;
128 const registry = yield* makeRegistry(() => timestamp);
129 const threadId = ThreadId.make("thread-3");
130 const issued = yield* registry.issue({
131 threadId,
132 providerInstanceId: ProviderInstanceId.make("claude"),
133 capabilities: new Set(["preview"]),
134 });
135 const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");
136​
137 // Well past the liveness window in total, but each turn reports in before
138 // it lapses — this is the long-session case that used to lose the toolkit.
139 for (let turn = 0; turn < 10; turn += 1) {
140 timestamp += 99;
141 yield* registry.touch(threadId);
142 }
143​
144 expect((yield* registry.resolve(token))?.threadId).toBe(threadId);
145 }),
146);
147​
148it.effect("does not keep credentials of other threads alive", () =>
149 Effect.gen(function* () {
150 let timestamp = 1_000;
151 const registry = yield* makeRegistry(() => timestamp);
152 const issued = yield* registry.issue({
153 threadId: ThreadId.make("thread-4"),
154 providerInstanceId: ProviderInstanceId.make("codex"),
155 capabilities: new Set(["preview"]),
156 });
157 const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");
158​
159 timestamp += 99;
160 yield* registry.touch(ThreadId.make("thread-unrelated"));
161 timestamp += 2;
162​
163 expect(yield* registry.resolve(token)).toBeUndefined();
164 }),
165);