Open History to see the last commit
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;
32}
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;
57}
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("uses the current runtime owner after a provider handoff", () => {
85 const environmentId = EnvironmentId.make("environment-a");
86 const serverConfigs = new Map<EnvironmentId, ServerConfig>([
87 [
88 environmentId,
89 makeConfig([
90 { instanceId: "claudeAgent", driver: "claudeAgent" },
91 { instanceId: "codex", driver: "codex", displayName: "Codex" },
92 { instanceId: "codex_work", driver: "codex", displayName: "Codex" },
93 ]),
94 ],
95 ]);
96 const thread = {
97 ...makeThread(environmentId, "claudeAgent"),
98 runtime: {
99 status: "running" as const,
100 activeRunId: null,
101 providerInstanceId: ProviderInstanceId.make("codex_work"),
102 providerName: "Codex",
103 lastError: null,
104 updatedAt: "2026-06-01T00:01:00.000Z",
105 },
106 };
107
108 expect(
109 resolveThreadProviderInstance(serverConfigs.get(environmentId)?.providers, thread),
110 ).toMatchObject({
111 driverKind: "codex",
112 displayName: "Codex Work",
113 showBadge: true,
114 });
115 });
116});
117
118describe("createThreadRowProviderInstanceResolver", () => {
119 it("updates thread badges through the environment projection when a sibling is disabled and re-enabled", () => {
120 const environmentId = EnvironmentId.make("environment-a");
121 const personal = makeThread(environmentId, "codex");
122 const work = makeThread(environmentId, "codex_work");
123 const config = makeConfig([
124 { instanceId: "codex", driver: "codex" },
125 { instanceId: "codex_work", driver: "codex" },
126 ]);
127 const configs = Atom.make<ReadonlyMap<EnvironmentId, ServerConfig>>(
128 new Map([[environmentId, config]]),
129 );
130 const environments = createThreadListEnvironmentsAtom(configs);
131 const registry = AtomRegistry.make();
132 registry.mount(environments);
133 try {
134 const before = createThreadRowProviderInstanceResolver(
135 registry.get(environments).providersByEnvironmentId,
136 );
137 expect(before(personal)?.showBadge).toBe(true);
138 expect(before(work)?.showBadge).toBe(true);
139
140 registry.set(
141 configs,
142 new Map([
143 [
144 environmentId,
145 {
146 ...config,
147 providers: config.providers.map((provider) =>
148 provider.instanceId === "codex_work" ? { ...provider, enabled: false } : provider,
149 ),
150 },
151 ],
152 ]),
153 );
154 const disabled = createThreadRowProviderInstanceResolver(
155 registry.get(environments).providersByEnvironmentId,
156 );
157 expect(disabled(personal)?.showBadge).toBe(false);
158 expect(disabled(work)?.showBadge).toBe(true);
159
160 registry.set(configs, new Map([[environmentId, config]]));
161 const restored = createThreadRowProviderInstanceResolver(
162 registry.get(environments).providersByEnvironmentId,
163 );
164 expect(restored(personal)?.showBadge).toBe(true);
165 expect(restored(work)?.showBadge).toBe(true);
166 } finally {
167 registry.dispose();
168 }
169 });
170});