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
64describe("getComposerProviderState", () => {
65 it("derives a stable prompt injection state for ordinary prompt edits", () => {
66 expect(getComposerPromptInjectionState("Investigate this failure")).toBe("none");
67 expect(getComposerPromptInjectionState("Ultrathink:\nInvestigate this failure")).toBe(
68 "ultrathink",
69 );
70 });
71
72 it("returns descriptor defaults when no selections are provided", () => {
73 const state = getComposerProviderState({
74 provider: PROVIDER,
75 model: MODEL,
76 models: modelWith([
77 selectDescriptor("effort", [
78 { id: "low", label: "Low" },
79 { id: "high", label: "High", isDefault: true },
80 ]),
81 ]),
82 modelOptions: undefined,
83 planModeEnabled: true,
84 });
85
86 expect(state).toEqual({
87 provider: PROVIDER,
88 promptEffort: "high",
89 modelOptionsForDispatch: selections(["effort", "high"]),
90 });
91 });
92
93 it("lets selections override defaults and propagates them through dispatch", () => {
94 const state = getComposerProviderState({
95 provider: PROVIDER,
96 model: MODEL,
97 models: modelWith([
98 selectDescriptor("effort", [
99 { id: "low", label: "Low" },
100 { id: "high", label: "High", isDefault: true },
101 ]),
102 booleanDescriptor("fastMode"),
103 ]),
104 modelOptions: selections(["effort", "low"], ["fastMode", true]),
105 planModeEnabled: true,
106 });
107
108 expect(state).toEqual({
109 provider: PROVIDER,
110 promptEffort: "low",
111 modelOptionsForDispatch: selections(["effort", "low"], ["fastMode", true]),
112 });
113 });
114
115 it("preserves selections that match defaults so deepMerge can overwrite prior state", () => {
116 const state = getComposerProviderState({
117 provider: PROVIDER,
118 model: MODEL,
119 models: modelWith([
120 selectDescriptor("effort", [{ id: "high", label: "High", isDefault: true }]),
121 booleanDescriptor("fastMode"),
122 ]),
123 modelOptions: selections(["effort", "high"], ["fastMode", false]),
124 planModeEnabled: true,
125 });
126
127 expect(state.modelOptionsForDispatch).toEqual(
128 selections(["effort", "high"], ["fastMode", false]),
129 );
130 });
131
132 it("drops selections for descriptors the model does not declare", () => {
133 const state = getComposerProviderState({
134 provider: PROVIDER,
135 model: MODEL,
136 models: modelWith([booleanDescriptor("thinking")]),
137 modelOptions: selections(["effort", "max"], ["thinking", false]),
138 planModeEnabled: true,
139 });
140
141 expect(state).toEqual({
142 provider: PROVIDER,
143 promptEffort: null,
144 modelOptionsForDispatch: selections(["thinking", false]),
145 });
146 });
147
148 it("derives promptEffort from the first select descriptor and preserves all others for dispatch", () => {
149 const state = getComposerProviderState({
150 provider: PROVIDER,
151 model: MODEL,
152 models: modelWith([
153 selectDescriptor("effort", [{ id: "high", label: "High", isDefault: true }]),
154 selectDescriptor("contextWindow", [
155 { id: "200k", label: "200k", isDefault: true },
156 { id: "1m", label: "1M" },
157 ]),
158 selectDescriptor("agent", [
159 { id: "build", label: "Build", isDefault: true },
160 { id: "plan", label: "Plan" },
161 ]),
162 ]),
163 modelOptions: selections(["agent", "plan"]),
164 planModeEnabled: true,
165 });
166
167 expect(state.promptEffort).toBe("high");
168 expect(state.modelOptionsForDispatch).toEqual(
169 selections(["effort", "high"], ["contextWindow", "200k"], ["agent", "plan"]),
170 );
171 });
172
173 it("drops the plan agent from dispatch when legacy plan mode is disabled", () => {
174 const state = getComposerProviderState({
175 provider: PROVIDER,
176 model: MODEL,
177 models: modelWith([
178 selectDescriptor("agent", [
179 { id: "build", label: "Build", isDefault: true },
180 { id: "plan", label: "Plan" },
181 ]),
182 ]),
183 modelOptions: selections(["agent", "plan"]),
184 planModeEnabled: false,
185 });
186
187 expect(state.modelOptionsForDispatch).toEqual(selections(["agent", "build"]));
188 });
189
190 it("drops the agent descriptor entirely when plan is the only option and plan mode is disabled", () => {
191 const state = getComposerProviderState({
192 provider: PROVIDER,
193 model: MODEL,
194 models: modelWith([
195 selectDescriptor("agent", [{ id: "plan", label: "Plan", isDefault: true }]),
196 ]),
197 modelOptions: selections(["agent", "plan"]),
198 planModeEnabled: false,
199 });
200
201 expect(state).toEqual({
202 provider: PROVIDER,
203 promptEffort: null,
204 modelOptionsForDispatch: undefined,
205 });
206 });
207
208 it("falls back to a surviving agent when plan was the descriptor default and plan mode is disabled", () => {
209 const state = getComposerProviderState({
210 provider: PROVIDER,
211 model: MODEL,
212 models: modelWith([
213 selectDescriptor("agent", [
214 { id: "plan", label: "Plan", isDefault: true },
215 { id: "research", label: "Research" },
216 ]),
217 ]),
218 modelOptions: undefined,
219 planModeEnabled: false,
220 });
221
222 expect(state.modelOptionsForDispatch).toEqual(selections(["agent", "research"]));
223 });
224
225 it("returns undefined dispatch options when the model declares no descriptors", () => {
226 const state = getComposerProviderState({
227 provider: PROVIDER,
228 model: MODEL,
229 models: modelWith([]),
230 modelOptions: selections(["anything", "value"]),
231 planModeEnabled: true,
232 });
233
234 expect(state).toEqual({
235 provider: PROVIDER,
236 promptEffort: null,
237 modelOptionsForDispatch: undefined,
238 });
239 });
240
241 it("preserves explicit options when the selected model is absent from the catalog", () => {
242 const state = getComposerProviderState({
243 provider: ProviderDriverKind.make("opencode"),
244 model: "opencode/kimi-k3",
245 models: [
246 {
247 slug: "opencode/big-pickle",
248 name: "Big Pickle",
249 isCustom: false,
250 capabilities: {},
251 },
252 ],
253 modelOptions: selections(["variant", "max"], ["agent", "build"]),
254 planModeEnabled: false,
255 });
256
257 expect(state.modelOptionsForDispatch).toEqual(
258 selections(["variant", "max"], ["agent", "build"]),
259 );
260 });
261
262 it.each(["codex", "claudeAgent", "cursor", "grok"])(
263 "does not preserve unknown options for a missing %s model",
264 (provider) => {
265 const state = getComposerProviderState({
266 provider: ProviderDriverKind.make(provider),
267 model: "missing-model",
268 models: modelWith([]),
269 modelOptions: selections(["unknown", "value"]),
270 planModeEnabled: true,
271 });
272
273 expect(state.modelOptionsForDispatch).toBeUndefined();
274 },
275 );
276
277 it("preserves explicit options while the catalog is empty", () => {
278 const state = getComposerProviderState({
279 provider: ProviderDriverKind.make("opencode"),
280 model: "opencode/kimi-k3",
281 models: [],
282 modelOptions: selections(["variant", "max"], ["agent", "build"]),
283 planModeEnabled: false,
284 });
285
286 expect(state.modelOptionsForDispatch).toEqual(
287 selections(["variant", "max"], ["agent", "build"]),
288 );
289 });
290
291 it("validates options for a known model selected through a legacy alias", () => {
292 const state = getComposerProviderState({
293 provider: ProviderDriverKind.make("claudeAgent"),
294 model: "opus",
295 models: [
296 {
297 slug: "claude-opus-5",
298 name: "Claude Opus 5",
299 isCustom: false,
300 capabilities: {
301 optionDescriptors: [
302 selectDescriptor("effort", [
303 { id: "low", label: "Low" },
304 { id: "high", label: "High", isDefault: true },
305 ]),
306 ],
307 },
308 },
309 ],
310 modelOptions: selections(["effort", "low"], ["unknown", "value"]),
311 planModeEnabled: false,
312 });
313
314 expect(state.modelOptionsForDispatch).toEqual(selections(["effort", "low"]));
315 });
316
317 it("still drops the plan agent when an absent model has a saved plan selection", () => {
318 const state = getComposerProviderState({
319 provider: ProviderDriverKind.make("opencode"),
320 model: "opencode/kimi-k3",
321 models: [],
322 modelOptions: selections(["variant", "max"], ["agent", "plan"]),
323 planModeEnabled: false,
324 });
325
326 expect(state.modelOptionsForDispatch).toEqual(selections(["variant", "max"]));
327 });
328
329 it("adds ultrathink class names when the prompt triggers a promptInjectedValues descriptor", () => {
330 const state = getComposerProviderState({
331 provider: PROVIDER,
332 model: MODEL,
333 models: modelWith([
334 selectDescriptor(
335 "effort",
336 [
337 { id: "medium", label: "Medium" },
338 { id: "high", label: "High", isDefault: true },
339 { id: "ultrathink", label: "Ultrathink" },
340 ],
341 ["ultrathink"],
342 ),
343 ]),
344 promptInjectionState: getComposerPromptInjectionState(
345 "Ultrathink:\nInvestigate this failure",
346 ),
347 modelOptions: selections(["effort", "medium"]),
348 planModeEnabled: true,
349 });
350
351 expect(state).toEqual({
352 provider: PROVIDER,
353 promptEffort: "medium",
354 modelOptionsForDispatch: selections(["effort", "medium"]),
355 ...ULTRATHINK_FRAME_CLASSES,
356 });
357 });
358
359 it("does not add ultrathink class names when the descriptor has no promptInjectedValues", () => {
360 const state = getComposerProviderState({
361 provider: PROVIDER,
362 model: MODEL,
363 models: modelWith([
364 selectDescriptor("effort", [{ id: "high", label: "High", isDefault: true }]),
365 ]),
366 promptInjectionState: getComposerPromptInjectionState(
367 "Ultrathink:\nInvestigate this failure",
368 ),
369 modelOptions: undefined,
370 planModeEnabled: true,
371 });
372
373 expect(state).not.toHaveProperty("composerFrameClassName");
374 expect(state).not.toHaveProperty("composerSurfaceClassName");
375 expect(state).not.toHaveProperty("modelPickerIconClassName");
376 });
377});
378
379describe("provider traits render guards", () => {
380 it("returns null when no thread target is provided", () => {
381 const models = modelWith([
382 selectDescriptor("effort", [{ id: "high", label: "High", isDefault: true }]),
383 ]);
384 const args = {
385 provider: PROVIDER,
386 model: MODEL,
387 models,
388 modelOptions: undefined,
389 prompt: "",
390 onPromptChange: () => {},
391 planModeEnabled: true,
392 };
393
394 expect(renderProviderTraitsPicker(args)).toBeNull();
395 expect(renderProviderTraitsMenuContent(args)).toBeNull();
396 });
397});