Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it } from "@effect/vitest";
2import { vi } from "vite-plus/test";
3
4import { ProviderInstanceId, type ProviderOptionSelection } from "@t3tools/contracts";
5import { getProviderOptionDescriptors } from "@t3tools/shared/model";
6
7import type { ModelOption } from "../../lib/modelOptions";
8import { appAtomRegistry } from "../../state/atom-registry";
9import { modelOptionMemoryAtom } from "../../state/use-composer-drafts";
10import {
11 rememberModelOptions,
12 withRememberedModelOptions,
13} from "../../state/use-model-option-memory";
14import {
15 canCommitPendingModel,
16 favoriteCatalogModels,
17 pendingModelAfterPress,
18 visibleCatalogModels,
19} from "./thread-settings-sheet-state";
20
21function modelOption(
22 model: string,
23 options: ReadonlyArray<ProviderOptionSelection> = [],
24): ModelOption {
25 return {
26 key: `codex:${model}`,
27 label: model,
28 subtitle: "",
29 providerKey: "codex",
30 providerLabel: "Codex",
31 providerDriver: "codex",
32 isDefault: false,
33 isLegacy: false,
34 capabilities: {
35 optionDescriptors: [
36 {
37 id: "effort",
38 label: "Reasoning",
39 type: "select",
40 options: [
41 { id: "medium", label: "Medium", isDefault: true },
42 { id: "high", label: "High" },
43 ],
44 currentValue: "medium",
45 },
46 {
47 id: "serviceTier",
48 label: "Service Tier",
49 type: "select",
50 options: [
51 { id: "default", label: "Standard", isDefault: true },
52 { id: "priority", label: "Fast" },
53 ],
54 currentValue: "default",
55 },
56 ],
57 },
58 selection: {
59 instanceId: ProviderInstanceId.make("codex"),
60 model,
61 options,
62 },
63 };
64}
65
66beforeEach(() => {
67 vi.useFakeTimers();
68});
69
70afterEach(() => {
71 vi.clearAllTimers();
72 vi.useRealTimers();
73 appAtomRegistry.set(modelOptionMemoryAtom, {});
74});
75
76describe("thread settings sheet state", () => {
77 it("preserves in-progress edits when the staged model is pressed again", () => {
78 rememberModelOptions("codex", "gpt-next", [{ id: "effort", value: "medium" }]);
79 const pending = modelOption("gpt-next", [{ id: "effort", value: "high" }]);
80
81 expect(
82 pendingModelAfterPress({
83 current: pending,
84 pressed: modelOption("gpt-next"),
85 pressedIsApplied: false,
86 restoreSelection: withRememberedModelOptions,
87 })?.selection.options,
88 ).toEqual([{ id: "effort", value: "high" }]);
89 });
90
91 it("previews remembered options before Save when switching models", () => {
92 const remembered = [
93 { id: "effort", value: "high" },
94 { id: "serviceTier", value: "priority" },
95 ];
96 rememberModelOptions("codex", "gpt-other", remembered);
97 const pressed = modelOption("gpt-other");
98
99 const pending = pendingModelAfterPress({
100 current: modelOption("gpt-next"),
101 pressed,
102 pressedIsApplied: false,
103 restoreSelection: withRememberedModelOptions,
104 });
105
106 expect(pending?.selection).toEqual({ ...pressed.selection, options: remembered });
107 const preview = getProviderOptionDescriptors({
108 caps: pressed.capabilities!,
109 selections: pending?.selection.options,
110 });
111 expect(preview.map(({ id, currentValue }) => ({ id, value: currentValue }))).toEqual(
112 remembered,
113 );
114 expect(pending?.selection).toEqual(withRememberedModelOptions(pressed.selection));
115 });
116
117 it("keeps catalog defaults for scheduled-task picks even when options are remembered", () => {
118 rememberModelOptions("codex", "gpt-other", [
119 { id: "effort", value: "high" },
120 { id: "serviceTier", value: "priority" },
121 ]);
122 const pressed = modelOption("gpt-other");
123
124 const pending = pendingModelAfterPress({
125 current: modelOption("gpt-next"),
126 pressed,
127 pressedIsApplied: false,
128 });
129
130 expect(pending?.selection).toEqual(pressed.selection);
131 const preview = getProviderOptionDescriptors({
132 caps: pressed.capabilities!,
133 selections: pending?.selection.options,
134 });
135 expect(preview.map(({ id, currentValue }) => ({ id, value: currentValue }))).toEqual([
136 { id: "effort", value: "medium" },
137 { id: "serviceTier", value: "default" },
138 ]);
139 });
140
141 // regression: #16816 sync model favorites and visibility across clients
142 it("cannot select a hidden model exposed only for visibility management", () => {
143 const pending = modelOption("gpt-next");
144 expect(
145 canCommitPendingModel(pending, [
146 { providerKey: "codex", providerLabel: "Codex", models: [{ ...pending, isHidden: true }] },
147 ]),
148 ).toBe(false);
149 });
150
151 it("cannot save a staged model after sign-out removes it from the catalog", () => {
152 const pending = modelOption("gemini-native");
153 const group = { providerKey: "codex", providerLabel: "Codex", models: [pending] };
154
155 expect(canCommitPendingModel(pending, [group])).toBe(true);
156 expect(canCommitPendingModel(pending, [])).toBe(false);
157 expect(
158 canCommitPendingModel(pending, [
159 {
160 ...group,
161 models: [{ ...pending, isUnavailable: true }],
162 },
163 ]),
164 ).toBe(false);
165 });
166
167 it("finds legacy models by search without enabling Show legacy models", () => {
168 const current = modelOption("gpt-next");
169 const legacy = { ...modelOption("claude-opus-4-8"), label: "Claude Opus 4.8", isLegacy: true };
170 const visible = (query: string) =>
171 visibleCatalogModels({
172 models: [current, legacy],
173 providerLabel: "Codex",
174 query,
175 favoriteKeys: new Set(),
176 favoritesOnly: false,
177 showLegacy: false,
178 isDisplayed: () => false,
179 }).map((model) => model.label);
180
181 expect(visible("")).toEqual(["gpt-next"]);
182 expect(visible(" ")).toEqual(["gpt-next"]);
183 expect(visible("Claude Opus 4.8")).toEqual(["Claude Opus 4.8"]);
184 });
185
186 it("collects favorites across providers in catalog order", () => {
187 const codexFav = modelOption("gpt-6.2-sol");
188 const codexOther = modelOption("gpt-6-luna");
189 const unavailableFav: ModelOption = {
190 ...modelOption("grok-build"),
191 key: "fx:grok-build",
192 subtitle: "Grok subscription",
193 providerKey: "fx",
194 providerLabel: "fx",
195 providerDriver: "fx",
196 selection: { instanceId: ProviderInstanceId.make("fx"), model: "grok-build" },
197 };
198 const claudeFav: ModelOption = {
199 ...modelOption("claude-opus-5-5"),
200 key: "claude:claude-opus-5-5",
201 label: "Claude Opus 5.5",
202 providerKey: "claude",
203 providerLabel: "Claude",
204 providerDriver: "claudeAgent",
205 isLegacy: true,
206 selection: {
207 instanceId: ProviderInstanceId.make("claude"),
208 model: "claude-opus-5-5",
209 options: [],
210 },
211 };
212 const providerGroups = [
213 { providerKey: "codex", providerLabel: "Codex", models: [codexOther, codexFav] },
214 { providerKey: "claude", providerLabel: "Claude", models: [claudeFav] },
215 {
216 providerKey: "fx",
217 providerLabel: "fx",
218 models: [unavailableFav],
219 subProviders: [{ name: "Grok subscription", unavailableReason: "Signed out." }],
220 },
221 ];
222 const favoriteKeys = new Set([codexFav.key, claudeFav.key, unavailableFav.key]);
223
224 expect(favoriteCatalogModels(providerGroups, favoriteKeys).map((model) => model.key)).toEqual([
225 codexFav.key,
226 claudeFav.key,
227 ]);
228 expect(favoriteCatalogModels(providerGroups, new Set())).toEqual([]);
229 });
230});