|
| 1 | +/** |
| 2 | + * @fileoverview Tests for token and model utility functions |
| 3 | + */ |
| 4 | + |
| 5 | +import { |
| 6 | + getMaxTokensForModel, |
| 7 | + calculateTokenDistribution, |
| 8 | + ModelInfo, |
| 9 | + ApiConfig, |
| 10 | + DEFAULT_THINKING_MODEL_MAX_TOKENS, |
| 11 | +} from "../model-utils" |
| 12 | + |
| 13 | +describe("Model utility functions", () => { |
| 14 | + describe("getMaxTokensForModel", () => { |
| 15 | + /** |
| 16 | + * Testing the specific fix in commit cc79178f: |
| 17 | + * For thinking models, use apiConfig.modelMaxTokens if available, |
| 18 | + * otherwise fall back to 16_384 (not modelInfo.maxTokens) |
| 19 | + */ |
| 20 | + |
| 21 | + it("should return apiConfig.modelMaxTokens for thinking models when provided", () => { |
| 22 | + const modelInfo: ModelInfo = { |
| 23 | + thinking: true, |
| 24 | + maxTokens: 8000, |
| 25 | + } |
| 26 | + |
| 27 | + const apiConfig: ApiConfig = { |
| 28 | + modelMaxTokens: 4000, |
| 29 | + } |
| 30 | + |
| 31 | + expect(getMaxTokensForModel(modelInfo, apiConfig)).toBe(4000) |
| 32 | + }) |
| 33 | + |
| 34 | + it("should return 16_384 for thinking models when modelMaxTokens not provided", () => { |
| 35 | + const modelInfo: ModelInfo = { |
| 36 | + thinking: true, |
| 37 | + maxTokens: 8000, |
| 38 | + } |
| 39 | + |
| 40 | + const apiConfig: ApiConfig = {} |
| 41 | + |
| 42 | + // This tests the specific fix: now using DEFAULT_THINKING_MODEL_MAX_TOKENS instead of falling back to modelInfo.maxTokens |
| 43 | + expect(getMaxTokensForModel(modelInfo, apiConfig)).toBe(DEFAULT_THINKING_MODEL_MAX_TOKENS) |
| 44 | + }) |
| 45 | + |
| 46 | + it("should return 16_384 for thinking models when apiConfig is undefined", () => { |
| 47 | + const modelInfo: ModelInfo = { |
| 48 | + thinking: true, |
| 49 | + maxTokens: 8000, |
| 50 | + } |
| 51 | + |
| 52 | + expect(getMaxTokensForModel(modelInfo, undefined)).toBe(DEFAULT_THINKING_MODEL_MAX_TOKENS) |
| 53 | + }) |
| 54 | + |
| 55 | + it("should return modelInfo.maxTokens for non-thinking models", () => { |
| 56 | + const modelInfo: ModelInfo = { |
| 57 | + thinking: false, |
| 58 | + maxTokens: 8000, |
| 59 | + } |
| 60 | + |
| 61 | + const apiConfig: ApiConfig = { |
| 62 | + modelMaxTokens: 4000, |
| 63 | + } |
| 64 | + |
| 65 | + expect(getMaxTokensForModel(modelInfo, apiConfig)).toBe(8000) |
| 66 | + }) |
| 67 | + |
| 68 | + it("should return undefined for non-thinking models with undefined maxTokens", () => { |
| 69 | + const modelInfo: ModelInfo = { |
| 70 | + thinking: false, |
| 71 | + } |
| 72 | + |
| 73 | + const apiConfig: ApiConfig = { |
| 74 | + modelMaxTokens: 4000, |
| 75 | + } |
| 76 | + |
| 77 | + expect(getMaxTokensForModel(modelInfo, apiConfig)).toBeUndefined() |
| 78 | + }) |
| 79 | + |
| 80 | + it("should return undefined when modelInfo is undefined", () => { |
| 81 | + const apiConfig: ApiConfig = { |
| 82 | + modelMaxTokens: 4000, |
| 83 | + } |
| 84 | + |
| 85 | + expect(getMaxTokensForModel(undefined, apiConfig)).toBeUndefined() |
| 86 | + }) |
| 87 | + }) |
| 88 | + |
| 89 | + describe("calculateTokenDistribution", () => { |
| 90 | + it("should calculate token distribution correctly", () => { |
| 91 | + const contextWindow = 10000 |
| 92 | + const contextTokens = 5000 |
| 93 | + const maxTokens = 2000 |
| 94 | + |
| 95 | + const result = calculateTokenDistribution(contextWindow, contextTokens, maxTokens) |
| 96 | + |
| 97 | + expect(result.reservedForOutput).toBe(maxTokens) |
| 98 | + expect(result.availableSize).toBe(3000) // 10000 - 5000 - 2000 |
| 99 | + |
| 100 | + // Percentages should sum to 100% |
| 101 | + expect(Math.round(result.currentPercent + result.reservedPercent + result.availablePercent)).toBe(100) |
| 102 | + }) |
| 103 | + |
| 104 | + it("should default to 20% of context window when maxTokens not provided", () => { |
| 105 | + const contextWindow = 10000 |
| 106 | + const contextTokens = 5000 |
| 107 | + |
| 108 | + const result = calculateTokenDistribution(contextWindow, contextTokens) |
| 109 | + |
| 110 | + expect(result.reservedForOutput).toBe(2000) // 20% of 10000 |
| 111 | + expect(result.availableSize).toBe(3000) // 10000 - 5000 - 2000 |
| 112 | + }) |
| 113 | + |
| 114 | + it("should handle negative or zero inputs by using positive fallbacks", () => { |
| 115 | + const result = calculateTokenDistribution(-1000, -500) |
| 116 | + |
| 117 | + expect(result.currentPercent).toBe(0) |
| 118 | + expect(result.reservedPercent).toBe(0) |
| 119 | + expect(result.availablePercent).toBe(0) |
| 120 | + expect(result.reservedForOutput).toBe(0) // With negative inputs, both context window and tokens become 0, so 20% of 0 is 0 |
| 121 | + expect(result.availableSize).toBe(0) |
| 122 | + }) |
| 123 | + |
| 124 | + it("should handle zero total tokens without division by zero errors", () => { |
| 125 | + const result = calculateTokenDistribution(0, 0, 0) |
| 126 | + |
| 127 | + expect(result.currentPercent).toBe(0) |
| 128 | + expect(result.reservedPercent).toBe(0) |
| 129 | + expect(result.availablePercent).toBe(0) |
| 130 | + expect(result.reservedForOutput).toBe(0) |
| 131 | + expect(result.availableSize).toBe(0) |
| 132 | + }) |
| 133 | + }) |
| 134 | +}) |
0 commit comments