Open History to see the last commit
1import { describe, expect, it } from "vitest";
2import {
3 DEFAULT_MODEL,
4 ProviderDriverKind,
5 ProviderInstanceId,
6 type ModelCapabilities,
7} from "@t3tools/contracts";
8
9import {
10 applyClaudePromptEffortPrefix,
11 buildProviderOptionSelectionsFromDescriptors,
12 createModelCapabilities,
13 createModelSelection,
14 getModelSelectionBooleanOptionValue,
15 getModelSelectionStringOptionValue,
16 getProviderOptionDescriptors,
17 getProviderOptionBooleanSelectionValue,
18 getProviderOptionStringSelectionValue,
19 isClaudeUltrathinkPrompt,
20 normalizeModelSlug,
21 resolveModelSlugForProvider,
22 resolveSelectableModel,
23 trimOrNull,
24} from "./model.ts";
25
26const codexCaps: ModelCapabilities = createModelCapabilities({
27 optionDescriptors: [
28 {
29 id: "reasoningEffort",
30 label: "Reasoning",
31 type: "select",
32 options: [
33 { id: "xhigh", label: "Extra High" },
34 { id: "high", label: "High", isDefault: true },
35 ],
36 currentValue: "high",
37 },
38 {
39 id: "fastMode",
40 label: "Fast Mode",
41 type: "boolean",
42 },
43 ],
44});
45
46const claudeCaps: ModelCapabilities = createModelCapabilities({
47 optionDescriptors: [
48 {
49 id: "effort",
50 label: "Reasoning",
51 type: "select",
52 options: [
53 { id: "medium", label: "Medium" },
54 { id: "high", label: "High", isDefault: true },
55 { id: "ultrathink", label: "Ultrathink" },
56 ],
57 currentValue: "high",
58 promptInjectedValues: ["ultrathink"],
59 },
60 {
61 id: "contextWindow",
62 label: "Context Window",
63 type: "select",
64 options: [
65 { id: "200k", label: "200k" },
66 { id: "1m", label: "1M", isDefault: true },
67 ],
68 currentValue: "1m",
69 },
70 ],
71});
72
73describe("normalizeModelSlug", () => {
74 it("maps known aliases to canonical slugs", () => {
75 const claude = ProviderDriverKind.make("claudeAgent");
76 expect(normalizeModelSlug("gpt-5-codex")).toBe("gpt-5.4");
77 expect(normalizeModelSlug("5.3")).toBe("gpt-5.3-codex");
78 expect(normalizeModelSlug("sonnet", claude)).toBe("claude-sonnet-4-6");
79 });
80
81 it("returns null for empty or missing values", () => {
82 expect(normalizeModelSlug("")).toBeNull();
83 expect(normalizeModelSlug(" ")).toBeNull();
84 expect(normalizeModelSlug(null)).toBeNull();
85 expect(normalizeModelSlug(undefined)).toBeNull();
86 });
87});
88
89describe("resolveModelSlugForProvider", () => {
90 it("returns defaults when the model is missing", () => {
91 expect(resolveModelSlugForProvider(ProviderDriverKind.make("codex"), undefined)).toBe(
92 DEFAULT_MODEL,
93 );
94 expect(resolveModelSlugForProvider(ProviderDriverKind.make("ollama"), undefined)).toBe(
95 DEFAULT_MODEL,
96 );
97 });
98
99 it("preserves normalized unknown models", () => {
100 expect(
101 resolveModelSlugForProvider(ProviderDriverKind.make("codex"), "custom/internal-model"),
102 ).toBe("custom/internal-model");
103 });
104});
105
106describe("resolveSelectableModel", () => {
107 it("resolves exact slugs, labels, and aliases", () => {
108 const options = [
109 { slug: "gpt-5.3-codex", name: "GPT-5.3 Codex" },
110 { slug: "claude-sonnet-4-6", name: "Claude Sonnet 4.6" },
111 ];
112 expect(resolveSelectableModel(ProviderDriverKind.make("codex"), "gpt-5.3-codex", options)).toBe(
113 "gpt-5.3-codex",
114 );
115 expect(resolveSelectableModel(ProviderDriverKind.make("codex"), "gpt-5.3 codex", options)).toBe(
116 "gpt-5.3-codex",
117 );
118 expect(resolveSelectableModel(ProviderDriverKind.make("claudeAgent"), "sonnet", options)).toBe(
119 "claude-sonnet-4-6",
120 );
121 });
122});
123
124describe("misc helpers", () => {
125 it("detects ultrathink prompts", () => {
126 expect(isClaudeUltrathinkPrompt("Please ultrathink about this")).toBe(true);
127 expect(isClaudeUltrathinkPrompt("Ultrathink:\nInvestigate")).toBe(true);
128 expect(isClaudeUltrathinkPrompt("Investigate")).toBe(false);
129 });
130
131 it("prefixes ultrathink prompts once", () => {
132 expect(applyClaudePromptEffortPrefix("Investigate", "ultrathink")).toBe(
133 "Ultrathink:\nInvestigate",
134 );
135 expect(applyClaudePromptEffortPrefix("Ultrathink:\nInvestigate", "ultrathink")).toBe(
136 "Ultrathink:\nInvestigate",
137 );
138 });
139
140 it("trims strings to null", () => {
141 expect(trimOrNull(" hi ")).toBe("hi");
142 expect(trimOrNull(" ")).toBeNull();
143 });
144});
145
146describe("descriptor helpers", () => {
147 it("applies selection values to capability descriptors", () => {
148 expect(
149 getProviderOptionDescriptors({
150 caps: claudeCaps,
151 selections: [
152 { id: "effort", value: "medium" },
153 { id: "contextWindow", value: "200k" },
154 ],
155 }),
156 ).toEqual([
157 {
158 id: "effort",
159 label: "Reasoning",
160 type: "select",
161 options: [
162 { id: "medium", label: "Medium" },
163 { id: "high", label: "High", isDefault: true },
164 { id: "ultrathink", label: "Ultrathink" },
165 ],
166 currentValue: "medium",
167 promptInjectedValues: ["ultrathink"],
168 },
169 {
170 id: "contextWindow",
171 label: "Context Window",
172 type: "select",
173 options: [
174 { id: "200k", label: "200k" },
175 { id: "1m", label: "1M", isDefault: true },
176 ],
177 currentValue: "200k",
178 },
179 ]);
180 });
181
182 it("builds wire-format option selections from descriptors", () => {
183 const descriptors = getProviderOptionDescriptors({
184 caps: codexCaps,
185 selections: [
186 { id: "reasoningEffort", value: "high" },
187 { id: "fastMode", value: true },
188 ],
189 });
190
191 expect(buildProviderOptionSelectionsFromDescriptors(descriptors)).toEqual([
192 { id: "reasoningEffort", value: "high" },
193 { id: "fastMode", value: true },
194 ]);
195 });
196
197 it("stores option selection arrays in model selections", () => {
198 expect(
199 createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.4", [
200 { id: "reasoningEffort", value: "high" },
201 { id: "fastMode", value: true },
202 ]),
203 ).toEqual({
204 instanceId: "codex",
205 model: "gpt-5.4",
206 options: [
207 { id: "reasoningEffort", value: "high" },
208 { id: "fastMode", value: true },
209 ],
210 });
211 });
212
213 it("reads typed option selection values", () => {
214 const selection = createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.4", [
215 { id: "reasoningEffort", value: "high" },
216 { id: "fastMode", value: true },
217 ]);
218
219 expect(getProviderOptionStringSelectionValue(selection.options, "reasoningEffort")).toBe(
220 "high",
221 );
222 expect(getProviderOptionStringSelectionValue(selection.options, "fastMode")).toBeUndefined();
223 expect(getProviderOptionBooleanSelectionValue(selection.options, "fastMode")).toBe(true);
224 expect(
225 getProviderOptionBooleanSelectionValue(selection.options, "reasoningEffort"),
226 ).toBeUndefined();
227 expect(getModelSelectionStringOptionValue(selection, "reasoningEffort")).toBe("high");
228 expect(getModelSelectionBooleanOptionValue(selection, "fastMode")).toBe(true);
229 });
230});