Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { ProviderInstanceId, type ProviderOptionDescriptor } from "@t3tools/contracts";
4
5import type { ModelOption, ProviderGroup } from "../../lib/modelOptions";
6import { buildThreadSettingsMenu, type ThreadSettingsMenuEvent } from "./thread-settings-menu";
7
8function modelOption(
9 model: string,
10 overrides: Partial<Pick<ModelOption, "isDefault" | "isLegacy" | "providerKey">> = {},
11): ModelOption {
12 const providerKey = overrides.providerKey ?? "codex";
13 return {
14 key: `${providerKey}:${model}`,
15 label: model,
16 subtitle: providerKey,
17 providerKey,
18 providerLabel: providerKey === "codex" ? "Codex" : "Claude",
19 providerDriver: providerKey === "codex" ? "codex" : "claudeAgent",
20 isDefault: overrides.isDefault ?? false,
21 isLegacy: overrides.isLegacy ?? false,
22 capabilities: null,
23 selection: {
24 instanceId: ProviderInstanceId.make(providerKey),
25 model,
26 options: [],
27 },
28 };
29}
30
31function group(models: ReadonlyArray<ModelOption>): ProviderGroup {
32 const first = models[0];
33 if (!first) {
34 throw new Error("group requires at least one model");
35 }
36 return {
37 providerKey: first.providerKey,
38 providerLabel: first.providerLabel,
39 models,
40 };
41}
42
43const effortDescriptor: ProviderOptionDescriptor = {
44 id: "effort",
45 label: "Reasoning",
46 type: "select",
47 options: [
48 { id: "low", label: "Low" },
49 { id: "medium", label: "Medium", isDefault: true },
50 { id: "high", label: "High" },
51 { id: "ultrathink", label: "Ultrathink" },
52 { id: "ultracode", label: "Ultracode" },
53 ],
54 currentValue: "high",
55 promptInjectedValues: ["ultrathink"],
56};
57
58const fastModeDescriptor: ProviderOptionDescriptor = {
59 id: "fastMode",
60 label: "Fast mode",
61 type: "boolean",
62 currentValue: false,
63};
64
65function baseInput() {
66 const models = [
67 modelOption("gpt-current", { isDefault: true }),
68 modelOption("gpt-next"),
69 modelOption("gpt-old", { isLegacy: true }),
70 ];
71 return {
72 providerGroups: [group(models)],
73 selectedModel: models[0]?.selection ?? null,
74 optionDescriptors: [effortDescriptor, fastModeDescriptor],
75 runtimeMode: "auto",
76 } as const;
77}
78
79function eventFor(menu: ReturnType<typeof buildThreadSettingsMenu>, id: string | undefined) {
80 return id === undefined ? undefined : menu.events.get(id);
81}
82
83describe("buildThreadSettingsMenu", () => {
84 it("orders the top level as model, options, runtime", () => {
85 const menu = buildThreadSettingsMenu(baseInput());
86
87 expect(menu.actions.map((action) => action.title)).toEqual([
88 "Model",
89 "Reasoning",
90 "Fast mode",
91 "Runtime",
92 ]);
93 });
94
95 it("summarizes the current choice on each submenu row", () => {
96 const menu = buildThreadSettingsMenu(baseInput());
97
98 expect(menu.actions.find((action) => action.title === "Model")?.subtitle).toBe("gpt-current");
99 expect(menu.actions.find((action) => action.title === "Reasoning")?.subtitle).toBe("High");
100 expect(menu.actions.find((action) => action.title === "Runtime")?.subtitle).toBe("Auto");
101 });
102
103 it("checkmarks the selected model and resolves selection events", () => {
104 const menu = buildThreadSettingsMenu(baseInput());
105
106 const modelItems = menu.actions.find((action) => action.title === "Model")?.subactions ?? [];
107 const current = modelItems.find((action) => action.title === "gpt-current");
108 expect(current?.state).toBe("on");
109 expect(current?.subtitle).toBe("Default");
110 expect(modelItems.find((action) => action.title === "gpt-next")?.state).toBe("off");
111
112 const event = eventFor(menu, modelItems.find((action) => action.title === "gpt-next")?.id);
113 expect(event?.type).toBe("select-model");
114 expect(event?.type === "select-model" ? event.option.selection.model : null).toBe("gpt-next");
115 });
116
117 it("folds unselected legacy models behind a nested submenu", () => {
118 const menu = buildThreadSettingsMenu(baseInput());
119
120 const modelItems = menu.actions.find((action) => action.title === "Model")?.subactions ?? [];
121 expect(modelItems.map((action) => action.title)).toEqual([
122 "gpt-current",
123 "gpt-next",
124 "Legacy Models",
125 ]);
126 expect(
127 modelItems
128 .find((action) => action.title === "Legacy Models")
129 ?.subactions?.map((action) => action.title),
130 ).toEqual(["gpt-old"]);
131 });
132
133 it("keeps a selected legacy model in the main list", () => {
134 const input = baseInput();
135 const legacy = input.providerGroups[0]?.models.find((model) => model.isLegacy);
136 const menu = buildThreadSettingsMenu({
137 ...input,
138 selectedModel: legacy?.selection ?? null,
139 });
140
141 const modelItems = menu.actions.find((action) => action.title === "Model")?.subactions ?? [];
142 expect(modelItems.map((action) => action.title)).toEqual([
143 "gpt-current",
144 "gpt-next",
145 "gpt-old",
146 ]);
147 expect(modelItems.find((action) => action.title === "gpt-old")?.state).toBe("on");
148 });
149
150 it("hides prompt-injected and workflow-trigger efforts but still summarizes them", () => {
151 const menu = buildThreadSettingsMenu({
152 ...baseInput(),
153 optionDescriptors: [{ ...effortDescriptor, currentValue: "ultracode" }],
154 });
155
156 const reasoning = menu.actions.find((action) => action.title === "Reasoning");
157 expect(reasoning?.subactions?.map((action) => action.title)).toEqual(["Low", "Medium", "High"]);
158 // The hidden value stays visible as the current summary; it just can't be
159 // picked from the phone.
160 expect(reasoning?.subtitle).toBe("Ultracode");
161 expect(reasoning?.subactions?.every((action) => action.state === "off")).toBe(true);
162 });
163
164 it("resolves select-option and runtime events with checkmarked current values", () => {
165 const menu = buildThreadSettingsMenu(baseInput());
166
167 const reasoningItems =
168 menu.actions.find((action) => action.title === "Reasoning")?.subactions ?? [];
169 expect(reasoningItems.find((action) => action.title === "High")?.state).toBe("on");
170 expect(eventFor(menu, reasoningItems.find((action) => action.title === "Low")?.id)).toEqual({
171 type: "set-option",
172 optionId: "effort",
173 value: "low",
174 });
175
176 const runtimeItems =
177 menu.actions.find((action) => action.title === "Runtime")?.subactions ?? [];
178 expect(runtimeItems.find((action) => action.title === "Auto")?.state).toBe("on");
179 expect(
180 eventFor(menu, runtimeItems.find((action) => action.title === "Full access")?.id),
181 ).toEqual({ type: "set-runtime", mode: "full-access" });
182 });
183
184 it("toggles boolean options with the inverted current value", () => {
185 const menu = buildThreadSettingsMenu(baseInput());
186
187 const fastMode = menu.actions.find((action) => action.title === "Fast mode");
188 expect(fastMode?.state).toBe("off");
189 expect(fastMode?.subactions).toBeUndefined();
190 expect(eventFor(menu, fastMode?.id)).toEqual({
191 type: "set-option",
192 optionId: "fastMode",
193 value: true,
194 });
195
196 const enabled = buildThreadSettingsMenu({
197 ...baseInput(),
198 optionDescriptors: [{ ...fastModeDescriptor, currentValue: true }],
199 });
200 const enabledRow = enabled.actions.find((action) => action.title === "Fast mode");
201 expect(enabledRow?.state).toBe("on");
202 expect(eventFor(enabled, enabledRow?.id)).toEqual({
203 type: "set-option",
204 optionId: "fastMode",
205 value: false,
206 });
207 });
208
209 it("keeps the menu presented only for top-level toggles", () => {
210 const menu = buildThreadSettingsMenu(baseInput());
211
212 // Root-level boolean toggles refresh in place with clean chrome, so they
213 // keep the menu presented.
214 expect(
215 menu.actions.find((action) => action.title === "Fast mode")?.attributes?.keepsMenuPresented,
216 ).toBe(true);
217
218 // Picks inside nested submenus close the menu: staying presented leaves
219 // the submenu on screen with an expanded-submenu header, and the
220 // bottom-anchored collapse back out drops by the levels' height delta.
221 const expected = undefined;
222 const modelItems = menu.actions.find((action) => action.title === "Model")?.subactions ?? [];
223 const reasoningItems =
224 menu.actions.find((action) => action.title === "Reasoning")?.subactions ?? [];
225 const runtimeItems =
226 menu.actions.find((action) => action.title === "Runtime")?.subactions ?? [];
227 const nestedPicks = [...modelItems, ...reasoningItems, ...runtimeItems].filter(
228 (action) => action.subactions === undefined,
229 );
230 expect(nestedPicks.length).toBeGreaterThan(0);
231 expect(nestedPicks.every((action) => action.attributes?.keepsMenuPresented === expected)).toBe(
232 true,
233 );
234 });
235
236 it("sections models by provider only when multiple groups are offered", () => {
237 const codexModels = [modelOption("gpt-current", { isDefault: true })];
238 const claudeModels = [modelOption("fable-5", { providerKey: "claude" })];
239 const menu = buildThreadSettingsMenu({
240 providerGroups: [group(codexModels), group(claudeModels)],
241 selectedModel: codexModels[0]?.selection ?? null,
242 optionDescriptors: [],
243 runtimeMode: "auto",
244 });
245
246 const modelItems = menu.actions.find((action) => action.title === "Model")?.subactions ?? [];
247 expect(
248 modelItems.map((action) => ({ title: action.title, inline: action.displayInline ?? false })),
249 ).toEqual([
250 { title: "Codex", inline: true },
251 { title: "Claude", inline: true },
252 ]);
253 const claudeSection = modelItems.find((action) => action.title === "Claude");
254 expect(claudeSection?.subactions?.map((action) => action.title)).toEqual(["fable-5"]);
255 });
256
257 const eventTypes = (menu: ReturnType<typeof buildThreadSettingsMenu>) => {
258 const types = new Set<ThreadSettingsMenuEvent["type"]>();
259 for (const event of menu.events.values()) {
260 types.add(event.type);
261 }
262 return types;
263 };
264
265 it("registers an event for every leaf action id", () => {
266 const menu = buildThreadSettingsMenu(baseInput());
267 const leafIds: string[] = [];
268 const collect = (items: ReadonlyArray<{ id?: string; subactions?: unknown[] }>) => {
269 for (const item of items) {
270 if (Array.isArray(item.subactions) && item.subactions.length > 0) {
271 collect(item.subactions as ReadonlyArray<{ id?: string; subactions?: unknown[] }>);
272 } else if (item.id !== undefined) {
273 leafIds.push(item.id);
274 }
275 }
276 };
277 collect(menu.actions);
278
279 for (const id of leafIds) {
280 expect(menu.events.get(id), `missing event for ${id}`).toBeDefined();
281 }
282 expect(eventTypes(menu)).toEqual(new Set(["select-model", "set-option", "set-runtime"]));
283 });
284});