import { render, screen, within } from '@testing-library/react'
import { describe, expect, it } from 'vitest'
import {
  CI_RUNNERS,
  CI_RUNNERS_ALL_BUSY,
  CI_RUNNERS_ALL_OFFLINE,
} from './fixtures/ci-trains'
import { RunnerFleetStrip } from './RunnerFleetStrip'

describe('RunnerFleetStrip', () => {
  it('renders runner registration scope when provided', () => {
    render(
      <RunnerFleetStrip
        runners={[{ ...CI_RUNNERS[0], scope: 'org:platform-modules' }]}
        runnersComplete
        oldestQueuedAgeH={0.1}
      />,
    )

    expect(screen.getByText('org:platform-modules')).toBeInTheDocument()
  })

  it('renders a busy runner current job and repository', () => {
    render(<RunnerFleetStrip runners={CI_RUNNERS} runnersComplete oldestQueuedAgeH={0.1} />)

    const runner = screen.getByRole('article', { name: 'Runner debian-1' })
    expect(within(runner).getByText('online')).toBeInTheDocument()
    expect(within(runner).getByText('busy')).toBeInTheDocument()
    expect(within(runner).getByText('Integration database lanes')).toBeInTheDocument()
    expect(within(runner).getByText('alexcodeplace/multideal')).toBeInTheDocument()
  })

  it('renders an idle runner without a current assignment', () => {
    render(<RunnerFleetStrip runners={CI_RUNNERS} runnersComplete oldestQueuedAgeH={0.1} />)

    const runner = screen.getByRole('article', { name: 'Runner debian-2' })
    expect(within(runner).getByText('idle')).toBeInTheDocument()
    expect(within(runner).getByText('No current job')).toBeInTheDocument()
  })

  it('renders explicit unknown and suppresses unverified current assignment values', () => {
    render(<RunnerFleetStrip runners={CI_RUNNERS} runnersComplete oldestQueuedAgeH={0.1} />)

    const runner = screen.getByRole('article', { name: 'Runner runner-current-unknown' })
    expect(within(runner).getByText('Current job unknown')).toBeInTheDocument()
    expect(screen.queryByText('Unverified job')).not.toBeInTheDocument()
    expect(screen.queryByText('unverified/repo')).not.toBeInTheDocument()
  })

  it('shows aggregate occupancy and head-of-line saturation for a non-empty all-busy online fleet', () => {
    render(<RunnerFleetStrip runners={CI_RUNNERS_ALL_BUSY} runnersComplete oldestQueuedAgeH={0.26} />)

    expect(screen.getByLabelText('Busy runners / total runners')).toHaveTextContent('2 / 2')
    expect(screen.getByRole('status', { name: 'Runner saturation' })).toHaveTextContent(
      'head-of-line saturation',
    )
  })

  it('does not show saturation at the exact fifteen-minute threshold', () => {
    render(<RunnerFleetStrip runners={CI_RUNNERS_ALL_BUSY} runnersComplete oldestQueuedAgeH={0.25} />)

    expect(screen.getByLabelText('Busy runners / total runners')).toHaveTextContent('2 / 2')
    expect(screen.queryByText('head-of-line saturation')).not.toBeInTheDocument()
  })

  it('labels incomplete runner data, hides aggregate occupancy, and suppresses saturation', () => {
    render(<RunnerFleetStrip runners={CI_RUNNERS_ALL_BUSY} runnersComplete={false} oldestQueuedAgeH={1} />)

    expect(screen.getByText('Runner fleet incomplete')).toBeInTheDocument()
    expect(screen.getByLabelText('Busy runners / total runners')).toHaveTextContent('—')
    expect(screen.queryByText('2 / 2')).not.toBeInTheDocument()
    expect(screen.queryByText('head-of-line saturation')).not.toBeInTheDocument()
  })

  it('renders a complete empty fleet without saturation', () => {
    render(<RunnerFleetStrip runners={[]} runnersComplete oldestQueuedAgeH={1} />)

    expect(screen.getByText('No runners reported')).toBeInTheDocument()
    expect(screen.getByLabelText('Busy runners / total runners')).toHaveTextContent('0 / 0')
    expect(screen.queryByText('head-of-line saturation')).not.toBeInTheDocument()
  })

  it('never renders saturation for an all-offline fleet with an old queue', () => {
    render(<RunnerFleetStrip runners={CI_RUNNERS_ALL_OFFLINE} runnersComplete oldestQueuedAgeH={2} />)

    expect(screen.getByText('offline')).toBeInTheDocument()
    expect(screen.queryByText('head-of-line saturation')).not.toBeInTheDocument()
  })
})
