The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { |
| 4 | DEFAULT_SERVER_SETTINGS, |
| 5 | ProjectId, |
| 6 | ProviderDriverKind, |
| 7 | ProviderInstanceId, |
| 8 | type ModelSelection, |
| 9 | type ServerConfig, |
| 10 | type ServerSettings, |
| 11 | } from "@t3tools/contracts"; |
| 12 | |
| 13 | import { |
| 14 | buildModelOptions, |
| 15 | describeUnavailableModelSelection, |
| 16 | groupByProvider, |
| 17 | isModelSelectionUnavailable, |
| 18 | resolveDefaultableModelSelection, |
| 19 | resolveNewTaskModelSelection, |
| 20 | resolveSelectableModelSelection, |
| 21 | withStoredThreadModelOptions, |
| 22 | type ModelOption, |
| 23 | } from "./modelOptions"; |
| 24 | import { groupModelsBySubProvider } from "@t3tools/client-runtime/providerSubProviders"; |
| 25 | |
| 26 | describe("mobile model options", () => { |
| 27 | it("distinguishes same-name OpenCode models without changing their routing", () => { |
| 28 | const sources = [ |
| 29 | { id: "anthropic", label: "Anthropic" }, |
| 30 | { id: "github-copilot", label: "GitHub Copilot" }, |
| 31 | { id: "opencode", label: "OpenCode Zen" }, |
| 32 | ]; |
| 33 | const config = { |
| 34 | providers: [ |
| 35 | { |
| 36 | instanceId: "opencode_work", |
| 37 | driver: "opencode", |
| 38 | displayName: "OpenCode Work", |
| 39 | enabled: true, |
| 40 | installed: true, |
| 41 | auth: { status: "authenticated" }, |
| 42 | models: sources.map((source) => ({ |
| 43 | slug: `${source.id}/claude-fable-5`, |
| 44 | name: "Claude Fable 5", |
| 45 | subProvider: source.label, |
| 46 | isCustom: false, |
| 47 | capabilities: null, |
| 48 | })), |
| 49 | }, |
| 50 | ], |
| 51 | } as unknown as ServerConfig; |
| 52 | const selection = { |
| 53 | instanceId: ProviderInstanceId.make("opencode_work"), |
| 54 | model: "github-copilot/claude-fable-5", |
| 55 | }; |
| 56 | |
| 57 | const options = buildModelOptions(config, selection, undefined, null); |
| 58 | |
| 59 | expect(options).toMatchObject( |
| 60 | sources.map((source) => ({ |
| 61 | key: `opencode_work:${source.id}/claude-fable-5`, |
| 62 | label: "Claude Fable 5", |
| 63 | subtitle: source.label, |
| 64 | providerLabel: "OpenCode Work", |
| 65 | selection: { |
| 66 | instanceId: "opencode_work", |
| 67 | model: `${source.id}/claude-fable-5`, |
| 68 | }, |
| 69 | })), |
| 70 | ); |
| 71 | expect(groupByProvider(options)).toEqual([ |
| 72 | { providerKey: "opencode_work", providerLabel: "OpenCode Work", models: options }, |
| 73 | ]); |
| 74 | }); |
| 75 | |
| 76 | it("groups fx's models under its backends, keeping a signed-out backend", () => { |
| 77 | const subProviders = [ |
| 78 | { name: "Codex subscription" }, |
| 79 | { name: "Grok subscription", unavailableReason: "Signed out." }, |
| 80 | ]; |
| 81 | const config = { |
| 82 | providers: [ |
| 83 | { |
| 84 | instanceId: "fx", |
| 85 | driver: "fx", |
| 86 | displayName: "fx", |
| 87 | enabled: true, |
| 88 | installed: true, |
| 89 | auth: { status: "authenticated" }, |
| 90 | subProviders, |
| 91 | models: [ |
| 92 | { slug: "default", name: "Default (gpt-5.5)", isCustom: false, capabilities: null }, |
| 93 | { |
| 94 | slug: "codex:gpt-5.5", |
| 95 | name: "gpt-5.5", |
| 96 | subProvider: "Codex subscription", |
| 97 | isCustom: false, |
| 98 | capabilities: null, |
| 99 | }, |
| 100 | ], |
| 101 | }, |
| 102 | ], |
| 103 | } as unknown as ServerConfig; |
| 104 | |
| 105 | const [group] = groupByProvider(buildModelOptions(config, null, undefined, null)); |
| 106 | expect(group?.subProviders).toEqual(subProviders); |
| 107 | const { ungrouped, groups } = groupModelsBySubProvider( |
| 108 | group?.models ?? [], |
| 109 | group?.subProviders ?? [], |
| 110 | (option) => option.subtitle, |
| 111 | ); |
| 112 | expect(ungrouped.map((option) => option.key)).toEqual(["fx:default"]); |
| 113 | expect(groups.map(({ subProvider, models }) => [subProvider.name, models.length])).toEqual([ |
| 114 | ["Codex subscription", 1], |
| 115 | ["Grok subscription", 0], |
| 116 | ]); |
| 117 | }); |
| 118 | |
| 119 | it("does not materialize catalog defaults for missing stored options", () => { |
| 120 | const config = { |
| 121 | providers: [ |
| 122 | { |
| 123 | instanceId: "codex", |
| 124 | driver: "codex", |
| 125 | displayName: "Codex", |
| 126 | enabled: true, |
| 127 | installed: true, |
| 128 | auth: { status: "authenticated" }, |
| 129 | models: [ |
| 130 | { |
| 131 | slug: "gpt-test", |
| 132 | name: "GPT Test", |
| 133 | isCustom: false, |
| 134 | capabilities: { |
| 135 | optionDescriptors: [ |
| 136 | { |
| 137 | id: "serviceTier", |
| 138 | label: "Service Tier", |
| 139 | type: "select", |
| 140 | options: [ |
| 141 | { id: "default", label: "Standard", isDefault: true }, |
| 142 | { id: "priority", label: "Fast" }, |
| 143 | ], |
| 144 | currentValue: "default", |
| 145 | }, |
| 146 | ], |
| 147 | }, |
| 148 | }, |
| 149 | ], |
| 150 | }, |
| 151 | ], |
| 152 | } as unknown as ServerConfig; |
| 153 | |
| 154 | const [option] = buildModelOptions( |
| 155 | config, |
| 156 | { |
| 157 | instanceId: ProviderInstanceId.make("codex"), |
| 158 | model: "gpt-test", |
| 159 | }, |
| 160 | undefined, |
| 161 | null, |
| 162 | ); |
| 163 | |
| 164 | expect(option?.capabilities?.optionDescriptors?.[0]?.id).toBe("serviceTier"); |
| 165 | expect(option?.selection.options).toBeUndefined(); |
| 166 | |
| 167 | const [emptyOption] = buildModelOptions( |
| 168 | config, |
| 169 | { |
| 170 | instanceId: ProviderInstanceId.make("codex"), |
| 171 | model: "gpt-test", |
| 172 | options: [], |
| 173 | }, |
| 174 | undefined, |
| 175 | null, |
| 176 | ); |
| 177 | expect(emptyOption?.selection).toEqual(option?.selection); |
| 178 | |
| 179 | const [explicitOption] = buildModelOptions( |
| 180 | config, |
| 181 | { |
| 182 | instanceId: ProviderInstanceId.make("codex"), |
| 183 | model: "gpt-test", |
| 184 | options: [{ id: "serviceTier", value: "priority" }], |
| 185 | }, |
| 186 | undefined, |
| 187 | null, |
| 188 | ); |
| 189 | expect(explicitOption?.selection.options).toEqual([{ id: "serviceTier", value: "priority" }]); |
| 190 | }); |
| 191 | |
| 192 | it("carries a thread's stored options onto an optionless pick of its own model", () => { |
| 193 | const claude = ProviderInstanceId.make("claudeAgent"); |
| 194 | const stored: ModelSelection = { |
| 195 | instanceId: claude, |
| 196 | model: "claude-opus-5-5", |
| 197 | options: [{ id: "effort", value: "high" }], |
| 198 | }; |
| 199 | expect( |
| 200 | withStoredThreadModelOptions({ instanceId: claude, model: stored.model }, stored), |
| 201 | ).toEqual(stored); |
| 202 | const remembered: ModelSelection = { ...stored, options: [{ id: "effort", value: "low" }] }; |
| 203 | expect(withStoredThreadModelOptions(remembered, stored)).toBe(remembered); |
| 204 | const otherInstance: ModelSelection = { |
| 205 | instanceId: ProviderInstanceId.make("claude_work"), |
| 206 | model: stored.model, |
| 207 | }; |
| 208 | expect(withStoredThreadModelOptions(otherInstance, stored)).toBe(otherInstance); |
| 209 | const otherModel: ModelSelection = { instanceId: claude, model: "claude-sonnet-5-5" }; |
| 210 | expect(withStoredThreadModelOptions(otherModel, stored)).toBe(otherModel); |
| 211 | }); |
| 212 | |
| 213 | it("limits existing threads to their provider while new tasks keep every provider", () => { |
| 214 | const providers = ["codex", "claudeAgent"].map((instanceId) => ({ |
| 215 | instanceId, |
| 216 | driver: instanceId, |
| 217 | enabled: true, |
| 218 | installed: true, |
| 219 | auth: { status: "authenticated" }, |
| 220 | models: [{ slug: "test", name: instanceId, capabilities: null }], |
| 221 | })); |
| 222 | const config = { providers } as unknown as ServerConfig; |
| 223 | const selection = { instanceId: ProviderInstanceId.make("codex"), model: "test" }; |
| 224 | |
| 225 | expect( |
| 226 | buildModelOptions(config, selection, undefined, null).map((option) => option.providerKey), |
| 227 | ).toEqual(["codex", "claudeAgent"]); |
| 228 | expect(buildModelOptions(config, selection, selection.instanceId, null)).toEqual( |
| 229 | buildModelOptions(config, selection, undefined, null).filter( |
| 230 | (option) => option.providerKey === "codex", |
| 231 | ), |
| 232 | ); |
| 233 | }); |
| 234 | |
| 235 | it("keeps accounts in the continuation group and excludes other providers", () => { |
| 236 | const config = { |
| 237 | providers: [ |
| 238 | { instanceId: "codex", driver: "codex", continuation: { groupKey: "shared" } }, |
| 239 | { instanceId: "codex_work", driver: "codex", continuation: { groupKey: "shared" } }, |
| 240 | { instanceId: "codex_other", driver: "codex", continuation: { groupKey: "other" } }, |
| 241 | { instanceId: "claude", driver: "claudeAgent", continuation: { groupKey: "shared" } }, |
| 242 | ].map((provider) => ({ |
| 243 | ...provider, |
| 244 | enabled: true, |
| 245 | installed: true, |
| 246 | auth: { status: "authenticated" }, |
| 247 | models: [{ slug: "test", name: "Test", capabilities: null }], |
| 248 | })), |
| 249 | } as unknown as ServerConfig; |
| 250 | expect( |
| 251 | buildModelOptions(config, null, ProviderInstanceId.make("codex"), null).map( |
| 252 | (option) => option.providerKey, |
| 253 | ), |
| 254 | ).toEqual(["codex", "codex_work"]); |
| 255 | }); |
| 256 | |
| 257 | it("matches the web driver fallback without moving unknown Antigravity history", () => { |
| 258 | const config = { |
| 259 | providers: [ |
| 260 | { instanceId: "codex", driver: "codex" }, |
| 261 | { instanceId: "codex_work", driver: "codex" }, |
| 262 | { instanceId: "google", driver: "antigravity" }, |
| 263 | { instanceId: "google_work", driver: "antigravity" }, |
| 264 | ].map((provider) => ({ |
| 265 | ...provider, |
| 266 | enabled: true, |
| 267 | installed: true, |
| 268 | auth: { status: "authenticated" }, |
| 269 | models: [{ slug: "test", name: "Test", capabilities: null }], |
| 270 | })), |
| 271 | } as unknown as ServerConfig; |
| 272 | expect( |
| 273 | buildModelOptions(config, null, ProviderInstanceId.make("codex"), null).map( |
| 274 | (option) => option.providerKey, |
| 275 | ), |
| 276 | ).toEqual(["codex", "codex_work"]); |
| 277 | expect( |
| 278 | buildModelOptions(config, null, ProviderInstanceId.make("google"), null).map( |
| 279 | (option) => option.providerKey, |
| 280 | ), |
| 281 | ).toEqual(["google"]); |
| 282 | }); |
| 283 | |
| 284 | it("carries account badges into catalog and saved fallback models", () => { |
| 285 | const config = { |
| 286 | providers: [ |
| 287 | { instanceId: "codex", driver: "codex", displayName: "Personal" }, |
| 288 | { instanceId: "codex_work", driver: "codex", displayName: "Work", accentColor: "#123456" }, |
| 289 | ].map((provider) => ({ |
| 290 | ...provider, |
| 291 | enabled: true, |
| 292 | installed: true, |
| 293 | auth: { status: "authenticated" }, |
| 294 | models: [{ slug: "test", name: "Test", capabilities: null }], |
| 295 | })), |
| 296 | } as unknown as ServerConfig; |
| 297 | const selection = { instanceId: ProviderInstanceId.make("codex_work"), model: "saved" }; |
| 298 | const options = buildModelOptions(config, selection, undefined, null); |
| 299 | expect(options[0]?.providerBadge).toEqual({ displayName: "Personal", showBadge: true }); |
| 300 | expect(options[1]?.providerBadge).toEqual({ |
| 301 | displayName: "Work", |
| 302 | accentColor: "#123456", |
| 303 | showBadge: true, |
| 304 | }); |
| 305 | expect(options[2]?.providerBadge).toEqual(options[1]?.providerBadge); |
| 306 | expect( |
| 307 | buildModelOptions( |
| 308 | { ...config, providers: config.providers.slice(0, 1) }, |
| 309 | null, |
| 310 | undefined, |
| 311 | null, |
| 312 | )[0]?.providerBadge?.showBadge, |
| 313 | ).toBe(false); |
| 314 | }); |
| 315 | |
| 316 | it("only badges ACP accounts that share the same agent", () => { |
| 317 | const config = { |
| 318 | settings: { |
| 319 | ...DEFAULT_SERVER_SETTINGS, |
| 320 | providerInstances: { |
| 321 | acp_one: { driver: "acpRegistry", enabled: true, config: { agentId: "one" } }, |
| 322 | acp_two: { driver: "acpRegistry", enabled: true, config: { agentId: "two" } }, |
| 323 | }, |
| 324 | }, |
| 325 | providers: ["acp_one", "acp_two"].map((instanceId) => ({ |
| 326 | instanceId, |
| 327 | driver: "acpRegistry", |
| 328 | enabled: true, |
| 329 | installed: true, |
| 330 | auth: { status: "authenticated" }, |
| 331 | models: [{ slug: "test", name: "Test", capabilities: null }], |
| 332 | })), |
| 333 | } as unknown as ServerConfig; |
| 334 | expect( |
| 335 | buildModelOptions(config, null, undefined, null).map( |
| 336 | (option) => option.providerBadge?.showBadge, |
| 337 | ), |
| 338 | ).toEqual([false, false]); |
| 339 | const shared = { |
| 340 | ...config, |
| 341 | settings: { |
| 342 | ...config.settings, |
| 343 | providerInstances: { |
| 344 | ...config.settings.providerInstances, |
| 345 | acp_two: config.settings.providerInstances[ProviderInstanceId.make("acp_one")]!, |
| 346 | }, |
| 347 | }, |
| 348 | }; |
| 349 | expect( |
| 350 | buildModelOptions(shared, null, undefined, null).map( |
| 351 | (option) => option.providerBadge?.showBadge, |
| 352 | ), |
| 353 | ).toEqual([true, true]); |
| 354 | }); |
| 355 | |
| 356 | it.each(["disabled", "unavailable", "missing"] as const)( |
| 357 | "retains the selected %s provider's fallback in a filtered catalog", |
| 358 | (state) => { |
| 359 | const selection = { |
| 360 | instanceId: ProviderInstanceId.make("google_work"), |
| 361 | model: "saved-model", |
| 362 | options: [{ id: "native-option", value: "saved-choice" }], |
| 363 | }; |
| 364 | const provider = { |
| 365 | instanceId: selection.instanceId, |
| 366 | driver: "antigravity", |
| 367 | displayName: "Google Work", |
| 368 | enabled: state !== "disabled", |
| 369 | installed: true, |
| 370 | availability: state === "unavailable" ? "unavailable" : "available", |
| 371 | auth: { status: "authenticated" }, |
| 372 | models: [{ slug: selection.model, name: "Saved model", capabilities: null }], |
| 373 | }; |
| 374 | const config = { |
| 375 | providers: state === "missing" ? [] : [provider], |
| 376 | settings: { providerInstances: { google_work: { driver: "antigravity" } } }, |
| 377 | } as unknown as ServerConfig; |
| 378 | const options = buildModelOptions(config, selection, selection.instanceId, null); |
| 379 | expect(options).toEqual(buildModelOptions(config, selection, undefined, null)); |
| 380 | expect(options).toHaveLength(1); |
| 381 | expect(options[0]).toMatchObject({ selection, isUnavailable: true }); |
| 382 | }, |
| 383 | ); |
| 384 | |
| 385 | it("rejects stored selections whose provider is not usable", () => { |
| 386 | const config = { |
| 387 | providers: [ |
| 388 | { |
| 389 | instanceId: "codex", |
| 390 | driver: "codex", |
| 391 | enabled: true, |
| 392 | installed: true, |
| 393 | auth: { status: "authenticated" }, |
| 394 | models: [], |
| 395 | }, |
| 396 | { |
| 397 | instanceId: "claudeAgent", |
| 398 | driver: "claudeAgent", |
| 399 | enabled: false, |
| 400 | installed: true, |
| 401 | auth: { status: "authenticated" }, |
| 402 | models: [], |
| 403 | }, |
| 404 | ], |
| 405 | } as unknown as ServerConfig; |
| 406 | |
| 407 | const usable = { |
| 408 | instanceId: ProviderInstanceId.make("codex"), |
| 409 | model: "gpt-5.6-sol", |
| 410 | }; |
| 411 | const disabled = { |
| 412 | instanceId: ProviderInstanceId.make("claudeAgent"), |
| 413 | model: "claude-sonnet-5", |
| 414 | }; |
| 415 | const removed = { |
| 416 | instanceId: ProviderInstanceId.make("codex_personal"), |
| 417 | model: "gpt-5.6-sol", |
| 418 | }; |
| 419 | |
| 420 | expect(resolveSelectableModelSelection(config, usable, null)).toBe(usable); |
| 421 | expect(resolveSelectableModelSelection(config, disabled, null)).toBeNull(); |
| 422 | expect(resolveSelectableModelSelection(config, removed, null)).toBeNull(); |
| 423 | expect(isModelSelectionUnavailable(config, disabled, null)).toBe(true); |
| 424 | expect(isModelSelectionUnavailable(config, removed, null)).toBe(false); |
| 425 | expect(resolveSelectableModelSelection(null, disabled, null)).toBe(disabled); |
| 426 | }); |
| 427 | |
| 428 | describe("Antigravity selections", () => { |
| 429 | const selection = { |
| 430 | instanceId: ProviderInstanceId.make("google_work"), |
| 431 | model: "gemini-3.1-pro-high", |
| 432 | options: [{ id: "native-option", value: "saved/opaque-choice" }], |
| 433 | }; |
| 434 | const model = { |
| 435 | slug: selection.model, |
| 436 | name: "Gemini 3.1 Pro High", |
| 437 | subProvider: "Google", |
| 438 | isCustom: false, |
| 439 | isDefault: true, |
| 440 | isLegacy: true, |
| 441 | capabilities: { |
| 442 | optionDescriptors: [ |
| 443 | { |
| 444 | id: "native-option", |
| 445 | label: "Native option", |
| 446 | type: "select", |
| 447 | options: [{ id: "current/default", label: "Default", isDefault: true }], |
| 448 | currentValue: "current/default", |
| 449 | }, |
| 450 | ], |
| 451 | }, |
| 452 | }; |
| 453 | const config = { |
| 454 | providers: [ |
| 455 | { |
| 456 | instanceId: selection.instanceId, |
| 457 | driver: "antigravity", |
| 458 | displayName: "Google Work", |
| 459 | enabled: true, |
| 460 | installed: true, |
| 461 | auth: { status: "authenticated" }, |
| 462 | models: [model], |
| 463 | }, |
| 464 | ], |
| 465 | } as unknown as ServerConfig; |
| 466 | |
| 467 | it.each([ |
| 468 | ["disabled", { enabled: false }], |
| 469 | ["signed out", { auth: { status: "unauthenticated" } }], |
| 470 | ] as const)("keeps a %s provider's selection and known model details", (_state, update) => { |
| 471 | const unavailableConfig = { |
| 472 | ...config, |
| 473 | providers: config.providers.map((provider) => ({ ...provider, ...update })), |
| 474 | }; |
| 475 | |
| 476 | expect(resolveSelectableModelSelection(unavailableConfig, selection, null)).toBe(selection); |
| 477 | expect(resolveDefaultableModelSelection(unavailableConfig, selection, null)).toBe(selection); |
| 478 | expect(isModelSelectionUnavailable(unavailableConfig, selection, null)).toBe(true); |
| 479 | expect(buildModelOptions(unavailableConfig, null, undefined, null)).toEqual([]); |
| 480 | const [option] = buildModelOptions(unavailableConfig, selection, undefined, null); |
| 481 | expect(option).toMatchObject({ |
| 482 | key: `google_work:${selection.model}`, |
| 483 | label: model.name, |
| 484 | subtitle: "Google", |
| 485 | providerKey: "google_work", |
| 486 | providerLabel: "Google Work", |
| 487 | providerDriver: "antigravity", |
| 488 | isDefault: false, |
| 489 | isLegacy: true, |
| 490 | isUnavailable: true, |
| 491 | capabilities: model.capabilities, |
| 492 | }); |
| 493 | expect(option?.selection).toBe(selection); |
| 494 | }); |
| 495 | |
| 496 | it("keeps an exact selection when its model leaves and returns to the catalog", () => { |
| 497 | const changedConfig = { |
| 498 | ...config, |
| 499 | providers: config.providers.map((provider) => ({ |
| 500 | ...provider, |
| 501 | models: provider.models.map((model) => ({ ...model, slug: "gemini-3.1-pro-low" })), |
| 502 | })), |
| 503 | }; |
| 504 | |
| 505 | expect(resolveDefaultableModelSelection(changedConfig, selection, null)).toBe(selection); |
| 506 | expect(isModelSelectionUnavailable(changedConfig, selection, null)).toBe(true); |
| 507 | const options = buildModelOptions(changedConfig, selection, undefined, null); |
| 508 | const missing = options.find((option) => option.selection.model === selection.model); |
| 509 | expect(missing).toMatchObject({ |
| 510 | label: selection.model, |
| 511 | providerLabel: "Google Work", |
| 512 | providerDriver: "antigravity", |
| 513 | isUnavailable: true, |
| 514 | capabilities: null, |
| 515 | }); |
| 516 | expect(missing?.selection).toBe(selection); |
| 517 | expect( |
| 518 | resolveNewTaskModelSelection({ |
| 519 | draftSelection: null, |
| 520 | projectDefaultSelection: resolveDefaultableModelSelection(changedConfig, selection, null), |
| 521 | stickySelection: null, |
| 522 | modelOptions: options, |
| 523 | }), |
| 524 | ).toBe(selection); |
| 525 | |
| 526 | const [restored] = buildModelOptions(config, selection, undefined, null); |
| 527 | expect(isModelSelectionUnavailable(config, selection, null)).toBe(false); |
| 528 | expect(restored?.isUnavailable).not.toBe(true); |
| 529 | expect(restored?.selection).toBe(selection); |
| 530 | expect(resolveDefaultableModelSelection(config, selection, null)).toBe(selection); |
| 531 | expect( |
| 532 | buildModelOptions(config, null, undefined, null)[0]?.selection.options, |
| 533 | ).toBeUndefined(); |
| 534 | }); |
| 535 | |
| 536 | it("uses configured instance metadata when provider status is missing", () => { |
| 537 | const missingStatusConfig = { |
| 538 | providers: [], |
| 539 | settings: { |
| 540 | providerInstances: { |
| 541 | [selection.instanceId]: { driver: "antigravity", displayName: "Google Work" }, |
| 542 | }, |
| 543 | }, |
| 544 | } as unknown as ServerConfig; |
| 545 | |
| 546 | expect(resolveDefaultableModelSelection(missingStatusConfig, selection, null)).toBe( |
| 547 | selection, |
| 548 | ); |
| 549 | expect(isModelSelectionUnavailable(missingStatusConfig, selection, null)).toBe(true); |
| 550 | expect(buildModelOptions(missingStatusConfig, selection, undefined, null)).toMatchObject([ |
| 551 | { |
| 552 | providerDriver: "antigravity", |
| 553 | providerLabel: "Google Work", |
| 554 | isUnavailable: true, |
| 555 | selection, |
| 556 | }, |
| 557 | ]); |
| 558 | }); |
| 559 | |
| 560 | it("keeps offline selections without assuming that an unknown instance is Antigravity", () => { |
| 561 | const unknownConfig = { ...config, providers: [] }; |
| 562 | |
| 563 | expect(resolveDefaultableModelSelection(null, selection, null)).toBe(selection); |
| 564 | expect(isModelSelectionUnavailable(null, selection, null)).toBe(false); |
| 565 | expect(buildModelOptions(null, selection, undefined, null)[0]?.selection).toBe(selection); |
| 566 | expect(buildModelOptions(null, selection, undefined, null)[0]?.isUnavailable).not.toBe(true); |
| 567 | expect(isModelSelectionUnavailable(unknownConfig, selection, null)).toBe(false); |
| 568 | expect(resolveSelectableModelSelection(unknownConfig, selection, null)).toBeNull(); |
| 569 | }); |
| 570 | }); |
| 571 | |
| 572 | it("keeps legacy models out of implicit defaults", () => { |
| 573 | const config = { |
| 574 | providers: [ |
| 575 | { |
| 576 | instanceId: "codex", |
| 577 | driver: "codex", |
| 578 | displayName: "Codex", |
| 579 | enabled: true, |
| 580 | installed: true, |
| 581 | auth: { status: "authenticated" }, |
| 582 | models: [ |
| 583 | { slug: "gpt-5.6-sol", name: "GPT-5.6 Sol", isCustom: false, capabilities: null }, |
| 584 | { |
| 585 | slug: "gpt-5.4", |
| 586 | name: "GPT-5.4", |
| 587 | isCustom: false, |
| 588 | isLegacy: true, |
| 589 | capabilities: null, |
| 590 | }, |
| 591 | ], |
| 592 | }, |
| 593 | ], |
| 594 | } as unknown as ServerConfig; |
| 595 | |
| 596 | const current = { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.6-sol" }; |
| 597 | const legacy = { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" }; |
| 598 | |
| 599 | expect(resolveDefaultableModelSelection(config, current, null)).toBe(current); |
| 600 | expect(resolveDefaultableModelSelection(config, legacy, null)).toBeNull(); |
| 601 | expect(resolveDefaultableModelSelection(null, legacy, null)).toBe(legacy); |
| 602 | }); |
| 603 | |
| 604 | it("resolves new tasks from draft, project, sticky, then provider defaults", () => { |
| 605 | const draft = { instanceId: ProviderInstanceId.make("codex"), model: "draft" }; |
| 606 | const project = { instanceId: ProviderInstanceId.make("codex"), model: "project" }; |
| 607 | const sticky = { instanceId: ProviderInstanceId.make("codex"), model: "sticky" }; |
| 608 | const providerDefault = { |
| 609 | selection: { instanceId: ProviderInstanceId.make("codex"), model: "default" }, |
| 610 | isDefault: true, |
| 611 | } as ModelOption; |
| 612 | const resolve = ( |
| 613 | draftSelection: ModelSelection | null, |
| 614 | projectDefaultSelection: ModelSelection | null, |
| 615 | stickySelection: ModelSelection | null, |
| 616 | ) => |
| 617 | resolveNewTaskModelSelection({ |
| 618 | draftSelection, |
| 619 | projectDefaultSelection, |
| 620 | stickySelection, |
| 621 | modelOptions: [providerDefault], |
| 622 | }); |
| 623 | |
| 624 | expect(resolve(draft, project, sticky)).toBe(draft); |
| 625 | expect(resolve(null, project, sticky)).toBe(project); |
| 626 | expect(resolve(null, null, sticky)).toBe(sticky); |
| 627 | expect(resolve(null, null, null)).toBe(providerDefault.selection); |
| 628 | |
| 629 | const unavailable = { ...providerDefault, isUnavailable: true }; |
| 630 | expect( |
| 631 | resolveNewTaskModelSelection({ |
| 632 | draftSelection: null, |
| 633 | projectDefaultSelection: null, |
| 634 | stickySelection: null, |
| 635 | modelOptions: [unavailable], |
| 636 | }), |
| 637 | ).toBeNull(); |
| 638 | }); |
| 639 | |
| 640 | describe("project-aware enablement", () => { |
| 641 | const projectId = ProjectId.make("project-a"); |
| 642 | const instanceId = ProviderInstanceId.make("codex_work"); |
| 643 | |
| 644 | function configWith(settings: ServerSettings): ServerConfig { |
| 645 | return { |
| 646 | providers: [ |
| 647 | { |
| 648 | instanceId, |
| 649 | driver: "codex", |
| 650 | displayName: "Codex Work", |
| 651 | enabled: true, |
| 652 | installed: true, |
| 653 | auth: { status: "authenticated" }, |
| 654 | models: [{ slug: "model-a", name: "Model A", isCustom: false, capabilities: null }], |
| 655 | }, |
| 656 | ], |
| 657 | settings, |
| 658 | } as unknown as ServerConfig; |
| 659 | } |
| 660 | |
| 661 | it("refuses to send a non-Antigravity model the project turned off", () => { |
| 662 | const settings: ServerSettings = { |
| 663 | ...DEFAULT_SERVER_SETTINGS, |
| 664 | providerInstances: { |
| 665 | [instanceId]: { driver: ProviderDriverKind.make("codex"), enabled: true }, |
| 666 | }, |
| 667 | projectSettingsOverrides: { |
| 668 | [projectId]: { providerInstances: { [instanceId]: { enabled: false } } }, |
| 669 | }, |
| 670 | }; |
| 671 | const selection = { instanceId, model: "model-a" }; |
| 672 | |
| 673 | expect(isModelSelectionUnavailable(configWith(settings), selection, projectId)).toBe(true); |
| 674 | expect(isModelSelectionUnavailable(configWith(settings), selection, null)).toBe(false); |
| 675 | }); |
| 676 | |
| 677 | it("hides an instance the project disables even though the machine enables it", () => { |
| 678 | const settings: ServerSettings = { |
| 679 | ...DEFAULT_SERVER_SETTINGS, |
| 680 | providerInstances: { |
| 681 | [instanceId]: { driver: ProviderDriverKind.make("codex"), enabled: true }, |
| 682 | }, |
| 683 | projectSettingsOverrides: { |
| 684 | [projectId]: { providerInstances: { [instanceId]: { enabled: false } } }, |
| 685 | }, |
| 686 | }; |
| 687 | |
| 688 | expect(buildModelOptions(configWith(settings), null, undefined, projectId)).toEqual([]); |
| 689 | }); |
| 690 | |
| 691 | it("hides a machine-disabled instance for a project with no override, even though another project's keep-alive left it running", () => { |
| 692 | const otherProjectId = ProjectId.make("project-b"); |
| 693 | const settings: ServerSettings = { |
| 694 | ...DEFAULT_SERVER_SETTINGS, |
| 695 | providerInstances: { |
| 696 | [instanceId]: { driver: ProviderDriverKind.make("codex"), enabled: false }, |
| 697 | }, |
| 698 | projectSettingsOverrides: { |
| 699 | [otherProjectId]: { providerInstances: { [instanceId]: { enabled: true } } }, |
| 700 | }, |
| 701 | }; |
| 702 | |
| 703 | expect(buildModelOptions(configWith(settings), null, undefined, projectId)).toEqual([]); |
| 704 | }); |
| 705 | |
| 706 | it("refuses to send an instance the project merely inherits as disabled, not just an explicit override", () => { |
| 707 | const settings: ServerSettings = { |
| 708 | ...DEFAULT_SERVER_SETTINGS, |
| 709 | providerInstances: { |
| 710 | [instanceId]: { driver: ProviderDriverKind.make("codex"), enabled: false }, |
| 711 | }, |
| 712 | }; |
| 713 | const selection = { instanceId, model: "model-a" }; |
| 714 | |
| 715 | expect(isModelSelectionUnavailable(configWith(settings), selection, projectId)).toBe(true); |
| 716 | expect( |
| 717 | describeUnavailableModelSelection(configWith(settings), selection, projectId).title, |
| 718 | ).toBe("Model turned off for this project"); |
| 719 | expect(isModelSelectionUnavailable(configWith(settings), selection, null)).toBe(true); |
| 720 | }); |
| 721 | |
| 722 | it("marks a project-disabled instance's fallback selection unavailable instead of reviving it", () => { |
| 723 | const settings: ServerSettings = { |
| 724 | ...DEFAULT_SERVER_SETTINGS, |
| 725 | providerInstances: { |
| 726 | [instanceId]: { driver: ProviderDriverKind.make("codex"), enabled: true }, |
| 727 | }, |
| 728 | projectSettingsOverrides: { |
| 729 | [projectId]: { providerInstances: { [instanceId]: { enabled: false } } }, |
| 730 | }, |
| 731 | }; |
| 732 | const fallback = { instanceId, model: "model-a" }; |
| 733 | |
| 734 | const options = buildModelOptions(configWith(settings), fallback, undefined, projectId); |
| 735 | |
| 736 | expect(options).toHaveLength(1); |
| 737 | expect(options[0]).toMatchObject({ selection: fallback, isUnavailable: true }); |
| 738 | }); |
| 739 | |
| 740 | it("rejects a project-disabled antigravity instance's selection instead of keeping it", () => { |
| 741 | const antigravityInstanceId = ProviderInstanceId.make("antigravity_work"); |
| 742 | const config = { |
| 743 | providers: [ |
| 744 | { |
| 745 | instanceId: antigravityInstanceId, |
| 746 | driver: "antigravity", |
| 747 | displayName: "Antigravity Work", |
| 748 | enabled: true, |
| 749 | installed: true, |
| 750 | auth: { status: "authenticated" }, |
| 751 | availability: "available", |
| 752 | models: [{ slug: "model-a", name: "Model A", isCustom: false, capabilities: null }], |
| 753 | }, |
| 754 | ], |
| 755 | settings: { |
| 756 | ...DEFAULT_SERVER_SETTINGS, |
| 757 | providerInstances: { |
| 758 | [antigravityInstanceId]: { |
| 759 | driver: ProviderDriverKind.make("antigravity"), |
| 760 | enabled: true, |
| 761 | }, |
| 762 | }, |
| 763 | projectSettingsOverrides: { |
| 764 | [projectId]: { providerInstances: { [antigravityInstanceId]: { enabled: false } } }, |
| 765 | }, |
| 766 | }, |
| 767 | } as unknown as ServerConfig; |
| 768 | const selection = { instanceId: antigravityInstanceId, model: "model-a" }; |
| 769 | |
| 770 | expect(resolveSelectableModelSelection(config, selection, projectId)).toBeNull(); |
| 771 | expect(resolveSelectableModelSelection(config, selection, null)).toBe(selection); |
| 772 | expect( |
| 773 | resolveSelectableModelSelection({ ...config, providers: [] }, selection, projectId), |
| 774 | ).toBeNull(); |
| 775 | }); |
| 776 | }); |
| 777 | }); |
| 778 | |
| 779 | describe("synchronized mobile model visibility", () => { |
| 780 | const provider = ProviderInstanceId.make("codex_work"); |
| 781 | const other = ProviderInstanceId.make("codex_personal"); |
| 782 | const models = [ |
| 783 | { slug: "hidden", name: "Hidden", isCustom: false, capabilities: null }, |
| 784 | { slug: "first", name: "First", isCustom: false, capabilities: null }, |
| 785 | { slug: "second", name: "Second", isCustom: false, capabilities: null }, |
| 786 | { slug: "custom", name: "Custom", isCustom: true, capabilities: null }, |
| 787 | ]; |
| 788 | const config = { |
| 789 | settings: { |
| 790 | ...DEFAULT_SERVER_SETTINGS, |
| 791 | providerInstances: { |
| 792 | [provider]: { driver: ProviderDriverKind.make("codex"), enabled: true }, |
| 793 | [other]: { driver: ProviderDriverKind.make("codex"), enabled: true }, |
| 794 | }, |
| 795 | providerModelPreferences: { |
| 796 | [provider]: { |
| 797 | hiddenModels: ["hidden", "custom", "missing"], |
| 798 | modelOrder: ["second", "first"], |
| 799 | }, |
| 800 | }, |
| 801 | }, |
| 802 | providers: [provider, other].map((instanceId) => ({ |
| 803 | instanceId, |
| 804 | continuation: { groupKey: instanceId }, |
| 805 | driver: "codex", |
| 806 | enabled: true, |
| 807 | installed: true, |
| 808 | auth: { status: "authenticated" }, |
| 809 | models, |
| 810 | })), |
| 811 | } as unknown as ServerConfig; |
| 812 | |
| 813 | it("applies server visibility and ordering independently to each provider instance", () => { |
| 814 | const options = buildModelOptions(config, null, undefined, null); |
| 815 | expect( |
| 816 | options |
| 817 | .filter((option) => option.providerKey === provider) |
| 818 | .map((option) => option.selection.model), |
| 819 | ).toEqual(["second", "first", "custom"]); |
| 820 | expect( |
| 821 | options |
| 822 | .filter((option) => option.providerKey === other) |
| 823 | .map((option) => option.selection.model), |
| 824 | ).toEqual(["hidden", "first", "second", "custom"]); |
| 825 | }); |
| 826 | |
| 827 | it("exposes hidden built-ins for management without offering custom visibility controls", () => { |
| 828 | const options = buildModelOptions( |
| 829 | config, |
| 830 | { instanceId: provider, model: "hidden" }, |
| 831 | provider, |
| 832 | null, |
| 833 | true, |
| 834 | ); |
| 835 | expect(options.map((option) => option.selection.model)).toEqual([ |
| 836 | "second", |
| 837 | "first", |
| 838 | "hidden", |
| 839 | "custom", |
| 840 | ]); |
| 841 | expect(options.find((option) => option.selection.model === "hidden")).toMatchObject({ |
| 842 | isHidden: true, |
| 843 | canHide: true, |
| 844 | }); |
| 845 | expect(options.find((option) => option.selection.model === "custom")).toMatchObject({ |
| 846 | isHidden: false, |
| 847 | canHide: false, |
| 848 | }); |
| 849 | }); |
| 850 | |
| 851 | it.each(["hidden", "missing"])("does not resurrect the hidden fallback %s", (model) => { |
| 852 | expect( |
| 853 | buildModelOptions(config, { instanceId: provider, model }, provider, null).map( |
| 854 | (option) => option.selection.model, |
| 855 | ), |
| 856 | ).toEqual(["second", "first", "custom"]); |
| 857 | }); |
| 858 | |
| 859 | // regression: #16816 sync model favorites and visibility across clients |
| 860 | it("keeps custom models visible and restores built-in models when the server clears hidden models", () => { |
| 861 | expect( |
| 862 | buildModelOptions(config, { instanceId: provider, model: "custom" }, provider, null).map( |
| 863 | (option) => option.selection.model, |
| 864 | ), |
| 865 | ).toContain("custom"); |
| 866 | const cleared = { ...config, settings: { ...config.settings, providerModelPreferences: {} } }; |
| 867 | expect( |
| 868 | buildModelOptions(cleared, null, provider, null).map((option) => option.selection.model), |
| 869 | ).toEqual(["hidden", "first", "second", "custom"]); |
| 870 | }); |
| 871 | |
| 872 | describe("managed provider policy", () => { |
| 873 | const config = { |
| 874 | providers: [ |
| 875 | { |
| 876 | instanceId: "codex", |
| 877 | driver: "codex", |
| 878 | enabled: true, |
| 879 | installed: true, |
| 880 | auth: { status: "authenticated" }, |
| 881 | models: [{ slug: "gpt-5.6-sol", name: "GPT", isCustom: false, capabilities: null }], |
| 882 | }, |
| 883 | { |
| 884 | instanceId: "claudeAgent", |
| 885 | driver: "claudeAgent", |
| 886 | enabled: true, |
| 887 | installed: true, |
| 888 | auth: { status: "authenticated" }, |
| 889 | models: [ |
| 890 | { slug: "claude-sonnet-5", name: "Sonnet", isCustom: false, capabilities: null }, |
| 891 | ], |
| 892 | }, |
| 893 | ], |
| 894 | managedPolicy: { |
| 895 | organization: "Example Corp", |
| 896 | lockedSettings: [], |
| 897 | limits: { providers: { allowedDrivers: [ProviderDriverKind.make("codex")] } }, |
| 898 | }, |
| 899 | } as unknown as ServerConfig; |
| 900 | const disallowed = { |
| 901 | instanceId: ProviderInstanceId.make("claudeAgent"), |
| 902 | model: "claude-sonnet-5", |
| 903 | }; |
| 904 | |
| 905 | it("leaves drivers the policy does not allow out of the picker", () => { |
| 906 | expect( |
| 907 | buildModelOptions(config, null, undefined, null).map((option) => option.providerKey), |
| 908 | ).toEqual(["codex"]); |
| 909 | }); |
| 910 | |
| 911 | it("marks a thread's selection on a disallowed driver unavailable with the policy reason", () => { |
| 912 | expect( |
| 913 | buildModelOptions(config, disallowed, undefined, null).find( |
| 914 | (option) => option.providerKey === "claudeAgent", |
| 915 | )?.isUnavailable, |
| 916 | ).toBe(true); |
| 917 | expect(isModelSelectionUnavailable(config, disallowed, null)).toBe(true); |
| 918 | expect(describeUnavailableModelSelection(config, disallowed, null)).toEqual({ |
| 919 | title: "Provider not allowed", |
| 920 | detail: "Example Corp's policy does not allow the Claude provider. Choose another model.", |
| 921 | }); |
| 922 | }); |
| 923 | }); |
| 924 | }); |