import { generateApiKey, isValidApiKeyFormat, looksLikeApiKey } from '../src/utils/apiKeyUtils';

describe('API Key Utilities', () => {
    describe('generateApiKey', () => {
        it('should generate a 64-character hex string', () => {
            const apiKey = generateApiKey();
            expect(apiKey).toHaveLength(64);
            expect(apiKey).toMatch(/^[a-f0-9]{64}$/);
        });

        it('should generate unique keys', () => {
            const key1 = generateApiKey();
            const key2 = generateApiKey();
            expect(key1).not.toBe(key2);
        });
    });

    describe('isValidApiKeyFormat', () => {
        it('should return true for valid 64-character hex string', () => {
            const validKey = 'a'.repeat(64);
            expect(isValidApiKeyFormat(validKey)).toBe(true);
        });

        it('should return true for mixed case hex string', () => {
            const validKey = 'A1b2C3d4'.repeat(8); // 64 chars
            expect(isValidApiKeyFormat(validKey)).toBe(true);
        });

        it('should return false for invalid length', () => {
            expect(isValidApiKeyFormat('a'.repeat(63))).toBe(false);
            expect(isValidApiKeyFormat('a'.repeat(65))).toBe(false);
        });

        it('should return false for non-hex characters', () => {
            const invalidKey = 'g'.repeat(64); // 'g' is not a hex character
            expect(isValidApiKeyFormat(invalidKey)).toBe(false);
        });
    });

    describe('looksLikeApiKey', () => {
        it('should return true for 64-character strings', () => {
            expect(looksLikeApiKey('a'.repeat(64))).toBe(true);
        });

        it('should return false for shorter strings', () => {
            expect(looksLikeApiKey('a'.repeat(63))).toBe(false);
        });

        it('should return false for longer strings', () => {
            expect(looksLikeApiKey('a'.repeat(65))).toBe(false);
        });

        it('should return false for JWT-like tokens', () => {
            const jwtToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2RhdGEiOnsiaWQiOjF9fQ.signature';
            expect(looksLikeApiKey(jwtToken)).toBe(false);
        });
    });
}); 