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

customModelEditor.logic.test.ts

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