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

modelOptions.test.ts

625 lines21.2 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 [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("Muse catalog changes", () => {
296 const selection: ModelSelection = {
297 instanceId: ProviderInstanceId.make("muse_work"),
298 model: "muse-spark-1.3-contributor",
299 options: [{ id: "reasoningEffort", value: "high" }],
300 };
301 const config = {
302 providers: [
303 {
304 instanceId: selection.instanceId,
305 driver: "muse",
306 displayName: "Muse Work",
307 enabled: true,
308 installed: true,
309 status: "ready",
310 auth: { status: "unknown" },
311 models: [
312 {
313 slug: selection.model,
314 name: "Muse Spark 1.3 Contributor",
315 isCustom: false,
316 isDefault: true,
317 capabilities: {
318 optionDescriptors: [
319 {
320 id: "reasoningEffort",
321 label: "Reasoning",
322 type: "select",
323 options: [
324 { id: "medium", label: "Medium", isDefault: true },
325 { id: "high", label: "High" },
326 ],
327 currentValue: "medium",
328 },
329 ],
330 },
331 },
332 ],
333 },
334 ],
335 } as unknown as ServerConfig;
336​
337 it.each([
338 ["disabled", { enabled: false }],
339 ["uninstalled", { installed: false }],
340 ["unavailable", { availability: "unavailable" }],
341 ["empty catalog", { models: [] }],
342 ] as const)(
343 "keeps a saved model unavailable during %s and restores it on recovery",
344 (_state, update) => {
345 const changedConfig = {
346 ...config,
347 providers: config.providers.map((provider) => ({ ...provider, ...update })),
348 };
349 expect(resolveSelectableModelSelection(changedConfig, selection)).toBe(selection);
350 expect(isModelSelectionUnavailable(changedConfig, selection)).toBe(true);
351 expect(buildModelOptions(changedConfig, null)).toEqual([]);
352 expect(buildModelOptions(changedConfig, selection)).toMatchObject([
353 {
354 providerKey: "muse_work",
355 providerDriver: "muse",
356 isUnavailable: true,
357 selection,
358 },
359 ]);
360​
361 const [restored] = buildModelOptions(config, selection);
362 expect(isModelSelectionUnavailable(config, selection)).toBe(false);
363 expect(restored?.isUnavailable).not.toBe(true);
364 expect(restored?.selection).toEqual(selection);
365 },
366 );
367​
368 it("permits a saved model retry after a failed probe until a ready catalog removes it", () => {
369 const failedProbeConfig = {
370 ...config,
371 providers: config.providers.map((provider) => ({
372 ...provider,
373 status: "error" as const,
374 models: [],
375 })),
376 };
377 expect(isModelSelectionUnavailable(failedProbeConfig, selection)).toBe(false);
378 expect(buildModelOptions(failedProbeConfig, selection)).toMatchObject([{ selection }]);
379 expect(buildModelOptions(failedProbeConfig, selection)[0]?.isUnavailable).not.toBe(true);
380​
381 const refreshedConfig = {
382 ...failedProbeConfig,
383 providers: failedProbeConfig.providers.map((provider) => ({
384 ...provider,
385 status: "ready" as const,
386 })),
387 };
388 expect(isModelSelectionUnavailable(refreshedConfig, selection)).toBe(true);
389 expect(buildModelOptions(refreshedConfig, selection)).toMatchObject([
390 { selection, isUnavailable: true },
391 ]);
392 });
393​
394 it("marks a model removed by refresh unavailable without changing the instance", () => {
395 const changedConfig = {
396 ...config,
397 providers: config.providers.map((provider) => ({
398 ...provider,
399 models: provider.models.map((model) => ({ ...model, slug: "muse-other-model" })),
400 })),
401 };
402 const options = buildModelOptions(changedConfig, selection);
403 expect(options.find((option) => option.selection.model === selection.model)).toMatchObject({
404 providerKey: "muse_work",
405 isUnavailable: true,
406 selection,
407 });
408 expect(
409 options.find((option) => option.selection.model === "muse-other-model")?.isUnavailable,
410 ).not.toBe(true);
411 });
412 });
413​
414 describe("Antigravity selections", () => {
415 const selection = {
416 instanceId: ProviderInstanceId.make("google_work"),
417 model: "gemini-3.1-pro-high",
418 options: [{ id: "native-option", value: "saved/opaque-choice" }],
419 };
420 const model = {
421 slug: selection.model,
422 name: "Gemini 3.1 Pro High",
423 subProvider: "Google",
424 isCustom: false,
425 isDefault: true,
426 isLegacy: true,
427 capabilities: {
428 optionDescriptors: [
429 {
430 id: "native-option",
431 label: "Native option",
432 type: "select",
433 options: [{ id: "current/default", label: "Default", isDefault: true }],
434 currentValue: "current/default",
435 },
436 ],
437 },
438 };
439 const config = {
440 providers: [
441 {
442 instanceId: selection.instanceId,
443 driver: "antigravity",
444 displayName: "Google Work",
445 enabled: true,
446 installed: true,
447 auth: { status: "authenticated" },
448 models: [model],
449 },
450 ],
451 } as unknown as ServerConfig;
452​
453 it.each([
454 ["disabled", { enabled: false }],
455 ["uninstalled", { installed: false }],
456 ["signed out", { auth: { status: "unauthenticated" } }],
457 ["unavailable", { availability: "unavailable" }],
458 ] as const)("keeps a %s provider's selection and known model details", (_state, update) => {
459 const unavailableConfig = {
460 ...config,
461 providers: config.providers.map((provider) => ({ ...provider, ...update })),
462 };
463​
464 expect(resolveSelectableModelSelection(unavailableConfig, selection)).toBe(selection);
465 expect(resolveDefaultableModelSelection(unavailableConfig, selection)).toBe(selection);
466 expect(isModelSelectionUnavailable(unavailableConfig, selection)).toBe(true);
467 expect(buildModelOptions(unavailableConfig, null)).toEqual([]);
468 const [option] = buildModelOptions(unavailableConfig, selection);
469 expect(option).toMatchObject({
470 key: `google_work:${selection.model}`,
471 label: model.name,
472 subtitle: "Google",
473 providerKey: "google_work",
474 providerLabel: "Google Work",
475 providerDriver: "antigravity",
476 isDefault: false,
477 isLegacy: true,
478 isUnavailable: true,
479 capabilities: model.capabilities,
480 });
481 expect(option?.selection).toBe(selection);
482 });
483​
484 it("keeps an exact selection when its model leaves and returns to the catalog", () => {
485 const changedConfig = {
486 ...config,
487 providers: config.providers.map((provider) => ({
488 ...provider,
489 models: provider.models.map((model) => ({ ...model, slug: "gemini-3.1-pro-low" })),
490 })),
491 };
492​
493 expect(resolveDefaultableModelSelection(changedConfig, selection)).toBe(selection);
494 expect(isModelSelectionUnavailable(changedConfig, selection)).toBe(true);
495 const options = buildModelOptions(changedConfig, selection);
496 const missing = options.find((option) => option.selection.model === selection.model);
497 expect(missing).toMatchObject({
498 label: selection.model,
499 providerLabel: "Google Work",
500 providerDriver: "antigravity",
501 isUnavailable: true,
502 capabilities: null,
503 });
504 expect(missing?.selection).toBe(selection);
505 expect(
506 resolveNewTaskModelSelection({
507 draftSelection: null,
508 projectDefaultSelection: resolveDefaultableModelSelection(changedConfig, selection),
509 stickySelection: null,
510 modelOptions: options,
511 }),
512 ).toBe(selection);
513​
514 const [restored] = buildModelOptions(config, selection);
515 expect(isModelSelectionUnavailable(config, selection)).toBe(false);
516 expect(restored?.isUnavailable).not.toBe(true);
517 expect(restored?.selection).toBe(selection);
518 expect(resolveDefaultableModelSelection(config, selection)).toBe(selection);
519 expect(buildModelOptions(config, null)[0]?.selection.options).toBeUndefined();
520 });
521​
522 it("uses configured instance metadata when provider status is missing", () => {
523 const missingStatusConfig = {
524 providers: [],
525 settings: {
526 providerInstances: {
527 [selection.instanceId]: { driver: "antigravity", displayName: "Google Work" },
528 },
529 },
530 } as unknown as ServerConfig;
531​
532 expect(resolveDefaultableModelSelection(missingStatusConfig, selection)).toBe(selection);
533 expect(isModelSelectionUnavailable(missingStatusConfig, selection)).toBe(true);
534 expect(buildModelOptions(missingStatusConfig, selection)).toMatchObject([
535 {
536 providerDriver: "antigravity",
537 providerLabel: "Google Work",
538 isUnavailable: true,
539 selection,
540 },
541 ]);
542 });
543​
544 it("keeps offline selections without assuming that an unknown instance is Antigravity", () => {
545 const unknownConfig = { ...config, providers: [] };
546​
547 expect(resolveDefaultableModelSelection(null, selection)).toBe(selection);
548 expect(isModelSelectionUnavailable(null, selection)).toBe(false);
549 expect(buildModelOptions(null, selection)[0]?.selection).toBe(selection);
550 expect(buildModelOptions(null, selection)[0]?.isUnavailable).not.toBe(true);
551 expect(isModelSelectionUnavailable(unknownConfig, selection)).toBe(false);
552 expect(resolveSelectableModelSelection(unknownConfig, selection)).toBeNull();
553 });
554 });
555​
556 it("keeps legacy models out of implicit defaults", () => {
557 const config = {
558 providers: [
559 {
560 instanceId: "codex",
561 driver: "codex",
562 displayName: "Codex",
563 enabled: true,
564 installed: true,
565 auth: { status: "authenticated" },
566 models: [
567 { slug: "gpt-5.6-sol", name: "GPT-5.6 Sol", isCustom: false, capabilities: null },
568 {
569 slug: "gpt-5.4",
570 name: "GPT-5.4",
571 isCustom: false,
572 isLegacy: true,
573 capabilities: null,
574 },
575 ],
576 },
577 ],
578 } as unknown as ServerConfig;
579​
580 const current = { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.6-sol" };
581 const legacy = { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" };
582​
583 expect(resolveDefaultableModelSelection(config, current)).toBe(current);
584 // A legacy last-used selection falls through to the provider default.
585 expect(resolveDefaultableModelSelection(config, legacy)).toBeNull();
586 // Offline: nothing to validate against, selection passes through.
587 expect(resolveDefaultableModelSelection(null, legacy)).toBe(legacy);
588 });
589​
590 it("resolves new tasks from draft, project, sticky, then provider defaults", () => {
591 const draft = { instanceId: ProviderInstanceId.make("codex"), model: "draft" };
592 const project = { instanceId: ProviderInstanceId.make("codex"), model: "project" };
593 const sticky = { instanceId: ProviderInstanceId.make("codex"), model: "sticky" };
594 const providerDefault = {
595 selection: { instanceId: ProviderInstanceId.make("codex"), model: "default" },
596 isDefault: true,
597 } as ModelOption;
598 const resolve = (
599 draftSelection: ModelSelection | null,
600 projectDefaultSelection: ModelSelection | null,
601 stickySelection: ModelSelection | null,
602 ) =>
603 resolveNewTaskModelSelection({
604 draftSelection,
605 projectDefaultSelection,
606 stickySelection,
607 modelOptions: [providerDefault],
608 });
609​
610 expect(resolve(draft, project, sticky)).toBe(draft);
611 expect(resolve(null, project, sticky)).toBe(project);
612 expect(resolve(null, null, sticky)).toBe(sticky);
613 expect(resolve(null, null, null)).toBe(providerDefault.selection);
614​
615 const unavailable = { ...providerDefault, isUnavailable: true };
616 expect(
617 resolveNewTaskModelSelection({
618 draftSelection: null,
619 projectDefaultSelection: null,
620 stickySelection: null,
621 modelOptions: [unavailable],
622 }),
623 ).toBeNull();
624 });
625});