Open History to see the last commit
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 {
9 getComposerPromptInjectionState,
10 getComposerProviderState,
11 renderProviderTraitsMenuContent,
12 renderProviderTraitsPicker,
13} from "./composerProviderState";
14
15// Everything in composerProviderState is now data-driven by the model's
16// optionDescriptors, so these tests use a single synthetic provider/model and
17// vary only the descriptor shape per scenario.
18
19const PROVIDER: ProviderDriverKind = ProviderDriverKind.make("codex");
20const MODEL = "test-model";
21
22function selectDescriptor(
23 id: string,
24 options: ReadonlyArray<{ id: string; label: string; isDefault?: boolean }>,
25 promptInjectedValues?: ReadonlyArray<string>,
26): Extract<ProviderOptionDescriptor, { type: "select" }> {
27 const defaultId = options.find((option) => option.isDefault)?.id;
28 return {
29 id,
30 label: id,
31 type: "select",
32 options: [...options],
33 ...(defaultId ? { currentValue: defaultId } : {}),
34 ...(promptInjectedValues && promptInjectedValues.length > 0
35 ? { promptInjectedValues: [...promptInjectedValues] }
36 : {}),
37 };
38}
39
40function booleanDescriptor(id: string): Extract<ProviderOptionDescriptor, { type: "boolean" }> {
41 return { id, label: id, type: "boolean" };
42}
43
44function modelWith(
45 descriptors: ReadonlyArray<ProviderOptionDescriptor>,
46): ReadonlyArray<ServerProviderModel> {
47 return [
48 { slug: MODEL, name: MODEL, isCustom: false, capabilities: { optionDescriptors: descriptors } },
49 ];
50}
51
52function selections(
53 ...entries: Array<[string, string | boolean]>
54): ReadonlyArray<ProviderOptionSelection> {
55 return entries.map(([id, value]) => ({ id, value }));
56}
57
58const ULTRATHINK_FRAME_CLASSES = {
59 composerFrameClassName: "ultrathink-frame",
60 composerSurfaceClassName: "shadow-[0_0_0_1px_rgba(255,255,255,0.07)_inset]",
61 modelPickerIconClassName: "ultrathink-chroma",
62} as const;
63
64const DEFAULT_INPUT_CAPS = { images: true, audio: true, files: true } as const;
65
66describe("getComposerProviderState", () => {
67 it("derives a stable prompt injection state for ordinary prompt edits", () => {
68 expect(getComposerPromptInjectionState("Investigate this failure")).toBe("none");
69 expect(getComposerPromptInjectionState("Ultrathink:\nInvestigate this failure")).toBe(
70 "ultrathink",
71 );
72 });
73
74 it("uses descriptor defaults for display without dispatching them as overrides", () => {
75 const state = getComposerProviderState({
76 provider: PROVIDER,
77 model: MODEL,
78 models: modelWith([
79 selectDescriptor("effort", [
80 { id: "low", label: "Low" },
81 { id: "high", label: "High", isDefault: true },
82 ]),
83 ]),
84 modelOptions: undefined,
85 planModeEnabled: true,
86 });
87
88 expect(state).toEqual({
89 provider: PROVIDER,
90 promptEffort: "high",
91 modelOptionsForDispatch: undefined,
92 inputCapabilities: DEFAULT_INPUT_CAPS,
93 });
94 });
95
96 it("lets selections override defaults and propagates them through dispatch", () => {
97 const state = getComposerProviderState({
98 provider: PROVIDER,
99 model: MODEL,
100 models: modelWith([
101 selectDescriptor("effort", [
102 { id: "low", label: "Low" },
103 { id: "high", label: "High", isDefault: true },
104 ]),
105 booleanDescriptor("fastMode"),
106 ]),
107 modelOptions: selections(["effort", "low"], ["fastMode", true]),
108 planModeEnabled: true,
109 });
110
111 expect(state).toEqual({
112 provider: PROVIDER,
113 promptEffort: "low",
114 modelOptionsForDispatch: selections(["effort", "low"], ["fastMode", true]),
115 inputCapabilities: DEFAULT_INPUT_CAPS,
116 });
117 });
118
119 it("preserves selections that match defaults so deepMerge can overwrite prior state", () => {
120 const state = getComposerProviderState({
121 provider: PROVIDER,
122 model: MODEL,
123 models: modelWith([
124 selectDescriptor("effort", [{ id: "high", label: "High", isDefault: true }]),
125 booleanDescriptor("fastMode"),
126 ]),
127 modelOptions: selections(["effort", "high"], ["fastMode", false]),
128 planModeEnabled: true,
129 });
130
131 expect(state.modelOptionsForDispatch).toEqual(
132 selections(["effort", "high"], ["fastMode", false]),
133 );
134 });
135
136 it("drops selections for descriptors the model does not declare", () => {
137 const state = getComposerProviderState({
138 provider: PROVIDER,
139 model: MODEL,
140 models: modelWith([booleanDescriptor("thinking")]),
141 modelOptions: selections(["effort", "max"], ["thinking", false]),
142 planModeEnabled: true,
143 });
144
145 expect(state).toEqual({
146 provider: PROVIDER,
147 promptEffort: null,
148 modelOptionsForDispatch: selections(["thinking", false]),
149 inputCapabilities: DEFAULT_INPUT_CAPS,
150 });
151 });
152
153 it("derives promptEffort from the first select descriptor and preserves all others for dispatch", () => {
154 const state = getComposerProviderState({
155 provider: PROVIDER,
156 model: MODEL,
157 models: modelWith([
158 selectDescriptor("effort", [{ id: "high", label: "High", isDefault: true }]),
159 selectDescriptor("contextWindow", [
160 { id: "200k", label: "200k", isDefault: true },
161 { id: "1m", label: "1M" },
162 ]),
163 selectDescriptor("agent", [
164 { id: "build", label: "Build", isDefault: true },
165 { id: "plan", label: "Plan" },
166 ]),
167 ]),
168 modelOptions: selections(["agent", "plan"]),
169 planModeEnabled: true,
170 });
171
172 expect(state.promptEffort).toBe("high");
173 expect(state.modelOptionsForDispatch).toEqual(selections(["agent", "plan"]));
174 });
175
176 it("drops the plan agent from dispatch when legacy plan mode is disabled", () => {
177 const state = getComposerProviderState({
178 provider: PROVIDER,
179 model: MODEL,
180 models: modelWith([
181 selectDescriptor("agent", [
182 { id: "build", label: "Build", isDefault: true },
183 { id: "plan", label: "Plan" },
184 ]),
185 ]),
186 modelOptions: selections(["agent", "plan"]),
187 planModeEnabled: false,
188 });
189
190 expect(state.modelOptionsForDispatch).toEqual(selections(["agent", "build"]));
191 });
192
193 it("drops the agent descriptor entirely when plan is the only option and plan mode is disabled", () => {
194 const state = getComposerProviderState({
195 provider: PROVIDER,
196 model: MODEL,
197 models: modelWith([
198 selectDescriptor("agent", [{ id: "plan", label: "Plan", isDefault: true }]),
199 ]),
200 modelOptions: selections(["agent", "plan"]),
201 planModeEnabled: false,
202 });
203
204 expect(state).toEqual({
205 provider: PROVIDER,
206 promptEffort: null,
207 modelOptionsForDispatch: undefined,
208 inputCapabilities: DEFAULT_INPUT_CAPS,
209 });
210 });
211
212 it("falls back to a surviving agent when plan was the descriptor default and plan mode is disabled", () => {
213 const state = getComposerProviderState({
214 provider: PROVIDER,
215 model: MODEL,
216 models: modelWith([
217 selectDescriptor("agent", [
218 { id: "plan", label: "Plan", isDefault: true },
219 { id: "research", label: "Research" },
220 ]),
221 ]),
222 modelOptions: undefined,
223 planModeEnabled: false,
224 });
225
226 expect(state.modelOptionsForDispatch).toBeUndefined();
227 });
228
229 it("returns undefined dispatch options when the model declares no descriptors", () => {
230 const state = getComposerProviderState({
231 provider: PROVIDER,
232 model: MODEL,
233 models: modelWith([]),
234 modelOptions: selections(["anything", "value"]),
235 planModeEnabled: true,
236 });
237
238 expect(state).toEqual({
239 provider: PROVIDER,
240 promptEffort: null,
241 modelOptionsForDispatch: undefined,
242 inputCapabilities: DEFAULT_INPUT_CAPS,
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 inputCapabilities: DEFAULT_INPUT_CAPS,
362 ...ULTRATHINK_FRAME_CLASSES,
363 });
364 });
365
366 it("does not add ultrathink class names when the descriptor has no promptInjectedValues", () => {
367 const state = getComposerProviderState({
368 provider: PROVIDER,
369 model: MODEL,
370 models: modelWith([
371 selectDescriptor("effort", [{ id: "high", label: "High", isDefault: true }]),
372 ]),
373 promptInjectionState: getComposerPromptInjectionState(
374 "Ultrathink:\nInvestigate this failure",
375 ),
376 modelOptions: undefined,
377 planModeEnabled: true,
378 });
379
380 expect(state).not.toHaveProperty("composerFrameClassName");
381 expect(state).not.toHaveProperty("composerSurfaceClassName");
382 expect(state).not.toHaveProperty("modelPickerIconClassName");
383 });
384
385 it("reflects the active model's declared input capabilities", () => {
386 const state = getComposerProviderState({
387 provider: PROVIDER,
388 model: MODEL,
389 models: [
390 {
391 slug: MODEL,
392 name: MODEL,
393 isCustom: false,
394 capabilities: { optionDescriptors: [], inputImages: false, inputAudio: false },
395 },
396 ],
397 modelOptions: undefined,
398 planModeEnabled: true,
399 });
400
401 expect(state.inputCapabilities).toEqual({
402 images: false,
403 audio: false,
404 files: true,
405 });
406 });
407
408 it("defaults unsupported-modalities to true when the model declares none", () => {
409 const state = getComposerProviderState({
410 provider: PROVIDER,
411 model: MODEL,
412 models: modelWith([]),
413 modelOptions: undefined,
414 planModeEnabled: true,
415 });
416
417 expect(state.inputCapabilities).toEqual(DEFAULT_INPUT_CAPS);
418 });
419});
420
421describe("provider traits render guards", () => {
422 it("returns null when no thread target is provided", () => {
423 const models = modelWith([
424 selectDescriptor("effort", [{ id: "high", label: "High", isDefault: true }]),
425 ]);
426 const args = {
427 provider: PROVIDER,
428 model: MODEL,
429 models,
430 modelOptions: undefined,
431 prompt: "",
432 onPromptChange: () => {},
433 planModeEnabled: true,
434 };
435
436 expect(renderProviderTraitsPicker(args)).toBeNull();
437 expect(renderProviderTraitsMenuContent(args)).toBeNull();
438 });
439});