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

composerProviderState.test.tsx

488 lines15.3 KB

1import { describe, expect, it } from "vite-plus/test";
2import {
3 ProviderDriverKind,
4 type ProviderOptionDescriptor,
5 type ProviderOptionSelection,
6 type ServerProviderModel,
7} from "@t3tools/contracts";
8import { getProviderOptionDescriptors } from "@t3tools/shared/model";
9import { getProviderModelCapabilities } from "../../providerModels";
10import {
11 getComposerPromptInjectionState,
12 getComposerProviderState,
13 renderProviderTraitsMenuContent,
14 renderProviderTraitsPicker,
15 withImplicitFastModeDefault,
16} from "./composerProviderState";
17​
18const PROVIDER: ProviderDriverKind = ProviderDriverKind.make("codex");
19const MODEL = "test-model";
20​
21function selectDescriptor(
22 id: string,
23 options: ReadonlyArray<{ id: string; label: string; isDefault?: boolean }>,
24 promptInjectedValues?: ReadonlyArray<string>,
25): Extract<ProviderOptionDescriptor, { type: "select" }> {
26 const defaultId = options.find((option) => option.isDefault)?.id;
27 return {
28 id,
29 label: id,
30 type: "select",
31 options: [...options],
32 ...(defaultId ? { currentValue: defaultId } : {}),
33 ...(promptInjectedValues && promptInjectedValues.length > 0
34 ? { promptInjectedValues: [...promptInjectedValues] }
35 : {}),
36 };
38​
39function booleanDescriptor(
40 id: string,
41 currentValue?: boolean,
42): Extract<ProviderOptionDescriptor, { type: "boolean" }> {
43 return {
44 id,
45 label: id,
46 type: "boolean",
47 ...(typeof currentValue === "boolean" ? { currentValue } : {}),
48 };
50​
51function modelWith(
52 descriptors: ReadonlyArray<ProviderOptionDescriptor>,
53): ReadonlyArray<ServerProviderModel> {
54 return [
55 { slug: MODEL, name: MODEL, isCustom: false, capabilities: { optionDescriptors: descriptors } },
56 ];
58​
59function selections(
60 ...entries: Array<[string, string | boolean]>
61): ReadonlyArray<ProviderOptionSelection> {
62 return entries.map(([id, value]) => ({ id, value }));
64​
65const ULTRATHINK_FRAME_CLASSES = {
66 composerFrameClassName: "ultrathink-frame",
67 composerSurfaceClassName: "shadow-[0_0_0_1px_rgba(255,255,255,0.07)_inset]",
68 modelPickerIconClassName: "ultrathink-chroma",
69} as const;
70​
71describe("getComposerProviderState", () => {
72 it("derives a stable prompt injection state for ordinary prompt edits", () => {
73 expect(getComposerPromptInjectionState("Investigate this failure")).toBe("none");
74 expect(getComposerPromptInjectionState("Ultrathink:\nInvestigate this failure")).toBe(
75 "ultrathink",
76 );
77 });
78​
79 it("uses descriptor defaults for display without dispatching them as overrides", () => {
80 const state = getComposerProviderState({
81 provider: PROVIDER,
82 model: MODEL,
83 models: modelWith([
84 selectDescriptor("effort", [
85 { id: "low", label: "Low" },
86 { id: "high", label: "High", isDefault: true },
87 ]),
88 ]),
89 modelOptions: undefined,
90 planModeEnabled: true,
91 });
92​
93 expect(state).toEqual({
94 provider: PROVIDER,
95 promptEffort: "high",
96 modelOptionsForDispatch: undefined,
97 });
98 });
99​
100 it("lets selections override defaults and propagates them through dispatch", () => {
101 const state = getComposerProviderState({
102 provider: PROVIDER,
103 model: MODEL,
104 models: modelWith([
105 selectDescriptor("effort", [
106 { id: "low", label: "Low" },
107 { id: "high", label: "High", isDefault: true },
108 ]),
109 booleanDescriptor("fastMode"),
110 ]),
111 modelOptions: selections(["effort", "low"], ["fastMode", true]),
112 planModeEnabled: true,
113 });
114​
115 expect(state).toEqual({
116 provider: PROVIDER,
117 promptEffort: "low",
118 modelOptionsForDispatch: selections(["effort", "low"], ["fastMode", true]),
119 });
120 });
121​
122 it("preserves selections that match defaults so deepMerge can overwrite prior state", () => {
123 const state = getComposerProviderState({
124 provider: PROVIDER,
125 model: MODEL,
126 models: modelWith([
127 selectDescriptor("effort", [{ id: "high", label: "High", isDefault: true }]),
128 booleanDescriptor("fastMode"),
129 ]),
130 modelOptions: selections(["effort", "high"], ["fastMode", false]),
131 planModeEnabled: true,
132 });
133​
134 expect(state.modelOptionsForDispatch).toEqual(
135 selections(["effort", "high"], ["fastMode", false]),
136 );
137 });
138​
139 it("drops selections for descriptors the model does not declare", () => {
140 const state = getComposerProviderState({
141 provider: PROVIDER,
142 model: MODEL,
143 models: modelWith([booleanDescriptor("thinking")]),
144 modelOptions: selections(["effort", "max"], ["thinking", false]),
145 planModeEnabled: true,
146 });
147​
148 expect(state).toEqual({
149 provider: PROVIDER,
150 promptEffort: null,
151 modelOptionsForDispatch: selections(["thinking", false]),
152 });
153 });
154​
155 it("derives promptEffort from the first select descriptor and preserves all others for dispatch", () => {
156 const state = getComposerProviderState({
157 provider: PROVIDER,
158 model: MODEL,
159 models: modelWith([
160 selectDescriptor("effort", [{ id: "high", label: "High", isDefault: true }]),
161 selectDescriptor("contextWindow", [
162 { id: "200k", label: "200k", isDefault: true },
163 { id: "1m", label: "1M" },
164 ]),
165 selectDescriptor("agent", [
166 { id: "build", label: "Build", isDefault: true },
167 { id: "plan", label: "Plan" },
168 ]),
169 ]),
170 modelOptions: selections(["agent", "plan"]),
171 planModeEnabled: true,
172 });
173​
174 expect(state.promptEffort).toBe("high");
175 expect(state.modelOptionsForDispatch).toEqual(selections(["agent", "plan"]));
176 });
177​
178 it("drops the plan agent from dispatch when legacy plan mode is disabled", () => {
179 const state = getComposerProviderState({
180 provider: PROVIDER,
181 model: MODEL,
182 models: modelWith([
183 selectDescriptor("agent", [
184 { id: "build", label: "Build", isDefault: true },
185 { id: "plan", label: "Plan" },
186 ]),
187 ]),
188 modelOptions: selections(["agent", "plan"]),
189 planModeEnabled: false,
190 });
191​
192 expect(state.modelOptionsForDispatch).toEqual(selections(["agent", "build"]));
193 });
194​
195 it("drops the agent descriptor entirely when plan is the only option and plan mode is disabled", () => {
196 const state = getComposerProviderState({
197 provider: PROVIDER,
198 model: MODEL,
199 models: modelWith([
200 selectDescriptor("agent", [{ id: "plan", label: "Plan", isDefault: true }]),
201 ]),
202 modelOptions: selections(["agent", "plan"]),
203 planModeEnabled: false,
204 });
205​
206 expect(state).toEqual({
207 provider: PROVIDER,
208 promptEffort: null,
209 modelOptionsForDispatch: undefined,
210 });
211 });
212​
213 it("falls back to a surviving agent when plan was the descriptor default and plan mode is disabled", () => {
214 const state = getComposerProviderState({
215 provider: PROVIDER,
216 model: MODEL,
217 models: modelWith([
218 selectDescriptor("agent", [
219 { id: "plan", label: "Plan", isDefault: true },
220 { id: "research", label: "Research" },
221 ]),
222 ]),
223 modelOptions: undefined,
224 planModeEnabled: false,
225 });
226​
227 expect(state.modelOptionsForDispatch).toBeUndefined();
228 });
229​
230 it("returns undefined dispatch options when the model declares no descriptors", () => {
231 const state = getComposerProviderState({
232 provider: PROVIDER,
233 model: MODEL,
234 models: modelWith([]),
235 modelOptions: selections(["anything", "value"]),
236 planModeEnabled: true,
237 });
238​
239 expect(state).toEqual({
240 provider: PROVIDER,
241 promptEffort: null,
242 modelOptionsForDispatch: undefined,
243 });
244 });
245​
246 it("preserves explicit options when the selected model is absent from the catalog", () => {
247 const state = getComposerProviderState({
248 provider: ProviderDriverKind.make("opencode"),
249 model: "opencode/kimi-k3",
250 models: [
251 {
252 slug: "opencode/big-pickle",
253 name: "Big Pickle",
254 isCustom: false,
255 capabilities: {},
256 },
257 ],
258 modelOptions: selections(["variant", "max"], ["agent", "build"]),
259 planModeEnabled: false,
260 });
261​
262 expect(state.modelOptionsForDispatch).toEqual(
263 selections(["variant", "max"], ["agent", "build"]),
264 );
265 });
266​
267 it.each(["codex", "claudeAgent", "cursor", "grok"])(
268 "does not preserve unknown options for a missing %s model",
269 (provider) => {
270 const state = getComposerProviderState({
271 provider: ProviderDriverKind.make(provider),
272 model: "missing-model",
273 models: modelWith([]),
274 modelOptions: selections(["unknown", "value"]),
275 planModeEnabled: true,
276 });
277​
278 expect(state.modelOptionsForDispatch).toBeUndefined();
279 },
280 );
281​
282 it("preserves explicit options while the catalog is empty", () => {
283 const state = getComposerProviderState({
284 provider: ProviderDriverKind.make("opencode"),
285 model: "opencode/kimi-k3",
286 models: [],
287 modelOptions: selections(["variant", "max"], ["agent", "build"]),
288 planModeEnabled: false,
289 });
290​
291 expect(state.modelOptionsForDispatch).toEqual(
292 selections(["variant", "max"], ["agent", "build"]),
293 );
294 });
295​
296 it("validates options for a known model selected through a legacy alias", () => {
297 const state = getComposerProviderState({
298 provider: ProviderDriverKind.make("claudeAgent"),
299 model: "legacy-test-model",
300 models: [
301 {
302 slug: "test-model",
303 name: "Test Model",
304 aliases: ["legacy-test-model"],
305 isCustom: false,
306 capabilities: {
307 optionDescriptors: [
308 selectDescriptor("effort", [
309 { id: "low", label: "Low" },
310 { id: "high", label: "High", isDefault: true },
311 ]),
312 ],
313 },
314 },
315 ],
316 modelOptions: selections(["effort", "low"], ["unknown", "value"]),
317 planModeEnabled: false,
318 });
319​
320 expect(state.modelOptionsForDispatch).toEqual(selections(["effort", "low"]));
321 });
322​
323 it("still drops the plan agent when an absent model has a saved plan selection", () => {
324 const state = getComposerProviderState({
325 provider: ProviderDriverKind.make("opencode"),
326 model: "opencode/kimi-k3",
327 models: [],
328 modelOptions: selections(["variant", "max"], ["agent", "plan"]),
329 planModeEnabled: false,
330 });
331​
332 expect(state.modelOptionsForDispatch).toEqual(selections(["variant", "max"]));
333 });
334​
335 it("adds ultrathink class names when the prompt triggers a promptInjectedValues descriptor", () => {
336 const state = getComposerProviderState({
337 provider: PROVIDER,
338 model: MODEL,
339 models: modelWith([
340 selectDescriptor(
341 "effort",
342 [
343 { id: "medium", label: "Medium" },
344 { id: "high", label: "High", isDefault: true },
345 { id: "ultrathink", label: "Ultrathink" },
346 ],
347 ["ultrathink"],
348 ),
349 ]),
350 promptInjectionState: getComposerPromptInjectionState(
351 "Ultrathink:\nInvestigate this failure",
352 ),
353 modelOptions: selections(["effort", "medium"]),
354 planModeEnabled: true,
355 });
356​
357 expect(state).toEqual({
358 provider: PROVIDER,
359 promptEffort: "medium",
360 modelOptionsForDispatch: selections(["effort", "medium"]),
361 ...ULTRATHINK_FRAME_CLASSES,
362 });
363 });
364​
365 it("does not add ultrathink class names when the descriptor has no promptInjectedValues", () => {
366 const state = getComposerProviderState({
367 provider: PROVIDER,
368 model: MODEL,
369 models: modelWith([
370 selectDescriptor("effort", [{ id: "high", label: "High", isDefault: true }]),
371 ]),
372 promptInjectionState: getComposerPromptInjectionState(
373 "Ultrathink:\nInvestigate this failure",
374 ),
375 modelOptions: undefined,
376 planModeEnabled: true,
377 });
378​
379 expect(state).not.toHaveProperty("composerFrameClassName");
380 expect(state).not.toHaveProperty("composerSurfaceClassName");
381 expect(state).not.toHaveProperty("modelPickerIconClassName");
382 });
383​
384 it("defaults fastMode to false when the provider reports true but the user has not selected it", () => {
385 const state = getComposerProviderState({
386 provider: ProviderDriverKind.make("cursor"),
387 model: MODEL,
388 models: modelWith([booleanDescriptor("fastMode", true)]),
389 modelOptions: undefined,
390 planModeEnabled: true,
391 });
392​
393 expect(state.modelOptionsForDispatch).toEqual(selections(["fastMode", false]));
394 });
395​
396 it("keeps explicit fastMode true when the user selected Fast", () => {
397 const state = getComposerProviderState({
398 provider: ProviderDriverKind.make("cursor"),
399 model: MODEL,
400 models: modelWith([booleanDescriptor("fastMode", true)]),
401 modelOptions: selections(["fastMode", true]),
402 planModeEnabled: true,
403 });
404​
405 expect(state.modelOptionsForDispatch).toEqual(selections(["fastMode", true]));
406 });
407​
408 it("keeps explicit fastMode false when the user selected Normal", () => {
409 const state = getComposerProviderState({
410 provider: ProviderDriverKind.make("cursor"),
411 model: MODEL,
412 models: modelWith([booleanDescriptor("fastMode", true)]),
413 modelOptions: selections(["fastMode", false]),
414 planModeEnabled: true,
415 });
416​
417 expect(state.modelOptionsForDispatch).toEqual(selections(["fastMode", false]));
418 });
419});
420​
421describe("withImplicitFastModeDefault", () => {
422 it("injects fastMode false only when the model exposes fastMode and no selection exists", () => {
423 expect(
424 withImplicitFastModeDefault(
425 {
426 optionDescriptors: [booleanDescriptor("fastMode", true)],
427 },
428 undefined,
429 ),
430 ).toEqual(selections(["fastMode", false]));
431​
432 expect(
433 withImplicitFastModeDefault(
434 {
435 optionDescriptors: [booleanDescriptor("fastMode", true)],
436 },
437 selections(["fastMode", true]),
438 ),
439 ).toEqual(selections(["fastMode", true]));
440 });
441​
442 it("does not add fastMode when the model does not expose it", () => {
443 expect(
444 withImplicitFastModeDefault(
445 {
446 optionDescriptors: [booleanDescriptor("thinking", true)],
447 },
448 undefined,
449 ),
450 ).toBeUndefined();
451 });
452});
453​
454describe("trait controls fastMode display", () => {
455 it("resolves traits fastMode to Normal when the provider defaults to true without a user selection", () => {
456 const models = modelWith([booleanDescriptor("fastMode", true)]);
457 const provider = ProviderDriverKind.make("cursor");
458 const caps = getProviderModelCapabilities(models, MODEL, provider);
459 const resolved = withImplicitFastModeDefault(caps, undefined);
460 const descriptors = getProviderOptionDescriptors({ caps, selections: resolved });
461 const fastMode = descriptors.find((descriptor) => descriptor.id === "fastMode");
462​
463 expect(fastMode?.type).toBe("boolean");
464 if (fastMode?.type === "boolean") {
465 expect(fastMode.currentValue).toBe(false);
466 }
467 });
468});
469​
470describe("provider traits render guards", () => {
471 it("returns null when no thread target is provided", () => {
472 const models = modelWith([
473 selectDescriptor("effort", [{ id: "high", label: "High", isDefault: true }]),
474 ]);
475 const args = {
476 provider: PROVIDER,
477 model: MODEL,
478 models,
479 modelOptions: undefined,
480 prompt: "",
481 onPromptChange: () => {},
482 planModeEnabled: true,
483 };
484​
485 expect(renderProviderTraitsPicker(args)).toBeNull();
486 expect(renderProviderTraitsMenuContent(args)).toBeNull();
487 });
488});