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

model.test.ts

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