import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { SecureStorage } from '../../../src/services/secure-storage'; // Implementation doesn't exist yet

describe('SecureStorage Service', () => {
    const TEST_KEY = 'test_api_key_123';
    const STORAGE_KEY = 'llm_provider_config';

    beforeEach(() => {
        localStorage.clear();
    });

    afterEach(() => {
        localStorage.clear();
    });

    it('should save API key with basic encoding (not plain text)', () => {
        SecureStorage.saveKey(TEST_KEY);

        // Direct access to localStorage to verify obfuscation
        const storedValue = localStorage.getItem(STORAGE_KEY);
        expect(storedValue).toBeDefined();
        expect(storedValue).not.toBe(TEST_KEY); // Should NOT be plain text
        expect(storedValue).not.toContain(TEST_KEY); // Should not contain the key
    });

    it('should retrieve the original API key correctly', () => {
        SecureStorage.saveKey(TEST_KEY);

        const retrievedKey = SecureStorage.getKey();
        expect(retrievedKey).toBe(TEST_KEY);
    });

    it('should return null if no key is stored', () => {
        const retrievedKey = SecureStorage.getKey();
        expect(retrievedKey).toBeNull();
    });

    it('should overwrite existing key when saving new one', () => {
        SecureStorage.saveKey('old_key');
        SecureStorage.saveKey('new_key');

        expect(SecureStorage.getKey()).toBe('new_key');
    });

    it('should clear key when requested', () => {
        SecureStorage.saveKey(TEST_KEY);
        SecureStorage.clearKey();

        expect(SecureStorage.getKey()).toBeNull();
        expect(localStorage.getItem(STORAGE_KEY)).toBeNull();
    });
});
