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

customModelEditor.logic.test.ts

185 lines6.0 KB

1import { describe, expect, it } from "vite-plus/test";
2import { ProviderDriverKind, type ModelCapabilities } from "@t3tools/contracts";
3​
4import {
5 DESCRIPTOR_PRESETS_BY_KIND,
6 descriptorFromPreset,
7 resolveDescriptorPreset,
8 definitionFromDraft,
9 descriptorsFromCapabilities,
10 draftFromDefinition,
11 validateDraft,
12 type CustomModelDraft,
13} from "./customModelEditor.logic";
14​
15const draft = (overrides: Partial<CustomModelDraft>): CustomModelDraft => ({
16 slug: "my-model",
17 name: "",
18 descriptors: [],
19 ...overrides,
20});
21​
22describe("customModelEditor.logic", () => {
23 it("round-trips a definition through the draft, marking the current value as default", () => {
24 const definition = definitionFromDraft(
25 draft({
26 name: " My Model ",
27 descriptors: [
28 {
29 key: "a",
30 type: "select",
31 id: "reasoningEffort",
32 label: "Reasoning",
33 choices: [
34 { key: "a1", id: "low", label: "Low", isDefault: false },
35 { key: "a2", id: "high", label: "", isDefault: true },
36 ],
37 },
38 { key: "b", type: "boolean", id: "fastMode", label: "Fast Mode", choices: [] },
39 ],
40 }),
41 );
42​
43 expect(definition).toEqual({
44 slug: "my-model",
45 name: "My Model",
46 capabilities: {
47 optionDescriptors: [
48 {
49 id: "reasoningEffort",
50 label: "Reasoning",
51 type: "select",
52 options: [
53 { id: "low", label: "Low" },
54 { id: "high", label: "high", isDefault: true },
55 ],
56 currentValue: "high",
57 },
58 { id: "fastMode", label: "Fast Mode", type: "boolean" },
59 ],
60 },
61 });
62​
63 const reopened = draftFromDefinition(definition);
64 expect(reopened.name).toBe("My Model");
65 expect(reopened.descriptors.map((descriptor) => descriptor.id)).toEqual([
66 "reasoningEffort",
67 "fastMode",
68 ]);
69 expect(reopened.descriptors[0]!.choices.map((choice) => choice.isDefault)).toEqual([
70 false,
71 true,
72 ]);
73 });
74​
75 it("preserves the current choice when it differs from the built-in default", () => {
76 const descriptors = descriptorsFromCapabilities(
77 {
78 optionDescriptors: [
79 {
80 id: "effort",
81 label: "Reasoning",
82 type: "select",
83 currentValue: "high",
84 options: [
85 { id: "low", label: "Low", isDefault: true },
86 { id: "high", label: "High" },
87 ],
88 },
89 ],
90 },
91 ProviderDriverKind.make("claudeAgent"),
92 );
93 expect(descriptors[0]!.choices.map((choice) => choice.isDefault)).toEqual([false, true]);
94 expect(
95 definitionFromDraft(draft({ descriptors })).capabilities?.optionDescriptors?.[0],
96 ).toMatchObject({ currentValue: "high" });
97 });
98​
99 it("drops prompt-injected choices when copying a built-in's descriptors", () => {
100 const [copied] = descriptorsFromCapabilities(
101 {
102 optionDescriptors: [
103 {
104 id: "effort",
105 label: "Reasoning",
106 type: "select",
107 options: [
108 { id: "high", label: "High", isDefault: true },
109 { id: "ultrathink", label: "Ultrathink" },
110 ],
111 promptInjectedValues: ["ultrathink"],
112 },
113 ],
114 },
115 ProviderDriverKind.make("claudeAgent"),
116 );
117 expect(copied!.choices.map((choice) => choice.id)).toEqual(["high"]);
118 });
119​
120 it("excludes Claude context choices from presets and copies without changing other providers or authored entries", () => {
121 const capabilities: ModelCapabilities = {
122 optionDescriptors: [
123 {
124 id: "contextWindow",
125 label: "Context",
126 type: "select",
127 options: [{ id: "1m", label: "1M", isDefault: true }],
128 },
129 { id: "thinking", label: "Thinking", type: "boolean", currentValue: true },
130 ],
131 };
132 const claude = ProviderDriverKind.make("claudeAgent");
133 const copied = definitionFromDraft(
134 draft({ descriptors: descriptorsFromCapabilities(capabilities, claude) }),
135 );
136 expect(copied.capabilities?.optionDescriptors).toEqual([capabilities.optionDescriptors![1]]);
137 const presets = definitionFromDraft(
138 draft({
139 descriptors: (DESCRIPTOR_PRESETS_BY_KIND[claude] ?? []).map((preset) =>
140 descriptorFromPreset(resolveDescriptorPreset(preset)),
141 ),
142 }),
143 );
144 expect(
145 presets.capabilities?.optionDescriptors?.some((option) => option.id === "contextWindow"),
146 ).toBe(false);
147 const cursorCopy = descriptorsFromCapabilities(capabilities, ProviderDriverKind.make("cursor"));
148 expect(cursorCopy.map((option) => option.id)).toEqual(["contextWindow", "thinking"]);
149 const authored = { slug: "custom", name: "Custom", capabilities };
150 expect(
151 definitionFromDraft(draftFromDefinition(authored)).capabilities?.optionDescriptors?.[0],
152 ).toMatchObject(capabilities.optionDescriptors![0]!);
153 });
154​
155 it("rejects duplicate ids, blank ids, and selects without choices", () => {
156 const select = (id: string, choices: Array<{ id: string }>) => ({
157 key: id,
158 type: "select" as const,
159 id,
160 label: "Label",
161 choices: choices.map((choice) => ({
162 key: choice.id,
163 label: "",
164 isDefault: false,
165 ...choice,
166 })),
167 });
168​
169 expect(validateDraft(draft({ descriptors: [select("", [{ id: "a" }])] }))).toBe(
170 "Option 1 needs an id.",
171 );
172 expect(
173 validateDraft(
174 draft({ descriptors: [select("effort", [{ id: "a" }]), select("effort", [{ id: "b" }])] }),
175 ),
176 ).toBe('Option 2: id "effort" is used twice.');
177 expect(validateDraft(draft({ descriptors: [select("effort", [])] }))).toBe(
178 "Option 1 needs at least one choice.",
179 );
180 expect(
181 validateDraft(draft({ descriptors: [select("effort", [{ id: "a" }, { id: "a" }])] })),
182 ).toBe('Option 1: choice "a" is used twice.');
183 expect(validateDraft(draft({ descriptors: [select("effort", [{ id: "a" }])] }))).toBeNull();
184 });
185});