/**
 * Encryption Utilities Tests
 */

import { describe, it, expect, beforeEach } from 'vitest'
import { encryptData, decryptData, SecureStorage } from '../../utils/encryption'

describe('Encryption Utilities', () => {
  const testData = 'test data to encrypt'
  const testKey = 'test-encryption-key'

  describe('encryptData / decryptData', () => {
    it('should encrypt and decrypt data correctly', async () => {
      const encrypted = await encryptData(testData, testKey)
      expect(encrypted).not.toBe(testData)
      expect(encrypted).toMatch(/^[0-9a-f]+$/i) // Hex string

      const decrypted = await decryptData(encrypted, testKey)
      expect(decrypted).toBe(testData)
    })

    it('should fail to decrypt with wrong key', async () => {
      const encrypted = await encryptData(testData, testKey)
      await expect(decryptData(encrypted, 'wrong-key')).rejects.toThrow()
    })
  })

  describe('SecureStorage', () => {
    let storage: SecureStorage

    beforeEach(() => {
      storage = new SecureStorage()
      localStorage.clear()
    })

    it('should store and retrieve encrypted data', async () => {
      await storage.setItem('test-key', testData)
      const retrieved = await storage.getItem('test-key')
      expect(retrieved).toBe(testData)
    })

    it('should return null for non-existent key', async () => {
      const result = await storage.getItem('non-existent')
      expect(result).toBeNull()
    })

    it('should remove items', async () => {
      await storage.setItem('test-key', testData)
      storage.removeItem('test-key')
      const result = await storage.getItem('test-key')
      expect(result).toBeNull()
    })

    it('should clear all items', async () => {
      await storage.setItem('key1', 'value1')
      await storage.setItem('key2', 'value2')
      storage.clear()
      expect(await storage.getItem('key1')).toBeNull()
      expect(await storage.getItem('key2')).toBeNull()
    })
  })
})
