|
| 1 | +import { describe, it, expect, beforeEach } from "vitest" |
| 2 | +import { AnthropicHandler } from "../anthropic" |
| 3 | +import { OpenRouterHandler } from "../openrouter" |
| 4 | +import { OpenAiHandler } from "../openai" |
| 5 | +import { GeminiHandler } from "../gemini" |
| 6 | +import type { ApiHandlerOptions } from "../../../shared/api" |
| 7 | + |
| 8 | +describe("Context Window Override", () => { |
| 9 | + describe("AnthropicHandler", () => { |
| 10 | + it("should apply modelContextWindow override", () => { |
| 11 | + const options: ApiHandlerOptions = { |
| 12 | + apiKey: "test-key", |
| 13 | + apiModelId: "claude-3-5-sonnet-20241022", |
| 14 | + modelContextWindow: 50000, // Custom context window |
| 15 | + } |
| 16 | + |
| 17 | + const handler = new AnthropicHandler(options) |
| 18 | + const model = handler.getModel() |
| 19 | + |
| 20 | + expect(model.info.contextWindow).toBe(50000) |
| 21 | + }) |
| 22 | + |
| 23 | + it("should use default context window when no override is provided", () => { |
| 24 | + const options: ApiHandlerOptions = { |
| 25 | + apiKey: "test-key", |
| 26 | + apiModelId: "claude-3-5-sonnet-20241022", |
| 27 | + } |
| 28 | + |
| 29 | + const handler = new AnthropicHandler(options) |
| 30 | + const model = handler.getModel() |
| 31 | + |
| 32 | + // Should use the default context window for this model |
| 33 | + expect(model.info.contextWindow).toBe(200000) |
| 34 | + }) |
| 35 | + }) |
| 36 | + |
| 37 | + describe("OpenRouterHandler", () => { |
| 38 | + it("should apply modelContextWindow override", async () => { |
| 39 | + const options: ApiHandlerOptions = { |
| 40 | + openRouterApiKey: "test-key", |
| 41 | + openRouterModelId: "anthropic/claude-3.5-sonnet", |
| 42 | + modelContextWindow: 75000, // Custom context window |
| 43 | + } |
| 44 | + |
| 45 | + const handler = new OpenRouterHandler(options) |
| 46 | + // Mock the models to avoid actual API calls |
| 47 | + ;(handler as any).models = { |
| 48 | + "anthropic/claude-3.5-sonnet": { |
| 49 | + contextWindow: 200000, |
| 50 | + maxTokens: 8192, |
| 51 | + supportsPromptCache: true, |
| 52 | + supportsImages: true, |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + const model = handler.getModel() |
| 57 | + expect(model.info.contextWindow).toBe(75000) |
| 58 | + }) |
| 59 | + }) |
| 60 | + |
| 61 | + describe("OpenAiHandler", () => { |
| 62 | + it("should apply modelContextWindow override to custom model info", () => { |
| 63 | + const options: ApiHandlerOptions = { |
| 64 | + openAiApiKey: "test-key", |
| 65 | + openAiModelId: "gpt-4", |
| 66 | + openAiCustomModelInfo: { |
| 67 | + contextWindow: 128000, |
| 68 | + maxTokens: 4096, |
| 69 | + supportsPromptCache: false, |
| 70 | + supportsImages: true, |
| 71 | + }, |
| 72 | + modelContextWindow: 60000, // Custom context window |
| 73 | + } |
| 74 | + |
| 75 | + const handler = new OpenAiHandler(options) |
| 76 | + const model = handler.getModel() |
| 77 | + |
| 78 | + expect(model.info.contextWindow).toBe(60000) |
| 79 | + }) |
| 80 | + }) |
| 81 | + |
| 82 | + describe("GeminiHandler", () => { |
| 83 | + it("should apply modelContextWindow override", () => { |
| 84 | + const options: ApiHandlerOptions = { |
| 85 | + geminiApiKey: "test-key", |
| 86 | + apiModelId: "gemini-1.5-pro-latest", |
| 87 | + modelContextWindow: 100000, // Custom context window |
| 88 | + } |
| 89 | + |
| 90 | + const handler = new GeminiHandler(options) |
| 91 | + const model = handler.getModel() |
| 92 | + |
| 93 | + expect(model.info.contextWindow).toBe(100000) |
| 94 | + }) |
| 95 | + }) |
| 96 | + |
| 97 | + describe("Edge cases", () => { |
| 98 | + it("should not apply override when modelContextWindow is 0", () => { |
| 99 | + const options: ApiHandlerOptions = { |
| 100 | + apiKey: "test-key", |
| 101 | + apiModelId: "claude-3-5-sonnet-20241022", |
| 102 | + modelContextWindow: 0, // Zero should not override |
| 103 | + } |
| 104 | + |
| 105 | + const handler = new AnthropicHandler(options) |
| 106 | + const model = handler.getModel() |
| 107 | + |
| 108 | + // Should use the default context window |
| 109 | + expect(model.info.contextWindow).toBe(200000) |
| 110 | + }) |
| 111 | + |
| 112 | + it("should not apply override when modelContextWindow is negative", () => { |
| 113 | + const options: ApiHandlerOptions = { |
| 114 | + apiKey: "test-key", |
| 115 | + apiModelId: "claude-3-5-sonnet-20241022", |
| 116 | + modelContextWindow: -1000, // Negative should not override |
| 117 | + } |
| 118 | + |
| 119 | + const handler = new AnthropicHandler(options) |
| 120 | + const model = handler.getModel() |
| 121 | + |
| 122 | + // Should use the default context window |
| 123 | + expect(model.info.contextWindow).toBe(200000) |
| 124 | + }) |
| 125 | + }) |
| 126 | +}) |
0 commit comments