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

thread-provider-instance.test.ts

309 lines10.7 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 { computeThreadMoveAvailability, createThreadMovePlanner } from "./threadOrder";
13import { makeThreadShellFixture } from "../../test-fixtures";
14import { createThreadListEnvironmentsAtom } from "../../state/thread-list-environments";
15​
16import {
17 createThreadRowProviderInstanceResolver,
18 resolveThreadProviderInstance,
19} from "./thread-provider-instance";
20​
21function makeConfig(
22 providers: ReadonlyArray<{
23 readonly instanceId: string;
24 readonly driver: string;
25 readonly displayName?: string;
26 readonly accentColor?: string;
27 }>,
28): ServerConfig {
29 return {
30 providers: providers.map((provider) => ({ enabled: true, ...provider })),
31 environment: { capabilities: {}, platform: { machine: "laptop" } },
32 settings: {},
33 } as unknown as ServerConfig;
35​
36function makeThread(environmentId: EnvironmentId, instanceId: string): EnvironmentThreadShell {
37 return {
38 environmentId,
39 id: ThreadId.make("thread-1"),
40 projectId: ProjectId.make("project-1"),
41 title: "Thread",
42 modelSelection: { instanceId: ProviderInstanceId.make(instanceId), model: "gpt-5.4" },
43 runtimeMode: "full-access",
44 interactionMode: "default",
45 branch: null,
46 worktreePath: null,
47 latestRun: null,
48 createdAt: "2026-06-01T00:00:00.000Z",
49 updatedAt: "2026-06-01T00:00:00.000Z",
50 archivedAt: null,
51 settledOverride: null,
52 settledAt: null,
53 runtime: null,
54 latestUserMessageAt: null,
55 hasPendingApprovals: false,
56 hasPendingUserInput: false,
57 hasActionableProposedPlan: false,
58 } as unknown as EnvironmentThreadShell;
60​
61describe("resolveThreadProviderInstance", () => {
62 it("resolves two environments with the same default instance id independently", () => {
63 const environmentA = EnvironmentId.make("environment-a");
64 const environmentB = EnvironmentId.make("environment-b");
65 const serverConfigs = new Map<EnvironmentId, ServerConfig>([
66 [
67 environmentA,
68 makeConfig([{ instanceId: "codex", driver: "codex", accentColor: "#ff8800" }]),
69 ],
70 [environmentB, makeConfig([{ instanceId: "codex", driver: "codex" }])],
71 ]);
72​
73 const threadA = makeThread(environmentA, "codex");
74 const threadB = makeThread(environmentB, "codex");
75​
76 expect(
77 resolveThreadProviderInstance(serverConfigs.get(environmentA)?.providers, threadA)
78 ?.accentColor,
79 ).toBe("#ff8800");
80 expect(
81 resolveThreadProviderInstance(serverConfigs.get(environmentB)?.providers, threadB)
82 ?.accentColor,
83 ).toBeUndefined();
84 });
85​
86 it("uses the current runtime owner after a provider handoff", () => {
87 const environmentId = EnvironmentId.make("environment-a");
88 const serverConfigs = new Map<EnvironmentId, ServerConfig>([
89 [
90 environmentId,
91 makeConfig([
92 { instanceId: "claudeAgent", driver: "claudeAgent" },
93 { instanceId: "codex", driver: "codex", displayName: "Codex" },
94 { instanceId: "codex_work", driver: "codex", displayName: "Codex" },
95 ]),
96 ],
97 ]);
98 const thread = {
99 ...makeThread(environmentId, "claudeAgent"),
100 runtime: {
101 status: "running" as const,
102 activeRunId: null,
103 providerInstanceId: ProviderInstanceId.make("codex_work"),
104 providerName: "Codex",
105 lastError: null,
106 updatedAt: "2026-06-01T00:01:00.000Z",
107 },
108 };
109​
110 expect(
111 resolveThreadProviderInstance(serverConfigs.get(environmentId)?.providers, thread),
112 ).toMatchObject({
113 driverKind: "codex",
114 displayName: "Codex Work",
115 showBadge: true,
116 });
117 });
118});
119​
120describe("createThreadRowProviderInstanceResolver", () => {
121 it("updates thread badges through the environment projection when a sibling is disabled and re-enabled", () => {
122 const environmentId = EnvironmentId.make("environment-a");
123 const personal = makeThread(environmentId, "codex");
124 const work = makeThread(environmentId, "codex_work");
125 const config = makeConfig([
126 { instanceId: "codex", driver: "codex" },
127 { instanceId: "codex_work", driver: "codex" },
128 ]);
129 const configs = Atom.make<ReadonlyMap<EnvironmentId, ServerConfig>>(
130 new Map([[environmentId, config]]),
131 );
132 const environments = createThreadListEnvironmentsAtom(configs, Atom.make([environmentId]));
133 const registry = AtomRegistry.make();
134 registry.mount(environments);
135 try {
136 const before = createThreadRowProviderInstanceResolver(
137 registry.get(environments).providersByEnvironmentId,
138 );
139 expect(before(personal)?.showBadge).toBe(true);
140 expect(before(work)?.showBadge).toBe(true);
141​
142 registry.set(
143 configs,
144 new Map([
145 [
146 environmentId,
147 {
148 ...config,
149 providers: config.providers.map((provider) =>
150 provider.instanceId === "codex_work" ? { ...provider, enabled: false } : provider,
151 ),
152 },
153 ],
154 ]),
155 );
156 const disabled = createThreadRowProviderInstanceResolver(
157 registry.get(environments).providersByEnvironmentId,
158 );
159 expect(disabled(personal)?.showBadge).toBe(false);
160 expect(disabled(work)?.showBadge).toBe(true);
161​
162 registry.set(configs, new Map([[environmentId, config]]));
163 const restored = createThreadRowProviderInstanceResolver(
164 registry.get(environments).providersByEnvironmentId,
165 );
166 expect(restored(personal)?.showBadge).toBe(true);
167 expect(restored(work)?.showBadge).toBe(true);
168 } finally {
169 registry.dispose();
170 }
171 });
172});
173​
174describe("thread list reorder availability", () => {
175 it.each(["active", "pinned"] as const)(
176 "updates %s moves on disconnect and reconnect without changing cached config",
177 (section) => {
178 const onlineId = EnvironmentId.make("online");
179 const offlineId = EnvironmentId.make("offline");
180 const config = makeConfig([{ instanceId: "codex", driver: "codex" }]);
181 const configs = Atom.make<ReadonlyMap<EnvironmentId, ServerConfig>>(
182 new Map([
183 [onlineId, config],
184 [offlineId, config],
185 ]),
186 );
187 const connected = Atom.make<ReadonlyArray<EnvironmentId>>([onlineId, offlineId]);
188 const environments = createThreadListEnvironmentsAtom(configs, connected);
189 const registry = AtomRegistry.make();
190 registry.mount(environments);
191 try {
192 const ordered = [onlineId, offlineId].map((environmentId, index) =>
193 makeThreadShellFixture({
194 environmentId,
195 id: ThreadId.make(`thread-${index}`),
196 pinnedAt: section === "pinned" ? "2026-09-02T00:00:00.000Z" : null,
197 }),
198 );
199 const movedId = `${onlineId}:thread-0`;
200 const plannerInput = () => {
201 const current = registry.get(environments);
202 return {
203 ordered,
204 section,
205 reorderableEnvironmentIds:
206 section === "pinned"
207 ? current.pinReorderEnvironmentIds
208 : current.activeReorderEnvironmentIds,
209 };
210 };
211 // Active rows sort by an implied key, so moving past an unarranged row writes only the mover.
212 const expectMoveDown = (neighborWritable: boolean) => {
213 const movable = section === "active" || neighborWritable;
214 expect(createThreadMovePlanner(plannerInput())(movedId, "down")?.length).toBe(
215 movable ? (section === "active" ? 1 : 2) : undefined,
216 );
217 expect(computeThreadMoveAvailability(plannerInput()).get(movedId)?.canMoveDown).toBe(
218 movable,
219 );
220 };
221 expectMoveDown(true);
222 const before = registry.get(environments);
223​
224 registry.set(connected, [onlineId]);
225​
226 expectMoveDown(false);
227 expect(registry.get(environments).pinningEnvironmentIds.has(offlineId)).toBe(true);
228 expect(registry.get(environments).settlementEnvironmentIds.has(offlineId)).toBe(true);
229 expect(registry.get(environments).providersByEnvironmentId.get(offlineId)).toBe(
230 before.providersByEnvironmentId.get(offlineId),
231 );
232​
233 const keyedInput = {
234 ...plannerInput(),
235 ordered: ordered.map((row, index) => ({
236 ...row,
237 pinOrderKey: section === "pinned" ? (index === 0 ? "bb" : "dd") : null,
238 activeOrderKey: section === "active" ? (index === 0 ? "bb" : "dd") : null,
239 })),
240 };
241 expect(createThreadMovePlanner(keyedInput)(movedId, "down")).toEqual([
242 expect.objectContaining({ id: movedId }),
243 ]);
244 expect(createThreadMovePlanner(keyedInput)(`${offlineId}:thread-1`, "up")).toBeNull();
245 expect(computeThreadMoveAvailability(keyedInput).get(movedId)?.canMoveDown).toBe(true);
246 expect(computeThreadMoveAvailability(keyedInput).get(`${offlineId}:thread-1`)).toEqual({
247 canMoveUp: false,
248 canMoveDown: false,
249 });
250​
251 const updatedConfig = {
252 ...config,
253 settings: {
254 ...config.settings,
255 snoozePresets: [{ kind: "after" as const, minutes: 90 }],
256 },
257 };
258 registry.set(
259 configs,
260 new Map([
261 [onlineId, config],
262 [offlineId, updatedConfig],
263 ]),
264 );
265​
266 const updated = registry.get(environments);
267 expect(updated.snoozePresetsByEnvironmentId.get(offlineId)).toEqual(
268 updatedConfig.settings.snoozePresets,
269 );
270 expect(updated.pinReorderEnvironmentIds.has(offlineId)).toBe(false);
271 expect(updated.activeReorderEnvironmentIds.has(offlineId)).toBe(false);
272 expectMoveDown(false);
273​
274 registry.set(
275 configs,
276 new Map([
277 [onlineId, config],
278 [
279 offlineId,
280 {
281 ...updatedConfig,
282 settings: {
283 ...updatedConfig.settings,
284 snoozePresets: updatedConfig.settings.snoozePresets.map((preset) => ({
285 ...preset,
286 })),
287 },
288 },
289 ],
290 ]),
291 );
292 expect(registry.get(environments)).toBe(updated);
293​
294 registry.set(connected, [onlineId, offlineId]);
295​
296 expectMoveDown(true);
297 expect(registry.get(environments).snoozePresetsByEnvironmentId.get(offlineId)).toBe(
298 updated.snoozePresetsByEnvironmentId.get(offlineId),
299 );
300​
301 registry.set(configs, new Map([[onlineId, config]]));
302​
303 expectMoveDown(false);
304 } finally {
305 registry.dispose();
306 }
307 },
308 );
309});