| 1 | import { ModelConfig } from "@continuedev/config-yaml"; |
| 2 | import { describe, expect, it } from "vitest"; |
| 3 | |
| 4 | import { validateContextLength } from "./tokenizer.js"; |
| 5 | |
| 6 | describe("validateContextLength", () => { |
| 7 | const createMockModel = ( |
| 8 | contextLength: number, |
| 9 | maxTokens?: number, |
| 10 | ): ModelConfig => ({ |
| 11 | name: "test-model", |
| 12 | model: "test", |
| 13 | provider: "openai", |
| 14 | defaultCompletionOptions: { |
| 15 | contextLength, |
| 16 | maxTokens, |
| 17 | }, |
| 18 | }); |
| 19 | |
| 20 | const createMockHistory = (messageCount: number, contentLength = 100) => { |
| 21 | return Array.from({ length: messageCount }, (_, i) => ({ |
| 22 | message: { |
| 23 | role: "user" as const, |
| 24 | content: "x".repeat(contentLength), // Roughly contentLength/4 tokens |
| 25 | }, |
| 26 | contextItems: [], |
| 27 | })); |
| 28 | }; |
| 29 | |
| 30 | it("should pass validation when within context limits", () => { |
| 31 | const model = createMockModel(1000, 200); |
| 32 | const chatHistory = createMockHistory(5, 40); // ~50 tokens total |
| 33 | |
| 34 | const result = validateContextLength(chatHistory, model); |
| 35 | |
| 36 | expect(result.isValid).toBe(true); |
| 37 | expect(result.error).toBeUndefined(); |
| 38 | }); |
| 39 | |
| 40 | it("should fail validation when input + maxTokens exceeds context limit", () => { |
| 41 | const model = createMockModel(1000, 800); |
| 42 | const chatHistory = createMockHistory(20, 100); // ~500+ tokens |
| 43 | |
| 44 | const result = validateContextLength(chatHistory, model); |
| 45 | |
| 46 | expect(result.isValid).toBe(false); |
| 47 | expect(result.error).toContain("Context length exceeded"); |
| 48 | expect(result.error).toContain("input"); |
| 49 | expect(result.error).toContain("max_tokens"); |
| 50 | expect(result.error).toContain("context_limit"); |
| 51 | }); |
| 52 | |
| 53 | it("should handle large models with appropriate scaling", () => { |
| 54 | const model = createMockModel(200_000, 64_000); // Claude-like model |
| 55 | const chatHistory = createMockHistory(100, 1000); // Large conversation |
| 56 | |
| 57 | const result = validateContextLength(chatHistory, model); |
| 58 | |
| 59 | expect(result.contextLimit).toBe(200_000); |
| 60 | expect(result.maxTokens).toBe(64_000); |
| 61 | }); |
| 62 | }); |