The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { afterEach, beforeEach, describe, expect, it } from "@effect/vitest"; |
| 2 | import { vi } from "vite-plus/test"; |
| 3 | |
| 4 | import { ProviderInstanceId, type ProviderOptionSelection } from "@t3tools/contracts"; |
| 5 | import { getProviderOptionDescriptors } from "@t3tools/shared/model"; |
| 6 | |
| 7 | import type { ModelOption } from "../../lib/modelOptions"; |
| 8 | import { appAtomRegistry } from "../../state/atom-registry"; |
| 9 | import { modelOptionMemoryAtom } from "../../state/use-composer-drafts"; |
| 10 | import { |
| 11 | rememberModelOptions, |
| 12 | withRememberedModelOptions, |
| 13 | } from "../../state/use-model-option-memory"; |
| 14 | import { canCommitPendingModel, pendingModelAfterPress } from "./thread-settings-sheet-state"; |
| 15 | |
| 16 | function modelOption( |
| 17 | model: string, |
| 18 | options: ReadonlyArray<ProviderOptionSelection> = [], |
| 19 | ): ModelOption { |
| 20 | return { |
| 21 | key: `codex:${model}`, |
| 22 | label: model, |
| 23 | subtitle: "", |
| 24 | providerKey: "codex", |
| 25 | providerLabel: "Codex", |
| 26 | providerDriver: "codex", |
| 27 | isDefault: false, |
| 28 | isLegacy: false, |
| 29 | capabilities: { |
| 30 | optionDescriptors: [ |
| 31 | { |
| 32 | id: "effort", |
| 33 | label: "Reasoning", |
| 34 | type: "select", |
| 35 | options: [ |
| 36 | { id: "medium", label: "Medium", isDefault: true }, |
| 37 | { id: "high", label: "High" }, |
| 38 | ], |
| 39 | currentValue: "medium", |
| 40 | }, |
| 41 | { |
| 42 | id: "serviceTier", |
| 43 | label: "Service Tier", |
| 44 | type: "select", |
| 45 | options: [ |
| 46 | { id: "default", label: "Standard", isDefault: true }, |
| 47 | { id: "priority", label: "Fast" }, |
| 48 | ], |
| 49 | currentValue: "default", |
| 50 | }, |
| 51 | ], |
| 52 | }, |
| 53 | selection: { |
| 54 | instanceId: ProviderInstanceId.make("codex"), |
| 55 | model, |
| 56 | options, |
| 57 | }, |
| 58 | }; |
| 59 | } |
| 60 | |
| 61 | beforeEach(() => { |
| 62 | vi.useFakeTimers(); |
| 63 | }); |
| 64 | |
| 65 | afterEach(() => { |
| 66 | vi.clearAllTimers(); |
| 67 | vi.useRealTimers(); |
| 68 | appAtomRegistry.set(modelOptionMemoryAtom, {}); |
| 69 | }); |
| 70 | |
| 71 | describe("thread settings sheet state", () => { |
| 72 | it("preserves in-progress edits when the staged model is pressed again", () => { |
| 73 | rememberModelOptions("codex", "gpt-next", [{ id: "effort", value: "medium" }]); |
| 74 | const pending = modelOption("gpt-next", [{ id: "effort", value: "high" }]); |
| 75 | |
| 76 | expect( |
| 77 | pendingModelAfterPress({ |
| 78 | current: pending, |
| 79 | pressed: modelOption("gpt-next"), |
| 80 | pressedIsApplied: false, |
| 81 | restoreSelection: withRememberedModelOptions, |
| 82 | })?.selection.options, |
| 83 | ).toEqual([{ id: "effort", value: "high" }]); |
| 84 | }); |
| 85 | |
| 86 | it("previews remembered options before Save when switching models", () => { |
| 87 | const remembered = [ |
| 88 | { id: "effort", value: "high" }, |
| 89 | { id: "serviceTier", value: "priority" }, |
| 90 | ]; |
| 91 | rememberModelOptions("codex", "gpt-other", remembered); |
| 92 | const pressed = modelOption("gpt-other"); |
| 93 | |
| 94 | const pending = pendingModelAfterPress({ |
| 95 | current: modelOption("gpt-next"), |
| 96 | pressed, |
| 97 | pressedIsApplied: false, |
| 98 | restoreSelection: withRememberedModelOptions, |
| 99 | }); |
| 100 | |
| 101 | expect(pending?.selection).toEqual({ ...pressed.selection, options: remembered }); |
| 102 | const preview = getProviderOptionDescriptors({ |
| 103 | caps: pressed.capabilities!, |
| 104 | selections: pending?.selection.options, |
| 105 | }); |
| 106 | expect(preview.map(({ id, currentValue }) => ({ id, value: currentValue }))).toEqual( |
| 107 | remembered, |
| 108 | ); |
| 109 | expect(pending?.selection).toEqual(withRememberedModelOptions(pressed.selection)); |
| 110 | }); |
| 111 | |
| 112 | it("keeps catalog defaults for scheduled-task picks even when options are remembered", () => { |
| 113 | rememberModelOptions("codex", "gpt-other", [ |
| 114 | { id: "effort", value: "high" }, |
| 115 | { id: "serviceTier", value: "priority" }, |
| 116 | ]); |
| 117 | const pressed = modelOption("gpt-other"); |
| 118 | |
| 119 | const pending = pendingModelAfterPress({ |
| 120 | current: modelOption("gpt-next"), |
| 121 | pressed, |
| 122 | pressedIsApplied: false, |
| 123 | }); |
| 124 | |
| 125 | expect(pending?.selection).toEqual(pressed.selection); |
| 126 | const preview = getProviderOptionDescriptors({ |
| 127 | caps: pressed.capabilities!, |
| 128 | selections: pending?.selection.options, |
| 129 | }); |
| 130 | expect(preview.map(({ id, currentValue }) => ({ id, value: currentValue }))).toEqual([ |
| 131 | { id: "effort", value: "medium" }, |
| 132 | { id: "serviceTier", value: "default" }, |
| 133 | ]); |
| 134 | }); |
| 135 | |
| 136 | // regression: #16816 sync model favorites and visibility across clients |
| 137 | it("cannot select a hidden model exposed only for visibility management", () => { |
| 138 | const pending = modelOption("gpt-next"); |
| 139 | expect( |
| 140 | canCommitPendingModel(pending, [ |
| 141 | { providerKey: "codex", providerLabel: "Codex", models: [{ ...pending, isHidden: true }] }, |
| 142 | ]), |
| 143 | ).toBe(false); |
| 144 | }); |
| 145 | |
| 146 | it("cannot save a staged model after sign-out removes it from the catalog", () => { |
| 147 | const pending = modelOption("gemini-native"); |
| 148 | const group = { providerKey: "codex", providerLabel: "Codex", models: [pending] }; |
| 149 | |
| 150 | expect(canCommitPendingModel(pending, [group])).toBe(true); |
| 151 | expect(canCommitPendingModel(pending, [])).toBe(false); |
| 152 | expect( |
| 153 | canCommitPendingModel(pending, [ |
| 154 | { |
| 155 | ...group, |
| 156 | models: [{ ...pending, isUnavailable: true }], |
| 157 | }, |
| 158 | ]), |
| 159 | ).toBe(false); |
| 160 | }); |
| 161 | }); |