/** @vitest-environment jsdom */
import { renderHook, act } from '@testing-library/react';
import { describe, it, expect, beforeEach } from 'vitest';
import { ApiKeyProvider, useApiKey } from './ApiKeyContext';
import React from 'react';

describe('ApiKeyContext', () => {
  beforeEach(() => {
    localStorage.clear();
  });

  it('provides and updates api key', () => {
    const wrapper = ({ children }: { children: React.ReactNode }) => (
      <ApiKeyProvider>{children}</ApiKeyProvider>
    );

    const { result } = renderHook(() => useApiKey(), { wrapper });

    act(() => {
      result.current.setApiKey('test-key');
    });

    expect(result.current.apiKey).toBe('test-key');
    expect(localStorage.getItem('xertica-api-key')).toBe('test-key');
  });
});
