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("uses descriptor defaults for display without dispatching them as overrides", () => {
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: undefined,
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(selections(["agent", "plan"]));
169 });
170
171 it("drops the plan agent from dispatch when legacy plan mode is disabled", () => {
172 const state = getComposerProviderState({
173 provider: PROVIDER,
174 model: MODEL,
175 models: modelWith([
176 selectDescriptor("agent", [
177 { id: "build", label: "Build", isDefault: true },
178 { id: "plan", label: "Plan" },
179 ]),
180 ]),
181 modelOptions: selections(["agent", "plan"]),
182 planModeEnabled: false,
183 });
184
185 expect(state.modelOptionsForDispatch).toEqual(selections(["agent", "build"]));
186 });
187
188 it("drops the agent descriptor entirely when plan is the only option and plan mode is disabled", () => {
189 const state = getComposerProviderState({
190 provider: PROVIDER,
191 model: MODEL,
192 models: modelWith([
193 selectDescriptor("agent", [{ id: "plan", label: "Plan", isDefault: true }]),
194 ]),
195 modelOptions: selections(["agent", "plan"]),
196 planModeEnabled: false,
197 });
198
199 expect(state).toEqual({
200 provider: PROVIDER,
201 promptEffort: null,
202 modelOptionsForDispatch: undefined,
203 });
204 });
205
206 it("falls back to a surviving agent when plan was the descriptor default and plan mode is disabled", () => {
207 const state = getComposerProviderState({
208 provider: PROVIDER,
209 model: MODEL,
210 models: modelWith([
211 selectDescriptor("agent", [
212 { id: "plan", label: "Plan", isDefault: true },
213 { id: "research", label: "Research" },
214 ]),
215 ]),
216 modelOptions: undefined,
217 planModeEnabled: false,
218 });
219
220 expect(state.modelOptionsForDispatch).toBeUndefined();
221 });
222
223 it("returns undefined dispatch options when the model declares no descriptors", () => {
224 const state = getComposerProviderState({
225 provider: PROVIDER,
226 model: MODEL,
227 models: modelWith([]),
228 modelOptions: selections(["anything", "value"]),
229 planModeEnabled: true,
230 });
231
232 expect(state).toEqual({
233 provider: PROVIDER,
234 promptEffort: null,
235 modelOptionsForDispatch: undefined,
236 });
237 });
238
239 it("preserves explicit options when the selected model is absent from the catalog", () => {
240 const state = getComposerProviderState({
241 provider: ProviderDriverKind.make("opencode"),
242 model: "opencode/kimi-k3",
243 models: [
244 {
245 slug: "opencode/big-pickle",
246 name: "Big Pickle",
247 isCustom: false,
248 capabilities: {},
249 },
250 ],
251 modelOptions: selections(["variant", "max"], ["agent", "build"]),
252 planModeEnabled: false,
253 });
254
255 expect(state.modelOptionsForDispatch).toEqual(
256 selections(["variant", "max"], ["agent", "build"]),
257 );
258 });
259
260 it.each(["codex", "claudeAgent", "cursor", "grok"])(
261 "does not preserve unknown options for a missing %s model",
262 (provider) => {
263 const state = getComposerProviderState({
264 provider: ProviderDriverKind.make(provider),
265 model: "missing-model",
266 models: modelWith([]),
267 modelOptions: selections(["unknown", "value"]),
268 planModeEnabled: true,
269 });
270
271 expect(state.modelOptionsForDispatch).toBeUndefined();
272 },
273 );
274
275 it("preserves explicit options while the catalog is empty", () => {
276 const state = getComposerProviderState({
277 provider: ProviderDriverKind.make("opencode"),
278 model: "opencode/kimi-k3",
279 models: [],
280 modelOptions: selections(["variant", "max"], ["agent", "build"]),
281 planModeEnabled: false,
282 });
283
284 expect(state.modelOptionsForDispatch).toEqual(
285 selections(["variant", "max"], ["agent", "build"]),
286 );
287 });
288
289 it("validates options for a known model selected through a legacy alias", () => {
290 const state = getComposerProviderState({
291 provider: ProviderDriverKind.make("claudeAgent"),
292 model: "legacy-test-model",
293 models: [
294 {
295 slug: "test-model",
296 name: "Test Model",
297 aliases: ["legacy-test-model"],
298 isCustom: false,
299 capabilities: {
300 optionDescriptors: [
301 selectDescriptor("effort", [
302 { id: "low", label: "Low" },
303 { id: "high", label: "High", isDefault: true },
304 ]),
305 ],
306 },
307 },
308 ],
309 modelOptions: selections(["effort", "low"], ["unknown", "value"]),
310 planModeEnabled: false,
311 });
312
313 expect(state.modelOptionsForDispatch).toEqual(selections(["effort", "low"]));
314 });
315
316 it("still drops the plan agent when an absent model has a saved plan selection", () => {
317 const state = getComposerProviderState({
318 provider: ProviderDriverKind.make("opencode"),
319 model: "opencode/kimi-k3",
320 models: [],
321 modelOptions: selections(["variant", "max"], ["agent", "plan"]),
322 planModeEnabled: false,
323 });
324
325 expect(state.modelOptionsForDispatch).toEqual(selections(["variant", "max"]));
326 });
327
328 it("adds ultrathink class names when the prompt triggers a promptInjectedValues descriptor", () => {
329 const state = getComposerProviderState({
330 provider: PROVIDER,
331 model: MODEL,
332 models: modelWith([
333 selectDescriptor(
334 "effort",
335 [
336 { id: "medium", label: "Medium" },
337 { id: "high", label: "High", isDefault: true },
338 { id: "ultrathink", label: "Ultrathink" },
339 ],
340 ["ultrathink"],
341 ),
342 ]),
343 promptInjectionState: getComposerPromptInjectionState(
344 "Ultrathink:\nInvestigate this failure",
345 ),
346 modelOptions: selections(["effort", "medium"]),
347 planModeEnabled: true,
348 });
349
350 expect(state).toEqual({
351 provider: PROVIDER,
352 promptEffort: "medium",
353 modelOptionsForDispatch: selections(["effort", "medium"]),
354 ...ULTRATHINK_FRAME_CLASSES,
355 });
356 });
357
358 it("does not add ultrathink class names when the descriptor has no promptInjectedValues", () => {
359 const state = getComposerProviderState({
360 provider: PROVIDER,
361 model: MODEL,
362 models: modelWith([
363 selectDescriptor("effort", [{ id: "high", label: "High", isDefault: true }]),
364 ]),
365 promptInjectionState: getComposerPromptInjectionState(
366 "Ultrathink:\nInvestigate this failure",
367 ),
368 modelOptions: undefined,
369 planModeEnabled: true,
370 });
371
372 expect(state).not.toHaveProperty("composerFrameClassName");
373 expect(state).not.toHaveProperty("composerSurfaceClassName");
374 expect(state).not.toHaveProperty("modelPickerIconClassName");
375 });
376});
377
378describe("provider traits render guards", () => {
379 it("returns null when no thread target is provided", () => {
380 const models = modelWith([
381 selectDescriptor("effort", [{ id: "high", label: "High", isDefault: true }]),
382 ]);
383 const args = {
384 provider: PROVIDER,
385 model: MODEL,
386 models,
387 modelOptions: undefined,
388 prompt: "",
389 onPromptChange: () => {},
390 planModeEnabled: true,
391 };
392
393 expect(renderProviderTraitsPicker(args)).toBeNull();
394 expect(renderProviderTraitsMenuContent(args)).toBeNull();
395 });
396});