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 pendingModelAfterPress,
17 visibleCatalogModels,
18} from "./thread-settings-sheet-state";
19
20function modelOption(
21 model: string,
22 options: ReadonlyArray<ProviderOptionSelection> = [],
23): ModelOption {
24 return {
25 key: `codex:${model}`,
26 label: model,
27 subtitle: "",
28 providerKey: "codex",
29 providerLabel: "Codex",
30 providerDriver: "codex",
31 isDefault: false,
32 isLegacy: false,
33 capabilities: {
34 optionDescriptors: [
35 {
36 id: "effort",
37 label: "Reasoning",
38 type: "select",
39 options: [
40 { id: "medium", label: "Medium", isDefault: true },
41 { id: "high", label: "High" },
42 ],
43 currentValue: "medium",
44 },
45 {
46 id: "serviceTier",
47 label: "Service Tier",
48 type: "select",
49 options: [
50 { id: "default", label: "Standard", isDefault: true },
51 { id: "priority", label: "Fast" },
52 ],
53 currentValue: "default",
54 },
55 ],
56 },
57 selection: {
58 instanceId: ProviderInstanceId.make("codex"),
59 model,
60 options,
61 },
62 };
63}
64
65beforeEach(() => {
66 vi.useFakeTimers();
67});
68
69afterEach(() => {
70 vi.clearAllTimers();
71 vi.useRealTimers();
72 appAtomRegistry.set(modelOptionMemoryAtom, {});
73});
74
75describe("thread settings sheet state", () => {
76 it("preserves in-progress edits when the staged model is pressed again", () => {
77 rememberModelOptions("codex", "gpt-next", [{ id: "effort", value: "medium" }]);
78 const pending = modelOption("gpt-next", [{ id: "effort", value: "high" }]);
79
80 expect(
81 pendingModelAfterPress({
82 current: pending,
83 pressed: modelOption("gpt-next"),
84 pressedIsApplied: false,
85 restoreSelection: withRememberedModelOptions,
86 })?.selection.options,
87 ).toEqual([{ id: "effort", value: "high" }]);
88 });
89
90 it("previews remembered options before Save when switching models", () => {
91 const remembered = [
92 { id: "effort", value: "high" },
93 { id: "serviceTier", value: "priority" },
94 ];
95 rememberModelOptions("codex", "gpt-other", remembered);
96 const pressed = modelOption("gpt-other");
97
98 const pending = pendingModelAfterPress({
99 current: modelOption("gpt-next"),
100 pressed,
101 pressedIsApplied: false,
102 restoreSelection: withRememberedModelOptions,
103 });
104
105 expect(pending?.selection).toEqual({ ...pressed.selection, options: remembered });
106 const preview = getProviderOptionDescriptors({
107 caps: pressed.capabilities!,
108 selections: pending?.selection.options,
109 });
110 expect(preview.map(({ id, currentValue }) => ({ id, value: currentValue }))).toEqual(
111 remembered,
112 );
113 expect(pending?.selection).toEqual(withRememberedModelOptions(pressed.selection));
114 });
115
116 it("keeps catalog defaults for scheduled-task picks even when options are remembered", () => {
117 rememberModelOptions("codex", "gpt-other", [
118 { id: "effort", value: "high" },
119 { id: "serviceTier", value: "priority" },
120 ]);
121 const pressed = modelOption("gpt-other");
122
123 const pending = pendingModelAfterPress({
124 current: modelOption("gpt-next"),
125 pressed,
126 pressedIsApplied: false,
127 });
128
129 expect(pending?.selection).toEqual(pressed.selection);
130 const preview = getProviderOptionDescriptors({
131 caps: pressed.capabilities!,
132 selections: pending?.selection.options,
133 });
134 expect(preview.map(({ id, currentValue }) => ({ id, value: currentValue }))).toEqual([
135 { id: "effort", value: "medium" },
136 { id: "serviceTier", value: "default" },
137 ]);
138 });
139
140 // regression: #16816 sync model favorites and visibility across clients
141 it("cannot select a hidden model exposed only for visibility management", () => {
142 const pending = modelOption("gpt-next");
143 expect(
144 canCommitPendingModel(pending, [
145 { providerKey: "codex", providerLabel: "Codex", models: [{ ...pending, isHidden: true }] },
146 ]),
147 ).toBe(false);
148 });
149
150 it("cannot save a staged model after sign-out removes it from the catalog", () => {
151 const pending = modelOption("gemini-native");
152 const group = { providerKey: "codex", providerLabel: "Codex", models: [pending] };
153
154 expect(canCommitPendingModel(pending, [group])).toBe(true);
155 expect(canCommitPendingModel(pending, [])).toBe(false);
156 expect(
157 canCommitPendingModel(pending, [
158 {
159 ...group,
160 models: [{ ...pending, isUnavailable: true }],
161 },
162 ]),
163 ).toBe(false);
164 });
165
166 it("finds legacy models by search without enabling Show legacy models", () => {
167 const current = modelOption("gpt-next");
168 const legacy = { ...modelOption("claude-opus-4-8"), label: "Claude Opus 4.8", isLegacy: true };
169 const visible = (query: string) =>
170 visibleCatalogModels({
171 models: [current, legacy],
172 providerLabel: "Codex",
173 query,
174 favoriteKeys: new Set(),
175 favoritesOnly: false,
176 showLegacy: false,
177 isDisplayed: () => false,
178 }).map((model) => model.label);
179
180 expect(visible("")).toEqual(["gpt-next"]);
181 expect(visible(" ")).toEqual(["gpt-next"]);
182 expect(visible("Claude Opus 4.8")).toEqual(["Claude Opus 4.8"]);
183 });
184});