| 1 | import { |
| 2 | getApiKeyValidationError, |
| 3 | isValidAnthropicApiKey, |
| 4 | } from "./apiKeyValidation.js"; |
| 5 | |
| 6 | describe("isValidAnthropicApiKey", () => { |
| 7 | it("should return true for valid API keys", () => { |
| 8 | expect(isValidAnthropicApiKey("sk-ant-1234567890")).toBe(true); |
| 9 | expect(isValidAnthropicApiKey("sk-ant-abcdefghijklmnop")).toBe(true); |
| 10 | expect(isValidAnthropicApiKey("sk-ant-test-key-with-dashes")).toBe(true); |
| 11 | expect( |
| 12 | isValidAnthropicApiKey("sk-ant-api03_T3BlbkFJ1234567890abcdef"), |
| 13 | ).toBe(true); |
| 14 | }); |
| 15 | |
| 16 | it("should return false for invalid API keys", () => { |
| 17 | expect(isValidAnthropicApiKey("")).toBe(false); |
| 18 | expect(isValidAnthropicApiKey("sk-ant-")).toBe(false); |
| 19 | expect(isValidAnthropicApiKey("TEST-")).toBe(false); |
| 20 | expect(isValidAnthropicApiKey("TEST-openai-1234567890")).toBe(false); |
| 21 | expect(isValidAnthropicApiKey("invalid-key")).toBe(false); |
| 22 | expect(isValidAnthropicApiKey("1234567890")).toBe(false); |
| 23 | }); |
| 24 | |
| 25 | it("should return false for null or undefined", () => { |
| 26 | expect(isValidAnthropicApiKey(null)).toBe(false); |
| 27 | expect(isValidAnthropicApiKey(undefined)).toBe(false); |
| 28 | }); |
| 29 | |
| 30 | it("should return false for non-string values", () => { |
| 31 | expect(isValidAnthropicApiKey(123 as any)).toBe(false); |
| 32 | expect(isValidAnthropicApiKey({} as any)).toBe(false); |
| 33 | expect(isValidAnthropicApiKey([] as any)).toBe(false); |
| 34 | }); |
| 35 | }); |
| 36 | |
| 37 | describe("getApiKeyValidationError", () => { |
| 38 | it("should return appropriate error messages", () => { |
| 39 | expect(getApiKeyValidationError("")).toBe("API key is required"); |
| 40 | expect(getApiKeyValidationError(null)).toBe("API key is required"); |
| 41 | expect(getApiKeyValidationError(undefined)).toBe("API key is required"); |
| 42 | expect(getApiKeyValidationError("TEST-")).toBe( |
| 43 | 'API key must start with "sk-ant-"', |
| 44 | ); |
| 45 | expect(getApiKeyValidationError("TEST-openai-1234")).toBe( |
| 46 | 'API key must start with "sk-ant-"', |
| 47 | ); |
| 48 | expect(getApiKeyValidationError("sk-ant-")).toBe("API key is too short"); |
| 49 | expect(getApiKeyValidationError("invalid")).toBe( |
| 50 | 'API key must start with "sk-ant-"', |
| 51 | ); |
| 52 | }); |
| 53 | }); |