Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import { ProviderInstanceId, type ModelCapabilities } from "@t3tools/contracts";
3
4import {
5 applyClaudePromptEffortPrefix,
6 buildExplicitProviderOptionSelectionsFromDescriptors,
7 buildProviderOptionSelectionsFromDescriptors,
8 createModelCapabilities,
9 createModelSelection,
10 DEFAULT_VARIANT_CHOICE_ID,
11 getProviderOptionCurrentLabel,
12 getProviderOptionCurrentValue,
13 getProviderOptionDescriptors,
14 readCustomModelEntries,
15 modelSelectionsEqual,
16 pickModelSelection,
17 withImplicitFastModeDefault,
18} from "./model.ts";
19
20const codexCaps: ModelCapabilities = createModelCapabilities({
21 optionDescriptors: [
22 {
23 id: "reasoningEffort",
24 label: "Reasoning",
25 type: "select",
26 options: [
27 { id: "xhigh", label: "Extra High" },
28 { id: "high", label: "High", isDefault: true },
29 ],
30 currentValue: "high",
31 },
32 {
33 id: "fastMode",
34 label: "Fast Mode",
35 type: "boolean",
36 },
37 ],
38});
39
40describe("descriptor helpers", () => {
41 it("builds dispatch options only from explicit selections", () => {
42 const descriptors = getProviderOptionDescriptors({
43 caps: codexCaps,
44 selections: [{ id: "fastMode", value: true }],
45 });
46
47 expect(buildExplicitProviderOptionSelectionsFromDescriptors(descriptors, undefined)).toBe(
48 undefined,
49 );
50 expect(
51 buildExplicitProviderOptionSelectionsFromDescriptors(descriptors, [
52 { id: "fastMode", value: true },
53 ]),
54 ).toEqual([{ id: "fastMode", value: true }]);
55 });
56
57 it("dispatches an old Ultracode effort as Extra High with the Ultracode toggle", () => {
58 const legacy = [{ id: "effort", value: "ultracode" }];
59 const descriptors = getProviderOptionDescriptors({
60 caps: createModelCapabilities({
61 optionDescriptors: [
62 {
63 id: "effort",
64 label: "Reasoning",
65 type: "select",
66 options: [
67 { id: "high", label: "High", isDefault: true },
68 { id: "xhigh", label: "Extra High" },
69 ],
70 },
71 { id: "ultracode", label: "Ultracode", type: "boolean" },
72 ],
73 }),
74 selections: legacy,
75 });
76
77 expect(buildExplicitProviderOptionSelectionsFromDescriptors(descriptors, legacy)).toEqual([
78 { id: "effort", value: "xhigh" },
79 { id: "ultracode", value: true },
80 ]);
81 });
82
83 it("compares complete model selections independent of option ordering", () => {
84 const left = createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.4", [
85 { id: "reasoningEffort", value: "high" },
86 { id: "fastMode", value: true },
87 ]);
88 const reordered = createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.4", [
89 { id: "fastMode", value: true },
90 { id: "reasoningEffort", value: "high" },
91 ]);
92
93 expect(modelSelectionsEqual(left, reordered)).toBe(true);
94 expect(
95 modelSelectionsEqual(left, {
96 ...reordered,
97 options: [
98 { id: "fastMode", value: true },
99 { id: "reasoningEffort", value: "medium" },
100 ],
101 }),
102 ).toBe(false);
103 expect(modelSelectionsEqual(left, { ...reordered, model: "gpt-5.5" })).toBe(false);
104 });
105
106 it("keeps the options when the current model is picked again", () => {
107 const codex = ProviderInstanceId.make("codex");
108 const current = createModelSelection(codex, "gpt-5.4", [
109 { id: "reasoningEffort", value: "high" },
110 ]);
111
112 expect(pickModelSelection(current, codex, "gpt-5.4")).toBe(current);
113 expect(pickModelSelection(current, codex, "gpt-5.5")).toEqual({
114 instanceId: "codex",
115 model: "gpt-5.5",
116 });
117 expect(pickModelSelection(current, ProviderInstanceId.make("codex_work"), "gpt-5.4")).toEqual({
118 instanceId: "codex_work",
119 model: "gpt-5.4",
120 });
121 expect(pickModelSelection(null, codex, "gpt-5.4")).toEqual({
122 instanceId: "codex",
123 model: "gpt-5.4",
124 });
125 });
126});
127
128describe("applyClaudePromptEffortPrefix", () => {
129 it("keeps slash commands intact when ultrathink is selected", () => {
130 expect(applyClaudePromptEffortPrefix("/compact", "ultrathink")).toBe("/compact");
131 expect(applyClaudePromptEffortPrefix(" /compact keep recent errors ", "ultrathink")).toBe(
132 "/compact keep recent errors",
133 );
134 expect(applyClaudePromptEffortPrefix(" /review src/model.ts ", "ultrathink")).toBe(
135 "/review src/model.ts",
136 );
137 expect(applyClaudePromptEffortPrefix("/security-review", "ultrathink")).toBe(
138 "/security-review",
139 );
140 expect(applyClaudePromptEffortPrefix("/plugin:skill run", "ultrathink")).toBe(
141 "/plugin:skill run",
142 );
143 expect(applyClaudePromptEffortPrefix("/deploy.prod to staging", "ultrathink")).toBe(
144 "/deploy.prod to staging",
145 );
146 });
147
148 it("still adds the ultrathink prefix to ordinary prompts", () => {
149 expect(applyClaudePromptEffortPrefix("Investigate this failure", "ultrathink")).toBe(
150 "Ultrathink:\nInvestigate this failure",
151 );
152 expect(applyClaudePromptEffortPrefix("/home/theo/app.ts crashed on load", "ultrathink")).toBe(
153 "Ultrathink:\n/home/theo/app.ts crashed on load",
154 );
155 });
156});
157
158describe("readCustomModelEntries", () => {
159 const capabilities: ModelCapabilities = {
160 optionDescriptors: [
161 {
162 id: "effort",
163 label: "Reasoning",
164 type: "select",
165 options: [{ id: "high", label: "High", isDefault: true }],
166 currentValue: "high",
167 },
168 ],
169 };
170
171 it("resolves bare slugs and entries, trimming and deduplicating on slug", () => {
172 expect(
173 readCustomModelEntries([
174 " bare ",
175 { slug: "named", name: " Named ", capabilities },
176 "bare",
177 { slug: "named", name: "Second" },
178 "",
179 { name: "no slug" },
180 42,
181 ]),
182 ).toEqual([
183 { slug: "bare", name: "bare", capabilities: null },
184 { slug: "named", name: "Named", capabilities },
185 ]);
186 });
187
188 it("drops unparseable capabilities but keeps the entry", () => {
189 expect(
190 readCustomModelEntries([{ slug: "x", capabilities: { optionDescriptors: "nope" } }]),
191 ).toEqual([{ slug: "x", name: "x", capabilities: null }]);
192 expect(readCustomModelEntries("not a list")).toEqual([]);
193 });
194});
195
196describe("provider-reported option display", () => {
197 const selection = createModelSelection(ProviderInstanceId.make("opencode"), "ling");
198 const reported = { ...selection, options: [{ id: "variant", value: "default" }] };
199 const descriptor = {
200 id: "variant",
201 label: "Reasoning",
202 type: "select" as const,
203 options: [
204 { id: "none", label: "None" },
205 { id: "thinking", label: "Thinking" },
206 ],
207 };
208
209 it("shows explicit reports without adding a choice or a dispatch option", () => {
210 expect(getProviderOptionCurrentLabel(descriptor, selection, reported)).toBe("Default");
211 expect(
212 getProviderOptionCurrentLabel(descriptor, selection, {
213 ...reported,
214 options: [{ id: "variant", value: "thinking" }],
215 }),
216 ).toBe("Thinking");
217 expect(
218 getProviderOptionCurrentLabel(
219 { ...descriptor, currentValue: "none" },
220 { ...selection, options: [{ id: "variant", value: "none" }] },
221 reported,
222 ),
223 ).toBe("None");
224 expect(descriptor.options.map((option) => option.id)).toEqual(["none", "thinking"]);
225 expect(buildProviderOptionSelectionsFromDescriptors([descriptor])).toBeUndefined();
226 expect(getProviderOptionCurrentLabel(descriptor, selection)).toBe("Unknown");
227 const effortDescriptor = { ...descriptor, id: "effort", currentValue: "default" };
228 expect(getProviderOptionCurrentLabel(effortDescriptor, selection)).toBeUndefined();
229 expect(
230 getProviderOptionCurrentLabel(effortDescriptor, selection, {
231 ...reported,
232 model: "other",
233 options: [{ id: "effort", value: "default" }],
234 }),
235 ).toBeUndefined();
236 expect(
237 getProviderOptionCurrentLabel(effortDescriptor, selection, {
238 ...reported,
239 options: [{ id: "effort", value: "default" }],
240 }),
241 ).toBe("Default");
242 expect(
243 getProviderOptionCurrentLabel({ ...descriptor, currentValue: "thinking" }, selection),
244 ).toBe("Unknown");
245 });
246
247 it("shows the catalog's Default choice while no variant is chosen", () => {
248 const withDefault = {
249 ...descriptor,
250 options: [{ id: DEFAULT_VARIANT_CHOICE_ID, label: "Default" }, ...descriptor.options],
251 currentValue: DEFAULT_VARIANT_CHOICE_ID,
252 };
253 expect(getProviderOptionCurrentValue(withDefault, selection)).toBe(DEFAULT_VARIANT_CHOICE_ID);
254 expect(getProviderOptionCurrentLabel(withDefault, selection)).toBe("Default");
255 expect(
256 getProviderOptionCurrentLabel(withDefault, selection, {
257 ...reported,
258 options: [{ id: "variant", value: "thinking" }],
259 }),
260 ).toBe("Thinking");
261 });
262
263 it.each([
264 { ...selection, model: "other" },
265 { ...selection, instanceId: ProviderInstanceId.make("other") },
266 { ...selection, options: [{ id: "variant", value: "none" }] },
267 ])("ignores reports after changing the model, instance, or option: %j", (selected) => {
268 expect(getProviderOptionCurrentLabel(descriptor, selected, reported)).toBe("Unknown");
269 });
270});
271
272describe("withImplicitFastModeDefault", () => {
273 // regression: #2981 a remembered Fast mode choice must survive the implicit default on new chats
274 it("injects fastMode false only when the model exposes fastMode and no selection exists", () => {
275 const caps = createModelCapabilities({
276 optionDescriptors: [{ id: "fastMode", label: "Fast", type: "boolean", currentValue: true }],
277 });
278
279 expect(withImplicitFastModeDefault(caps, undefined)).toEqual([
280 { id: "fastMode", value: false },
281 ]);
282 expect(withImplicitFastModeDefault(caps, [{ id: "fastMode", value: true }])).toEqual([
283 { id: "fastMode", value: true },
284 ]);
285 });
286});