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