|
| 1 | +import { describe, it, expect } from "vitest" |
| 2 | +import { validateApiKeyForByteString, validateApiKeyForAscii } from "../api-key-validation" |
| 3 | + |
| 4 | +describe("API Key Validation", () => { |
| 5 | + describe("validateApiKeyForByteString", () => { |
| 6 | + it("should accept valid ASCII characters", () => { |
| 7 | + expect(() => validateApiKeyForByteString("abc123XYZ", "TestProvider")).not.toThrow() |
| 8 | + expect(() => validateApiKeyForByteString("test-api-key_123", "TestProvider")).not.toThrow() |
| 9 | + expect(() => validateApiKeyForByteString("!@#$%^&*()", "TestProvider")).not.toThrow() |
| 10 | + }) |
| 11 | + |
| 12 | + it("should accept extended ASCII characters (128-255)", () => { |
| 13 | + // Extended ASCII characters like ñ (241), ü (252) |
| 14 | + expect(() => validateApiKeyForByteString("test\xF1\xFC", "TestProvider")).not.toThrow() |
| 15 | + expect(() => validateApiKeyForByteString("key\xFF", "TestProvider")).not.toThrow() |
| 16 | + }) |
| 17 | + |
| 18 | + it("should reject characters above 255", () => { |
| 19 | + // Chinese character 中 (20013) |
| 20 | + expect(() => validateApiKeyForByteString("test中key", "TestProvider")).toThrow( |
| 21 | + "Invalid TestProvider API key: contains non-ASCII character at position 5", |
| 22 | + ) |
| 23 | + |
| 24 | + // Emoji 😀 (128512) |
| 25 | + expect(() => validateApiKeyForByteString("key😀", "TestProvider")).toThrow( |
| 26 | + "Invalid TestProvider API key: contains non-ASCII character at position 4", |
| 27 | + ) |
| 28 | + |
| 29 | + // Korean character 한 (54620) |
| 30 | + expect(() => validateApiKeyForByteString("한글key", "TestProvider")).toThrow( |
| 31 | + "Invalid TestProvider API key: contains non-ASCII character at position 1", |
| 32 | + ) |
| 33 | + }) |
| 34 | + |
| 35 | + it("should handle undefined and empty keys", () => { |
| 36 | + expect(() => validateApiKeyForByteString(undefined, "TestProvider")).not.toThrow() |
| 37 | + expect(() => validateApiKeyForByteString("", "TestProvider")).not.toThrow() |
| 38 | + }) |
| 39 | + |
| 40 | + it("should provide clear error messages", () => { |
| 41 | + expect(() => validateApiKeyForByteString("abc中def", "DeepSeek")).toThrow( |
| 42 | + "Invalid DeepSeek API key: contains non-ASCII character at position 4. " + |
| 43 | + "API keys must contain only ASCII characters (character codes 0-255). " + |
| 44 | + "Please check your API key configuration.", |
| 45 | + ) |
| 46 | + }) |
| 47 | + }) |
| 48 | + |
| 49 | + describe("validateApiKeyForAscii", () => { |
| 50 | + it("should accept standard ASCII characters (0-127)", () => { |
| 51 | + expect(() => validateApiKeyForAscii("abc123XYZ", "TestProvider")).not.toThrow() |
| 52 | + expect(() => validateApiKeyForAscii("test-api-key_123", "TestProvider")).not.toThrow() |
| 53 | + expect(() => validateApiKeyForAscii("!@#$%^&*()", "TestProvider")).not.toThrow() |
| 54 | + }) |
| 55 | + |
| 56 | + it("should reject extended ASCII characters (128-255)", () => { |
| 57 | + // Extended ASCII character ñ (241) |
| 58 | + expect(() => validateApiKeyForAscii("test\xF1key", "TestProvider")).toThrow( |
| 59 | + "Invalid TestProvider API key: contains non-ASCII character at position 5", |
| 60 | + ) |
| 61 | + |
| 62 | + // Extended ASCII character ü (252) |
| 63 | + expect(() => validateApiKeyForAscii("key\xFC", "TestProvider")).toThrow( |
| 64 | + "Invalid TestProvider API key: contains non-ASCII character at position 4", |
| 65 | + ) |
| 66 | + }) |
| 67 | + |
| 68 | + it("should reject Unicode characters", () => { |
| 69 | + // Chinese character 中 (20013) |
| 70 | + expect(() => validateApiKeyForAscii("test中key", "TestProvider")).toThrow( |
| 71 | + "Invalid TestProvider API key: contains non-ASCII character at position 5", |
| 72 | + ) |
| 73 | + |
| 74 | + // Emoji 😀 (128512) |
| 75 | + expect(() => validateApiKeyForAscii("key😀", "TestProvider")).toThrow( |
| 76 | + "Invalid TestProvider API key: contains non-ASCII character at position 4", |
| 77 | + ) |
| 78 | + }) |
| 79 | + |
| 80 | + it("should handle undefined and empty keys", () => { |
| 81 | + expect(() => validateApiKeyForAscii(undefined, "TestProvider")).not.toThrow() |
| 82 | + expect(() => validateApiKeyForAscii("", "TestProvider")).not.toThrow() |
| 83 | + }) |
| 84 | + |
| 85 | + it("should provide clear error messages", () => { |
| 86 | + expect(() => validateApiKeyForAscii("abc\xF1def", "OpenAI")).toThrow( |
| 87 | + "Invalid OpenAI API key: contains non-ASCII character at position 4. " + |
| 88 | + "API keys must contain only standard ASCII characters (character codes 0-127). " + |
| 89 | + "Please check your API key configuration.", |
| 90 | + ) |
| 91 | + }) |
| 92 | + }) |
| 93 | +}) |
0 commit comments