/**
 * @vitest-environment happy-dom
 */
import { describe, expect, it } from 'vitest';
import { cleanup, render, screen } from '@testing-library/react';
import ForgeProgressBar, {
  FORGE_INITIAL_STAGE,
  isForgeProgressIndeterminate,
} from './ForgeProgressBar';

describe('isForgeProgressIndeterminate', () => {
  it('is indeterminate before server reports progress', () => {
    expect(isForgeProgressIndeterminate(FORGE_INITIAL_STAGE, 0)).toBe(true);
    expect(isForgeProgressIndeterminate('', 0)).toBe(true);
  });

  it('is determinate once server reports pct or stage advances', () => {
    expect(isForgeProgressIndeterminate('Compiling agent', 20)).toBe(false);
    expect(isForgeProgressIndeterminate(FORGE_INITIAL_STAGE, 5)).toBe(false);
  });
});

describe('ForgeProgressBar', () => {
  it('renders nothing when not building', () => {
    const { container } = render(
      <ForgeProgressBar building={false} stage="" progress={0} />,
    );
    expect(container.firstChild).toBeNull();
  });

  it('shows indeterminate track before server progress', () => {
    const { container } = render(
      <ForgeProgressBar building stage={FORGE_INITIAL_STAGE} progress={0} />,
    );
    expect(screen.getByText('…')).toBeInTheDocument();
    expect(container.querySelector('.forge-progress-track.indeterminate')).toBeTruthy();
    cleanup();
  });

  it('shows server stage and pct when progress is reported', () => {
    const { container } = render(
      <ForgeProgressBar building stage="Compiling agent" progress={42} />,
    );
    expect(screen.getByText('Compiling agent')).toBeInTheDocument();
    expect(screen.getByText('42%')).toBeInTheDocument();
    expect(container.querySelector('.forge-progress-track.indeterminate')).toBeNull();
  });
});
