|
| 1 | +// npx jest src/shared/__tests__/api.test.ts |
| 2 | + |
| 3 | +import { type ModelInfo, ProviderSettings, getModelMaxOutputTokens } from "../api" |
| 4 | + |
| 5 | +describe("getMaxTokensForModel", () => { |
| 6 | + /** |
| 7 | + * Testing the specific fix in commit cc79178f: |
| 8 | + * For thinking models, use apiConfig.modelMaxTokens if available, |
| 9 | + * otherwise fall back to 8192 (not modelInfo.maxTokens) |
| 10 | + */ |
| 11 | + |
| 12 | + it("should return apiConfig.modelMaxTokens for thinking models when provided", () => { |
| 13 | + const model: ModelInfo = { |
| 14 | + contextWindow: 200_000, |
| 15 | + supportsPromptCache: true, |
| 16 | + requiredReasoningBudget: true, |
| 17 | + maxTokens: 8000, |
| 18 | + } |
| 19 | + |
| 20 | + const settings: ProviderSettings = { |
| 21 | + modelMaxTokens: 4000, |
| 22 | + } |
| 23 | + |
| 24 | + expect(getModelMaxOutputTokens({ model, settings })).toBe(4000) |
| 25 | + }) |
| 26 | + |
| 27 | + it("should return 16_384 for thinking models when modelMaxTokens not provided", () => { |
| 28 | + const model: ModelInfo = { |
| 29 | + contextWindow: 200_000, |
| 30 | + supportsPromptCache: true, |
| 31 | + requiredReasoningBudget: true, |
| 32 | + maxTokens: 8000, |
| 33 | + } |
| 34 | + |
| 35 | + const settings = {} |
| 36 | + |
| 37 | + expect(getModelMaxOutputTokens({ model, settings })).toBe(16_384) |
| 38 | + }) |
| 39 | + |
| 40 | + it("should return 16_384 for thinking models when apiConfig is undefined", () => { |
| 41 | + const model: ModelInfo = { |
| 42 | + contextWindow: 200_000, |
| 43 | + supportsPromptCache: true, |
| 44 | + requiredReasoningBudget: true, |
| 45 | + maxTokens: 8000, |
| 46 | + } |
| 47 | + |
| 48 | + expect(getModelMaxOutputTokens({ model, settings: undefined })).toBe(16_384) |
| 49 | + }) |
| 50 | + |
| 51 | + it("should return modelInfo.maxTokens for non-thinking models", () => { |
| 52 | + const model: ModelInfo = { |
| 53 | + contextWindow: 200_000, |
| 54 | + supportsPromptCache: true, |
| 55 | + maxTokens: 8000, |
| 56 | + } |
| 57 | + |
| 58 | + const settings: ProviderSettings = { |
| 59 | + modelMaxTokens: 4000, |
| 60 | + } |
| 61 | + |
| 62 | + expect(getModelMaxOutputTokens({ model, settings })).toBe(8000) |
| 63 | + }) |
| 64 | + |
| 65 | + it("should return undefined for non-thinking models with undefined maxTokens", () => { |
| 66 | + const model: ModelInfo = { |
| 67 | + contextWindow: 200_000, |
| 68 | + supportsPromptCache: true, |
| 69 | + } |
| 70 | + |
| 71 | + const settings: ProviderSettings = { |
| 72 | + modelMaxTokens: 4000, |
| 73 | + } |
| 74 | + |
| 75 | + expect(getModelMaxOutputTokens({ model, settings })).toBeUndefined() |
| 76 | + }) |
| 77 | + |
| 78 | + test("should return maxTokens from modelInfo when thinking is false", () => { |
| 79 | + const model: ModelInfo = { |
| 80 | + contextWindow: 200_000, |
| 81 | + supportsPromptCache: true, |
| 82 | + maxTokens: 2048, |
| 83 | + } |
| 84 | + |
| 85 | + const settings: ProviderSettings = { |
| 86 | + modelMaxTokens: 4096, |
| 87 | + } |
| 88 | + |
| 89 | + const result = getModelMaxOutputTokens({ model, settings }) |
| 90 | + expect(result).toBe(2048) |
| 91 | + }) |
| 92 | + |
| 93 | + test("should return modelMaxTokens from apiConfig when thinking is true", () => { |
| 94 | + const model: ModelInfo = { |
| 95 | + contextWindow: 200_000, |
| 96 | + supportsPromptCache: true, |
| 97 | + maxTokens: 2048, |
| 98 | + requiredReasoningBudget: true, |
| 99 | + } |
| 100 | + |
| 101 | + const settings: ProviderSettings = { |
| 102 | + modelMaxTokens: 4096, |
| 103 | + } |
| 104 | + |
| 105 | + const result = getModelMaxOutputTokens({ model, settings }) |
| 106 | + expect(result).toBe(4096) |
| 107 | + }) |
| 108 | + |
| 109 | + test("should fallback to DEFAULT_THINKING_MODEL_MAX_TOKENS when thinking is true but apiConfig.modelMaxTokens is not defined", () => { |
| 110 | + const model: ModelInfo = { |
| 111 | + contextWindow: 200_000, |
| 112 | + supportsPromptCache: true, |
| 113 | + maxTokens: 2048, |
| 114 | + requiredReasoningBudget: true, |
| 115 | + } |
| 116 | + |
| 117 | + const settings: ProviderSettings = {} |
| 118 | + |
| 119 | + const result = getModelMaxOutputTokens({ model, settings: undefined }) |
| 120 | + expect(result).toBe(16_384) |
| 121 | + }) |
| 122 | + |
| 123 | + test("should handle undefined inputs gracefully", () => { |
| 124 | + const modelInfoOnly: ModelInfo = { |
| 125 | + contextWindow: 200_000, |
| 126 | + supportsPromptCache: true, |
| 127 | + maxTokens: 2048, |
| 128 | + } |
| 129 | + |
| 130 | + expect(getModelMaxOutputTokens({ model: modelInfoOnly, settings: undefined })).toBe(2048) |
| 131 | + }) |
| 132 | + |
| 133 | + test("should handle missing properties gracefully", () => { |
| 134 | + const modelInfoWithoutMaxTokens: ModelInfo = { |
| 135 | + contextWindow: 200_000, |
| 136 | + supportsPromptCache: true, |
| 137 | + requiredReasoningBudget: true, |
| 138 | + } |
| 139 | + |
| 140 | + const settings: ProviderSettings = { |
| 141 | + modelMaxTokens: 4096, |
| 142 | + } |
| 143 | + |
| 144 | + expect(getModelMaxOutputTokens({ model: modelInfoWithoutMaxTokens, settings })).toBe(4096) |
| 145 | + |
| 146 | + const modelInfoWithoutThinking: ModelInfo = { |
| 147 | + contextWindow: 200_000, |
| 148 | + supportsPromptCache: true, |
| 149 | + maxTokens: 2048, |
| 150 | + } |
| 151 | + |
| 152 | + expect(getModelMaxOutputTokens({ model: modelInfoWithoutThinking, settings: undefined })).toBe(2048) |
| 153 | + }) |
| 154 | +}) |
0 commit comments