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