Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import { ProviderInstanceId, type ModelCapabilities } from "@t3tools/contracts";
3
4import {
5 applyClaudePromptEffortPrefix,
6 buildExplicitProviderOptionSelectionsFromDescriptors,
7 buildProviderOptionSelectionsFromDescriptors,
8 createModelCapabilities,
9 createModelSelection,
10 getProviderOptionCurrentLabel,
11 getProviderOptionDescriptors,
12 readCustomModelEntries,
13 modelSelectionsEqual,
14} from "./model.ts";
15
16const codexCaps: ModelCapabilities = createModelCapabilities({
17 optionDescriptors: [
18 {
19 id: "reasoningEffort",
20 label: "Reasoning",
21 type: "select",
22 options: [
23 { id: "xhigh", label: "Extra High" },
24 { id: "high", label: "High", isDefault: true },
25 ],
26 currentValue: "high",
27 },
28 {
29 id: "fastMode",
30 label: "Fast Mode",
31 type: "boolean",
32 },
33 ],
34});
35
36describe("descriptor helpers", () => {
37 it("builds dispatch options only from explicit selections", () => {
38 const descriptors = getProviderOptionDescriptors({
39 caps: codexCaps,
40 selections: [{ id: "fastMode", value: true }],
41 });
42
43 expect(buildExplicitProviderOptionSelectionsFromDescriptors(descriptors, undefined)).toBe(
44 undefined,
45 );
46 expect(
47 buildExplicitProviderOptionSelectionsFromDescriptors(descriptors, [
48 { id: "fastMode", value: true },
49 ]),
50 ).toEqual([{ id: "fastMode", value: true }]);
51 });
52
53 it("dispatches an old Ultracode effort as Extra High with the Ultracode toggle", () => {
54 const legacy = [{ id: "effort", value: "ultracode" }];
55 const descriptors = getProviderOptionDescriptors({
56 caps: createModelCapabilities({
57 optionDescriptors: [
58 {
59 id: "effort",
60 label: "Reasoning",
61 type: "select",
62 options: [
63 { id: "high", label: "High", isDefault: true },
64 { id: "xhigh", label: "Extra High" },
65 ],
66 },
67 { id: "ultracode", label: "Ultracode", type: "boolean" },
68 ],
69 }),
70 selections: legacy,
71 });
72
73 expect(buildExplicitProviderOptionSelectionsFromDescriptors(descriptors, legacy)).toEqual([
74 { id: "effort", value: "xhigh" },
75 { id: "ultracode", value: true },
76 ]);
77 });
78
79 it("compares complete model selections independent of option ordering", () => {
80 const left = createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.4", [
81 { id: "reasoningEffort", value: "high" },
82 { id: "fastMode", value: true },
83 ]);
84 const reordered = createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.4", [
85 { id: "fastMode", value: true },
86 { id: "reasoningEffort", value: "high" },
87 ]);
88
89 expect(modelSelectionsEqual(left, reordered)).toBe(true);
90 expect(
91 modelSelectionsEqual(left, {
92 ...reordered,
93 options: [
94 { id: "fastMode", value: true },
95 { id: "reasoningEffort", value: "medium" },
96 ],
97 }),
98 ).toBe(false);
99 expect(modelSelectionsEqual(left, { ...reordered, model: "gpt-5.5" })).toBe(false);
100 });
101});
102
103describe("applyClaudePromptEffortPrefix", () => {
104 it("keeps slash commands intact when ultrathink is selected", () => {
105 expect(applyClaudePromptEffortPrefix("/compact", "ultrathink")).toBe("/compact");
106 expect(applyClaudePromptEffortPrefix(" /compact keep recent errors ", "ultrathink")).toBe(
107 "/compact keep recent errors",
108 );
109 expect(applyClaudePromptEffortPrefix(" /review src/model.ts ", "ultrathink")).toBe(
110 "/review src/model.ts",
111 );
112 expect(applyClaudePromptEffortPrefix("/security-review", "ultrathink")).toBe(
113 "/security-review",
114 );
115 expect(applyClaudePromptEffortPrefix("/plugin:skill run", "ultrathink")).toBe(
116 "/plugin:skill run",
117 );
118 expect(applyClaudePromptEffortPrefix("/deploy.prod to staging", "ultrathink")).toBe(
119 "/deploy.prod to staging",
120 );
121 });
122
123 it("still adds the ultrathink prefix to ordinary prompts", () => {
124 expect(applyClaudePromptEffortPrefix("Investigate this failure", "ultrathink")).toBe(
125 "Ultrathink:\nInvestigate this failure",
126 );
127 expect(applyClaudePromptEffortPrefix("/home/theo/app.ts crashed on load", "ultrathink")).toBe(
128 "Ultrathink:\n/home/theo/app.ts crashed on load",
129 );
130 });
131});
132
133describe("readCustomModelEntries", () => {
134 const capabilities: ModelCapabilities = {
135 optionDescriptors: [
136 {
137 id: "effort",
138 label: "Reasoning",
139 type: "select",
140 options: [{ id: "high", label: "High", isDefault: true }],
141 currentValue: "high",
142 },
143 ],
144 };
145
146 it("resolves bare slugs and entries, trimming and deduplicating on slug", () => {
147 expect(
148 readCustomModelEntries([
149 " bare ",
150 { slug: "named", name: " Named ", capabilities },
151 "bare",
152 { slug: "named", name: "Second" },
153 "",
154 { name: "no slug" },
155 42,
156 ]),
157 ).toEqual([
158 { slug: "bare", name: "bare", capabilities: null },
159 { slug: "named", name: "Named", capabilities },
160 ]);
161 });
162
163 it("drops unparseable capabilities but keeps the entry", () => {
164 expect(
165 readCustomModelEntries([{ slug: "x", capabilities: { optionDescriptors: "nope" } }]),
166 ).toEqual([{ slug: "x", name: "x", capabilities: null }]);
167 expect(readCustomModelEntries("not a list")).toEqual([]);
168 });
169});
170
171describe("provider-reported option display", () => {
172 const selection = createModelSelection(ProviderInstanceId.make("opencode"), "ling");
173 const reported = { ...selection, options: [{ id: "variant", value: "default" }] };
174 const descriptor = {
175 id: "variant",
176 label: "Reasoning",
177 type: "select" as const,
178 options: [
179 { id: "none", label: "None" },
180 { id: "thinking", label: "Thinking" },
181 ],
182 };
183
184 it("shows explicit reports without adding a choice or a dispatch option", () => {
185 expect(getProviderOptionCurrentLabel(descriptor, selection, reported)).toBe("Default");
186 expect(
187 getProviderOptionCurrentLabel(descriptor, selection, {
188 ...reported,
189 options: [{ id: "variant", value: "thinking" }],
190 }),
191 ).toBe("Thinking");
192 expect(
193 getProviderOptionCurrentLabel(
194 { ...descriptor, currentValue: "none" },
195 { ...selection, options: [{ id: "variant", value: "none" }] },
196 reported,
197 ),
198 ).toBe("None");
199 expect(descriptor.options.map((option) => option.id)).toEqual(["none", "thinking"]);
200 expect(buildProviderOptionSelectionsFromDescriptors([descriptor])).toBeUndefined();
201 expect(getProviderOptionCurrentLabel(descriptor, selection)).toBe("Unknown");
202 const effortDescriptor = { ...descriptor, id: "effort", currentValue: "default" };
203 expect(getProviderOptionCurrentLabel(effortDescriptor, selection)).toBeUndefined();
204 expect(
205 getProviderOptionCurrentLabel(effortDescriptor, selection, {
206 ...reported,
207 model: "other",
208 options: [{ id: "effort", value: "default" }],
209 }),
210 ).toBeUndefined();
211 expect(
212 getProviderOptionCurrentLabel(effortDescriptor, selection, {
213 ...reported,
214 options: [{ id: "effort", value: "default" }],
215 }),
216 ).toBe("Default");
217 expect(
218 getProviderOptionCurrentLabel({ ...descriptor, currentValue: "thinking" }, selection),
219 ).toBe("Unknown");
220 });
221
222 it.each([
223 { ...selection, model: "other" },
224 { ...selection, instanceId: ProviderInstanceId.make("other") },
225 { ...selection, options: [{ id: "variant", value: "none" }] },
226 ])("ignores reports after changing the model, instance, or option: %j", (selected) => {
227 expect(getProviderOptionCurrentLabel(descriptor, selected, reported)).toBe("Unknown");
228 });
229});