Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import {
3 ProviderDriverKind,
4 ProviderInstanceId,
5 type ProviderOptionDescriptor,
6 type ProviderOptionSelection,
7 type ServerProviderModel,
8} from "@t3tools/contracts";
9import {
10 getComposerProviderState,
11 getComposerReasoningStep,
12 withImplicitFastModeDefault,
13} from "./composerProviderState";
14import { readPromptUltrathink } from "./TraitsPicker";
15
16const PROVIDER: ProviderDriverKind = ProviderDriverKind.make("codex");
17const MODEL = "test-model";
18
19function selectDescriptor(
20 id: string,
21 options: ReadonlyArray<{ id: string; label: string; isDefault?: boolean }>,
22 promptInjectedValues?: ReadonlyArray<string>,
23): Extract<ProviderOptionDescriptor, { type: "select" }> {
24 const defaultId = options.find((option) => option.isDefault)?.id;
25 return {
26 id,
27 label: id,
28 type: "select",
29 options: [...options],
30 ...(defaultId ? { currentValue: defaultId } : {}),
31 ...(promptInjectedValues && promptInjectedValues.length > 0
32 ? { promptInjectedValues: [...promptInjectedValues] }
33 : {}),
34 };
35}
36
37function booleanDescriptor(
38 id: string,
39 currentValue?: boolean,
40): Extract<ProviderOptionDescriptor, { type: "boolean" }> {
41 return {
42 id,
43 label: id,
44 type: "boolean",
45 ...(typeof currentValue === "boolean" ? { currentValue } : {}),
46 };
47}
48
49function modelWith(
50 descriptors: ReadonlyArray<ProviderOptionDescriptor>,
51): ReadonlyArray<ServerProviderModel> {
52 return [
53 { slug: MODEL, name: MODEL, isCustom: false, capabilities: { optionDescriptors: descriptors } },
54 ];
55}
56
57function selections(
58 ...entries: Array<[string, string | boolean]>
59): ReadonlyArray<ProviderOptionSelection> {
60 return entries.map(([id, value]) => ({ id, value }));
61}
62
63describe("getComposerProviderState", () => {
64 it("uses descriptor defaults for display without dispatching them as overrides", () => {
65 const state = getComposerProviderState({
66 provider: PROVIDER,
67 model: MODEL,
68 models: modelWith([
69 selectDescriptor("effort", [
70 { id: "low", label: "Low" },
71 { id: "high", label: "High", isDefault: true },
72 ]),
73 ]),
74 modelOptions: undefined,
75 });
76
77 expect(state).toEqual({
78 provider: PROVIDER,
79 promptEffort: "high",
80 modelOptionsForDispatch: undefined,
81 });
82 });
83
84 it("preserves selections that match defaults so deepMerge can overwrite prior state", () => {
85 const state = getComposerProviderState({
86 provider: PROVIDER,
87 model: MODEL,
88 models: modelWith([
89 selectDescriptor("effort", [{ id: "high", label: "High", isDefault: true }]),
90 booleanDescriptor("fastMode"),
91 ]),
92 modelOptions: selections(["effort", "high"], ["fastMode", false]),
93 });
94
95 expect(state.modelOptionsForDispatch).toEqual(
96 selections(["effort", "high"], ["fastMode", false]),
97 );
98 });
99
100 it("drops selections for descriptors the model does not declare", () => {
101 const state = getComposerProviderState({
102 provider: PROVIDER,
103 model: MODEL,
104 models: modelWith([booleanDescriptor("thinking")]),
105 modelOptions: selections(["effort", "max"], ["thinking", false]),
106 });
107
108 expect(state).toEqual({
109 provider: PROVIDER,
110 promptEffort: null,
111 modelOptionsForDispatch: selections(["thinking", false]),
112 });
113 });
114
115 it("derives promptEffort from the first select descriptor and preserves all others for dispatch", () => {
116 const state = getComposerProviderState({
117 provider: PROVIDER,
118 model: MODEL,
119 models: modelWith([
120 selectDescriptor("effort", [{ id: "high", label: "High", isDefault: true }]),
121 selectDescriptor("contextWindow", [
122 { id: "200k", label: "200k", isDefault: true },
123 { id: "1m", label: "1M" },
124 ]),
125 selectDescriptor("agent", [
126 { id: "build", label: "Build", isDefault: true },
127 { id: "plan", label: "Plan" },
128 ]),
129 ]),
130 modelOptions: selections(["agent", "plan"]),
131 includePlanAgent: true,
132 });
133
134 expect(state.promptEffort).toBe("high");
135 expect(state.modelOptionsForDispatch).toEqual(selections(["agent", "plan"]));
136 });
137
138 it("drops the plan agent from dispatch unless it is included", () => {
139 const state = getComposerProviderState({
140 provider: PROVIDER,
141 model: MODEL,
142 models: modelWith([
143 selectDescriptor("agent", [
144 { id: "build", label: "Build", isDefault: true },
145 { id: "plan", label: "Plan" },
146 ]),
147 ]),
148 modelOptions: selections(["agent", "plan"]),
149 });
150
151 expect(state.modelOptionsForDispatch).toEqual(selections(["agent", "build"]));
152 });
153
154 it("preserves explicit options when the selected model is absent from the catalog", () => {
155 const state = getComposerProviderState({
156 provider: ProviderDriverKind.make("opencode"),
157 model: "opencode/kimi-k3",
158 models: [
159 {
160 slug: "opencode/big-pickle",
161 name: "Big Pickle",
162 isCustom: false,
163 capabilities: {},
164 },
165 ],
166 modelOptions: selections(["variant", "max"], ["agent", "build"]),
167 });
168
169 expect(state.modelOptionsForDispatch).toEqual(
170 selections(["variant", "max"], ["agent", "build"]),
171 );
172 });
173
174 it("validates options for a known model selected through a legacy alias", () => {
175 const state = getComposerProviderState({
176 provider: ProviderDriverKind.make("claudeAgent"),
177 model: "legacy-test-model",
178 models: [
179 {
180 slug: "test-model",
181 name: "Test Model",
182 aliases: ["legacy-test-model"],
183 isCustom: false,
184 capabilities: {
185 optionDescriptors: [
186 selectDescriptor("effort", [
187 { id: "low", label: "Low" },
188 { id: "high", label: "High", isDefault: true },
189 ]),
190 ],
191 },
192 },
193 ],
194 modelOptions: selections(["effort", "low"], ["unknown", "value"]),
195 });
196
197 expect(state.modelOptionsForDispatch).toEqual(selections(["effort", "low"]));
198 });
199
200 it("defaults fastMode to false when the provider reports true but the user has not selected it", () => {
201 const state = getComposerProviderState({
202 provider: ProviderDriverKind.make("cursor"),
203 model: MODEL,
204 models: modelWith([booleanDescriptor("fastMode", true)]),
205 modelOptions: undefined,
206 });
207
208 expect(state.modelOptionsForDispatch).toEqual(selections(["fastMode", false]));
209 });
210});
211
212describe("withImplicitFastModeDefault", () => {
213 // regression: #2981 a remembered Fast mode choice must survive the implicit default on new chats
214 it("injects fastMode false only when the model exposes fastMode and no selection exists", () => {
215 expect(
216 withImplicitFastModeDefault(
217 {
218 optionDescriptors: [booleanDescriptor("fastMode", true)],
219 },
220 undefined,
221 ),
222 ).toEqual(selections(["fastMode", false]));
223
224 expect(
225 withImplicitFastModeDefault(
226 {
227 optionDescriptors: [booleanDescriptor("fastMode", true)],
228 },
229 selections(["fastMode", true]),
230 ),
231 ).toEqual(selections(["fastMode", true]));
232 });
233});
234
235describe("readPromptUltrathink", () => {
236 it("tells the Ultrathink prefix apart from the word in the body", () => {
237 expect(readPromptUltrathink("Fix the bug")).toBe("none");
238 expect(readPromptUltrathink("Ultrathink: Fix the bug")).toBe("prefix");
239 expect(readPromptUltrathink("ultrathink about the bug")).toBe("body");
240 expect(readPromptUltrathink("Ultrathink: ultrathink harder")).toBe("body");
241 });
242});
243
244describe("getComposerReasoningStep", () => {
245 const LEVELS = [
246 { id: "low", label: "Low" },
247 { id: "medium", label: "Medium", isDefault: true },
248 { id: "high", label: "High" },
249 ];
250 const INSTANCE = ProviderInstanceId.make("codex");
251 const CLAUDE = {
252 provider: ProviderDriverKind.make("claudeAgent"),
253 instanceId: ProviderInstanceId.make("claudeAgent"),
254 models: modelWith([
255 selectDescriptor(
256 "effort",
257 [
258 { id: "medium", label: "Medium" },
259 { id: "high", label: "High", isDefault: true },
260 { id: "ultrathink", label: "Ultrathink" },
261 ],
262 ["ultrathink"],
263 ),
264 ]),
265 };
266
267 function step(input: Partial<Parameters<typeof getComposerReasoningStep>[0]>) {
268 return getComposerReasoningStep({
269 provider: PROVIDER,
270 instanceId: INSTANCE,
271 model: MODEL,
272 models: modelWith([selectDescriptor("reasoningEffort", LEVELS)]),
273 modelOptions: undefined,
274 prompt: "Fix the bug",
275 cursor: 0,
276 direction: 1,
277 ...input,
278 });
279 }
280
281 it("walks the catalog order, wrapping at both ends and keeping the other traits", () => {
282 const models = modelWith([
283 selectDescriptor("contextWindow", [
284 { id: "200k", label: "200k", isDefault: true },
285 { id: "1m", label: "1M" },
286 ]),
287 selectDescriptor("reasoningEffort", LEVELS),
288 ]);
289 for (const [from, direction, to] of [
290 ["low", -1, "high"],
291 ["low", 1, "medium"],
292 ["high", 1, "low"],
293 ["high", -1, "medium"],
294 ] as const) {
295 expect(
296 step({
297 models,
298 modelOptions: selections(["contextWindow", "1m"], ["reasoningEffort", from]),
299 direction,
300 }),
301 `${from} by ${direction}`,
302 ).toStrictEqual({
303 prompt: "Fix the bug",
304 cursor: 0,
305 modelOptions: selections(["contextWindow", "1m"], ["reasoningEffort", to]),
306 });
307 }
308 });
309
310 it("steps Antigravity's thinking_level like other effort controls", () => {
311 expect(
312 step({
313 provider: ProviderDriverKind.make("antigravity"),
314 models: modelWith([selectDescriptor("thinking_level", LEVELS)]),
315 })?.modelOptions,
316 ).toStrictEqual(selections(["thinking_level", "high"]));
317 });
318
319 it("never steps into Ultrathink and strips a typed prefix with the caret on the same character", () => {
320 expect(step({ ...CLAUDE, modelOptions: selections(["effort", "high"]) })).toStrictEqual({
321 prompt: "Fix the bug",
322 cursor: 0,
323 modelOptions: selections(["effort", "medium"]),
324 });
325 const prompt = "Ultrathink:\n Fix the bug";
326 // A typed Ultrathink sits above the pickable levels, so up wraps and down lands on the top level.
327 for (const [direction, to] of [
328 [-1, "high"],
329 [1, "medium"],
330 ] as const) {
331 const result = step({
332 ...CLAUDE,
333 prompt,
334 cursor: prompt.indexOf("bug"),
335 modelOptions: selections(["effort", "medium"]),
336 direction,
337 });
338 expect(result, `by ${direction}`).toStrictEqual({
339 prompt: "Fix the bug",
340 cursor: 8,
341 modelOptions: selections(["effort", to]),
342 });
343 }
344 for (const [prompt, direction] of [
345 ["Please ultrathink harder", 1],
346 ["Ultrathink:\nPlease ultrathink harder", -1],
347 ] as const) {
348 expect(
349 step({ ...CLAUDE, prompt, modelOptions: selections(["effort", "high"]), direction }),
350 prompt,
351 ).toBeNull();
352 }
353 });
354
355 it("keeps a provider-default Fast mode off unless the user chose it", () => {
356 const cursor = {
357 provider: ProviderDriverKind.make("cursor"),
358 instanceId: ProviderInstanceId.make("cursor"),
359 models: modelWith([
360 selectDescriptor("reasoning_effort", LEVELS),
361 booleanDescriptor("fastMode", true),
362 ]),
363 };
364
365 expect(step({ ...cursor, modelOptions: undefined })?.modelOptions).toStrictEqual(
366 selections(["reasoning_effort", "high"], ["fastMode", false]),
367 );
368 expect(
369 step({ ...cursor, modelOptions: selections(["fastMode", true]) })?.modelOptions,
370 ).toStrictEqual(selections(["reasoning_effort", "high"], ["fastMode", true]));
371 });
372
373 it("steps from the provider-reported level for the same instance and model", () => {
374 const reported = {
375 instanceId: INSTANCE,
376 model: MODEL,
377 options: selections(["reasoningEffort", "high"]),
378 };
379 for (const { label, modelOptions, reportedModelSelection, to } of [
380 { label: "reported", modelOptions: undefined, reportedModelSelection: reported, to: "low" },
381 {
382 label: "other instance",
383 modelOptions: undefined,
384 reportedModelSelection: { ...reported, instanceId: ProviderInstanceId.make("codex-work") },
385 to: "high",
386 },
387 {
388 label: "other model",
389 modelOptions: undefined,
390 reportedModelSelection: { ...reported, model: "other-model" },
391 to: "high",
392 },
393 {
394 label: "explicit draft",
395 modelOptions: selections(["reasoningEffort", "low"]),
396 reportedModelSelection: reported,
397 to: "medium",
398 },
399 {
400 // OpenCode reports "default" when no variant was chosen.
401 label: "reported value outside the catalog",
402 modelOptions: undefined,
403 reportedModelSelection: {
404 ...reported,
405 options: selections(["reasoningEffort", "default"]),
406 },
407 to: "high",
408 },
409 ]) {
410 expect(step({ modelOptions, reportedModelSelection })?.modelOptions, label).toStrictEqual(
411 selections(["reasoningEffort", to]),
412 );
413 }
414 });
415});