Open History to see the last commit
1import {
2 ProviderDriverKind,
3 type ModelCapabilities,
4 type ProviderOptionSelection,
5} from "@t3tools/contracts";
6import { describe, expect, it } from "vite-plus/test";
7import {
8 getReasoningLevelDescriptor,
9 resolveReasoningLevel,
10 resolveReasoningLevelLabel,
11 withReasoningLevelChange,
12 getModelFamilyLabel,
13 getProviderGroupLabel,
14 groupByProvider,
15 findModelCapabilities,
16} from "./modelFamilyGrouping";
17
18function capsWithEffort(
19 effortId: string,
20 options: Array<{ id: string; label: string; isDefault?: boolean }>,
21 currentValue?: string,
22): ModelCapabilities {
23 return {
24 optionDescriptors: [
25 {
26 id: effortId,
27 label: "Reasoning effort",
28 type: "select",
29 options,
30 ...(currentValue ? { currentValue } : {}),
31 },
32 ],
33 };
34}
35
36describe("getReasoningLevelDescriptor", () => {
37 it("extracts the effort select descriptor by id", () => {
38 const caps = capsWithEffort("effort", [
39 { id: "low", label: "Low", isDefault: true },
40 { id: "high", label: "High" },
41 ]);
42 const descriptor = getReasoningLevelDescriptor({
43 caps,
44 selections: [{ id: "effort", value: "high" }],
45 });
46 expect(descriptor).not.toBeNull();
47 expect(descriptor!.descriptorId).toBe("effort");
48 expect(descriptor!.currentValue).toBe("high");
49 expect(descriptor!.options).toHaveLength(2);
50 });
51
52 it("falls back to the first select descriptor when no id matches", () => {
53 const caps: ModelCapabilities = {
54 optionDescriptors: [
55 {
56 id: "serviceTier",
57 label: "Service tier",
58 type: "select",
59 options: [{ id: "default", label: "Default", isDefault: true }],
60 },
61 ],
62 };
63 const descriptor = getReasoningLevelDescriptor({ caps });
64 expect(descriptor).not.toBeNull();
65 expect(descriptor!.descriptorId).toBe("serviceTier");
66 });
67
68 it("returns null when there are no select descriptors", () => {
69 const caps: ModelCapabilities = {
70 optionDescriptors: [
71 { id: "fastMode", label: "Fast mode", type: "boolean", currentValue: false },
72 ],
73 };
74 expect(getReasoningLevelDescriptor({ caps })).toBeNull();
75 });
76
77 it("resolves the default value when no selection is stored", () => {
78 const caps = capsWithEffort("effort", [
79 { id: "low", label: "Low", isDefault: true },
80 { id: "high", label: "High" },
81 ]);
82 const descriptor = getReasoningLevelDescriptor({ caps, selections: null });
83 expect(descriptor!.currentValue).toBe("low");
84 });
85});
86
87describe("resolveReasoningLevel", () => {
88 it("returns the stored effort value", () => {
89 const caps = capsWithEffort("effort", [
90 { id: "low", label: "Low" },
91 { id: "high", label: "High" },
92 ]);
93 expect(resolveReasoningLevel({ caps, selections: [{ id: "effort", value: "high" }] })).toBe(
94 "high",
95 );
96 });
97
98 it("returns null when the model has no reasoning descriptor", () => {
99 const caps: ModelCapabilities = { optionDescriptors: [] };
100 expect(resolveReasoningLevel({ caps })).toBeNull();
101 });
102});
103
104describe("resolveReasoningLevelLabel", () => {
105 it("returns the human-readable label for the current value", () => {
106 const caps = capsWithEffort("effort", [
107 { id: "low", label: "Low" },
108 { id: "high", label: "High" },
109 ]);
110 expect(
111 resolveReasoningLevelLabel({ caps, selections: [{ id: "effort", value: "high" }] }),
112 ).toBe("High");
113 });
114
115 it("returns null when no reasoning descriptor exists", () => {
116 const caps: ModelCapabilities = { optionDescriptors: [] };
117 expect(resolveReasoningLevelLabel({ caps })).toBeNull();
118 });
119});
120
121describe("withReasoningLevelChange", () => {
122 it("replaces the reasoning value and preserves other options", () => {
123 const caps = capsWithEffort("effort", [
124 { id: "low", label: "Low" },
125 { id: "high", label: "High" },
126 ]);
127 const selections: ProviderOptionSelection[] = [
128 { id: "effort", value: "low" },
129 { id: "fastMode", value: true },
130 ];
131 const next = withReasoningLevelChange({ caps, selections, nextValue: "high" });
132 expect(next).toEqual([
133 { id: "fastMode", value: true },
134 { id: "effort", value: "high" },
135 ]);
136 });
137
138 it("adds the reasoning option when no prior selection exists", () => {
139 const caps = capsWithEffort("effort", [
140 { id: "low", label: "Low" },
141 { id: "high", label: "High" },
142 ]);
143 const next = withReasoningLevelChange({ caps, selections: [], nextValue: "high" });
144 expect(next).toEqual([{ id: "effort", value: "high" }]);
145 });
146
147 it("returns undefined when the model has no reasoning descriptor", () => {
148 const caps: ModelCapabilities = { optionDescriptors: [] };
149 expect(withReasoningLevelChange({ caps, selections: [], nextValue: "high" })).toBeUndefined();
150 });
151});
152
153describe("getModelFamilyLabel", () => {
154 it("strips trailing reasoning qualifiers from the slug", () => {
155 expect(
156 getModelFamilyLabel("claude-sonnet-5-high", ProviderDriverKind.make("claudeAgent")),
157 ).toBe("Claude Sonnet 5");
158 });
159
160 it("uses the fallback name when the slug has no qualifier", () => {
161 expect(
162 getModelFamilyLabel(
163 "claude-sonnet-5",
164 ProviderDriverKind.make("claudeAgent"),
165 "Claude Sonnet 5",
166 ),
167 ).toBe("Claude Sonnet 5");
168 });
169
170 it("humanizes a bare slug", () => {
171 expect(getModelFamilyLabel("gpt-5-codex", ProviderDriverKind.make("codex"))).toBe(
172 "Gpt 5 Codex",
173 );
174 });
175});
176
177describe("getProviderGroupLabel", () => {
178 it("uses the canonical PROVIDER_DISPLAY_NAMES entry", () => {
179 expect(getProviderGroupLabel(ProviderDriverKind.make("claudeAgent"))).toBe("Claude");
180 expect(getProviderGroupLabel(ProviderDriverKind.make("devin"))).toBe("Devin");
181 });
182
183 it("falls back to a humanized slug for unknown kinds", () => {
184 expect(getProviderGroupLabel(ProviderDriverKind.make("claudeAgent"))).not.toBe("claudeAgent");
185 });
186});
187
188describe("groupByProvider", () => {
189 const codex = ProviderDriverKind.make("codex");
190 const claude = ProviderDriverKind.make("claudeAgent");
191 const grok = ProviderDriverKind.make("grok");
192
193 it("groups items by driver kind preserving first-appearance order", () => {
194 const items = [
195 { driverKind: codex, slug: "gpt-5" },
196 { driverKind: claude, slug: "sonnet" },
197 { driverKind: codex, slug: "gpt-5.4" },
198 { driverKind: grok, slug: "grok-build" },
199 ];
200 const groups = groupByProvider(items);
201 expect(groups).toHaveLength(3);
202 expect(groups[0]!.driverKind).toBe("codex");
203 expect(groups[0]!.items.map((i) => i.slug)).toEqual(["gpt-5", "gpt-5.4"]);
204 expect(groups[1]!.driverKind).toBe("claudeAgent");
205 expect(groups[1]!.items).toHaveLength(1);
206 expect(groups[2]!.driverKind).toBe("grok");
207 });
208
209 it("returns a single group when all items share a provider", () => {
210 const items = [
211 { driverKind: codex, slug: "a" },
212 { driverKind: codex, slug: "b" },
213 ];
214 const groups = groupByProvider(items);
215 expect(groups).toHaveLength(1);
216 expect(groups[0]!.items).toHaveLength(2);
217 });
218
219 it("returns an empty array for no items", () => {
220 expect(groupByProvider([])).toEqual([]);
221 });
222});
223
224describe("findModelCapabilities", () => {
225 it("returns the matching model capabilities", () => {
226 const caps = capsWithEffort("effort", [{ id: "low", label: "Low" }]);
227 const models = [
228 { slug: "model-a", name: "Model A", isCustom: false, capabilities: caps },
229 {
230 slug: "model-b",
231 name: "Model B",
232 isCustom: false,
233 capabilities: { optionDescriptors: [] },
234 },
235 ];
236 expect(findModelCapabilities(models, "model-a")).toBe(caps);
237 });
238
239 it("returns empty capabilities when the model is not found", () => {
240 const result = findModelCapabilities([], "missing");
241 expect(result.optionDescriptors).toEqual([]);
242 });
243});