Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 DEFAULT_SERVER_SETTINGS,
5 ProviderInstanceId,
6 type ModelSelection,
7 type ServerConfig,
8} from "@t3tools/contracts";
9
10import {
11 buildModelOptions,
12 groupByProvider,
13 isModelSelectionUnavailable,
14 resolveDefaultableModelSelection,
15 resolveNewTaskModelSelection,
16 resolveSelectableModelSelection,
17 type ModelOption,
18} from "./modelOptions";
19
20describe("mobile model options", () => {
21 it("groups models by provider and flags legacy entries", () => {
22 const config = {
23 providers: [
24 {
25 instanceId: "codex",
26 driver: "codex",
27 displayName: "Codex",
28 enabled: true,
29 installed: true,
30 auth: { status: "authenticated" },
31 models: [
32 {
33 slug: "gpt-5.6-sol",
34 name: "GPT-5.6 Sol",
35 isCustom: false,
36 capabilities: null,
37 },
38 {
39 slug: "gpt-5.4",
40 name: "GPT-5.4",
41 isCustom: false,
42 isLegacy: true,
43 capabilities: null,
44 },
45 ],
46 },
47 ],
48 } as unknown as ServerConfig;
49
50 expect(groupByProvider(buildModelOptions(config, null))).toMatchObject([
51 {
52 providerKey: "codex",
53 providerLabel: "Codex",
54 models: [
55 { key: "codex:gpt-5.6-sol", label: "GPT-5.6 Sol", subtitle: "", isLegacy: false },
56 { key: "codex:gpt-5.4", label: "GPT-5.4", isLegacy: true },
57 ],
58 },
59 ]);
60 });
61
62 it("carries configured ACP identity into model and provider catalogs", () => {
63 const iconUrl = "https://cdn.agentclientprotocol.com/registry/v1/latest/antigravity-acp.svg";
64 const config = {
65 providers: [
66 {
67 instanceId: "acpRegistry_antigravity",
68 driver: "acpRegistry",
69 displayName: "Antigravity",
70 iconUrl,
71 enabled: true,
72 installed: true,
73 auth: { status: "authenticated" },
74 models: [
75 {
76 slug: "default",
77 name: "Default",
78 isCustom: false,
79 capabilities: null,
80 },
81 ],
82 },
83 ],
84 } as unknown as ServerConfig;
85
86 const [group] = groupByProvider(buildModelOptions(config, null));
87
88 expect(group).toMatchObject({
89 providerKey: "acpRegistry_antigravity",
90 providerLabel: "Antigravity",
91 models: [
92 {
93 providerDriver: "acpRegistry",
94 providerIconUrl: iconUrl,
95 },
96 ],
97 });
98 });
99
100 it("distinguishes same-name OpenCode models without changing their routing", () => {
101 const sources = [
102 { id: "anthropic", label: "Anthropic" },
103 { id: "github-copilot", label: "GitHub Copilot" },
104 { id: "opencode", label: "OpenCode Zen" },
105 ];
106 const config = {
107 providers: [
108 {
109 instanceId: "opencode_work",
110 driver: "opencode",
111 displayName: "OpenCode Work",
112 enabled: true,
113 installed: true,
114 auth: { status: "authenticated" },
115 models: sources.map((source) => ({
116 slug: `${source.id}/claude-fable-5`,
117 name: "Claude Fable 5",
118 subProvider: source.label,
119 isCustom: false,
120 capabilities: null,
121 })),
122 },
123 ],
124 } as unknown as ServerConfig;
125 const selection = {
126 instanceId: ProviderInstanceId.make("opencode_work"),
127 model: "github-copilot/claude-fable-5",
128 };
129
130 const options = buildModelOptions(config, selection);
131
132 expect(options).toMatchObject(
133 sources.map((source) => ({
134 key: `opencode_work:${source.id}/claude-fable-5`,
135 label: "Claude Fable 5",
136 subtitle: source.label,
137 providerLabel: "OpenCode Work",
138 selection: {
139 instanceId: "opencode_work",
140 model: `${source.id}/claude-fable-5`,
141 },
142 })),
143 );
144 expect(groupByProvider(options)).toEqual([
145 { providerKey: "opencode_work", providerLabel: "OpenCode Work", models: options },
146 ]);
147 });
148
149 it("does not materialize catalog defaults for missing stored options", () => {
150 const config = {
151 providers: [
152 {
153 instanceId: "codex",
154 driver: "codex",
155 displayName: "Codex",
156 enabled: true,
157 installed: true,
158 auth: { status: "authenticated" },
159 models: [
160 {
161 slug: "gpt-test",
162 name: "GPT Test",
163 isCustom: false,
164 capabilities: {
165 optionDescriptors: [
166 {
167 id: "serviceTier",
168 label: "Service Tier",
169 type: "select",
170 options: [
171 { id: "default", label: "Standard", isDefault: true },
172 { id: "priority", label: "Fast" },
173 ],
174 currentValue: "default",
175 },
176 ],
177 },
178 },
179 ],
180 },
181 ],
182 } as unknown as ServerConfig;
183
184 const [option] = buildModelOptions(config, {
185 instanceId: ProviderInstanceId.make("codex"),
186 model: "gpt-test",
187 });
188
189 expect(option?.capabilities?.optionDescriptors?.[0]?.id).toBe("serviceTier");
190 expect(option?.selection.options).toBeUndefined();
191
192 const [emptyOption] = buildModelOptions(config, {
193 instanceId: ProviderInstanceId.make("codex"),
194 model: "gpt-test",
195 options: [],
196 });
197 expect(emptyOption?.selection).toEqual(option?.selection);
198
199 const [explicitOption] = buildModelOptions(config, {
200 instanceId: ProviderInstanceId.make("codex"),
201 model: "gpt-test",
202 options: [{ id: "serviceTier", value: "priority" }],
203 });
204 expect(explicitOption?.selection.options).toEqual([{ id: "serviceTier", value: "priority" }]);
205 });
206
207 it("limits existing threads to their provider while new tasks keep every provider", () => {
208 const providers = ["codex", "claudeAgent"].map((instanceId) => ({
209 instanceId,
210 driver: instanceId,
211 enabled: true,
212 installed: true,
213 auth: { status: "authenticated" },
214 models: [{ slug: "test", name: instanceId, capabilities: null }],
215 }));
216 const config = { providers } as unknown as ServerConfig;
217 const selection = { instanceId: ProviderInstanceId.make("codex"), model: "test" };
218
219 expect(buildModelOptions(config, selection).map((option) => option.providerKey)).toEqual([
220 "codex",
221 "claudeAgent",
222 ]);
223 expect(buildModelOptions(config, selection, selection.instanceId)).toEqual(
224 buildModelOptions(config, selection).filter((option) => option.providerKey === "codex"),
225 );
226 });
227
228 it.each(["disabled", "unavailable", "missing"] as const)(
229 "retains the selected %s provider's fallback in a filtered catalog",
230 (state) => {
231 const selection = {
232 instanceId: ProviderInstanceId.make("google_work"),
233 model: "saved-model",
234 options: [{ id: "native-option", value: "saved-choice" }],
235 };
236 const provider = {
237 instanceId: selection.instanceId,
238 driver: "antigravity",
239 displayName: "Google Work",
240 enabled: state !== "disabled",
241 installed: true,
242 availability: state === "unavailable" ? "unavailable" : "available",
243 auth: { status: "authenticated" },
244 models: [{ slug: selection.model, name: "Saved model", capabilities: null }],
245 };
246 const config = {
247 providers: state === "missing" ? [] : [provider],
248 settings: { providerInstances: { google_work: { driver: "antigravity" } } },
249 } as unknown as ServerConfig;
250 const options = buildModelOptions(config, selection, selection.instanceId);
251 expect(options).toEqual(buildModelOptions(config, selection));
252 expect(options).toHaveLength(1);
253 expect(options[0]).toMatchObject({ selection, isUnavailable: true });
254 },
255 );
256
257 it("rejects stored selections whose provider is not usable", () => {
258 const config = {
259 providers: [
260 {
261 instanceId: "codex",
262 driver: "codex",
263 enabled: true,
264 installed: true,
265 auth: { status: "authenticated" },
266 models: [],
267 },
268 {
269 instanceId: "claudeAgent",
270 driver: "claudeAgent",
271 enabled: false,
272 installed: true,
273 auth: { status: "authenticated" },
274 models: [],
275 },
276 ],
277 } as unknown as ServerConfig;
278
279 const usable = {
280 instanceId: ProviderInstanceId.make("codex"),
281 model: "gpt-5.6-sol",
282 };
283 const disabled = {
284 instanceId: ProviderInstanceId.make("claudeAgent"),
285 model: "claude-sonnet-5",
286 };
287 const removed = {
288 instanceId: ProviderInstanceId.make("codex_personal"),
289 model: "gpt-5.6-sol",
290 };
291
292 expect(resolveSelectableModelSelection(config, usable)).toBe(usable);
293 expect(resolveSelectableModelSelection(config, disabled)).toBeNull();
294 expect(resolveSelectableModelSelection(config, removed)).toBeNull();
295 expect(isModelSelectionUnavailable(config, disabled)).toBe(false);
296 // An offline environment has no config to validate.
297 expect(resolveSelectableModelSelection(null, disabled)).toBe(disabled);
298 });
299
300 describe("Antigravity selections", () => {
301 const selection = {
302 instanceId: ProviderInstanceId.make("google_work"),
303 model: "gemini-3.1-pro-high",
304 options: [{ id: "native-option", value: "saved/opaque-choice" }],
305 };
306 const model = {
307 slug: selection.model,
308 name: "Gemini 3.1 Pro High",
309 subProvider: "Google",
310 isCustom: false,
311 isDefault: true,
312 isLegacy: true,
313 capabilities: {
314 optionDescriptors: [
315 {
316 id: "native-option",
317 label: "Native option",
318 type: "select",
319 options: [{ id: "current/default", label: "Default", isDefault: true }],
320 currentValue: "current/default",
321 },
322 ],
323 },
324 };
325 const config = {
326 providers: [
327 {
328 instanceId: selection.instanceId,
329 driver: "antigravity",
330 displayName: "Google Work",
331 enabled: true,
332 installed: true,
333 auth: { status: "authenticated" },
334 models: [model],
335 },
336 ],
337 } as unknown as ServerConfig;
338
339 it.each([
340 ["disabled", { enabled: false }],
341 ["uninstalled", { installed: false }],
342 ["signed out", { auth: { status: "unauthenticated" } }],
343 ["unavailable", { availability: "unavailable" }],
344 ] as const)("keeps a %s provider's selection and known model details", (_state, update) => {
345 const unavailableConfig = {
346 ...config,
347 providers: config.providers.map((provider) => ({ ...provider, ...update })),
348 };
349
350 expect(resolveSelectableModelSelection(unavailableConfig, selection)).toBe(selection);
351 expect(resolveDefaultableModelSelection(unavailableConfig, selection)).toBe(selection);
352 expect(isModelSelectionUnavailable(unavailableConfig, selection)).toBe(true);
353 expect(buildModelOptions(unavailableConfig, null)).toEqual([]);
354 const [option] = buildModelOptions(unavailableConfig, selection);
355 expect(option).toMatchObject({
356 key: `google_work:${selection.model}`,
357 label: model.name,
358 subtitle: "Google",
359 providerKey: "google_work",
360 providerLabel: "Google Work",
361 providerDriver: "antigravity",
362 isDefault: false,
363 isLegacy: true,
364 isUnavailable: true,
365 capabilities: model.capabilities,
366 });
367 expect(option?.selection).toBe(selection);
368 });
369
370 it("keeps an exact selection when its model leaves and returns to the catalog", () => {
371 const changedConfig = {
372 ...config,
373 providers: config.providers.map((provider) => ({
374 ...provider,
375 models: provider.models.map((model) => ({ ...model, slug: "gemini-3.1-pro-low" })),
376 })),
377 };
378
379 expect(resolveDefaultableModelSelection(changedConfig, selection)).toBe(selection);
380 expect(isModelSelectionUnavailable(changedConfig, selection)).toBe(true);
381 const options = buildModelOptions(changedConfig, selection);
382 const missing = options.find((option) => option.selection.model === selection.model);
383 expect(missing).toMatchObject({
384 label: selection.model,
385 providerLabel: "Google Work",
386 providerDriver: "antigravity",
387 isUnavailable: true,
388 capabilities: null,
389 });
390 expect(missing?.selection).toBe(selection);
391 expect(
392 resolveNewTaskModelSelection({
393 draftSelection: null,
394 projectDefaultSelection: resolveDefaultableModelSelection(changedConfig, selection),
395 stickySelection: null,
396 modelOptions: options,
397 }),
398 ).toBe(selection);
399
400 const [restored] = buildModelOptions(config, selection);
401 expect(isModelSelectionUnavailable(config, selection)).toBe(false);
402 expect(restored?.isUnavailable).not.toBe(true);
403 expect(restored?.selection).toBe(selection);
404 expect(resolveDefaultableModelSelection(config, selection)).toBe(selection);
405 expect(buildModelOptions(config, null)[0]?.selection.options).toBeUndefined();
406 });
407
408 it("uses configured instance metadata when provider status is missing", () => {
409 const missingStatusConfig = {
410 providers: [],
411 settings: {
412 providerInstances: {
413 [selection.instanceId]: { driver: "antigravity", displayName: "Google Work" },
414 },
415 },
416 } as unknown as ServerConfig;
417
418 expect(resolveDefaultableModelSelection(missingStatusConfig, selection)).toBe(selection);
419 expect(isModelSelectionUnavailable(missingStatusConfig, selection)).toBe(true);
420 expect(buildModelOptions(missingStatusConfig, selection)).toMatchObject([
421 {
422 providerDriver: "antigravity",
423 providerLabel: "Google Work",
424 isUnavailable: true,
425 selection,
426 },
427 ]);
428 });
429
430 it("keeps offline selections without assuming that an unknown instance is Antigravity", () => {
431 const unknownConfig = { ...config, providers: [] };
432
433 expect(resolveDefaultableModelSelection(null, selection)).toBe(selection);
434 expect(isModelSelectionUnavailable(null, selection)).toBe(false);
435 expect(buildModelOptions(null, selection)[0]?.selection).toBe(selection);
436 expect(buildModelOptions(null, selection)[0]?.isUnavailable).not.toBe(true);
437 expect(isModelSelectionUnavailable(unknownConfig, selection)).toBe(false);
438 expect(resolveSelectableModelSelection(unknownConfig, selection)).toBeNull();
439 });
440 });
441
442 it("keeps legacy models out of implicit defaults", () => {
443 const config = {
444 providers: [
445 {
446 instanceId: "codex",
447 driver: "codex",
448 displayName: "Codex",
449 enabled: true,
450 installed: true,
451 auth: { status: "authenticated" },
452 models: [
453 { slug: "gpt-5.6-sol", name: "GPT-5.6 Sol", isCustom: false, capabilities: null },
454 {
455 slug: "gpt-5.4",
456 name: "GPT-5.4",
457 isCustom: false,
458 isLegacy: true,
459 capabilities: null,
460 },
461 ],
462 },
463 ],
464 } as unknown as ServerConfig;
465
466 const current = { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.6-sol" };
467 const legacy = { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" };
468
469 expect(resolveDefaultableModelSelection(config, current)).toBe(current);
470 // A legacy last-used selection falls through to the provider default.
471 expect(resolveDefaultableModelSelection(config, legacy)).toBeNull();
472 // Offline: nothing to validate against, selection passes through.
473 expect(resolveDefaultableModelSelection(null, legacy)).toBe(legacy);
474 });
475
476 it("resolves new tasks from draft, project, sticky, then provider defaults", () => {
477 const draft = { instanceId: ProviderInstanceId.make("codex"), model: "draft" };
478 const project = { instanceId: ProviderInstanceId.make("codex"), model: "project" };
479 const sticky = { instanceId: ProviderInstanceId.make("codex"), model: "sticky" };
480 const providerDefault = {
481 selection: { instanceId: ProviderInstanceId.make("codex"), model: "default" },
482 isDefault: true,
483 } as ModelOption;
484 const resolve = (
485 draftSelection: ModelSelection | null,
486 projectDefaultSelection: ModelSelection | null,
487 stickySelection: ModelSelection | null,
488 ) =>
489 resolveNewTaskModelSelection({
490 draftSelection,
491 projectDefaultSelection,
492 stickySelection,
493 modelOptions: [providerDefault],
494 });
495
496 expect(resolve(draft, project, sticky)).toBe(draft);
497 expect(resolve(null, project, sticky)).toBe(project);
498 expect(resolve(null, null, sticky)).toBe(sticky);
499 expect(resolve(null, null, null)).toBe(providerDefault.selection);
500
501 const unavailable = { ...providerDefault, isUnavailable: true };
502 expect(
503 resolveNewTaskModelSelection({
504 draftSelection: null,
505 projectDefaultSelection: null,
506 stickySelection: null,
507 modelOptions: [unavailable],
508 }),
509 ).toBeNull();
510 });
511});
512
513describe("synchronized mobile model visibility", () => {
514 const provider = ProviderInstanceId.make("codex_work");
515 const other = ProviderInstanceId.make("codex_personal");
516 const models = [
517 { slug: "hidden", name: "Hidden", isCustom: false, capabilities: null },
518 { slug: "first", name: "First", isCustom: false, capabilities: null },
519 { slug: "second", name: "Second", isCustom: false, capabilities: null },
520 { slug: "custom", name: "Custom", isCustom: true, capabilities: null },
521 ];
522 const config = {
523 settings: {
524 ...DEFAULT_SERVER_SETTINGS,
525 providerModelPreferences: {
526 [provider]: {
527 hiddenModels: ["hidden", "custom", "missing"],
528 modelOrder: ["second", "first"],
529 },
530 },
531 },
532 providers: [provider, other].map((instanceId) => ({
533 instanceId,
534 driver: "codex",
535 enabled: true,
536 installed: true,
537 auth: { status: "authenticated" },
538 models,
539 })),
540 } as unknown as ServerConfig;
541
542 it("applies server visibility and ordering independently to each provider instance", () => {
543 const options = buildModelOptions(config, null);
544 expect(
545 options
546 .filter((option) => option.providerKey === provider)
547 .map((option) => option.selection.model),
548 ).toEqual(["second", "first", "custom"]);
549 expect(
550 options
551 .filter((option) => option.providerKey === other)
552 .map((option) => option.selection.model),
553 ).toEqual(["hidden", "first", "second", "custom"]);
554 });
555
556 it("exposes hidden built-ins for management without offering custom visibility controls", () => {
557 const options = buildModelOptions(
558 config,
559 { instanceId: provider, model: "hidden" },
560 provider,
561 true,
562 );
563 expect(options.map((option) => option.selection.model)).toEqual([
564 "second",
565 "first",
566 "hidden",
567 "custom",
568 ]);
569 expect(options.find((option) => option.selection.model === "hidden")).toMatchObject({
570 isHidden: true,
571 canHide: true,
572 });
573 expect(options.find((option) => option.selection.model === "custom")).toMatchObject({
574 isHidden: false,
575 canHide: false,
576 });
577 });
578
579 it.each(["hidden", "missing"])("does not resurrect the hidden fallback %s", (model) => {
580 expect(
581 buildModelOptions(config, { instanceId: provider, model }, provider).map(
582 (option) => option.selection.model,
583 ),
584 ).toEqual(["second", "first", "custom"]);
585 });
586
587 it("keeps custom models visible and restores built-in models when the server clears hidden models", () => {
588 expect(
589 buildModelOptions(config, { instanceId: provider, model: "custom" }, provider).map(
590 (option) => option.selection.model,
591 ),
592 ).toContain("custom");
593 const cleared = { ...config, settings: { ...config.settings, providerModelPreferences: {} } };
594 expect(
595 buildModelOptions(cleared, null, provider).map((option) => option.selection.model),
596 ).toEqual(["hidden", "first", "second", "custom"]);
597 });
598});