Open History to see the last commit
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 [emptyOption] = buildModelOptions(config, {
188 instanceId: ProviderInstanceId.make("codex"),
189 model: "gpt-test",
190 options: [],
191 });
192 expect(emptyOption?.selection).toEqual(option?.selection);
193
194 const [explicitOption] = buildModelOptions(config, {
195 instanceId: ProviderInstanceId.make("codex"),
196 model: "gpt-test",
197 options: [{ id: "serviceTier", value: "priority" }],
198 });
199 expect(explicitOption?.selection.options).toEqual([{ id: "serviceTier", value: "priority" }]);
200 });
201
202 it("limits existing threads to their provider while new tasks keep every provider", () => {
203 const providers = ["codex", "claudeAgent"].map((instanceId) => ({
204 instanceId,
205 driver: instanceId,
206 enabled: true,
207 installed: true,
208 auth: { status: "authenticated" },
209 models: [{ slug: "test", name: instanceId, capabilities: null }],
210 }));
211 const config = { providers } as unknown as ServerConfig;
212 const selection = { instanceId: ProviderInstanceId.make("codex"), model: "test" };
213
214 expect(buildModelOptions(config, selection).map((option) => option.providerKey)).toEqual([
215 "codex",
216 "claudeAgent",
217 ]);
218 expect(buildModelOptions(config, selection, selection.instanceId)).toEqual(
219 buildModelOptions(config, selection).filter((option) => option.providerKey === "codex"),
220 );
221 });
222
223 it.each(["disabled", "unavailable", "missing"] as const)(
224 "retains the selected %s provider's fallback in a filtered catalog",
225 (state) => {
226 const selection = {
227 instanceId: ProviderInstanceId.make("google_work"),
228 model: "saved-model",
229 options: [{ id: "native-option", value: "saved-choice" }],
230 };
231 const provider = {
232 instanceId: selection.instanceId,
233 driver: "antigravity",
234 displayName: "Google Work",
235 enabled: state !== "disabled",
236 installed: true,
237 availability: state === "unavailable" ? "unavailable" : "available",
238 auth: { status: "authenticated" },
239 models: [{ slug: selection.model, name: "Saved model", capabilities: null }],
240 };
241 const config = {
242 providers: state === "missing" ? [] : [provider],
243 settings: { providerInstances: { google_work: { driver: "antigravity" } } },
244 } as unknown as ServerConfig;
245 const options = buildModelOptions(config, selection, selection.instanceId);
246 expect(options).toEqual(buildModelOptions(config, selection));
247 expect(options).toHaveLength(1);
248 expect(options[0]).toMatchObject({ selection, isUnavailable: true });
249 },
250 );
251
252 it("rejects stored selections whose provider is not usable", () => {
253 const config = {
254 providers: [
255 {
256 instanceId: "codex",
257 driver: "codex",
258 enabled: true,
259 installed: true,
260 auth: { status: "authenticated" },
261 models: [],
262 },
263 {
264 instanceId: "claudeAgent",
265 driver: "claudeAgent",
266 enabled: false,
267 installed: true,
268 auth: { status: "authenticated" },
269 models: [],
270 },
271 ],
272 } as unknown as ServerConfig;
273
274 const usable = {
275 instanceId: ProviderInstanceId.make("codex"),
276 model: "gpt-5.6-sol",
277 };
278 const disabled = {
279 instanceId: ProviderInstanceId.make("claudeAgent"),
280 model: "claude-sonnet-5",
281 };
282 const removed = {
283 instanceId: ProviderInstanceId.make("codex_personal"),
284 model: "gpt-5.6-sol",
285 };
286
287 expect(resolveSelectableModelSelection(config, usable)).toBe(usable);
288 expect(resolveSelectableModelSelection(config, disabled)).toBeNull();
289 expect(resolveSelectableModelSelection(config, removed)).toBeNull();
290 expect(isModelSelectionUnavailable(config, disabled)).toBe(false);
291 // An offline environment has no config to validate.
292 expect(resolveSelectableModelSelection(null, disabled)).toBe(disabled);
293 });
294
295 describe("Antigravity selections", () => {
296 const selection = {
297 instanceId: ProviderInstanceId.make("google_work"),
298 model: "gemini-3.1-pro-high",
299 options: [{ id: "native-option", value: "saved/opaque-choice" }],
300 };
301 const model = {
302 slug: selection.model,
303 name: "Gemini 3.1 Pro High",
304 subProvider: "Google",
305 isCustom: false,
306 isDefault: true,
307 isLegacy: true,
308 capabilities: {
309 optionDescriptors: [
310 {
311 id: "native-option",
312 label: "Native option",
313 type: "select",
314 options: [{ id: "current/default", label: "Default", isDefault: true }],
315 currentValue: "current/default",
316 },
317 ],
318 },
319 };
320 const config = {
321 providers: [
322 {
323 instanceId: selection.instanceId,
324 driver: "antigravity",
325 displayName: "Google Work",
326 enabled: true,
327 installed: true,
328 auth: { status: "authenticated" },
329 models: [model],
330 },
331 ],
332 } as unknown as ServerConfig;
333
334 it.each([
335 ["disabled", { enabled: false }],
336 ["uninstalled", { installed: false }],
337 ["signed out", { auth: { status: "unauthenticated" } }],
338 ["unavailable", { availability: "unavailable" }],
339 ] as const)("keeps a %s provider's selection and known model details", (_state, update) => {
340 const unavailableConfig = {
341 ...config,
342 providers: config.providers.map((provider) => ({ ...provider, ...update })),
343 };
344
345 expect(resolveSelectableModelSelection(unavailableConfig, selection)).toBe(selection);
346 expect(resolveDefaultableModelSelection(unavailableConfig, selection)).toBe(selection);
347 expect(isModelSelectionUnavailable(unavailableConfig, selection)).toBe(true);
348 expect(buildModelOptions(unavailableConfig, null)).toEqual([]);
349 const [option] = buildModelOptions(unavailableConfig, selection);
350 expect(option).toMatchObject({
351 key: `google_work:${selection.model}`,
352 label: model.name,
353 subtitle: "Google",
354 providerKey: "google_work",
355 providerLabel: "Google Work",
356 providerDriver: "antigravity",
357 isDefault: false,
358 isLegacy: true,
359 isUnavailable: true,
360 capabilities: model.capabilities,
361 });
362 expect(option?.selection).toBe(selection);
363 });
364
365 it("keeps an exact selection when its model leaves and returns to the catalog", () => {
366 const changedConfig = {
367 ...config,
368 providers: config.providers.map((provider) => ({
369 ...provider,
370 models: provider.models.map((model) => ({ ...model, slug: "gemini-3.1-pro-low" })),
371 })),
372 };
373
374 expect(resolveDefaultableModelSelection(changedConfig, selection)).toBe(selection);
375 expect(isModelSelectionUnavailable(changedConfig, selection)).toBe(true);
376 const options = buildModelOptions(changedConfig, selection);
377 const missing = options.find((option) => option.selection.model === selection.model);
378 expect(missing).toMatchObject({
379 label: selection.model,
380 providerLabel: "Google Work",
381 providerDriver: "antigravity",
382 isUnavailable: true,
383 capabilities: null,
384 });
385 expect(missing?.selection).toBe(selection);
386 expect(
387 resolveNewTaskModelSelection({
388 draftSelection: null,
389 projectDefaultSelection: resolveDefaultableModelSelection(changedConfig, selection),
390 stickySelection: null,
391 modelOptions: options,
392 }),
393 ).toBe(selection);
394
395 const [restored] = buildModelOptions(config, selection);
396 expect(isModelSelectionUnavailable(config, selection)).toBe(false);
397 expect(restored?.isUnavailable).not.toBe(true);
398 expect(restored?.selection).toBe(selection);
399 expect(resolveDefaultableModelSelection(config, selection)).toBe(selection);
400 expect(buildModelOptions(config, null)[0]?.selection.options).toBeUndefined();
401 });
402
403 it("uses configured instance metadata when provider status is missing", () => {
404 const missingStatusConfig = {
405 providers: [],
406 settings: {
407 providerInstances: {
408 [selection.instanceId]: { driver: "antigravity", displayName: "Google Work" },
409 },
410 },
411 } as unknown as ServerConfig;
412
413 expect(resolveDefaultableModelSelection(missingStatusConfig, selection)).toBe(selection);
414 expect(isModelSelectionUnavailable(missingStatusConfig, selection)).toBe(true);
415 expect(buildModelOptions(missingStatusConfig, selection)).toMatchObject([
416 {
417 providerDriver: "antigravity",
418 providerLabel: "Google Work",
419 isUnavailable: true,
420 selection,
421 },
422 ]);
423 });
424
425 it("keeps offline selections without assuming that an unknown instance is Antigravity", () => {
426 const unknownConfig = { ...config, providers: [] };
427
428 expect(resolveDefaultableModelSelection(null, selection)).toBe(selection);
429 expect(isModelSelectionUnavailable(null, selection)).toBe(false);
430 expect(buildModelOptions(null, selection)[0]?.selection).toBe(selection);
431 expect(buildModelOptions(null, selection)[0]?.isUnavailable).not.toBe(true);
432 expect(isModelSelectionUnavailable(unknownConfig, selection)).toBe(false);
433 expect(resolveSelectableModelSelection(unknownConfig, selection)).toBeNull();
434 });
435 });
436
437 it("keeps legacy models out of implicit defaults", () => {
438 const config = {
439 providers: [
440 {
441 instanceId: "codex",
442 driver: "codex",
443 displayName: "Codex",
444 enabled: true,
445 installed: true,
446 auth: { status: "authenticated" },
447 models: [
448 { slug: "gpt-5.6-sol", name: "GPT-5.6 Sol", isCustom: false, capabilities: null },
449 {
450 slug: "gpt-5.4",
451 name: "GPT-5.4",
452 isCustom: false,
453 isLegacy: true,
454 capabilities: null,
455 },
456 ],
457 },
458 ],
459 } as unknown as ServerConfig;
460
461 const current = { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.6-sol" };
462 const legacy = { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" };
463
464 expect(resolveDefaultableModelSelection(config, current)).toBe(current);
465 // A legacy last-used selection falls through to the provider default.
466 expect(resolveDefaultableModelSelection(config, legacy)).toBeNull();
467 // Offline: nothing to validate against, selection passes through.
468 expect(resolveDefaultableModelSelection(null, legacy)).toBe(legacy);
469 });
470
471 it("resolves new tasks from draft, project, sticky, then provider defaults", () => {
472 const draft = { instanceId: ProviderInstanceId.make("codex"), model: "draft" };
473 const project = { instanceId: ProviderInstanceId.make("codex"), model: "project" };
474 const sticky = { instanceId: ProviderInstanceId.make("codex"), model: "sticky" };
475 const providerDefault = {
476 selection: { instanceId: ProviderInstanceId.make("codex"), model: "default" },
477 isDefault: true,
478 } as ModelOption;
479 const resolve = (
480 draftSelection: ModelSelection | null,
481 projectDefaultSelection: ModelSelection | null,
482 stickySelection: ModelSelection | null,
483 ) =>
484 resolveNewTaskModelSelection({
485 draftSelection,
486 projectDefaultSelection,
487 stickySelection,
488 modelOptions: [providerDefault],
489 });
490
491 expect(resolve(draft, project, sticky)).toBe(draft);
492 expect(resolve(null, project, sticky)).toBe(project);
493 expect(resolve(null, null, sticky)).toBe(sticky);
494 expect(resolve(null, null, null)).toBe(providerDefault.selection);
495
496 const unavailable = { ...providerDefault, isUnavailable: true };
497 expect(
498 resolveNewTaskModelSelection({
499 draftSelection: null,
500 projectDefaultSelection: null,
501 stickySelection: null,
502 modelOptions: [unavailable],
503 }),
504 ).toBeNull();
505 });
506});