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

ProviderInstanceRegistryLive.test.ts

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