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

thread-provider-instance.test.ts

144 lines4.6 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";
10​
11import { resolveThreadProviderInstance } from "./thread-provider-instance";
12​
13function makeConfig(
14 providers: ReadonlyArray<{
15 readonly instanceId: string;
16 readonly driver: string;
17 readonly displayName?: string;
18 readonly accentColor?: string;
19 }>,
20): ServerConfig {
21 return { providers } as unknown as ServerConfig;
23​
24function makeThread(environmentId: EnvironmentId, instanceId: string): EnvironmentThreadShell {
25 return {
26 environmentId,
27 id: ThreadId.make("thread-1"),
28 projectId: ProjectId.make("project-1"),
29 title: "Thread",
30 modelSelection: { instanceId: ProviderInstanceId.make(instanceId), model: "gpt-5.4" },
31 runtimeMode: "full-access",
32 interactionMode: "default",
33 branch: null,
34 worktreePath: null,
35 latestTurn: null,
36 createdAt: "2026-06-01T00:00:00.000Z",
37 updatedAt: "2026-06-01T00:00:00.000Z",
38 archivedAt: null,
39 settledOverride: null,
40 settledAt: null,
41 session: null,
42 latestUserMessageAt: null,
43 hasPendingApprovals: false,
44 hasPendingUserInput: false,
45 hasActionableProposedPlan: false,
46 } as unknown as EnvironmentThreadShell;
48​
49describe("resolveThreadProviderInstance", () => {
50 it("resolves two environments with the same default instance id independently", () => {
51 const environmentA = EnvironmentId.make("environment-a");
52 const environmentB = EnvironmentId.make("environment-b");
53 const serverConfigs = new Map<EnvironmentId, ServerConfig>([
54 [
55 environmentA,
56 makeConfig([{ instanceId: "codex", driver: "codex", accentColor: "#ff8800" }]),
57 ],
58 [environmentB, makeConfig([{ instanceId: "codex", driver: "codex" }])],
59 ]);
60​
61 const threadA = makeThread(environmentA, "codex");
62 const threadB = makeThread(environmentB, "codex");
63​
64 expect(
65 resolveThreadProviderInstance(serverConfigs.get(environmentA)?.providers, threadA)
66 ?.accentColor,
67 ).toBe("#ff8800");
68 expect(
69 resolveThreadProviderInstance(serverConfigs.get(environmentB)?.providers, threadB)
70 ?.accentColor,
71 ).toBeUndefined();
72 });
73​
74 it("labels a custom instance by its id so its initials differ from the default", () => {
75 const environmentId = EnvironmentId.make("environment-a");
76 const serverConfigs = new Map<EnvironmentId, ServerConfig>([
77 [
78 environmentId,
79 makeConfig([
80 { instanceId: "codex", driver: "codex", displayName: "Codex" },
81 { instanceId: "codex_personal", driver: "codex", displayName: "Codex" },
82 ]),
83 ],
84 ]);
85​
86 expect(
87 resolveThreadProviderInstance(
88 serverConfigs.get(environmentId)?.providers,
89 makeThread(environmentId, "codex"),
90 )?.displayName,
91 ).toBe("Codex");
92 expect(
93 resolveThreadProviderInstance(
94 serverConfigs.get(environmentId)?.providers,
95 makeThread(environmentId, "codex_personal"),
96 )?.displayName,
97 ).toBe("Codex Personal");
98 });
99​
100 it("uses the current runtime owner after a provider handoff", () => {
101 const environmentId = EnvironmentId.make("environment-a");
102 const serverConfigs = new Map<EnvironmentId, ServerConfig>([
103 [
104 environmentId,
105 makeConfig([
106 { instanceId: "claudeAgent", driver: "claudeAgent" },
107 { instanceId: "codex", driver: "codex", displayName: "Codex" },
108 { instanceId: "codex_work", driver: "codex", displayName: "Codex" },
109 ]),
110 ],
111 ]);
112 const thread = {
113 ...makeThread(environmentId, "claudeAgent"),
114 runtime: {
115 status: "running" as const,
116 activeRunId: null,
117 providerInstanceId: ProviderInstanceId.make("codex_work"),
118 providerName: "Codex",
119 lastError: null,
120 updatedAt: "2026-06-01T00:01:00.000Z",
121 },
122 };
123​
124 expect(
125 resolveThreadProviderInstance(serverConfigs.get(environmentId)?.providers, thread),
126 ).toMatchObject({
127 driverKind: "codex",
128 displayName: "Codex Work",
129 showBadge: true,
130 });
131 });
132​
133 it("hides the badge for a single instance with no accent color", () => {
134 const environmentId = EnvironmentId.make("environment-a");
135 const serverConfigs = new Map<EnvironmentId, ServerConfig>([
136 [environmentId, makeConfig([{ instanceId: "codex", driver: "codex" }])],
137 ]);
138 const thread = makeThread(environmentId, "codex");
139​
140 expect(
141 resolveThreadProviderInstance(serverConfigs.get(environmentId)?.providers, thread)?.showBadge,
142 ).toBe(false);
143 });
144});