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

ProviderInstanceRegistryLive.test.ts

411 lines16.6 KB

1/**
2 * Multi-instance validation slices for `ProviderInstanceRegistryLive`.
3 *
4 * Two axes of the driver/registry refactor are exercised here:
5 *
6 * 1. **Same driver, many instances** — the "multi-instance codex slice"
7 * describe block below configures two independent `codex` instances and
8 * asserts each gets its own closures and identity. This is the
9 * multi-codex capability the refactor exists to unlock.
10 *
11 * 2. **Many drivers, one registry** — the "all drivers slice" describe
12 * block below configures one instance of every shipped driver
13 * (`codex`, `claudeAgent`, `cursor`, `grok`, `opencode`) in a single
14 * `ProviderInstanceConfigMap` and asserts the registry boots them all
15 * without cross-contamination. This proves the driver SPI is uniform
16 * across every provider — any driver plugs into the registry through
17 * the same `ProviderDriver` value contract.
18 *
19 * Every instance in these tests is configured with `enabled: false` so the
20 * provider-status checks short-circuit to pending/disabled snapshots
21 * without trying to spawn real `codex` / `claude` / `agent` / `grok` / `opencode`
22 * binaries. That keeps the assertions focused on registry routing
23 * behaviour rather than the runtime details of each provider.
24 */
25import { describe, expect, it } from "@effect/vitest";
26import * as NodeServices from "@effect/platform-node/NodeServices";
27import {
28 type ClaudeSettings,
29 type CodexSettings,
30 type CursorSettings,
31 type GrokSettings,
32 type OpenCodeSettings,
33 ProviderDriverKind,
34 type ProviderInstanceConfigMap,
35 ProviderInstanceId,
36} from "@t3tools/contracts";
37import * as Effect from "effect/Effect";
38import * as Layer from "effect/Layer";
39import { HttpClient, HttpClientResponse } from "effect/unstable/http";
40​
41import { ServerConfig } from "../../config.ts";
42import { ServerSettingsService } from "../../serverSettings.ts";
43import { ClaudeDriver } from "../Drivers/ClaudeDriver.ts";
44import { CodexDriver } from "../Drivers/CodexDriver.ts";
45import { CursorDriver } from "../Drivers/CursorDriver.ts";
46import { GrokDriver } from "../Drivers/GrokDriver.ts";
47import { OpenCodeDriver } from "../Drivers/OpenCodeDriver.ts";
48import { OpenCodeRuntimeLive } from "../opencodeRuntime.ts";
49import { NoOpProviderEventLoggers, ProviderEventLoggers } from "./ProviderEventLoggers.ts";
50import { makeProviderInstanceRegistry } from "./ProviderInstanceRegistryLive.ts";
51​
52const TestHttpClientLive = Layer.succeed(
53 HttpClient.HttpClient,
54 HttpClient.make((request) =>
55 Effect.succeed(HttpClientResponse.fromWeb(request, Response.json({ version: "0.0.0" }))),
56 ),
57);
58​
59const makeCodexConfig = (overrides: Partial<CodexSettings>): CodexSettings => ({
60 enabled: false,
61 binaryPath: "codex",
62 homePath: "",
63 shadowHomePath: "",
64 launchArgs: "",
65 customModels: [],
66 ...overrides,
67});
68​
69const makeClaudeConfig = (overrides: Partial<ClaudeSettings>): ClaudeSettings => ({
70 enabled: false,
71 binaryPath: "claude",
72 homePath: "",
73 customModels: [],
74 launchArgs: "",
75 ...overrides,
76});
77​
78const makeCursorConfig = (overrides: Partial<CursorSettings>): CursorSettings => ({
79 enabled: false,
80 binaryPath: "cursor-agent",
81 apiEndpoint: "",
82 customModels: [],
83 ...overrides,
84});
85​
86const makeGrokConfig = (overrides: Partial<GrokSettings>): GrokSettings => ({
87 enabled: false,
88 binaryPath: "grok",
89 customModels: [],
90 ...overrides,
91});
92​
93const makeOpenCodeConfig = (overrides: Partial<OpenCodeSettings>): OpenCodeSettings => ({
94 enabled: false,
95 binaryPath: "opencode",
96 serverUrl: "",
97 serverPassword: "",
98 customModels: [],
99 ...overrides,
100});
101​
102describe("ProviderInstanceRegistryLive — multi-instance codex slice", () => {
103 // `ServerConfig.layerTest` needs `FileSystem` to materialize its scratch
104 // directory. `Layer.merge` just unions requirements, so we have to push
105 // `NodeServices.layer` through `Layer.provideMerge` to satisfy that
106 // dependency while still surfacing NodeServices to the test body (the
107 // codex driver's `create` yields `ChildProcessSpawner` directly).
108 const testLayer = ServerConfig.layerTest(process.cwd(), {
109 prefix: "provider-instance-registry-test",
110 }).pipe(
111 Layer.provideMerge(NodeServices.layer),
112 Layer.provideMerge(ServerSettingsService.layerTest()),
113 Layer.provideMerge(TestHttpClientLive),
114 Layer.provideMerge(Layer.succeed(ProviderEventLoggers, NoOpProviderEventLoggers)),
115 );
116​
117 it.live("boots two independent codex instances from a ProviderInstanceConfigMap", () =>
118 Effect.gen(function* () {
119 const personalId = ProviderInstanceId.make("codex_personal");
120 const workId = ProviderInstanceId.make("codex_work");
121 const codexDriverKind = ProviderDriverKind.make("codex");
122​
123 const configMap: ProviderInstanceConfigMap = {
124 [personalId]: {
125 driver: codexDriverKind,
126 displayName: "Codex (personal)",
127 enabled: false,
128 config: makeCodexConfig({
129 binaryPath: "/opt/codex-personal/bin/codex",
130 homePath: "/home/julius/.codex_personal",
131 customModels: ["personal-preview"],
132 }),
133 },
134 [workId]: {
135 driver: codexDriverKind,
136 displayName: "Codex (work)",
137 enabled: false,
138 config: makeCodexConfig({
139 binaryPath: "/opt/codex-work/bin/codex",
140 homePath: "/home/julius/.codex",
141 customModels: ["work-preview"],
142 }),
143 },
144 };
145​
146 const { registry } = yield* makeProviderInstanceRegistry({
147 drivers: [CodexDriver],
148 configMap,
149 });
150​
151 const instances = yield* registry.listInstances;
152 expect(instances.map((instance) => instance.instanceId).toSorted()).toEqual(
153 [personalId, workId].toSorted(),
154 );
155 expect(instances.every((instance) => instance.driverKind === codexDriverKind)).toBe(true);
156 expect(instances.map((instance) => instance.displayName).toSorted()).toEqual(
157 ["Codex (personal)", "Codex (work)"].toSorted(),
158 );
159​
160 // Each instance must be retrievable by id and carry its *own* closures.
161 const personal = yield* registry.getInstance(personalId);
162 const work = yield* registry.getInstance(workId);
163 expect(personal).toBeDefined();
164 expect(work).toBeDefined();
165 expect(personal!.adapter).not.toBe(work!.adapter);
166 expect(personal!.textGeneration).not.toBe(work!.textGeneration);
167 expect(personal!.snapshot).not.toBe(work!.snapshot);
168​
169 // Snapshots identify themselves by instanceId + driver — this is
170 // what makes per-instance routing distinguishable downstream.
171 const personalSnapshot = yield* personal!.snapshot.getSnapshot;
172 expect(personalSnapshot.instanceId).toBe(personalId);
173 expect(personalSnapshot.driver).toBe(codexDriverKind);
174 expect(personalSnapshot.enabled).toBe(false);
175 expect(personalSnapshot.continuation?.groupKey).toBe(
176 "codex:home:/home/julius/.codex_personal",
177 );
178​
179 const workSnapshot = yield* work!.snapshot.getSnapshot;
180 expect(workSnapshot.instanceId).toBe(workId);
181 expect(workSnapshot.driver).toBe(codexDriverKind);
182 expect(workSnapshot.enabled).toBe(false);
183 expect(workSnapshot.continuation?.groupKey).toBe("codex:home:/home/julius/.codex");
184​
185 // Nothing goes to the unavailable bucket — both drivers are registered.
186 const unavailable = yield* registry.listUnavailable;
187 expect(unavailable).toEqual([]);
188 }).pipe(Effect.provide(testLayer)),
189 );
190​
191 it.live(
192 "shadows instances whose driver is not registered in this build without failing boot",
193 () =>
194 Effect.gen(function* () {
195 const codexId = ProviderInstanceId.make("codex_main");
196 const ghostId = ProviderInstanceId.make("ghost_main");
197​
198 const configMap: ProviderInstanceConfigMap = {
199 [codexId]: {
200 driver: ProviderDriverKind.make("codex"),
201 enabled: false,
202 config: makeCodexConfig({}),
203 },
204 [ghostId]: {
205 driver: ProviderDriverKind.make("ghostDriver"),
206 displayName: "A fork-only driver we don't ship",
207 enabled: false,
208 config: { arbitrary: "payload", preserved: true },
209 },
210 };
211​
212 const { registry } = yield* makeProviderInstanceRegistry({
213 drivers: [CodexDriver],
214 configMap,
215 });
216​
217 const instances = yield* registry.listInstances;
218 expect(instances).toHaveLength(1);
219 expect(instances[0]!.instanceId).toBe(codexId);
220​
221 const unavailable = yield* registry.listUnavailable;
222 expect(unavailable).toHaveLength(1);
223 const ghost = unavailable[0]!;
224 expect(ghost.instanceId).toBe(ghostId);
225 expect(ghost.driver).toBe("ghostDriver");
226 expect(ghost.availability).toBe("unavailable");
227 expect(ghost.unavailableReason).toMatch(/ghostDriver/);
228 }).pipe(Effect.provide(testLayer)),
229 );
230});
231​
232describe("ProviderInstanceRegistryLive — all drivers slice", () => {
233 // All drivers need `NodeServices` (ChildProcessSpawner + FileSystem +
234 // Path). `OpenCodeDriver.create` additionally yields `OpenCodeRuntime`
235 // at construction time, so we wire `OpenCodeRuntimeLive` into the stack.
236 // `OpenCodeRuntimeLive` bundles its own `NetService.layer` via
237 // `Layer.provide`, so the only external requirement it still exposes is
238 // `ChildProcessSpawner` — resolved here by piping it through
239 // `provideMerge(NodeServices.layer)`.
240 //
241 // The nested `provideMerge`s read bottom-up: `NodeServices.layer`
242 // provides `OpenCodeRuntimeLive`'s deps while keeping its own outputs
243 // surfaced; that merged layer then provides `ServerConfig.layerTest`'s
244 // `FileSystem` dep while keeping everything else surfaced to the test.
245 const infraLayer = OpenCodeRuntimeLive.pipe(Layer.provideMerge(NodeServices.layer));
246 const testLayer = ServerConfig.layerTest(process.cwd(), {
247 prefix: "provider-instance-registry-all-drivers-test",
248 }).pipe(
249 Layer.provideMerge(infraLayer),
250 Layer.provideMerge(ServerSettingsService.layerTest()),
251 Layer.provideMerge(TestHttpClientLive),
252 Layer.provideMerge(Layer.succeed(ProviderEventLoggers, NoOpProviderEventLoggers)),
253 );
254​
255 it.live("boots one instance of every shipped driver from a single config map", () =>
256 Effect.gen(function* () {
257 const codexId = ProviderInstanceId.make("codex_default");
258 const claudeId = ProviderInstanceId.make("claude_default");
259 const cursorId = ProviderInstanceId.make("cursor_default");
260 const grokId = ProviderInstanceId.make("grok_default");
261 const openCodeId = ProviderInstanceId.make("opencode_default");
262​
263 const codexDriverKind = ProviderDriverKind.make("codex");
264 const claudeDriverKind = ProviderDriverKind.make("claudeAgent");
265 const cursorDriverKind = ProviderDriverKind.make("cursor");
266 const grokDriverKind = ProviderDriverKind.make("grok");
267 const openCodeDriverKind = ProviderDriverKind.make("opencode");
268​
269 const configMap: ProviderInstanceConfigMap = {
270 [codexId]: {
271 driver: codexDriverKind,
272 displayName: "Codex",
273 enabled: false,
274 config: makeCodexConfig({ homePath: "/home/julius/.codex" }),
275 },
276 [claudeId]: {
277 driver: claudeDriverKind,
278 displayName: "Claude",
279 enabled: false,
280 config: makeClaudeConfig({
281 homePath: "/home/julius/.claude-work",
282 launchArgs: "--verbose",
283 }),
284 },
285 [cursorId]: {
286 driver: cursorDriverKind,
287 displayName: "Cursor",
288 enabled: false,
289 config: makeCursorConfig({}),
290 },
291 [grokId]: {
292 driver: grokDriverKind,
293 displayName: "Grok",
294 enabled: false,
295 config: makeGrokConfig({}),
296 },
297 [openCodeId]: {
298 driver: openCodeDriverKind,
299 displayName: "OpenCode",
300 enabled: false,
301 config: makeOpenCodeConfig({}),
302 },
303 };
304​
305 const { registry } = yield* makeProviderInstanceRegistry({
306 drivers: [CodexDriver, ClaudeDriver, CursorDriver, GrokDriver, OpenCodeDriver],
307 configMap,
308 });
309​
310 // Every configured instance must materialize — none downgraded to a
311 // shadow snapshot, because every driver in the map is registered.
312 const unavailable = yield* registry.listUnavailable;
313 expect(unavailable).toEqual([]);
314​
315 const instances = yield* registry.listInstances;
316 expect(instances).toHaveLength(5);
317 expect(instances.map((instance) => instance.instanceId).toSorted()).toEqual(
318 [codexId, claudeId, cursorId, grokId, openCodeId].toSorted(),
319 );
320​
321 // Instance lookup by id resolves each instance to its own bundle —
322 // this is how rest-of-server routes turn/session calls in the new
323 // model. Each driver's bundle carries its advertised `driverKind`.
324 const codex = yield* registry.getInstance(codexId);
325 const claude = yield* registry.getInstance(claudeId);
326 const cursor = yield* registry.getInstance(cursorId);
327 const grok = yield* registry.getInstance(grokId);
328 const openCode = yield* registry.getInstance(openCodeId);
329 expect(codex?.driverKind).toBe(codexDriverKind);
330 expect(claude?.driverKind).toBe(claudeDriverKind);
331 expect(cursor?.driverKind).toBe(cursorDriverKind);
332 expect(grok?.driverKind).toBe(grokDriverKind);
333 expect(openCode?.driverKind).toBe(openCodeDriverKind);
334 expect(codex?.displayName).toBe("Codex");
335 expect(claude?.displayName).toBe("Claude");
336 expect(cursor?.displayName).toBe("Cursor");
337 expect(grok?.displayName).toBe("Grok");
338 expect(openCode?.displayName).toBe("OpenCode");
339​
340 // Every instance owns its own set of closures — no sharing across
341 // drivers. `adapter` / `textGeneration` / `snapshot` are all
342 // distinct references even when two instances happen to share a
343 // trait (e.g. Cursor + others all use a stub-or-real
344 // `textGeneration`; they must still be different object values).
345 const adapters = [
346 codex!.adapter,
347 claude!.adapter,
348 cursor!.adapter,
349 grok!.adapter,
350 openCode!.adapter,
351 ];
352 expect(new Set(adapters).size).toBe(adapters.length);
353 const textGenerations = [
354 codex!.textGeneration,
355 claude!.textGeneration,
356 cursor!.textGeneration,
357 grok!.textGeneration,
358 openCode!.textGeneration,
359 ];
360 expect(new Set(textGenerations).size).toBe(textGenerations.length);
361 const snapshots = [
362 codex!.snapshot,
363 claude!.snapshot,
364 cursor!.snapshot,
365 grok!.snapshot,
366 openCode!.snapshot,
367 ];
368 expect(new Set(snapshots).size).toBe(snapshots.length);
369​
370 // Snapshots identify themselves by `instanceId` + `driver` so
371 // downstream aggregation in `ProviderRegistry` can tell instances
372 // apart even when two share a driver. With `enabled: false`, the
373 // check short-circuits and we get a disabled/pending snapshot back
374 // — that's enough signal to validate the stamping wrapper without
375 // spawning real binaries.
376 const codexSnapshot = yield* codex!.snapshot.getSnapshot;
377 expect(codexSnapshot.instanceId).toBe(codexId);
378 expect(codexSnapshot.driver).toBe(codexDriverKind);
379 expect(codexSnapshot.enabled).toBe(false);
380 expect(codexSnapshot.continuation?.groupKey).toBe("codex:home:/home/julius/.codex");
381​
382 const claudeSnapshot = yield* claude!.snapshot.getSnapshot;
383 expect(claudeSnapshot.instanceId).toBe(claudeId);
384 expect(claudeSnapshot.driver).toBe(claudeDriverKind);
385 expect(claudeSnapshot.enabled).toBe(false);
386 expect(claudeSnapshot.continuation?.groupKey).toBe("claude:home:/home/julius/.claude-work");
387​
388 const cursorSnapshot = yield* cursor!.snapshot.getSnapshot;
389 expect(cursorSnapshot.instanceId).toBe(cursorId);
390 expect(cursorSnapshot.driver).toBe(cursorDriverKind);
391 expect(cursorSnapshot.enabled).toBe(false);
392 expect(cursorSnapshot.continuation?.groupKey).toBe(
393 `${cursorDriverKind}:instance:${cursorId}`,
394 );
395​
396 const grokSnapshot = yield* grok!.snapshot.getSnapshot;
397 expect(grokSnapshot.instanceId).toBe(grokId);
398 expect(grokSnapshot.driver).toBe(grokDriverKind);
399 expect(grokSnapshot.enabled).toBe(false);
400 expect(grokSnapshot.continuation?.groupKey).toBe(`${grokDriverKind}:instance:${grokId}`);
401​
402 const openCodeSnapshot = yield* openCode!.snapshot.getSnapshot;
403 expect(openCodeSnapshot.instanceId).toBe(openCodeId);
404 expect(openCodeSnapshot.driver).toBe(openCodeDriverKind);
405 expect(openCodeSnapshot.enabled).toBe(false);
406 expect(openCodeSnapshot.continuation?.groupKey).toBe(
407 `${openCodeDriverKind}:instance:${openCodeId}`,
408 );
409 }).pipe(Effect.provide(testLayer)),
410 );
411});