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("adds ultrathink class names when the prompt triggers a promptInjectedValues descriptor", () => {
242 const state = getComposerProviderState({
243 provider: PROVIDER,
244 model: MODEL,
245 models: modelWith([
246 selectDescriptor(
247 "effort",
248 [
249 { id: "medium", label: "Medium" },
250 { id: "high", label: "High", isDefault: true },
251 { id: "ultrathink", label: "Ultrathink" },
252 ],
253 ["ultrathink"],
254 ),
255 ]),
256 promptInjectionState: getComposerPromptInjectionState(
257 "Ultrathink:\nInvestigate this failure",
258 ),
259 modelOptions: selections(["effort", "medium"]),
260 planModeEnabled: true,
261 });
262
263 expect(state).toEqual({
264 provider: PROVIDER,
265 promptEffort: "medium",
266 modelOptionsForDispatch: selections(["effort", "medium"]),
267 ...ULTRATHINK_FRAME_CLASSES,
268 });
269 });
270
271 it("does not add ultrathink class names when the descriptor has no promptInjectedValues", () => {
272 const state = getComposerProviderState({
273 provider: PROVIDER,
274 model: MODEL,
275 models: modelWith([
276 selectDescriptor("effort", [{ id: "high", label: "High", isDefault: true }]),
277 ]),
278 promptInjectionState: getComposerPromptInjectionState(
279 "Ultrathink:\nInvestigate this failure",
280 ),
281 modelOptions: undefined,
282 planModeEnabled: true,
283 });
284
285 expect(state).not.toHaveProperty("composerFrameClassName");
286 expect(state).not.toHaveProperty("composerSurfaceClassName");
287 expect(state).not.toHaveProperty("modelPickerIconClassName");
288 });
289});
290
291describe("provider traits render guards", () => {
292 it("returns null when no thread target is provided", () => {
293 const models = modelWith([
294 selectDescriptor("effort", [{ id: "high", label: "High", isDefault: true }]),
295 ]);
296 const args = {
297 provider: PROVIDER,
298 model: MODEL,
299 models,
300 modelOptions: undefined,
301 prompt: "",
302 onPromptChange: () => {},
303 planModeEnabled: true,
304 };
305
306 expect(renderProviderTraitsPicker(args)).toBeNull();
307 expect(renderProviderTraitsMenuContent(args)).toBeNull();
308 });
309});