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

model.test.ts

332 lines11.5 KB

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