Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

thread-settings-sheet-state.test.ts

233 lines6.9 KB

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 favoritesFirst,
17 modelMatchesCatalogQuery,
18 pendingModelAfterPress,
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 };
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("keeps favorites in catalog order ahead of other models", () => {
78 const models = [
79 modelOption("first"),
80 modelOption("second"),
81 modelOption("third"),
82 modelOption("fourth"),
83 ];
84 const favorites = new Set([models[2]!.key, models[0]!.key]);
85​
86 expect(favoritesFirst(models, favorites).map((model) => model.selection.model)).toEqual([
87 "first",
88 "third",
89 "second",
90 "fourth",
91 ]);
92 expect(models.map((model) => model.selection.model)).toEqual([
93 "first",
94 "second",
95 "third",
96 "fourth",
97 ]);
98 });
99​
100 it("matches visible model and provider terms", () => {
101 const model = modelOption("gpt-next");
102​
103 expect(modelMatchesCatalogQuery({ model, providerLabel: "Codex", query: "NEXT" })).toBe(true);
104 expect(modelMatchesCatalogQuery({ model, providerLabel: "Codex", query: "codex" })).toBe(true);
105 expect(modelMatchesCatalogQuery({ model, providerLabel: "Codex", query: "claude" })).toBe(
106 false,
107 );
108 });
109​
110 it("treats whitespace-only catalog searches as empty", () => {
111 expect(
112 modelMatchesCatalogQuery({
113 model: modelOption("gpt-next"),
114 providerLabel: "Codex",
115 query: " ",
116 }),
117 ).toBe(true);
118 });
119​
120 it("matches the upstream provider's display name", () => {
121 const model = {
122 ...modelOption("opencode/claude-fable-5"),
123 label: "Claude Fable 5",
124 subtitle: "OpenCode Zen",
125 };
126​
127 expect(modelMatchesCatalogQuery({ model, providerLabel: "OpenCode", query: " ZEN " })).toBe(
128 true,
129 );
130 expect(modelMatchesCatalogQuery({ model, providerLabel: "OpenCode", query: "copilot" })).toBe(
131 false,
132 );
133 });
134​
135 it("clears staging when the applied model is pressed", () => {
136 expect(
137 pendingModelAfterPress({
138 current: modelOption("gpt-next"),
139 pressed: modelOption("gpt-current"),
140 pressedIsApplied: true,
141 }),
142 ).toBeNull();
143 });
144​
145 it("preserves in-progress edits when the staged model is pressed again", () => {
146 rememberModelOptions("codex", "gpt-next", [{ id: "effort", value: "medium" }]);
147 const pending = modelOption("gpt-next", [{ id: "effort", value: "high" }]);
148​
149 expect(
150 pendingModelAfterPress({
151 current: pending,
152 pressed: modelOption("gpt-next"),
153 pressedIsApplied: false,
154 restoreSelection: withRememberedModelOptions,
155 })?.selection.options,
156 ).toEqual([{ id: "effort", value: "high" }]);
157 });
158​
159 it("previews remembered options before Save when switching models", () => {
160 const remembered = [
161 { id: "effort", value: "high" },
162 { id: "serviceTier", value: "priority" },
163 ];
164 rememberModelOptions("codex", "gpt-other", remembered);
165 const pressed = modelOption("gpt-other");
166​
167 const pending = pendingModelAfterPress({
168 current: modelOption("gpt-next"),
169 pressed,
170 pressedIsApplied: false,
171 restoreSelection: withRememberedModelOptions,
172 });
173​
174 expect(pending?.selection).toEqual({ ...pressed.selection, options: remembered });
175 const preview = getProviderOptionDescriptors({
176 caps: pressed.capabilities!,
177 selections: pending?.selection.options,
178 });
179 expect(preview.map(({ id, currentValue }) => ({ id, value: currentValue }))).toEqual(
180 remembered,
181 );
182 expect(pending?.selection).toEqual(withRememberedModelOptions(pressed.selection));
183 });
184​
185 it("keeps catalog defaults for scheduled-task picks even when options are remembered", () => {
186 rememberModelOptions("codex", "gpt-other", [
187 { id: "effort", value: "high" },
188 { id: "serviceTier", value: "priority" },
189 ]);
190 const pressed = modelOption("gpt-other");
191​
192 const pending = pendingModelAfterPress({
193 current: modelOption("gpt-next"),
194 pressed,
195 pressedIsApplied: false,
196 });
197​
198 expect(pending?.selection).toEqual(pressed.selection);
199 const preview = getProviderOptionDescriptors({
200 caps: pressed.capabilities!,
201 selections: pending?.selection.options,
202 });
203 expect(preview.map(({ id, currentValue }) => ({ id, value: currentValue }))).toEqual([
204 { id: "effort", value: "medium" },
205 { id: "serviceTier", value: "default" },
206 ]);
207 });
208​
209 it("cannot select a hidden model exposed only for visibility management", () => {
210 const pending = modelOption("gpt-next");
211 expect(
212 canCommitPendingModel(pending, [
213 { providerKey: "codex", providerLabel: "Codex", models: [{ ...pending, isHidden: true }] },
214 ]),
215 ).toBe(false);
216 });
217​
218 it("cannot save a staged model after sign-out removes it from the catalog", () => {
219 const pending = modelOption("gemini-native");
220 const group = { providerKey: "codex", providerLabel: "Codex", models: [pending] };
221​
222 expect(canCommitPendingModel(pending, [group])).toBe(true);
223 expect(canCommitPendingModel(pending, [])).toBe(false);
224 expect(
225 canCommitPendingModel(pending, [
226 {
227 ...group,
228 models: [{ ...pending, isUnavailable: true }],
229 },
230 ]),
231 ).toBe(false);
232 });
233});