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