@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 401 lines 12.6 KB
Raw
1 import { describe, expect, it } from "vite-plus/test";
2 import {
3 ProviderDriverKind,
4 ProviderInstanceId,
5 type ProviderOptionDescriptor,
6 type ProviderOptionSelection,
7 type ServerProviderModel,
8 } from "@t3tools/contracts";
9 import { getComposerProviderState, getComposerReasoningStep } from "./composerProviderState";
10 import { readPromptUltrathink } from "./TraitsPicker";
11
12 const PROVIDER: ProviderDriverKind = ProviderDriverKind.make("codex");
13 const MODEL = "test-model";
14
15 function selectDescriptor(
16 id: string,
17 options: ReadonlyArray<{ id: string; label: string; isDefault?: boolean }>,
18 promptInjectedValues?: ReadonlyArray<string>,
19 ): Extract<ProviderOptionDescriptor, { type: "select" }> {
20 const defaultId = options.find((option) => option.isDefault)?.id;
21 return {
22 id,
23 label: id,
24 type: "select",
25 options: [...options],
26 ...(defaultId ? { currentValue: defaultId } : {}),
27 ...(promptInjectedValues && promptInjectedValues.length > 0
28 ? { promptInjectedValues: [...promptInjectedValues] }
29 : {}),
30 };
31 }
32
33 function booleanDescriptor(
34 id: string,
35 currentValue?: boolean,
36 ): Extract<ProviderOptionDescriptor, { type: "boolean" }> {
37 return {
38 id,
39 label: id,
40 type: "boolean",
41 ...(typeof currentValue === "boolean" ? { currentValue } : {}),
42 };
43 }
44
45 function modelWith(
46 descriptors: ReadonlyArray<ProviderOptionDescriptor>,
47 ): ReadonlyArray<ServerProviderModel> {
48 return [
49 { slug: MODEL, name: MODEL, isCustom: false, capabilities: { optionDescriptors: descriptors } },
50 ];
51 }
52
53 function selections(
54 ...entries: Array<[string, string | boolean]>
55 ): ReadonlyArray<ProviderOptionSelection> {
56 return entries.map(([id, value]) => ({ id, value }));
57 }
58
59 describe("getComposerProviderState", () => {
60 it("uses descriptor defaults for display without dispatching them as overrides", () => {
61 const state = getComposerProviderState({
62 provider: PROVIDER,
63 model: MODEL,
64 models: modelWith([
65 selectDescriptor("effort", [
66 { id: "low", label: "Low" },
67 { id: "high", label: "High", isDefault: true },
68 ]),
69 ]),
70 modelOptions: undefined,
71 });
72
73 expect(state).toEqual({
74 provider: PROVIDER,
75 promptEffort: "high",
76 modelOptionsForDispatch: undefined,
77 });
78 });
79
80 it("preserves selections that match defaults so deepMerge can overwrite prior state", () => {
81 const state = getComposerProviderState({
82 provider: PROVIDER,
83 model: MODEL,
84 models: modelWith([
85 selectDescriptor("effort", [{ id: "high", label: "High", isDefault: true }]),
86 booleanDescriptor("fastMode"),
87 ]),
88 modelOptions: selections(["effort", "high"], ["fastMode", false]),
89 });
90
91 expect(state.modelOptionsForDispatch).toEqual(
92 selections(["effort", "high"], ["fastMode", false]),
93 );
94 });
95
96 it("drops selections for descriptors the model does not declare", () => {
97 const state = getComposerProviderState({
98 provider: PROVIDER,
99 model: MODEL,
100 models: modelWith([booleanDescriptor("thinking")]),
101 modelOptions: selections(["effort", "max"], ["thinking", false]),
102 });
103
104 expect(state).toEqual({
105 provider: PROVIDER,
106 promptEffort: null,
107 modelOptionsForDispatch: selections(["thinking", false]),
108 });
109 });
110
111 it("derives promptEffort from the first select descriptor and preserves all others for dispatch", () => {
112 const state = getComposerProviderState({
113 provider: PROVIDER,
114 model: MODEL,
115 models: modelWith([
116 selectDescriptor("effort", [{ id: "high", label: "High", isDefault: true }]),
117 selectDescriptor("contextWindow", [
118 { id: "200k", label: "200k", isDefault: true },
119 { id: "1m", label: "1M" },
120 ]),
121 selectDescriptor("agent", [
122 { id: "build", label: "Build", isDefault: true },
123 { id: "plan", label: "Plan" },
124 ]),
125 ]),
126 modelOptions: selections(["agent", "plan"]),
127 includePlanAgent: true,
128 });
129
130 expect(state.promptEffort).toBe("high");
131 expect(state.modelOptionsForDispatch).toEqual(selections(["agent", "plan"]));
132 });
133
134 it("drops the plan agent from dispatch unless it is included", () => {
135 const state = getComposerProviderState({
136 provider: PROVIDER,
137 model: MODEL,
138 models: modelWith([
139 selectDescriptor("agent", [
140 { id: "build", label: "Build", isDefault: true },
141 { id: "plan", label: "Plan" },
142 ]),
143 ]),
144 modelOptions: selections(["agent", "plan"]),
145 });
146
147 expect(state.modelOptionsForDispatch).toEqual(selections(["agent", "build"]));
148 });
149
150 it.each([
151 {
152 driver: "opencode",
153 model: "opencode/kimi-k3",
154 catalogModel: "opencode/big-pickle",
155 options: selections(["variant", "max"], ["agent", "build"]),
156 },
157 {
158 driver: "pi",
159 model: "anthropic/claude-sonnet-4",
160 catalogModel: "default",
161 options: selections(["thinking", "high"]),
162 },
163 {
164 driver: "acpRegistry",
165 model: "gateway/chosen-model",
166 catalogModel: "default",
167 options: selections(["reasoning", "high"]),
168 },
169 ])(
170 "preserves explicit $driver options when the selected model is absent from the catalog",
171 ({ driver, model, catalogModel, options }) => {
172 const state = getComposerProviderState({
173 provider: ProviderDriverKind.make(driver),
174 model,
175 models: [{ slug: catalogModel, name: catalogModel, isCustom: false, capabilities: {} }],
176 modelOptions: options,
177 });
178
179 expect(state.modelOptionsForDispatch).toEqual(options);
180 },
181 );
182
183 it("validates options for a known model selected through a legacy alias", () => {
184 const state = getComposerProviderState({
185 provider: ProviderDriverKind.make("claudeAgent"),
186 model: "legacy-test-model",
187 models: [
188 {
189 slug: "test-model",
190 name: "Test Model",
191 aliases: ["legacy-test-model"],
192 isCustom: false,
193 capabilities: {
194 optionDescriptors: [
195 selectDescriptor("effort", [
196 { id: "low", label: "Low" },
197 { id: "high", label: "High", isDefault: true },
198 ]),
199 ],
200 },
201 },
202 ],
203 modelOptions: selections(["effort", "low"], ["unknown", "value"]),
204 });
205
206 expect(state.modelOptionsForDispatch).toEqual(selections(["effort", "low"]));
207 });
208
209 it("defaults fastMode to false when the provider reports true but the user has not selected it", () => {
210 const state = getComposerProviderState({
211 provider: ProviderDriverKind.make("cursor"),
212 model: MODEL,
213 models: modelWith([booleanDescriptor("fastMode", true)]),
214 modelOptions: undefined,
215 });
216
217 expect(state.modelOptionsForDispatch).toEqual(selections(["fastMode", false]));
218 });
219 });
220
221 describe("readPromptUltrathink", () => {
222 it("tells the Ultrathink prefix apart from the word in the body", () => {
223 expect(readPromptUltrathink("Fix the bug")).toBe("none");
224 expect(readPromptUltrathink("Ultrathink: Fix the bug")).toBe("prefix");
225 expect(readPromptUltrathink("ultrathink about the bug")).toBe("body");
226 expect(readPromptUltrathink("Ultrathink: ultrathink harder")).toBe("body");
227 });
228 });
229
230 describe("getComposerReasoningStep", () => {
231 const LEVELS = [
232 { id: "low", label: "Low" },
233 { id: "medium", label: "Medium", isDefault: true },
234 { id: "high", label: "High" },
235 ];
236 const INSTANCE = ProviderInstanceId.make("codex");
237 const CLAUDE = {
238 provider: ProviderDriverKind.make("claudeAgent"),
239 instanceId: ProviderInstanceId.make("claudeAgent"),
240 models: modelWith([
241 selectDescriptor(
242 "effort",
243 [
244 { id: "medium", label: "Medium" },
245 { id: "high", label: "High", isDefault: true },
246 { id: "ultrathink", label: "Ultrathink" },
247 ],
248 ["ultrathink"],
249 ),
250 ]),
251 };
252
253 function step(input: Partial<Parameters<typeof getComposerReasoningStep>[0]>) {
254 return getComposerReasoningStep({
255 provider: PROVIDER,
256 instanceId: INSTANCE,
257 model: MODEL,
258 models: modelWith([selectDescriptor("reasoningEffort", LEVELS)]),
259 modelOptions: undefined,
260 prompt: "Fix the bug",
261 cursor: 0,
262 direction: 1,
263 ...input,
264 });
265 }
266
267 it("walks the catalog order, wrapping at both ends and keeping the other traits", () => {
268 const models = modelWith([
269 selectDescriptor("contextWindow", [
270 { id: "200k", label: "200k", isDefault: true },
271 { id: "1m", label: "1M" },
272 ]),
273 selectDescriptor("reasoningEffort", LEVELS),
274 ]);
275 for (const [from, direction, to] of [
276 ["low", -1, "high"],
277 ["low", 1, "medium"],
278 ["high", 1, "low"],
279 ["high", -1, "medium"],
280 ] as const) {
281 expect(
282 step({
283 models,
284 modelOptions: selections(["contextWindow", "1m"], ["reasoningEffort", from]),
285 direction,
286 }),
287 `${from} by ${direction}`,
288 ).toStrictEqual({
289 prompt: "Fix the bug",
290 cursor: 0,
291 modelOptions: selections(["contextWindow", "1m"], ["reasoningEffort", to]),
292 });
293 }
294 });
295
296 it("steps Antigravity's thinking_level like other effort controls", () => {
297 expect(
298 step({
299 provider: ProviderDriverKind.make("antigravity"),
300 models: modelWith([selectDescriptor("thinking_level", LEVELS)]),
301 })?.modelOptions,
302 ).toStrictEqual(selections(["thinking_level", "high"]));
303 });
304
305 it("never steps into Ultrathink and strips a typed prefix with the caret on the same character", () => {
306 expect(step({ ...CLAUDE, modelOptions: selections(["effort", "high"]) })).toStrictEqual({
307 prompt: "Fix the bug",
308 cursor: 0,
309 modelOptions: selections(["effort", "medium"]),
310 });
311 const prompt = "Ultrathink:\n Fix the bug";
312 // A typed Ultrathink sits above the pickable levels, so up wraps and down lands on the top level.
313 for (const [direction, to] of [
314 [-1, "high"],
315 [1, "medium"],
316 ] as const) {
317 const result = step({
318 ...CLAUDE,
319 prompt,
320 cursor: prompt.indexOf("bug"),
321 modelOptions: selections(["effort", "medium"]),
322 direction,
323 });
324 expect(result, `by ${direction}`).toStrictEqual({
325 prompt: "Fix the bug",
326 cursor: 8,
327 modelOptions: selections(["effort", to]),
328 });
329 }
330 for (const [prompt, direction] of [
331 ["Please ultrathink harder", 1],
332 ["Ultrathink:\nPlease ultrathink harder", -1],
333 ] as const) {
334 expect(
335 step({ ...CLAUDE, prompt, modelOptions: selections(["effort", "high"]), direction }),
336 prompt,
337 ).toBeNull();
338 }
339 });
340
341 it("keeps a provider-default Fast mode off unless the user chose it", () => {
342 const cursor = {
343 provider: ProviderDriverKind.make("cursor"),
344 instanceId: ProviderInstanceId.make("cursor"),
345 models: modelWith([
346 selectDescriptor("reasoning_effort", LEVELS),
347 booleanDescriptor("fastMode", true),
348 ]),
349 };
350
351 expect(step({ ...cursor, modelOptions: undefined })?.modelOptions).toStrictEqual(
352 selections(["reasoning_effort", "high"], ["fastMode", false]),
353 );
354 expect(
355 step({ ...cursor, modelOptions: selections(["fastMode", true]) })?.modelOptions,
356 ).toStrictEqual(selections(["reasoning_effort", "high"], ["fastMode", true]));
357 });
358
359 it("steps from the provider-reported level for the same instance and model", () => {
360 const reported = {
361 instanceId: INSTANCE,
362 model: MODEL,
363 options: selections(["reasoningEffort", "high"]),
364 };
365 for (const { label, modelOptions, reportedModelSelection, to } of [
366 { label: "reported", modelOptions: undefined, reportedModelSelection: reported, to: "low" },
367 {
368 label: "other instance",
369 modelOptions: undefined,
370 reportedModelSelection: { ...reported, instanceId: ProviderInstanceId.make("codex-work") },
371 to: "high",
372 },
373 {
374 label: "other model",
375 modelOptions: undefined,
376 reportedModelSelection: { ...reported, model: "other-model" },
377 to: "high",
378 },
379 {
380 label: "explicit draft",
381 modelOptions: selections(["reasoningEffort", "low"]),
382 reportedModelSelection: reported,
383 to: "medium",
384 },
385 {
386 // OpenCode reports "default" when no variant was chosen.
387 label: "reported value outside the catalog",
388 modelOptions: undefined,
389 reportedModelSelection: {
390 ...reported,
391 options: selections(["reasoningEffort", "default"]),
392 },
393 to: "high",
394 },
395 ]) {
396 expect(step({ modelOptions, reportedModelSelection })?.modelOptions, label).toStrictEqual(
397 selections(["reasoningEffort", to]),
398 );
399 }
400 });
401 });