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

modelOptions.test.ts

449 lines14.8 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { ProviderInstanceId, type ModelSelection, type ServerConfig } from "@t3tools/contracts";
4​
5import {
6 buildModelOptions,
7 groupByProvider,
8 isModelSelectionUnavailable,
9 resolveDefaultableModelSelection,
10 resolveNewTaskModelSelection,
11 resolveSelectableModelSelection,
12 type ModelOption,
13} from "./modelOptions";
14​
15describe("mobile model options", () => {
16 it("groups models by provider and flags legacy entries", () => {
17 const config = {
18 providers: [
19 {
20 instanceId: "codex",
21 driver: "codex",
22 displayName: "Codex",
23 enabled: true,
24 installed: true,
25 auth: { status: "authenticated" },
26 models: [
27 {
28 slug: "gpt-5.6-sol",
29 name: "GPT-5.6 Sol",
30 isCustom: false,
31 capabilities: null,
32 },
33 {
34 slug: "gpt-5.4",
35 name: "GPT-5.4",
36 isCustom: false,
37 isLegacy: true,
38 capabilities: null,
39 },
40 ],
41 },
42 ],
43 } as unknown as ServerConfig;
44​
45 expect(groupByProvider(buildModelOptions(config, null))).toMatchObject([
46 {
47 providerKey: "codex",
48 providerLabel: "Codex",
49 models: [
50 { key: "codex:gpt-5.6-sol", label: "GPT-5.6 Sol", subtitle: "", isLegacy: false },
51 { key: "codex:gpt-5.4", label: "GPT-5.4", isLegacy: true },
52 ],
53 },
54 ]);
55 });
56​
57 it("carries configured ACP identity into model and provider catalogs", () => {
58 const iconUrl = "https://cdn.agentclientprotocol.com/registry/v1/latest/antigravity-acp.svg";
59 const config = {
60 providers: [
61 {
62 instanceId: "acpRegistry_antigravity",
63 driver: "acpRegistry",
64 displayName: "Antigravity",
65 iconUrl,
66 enabled: true,
67 installed: true,
68 auth: { status: "authenticated" },
69 models: [
70 {
71 slug: "default",
72 name: "Default",
73 isCustom: false,
74 capabilities: null,
75 },
76 ],
77 },
78 ],
79 } as unknown as ServerConfig;
80​
81 const [group] = groupByProvider(buildModelOptions(config, null));
82​
83 expect(group).toMatchObject({
84 providerKey: "acpRegistry_antigravity",
85 providerLabel: "Antigravity",
86 models: [
87 {
88 providerDriver: "acpRegistry",
89 providerIconUrl: iconUrl,
90 },
91 ],
92 });
93 });
94​
95 it("distinguishes same-name OpenCode models without changing their routing", () => {
96 const sources = [
97 { id: "anthropic", label: "Anthropic" },
98 { id: "github-copilot", label: "GitHub Copilot" },
99 { id: "opencode", label: "OpenCode Zen" },
100 ];
101 const config = {
102 providers: [
103 {
104 instanceId: "opencode_work",
105 driver: "opencode",
106 displayName: "OpenCode Work",
107 enabled: true,
108 installed: true,
109 auth: { status: "authenticated" },
110 models: sources.map((source) => ({
111 slug: `${source.id}/claude-fable-5`,
112 name: "Claude Fable 5",
113 subProvider: source.label,
114 isCustom: false,
115 capabilities: null,
116 })),
117 },
118 ],
119 } as unknown as ServerConfig;
120 const selection = {
121 instanceId: ProviderInstanceId.make("opencode_work"),
122 model: "github-copilot/claude-fable-5",
123 };
124​
125 const options = buildModelOptions(config, selection);
126​
127 expect(options).toMatchObject(
128 sources.map((source) => ({
129 key: `opencode_work:${source.id}/claude-fable-5`,
130 label: "Claude Fable 5",
131 subtitle: source.label,
132 providerLabel: "OpenCode Work",
133 selection: {
134 instanceId: "opencode_work",
135 model: `${source.id}/claude-fable-5`,
136 },
137 })),
138 );
139 expect(groupByProvider(options)).toEqual([
140 { providerKey: "opencode_work", providerLabel: "OpenCode Work", models: options },
141 ]);
142 });
143​
144 it("does not materialize catalog defaults for missing stored options", () => {
145 const config = {
146 providers: [
147 {
148 instanceId: "codex",
149 driver: "codex",
150 displayName: "Codex",
151 enabled: true,
152 installed: true,
153 auth: { status: "authenticated" },
154 models: [
155 {
156 slug: "gpt-test",
157 name: "GPT Test",
158 isCustom: false,
159 capabilities: {
160 optionDescriptors: [
161 {
162 id: "serviceTier",
163 label: "Service Tier",
164 type: "select",
165 options: [
166 { id: "default", label: "Standard", isDefault: true },
167 { id: "priority", label: "Fast" },
168 ],
169 currentValue: "default",
170 },
171 ],
172 },
173 },
174 ],
175 },
176 ],
177 } as unknown as ServerConfig;
178​
179 const [option] = buildModelOptions(config, {
180 instanceId: ProviderInstanceId.make("codex"),
181 model: "gpt-test",
182 });
183​
184 expect(option?.capabilities?.optionDescriptors?.[0]?.id).toBe("serviceTier");
185 expect(option?.selection.options).toBeUndefined();
186​
187 const [explicitOption] = buildModelOptions(config, {
188 instanceId: ProviderInstanceId.make("codex"),
189 model: "gpt-test",
190 options: [{ id: "serviceTier", value: "priority" }],
191 });
192 expect(explicitOption?.selection.options).toEqual([{ id: "serviceTier", value: "priority" }]);
193 });
194​
195 it("rejects stored selections whose provider is not usable", () => {
196 const config = {
197 providers: [
198 {
199 instanceId: "codex",
200 driver: "codex",
201 enabled: true,
202 installed: true,
203 auth: { status: "authenticated" },
204 models: [],
205 },
206 {
207 instanceId: "claudeAgent",
208 driver: "claudeAgent",
209 enabled: false,
210 installed: true,
211 auth: { status: "authenticated" },
212 models: [],
213 },
214 ],
215 } as unknown as ServerConfig;
216​
217 const usable = {
218 instanceId: ProviderInstanceId.make("codex"),
219 model: "gpt-5.6-sol",
220 };
221 const disabled = {
222 instanceId: ProviderInstanceId.make("claudeAgent"),
223 model: "claude-sonnet-5",
224 };
225 const removed = {
226 instanceId: ProviderInstanceId.make("codex_personal"),
227 model: "gpt-5.6-sol",
228 };
229​
230 expect(resolveSelectableModelSelection(config, usable)).toBe(usable);
231 expect(resolveSelectableModelSelection(config, disabled)).toBeNull();
232 expect(resolveSelectableModelSelection(config, removed)).toBeNull();
233 expect(isModelSelectionUnavailable(config, disabled)).toBe(false);
234 // An offline environment has no config to validate.
235 expect(resolveSelectableModelSelection(null, disabled)).toBe(disabled);
236 });
237​
238 describe("Antigravity selections", () => {
239 const selection = {
240 instanceId: ProviderInstanceId.make("google_work"),
241 model: "gemini-3.1-pro-high",
242 options: [{ id: "native-option", value: "saved/opaque-choice" }],
243 };
244 const model = {
245 slug: selection.model,
246 name: "Gemini 3.1 Pro High",
247 subProvider: "Google",
248 isCustom: false,
249 isDefault: true,
250 isLegacy: true,
251 capabilities: {
252 optionDescriptors: [
253 {
254 id: "native-option",
255 label: "Native option",
256 type: "select",
257 options: [{ id: "current/default", label: "Default", isDefault: true }],
258 currentValue: "current/default",
259 },
260 ],
261 },
262 };
263 const config = {
264 providers: [
265 {
266 instanceId: selection.instanceId,
267 driver: "antigravity",
268 displayName: "Google Work",
269 enabled: true,
270 installed: true,
271 auth: { status: "authenticated" },
272 models: [model],
273 },
274 ],
275 } as unknown as ServerConfig;
276​
277 it.each([
278 ["disabled", { enabled: false }],
279 ["uninstalled", { installed: false }],
280 ["signed out", { auth: { status: "unauthenticated" } }],
281 ["unavailable", { availability: "unavailable" }],
282 ] as const)("keeps a %s provider's selection and known model details", (_state, update) => {
283 const unavailableConfig = {
284 ...config,
285 providers: config.providers.map((provider) => ({ ...provider, ...update })),
286 };
287​
288 expect(resolveSelectableModelSelection(unavailableConfig, selection)).toBe(selection);
289 expect(resolveDefaultableModelSelection(unavailableConfig, selection)).toBe(selection);
290 expect(isModelSelectionUnavailable(unavailableConfig, selection)).toBe(true);
291 expect(buildModelOptions(unavailableConfig, null)).toEqual([]);
292 const [option] = buildModelOptions(unavailableConfig, selection);
293 expect(option).toMatchObject({
294 key: `google_work:${selection.model}`,
295 label: model.name,
296 subtitle: "Google",
297 providerKey: "google_work",
298 providerLabel: "Google Work",
299 providerDriver: "antigravity",
300 isDefault: false,
301 isLegacy: true,
302 isUnavailable: true,
303 capabilities: model.capabilities,
304 });
305 expect(option?.selection).toBe(selection);
306 });
307​
308 it("keeps an exact selection when its model leaves and returns to the catalog", () => {
309 const changedConfig = {
310 ...config,
311 providers: config.providers.map((provider) => ({
312 ...provider,
313 models: provider.models.map((model) => ({ ...model, slug: "gemini-3.1-pro-low" })),
314 })),
315 };
316​
317 expect(resolveDefaultableModelSelection(changedConfig, selection)).toBe(selection);
318 expect(isModelSelectionUnavailable(changedConfig, selection)).toBe(true);
319 const options = buildModelOptions(changedConfig, selection);
320 const missing = options.find((option) => option.selection.model === selection.model);
321 expect(missing).toMatchObject({
322 label: selection.model,
323 providerLabel: "Google Work",
324 providerDriver: "antigravity",
325 isUnavailable: true,
326 capabilities: null,
327 });
328 expect(missing?.selection).toBe(selection);
329 expect(
330 resolveNewTaskModelSelection({
331 draftSelection: null,
332 projectDefaultSelection: resolveDefaultableModelSelection(changedConfig, selection),
333 stickySelection: null,
334 modelOptions: options,
335 }),
336 ).toBe(selection);
337​
338 const [restored] = buildModelOptions(config, selection);
339 expect(isModelSelectionUnavailable(config, selection)).toBe(false);
340 expect(restored?.isUnavailable).not.toBe(true);
341 expect(restored?.selection).toBe(selection);
342 expect(resolveDefaultableModelSelection(config, selection)).toBe(selection);
343 expect(buildModelOptions(config, null)[0]?.selection.options).toBeUndefined();
344 });
345​
346 it("uses configured instance metadata when provider status is missing", () => {
347 const missingStatusConfig = {
348 providers: [],
349 settings: {
350 providerInstances: {
351 [selection.instanceId]: { driver: "antigravity", displayName: "Google Work" },
352 },
353 },
354 } as unknown as ServerConfig;
355​
356 expect(resolveDefaultableModelSelection(missingStatusConfig, selection)).toBe(selection);
357 expect(isModelSelectionUnavailable(missingStatusConfig, selection)).toBe(true);
358 expect(buildModelOptions(missingStatusConfig, selection)).toMatchObject([
359 {
360 providerDriver: "antigravity",
361 providerLabel: "Google Work",
362 isUnavailable: true,
363 selection,
364 },
365 ]);
366 });
367​
368 it("keeps offline selections without assuming that an unknown instance is Antigravity", () => {
369 const unknownConfig = { ...config, providers: [] };
370​
371 expect(resolveDefaultableModelSelection(null, selection)).toBe(selection);
372 expect(isModelSelectionUnavailable(null, selection)).toBe(false);
373 expect(buildModelOptions(null, selection)[0]?.selection).toBe(selection);
374 expect(buildModelOptions(null, selection)[0]?.isUnavailable).not.toBe(true);
375 expect(isModelSelectionUnavailable(unknownConfig, selection)).toBe(false);
376 expect(resolveSelectableModelSelection(unknownConfig, selection)).toBeNull();
377 });
378 });
379​
380 it("keeps legacy models out of implicit defaults", () => {
381 const config = {
382 providers: [
383 {
384 instanceId: "codex",
385 driver: "codex",
386 displayName: "Codex",
387 enabled: true,
388 installed: true,
389 auth: { status: "authenticated" },
390 models: [
391 { slug: "gpt-5.6-sol", name: "GPT-5.6 Sol", isCustom: false, capabilities: null },
392 {
393 slug: "gpt-5.4",
394 name: "GPT-5.4",
395 isCustom: false,
396 isLegacy: true,
397 capabilities: null,
398 },
399 ],
400 },
401 ],
402 } as unknown as ServerConfig;
403​
404 const current = { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.6-sol" };
405 const legacy = { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" };
406​
407 expect(resolveDefaultableModelSelection(config, current)).toBe(current);
408 // A legacy last-used selection falls through to the provider default.
409 expect(resolveDefaultableModelSelection(config, legacy)).toBeNull();
410 // Offline: nothing to validate against, selection passes through.
411 expect(resolveDefaultableModelSelection(null, legacy)).toBe(legacy);
412 });
413​
414 it("resolves new tasks from draft, project, sticky, then provider defaults", () => {
415 const draft = { instanceId: ProviderInstanceId.make("codex"), model: "draft" };
416 const project = { instanceId: ProviderInstanceId.make("codex"), model: "project" };
417 const sticky = { instanceId: ProviderInstanceId.make("codex"), model: "sticky" };
418 const providerDefault = {
419 selection: { instanceId: ProviderInstanceId.make("codex"), model: "default" },
420 isDefault: true,
421 } as ModelOption;
422 const resolve = (
423 draftSelection: ModelSelection | null,
424 projectDefaultSelection: ModelSelection | null,
425 stickySelection: ModelSelection | null,
426 ) =>
427 resolveNewTaskModelSelection({
428 draftSelection,
429 projectDefaultSelection,
430 stickySelection,
431 modelOptions: [providerDefault],
432 });
433​
434 expect(resolve(draft, project, sticky)).toBe(draft);
435 expect(resolve(null, project, sticky)).toBe(project);
436 expect(resolve(null, null, sticky)).toBe(sticky);
437 expect(resolve(null, null, null)).toBe(providerDefault.selection);
438​
439 const unavailable = { ...providerDefault, isUnavailable: true };
440 expect(
441 resolveNewTaskModelSelection({
442 draftSelection: null,
443 projectDefaultSelection: null,
444 stickySelection: null,
445 modelOptions: [unavailable],
446 }),
447 ).toBeNull();
448 });
449});