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

composerProviderState.test.tsx

674 lines21.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 { getProviderOptionDescriptors } from "@t3tools/shared/model";
10import { getProviderModelCapabilities } from "../../providerModels";
11import {
12 getComposerPromptInjectionState,
13 getComposerProviderState,
14 getComposerReasoningStep,
15 renderProviderTraitsMenuContent,
16 renderProviderTraitsPicker,
17 withImplicitFastModeDefault,
18} from "./composerProviderState";
19​
20const PROVIDER: ProviderDriverKind = ProviderDriverKind.make("codex");
21const MODEL = "test-model";
22​
23function selectDescriptor(
24 id: string,
25 options: ReadonlyArray<{ id: string; label: string; isDefault?: boolean }>,
26 promptInjectedValues?: ReadonlyArray<string>,
27): Extract<ProviderOptionDescriptor, { type: "select" }> {
28 const defaultId = options.find((option) => option.isDefault)?.id;
29 return {
30 id,
31 label: id,
32 type: "select",
33 options: [...options],
34 ...(defaultId ? { currentValue: defaultId } : {}),
35 ...(promptInjectedValues && promptInjectedValues.length > 0
36 ? { promptInjectedValues: [...promptInjectedValues] }
37 : {}),
38 };
40​
41function booleanDescriptor(
42 id: string,
43 currentValue?: boolean,
44): Extract<ProviderOptionDescriptor, { type: "boolean" }> {
45 return {
46 id,
47 label: id,
48 type: "boolean",
49 ...(typeof currentValue === "boolean" ? { currentValue } : {}),
50 };
52​
53function modelWith(
54 descriptors: ReadonlyArray<ProviderOptionDescriptor>,
55): ReadonlyArray<ServerProviderModel> {
56 return [
57 { slug: MODEL, name: MODEL, isCustom: false, capabilities: { optionDescriptors: descriptors } },
58 ];
60​
61function selections(
62 ...entries: Array<[string, string | boolean]>
63): ReadonlyArray<ProviderOptionSelection> {
64 return entries.map(([id, value]) => ({ id, value }));
66​
67const ULTRATHINK_FRAME_CLASSES = {
68 composerFrameClassName: "ultrathink-frame",
69 composerSurfaceClassName: "shadow-[0_0_0_1px_rgba(255,255,255,0.07)_inset]",
70 modelPickerIconClassName: "ultrathink-chroma",
71} as const;
72​
73describe("getComposerProviderState", () => {
74 it("derives a stable prompt injection state for ordinary prompt edits", () => {
75 expect(getComposerPromptInjectionState("Investigate this failure")).toBe("none");
76 expect(getComposerPromptInjectionState("Ultrathink:\nInvestigate this failure")).toBe(
77 "ultrathink",
78 );
79 });
80​
81 it("uses descriptor defaults for display without dispatching them as overrides", () => {
82 const state = getComposerProviderState({
83 provider: PROVIDER,
84 model: MODEL,
85 models: modelWith([
86 selectDescriptor("effort", [
87 { id: "low", label: "Low" },
88 { id: "high", label: "High", isDefault: true },
89 ]),
90 ]),
91 modelOptions: undefined,
92 planModeEnabled: true,
93 });
94​
95 expect(state).toEqual({
96 provider: PROVIDER,
97 promptEffort: "high",
98 modelOptionsForDispatch: undefined,
99 });
100 });
101​
102 it("lets selections override defaults and propagates them through dispatch", () => {
103 const state = getComposerProviderState({
104 provider: PROVIDER,
105 model: MODEL,
106 models: modelWith([
107 selectDescriptor("effort", [
108 { id: "low", label: "Low" },
109 { id: "high", label: "High", isDefault: true },
110 ]),
111 booleanDescriptor("fastMode"),
112 ]),
113 modelOptions: selections(["effort", "low"], ["fastMode", true]),
114 planModeEnabled: true,
115 });
116​
117 expect(state).toEqual({
118 provider: PROVIDER,
119 promptEffort: "low",
120 modelOptionsForDispatch: selections(["effort", "low"], ["fastMode", true]),
121 });
122 });
123​
124 it("preserves selections that match defaults so deepMerge can overwrite prior state", () => {
125 const state = getComposerProviderState({
126 provider: PROVIDER,
127 model: MODEL,
128 models: modelWith([
129 selectDescriptor("effort", [{ id: "high", label: "High", isDefault: true }]),
130 booleanDescriptor("fastMode"),
131 ]),
132 modelOptions: selections(["effort", "high"], ["fastMode", false]),
133 planModeEnabled: true,
134 });
135​
136 expect(state.modelOptionsForDispatch).toEqual(
137 selections(["effort", "high"], ["fastMode", false]),
138 );
139 });
140​
141 it("drops selections for descriptors the model does not declare", () => {
142 const state = getComposerProviderState({
143 provider: PROVIDER,
144 model: MODEL,
145 models: modelWith([booleanDescriptor("thinking")]),
146 modelOptions: selections(["effort", "max"], ["thinking", false]),
147 planModeEnabled: true,
148 });
149​
150 expect(state).toEqual({
151 provider: PROVIDER,
152 promptEffort: null,
153 modelOptionsForDispatch: selections(["thinking", false]),
154 });
155 });
156​
157 it("derives promptEffort from the first select descriptor and preserves all others for dispatch", () => {
158 const state = getComposerProviderState({
159 provider: PROVIDER,
160 model: MODEL,
161 models: modelWith([
162 selectDescriptor("effort", [{ id: "high", label: "High", isDefault: true }]),
163 selectDescriptor("contextWindow", [
164 { id: "200k", label: "200k", isDefault: true },
165 { id: "1m", label: "1M" },
166 ]),
167 selectDescriptor("agent", [
168 { id: "build", label: "Build", isDefault: true },
169 { id: "plan", label: "Plan" },
170 ]),
171 ]),
172 modelOptions: selections(["agent", "plan"]),
173 planModeEnabled: true,
174 });
175​
176 expect(state.promptEffort).toBe("high");
177 expect(state.modelOptionsForDispatch).toEqual(selections(["agent", "plan"]));
178 });
179​
180 it("drops the plan agent from dispatch when legacy plan mode is disabled", () => {
181 const state = getComposerProviderState({
182 provider: PROVIDER,
183 model: MODEL,
184 models: modelWith([
185 selectDescriptor("agent", [
186 { id: "build", label: "Build", isDefault: true },
187 { id: "plan", label: "Plan" },
188 ]),
189 ]),
190 modelOptions: selections(["agent", "plan"]),
191 planModeEnabled: false,
192 });
193​
194 expect(state.modelOptionsForDispatch).toEqual(selections(["agent", "build"]));
195 });
196​
197 it("drops the agent descriptor entirely when plan is the only option and plan mode is disabled", () => {
198 const state = getComposerProviderState({
199 provider: PROVIDER,
200 model: MODEL,
201 models: modelWith([
202 selectDescriptor("agent", [{ id: "plan", label: "Plan", isDefault: true }]),
203 ]),
204 modelOptions: selections(["agent", "plan"]),
205 planModeEnabled: false,
206 });
207​
208 expect(state).toEqual({
209 provider: PROVIDER,
210 promptEffort: null,
211 modelOptionsForDispatch: undefined,
212 });
213 });
214​
215 it("falls back to a surviving agent when plan was the descriptor default and plan mode is disabled", () => {
216 const state = getComposerProviderState({
217 provider: PROVIDER,
218 model: MODEL,
219 models: modelWith([
220 selectDescriptor("agent", [
221 { id: "plan", label: "Plan", isDefault: true },
222 { id: "research", label: "Research" },
223 ]),
224 ]),
225 modelOptions: undefined,
226 planModeEnabled: false,
227 });
228​
229 expect(state.modelOptionsForDispatch).toBeUndefined();
230 });
231​
232 it("returns undefined dispatch options when the model declares no descriptors", () => {
233 const state = getComposerProviderState({
234 provider: PROVIDER,
235 model: MODEL,
236 models: modelWith([]),
237 modelOptions: selections(["anything", "value"]),
238 planModeEnabled: true,
239 });
240​
241 expect(state).toEqual({
242 provider: PROVIDER,
243 promptEffort: null,
244 modelOptionsForDispatch: undefined,
245 });
246 });
247​
248 it("preserves explicit options when the selected model is absent from the catalog", () => {
249 const state = getComposerProviderState({
250 provider: ProviderDriverKind.make("opencode"),
251 model: "opencode/kimi-k3",
252 models: [
253 {
254 slug: "opencode/big-pickle",
255 name: "Big Pickle",
256 isCustom: false,
257 capabilities: {},
258 },
259 ],
260 modelOptions: selections(["variant", "max"], ["agent", "build"]),
261 planModeEnabled: false,
262 });
263​
264 expect(state.modelOptionsForDispatch).toEqual(
265 selections(["variant", "max"], ["agent", "build"]),
266 );
267 });
268​
269 it.each(["codex", "claudeAgent", "cursor", "grok"])(
270 "does not preserve unknown options for a missing %s model",
271 (provider) => {
272 const state = getComposerProviderState({
273 provider: ProviderDriverKind.make(provider),
274 model: "missing-model",
275 models: modelWith([]),
276 modelOptions: selections(["unknown", "value"]),
277 planModeEnabled: true,
278 });
279​
280 expect(state.modelOptionsForDispatch).toBeUndefined();
281 },
282 );
283​
284 it("preserves explicit options while the catalog is empty", () => {
285 const state = getComposerProviderState({
286 provider: ProviderDriverKind.make("opencode"),
287 model: "opencode/kimi-k3",
288 models: [],
289 modelOptions: selections(["variant", "max"], ["agent", "build"]),
290 planModeEnabled: false,
291 });
292​
293 expect(state.modelOptionsForDispatch).toEqual(
294 selections(["variant", "max"], ["agent", "build"]),
295 );
296 });
297​
298 it("validates options for a known model selected through a legacy alias", () => {
299 const state = getComposerProviderState({
300 provider: ProviderDriverKind.make("claudeAgent"),
301 model: "legacy-test-model",
302 models: [
303 {
304 slug: "test-model",
305 name: "Test Model",
306 aliases: ["legacy-test-model"],
307 isCustom: false,
308 capabilities: {
309 optionDescriptors: [
310 selectDescriptor("effort", [
311 { id: "low", label: "Low" },
312 { id: "high", label: "High", isDefault: true },
313 ]),
314 ],
315 },
316 },
317 ],
318 modelOptions: selections(["effort", "low"], ["unknown", "value"]),
319 planModeEnabled: false,
320 });
321​
322 expect(state.modelOptionsForDispatch).toEqual(selections(["effort", "low"]));
323 });
324​
325 it("still drops the plan agent when an absent model has a saved plan selection", () => {
326 const state = getComposerProviderState({
327 provider: ProviderDriverKind.make("opencode"),
328 model: "opencode/kimi-k3",
329 models: [],
330 modelOptions: selections(["variant", "max"], ["agent", "plan"]),
331 planModeEnabled: false,
332 });
333​
334 expect(state.modelOptionsForDispatch).toEqual(selections(["variant", "max"]));
335 });
336​
337 it("adds ultrathink class names when the prompt triggers a promptInjectedValues descriptor", () => {
338 const state = getComposerProviderState({
339 provider: PROVIDER,
340 model: MODEL,
341 models: modelWith([
342 selectDescriptor(
343 "effort",
344 [
345 { id: "medium", label: "Medium" },
346 { id: "high", label: "High", isDefault: true },
347 { id: "ultrathink", label: "Ultrathink" },
348 ],
349 ["ultrathink"],
350 ),
351 ]),
352 promptInjectionState: getComposerPromptInjectionState(
353 "Ultrathink:\nInvestigate this failure",
354 ),
355 modelOptions: selections(["effort", "medium"]),
356 planModeEnabled: true,
357 });
358​
359 expect(state).toEqual({
360 provider: PROVIDER,
361 promptEffort: "medium",
362 modelOptionsForDispatch: selections(["effort", "medium"]),
363 ...ULTRATHINK_FRAME_CLASSES,
364 });
365 });
366​
367 it("does not add ultrathink class names when the descriptor has no promptInjectedValues", () => {
368 const state = getComposerProviderState({
369 provider: PROVIDER,
370 model: MODEL,
371 models: modelWith([
372 selectDescriptor("effort", [{ id: "high", label: "High", isDefault: true }]),
373 ]),
374 promptInjectionState: getComposerPromptInjectionState(
375 "Ultrathink:\nInvestigate this failure",
376 ),
377 modelOptions: undefined,
378 planModeEnabled: true,
379 });
380​
381 expect(state).not.toHaveProperty("composerFrameClassName");
382 expect(state).not.toHaveProperty("composerSurfaceClassName");
383 expect(state).not.toHaveProperty("modelPickerIconClassName");
384 });
385​
386 it("defaults fastMode to false when the provider reports true but the user has not selected it", () => {
387 const state = getComposerProviderState({
388 provider: ProviderDriverKind.make("cursor"),
389 model: MODEL,
390 models: modelWith([booleanDescriptor("fastMode", true)]),
391 modelOptions: undefined,
392 planModeEnabled: true,
393 });
394​
395 expect(state.modelOptionsForDispatch).toEqual(selections(["fastMode", false]));
396 });
397​
398 it("keeps explicit fastMode true when the user selected Fast", () => {
399 const state = getComposerProviderState({
400 provider: ProviderDriverKind.make("cursor"),
401 model: MODEL,
402 models: modelWith([booleanDescriptor("fastMode", true)]),
403 modelOptions: selections(["fastMode", true]),
404 planModeEnabled: true,
405 });
406​
407 expect(state.modelOptionsForDispatch).toEqual(selections(["fastMode", true]));
408 });
409​
410 it("keeps explicit fastMode false when the user selected Normal", () => {
411 const state = getComposerProviderState({
412 provider: ProviderDriverKind.make("cursor"),
413 model: MODEL,
414 models: modelWith([booleanDescriptor("fastMode", true)]),
415 modelOptions: selections(["fastMode", false]),
416 planModeEnabled: true,
417 });
418​
419 expect(state.modelOptionsForDispatch).toEqual(selections(["fastMode", false]));
420 });
421});
422​
423describe("withImplicitFastModeDefault", () => {
424 it("injects fastMode false only when the model exposes fastMode and no selection exists", () => {
425 expect(
426 withImplicitFastModeDefault(
427 {
428 optionDescriptors: [booleanDescriptor("fastMode", true)],
429 },
430 undefined,
431 ),
432 ).toEqual(selections(["fastMode", false]));
433​
434 expect(
435 withImplicitFastModeDefault(
436 {
437 optionDescriptors: [booleanDescriptor("fastMode", true)],
438 },
439 selections(["fastMode", true]),
440 ),
441 ).toEqual(selections(["fastMode", true]));
442 });
443​
444 it("does not add fastMode when the model does not expose it", () => {
445 expect(
446 withImplicitFastModeDefault(
447 {
448 optionDescriptors: [booleanDescriptor("thinking", true)],
449 },
450 undefined,
451 ),
452 ).toBeUndefined();
453 });
454});
455​
456describe("trait controls fastMode display", () => {
457 it("resolves traits fastMode to Normal when the provider defaults to true without a user selection", () => {
458 const models = modelWith([booleanDescriptor("fastMode", true)]);
459 const provider = ProviderDriverKind.make("cursor");
460 const caps = getProviderModelCapabilities(models, MODEL, provider);
461 const resolved = withImplicitFastModeDefault(caps, undefined);
462 const descriptors = getProviderOptionDescriptors({ caps, selections: resolved });
463 const fastMode = descriptors.find((descriptor) => descriptor.id === "fastMode");
464​
465 expect(fastMode?.type).toBe("boolean");
466 if (fastMode?.type === "boolean") {
467 expect(fastMode.currentValue).toBe(false);
468 }
469 });
470});
471​
472describe("provider traits render guards", () => {
473 it("returns null when no thread target is provided", () => {
474 const models = modelWith([
475 selectDescriptor("effort", [{ id: "high", label: "High", isDefault: true }]),
476 ]);
477 const args = {
478 provider: PROVIDER,
479 model: MODEL,
480 models,
481 modelOptions: undefined,
482 prompt: "",
483 onPromptChange: () => {},
484 planModeEnabled: true,
485 };
486​
487 expect(renderProviderTraitsPicker(args)).toBeNull();
488 expect(renderProviderTraitsMenuContent(args)).toBeNull();
489 });
490});
491​
492describe("getComposerReasoningStep", () => {
493 const LEVELS = [
494 { id: "low", label: "Low" },
495 { id: "medium", label: "Medium", isDefault: true },
496 { id: "high", label: "High" },
497 ];
498 const INSTANCE = ProviderInstanceId.make("codex");
499 const CLAUDE = {
500 provider: ProviderDriverKind.make("claudeAgent"),
501 instanceId: ProviderInstanceId.make("claudeAgent"),
502 models: modelWith([
503 selectDescriptor(
504 "effort",
505 [
506 { id: "medium", label: "Medium" },
507 { id: "high", label: "High", isDefault: true },
508 { id: "ultrathink", label: "Ultrathink" },
509 ],
510 ["ultrathink"],
511 ),
512 ]),
513 };
514​
515 function step(input: Partial<Parameters<typeof getComposerReasoningStep>[0]>) {
516 return getComposerReasoningStep({
517 provider: PROVIDER,
518 instanceId: INSTANCE,
519 model: MODEL,
520 models: modelWith([selectDescriptor("reasoningEffort", LEVELS)]),
521 modelOptions: undefined,
522 prompt: "Fix the bug",
523 cursor: 0,
524 planModeEnabled: true,
525 direction: 1,
526 ...input,
527 });
528 }
529​
530 it("walks the catalog order, wrapping at both ends and keeping the other traits", () => {
531 const models = modelWith([
532 selectDescriptor("contextWindow", [
533 { id: "200k", label: "200k", isDefault: true },
534 { id: "1m", label: "1M" },
535 ]),
536 selectDescriptor("reasoningEffort", LEVELS),
537 ]);
538 for (const [from, direction, to] of [
539 ["low", -1, "high"],
540 ["low", 1, "medium"],
541 ["high", 1, "low"],
542 ["high", -1, "medium"],
543 ] as const) {
544 expect(
545 step({
546 models,
547 modelOptions: selections(["contextWindow", "1m"], ["reasoningEffort", from]),
548 direction,
549 }),
550 `${from} by ${direction}`,
551 ).toStrictEqual({
552 prompt: "Fix the bug",
553 cursor: 0,
554 modelOptions: selections(["contextWindow", "1m"], ["reasoningEffort", to]),
555 });
556 }
557 });
558​
559 it("adds and strips the Ultrathink prefix while the caret stays on the same character", () => {
560 for (const { prompt, modelOptions, direction, expected } of [
561 {
562 prompt: " Fix the bug \n",
563 modelOptions: selections(["effort", "high"]),
564 direction: 1,
565 expected: { prompt: "Ultrathink:\nFix the bug", cursor: 20 },
566 },
567 {
568 // The prefix, not the draft's Medium, is the current level.
569 prompt: "Ultrathink:\n Fix the bug",
570 modelOptions: selections(["effort", "medium"]),
571 direction: -1,
572 expected: {
573 prompt: "Fix the bug",
574 cursor: 8,
575 modelOptions: selections(["effort", "high"]),
576 },
577 },
578 ] as const) {
579 const result = step({
580 ...CLAUDE,
581 prompt,
582 cursor: prompt.indexOf("bug"),
583 modelOptions,
584 direction,
585 });
586 expect(result).toStrictEqual(expected);
587 expect(result?.prompt.slice(result.cursor)).toMatch(/^bug/);
588 }
589 // A caret in leading whitespace must not land inside the new prefix.
590 expect(
591 step({ ...CLAUDE, prompt: " Fix", modelOptions: selections(["effort", "high"]) })?.cursor,
592 ).toBe("Ultrathink:\n".length);
593 });
594​
595 it("skips Ultrathink for slash commands and does nothing when the prompt locks it", () => {
596 expect(
597 step({ ...CLAUDE, prompt: "/review", modelOptions: selections(["effort", "high"]) }),
598 ).toStrictEqual({
599 prompt: "/review",
600 cursor: 0,
601 modelOptions: selections(["effort", "medium"]),
602 });
603 for (const [prompt, direction] of [
604 ["Please ultrathink harder", 1],
605 ["Ultrathink:\nPlease ultrathink harder", -1],
606 ] as const) {
607 expect(
608 step({ ...CLAUDE, prompt, modelOptions: selections(["effort", "high"]), direction }),
609 prompt,
610 ).toBeNull();
611 }
612 });
613​
614 it("keeps a provider-default Fast mode off unless the user chose it", () => {
615 const cursor = {
616 provider: ProviderDriverKind.make("cursor"),
617 instanceId: ProviderInstanceId.make("cursor"),
618 models: modelWith([
619 selectDescriptor("reasoning", LEVELS),
620 booleanDescriptor("fastMode", true),
621 ]),
622 };
623​
624 expect(step({ ...cursor, modelOptions: undefined })?.modelOptions).toStrictEqual(
625 selections(["reasoning", "high"], ["fastMode", false]),
626 );
627 expect(
628 step({ ...cursor, modelOptions: selections(["fastMode", true]) })?.modelOptions,
629 ).toStrictEqual(selections(["reasoning", "high"], ["fastMode", true]));
630 });
631​
632 it("steps from the provider-reported level for the same instance and model", () => {
633 const reported = {
634 instanceId: INSTANCE,
635 model: MODEL,
636 options: selections(["reasoningEffort", "high"]),
637 };
638 for (const { label, modelOptions, reportedModelSelection, to } of [
639 { label: "reported", modelOptions: undefined, reportedModelSelection: reported, to: "low" },
640 {
641 label: "other instance",
642 modelOptions: undefined,
643 reportedModelSelection: { ...reported, instanceId: ProviderInstanceId.make("codex-work") },
644 to: "high",
645 },
646 {
647 label: "other model",
648 modelOptions: undefined,
649 reportedModelSelection: { ...reported, model: "other-model" },
650 to: "high",
651 },
652 {
653 label: "explicit draft",
654 modelOptions: selections(["reasoningEffort", "low"]),
655 reportedModelSelection: reported,
656 to: "medium",
657 },
658 {
659 // OpenCode reports "default" when no variant was chosen.
660 label: "reported value outside the catalog",
661 modelOptions: undefined,
662 reportedModelSelection: {
663 ...reported,
664 options: selections(["reasoningEffort", "default"]),
665 },
666 to: "high",
667 },
668 ]) {
669 expect(step({ modelOptions, reportedModelSelection })?.modelOptions, label).toStrictEqual(
670 selections(["reasoningEffort", to]),
671 );
672 }
673 });
674});