Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

model.test.ts

216 lines6.6 KB

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