The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | import { ProviderInstanceId, type ModelCapabilities } from "@t3tools/contracts"; |
| 3 | |
| 4 | import { |
| 5 | applyClaudePromptEffortPrefix, |
| 6 | buildExplicitProviderOptionSelectionsFromDescriptors, |
| 7 | buildProviderOptionSelectionsFromDescriptors, |
| 8 | createModelCapabilities, |
| 9 | createModelSelection, |
| 10 | getProviderOptionCurrentLabel, |
| 11 | getProviderOptionDescriptors, |
| 12 | readCustomModelEntries, |
| 13 | modelSelectionsEqual, |
| 14 | pickModelSelection, |
| 15 | withImplicitFastModeDefault, |
| 16 | } from "./model.ts"; |
| 17 | |
| 18 | const codexCaps: ModelCapabilities = createModelCapabilities({ |
| 19 | optionDescriptors: [ |
| 20 | { |
| 21 | id: "reasoningEffort", |
| 22 | label: "Reasoning", |
| 23 | type: "select", |
| 24 | options: [ |
| 25 | { id: "xhigh", label: "Extra High" }, |
| 26 | { id: "high", label: "High", isDefault: true }, |
| 27 | ], |
| 28 | currentValue: "high", |
| 29 | }, |
| 30 | { |
| 31 | id: "fastMode", |
| 32 | label: "Fast Mode", |
| 33 | type: "boolean", |
| 34 | }, |
| 35 | ], |
| 36 | }); |
| 37 | |
| 38 | describe("descriptor helpers", () => { |
| 39 | it("builds dispatch options only from explicit selections", () => { |
| 40 | const descriptors = getProviderOptionDescriptors({ |
| 41 | caps: codexCaps, |
| 42 | selections: [{ id: "fastMode", value: true }], |
| 43 | }); |
| 44 | |
| 45 | expect(buildExplicitProviderOptionSelectionsFromDescriptors(descriptors, undefined)).toBe( |
| 46 | undefined, |
| 47 | ); |
| 48 | expect( |
| 49 | buildExplicitProviderOptionSelectionsFromDescriptors(descriptors, [ |
| 50 | { id: "fastMode", value: true }, |
| 51 | ]), |
| 52 | ).toEqual([{ id: "fastMode", value: true }]); |
| 53 | }); |
| 54 | |
| 55 | it("dispatches an old Ultracode effort as Extra High with the Ultracode toggle", () => { |
| 56 | const legacy = [{ id: "effort", value: "ultracode" }]; |
| 57 | const descriptors = getProviderOptionDescriptors({ |
| 58 | caps: createModelCapabilities({ |
| 59 | optionDescriptors: [ |
| 60 | { |
| 61 | id: "effort", |
| 62 | label: "Reasoning", |
| 63 | type: "select", |
| 64 | options: [ |
| 65 | { id: "high", label: "High", isDefault: true }, |
| 66 | { id: "xhigh", label: "Extra High" }, |
| 67 | ], |
| 68 | }, |
| 69 | { id: "ultracode", label: "Ultracode", type: "boolean" }, |
| 70 | ], |
| 71 | }), |
| 72 | selections: legacy, |
| 73 | }); |
| 74 | |
| 75 | expect(buildExplicitProviderOptionSelectionsFromDescriptors(descriptors, legacy)).toEqual([ |
| 76 | { id: "effort", value: "xhigh" }, |
| 77 | { id: "ultracode", value: true }, |
| 78 | ]); |
| 79 | }); |
| 80 | |
| 81 | it("compares complete model selections independent of option ordering", () => { |
| 82 | const left = createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.4", [ |
| 83 | { id: "reasoningEffort", value: "high" }, |
| 84 | { id: "fastMode", value: true }, |
| 85 | ]); |
| 86 | const reordered = createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.4", [ |
| 87 | { id: "fastMode", value: true }, |
| 88 | { id: "reasoningEffort", value: "high" }, |
| 89 | ]); |
| 90 | |
| 91 | expect(modelSelectionsEqual(left, reordered)).toBe(true); |
| 92 | expect( |
| 93 | modelSelectionsEqual(left, { |
| 94 | ...reordered, |
| 95 | options: [ |
| 96 | { id: "fastMode", value: true }, |
| 97 | { id: "reasoningEffort", value: "medium" }, |
| 98 | ], |
| 99 | }), |
| 100 | ).toBe(false); |
| 101 | expect(modelSelectionsEqual(left, { ...reordered, model: "gpt-5.5" })).toBe(false); |
| 102 | }); |
| 103 | |
| 104 | it("keeps the options when the current model is picked again", () => { |
| 105 | const codex = ProviderInstanceId.make("codex"); |
| 106 | const current = createModelSelection(codex, "gpt-5.4", [ |
| 107 | { id: "reasoningEffort", value: "high" }, |
| 108 | ]); |
| 109 | |
| 110 | expect(pickModelSelection(current, codex, "gpt-5.4")).toBe(current); |
| 111 | expect(pickModelSelection(current, codex, "gpt-5.5")).toEqual({ |
| 112 | instanceId: "codex", |
| 113 | model: "gpt-5.5", |
| 114 | }); |
| 115 | expect(pickModelSelection(current, ProviderInstanceId.make("codex_work"), "gpt-5.4")).toEqual({ |
| 116 | instanceId: "codex_work", |
| 117 | model: "gpt-5.4", |
| 118 | }); |
| 119 | expect(pickModelSelection(null, codex, "gpt-5.4")).toEqual({ |
| 120 | instanceId: "codex", |
| 121 | model: "gpt-5.4", |
| 122 | }); |
| 123 | }); |
| 124 | }); |
| 125 | |
| 126 | describe("applyClaudePromptEffortPrefix", () => { |
| 127 | it("keeps slash commands intact when ultrathink is selected", () => { |
| 128 | expect(applyClaudePromptEffortPrefix("/compact", "ultrathink")).toBe("/compact"); |
| 129 | expect(applyClaudePromptEffortPrefix(" /compact keep recent errors ", "ultrathink")).toBe( |
| 130 | "/compact keep recent errors", |
| 131 | ); |
| 132 | expect(applyClaudePromptEffortPrefix(" /review src/model.ts ", "ultrathink")).toBe( |
| 133 | "/review src/model.ts", |
| 134 | ); |
| 135 | expect(applyClaudePromptEffortPrefix("/security-review", "ultrathink")).toBe( |
| 136 | "/security-review", |
| 137 | ); |
| 138 | expect(applyClaudePromptEffortPrefix("/plugin:skill run", "ultrathink")).toBe( |
| 139 | "/plugin:skill run", |
| 140 | ); |
| 141 | expect(applyClaudePromptEffortPrefix("/deploy.prod to staging", "ultrathink")).toBe( |
| 142 | "/deploy.prod to staging", |
| 143 | ); |
| 144 | }); |
| 145 | |
| 146 | it("still adds the ultrathink prefix to ordinary prompts", () => { |
| 147 | expect(applyClaudePromptEffortPrefix("Investigate this failure", "ultrathink")).toBe( |
| 148 | "Ultrathink:\nInvestigate this failure", |
| 149 | ); |
| 150 | expect(applyClaudePromptEffortPrefix("/home/theo/app.ts crashed on load", "ultrathink")).toBe( |
| 151 | "Ultrathink:\n/home/theo/app.ts crashed on load", |
| 152 | ); |
| 153 | }); |
| 154 | }); |
| 155 | |
| 156 | describe("readCustomModelEntries", () => { |
| 157 | const capabilities: ModelCapabilities = { |
| 158 | optionDescriptors: [ |
| 159 | { |
| 160 | id: "effort", |
| 161 | label: "Reasoning", |
| 162 | type: "select", |
| 163 | options: [{ id: "high", label: "High", isDefault: true }], |
| 164 | currentValue: "high", |
| 165 | }, |
| 166 | ], |
| 167 | }; |
| 168 | |
| 169 | it("resolves bare slugs and entries, trimming and deduplicating on slug", () => { |
| 170 | expect( |
| 171 | readCustomModelEntries([ |
| 172 | " bare ", |
| 173 | { slug: "named", name: " Named ", capabilities }, |
| 174 | "bare", |
| 175 | { slug: "named", name: "Second" }, |
| 176 | "", |
| 177 | { name: "no slug" }, |
| 178 | 42, |
| 179 | ]), |
| 180 | ).toEqual([ |
| 181 | { slug: "bare", name: "bare", capabilities: null }, |
| 182 | { slug: "named", name: "Named", capabilities }, |
| 183 | ]); |
| 184 | }); |
| 185 | |
| 186 | it("drops unparseable capabilities but keeps the entry", () => { |
| 187 | expect( |
| 188 | readCustomModelEntries([{ slug: "x", capabilities: { optionDescriptors: "nope" } }]), |
| 189 | ).toEqual([{ slug: "x", name: "x", capabilities: null }]); |
| 190 | expect(readCustomModelEntries("not a list")).toEqual([]); |
| 191 | }); |
| 192 | }); |
| 193 | |
| 194 | describe("provider-reported option display", () => { |
| 195 | const selection = createModelSelection(ProviderInstanceId.make("opencode"), "ling"); |
| 196 | const reported = { ...selection, options: [{ id: "variant", value: "default" }] }; |
| 197 | const descriptor = { |
| 198 | id: "variant", |
| 199 | label: "Reasoning", |
| 200 | type: "select" as const, |
| 201 | options: [ |
| 202 | { id: "none", label: "None" }, |
| 203 | { id: "thinking", label: "Thinking" }, |
| 204 | ], |
| 205 | }; |
| 206 | |
| 207 | it("shows explicit reports without adding a choice or a dispatch option", () => { |
| 208 | expect(getProviderOptionCurrentLabel(descriptor, selection, reported)).toBe("Default"); |
| 209 | expect( |
| 210 | getProviderOptionCurrentLabel(descriptor, selection, { |
| 211 | ...reported, |
| 212 | options: [{ id: "variant", value: "thinking" }], |
| 213 | }), |
| 214 | ).toBe("Thinking"); |
| 215 | expect( |
| 216 | getProviderOptionCurrentLabel( |
| 217 | { ...descriptor, currentValue: "none" }, |
| 218 | { ...selection, options: [{ id: "variant", value: "none" }] }, |
| 219 | reported, |
| 220 | ), |
| 221 | ).toBe("None"); |
| 222 | expect(descriptor.options.map((option) => option.id)).toEqual(["none", "thinking"]); |
| 223 | expect(buildProviderOptionSelectionsFromDescriptors([descriptor])).toBeUndefined(); |
| 224 | expect(getProviderOptionCurrentLabel(descriptor, selection)).toBe("Unknown"); |
| 225 | const effortDescriptor = { ...descriptor, id: "effort", currentValue: "default" }; |
| 226 | expect(getProviderOptionCurrentLabel(effortDescriptor, selection)).toBeUndefined(); |
| 227 | expect( |
| 228 | getProviderOptionCurrentLabel(effortDescriptor, selection, { |
| 229 | ...reported, |
| 230 | model: "other", |
| 231 | options: [{ id: "effort", value: "default" }], |
| 232 | }), |
| 233 | ).toBeUndefined(); |
| 234 | expect( |
| 235 | getProviderOptionCurrentLabel(effortDescriptor, selection, { |
| 236 | ...reported, |
| 237 | options: [{ id: "effort", value: "default" }], |
| 238 | }), |
| 239 | ).toBe("Default"); |
| 240 | expect( |
| 241 | getProviderOptionCurrentLabel({ ...descriptor, currentValue: "thinking" }, selection), |
| 242 | ).toBe("Unknown"); |
| 243 | }); |
| 244 | |
| 245 | it.each([ |
| 246 | { ...selection, model: "other" }, |
| 247 | { ...selection, instanceId: ProviderInstanceId.make("other") }, |
| 248 | { ...selection, options: [{ id: "variant", value: "none" }] }, |
| 249 | ])("ignores reports after changing the model, instance, or option: %j", (selected) => { |
| 250 | expect(getProviderOptionCurrentLabel(descriptor, selected, reported)).toBe("Unknown"); |
| 251 | }); |
| 252 | }); |
| 253 | |
| 254 | describe("withImplicitFastModeDefault", () => { |
| 255 | // regression: #2981 a remembered Fast mode choice must survive the implicit default on new chats |
| 256 | it("injects fastMode false only when the model exposes fastMode and no selection exists", () => { |
| 257 | const caps = createModelCapabilities({ |
| 258 | optionDescriptors: [{ id: "fastMode", label: "Fast", type: "boolean", currentValue: true }], |
| 259 | }); |
| 260 | |
| 261 | expect(withImplicitFastModeDefault(caps, undefined)).toEqual([ |
| 262 | { id: "fastMode", value: false }, |
| 263 | ]); |
| 264 | expect(withImplicitFastModeDefault(caps, [{ id: "fastMode", value: true }])).toEqual([ |
| 265 | { id: "fastMode", value: true }, |
| 266 | ]); |
| 267 | }); |
| 268 | }); |