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

composerProviderState.test.tsx

423 lines13.1 KB

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";
14​
15const PROVIDER: ProviderDriverKind = ProviderDriverKind.make("codex");
16const MODEL = "test-model";
17​
18function selectDescriptor(
19 id: string,
20 options: ReadonlyArray<{ id: string; label: string; isDefault?: boolean }>,
21 promptInjectedValues?: ReadonlyArray<string>,
22): Extract<ProviderOptionDescriptor, { type: "select" }> {
23 const defaultId = options.find((option) => option.isDefault)?.id;
24 return {
25 id,
26 label: id,
27 type: "select",
28 options: [...options],
29 ...(defaultId ? { currentValue: defaultId } : {}),
30 ...(promptInjectedValues && promptInjectedValues.length > 0
31 ? { promptInjectedValues: [...promptInjectedValues] }
32 : {}),
33 };
35​
36function booleanDescriptor(
37 id: string,
38 currentValue?: boolean,
39): Extract<ProviderOptionDescriptor, { type: "boolean" }> {
40 return {
41 id,
42 label: id,
43 type: "boolean",
44 ...(typeof currentValue === "boolean" ? { currentValue } : {}),
45 };
47​
48function modelWith(
49 descriptors: ReadonlyArray<ProviderOptionDescriptor>,
50): ReadonlyArray<ServerProviderModel> {
51 return [
52 { slug: MODEL, name: MODEL, isCustom: false, capabilities: { optionDescriptors: descriptors } },
53 ];
55​
56function selections(
57 ...entries: Array<[string, string | boolean]>
58): ReadonlyArray<ProviderOptionSelection> {
59 return entries.map(([id, value]) => ({ id, value }));
61​
62describe("getComposerProviderState", () => {
63 it("uses descriptor defaults for display without dispatching them as overrides", () => {
64 const state = getComposerProviderState({
65 provider: PROVIDER,
66 model: MODEL,
67 models: modelWith([
68 selectDescriptor("effort", [
69 { id: "low", label: "Low" },
70 { id: "high", label: "High", isDefault: true },
71 ]),
72 ]),
73 modelOptions: undefined,
74 planModeEnabled: true,
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 planModeEnabled: true,
94 });
95​
96 expect(state.modelOptionsForDispatch).toEqual(
97 selections(["effort", "high"], ["fastMode", false]),
98 );
99 });
100​
101 it("drops selections for descriptors the model does not declare", () => {
102 const state = getComposerProviderState({
103 provider: PROVIDER,
104 model: MODEL,
105 models: modelWith([booleanDescriptor("thinking")]),
106 modelOptions: selections(["effort", "max"], ["thinking", false]),
107 planModeEnabled: true,
108 });
109​
110 expect(state).toEqual({
111 provider: PROVIDER,
112 promptEffort: null,
113 modelOptionsForDispatch: selections(["thinking", false]),
114 });
115 });
116​
117 it("derives promptEffort from the first select descriptor and preserves all others for dispatch", () => {
118 const state = getComposerProviderState({
119 provider: PROVIDER,
120 model: MODEL,
121 models: modelWith([
122 selectDescriptor("effort", [{ id: "high", label: "High", isDefault: true }]),
123 selectDescriptor("contextWindow", [
124 { id: "200k", label: "200k", isDefault: true },
125 { id: "1m", label: "1M" },
126 ]),
127 selectDescriptor("agent", [
128 { id: "build", label: "Build", isDefault: true },
129 { id: "plan", label: "Plan" },
130 ]),
131 ]),
132 modelOptions: selections(["agent", "plan"]),
133 planModeEnabled: true,
134 });
135​
136 expect(state.promptEffort).toBe("high");
137 expect(state.modelOptionsForDispatch).toEqual(selections(["agent", "plan"]));
138 });
139​
140 it("drops the plan agent from dispatch when legacy plan mode is disabled", () => {
141 const state = getComposerProviderState({
142 provider: PROVIDER,
143 model: MODEL,
144 models: modelWith([
145 selectDescriptor("agent", [
146 { id: "build", label: "Build", isDefault: true },
147 { id: "plan", label: "Plan" },
148 ]),
149 ]),
150 modelOptions: selections(["agent", "plan"]),
151 planModeEnabled: false,
152 });
153​
154 expect(state.modelOptionsForDispatch).toEqual(selections(["agent", "build"]));
155 });
156​
157 it("preserves explicit options when the selected model is absent from the catalog", () => {
158 const state = getComposerProviderState({
159 provider: ProviderDriverKind.make("opencode"),
160 model: "opencode/kimi-k3",
161 models: [
162 {
163 slug: "opencode/big-pickle",
164 name: "Big Pickle",
165 isCustom: false,
166 capabilities: {},
167 },
168 ],
169 modelOptions: selections(["variant", "max"], ["agent", "build"]),
170 planModeEnabled: false,
171 });
172​
173 expect(state.modelOptionsForDispatch).toEqual(
174 selections(["variant", "max"], ["agent", "build"]),
175 );
176 });
177​
178 it("validates options for a known model selected through a legacy alias", () => {
179 const state = getComposerProviderState({
180 provider: ProviderDriverKind.make("claudeAgent"),
181 model: "legacy-test-model",
182 models: [
183 {
184 slug: "test-model",
185 name: "Test Model",
186 aliases: ["legacy-test-model"],
187 isCustom: false,
188 capabilities: {
189 optionDescriptors: [
190 selectDescriptor("effort", [
191 { id: "low", label: "Low" },
192 { id: "high", label: "High", isDefault: true },
193 ]),
194 ],
195 },
196 },
197 ],
198 modelOptions: selections(["effort", "low"], ["unknown", "value"]),
199 planModeEnabled: false,
200 });
201​
202 expect(state.modelOptionsForDispatch).toEqual(selections(["effort", "low"]));
203 });
204​
205 it("defaults fastMode to false when the provider reports true but the user has not selected it", () => {
206 const state = getComposerProviderState({
207 provider: ProviderDriverKind.make("cursor"),
208 model: MODEL,
209 models: modelWith([booleanDescriptor("fastMode", true)]),
210 modelOptions: undefined,
211 planModeEnabled: true,
212 });
213​
214 expect(state.modelOptionsForDispatch).toEqual(selections(["fastMode", false]));
215 });
216});
217​
218describe("withImplicitFastModeDefault", () => {
219 // regression: #2981 a remembered Fast mode choice must survive the implicit default on new chats
220 it("injects fastMode false only when the model exposes fastMode and no selection exists", () => {
221 expect(
222 withImplicitFastModeDefault(
223 {
224 optionDescriptors: [booleanDescriptor("fastMode", true)],
225 },
226 undefined,
227 ),
228 ).toEqual(selections(["fastMode", false]));
229​
230 expect(
231 withImplicitFastModeDefault(
232 {
233 optionDescriptors: [booleanDescriptor("fastMode", true)],
234 },
235 selections(["fastMode", true]),
236 ),
237 ).toEqual(selections(["fastMode", true]));
238 });
239});
240​
241describe("getComposerReasoningStep", () => {
242 const LEVELS = [
243 { id: "low", label: "Low" },
244 { id: "medium", label: "Medium", isDefault: true },
245 { id: "high", label: "High" },
246 ];
247 const INSTANCE = ProviderInstanceId.make("codex");
248 const CLAUDE = {
249 provider: ProviderDriverKind.make("claudeAgent"),
250 instanceId: ProviderInstanceId.make("claudeAgent"),
251 models: modelWith([
252 selectDescriptor(
253 "effort",
254 [
255 { id: "medium", label: "Medium" },
256 { id: "high", label: "High", isDefault: true },
257 { id: "ultrathink", label: "Ultrathink" },
258 ],
259 ["ultrathink"],
260 ),
261 ]),
262 };
263​
264 function step(input: Partial<Parameters<typeof getComposerReasoningStep>[0]>) {
265 return getComposerReasoningStep({
266 provider: PROVIDER,
267 instanceId: INSTANCE,
268 model: MODEL,
269 models: modelWith([selectDescriptor("reasoningEffort", LEVELS)]),
270 modelOptions: undefined,
271 prompt: "Fix the bug",
272 cursor: 0,
273 planModeEnabled: true,
274 direction: 1,
275 ...input,
276 });
277 }
278​
279 it("walks the catalog order, wrapping at both ends and keeping the other traits", () => {
280 const models = modelWith([
281 selectDescriptor("contextWindow", [
282 { id: "200k", label: "200k", isDefault: true },
283 { id: "1m", label: "1M" },
284 ]),
285 selectDescriptor("reasoningEffort", LEVELS),
286 ]);
287 for (const [from, direction, to] of [
288 ["low", -1, "high"],
289 ["low", 1, "medium"],
290 ["high", 1, "low"],
291 ["high", -1, "medium"],
292 ] as const) {
293 expect(
294 step({
295 models,
296 modelOptions: selections(["contextWindow", "1m"], ["reasoningEffort", from]),
297 direction,
298 }),
299 `${from} by ${direction}`,
300 ).toStrictEqual({
301 prompt: "Fix the bug",
302 cursor: 0,
303 modelOptions: selections(["contextWindow", "1m"], ["reasoningEffort", to]),
304 });
305 }
306 });
307​
308 it("adds and strips the Ultrathink prefix while the caret stays on the same character", () => {
309 for (const { prompt, modelOptions, direction, expected } of [
310 {
311 prompt: " Fix the bug \n",
312 modelOptions: selections(["effort", "high"]),
313 direction: 1,
314 expected: { prompt: "Ultrathink:\nFix the bug", cursor: 20 },
315 },
316 {
317 // The prefix, not the draft's Medium, is the current level.
318 prompt: "Ultrathink:\n Fix the bug",
319 modelOptions: selections(["effort", "medium"]),
320 direction: -1,
321 expected: {
322 prompt: "Fix the bug",
323 cursor: 8,
324 modelOptions: selections(["effort", "high"]),
325 },
326 },
327 ] as const) {
328 const result = step({
329 ...CLAUDE,
330 prompt,
331 cursor: prompt.indexOf("bug"),
332 modelOptions,
333 direction,
334 });
335 expect(result).toStrictEqual(expected);
336 expect(result?.prompt.slice(result.cursor)).toMatch(/^bug/);
337 }
338 // A caret in leading whitespace must not land inside the new prefix.
339 expect(
340 step({ ...CLAUDE, prompt: " Fix", modelOptions: selections(["effort", "high"]) })?.cursor,
341 ).toBe("Ultrathink:\n".length);
342 });
343​
344 it("skips Ultrathink for slash commands and does nothing when the prompt locks it", () => {
345 expect(
346 step({ ...CLAUDE, prompt: "/review", modelOptions: selections(["effort", "high"]) }),
347 ).toStrictEqual({
348 prompt: "/review",
349 cursor: 0,
350 modelOptions: selections(["effort", "medium"]),
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});