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

model.test.ts

276 lines8.7 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 getProviderOptionCurrentLabel,
11 getProviderOptionDescriptors,
12 readCustomModelEntries,
13 normalizeCustomModelSlug,
14 normalizeModelSlug,
15 modelSelectionsEqual,
16} from "./model.ts";
17​
18const codexCaps: ModelCapabilities = createModelCapabilities({
19 optionDescriptors: [
20 {
21 id: "reasoningEffort",
22 label: "Reasoning",
23 type: "select",
24 options: [
25 { id: "xhigh", label: "Extra High" },
26 { id: "high", label: "High", isDefault: true },
27 ],
28 currentValue: "high",
29 },
30 {
31 id: "fastMode",
32 label: "Fast Mode",
33 type: "boolean",
34 },
35 ],
36});
37​
38describe("model slug normalization", () => {
39 it("preserves exact custom slugs instead of expanding provider aliases", () => {
40 const claude = ProviderDriverKind.make("claudeAgent");
41​
42 expect(normalizeModelSlug("opus", claude)).toBe("opus");
43 expect(normalizeCustomModelSlug(" opus ")).toBe("opus");
44 });
45});
46​
47const claudeCaps: ModelCapabilities = createModelCapabilities({
48 optionDescriptors: [
49 {
50 id: "effort",
51 label: "Reasoning",
52 type: "select",
53 options: [
54 { id: "medium", label: "Medium" },
55 { id: "high", label: "High", isDefault: true },
56 { id: "ultrathink", label: "Ultrathink" },
57 ],
58 currentValue: "high",
59 promptInjectedValues: ["ultrathink"],
60 },
61 {
62 id: "contextWindow",
63 label: "Context Window",
64 type: "select",
65 options: [
66 { id: "200k", label: "200k" },
67 { id: "1m", label: "1M", isDefault: true },
68 ],
69 currentValue: "1m",
70 },
71 ],
72});
73​
74describe("descriptor helpers", () => {
75 it("applies selection values to capability descriptors", () => {
76 expect(
77 getProviderOptionDescriptors({
78 caps: claudeCaps,
79 selections: [
80 { id: "effort", value: "medium" },
81 { id: "contextWindow", value: "200k" },
82 ],
83 }),
84 ).toEqual([
85 {
86 id: "effort",
87 label: "Reasoning",
88 type: "select",
89 options: [
90 { id: "medium", label: "Medium" },
91 { id: "high", label: "High", isDefault: true },
92 { id: "ultrathink", label: "Ultrathink" },
93 ],
94 currentValue: "medium",
95 promptInjectedValues: ["ultrathink"],
96 },
97 {
98 id: "contextWindow",
99 label: "Context Window",
100 type: "select",
101 options: [
102 { id: "200k", label: "200k" },
103 { id: "1m", label: "1M", isDefault: true },
104 ],
105 currentValue: "200k",
106 },
107 ]);
108 });
109​
110 it("builds dispatch options only from explicit selections", () => {
111 const descriptors = getProviderOptionDescriptors({
112 caps: codexCaps,
113 selections: [{ id: "fastMode", value: true }],
114 });
115​
116 expect(buildExplicitProviderOptionSelectionsFromDescriptors(descriptors, undefined)).toBe(
117 undefined,
118 );
119 expect(
120 buildExplicitProviderOptionSelectionsFromDescriptors(descriptors, [
121 { id: "fastMode", value: true },
122 ]),
123 ).toEqual([{ id: "fastMode", value: true }]);
124 });
125​
126 it("compares complete model selections independent of option ordering", () => {
127 const left = createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.4", [
128 { id: "reasoningEffort", value: "high" },
129 { id: "fastMode", value: true },
130 ]);
131 const reordered = createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.4", [
132 { id: "fastMode", value: true },
133 { id: "reasoningEffort", value: "high" },
134 ]);
135​
136 expect(modelSelectionsEqual(left, reordered)).toBe(true);
137 expect(
138 modelSelectionsEqual(left, {
139 ...reordered,
140 options: [
141 { id: "fastMode", value: true },
142 { id: "reasoningEffort", value: "medium" },
143 ],
144 }),
145 ).toBe(false);
146 expect(modelSelectionsEqual(left, { ...reordered, model: "gpt-5.5" })).toBe(false);
147 });
148});
149​
150describe("applyClaudePromptEffortPrefix", () => {
151 it("keeps slash commands intact when ultrathink is selected", () => {
152 expect(applyClaudePromptEffortPrefix("/compact", "ultrathink")).toBe("/compact");
153 expect(applyClaudePromptEffortPrefix(" /compact keep recent errors ", "ultrathink")).toBe(
154 "/compact keep recent errors",
155 );
156 expect(applyClaudePromptEffortPrefix(" /review src/model.ts ", "ultrathink")).toBe(
157 "/review src/model.ts",
158 );
159 expect(applyClaudePromptEffortPrefix("/security-review", "ultrathink")).toBe(
160 "/security-review",
161 );
162 expect(applyClaudePromptEffortPrefix("/plugin:skill run", "ultrathink")).toBe(
163 "/plugin:skill run",
164 );
165 expect(applyClaudePromptEffortPrefix("/deploy.prod to staging", "ultrathink")).toBe(
166 "/deploy.prod to staging",
167 );
168 });
169​
170 it("still adds the ultrathink prefix to ordinary prompts", () => {
171 expect(applyClaudePromptEffortPrefix("Investigate this failure", "ultrathink")).toBe(
172 "Ultrathink:\nInvestigate this failure",
173 );
174 expect(applyClaudePromptEffortPrefix("/home/theo/app.ts crashed on load", "ultrathink")).toBe(
175 "Ultrathink:\n/home/theo/app.ts crashed on load",
176 );
177 });
178});
179​
180describe("readCustomModelEntries", () => {
181 const capabilities: ModelCapabilities = {
182 optionDescriptors: [
183 {
184 id: "effort",
185 label: "Reasoning",
186 type: "select",
187 options: [{ id: "high", label: "High", isDefault: true }],
188 currentValue: "high",
189 },
190 ],
191 };
192​
193 it("resolves bare slugs and entries, trimming and deduplicating on slug", () => {
194 expect(
195 readCustomModelEntries([
196 " bare ",
197 { slug: "named", name: " Named ", capabilities },
198 "bare",
199 { slug: "named", name: "Second" },
200 "",
201 { name: "no slug" },
202 42,
203 ]),
204 ).toEqual([
205 { slug: "bare", name: "bare", capabilities: null },
206 { slug: "named", name: "Named", capabilities },
207 ]);
208 });
209​
210 it("drops unparseable capabilities but keeps the entry", () => {
211 expect(
212 readCustomModelEntries([{ slug: "x", capabilities: { optionDescriptors: "nope" } }]),
213 ).toEqual([{ slug: "x", name: "x", capabilities: null }]);
214 expect(readCustomModelEntries("not a list")).toEqual([]);
215 });
216});
217​
218describe("provider-reported option display", () => {
219 const selection = createModelSelection(ProviderInstanceId.make("opencode"), "ling");
220 const reported = { ...selection, options: [{ id: "variant", value: "default" }] };
221 const descriptor = {
222 id: "variant",
223 label: "Reasoning",
224 type: "select" as const,
225 options: [
226 { id: "none", label: "None" },
227 { id: "thinking", label: "Thinking" },
228 ],
229 };
230​
231 it("shows explicit reports without adding a choice or a dispatch option", () => {
232 expect(getProviderOptionCurrentLabel(descriptor, selection, reported)).toBe("Default");
233 expect(
234 getProviderOptionCurrentLabel(descriptor, selection, {
235 ...reported,
236 options: [{ id: "variant", value: "thinking" }],
237 }),
238 ).toBe("Thinking");
239 expect(
240 getProviderOptionCurrentLabel(
241 { ...descriptor, currentValue: "none" },
242 { ...selection, options: [{ id: "variant", value: "none" }] },
243 reported,
244 ),
245 ).toBe("None");
246 expect(descriptor.options.map((option) => option.id)).toEqual(["none", "thinking"]);
247 expect(buildProviderOptionSelectionsFromDescriptors([descriptor])).toBeUndefined();
248 expect(getProviderOptionCurrentLabel(descriptor, selection)).toBe("Unknown");
249 const effortDescriptor = { ...descriptor, id: "effort", currentValue: "default" };
250 expect(getProviderOptionCurrentLabel(effortDescriptor, selection)).toBeUndefined();
251 expect(
252 getProviderOptionCurrentLabel(effortDescriptor, selection, {
253 ...reported,
254 model: "other",
255 options: [{ id: "effort", value: "default" }],
256 }),
257 ).toBeUndefined();
258 expect(
259 getProviderOptionCurrentLabel(effortDescriptor, selection, {
260 ...reported,
261 options: [{ id: "effort", value: "default" }],
262 }),
263 ).toBe("Default");
264 expect(
265 getProviderOptionCurrentLabel({ ...descriptor, currentValue: "thinking" }, selection),
266 ).toBe("Unknown");
267 });
268​
269 it.each([
270 { ...selection, model: "other" },
271 { ...selection, instanceId: ProviderInstanceId.make("other") },
272 { ...selection, options: [{ id: "variant", value: "none" }] },
273 ])("ignores reports after changing the model, instance, or option: %j", (selected) => {
274 expect(getProviderOptionCurrentLabel(descriptor, selected, reported)).toBe("Unknown");
275 });
276});