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";
10
11import {
12 createThreadRowProviderInstanceResolver,
13 resolveThreadProviderInstance,
14} from "./thread-provider-instance";
15
16function makeConfig(
17 providers: ReadonlyArray<{
18 readonly instanceId: string;
19 readonly driver: string;
20 readonly displayName?: string;
21 readonly accentColor?: string;
22 }>,
23): ServerConfig {
24 return { providers } as unknown as ServerConfig;
25}
26
27function makeThread(environmentId: EnvironmentId, instanceId: string): EnvironmentThreadShell {
28 return {
29 environmentId,
30 id: ThreadId.make("thread-1"),
31 projectId: ProjectId.make("project-1"),
32 title: "Thread",
33 modelSelection: { instanceId: ProviderInstanceId.make(instanceId), model: "gpt-5.4" },
34 runtimeMode: "full-access",
35 interactionMode: "default",
36 branch: null,
37 worktreePath: null,
38 latestTurn: null,
39 createdAt: "2026-06-01T00:00:00.000Z",
40 updatedAt: "2026-06-01T00:00:00.000Z",
41 archivedAt: null,
42 settledOverride: null,
43 settledAt: null,
44 session: null,
45 latestUserMessageAt: null,
46 hasPendingApprovals: false,
47 hasPendingUserInput: false,
48 hasActionableProposedPlan: false,
49 } as unknown as EnvironmentThreadShell;
50}
51
52describe("resolveThreadProviderInstance", () => {
53 it("resolves two environments with the same default instance id independently", () => {
54 const environmentA = EnvironmentId.make("environment-a");
55 const environmentB = EnvironmentId.make("environment-b");
56 const serverConfigs = new Map<EnvironmentId, ServerConfig>([
57 [
58 environmentA,
59 makeConfig([{ instanceId: "codex", driver: "codex", accentColor: "#ff8800" }]),
60 ],
61 [environmentB, makeConfig([{ instanceId: "codex", driver: "codex" }])],
62 ]);
63
64 const threadA = makeThread(environmentA, "codex");
65 const threadB = makeThread(environmentB, "codex");
66
67 expect(resolveThreadProviderInstance(serverConfigs, threadA)?.accentColor).toBe("#ff8800");
68 expect(resolveThreadProviderInstance(serverConfigs, threadB)?.accentColor).toBeUndefined();
69 });
70
71 it("labels a custom instance by its id so its initials differ from the default", () => {
72 const environmentId = EnvironmentId.make("environment-a");
73 const serverConfigs = new Map<EnvironmentId, ServerConfig>([
74 [
75 environmentId,
76 makeConfig([
77 { instanceId: "codex", driver: "codex", displayName: "Codex" },
78 { instanceId: "codex_personal", driver: "codex", displayName: "Codex" },
79 ]),
80 ],
81 ]);
82
83 expect(
84 resolveThreadProviderInstance(serverConfigs, makeThread(environmentId, "codex"))?.displayName,
85 ).toBe("Codex");
86 expect(
87 resolveThreadProviderInstance(serverConfigs, makeThread(environmentId, "codex_personal"))
88 ?.displayName,
89 ).toBe("Codex Personal");
90 });
91
92 it("hides the badge for a single instance with no accent color", () => {
93 const environmentId = EnvironmentId.make("environment-a");
94 const serverConfigs = new Map<EnvironmentId, ServerConfig>([
95 [environmentId, makeConfig([{ instanceId: "codex", driver: "codex" }])],
96 ]);
97 const thread = makeThread(environmentId, "codex");
98
99 expect(resolveThreadProviderInstance(serverConfigs, thread)?.showBadge).toBe(false);
100 });
101});
102
103describe("createThreadRowProviderInstanceResolver", () => {
104 const environmentId = EnvironmentId.make("environment-a");
105 const serverConfigs = new Map<EnvironmentId, ServerConfig>([
106 [
107 environmentId,
108 makeConfig([
109 { instanceId: "codex", driver: "codex", displayName: "Codex" },
110 { instanceId: "codex_work", driver: "codex", displayName: "Codex" },
111 ]),
112 ],
113 ]);
114
115 it("hands out the same reference for repeated lookups of one instance", () => {
116 const resolve = createThreadRowProviderInstanceResolver(serverConfigs);
117 const first = resolve(makeThread(environmentId, "codex"));
118 const second = resolve(makeThread(environmentId, "codex"));
119 // Memoized rows compare props by reference: a fresh object per call would
120 // re-render every row on every parent render (minute tick included).
121 expect(first).not.toBeNull();
122 expect(second).toBe(first);
123 });
124
125 it("distinguishes instances of the same driver", () => {
126 const resolve = createThreadRowProviderInstanceResolver(serverConfigs);
127 const personal = resolve(makeThread(environmentId, "codex"));
128 const work = resolve(makeThread(environmentId, "codex_work"));
129 expect(personal).not.toBeNull();
130 expect(work).not.toBeNull();
131 expect(work).not.toBe(personal);
132 expect(work?.displayName).toBe("Codex Work");
133 });
134
135 it("hands out a new identity when the server-config generation changes", () => {
136 const before = createThreadRowProviderInstanceResolver(serverConfigs);
137 const nextConfigs = new Map<EnvironmentId, ServerConfig>([
138 [environmentId, makeConfig([{ instanceId: "codex", driver: "codex" }])],
139 ]);
140 const after = createThreadRowProviderInstanceResolver(nextConfigs);
141 expect(after(makeThread(environmentId, "codex"))).not.toBe(
142 before(makeThread(environmentId, "codex")),
143 );
144 });
145
146 it("resolves unknown instances to null without throwing", () => {
147 const resolve = createThreadRowProviderInstanceResolver(serverConfigs);
148 expect(resolve(makeThread(environmentId, "ghost"))).toBeNull();
149 expect(resolve(makeThread(environmentId, "ghost"))).toBeNull();
150 });
151});