Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import { ProviderDriverKind, ProviderInstanceId, type ModelCapabilities } from "@t3tools/contracts";
3
4import {
5 applyClaudePromptEffortPrefix,
6 buildExplicitProviderOptionSelectionsFromDescriptors,
7 buildProviderOptionSelectionsFromDescriptors,
8 createModelCapabilities,
9 createModelSelection,
10 formatCodexModelName,
11 formatModelSlugName,
12 getProviderOptionCurrentLabel,
13 getModelSelectionBooleanOptionValue,
14 getModelSelectionStringOptionValue,
15 getProviderOptionDescriptors,
16 readCustomModelEntries,
17 toCustomModelSetting,
18 getProviderOptionBooleanSelectionValue,
19 getProviderOptionStringSelectionValue,
20 normalizeCustomModelSlug,
21 normalizeModelSlug,
22 modelSelectionsEqual,
23} from "./model.ts";
24
25it("keeps the Codex catalog display formatting", () => {
26 expect(formatCodexModelName("gpt-5.3-codex-spark")).toBe("GPT-5.3-Codex-Spark");
27 expect(formatCodexModelName("GPT Test")).toBe("GPT Test");
28});
29
30it.each([
31 ["gpt-5.4", "GPT-5.4"],
32 ["claude-opus-4-6", "Claude Opus 4.6"],
33 ["claude-sonnet-4-20250514", "Claude Sonnet 4 20250514"],
34 ["claude-opus-4-6[1m]", "Claude Opus 4.6[1m]"],
35 ["openai/gpt-5.4-mini", "openai/GPT-5.4-Mini"],
36 ["gemini-2.5-pro-preview-06-05", "Gemini 2.5 Pro Preview 06 05"],
37 ["custom/model-v2", "custom/model-v2"],
38 ["gpt-proxy", "gpt-proxy"],
39 ["My Custom Model", "My Custom Model"],
40])("formats a known model ID without losing its qualifiers: %s", (slug, expected) => {
41 expect(formatModelSlugName(slug)).toBe(expected);
42});
43
44const codexCaps: ModelCapabilities = createModelCapabilities({
45 optionDescriptors: [
46 {
47 id: "reasoningEffort",
48 label: "Reasoning",
49 type: "select",
50 options: [
51 { id: "xhigh", label: "Extra High" },
52 { id: "high", label: "High", isDefault: true },
53 ],
54 currentValue: "high",
55 },
56 {
57 id: "fastMode",
58 label: "Fast Mode",
59 type: "boolean",
60 },
61 ],
62});
63
64describe("model slug normalization", () => {
65 it("preserves exact custom slugs instead of expanding provider aliases", () => {
66 const claude = ProviderDriverKind.make("claudeAgent");
67
68 expect(normalizeModelSlug("opus", claude)).toBe("opus");
69 expect(normalizeCustomModelSlug(" opus ")).toBe("opus");
70 });
71});
72
73const claudeCaps: ModelCapabilities = createModelCapabilities({
74 optionDescriptors: [
75 {
76 id: "effort",
77 label: "Reasoning",
78 type: "select",
79 options: [
80 { id: "medium", label: "Medium" },
81 { id: "high", label: "High", isDefault: true },
82 { id: "ultrathink", label: "Ultrathink" },
83 ],
84 currentValue: "high",
85 promptInjectedValues: ["ultrathink"],
86 },
87 {
88 id: "contextWindow",
89 label: "Context Window",
90 type: "select",
91 options: [
92 { id: "200k", label: "200k" },
93 { id: "1m", label: "1M", isDefault: true },
94 ],
95 currentValue: "1m",
96 },
97 ],
98});
99
100describe("descriptor helpers", () => {
101 it("applies selection values to capability descriptors", () => {
102 expect(
103 getProviderOptionDescriptors({
104 caps: claudeCaps,
105 selections: [
106 { id: "effort", value: "medium" },
107 { id: "contextWindow", value: "200k" },
108 ],
109 }),
110 ).toEqual([
111 {
112 id: "effort",
113 label: "Reasoning",
114 type: "select",
115 options: [
116 { id: "medium", label: "Medium" },
117 { id: "high", label: "High", isDefault: true },
118 { id: "ultrathink", label: "Ultrathink" },
119 ],
120 currentValue: "medium",
121 promptInjectedValues: ["ultrathink"],
122 },
123 {
124 id: "contextWindow",
125 label: "Context Window",
126 type: "select",
127 options: [
128 { id: "200k", label: "200k" },
129 { id: "1m", label: "1M", isDefault: true },
130 ],
131 currentValue: "200k",
132 },
133 ]);
134 });
135
136 it("builds wire-format option selections from descriptors", () => {
137 const descriptors = getProviderOptionDescriptors({
138 caps: codexCaps,
139 selections: [
140 { id: "reasoningEffort", value: "high" },
141 { id: "fastMode", value: true },
142 ],
143 });
144
145 expect(buildProviderOptionSelectionsFromDescriptors(descriptors)).toEqual([
146 { id: "reasoningEffort", value: "high" },
147 { id: "fastMode", value: true },
148 ]);
149 });
150
151 it("builds dispatch options only from explicit selections", () => {
152 const descriptors = getProviderOptionDescriptors({
153 caps: codexCaps,
154 selections: [{ id: "fastMode", value: true }],
155 });
156
157 expect(buildExplicitProviderOptionSelectionsFromDescriptors(descriptors, undefined)).toBe(
158 undefined,
159 );
160 expect(
161 buildExplicitProviderOptionSelectionsFromDescriptors(descriptors, [
162 { id: "fastMode", value: true },
163 ]),
164 ).toEqual([{ id: "fastMode", value: true }]);
165 });
166
167 it("stores option selection arrays in model selections", () => {
168 expect(
169 createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.4", [
170 { id: "reasoningEffort", value: "high" },
171 { id: "fastMode", value: true },
172 ]),
173 ).toEqual({
174 instanceId: "codex",
175 model: "gpt-5.4",
176 options: [
177 { id: "reasoningEffort", value: "high" },
178 { id: "fastMode", value: true },
179 ],
180 });
181 });
182
183 it("reads typed option selection values", () => {
184 const selection = createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.4", [
185 { id: "reasoningEffort", value: "high" },
186 { id: "fastMode", value: true },
187 ]);
188
189 expect(getProviderOptionStringSelectionValue(selection.options, "reasoningEffort")).toBe(
190 "high",
191 );
192 expect(getProviderOptionStringSelectionValue(selection.options, "fastMode")).toBeUndefined();
193 expect(getProviderOptionBooleanSelectionValue(selection.options, "fastMode")).toBe(true);
194 expect(
195 getProviderOptionBooleanSelectionValue(selection.options, "reasoningEffort"),
196 ).toBeUndefined();
197 expect(getModelSelectionStringOptionValue(selection, "reasoningEffort")).toBe("high");
198 expect(getModelSelectionBooleanOptionValue(selection, "fastMode")).toBe(true);
199 });
200
201 it("compares complete model selections independent of option ordering", () => {
202 const left = createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.4", [
203 { id: "reasoningEffort", value: "high" },
204 { id: "fastMode", value: true },
205 ]);
206 const reordered = createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.4", [
207 { id: "fastMode", value: true },
208 { id: "reasoningEffort", value: "high" },
209 ]);
210
211 expect(modelSelectionsEqual(left, reordered)).toBe(true);
212 expect(
213 modelSelectionsEqual(left, {
214 ...reordered,
215 options: [
216 { id: "fastMode", value: true },
217 { id: "reasoningEffort", value: "medium" },
218 ],
219 }),
220 ).toBe(false);
221 expect(modelSelectionsEqual(left, { ...reordered, model: "gpt-5.5" })).toBe(false);
222 });
223});
224
225describe("applyClaudePromptEffortPrefix", () => {
226 it("keeps slash commands intact when ultrathink is selected", () => {
227 expect(applyClaudePromptEffortPrefix("/compact", "ultrathink")).toBe("/compact");
228 expect(applyClaudePromptEffortPrefix(" /compact keep recent errors ", "ultrathink")).toBe(
229 "/compact keep recent errors",
230 );
231 expect(applyClaudePromptEffortPrefix(" /review src/model.ts ", "ultrathink")).toBe(
232 "/review src/model.ts",
233 );
234 expect(applyClaudePromptEffortPrefix("/security-review", "ultrathink")).toBe(
235 "/security-review",
236 );
237 expect(applyClaudePromptEffortPrefix("/plugin:skill run", "ultrathink")).toBe(
238 "/plugin:skill run",
239 );
240 expect(applyClaudePromptEffortPrefix("/deploy.prod to staging", "ultrathink")).toBe(
241 "/deploy.prod to staging",
242 );
243 });
244
245 it("still adds the ultrathink prefix to ordinary prompts", () => {
246 expect(applyClaudePromptEffortPrefix("Investigate this failure", "ultrathink")).toBe(
247 "Ultrathink:\nInvestigate this failure",
248 );
249 expect(applyClaudePromptEffortPrefix("/home/theo/app.ts crashed on load", "ultrathink")).toBe(
250 "Ultrathink:\n/home/theo/app.ts crashed on load",
251 );
252 });
253});
254
255describe("readCustomModelEntries", () => {
256 const capabilities: ModelCapabilities = {
257 optionDescriptors: [
258 {
259 id: "effort",
260 label: "Reasoning",
261 type: "select",
262 options: [{ id: "high", label: "High", isDefault: true }],
263 currentValue: "high",
264 },
265 ],
266 };
267
268 it("resolves bare slugs and entries, trimming and deduplicating on slug", () => {
269 expect(
270 readCustomModelEntries([
271 " bare ",
272 { slug: "named", name: " Named ", capabilities },
273 "bare",
274 { slug: "named", name: "Second" },
275 "",
276 { name: "no slug" },
277 42,
278 ]),
279 ).toEqual([
280 { slug: "bare", name: "bare", capabilities: null },
281 { slug: "named", name: "Named", capabilities },
282 ]);
283 });
284
285 it("drops unparseable capabilities but keeps the entry", () => {
286 expect(
287 readCustomModelEntries([{ slug: "x", capabilities: { optionDescriptors: "nope" } }]),
288 ).toEqual([{ slug: "x", name: "x", capabilities: null }]);
289 expect(readCustomModelEntries("not a list")).toEqual([]);
290 });
291
292 it("writes the compact stored shape back", () => {
293 expect(toCustomModelSetting({ slug: "x", name: "x", capabilities: null })).toBe("x");
294 expect(
295 toCustomModelSetting({ slug: "x", name: "x", capabilities: { optionDescriptors: [] } }),
296 ).toBe("x");
297 expect(toCustomModelSetting({ slug: "x", name: "X", capabilities })).toEqual({
298 slug: "x",
299 name: "X",
300 capabilities,
301 });
302 });
303});
304
305describe("provider-reported option display", () => {
306 const selection = createModelSelection(ProviderInstanceId.make("opencode"), "ling");
307 const reported = { ...selection, options: [{ id: "variant", value: "default" }] };
308 const descriptor = {
309 id: "variant",
310 label: "Reasoning",
311 type: "select" as const,
312 options: [
313 { id: "none", label: "None" },
314 { id: "thinking", label: "Thinking" },
315 ],
316 };
317
318 it("shows explicit reports without adding a choice or a dispatch option", () => {
319 expect(getProviderOptionCurrentLabel(descriptor, selection, reported)).toBe("Default");
320 expect(
321 getProviderOptionCurrentLabel(descriptor, selection, {
322 ...reported,
323 options: [{ id: "variant", value: "thinking" }],
324 }),
325 ).toBe("Thinking");
326 expect(
327 getProviderOptionCurrentLabel(
328 { ...descriptor, currentValue: "none" },
329 { ...selection, options: [{ id: "variant", value: "none" }] },
330 reported,
331 ),
332 ).toBe("None");
333 expect(descriptor.options.map((option) => option.id)).toEqual(["none", "thinking"]);
334 expect(buildProviderOptionSelectionsFromDescriptors([descriptor])).toBeUndefined();
335 expect(getProviderOptionCurrentLabel(descriptor, selection)).toBe("Unknown");
336 const effortDescriptor = { ...descriptor, id: "effort", currentValue: "default" };
337 expect(getProviderOptionCurrentLabel(effortDescriptor, selection)).toBeUndefined();
338 expect(
339 getProviderOptionCurrentLabel(effortDescriptor, selection, {
340 ...reported,
341 model: "other",
342 options: [{ id: "effort", value: "default" }],
343 }),
344 ).toBeUndefined();
345 expect(
346 getProviderOptionCurrentLabel(effortDescriptor, selection, {
347 ...reported,
348 options: [{ id: "effort", value: "default" }],
349 }),
350 ).toBe("Default");
351 expect(
352 getProviderOptionCurrentLabel({ ...descriptor, currentValue: "thinking" }, selection),
353 ).toBe("Unknown");
354 });
355
356 it.each([
357 { ...selection, model: "other" },
358 { ...selection, instanceId: ProviderInstanceId.make("other") },
359 { ...selection, options: [{ id: "variant", value: "none" }] },
360 ])("ignores reports after changing the model, instance, or option: %j", (selected) => {
361 expect(getProviderOptionCurrentLabel(descriptor, selected, reported)).toBe("Unknown");
362 });
363});