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

model.test.ts

347 lines11.4 KB

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("reads typed option selection values", () => {
168 const selection = createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.4", [
169 { id: "reasoningEffort", value: "high" },
170 { id: "fastMode", value: true },
171 ]);
172​
173 expect(getProviderOptionStringSelectionValue(selection.options, "reasoningEffort")).toBe(
174 "high",
175 );
176 expect(getProviderOptionStringSelectionValue(selection.options, "fastMode")).toBeUndefined();
177 expect(getProviderOptionBooleanSelectionValue(selection.options, "fastMode")).toBe(true);
178 expect(
179 getProviderOptionBooleanSelectionValue(selection.options, "reasoningEffort"),
180 ).toBeUndefined();
181 expect(getModelSelectionStringOptionValue(selection, "reasoningEffort")).toBe("high");
182 expect(getModelSelectionBooleanOptionValue(selection, "fastMode")).toBe(true);
183 });
184​
185 it("compares complete model selections independent of option ordering", () => {
186 const left = createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.4", [
187 { id: "reasoningEffort", value: "high" },
188 { id: "fastMode", value: true },
189 ]);
190 const reordered = createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.4", [
191 { id: "fastMode", value: true },
192 { id: "reasoningEffort", value: "high" },
193 ]);
194​
195 expect(modelSelectionsEqual(left, reordered)).toBe(true);
196 expect(
197 modelSelectionsEqual(left, {
198 ...reordered,
199 options: [
200 { id: "fastMode", value: true },
201 { id: "reasoningEffort", value: "medium" },
202 ],
203 }),
204 ).toBe(false);
205 expect(modelSelectionsEqual(left, { ...reordered, model: "gpt-5.5" })).toBe(false);
206 });
207});
208​
209describe("applyClaudePromptEffortPrefix", () => {
210 it("keeps slash commands intact when ultrathink is selected", () => {
211 expect(applyClaudePromptEffortPrefix("/compact", "ultrathink")).toBe("/compact");
212 expect(applyClaudePromptEffortPrefix(" /compact keep recent errors ", "ultrathink")).toBe(
213 "/compact keep recent errors",
214 );
215 expect(applyClaudePromptEffortPrefix(" /review src/model.ts ", "ultrathink")).toBe(
216 "/review src/model.ts",
217 );
218 expect(applyClaudePromptEffortPrefix("/security-review", "ultrathink")).toBe(
219 "/security-review",
220 );
221 expect(applyClaudePromptEffortPrefix("/plugin:skill run", "ultrathink")).toBe(
222 "/plugin:skill run",
223 );
224 expect(applyClaudePromptEffortPrefix("/deploy.prod to staging", "ultrathink")).toBe(
225 "/deploy.prod to staging",
226 );
227 });
228​
229 it("still adds the ultrathink prefix to ordinary prompts", () => {
230 expect(applyClaudePromptEffortPrefix("Investigate this failure", "ultrathink")).toBe(
231 "Ultrathink:\nInvestigate this failure",
232 );
233 expect(applyClaudePromptEffortPrefix("/home/theo/app.ts crashed on load", "ultrathink")).toBe(
234 "Ultrathink:\n/home/theo/app.ts crashed on load",
235 );
236 });
237});
238​
239describe("readCustomModelEntries", () => {
240 const capabilities: ModelCapabilities = {
241 optionDescriptors: [
242 {
243 id: "effort",
244 label: "Reasoning",
245 type: "select",
246 options: [{ id: "high", label: "High", isDefault: true }],
247 currentValue: "high",
248 },
249 ],
250 };
251​
252 it("resolves bare slugs and entries, trimming and deduplicating on slug", () => {
253 expect(
254 readCustomModelEntries([
255 " bare ",
256 { slug: "named", name: " Named ", capabilities },
257 "bare",
258 { slug: "named", name: "Second" },
259 "",
260 { name: "no slug" },
261 42,
262 ]),
263 ).toEqual([
264 { slug: "bare", name: "bare", capabilities: null },
265 { slug: "named", name: "Named", capabilities },
266 ]);
267 });
268​
269 it("drops unparseable capabilities but keeps the entry", () => {
270 expect(
271 readCustomModelEntries([{ slug: "x", capabilities: { optionDescriptors: "nope" } }]),
272 ).toEqual([{ slug: "x", name: "x", capabilities: null }]);
273 expect(readCustomModelEntries("not a list")).toEqual([]);
274 });
275​
276 it("writes the compact stored shape back", () => {
277 expect(toCustomModelSetting({ slug: "x", name: "x", capabilities: null })).toBe("x");
278 expect(
279 toCustomModelSetting({ slug: "x", name: "x", capabilities: { optionDescriptors: [] } }),
280 ).toBe("x");
281 expect(toCustomModelSetting({ slug: "x", name: "X", capabilities })).toEqual({
282 slug: "x",
283 name: "X",
284 capabilities,
285 });
286 });
287});
288​
289describe("provider-reported option display", () => {
290 const selection = createModelSelection(ProviderInstanceId.make("opencode"), "ling");
291 const reported = { ...selection, options: [{ id: "variant", value: "default" }] };
292 const descriptor = {
293 id: "variant",
294 label: "Reasoning",
295 type: "select" as const,
296 options: [
297 { id: "none", label: "None" },
298 { id: "thinking", label: "Thinking" },
299 ],
300 };
301​
302 it("shows explicit reports without adding a choice or a dispatch option", () => {
303 expect(getProviderOptionCurrentLabel(descriptor, selection, reported)).toBe("Default");
304 expect(
305 getProviderOptionCurrentLabel(descriptor, selection, {
306 ...reported,
307 options: [{ id: "variant", value: "thinking" }],
308 }),
309 ).toBe("Thinking");
310 expect(
311 getProviderOptionCurrentLabel(
312 { ...descriptor, currentValue: "none" },
313 { ...selection, options: [{ id: "variant", value: "none" }] },
314 reported,
315 ),
316 ).toBe("None");
317 expect(descriptor.options.map((option) => option.id)).toEqual(["none", "thinking"]);
318 expect(buildProviderOptionSelectionsFromDescriptors([descriptor])).toBeUndefined();
319 expect(getProviderOptionCurrentLabel(descriptor, selection)).toBe("Unknown");
320 const effortDescriptor = { ...descriptor, id: "effort", currentValue: "default" };
321 expect(getProviderOptionCurrentLabel(effortDescriptor, selection)).toBeUndefined();
322 expect(
323 getProviderOptionCurrentLabel(effortDescriptor, selection, {
324 ...reported,
325 model: "other",
326 options: [{ id: "effort", value: "default" }],
327 }),
328 ).toBeUndefined();
329 expect(
330 getProviderOptionCurrentLabel(effortDescriptor, selection, {
331 ...reported,
332 options: [{ id: "effort", value: "default" }],
333 }),
334 ).toBe("Default");
335 expect(
336 getProviderOptionCurrentLabel({ ...descriptor, currentValue: "thinking" }, selection),
337 ).toBe("Unknown");
338 });
339​
340 it.each([
341 { ...selection, model: "other" },
342 { ...selection, instanceId: ProviderInstanceId.make("other") },
343 { ...selection, options: [{ id: "variant", value: "none" }] },
344 ])("ignores reports after changing the model, instance, or option: %j", (selected) => {
345 expect(getProviderOptionCurrentLabel(descriptor, selected, reported)).toBe("Unknown");
346 });
347});