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

modelSelection.test.ts

405 lines12.4 KB

1import { ProviderDriverKind, ProviderInstanceId, type ServerProvider } from "@t3tools/contracts";
2import { DEFAULT_UNIFIED_SETTINGS, type UnifiedSettings } from "@t3tools/contracts/settings";
3import { describe, expect, it } from "vite-plus/test";
4import { createModelSelection } from "@t3tools/shared/model";
5import { deriveProviderInstanceEntries } from "./providerInstances";
6import {
7 getAppModelOptionsForInstance,
8 resolveAppModelSelectionForInstance,
9 resolveAppModelSelectionState,
10 resolvePlanAgentHealPatch,
11 withoutPlanAgentSelection,
12} from "./modelSelection";
13​
14function provider(input: {
15 provider?: ProviderDriverKind;
16 instanceId: string;
17 models?: ReadonlyArray<string>;
18}): ServerProvider {
19 const driver =
20 input.provider ??
21 (input.instanceId.startsWith("claude_")
22 ? ProviderDriverKind.make("claudeAgent")
23 : ProviderDriverKind.make("codex"));
24 return {
25 instanceId: ProviderInstanceId.make(input.instanceId),
26 driver,
27 enabled: true,
28 installed: true,
29 version: null,
30 status: "ready",
31 auth: { status: "authenticated" },
32 checkedAt: "2026-01-01T00:00:00.000Z",
33 models: (input.models ?? []).map((slug) => ({
34 slug,
35 name: slug,
36 isCustom: false,
37 capabilities: {},
38 })),
39 slashCommands: [],
40 skills: [],
41 };
43​
44function settingsWithProviderInstances(): UnifiedSettings {
45 return {
46 ...DEFAULT_UNIFIED_SETTINGS,
47 providerInstances: {
48 [ProviderInstanceId.make("claudeAgent")]: {
49 driver: ProviderDriverKind.make("claudeAgent"),
50 config: { customModels: [] },
51 },
52 [ProviderInstanceId.make("claude_openrouter")]: {
53 driver: ProviderDriverKind.make("claudeAgent"),
54 config: { customModels: ["openai/gpt-5.5"] },
55 },
56 },
57 };
59​
60describe("instance-scoped model selection", () => {
61 it("preserves server-provided legacy model metadata", () => {
62 const baseProvider = provider({
63 instanceId: "claudeAgent",
64 models: ["claude-opus-4-8"],
65 });
66 const providers = [
67 {
68 ...baseProvider,
69 models: [{ ...baseProvider.models[0]!, isLegacy: true }],
70 },
71 ];
72 const stock = deriveProviderInstanceEntries(providers)[0]!;
73​
74 expect(getAppModelOptionsForInstance(settingsWithProviderInstances(), stock)[0]?.isLegacy).toBe(
75 true,
76 );
77 });
78​
79 it("keeps custom models on the provider instance that declared them", () => {
80 const providers = [
81 provider({
82 instanceId: "claudeAgent",
83 models: ["claude-sonnet-4-6"],
84 }),
85 provider({
86 instanceId: "claude_openrouter",
87 models: ["claude-sonnet-4-6"],
88 }),
89 ];
90 const entries = deriveProviderInstanceEntries(providers);
91 const stock = entries.find((entry) => entry.instanceId === "claudeAgent")!;
92 const openrouter = entries.find((entry) => entry.instanceId === "claude_openrouter")!;
93​
94 expect(
95 getAppModelOptionsForInstance(settingsWithProviderInstances(), stock).map(
96 (option) => option.slug,
97 ),
98 ).not.toContain("openai/gpt-5.5");
99 expect(
100 getAppModelOptionsForInstance(settingsWithProviderInstances(), openrouter).map(
101 (option) => option.slug,
102 ),
103 ).toContain("openai/gpt-5.5");
104 });
105​
106 it("resolves a custom slug against the selected custom instance", () => {
107 const providers = [
108 provider({ provider: ProviderDriverKind.make("claudeAgent"), instanceId: "claudeAgent" }),
109 provider({
110 provider: ProviderDriverKind.make("claudeAgent"),
111 instanceId: "claude_openrouter",
112 }),
113 ];
114​
115 expect(
116 resolveAppModelSelectionForInstance(
117 ProviderInstanceId.make("claude_openrouter"),
118 settingsWithProviderInstances(),
119 providers,
120 "openai/gpt-5.5",
121 ),
122 ).toBe("openai/gpt-5.5");
123 });
124​
125 it("preserves a custom slug that collides with a provider alias", () => {
126 const providers = [
127 provider({
128 provider: ProviderDriverKind.make("claudeAgent"),
129 instanceId: "claude_openrouter",
130 models: ["claude-opus-4-8"],
131 }),
132 ];
133 const settings: UnifiedSettings = {
134 ...settingsWithProviderInstances(),
135 providerInstances: {
136 ...settingsWithProviderInstances().providerInstances,
137 [ProviderInstanceId.make("claude_openrouter")]: {
138 driver: ProviderDriverKind.make("claudeAgent"),
139 config: { customModels: ["opus"] },
140 },
141 },
142 };
143 const openrouter = deriveProviderInstanceEntries(providers)[0]!;
144​
145 expect(
146 getAppModelOptionsForInstance(settings, openrouter).map((option) => option.slug),
147 ).toEqual(["claude-opus-4-8", "opus"]);
148 expect(
149 resolveAppModelSelectionForInstance(
150 ProviderInstanceId.make("claude_openrouter"),
151 settings,
152 providers,
153 "opus",
154 ),
155 ).toBe("opus");
156 });
157​
158 it("includes Grok custom models from the selected provider instance", () => {
159 const providers = [provider({ provider: ProviderDriverKind.make("grok"), instanceId: "grok" })];
160 const settings: UnifiedSettings = {
161 ...settingsWithProviderInstances(),
162 providerInstances: {
163 ...settingsWithProviderInstances().providerInstances,
164 [ProviderInstanceId.make("grok")]: {
165 driver: ProviderDriverKind.make("grok"),
166 config: { customModels: ["grok-test-custom-model"] },
167 },
168 },
169 };
170 const grok = deriveProviderInstanceEntries(providers).find(
171 (entry) => entry.instanceId === "grok",
172 )!;
173​
174 expect(getAppModelOptionsForInstance(settings, grok).map((option) => option.slug)).toContain(
175 "grok-test-custom-model",
176 );
177 });
178​
179 it("does not inject an unknown selected slug into the stock instance list", () => {
180 const providers = [
181 provider({
182 instanceId: "claudeAgent",
183 models: ["claude-sonnet-4-6"],
184 }),
185 provider({
186 instanceId: "claude_openrouter",
187 models: ["claude-sonnet-4-6"],
188 }),
189 ];
190 const stock = deriveProviderInstanceEntries(providers).find(
191 (entry) => entry.instanceId === "claudeAgent",
192 )!;
193​
194 expect(
195 getAppModelOptionsForInstance(settingsWithProviderInstances(), stock).map(
196 (option) => option.slug,
197 ),
198 ).not.toContain("openai/gpt-5.5");
199 });
200​
201 it("hides server models from the instance option list", () => {
202 const providers = [
203 provider({
204 instanceId: "claudeAgent",
205 models: ["claude-opus-4-6", "claude-sonnet-4-6"],
206 }),
207 ];
208 const settings: UnifiedSettings = {
209 ...settingsWithProviderInstances(),
210 providerModelPreferences: {
211 [ProviderInstanceId.make("claudeAgent")]: {
212 hiddenModels: ["claude-opus-4-6"],
213 modelOrder: [],
214 },
215 },
216 };
217 const stock = deriveProviderInstanceEntries(providers).find(
218 (entry) => entry.instanceId === "claudeAgent",
219 )!;
220​
221 expect(getAppModelOptionsForInstance(settings, stock).map((option) => option.slug)).toEqual([
222 "claude-sonnet-4-6",
223 ]);
224 });
225​
226 it("applies persisted per-instance model ordering", () => {
227 const providers = [
228 provider({
229 instanceId: "claudeAgent",
230 models: ["claude-opus-4-6", "claude-sonnet-4-6", "claude-haiku-4-5"],
231 }),
232 ];
233 const settings: UnifiedSettings = {
234 ...settingsWithProviderInstances(),
235 providerModelPreferences: {
236 [ProviderInstanceId.make("claudeAgent")]: {
237 hiddenModels: [],
238 modelOrder: ["claude-haiku-4-5", "claude-opus-4-6"],
239 },
240 },
241 };
242 const stock = deriveProviderInstanceEntries(providers).find(
243 (entry) => entry.instanceId === "claudeAgent",
244 )!;
245​
246 expect(getAppModelOptionsForInstance(settings, stock).map((option) => option.slug)).toEqual([
247 "claude-haiku-4-5",
248 "claude-opus-4-6",
249 "claude-sonnet-4-6",
250 ]);
251 });
252​
253 it("falls back when the selected model is hidden", () => {
254 const providers = [
255 provider({
256 instanceId: "claudeAgent",
257 models: ["claude-opus-4-6", "claude-sonnet-4-6"],
258 }),
259 ];
260 const settings: UnifiedSettings = {
261 ...settingsWithProviderInstances(),
262 providerModelPreferences: {
263 [ProviderInstanceId.make("claudeAgent")]: {
264 hiddenModels: ["claude-opus-4-6"],
265 modelOrder: [],
266 },
267 },
268 };
269​
270 expect(
271 resolveAppModelSelectionForInstance(
272 ProviderInstanceId.make("claudeAgent"),
273 settings,
274 providers,
275 "claude-opus-4-6",
276 ),
277 ).toBe("claude-sonnet-4-6");
278 });
279​
280 it("falls back instead of resolving a custom slug against the wrong instance", () => {
281 const providers = [
282 provider({
283 instanceId: "claudeAgent",
284 models: ["claude-sonnet-4-6"],
285 }),
286 provider({
287 instanceId: "claude_openrouter",
288 models: ["claude-sonnet-4-6"],
289 }),
290 ];
291​
292 expect(
293 resolveAppModelSelectionForInstance(
294 ProviderInstanceId.make("claudeAgent"),
295 settingsWithProviderInstances(),
296 providers,
297 "openai/gpt-5.5",
298 ),
299 ).toBe("claude-sonnet-4-6");
300 });
301​
302 it("preserves custom provider instances in settings model selection", () => {
303 const providers = [
304 provider({
305 instanceId: "claudeAgent",
306 models: ["claude-sonnet-4-6"],
307 }),
308 provider({
309 instanceId: "claude_openrouter",
310 models: ["claude-sonnet-4-6"],
311 }),
312 ];
313 const settings: UnifiedSettings = {
314 ...settingsWithProviderInstances(),
315 textGenerationModelSelection: {
316 instanceId: ProviderInstanceId.make("claude_openrouter"),
317 model: "openai/gpt-5.5",
318 },
319 };
320​
321 expect(resolveAppModelSelectionState(settings, providers)).toEqual({
322 instanceId: ProviderInstanceId.make("claude_openrouter"),
323 model: "openai/gpt-5.5",
324 });
325 });
326});
327​
328describe("withoutPlanAgentSelection", () => {
329 const instance = ProviderInstanceId.make("opencode");
330 const model = "opencode/gpt-5.4";
331​
332 it("drops a stored plan agent option", () => {
333 const selection = createModelSelection(instance, model, [
334 { id: "variant", value: "high" },
335 { id: "agent", value: "plan" },
336 ]);
337 expect(withoutPlanAgentSelection(selection)).toEqual(
338 createModelSelection(instance, model, [{ id: "variant", value: "high" }]),
339 );
340 });
341​
342 it("keeps non-plan agent options", () => {
343 const selection = createModelSelection(instance, model, [{ id: "agent", value: "build" }]);
344 expect(withoutPlanAgentSelection(selection)).toBe(selection);
345 });
346​
347 it("omits options entirely when plan was the only stored option", () => {
348 const selection = createModelSelection(instance, model, [{ id: "agent", value: "plan" }]);
349 expect(withoutPlanAgentSelection(selection)).toEqual({ instanceId: instance, model });
350 });
351​
352 it("returns null and undefined selections unchanged", () => {
353 expect(withoutPlanAgentSelection(null)).toBeNull();
354 expect(withoutPlanAgentSelection(undefined)).toBeUndefined();
355 });
356});
357​
358describe("resolvePlanAgentHealPatch", () => {
359 const instance = ProviderInstanceId.make("opencode");
360 const model = "opencode/gpt-5.4";
361 const healed = createModelSelection(instance, model, [{ id: "variant", value: "high" }]);
362 const storedPlan = createModelSelection(instance, model, [
363 { id: "variant", value: "high" },
364 { id: "agent", value: "plan" },
365 ]);
366 const nullPatch = {
367 planModeEnabled: true,
368 textGenerationModelSelection: storedPlan,
369 sourceControlWriterModelSelection: null,
370 };
371​
372 it("returns null when plan mode is on", () => {
373 expect(resolvePlanAgentHealPatch(nullPatch)).toBeNull();
374 });
375​
376 it("returns null when nothing needs healing", () => {
377 expect(
378 resolvePlanAgentHealPatch({
379 planModeEnabled: false,
380 textGenerationModelSelection: healed,
381 sourceControlWriterModelSelection: null,
382 }),
383 ).toBeNull();
384 });
385​
386 it("patches the stored text generation selection to drop the plan agent", () => {
387 expect(
388 resolvePlanAgentHealPatch({
389 planModeEnabled: false,
390 textGenerationModelSelection: storedPlan,
391 sourceControlWriterModelSelection: null,
392 }),
393 ).toEqual({ textGenerationModelSelection: healed });
394 });
395​
396 it("patches a stored source control writer selection that uses the plan agent", () => {
397 expect(
398 resolvePlanAgentHealPatch({
399 planModeEnabled: false,
400 textGenerationModelSelection: healed,
401 sourceControlWriterModelSelection: storedPlan,
402 }),
403 ).toEqual({ sourceControlWriterModelSelection: healed });
404 });
405});