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

model.test.ts

311 lines9.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 getModelInputCapabilities,
11 getModelSelectionBooleanOptionValue,
12 getModelSelectionStringOptionValue,
13 getProviderOptionDescriptors,
14 readCustomModelEntries,
15 toCustomModelSetting,
16 getProviderOptionBooleanSelectionValue,
17 getProviderOptionStringSelectionValue,
18 normalizeCustomModelSlug,
19 normalizeModelSlug,
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​
42const claudeCaps: ModelCapabilities = createModelCapabilities({
43 optionDescriptors: [
44 {
45 id: "effort",
46 label: "Reasoning",
47 type: "select",
48 options: [
49 { id: "medium", label: "Medium" },
50 { id: "high", label: "High", isDefault: true },
51 { id: "ultrathink", label: "Ultrathink" },
52 ],
53 currentValue: "high",
54 promptInjectedValues: ["ultrathink"],
55 },
56 {
57 id: "contextWindow",
58 label: "Context Window",
59 type: "select",
60 options: [
61 { id: "200k", label: "200k" },
62 { id: "1m", label: "1M", isDefault: true },
63 ],
64 currentValue: "1m",
65 },
66 ],
67});
68​
69describe("descriptor helpers", () => {
70 it("applies selection values to capability descriptors", () => {
71 expect(
72 getProviderOptionDescriptors({
73 caps: claudeCaps,
74 selections: [
75 { id: "effort", value: "medium" },
76 { id: "contextWindow", value: "200k" },
77 ],
78 }),
79 ).toEqual([
80 {
81 id: "effort",
82 label: "Reasoning",
83 type: "select",
84 options: [
85 { id: "medium", label: "Medium" },
86 { id: "high", label: "High", isDefault: true },
87 { id: "ultrathink", label: "Ultrathink" },
88 ],
89 currentValue: "medium",
90 promptInjectedValues: ["ultrathink"],
91 },
92 {
93 id: "contextWindow",
94 label: "Context Window",
95 type: "select",
96 options: [
97 { id: "200k", label: "200k" },
98 { id: "1m", label: "1M", isDefault: true },
99 ],
100 currentValue: "200k",
101 },
102 ]);
103 });
104​
105 it("builds wire-format option selections from descriptors", () => {
106 const descriptors = getProviderOptionDescriptors({
107 caps: codexCaps,
108 selections: [
109 { id: "reasoningEffort", value: "high" },
110 { id: "fastMode", value: true },
111 ],
112 });
113​
114 expect(buildProviderOptionSelectionsFromDescriptors(descriptors)).toEqual([
115 { id: "reasoningEffort", value: "high" },
116 { id: "fastMode", value: true },
117 ]);
118 });
119​
120 it("builds dispatch options only from explicit selections", () => {
121 const descriptors = getProviderOptionDescriptors({
122 caps: codexCaps,
123 selections: [{ id: "fastMode", value: true }],
124 });
125​
126 expect(buildExplicitProviderOptionSelectionsFromDescriptors(descriptors, undefined)).toBe(
127 undefined,
128 );
129 expect(
130 buildExplicitProviderOptionSelectionsFromDescriptors(descriptors, [
131 { id: "fastMode", value: true },
132 ]),
133 ).toEqual([{ id: "fastMode", value: true }]);
134 });
135​
136 it("stores option selection arrays in model selections", () => {
137 expect(
138 createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.4", [
139 { id: "reasoningEffort", value: "high" },
140 { id: "fastMode", value: true },
141 ]),
142 ).toEqual({
143 instanceId: "codex",
144 model: "gpt-5.4",
145 options: [
146 { id: "reasoningEffort", value: "high" },
147 { id: "fastMode", value: true },
148 ],
149 });
150 });
151​
152 it("reads typed option selection values", () => {
153 const selection = createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.4", [
154 { id: "reasoningEffort", value: "high" },
155 { id: "fastMode", value: true },
156 ]);
157​
158 expect(getProviderOptionStringSelectionValue(selection.options, "reasoningEffort")).toBe(
159 "high",
160 );
161 expect(getProviderOptionStringSelectionValue(selection.options, "fastMode")).toBeUndefined();
162 expect(getProviderOptionBooleanSelectionValue(selection.options, "fastMode")).toBe(true);
163 expect(
164 getProviderOptionBooleanSelectionValue(selection.options, "reasoningEffort"),
165 ).toBeUndefined();
166 expect(getModelSelectionStringOptionValue(selection, "reasoningEffort")).toBe("high");
167 expect(getModelSelectionBooleanOptionValue(selection, "fastMode")).toBe(true);
168 });
169});
170​
171describe("model slug normalization", () => {
172 it("preserves exact custom slugs instead of expanding provider aliases", () => {
173 const cursor = ProviderDriverKind.make("cursor");
174​
175 expect(normalizeModelSlug("opus-4.6", cursor)).toBe("claude-opus-4-6");
176 expect(normalizeCustomModelSlug(" opus-4.6 ")).toBe("opus-4.6");
177 });
178});
179​
180describe("input capabilities", () => {
181 it("omits capability fields when no modality is disabled", () => {
182 const caps = createModelCapabilities({ optionDescriptors: [] });
183 expect(caps.inputImages).toBeUndefined();
184 expect(caps.inputAudio).toBeUndefined();
185 expect(caps.inputFiles).toBeUndefined();
186 });
187​
188 it("only records modalities that are explicitly disabled", () => {
189 const caps = createModelCapabilities({
190 optionDescriptors: [],
191 inputImages: false,
192 inputAudio: false,
193 });
194 expect(caps.inputImages).toBe(false);
195 expect(caps.inputAudio).toBe(false);
196 expect(caps.inputFiles).toBeUndefined();
197 });
198​
199 it("ignores explicit true so the wire shape stays minimal", () => {
200 const caps = createModelCapabilities({
201 optionDescriptors: [],
202 inputImages: true,
203 inputFiles: true,
204 });
205 expect(caps.inputImages).toBeUndefined();
206 expect(caps.inputFiles).toBeUndefined();
207 });
208​
209 it("resolves absent fields to supported", () => {
210 expect(getModelInputCapabilities(undefined)).toEqual({
211 images: true,
212 audio: true,
213 files: true,
214 });
215 expect(getModelInputCapabilities({ optionDescriptors: [] })).toEqual({
216 images: true,
217 audio: true,
218 files: true,
219 });
220 });
221​
222 it("resolves explicitly disabled fields to false", () => {
223 expect(
224 getModelInputCapabilities({
225 optionDescriptors: [],
226 inputImages: false,
227 inputAudio: false,
228 }),
229 ).toEqual({ images: false, audio: false, files: true });
230 });
231});
232​
233describe("applyClaudePromptEffortPrefix", () => {
234 it("keeps slash commands intact when ultrathink is selected", () => {
235 expect(applyClaudePromptEffortPrefix("/compact", "ultrathink")).toBe("/compact");
236 expect(applyClaudePromptEffortPrefix(" /compact keep recent errors ", "ultrathink")).toBe(
237 "/compact keep recent errors",
238 );
239 expect(applyClaudePromptEffortPrefix(" /review src/model.ts ", "ultrathink")).toBe(
240 "/review src/model.ts",
241 );
242 expect(applyClaudePromptEffortPrefix("/security-review", "ultrathink")).toBe(
243 "/security-review",
244 );
245 expect(applyClaudePromptEffortPrefix("/plugin:skill run", "ultrathink")).toBe(
246 "/plugin:skill run",
247 );
248 expect(applyClaudePromptEffortPrefix("/deploy.prod to staging", "ultrathink")).toBe(
249 "/deploy.prod to staging",
250 );
251 });
252​
253 it("still adds the ultrathink prefix to ordinary prompts", () => {
254 expect(applyClaudePromptEffortPrefix("Investigate this failure", "ultrathink")).toBe(
255 "Ultrathink:\nInvestigate this failure",
256 );
257 expect(applyClaudePromptEffortPrefix("/home/theo/app.ts crashed on load", "ultrathink")).toBe(
258 "Ultrathink:\n/home/theo/app.ts crashed on load",
259 );
260 });
261});
262​
263describe("readCustomModelEntries", () => {
264 const capabilities: ModelCapabilities = {
265 optionDescriptors: [
266 {
267 id: "effort",
268 label: "Reasoning",
269 type: "select",
270 options: [{ id: "high", label: "High", isDefault: true }],
271 currentValue: "high",
272 },
273 ],
274 };
275​
276 it("resolves bare slugs and entries, trimming and deduplicating on slug", () => {
277 expect(
278 readCustomModelEntries([
279 " bare ",
280 { slug: "named", name: " Named ", capabilities },
281 "bare",
282 { slug: "named", name: "Second" },
283 "",
284 { name: "no slug" },
285 42,
286 ]),
287 ).toEqual([
288 { slug: "bare", name: "bare", capabilities: null },
289 { slug: "named", name: "Named", capabilities },
290 ]);
291 });
292​
293 it("drops unparseable capabilities but keeps the entry", () => {
294 expect(
295 readCustomModelEntries([{ slug: "x", capabilities: { optionDescriptors: "nope" } }]),
296 ).toEqual([{ slug: "x", name: "x", capabilities: null }]);
297 expect(readCustomModelEntries("not a list")).toEqual([]);
298 });
299​
300 it("writes the compact stored shape back", () => {
301 expect(toCustomModelSetting({ slug: "x", name: "x", capabilities: null })).toBe("x");
302 expect(
303 toCustomModelSetting({ slug: "x", name: "x", capabilities: { optionDescriptors: [] } }),
304 ).toBe("x");
305 expect(toCustomModelSetting({ slug: "x", name: "X", capabilities })).toEqual({
306 slug: "x",
307 name: "X",
308 capabilities,
309 });
310 });
311});