|
1 | 1 | // npx jest src/shared/__tests__/api.test.ts |
2 | 2 |
|
3 | | -import { type ModelInfo, ProviderSettings, getModelMaxOutputTokens } from "../api" |
| 3 | +import { |
| 4 | + type ModelInfo, |
| 5 | + ProviderSettings, |
| 6 | + getModelMaxOutputTokens, |
| 7 | + shouldUseReasoningBudget, |
| 8 | + shouldUseReasoningEffort, |
| 9 | +} from "../api" |
4 | 10 |
|
5 | 11 | describe("getMaxTokensForModel", () => { |
6 | 12 | /** |
@@ -152,3 +158,187 @@ describe("getMaxTokensForModel", () => { |
152 | 158 | expect(getModelMaxOutputTokens({ model: modelInfoWithoutThinking, settings: undefined })).toBe(2048) |
153 | 159 | }) |
154 | 160 | }) |
| 161 | + |
| 162 | +describe("shouldUseReasoningBudget", () => { |
| 163 | + it("should return true when model has requiredReasoningBudget", () => { |
| 164 | + const model: ModelInfo = { |
| 165 | + contextWindow: 200_000, |
| 166 | + supportsPromptCache: true, |
| 167 | + requiredReasoningBudget: true, |
| 168 | + } |
| 169 | + |
| 170 | + // Should return true regardless of settings |
| 171 | + expect(shouldUseReasoningBudget({ model })).toBe(true) |
| 172 | + expect(shouldUseReasoningBudget({ model, settings: {} })).toBe(true) |
| 173 | + expect(shouldUseReasoningBudget({ model, settings: { enableReasoningEffort: false } })).toBe(true) |
| 174 | + }) |
| 175 | + |
| 176 | + it("should return true when model supports reasoning budget and settings enable reasoning effort", () => { |
| 177 | + const model: ModelInfo = { |
| 178 | + contextWindow: 200_000, |
| 179 | + supportsPromptCache: true, |
| 180 | + supportsReasoningBudget: true, |
| 181 | + } |
| 182 | + |
| 183 | + const settings: ProviderSettings = { |
| 184 | + enableReasoningEffort: true, |
| 185 | + } |
| 186 | + |
| 187 | + expect(shouldUseReasoningBudget({ model, settings })).toBe(true) |
| 188 | + }) |
| 189 | + |
| 190 | + it("should return false when model supports reasoning budget but settings don't enable reasoning effort", () => { |
| 191 | + const model: ModelInfo = { |
| 192 | + contextWindow: 200_000, |
| 193 | + supportsPromptCache: true, |
| 194 | + supportsReasoningBudget: true, |
| 195 | + } |
| 196 | + |
| 197 | + const settings: ProviderSettings = { |
| 198 | + enableReasoningEffort: false, |
| 199 | + } |
| 200 | + |
| 201 | + expect(shouldUseReasoningBudget({ model, settings })).toBe(false) |
| 202 | + expect(shouldUseReasoningBudget({ model, settings: {} })).toBe(false) |
| 203 | + expect(shouldUseReasoningBudget({ model })).toBe(false) |
| 204 | + }) |
| 205 | + |
| 206 | + it("should return false when model doesn't support reasoning budget", () => { |
| 207 | + const model: ModelInfo = { |
| 208 | + contextWindow: 200_000, |
| 209 | + supportsPromptCache: true, |
| 210 | + } |
| 211 | + |
| 212 | + const settings: ProviderSettings = { |
| 213 | + enableReasoningEffort: true, |
| 214 | + } |
| 215 | + |
| 216 | + expect(shouldUseReasoningBudget({ model, settings })).toBe(false) |
| 217 | + expect(shouldUseReasoningBudget({ model })).toBe(false) |
| 218 | + }) |
| 219 | + |
| 220 | + it("should handle undefined settings gracefully", () => { |
| 221 | + const modelWithRequired: ModelInfo = { |
| 222 | + contextWindow: 200_000, |
| 223 | + supportsPromptCache: true, |
| 224 | + requiredReasoningBudget: true, |
| 225 | + } |
| 226 | + |
| 227 | + const modelWithSupported: ModelInfo = { |
| 228 | + contextWindow: 200_000, |
| 229 | + supportsPromptCache: true, |
| 230 | + supportsReasoningBudget: true, |
| 231 | + } |
| 232 | + |
| 233 | + expect(shouldUseReasoningBudget({ model: modelWithRequired, settings: undefined })).toBe(true) |
| 234 | + expect(shouldUseReasoningBudget({ model: modelWithSupported, settings: undefined })).toBe(false) |
| 235 | + }) |
| 236 | +}) |
| 237 | + |
| 238 | +describe("shouldUseReasoningEffort", () => { |
| 239 | + it("should return true when model has reasoningEffort property", () => { |
| 240 | + const model: ModelInfo = { |
| 241 | + contextWindow: 200_000, |
| 242 | + supportsPromptCache: true, |
| 243 | + reasoningEffort: "medium", |
| 244 | + } |
| 245 | + |
| 246 | + // Should return true regardless of settings |
| 247 | + expect(shouldUseReasoningEffort({ model })).toBe(true) |
| 248 | + expect(shouldUseReasoningEffort({ model, settings: {} })).toBe(true) |
| 249 | + expect(shouldUseReasoningEffort({ model, settings: { reasoningEffort: undefined } })).toBe(true) |
| 250 | + }) |
| 251 | + |
| 252 | + it("should return true when model supports reasoning effort and settings provide reasoning effort", () => { |
| 253 | + const model: ModelInfo = { |
| 254 | + contextWindow: 200_000, |
| 255 | + supportsPromptCache: true, |
| 256 | + supportsReasoningEffort: true, |
| 257 | + } |
| 258 | + |
| 259 | + const settings: ProviderSettings = { |
| 260 | + reasoningEffort: "high", |
| 261 | + } |
| 262 | + |
| 263 | + expect(shouldUseReasoningEffort({ model, settings })).toBe(true) |
| 264 | + }) |
| 265 | + |
| 266 | + it("should return false when model supports reasoning effort but settings don't provide reasoning effort", () => { |
| 267 | + const model: ModelInfo = { |
| 268 | + contextWindow: 200_000, |
| 269 | + supportsPromptCache: true, |
| 270 | + supportsReasoningEffort: true, |
| 271 | + } |
| 272 | + |
| 273 | + const settings: ProviderSettings = { |
| 274 | + reasoningEffort: undefined, |
| 275 | + } |
| 276 | + |
| 277 | + expect(shouldUseReasoningEffort({ model, settings })).toBe(false) |
| 278 | + expect(shouldUseReasoningEffort({ model, settings: {} })).toBe(false) |
| 279 | + expect(shouldUseReasoningEffort({ model })).toBe(false) |
| 280 | + }) |
| 281 | + |
| 282 | + it("should return false when model doesn't support reasoning effort", () => { |
| 283 | + const model: ModelInfo = { |
| 284 | + contextWindow: 200_000, |
| 285 | + supportsPromptCache: true, |
| 286 | + } |
| 287 | + |
| 288 | + const settings: ProviderSettings = { |
| 289 | + reasoningEffort: "high", |
| 290 | + } |
| 291 | + |
| 292 | + expect(shouldUseReasoningEffort({ model, settings })).toBe(false) |
| 293 | + expect(shouldUseReasoningEffort({ model })).toBe(false) |
| 294 | + }) |
| 295 | + |
| 296 | + it("should handle different reasoning effort values", () => { |
| 297 | + const model: ModelInfo = { |
| 298 | + contextWindow: 200_000, |
| 299 | + supportsPromptCache: true, |
| 300 | + supportsReasoningEffort: true, |
| 301 | + } |
| 302 | + |
| 303 | + const settingsLow: ProviderSettings = { reasoningEffort: "low" } |
| 304 | + const settingsMedium: ProviderSettings = { reasoningEffort: "medium" } |
| 305 | + const settingsHigh: ProviderSettings = { reasoningEffort: "high" } |
| 306 | + |
| 307 | + expect(shouldUseReasoningEffort({ model, settings: settingsLow })).toBe(true) |
| 308 | + expect(shouldUseReasoningEffort({ model, settings: settingsMedium })).toBe(true) |
| 309 | + expect(shouldUseReasoningEffort({ model, settings: settingsHigh })).toBe(true) |
| 310 | + }) |
| 311 | + |
| 312 | + it("should handle undefined settings gracefully", () => { |
| 313 | + const modelWithReasoning: ModelInfo = { |
| 314 | + contextWindow: 200_000, |
| 315 | + supportsPromptCache: true, |
| 316 | + reasoningEffort: "medium", |
| 317 | + } |
| 318 | + |
| 319 | + const modelWithSupported: ModelInfo = { |
| 320 | + contextWindow: 200_000, |
| 321 | + supportsPromptCache: true, |
| 322 | + supportsReasoningEffort: true, |
| 323 | + } |
| 324 | + |
| 325 | + expect(shouldUseReasoningEffort({ model: modelWithReasoning, settings: undefined })).toBe(true) |
| 326 | + expect(shouldUseReasoningEffort({ model: modelWithSupported, settings: undefined })).toBe(false) |
| 327 | + }) |
| 328 | + |
| 329 | + it("should prioritize model reasoningEffort over settings", () => { |
| 330 | + const model: ModelInfo = { |
| 331 | + contextWindow: 200_000, |
| 332 | + supportsPromptCache: true, |
| 333 | + supportsReasoningEffort: true, |
| 334 | + reasoningEffort: "low", |
| 335 | + } |
| 336 | + |
| 337 | + const settings: ProviderSettings = { |
| 338 | + reasoningEffort: "high", |
| 339 | + } |
| 340 | + |
| 341 | + // Should return true because model.reasoningEffort exists, regardless of settings |
| 342 | + expect(shouldUseReasoningEffort({ model, settings })).toBe(true) |
| 343 | + }) |
| 344 | +}) |
0 commit comments