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

thread-provider-instance.test.ts

264 lines9.2 KB

1import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
2import {
3 EnvironmentId,
4 ProjectId,
5 ProviderInstanceId,
6 ThreadId,
7 type ServerConfig,
8} from "@t3tools/contracts";
9import { describe, expect, it } from "vite-plus/test";
10import { Atom, AtomRegistry } from "effect/reactivity";
11​
12import { createThreadListEnvironmentsAtom } from "../../state/thread-list-environments";
13​
14import {
15 createThreadRowProviderInstanceResolver,
16 resolveThreadProviderInstance,
17} from "./thread-provider-instance";
18​
19function makeConfig(
20 providers: ReadonlyArray<{
21 readonly instanceId: string;
22 readonly driver: string;
23 readonly displayName?: string;
24 readonly accentColor?: string;
25 }>,
26): ServerConfig {
27 return {
28 providers: providers.map((provider) => ({ enabled: true, ...provider })),
29 environment: { capabilities: {}, platform: { machine: "laptop" } },
30 settings: {},
31 } as unknown as ServerConfig;
33​
34function makeThread(environmentId: EnvironmentId, instanceId: string): EnvironmentThreadShell {
35 return {
36 environmentId,
37 id: ThreadId.make("thread-1"),
38 projectId: ProjectId.make("project-1"),
39 title: "Thread",
40 modelSelection: { instanceId: ProviderInstanceId.make(instanceId), model: "gpt-5.4" },
41 runtimeMode: "full-access",
42 interactionMode: "default",
43 branch: null,
44 worktreePath: null,
45 latestRun: null,
46 createdAt: "2026-06-01T00:00:00.000Z",
47 updatedAt: "2026-06-01T00:00:00.000Z",
48 archivedAt: null,
49 settledOverride: null,
50 settledAt: null,
51 runtime: null,
52 latestUserMessageAt: null,
53 hasPendingApprovals: false,
54 hasPendingUserInput: false,
55 hasActionableProposedPlan: false,
56 } as unknown as EnvironmentThreadShell;
58​
59describe("resolveThreadProviderInstance", () => {
60 it("resolves two environments with the same default instance id independently", () => {
61 const environmentA = EnvironmentId.make("environment-a");
62 const environmentB = EnvironmentId.make("environment-b");
63 const serverConfigs = new Map<EnvironmentId, ServerConfig>([
64 [
65 environmentA,
66 makeConfig([{ instanceId: "codex", driver: "codex", accentColor: "#ff8800" }]),
67 ],
68 [environmentB, makeConfig([{ instanceId: "codex", driver: "codex" }])],
69 ]);
70​
71 const threadA = makeThread(environmentA, "codex");
72 const threadB = makeThread(environmentB, "codex");
73​
74 expect(
75 resolveThreadProviderInstance(serverConfigs.get(environmentA)?.providers, threadA)
76 ?.accentColor,
77 ).toBe("#ff8800");
78 expect(
79 resolveThreadProviderInstance(serverConfigs.get(environmentB)?.providers, threadB)
80 ?.accentColor,
81 ).toBeUndefined();
82 });
83​
84 it("labels a custom instance by its id so its initials differ from the default", () => {
85 const environmentId = EnvironmentId.make("environment-a");
86 const serverConfigs = new Map<EnvironmentId, ServerConfig>([
87 [
88 environmentId,
89 makeConfig([
90 { instanceId: "codex", driver: "codex", displayName: "Codex" },
91 { instanceId: "codex_personal", driver: "codex", displayName: "Codex" },
92 ]),
93 ],
94 ]);
95​
96 expect(
97 resolveThreadProviderInstance(
98 serverConfigs.get(environmentId)?.providers,
99 makeThread(environmentId, "codex"),
100 )?.displayName,
101 ).toBe("Codex");
102 expect(
103 resolveThreadProviderInstance(
104 serverConfigs.get(environmentId)?.providers,
105 makeThread(environmentId, "codex_personal"),
106 )?.displayName,
107 ).toBe("Codex Personal");
108 });
109​
110 it("uses the current runtime owner after a provider handoff", () => {
111 const environmentId = EnvironmentId.make("environment-a");
112 const serverConfigs = new Map<EnvironmentId, ServerConfig>([
113 [
114 environmentId,
115 makeConfig([
116 { instanceId: "claudeAgent", driver: "claudeAgent" },
117 { instanceId: "codex", driver: "codex", displayName: "Codex" },
118 { instanceId: "codex_work", driver: "codex", displayName: "Codex" },
119 ]),
120 ],
121 ]);
122 const thread = {
123 ...makeThread(environmentId, "claudeAgent"),
124 runtime: {
125 status: "running" as const,
126 activeRunId: null,
127 providerInstanceId: ProviderInstanceId.make("codex_work"),
128 providerName: "Codex",
129 lastError: null,
130 updatedAt: "2026-06-01T00:01:00.000Z",
131 },
132 };
133​
134 expect(
135 resolveThreadProviderInstance(serverConfigs.get(environmentId)?.providers, thread),
136 ).toMatchObject({
137 driverKind: "codex",
138 displayName: "Codex Work",
139 showBadge: true,
140 });
141 });
142​
143 it("hides the badge for a single instance with no accent color", () => {
144 const environmentId = EnvironmentId.make("environment-a");
145 const serverConfigs = new Map<EnvironmentId, ServerConfig>([
146 [environmentId, makeConfig([{ instanceId: "codex", driver: "codex" }])],
147 ]);
148 const thread = makeThread(environmentId, "codex");
149​
150 expect(
151 resolveThreadProviderInstance(serverConfigs.get(environmentId)?.providers, thread)?.showBadge,
152 ).toBe(false);
153 });
154});
155​
156describe("createThreadRowProviderInstanceResolver", () => {
157 it("updates thread badges through the environment projection when a sibling is disabled and re-enabled", () => {
158 const environmentId = EnvironmentId.make("environment-a");
159 const personal = makeThread(environmentId, "codex");
160 const work = makeThread(environmentId, "codex_work");
161 const config = makeConfig([
162 { instanceId: "codex", driver: "codex" },
163 { instanceId: "codex_work", driver: "codex" },
164 ]);
165 const configs = Atom.make<ReadonlyMap<EnvironmentId, ServerConfig>>(
166 new Map([[environmentId, config]]),
167 );
168 const environments = createThreadListEnvironmentsAtom(configs);
169 const registry = AtomRegistry.make();
170 registry.mount(environments);
171 try {
172 const before = createThreadRowProviderInstanceResolver(
173 registry.get(environments).providersByEnvironmentId,
174 );
175 expect(before(personal)?.showBadge).toBe(true);
176 expect(before(work)?.showBadge).toBe(true);
177​
178 registry.set(
179 configs,
180 new Map([
181 [
182 environmentId,
183 {
184 ...config,
185 providers: config.providers.map((provider) =>
186 provider.instanceId === "codex_work" ? { ...provider, enabled: false } : provider,
187 ),
188 },
189 ],
190 ]),
191 );
192 const disabled = createThreadRowProviderInstanceResolver(
193 registry.get(environments).providersByEnvironmentId,
194 );
195 expect(disabled(personal)?.showBadge).toBe(false);
196 expect(disabled(work)?.showBadge).toBe(true);
197​
198 registry.set(configs, new Map([[environmentId, config]]));
199 const restored = createThreadRowProviderInstanceResolver(
200 registry.get(environments).providersByEnvironmentId,
201 );
202 expect(restored(personal)?.showBadge).toBe(true);
203 expect(restored(work)?.showBadge).toBe(true);
204 } finally {
205 registry.dispose();
206 }
207 });
208​
209 const environmentId = EnvironmentId.make("environment-a");
210 const serverConfigs = new Map<EnvironmentId, ServerConfig>([
211 [
212 environmentId,
213 makeConfig([
214 { instanceId: "codex", driver: "codex", displayName: "Codex" },
215 { instanceId: "codex_work", driver: "codex", displayName: "Codex" },
216 ]),
217 ],
218 ]);
219​
220 it("hands out the same reference for repeated lookups of one instance", () => {
221 const resolve = createThreadRowProviderInstanceResolver(
222 new Map([...serverConfigs].map(([id, config]) => [id, config.providers])),
223 );
224 const first = resolve(makeThread(environmentId, "codex"));
225 const second = resolve(makeThread(environmentId, "codex"));
226 expect(first).not.toBeNull();
227 expect(second).toBe(first);
228 });
229​
230 it("distinguishes instances of the same driver", () => {
231 const resolve = createThreadRowProviderInstanceResolver(
232 new Map([...serverConfigs].map(([id, config]) => [id, config.providers])),
233 );
234 const personal = resolve(makeThread(environmentId, "codex"));
235 const work = resolve(makeThread(environmentId, "codex_work"));
236 expect(personal).not.toBeNull();
237 expect(work).not.toBeNull();
238 expect(work).not.toBe(personal);
239 expect(work?.displayName).toBe("Codex Work");
240 });
241​
242 it("hands out a new identity when the server-config generation changes", () => {
243 const before = createThreadRowProviderInstanceResolver(
244 new Map([...serverConfigs].map(([id, config]) => [id, config.providers])),
245 );
246 const nextConfigs = new Map<EnvironmentId, ServerConfig>([
247 [environmentId, makeConfig([{ instanceId: "codex", driver: "codex" }])],
248 ]);
249 const after = createThreadRowProviderInstanceResolver(
250 new Map([...nextConfigs].map(([id, config]) => [id, config.providers])),
251 );
252 expect(after(makeThread(environmentId, "codex"))).not.toBe(
253 before(makeThread(environmentId, "codex")),
254 );
255 });
256​
257 it("resolves unknown instances to null without throwing", () => {
258 const resolve = createThreadRowProviderInstanceResolver(
259 new Map([...serverConfigs].map(([id, config]) => [id, config.providers])),
260 );
261 expect(resolve(makeThread(environmentId, "ghost"))).toBeNull();
262 expect(resolve(makeThread(environmentId, "ghost"))).toBeNull();
263 });
264});