import { render, screen, within } from '@testing-library/react'
import { describe, expect, it } from 'vitest'
import {
  CI_AMPLIFICATION_GATES,
  CI_AMPLIFICATION_INVALID_GATES,
  CI_AMPLIFICATION_RESTART,
} from './fixtures/ci-trains'
import { AmplificationSpark } from './AmplificationSpark'

describe('AmplificationSpark', () => {
  it('renders the newest ten gates in input order and caps the ring', () => {
    render(
      <AmplificationSpark
        repo="alexcodeplace/multideal"
        gates={CI_AMPLIFICATION_GATES}
        historyComplete
        warmedAt="2026-07-19T12:00:00.000Z"
      />,
    )

    const runs = screen.getAllByRole('listitem')
    expect(runs).toHaveLength(10)
    expect(runs.map((run) => run.getAttribute('data-run-id'))).toEqual(
      CI_AMPLIFICATION_GATES.slice(0, 10).map((gate) => String(gate.runId)),
    )
    expect(screen.queryByText('Run 102')).not.toBeInTheDocument()
    expect(screen.queryByText('Run 101')).not.toBeInTheDocument()
  })

  it('renders paired compute and wall bars for every visible run', () => {
    render(
      <AmplificationSpark
        repo="alexcodeplace/multideal"
        gates={CI_AMPLIFICATION_GATES.slice(0, 1)}
        historyComplete
        warmedAt="2026-07-19T12:00:00.000Z"
      />,
    )

    const run = screen.getByRole('listitem')
    expect(within(run).getByText('Run 112')).toBeInTheDocument()
    expect(within(run).getByLabelText('Compute 10 minutes')).toBeInTheDocument()
    expect(within(run).getByLabelText('Wall 30 minutes')).toBeInTheDocument()
  })

  it('renders the median multiplier', () => {
    render(
      <AmplificationSpark
        repo="alexcodeplace/multideal"
        gates={CI_AMPLIFICATION_GATES}
        historyComplete
        warmedAt="2026-07-19T12:00:00.000Z"
      />,
    )

    expect(screen.getByText('wall = 3× compute')).toBeInTheDocument()
  })

  it('excludes zero and invalid compute denominators from the median', () => {
    render(
      <AmplificationSpark
        repo="alexcodeplace/multideal"
        gates={CI_AMPLIFICATION_INVALID_GATES}
        historyComplete
        warmedAt="2026-07-19T12:00:00.000Z"
      />,
    )

    expect(screen.getByText('wall = 3× compute')).toBeInTheDocument()
    expect(screen.queryByText(/Infinity|NaN/)).not.toBeInTheDocument()
  })

  it('renders an honest warm-up gap for complete empty history', () => {
    render(
      <AmplificationSpark
        repo="alexcodeplace/multideal"
        gates={[]}
        historyComplete
        warmedAt="2026-07-19T12:00:00.000Z"
      />,
    )

    expect(screen.getByRole('status')).toHaveTextContent('Amplification history warming up')
    expect(screen.queryByText(/wall =/)).not.toBeInTheDocument()
  })

  it('renders restart warm-up without claiming persisted history', () => {
    render(<AmplificationSpark {...CI_AMPLIFICATION_RESTART} />)

    expect(screen.getByRole('status')).toHaveTextContent(
      'History unavailable — warming up after restart',
    )
    expect(screen.queryByRole('list')).not.toBeInTheDocument()
    expect(screen.queryByText(/wall =/)).not.toBeInTheDocument()
  })

  it('renders incomplete history as a gap even when returned samples exist', () => {
    render(
      <AmplificationSpark
        repo="alexcodeplace/multideal"
        gates={CI_AMPLIFICATION_GATES}
        historyComplete={false}
        warmedAt="2026-07-19T12:00:00.000Z"
      />,
    )

    expect(screen.getByRole('status')).toHaveTextContent('Run history incomplete — warming up')
    expect(screen.queryByRole('list')).not.toBeInTheDocument()
  })
})
