/**
 * @vitest-environment happy-dom
 */
import { describe, expect, it } from 'vitest';
import { act, renderHook } from '@testing-library/react';
import { ForgeProvider, useForge } from './ForgeContext';

describe('ForgeContext', () => {
  const wrapper = ({ children }: { children: React.ReactNode }) => (
    <ForgeProvider>{children}</ForgeProvider>
  );

  it('starts with idle forge state', () => {
    const { result } = renderHook(() => useForge(), { wrapper });
    expect(result.current.forging).toBe(false);
    expect(result.current.stage).toBe('');
    expect(result.current.progress).toBe(0);
  });

  it('startForge sets initializing state', () => {
    const { result } = renderHook(() => useForge(), { wrapper });

    act(() => result.current.startForge());

    expect(result.current.forging).toBe(true);
    expect(result.current.stage).toBe('Initializing forge...');
    expect(result.current.progress).toBe(0);
  });

  it('setStage updates stage and progress while forging', () => {
    const { result } = renderHook(() => useForge(), { wrapper });

    act(() => {
      result.current.startForge();
      result.current.setStage('Compiling', 42);
    });

    expect(result.current.stage).toBe('Compiling');
    expect(result.current.progress).toBe(42);
  });

  it('endForge resets state', () => {
    const { result } = renderHook(() => useForge(), { wrapper });

    act(() => {
      result.current.startForge();
      result.current.setStage('Done', 100);
      result.current.endForge();
    });

    expect(result.current.forging).toBe(false);
    expect(result.current.stage).toBe('');
    expect(result.current.progress).toBe(0);
  });
});
