import { render, screen } from '@testing-library/react'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { CI_TRAIN } from './fixtures/ci-trains'
import { LaneBar } from './LaneBar'

const { useDeckTooltip } = vi.hoisted(() => ({
  useDeckTooltip: vi.fn((tipb: string, tips?: string) => ({
    'data-tipb': tipb,
    'data-tips': tips,
  })),
}))

vi.mock('./DeckTooltip', () => ({ useDeckTooltip }))

describe('LaneBar', () => {
  beforeEach(() => useDeckTooltip.mockClear())

  it('maps every returned job to one segment without hook-order violations', () => {
    const jobs = CI_TRAIN.gateRun!.jobs
    const { rerender } = render(<LaneBar jobs={jobs} jobsComplete />)
    expect(screen.getAllByTestId('lane-segment')).toHaveLength(2)
    expect(useDeckTooltip).toHaveBeenCalledTimes(2)

    rerender(<LaneBar jobs={[jobs[0]!]} jobsComplete />)
    expect(screen.getAllByTestId('lane-segment')).toHaveLength(1)
    expect(useDeckTooltip).toHaveBeenCalledTimes(3)
  })

  it('stripes only the infra-classified job when jobs are complete', () => {
    render(<LaneBar jobs={CI_TRAIN.gateRun!.jobs} jobsComplete />)
    expect(screen.getByLabelText('Integration database lanes')).toHaveAttribute(
      'data-infra-striped',
      'true',
    )
    expect(screen.getByLabelText('Unit tests')).not.toHaveAttribute('data-infra-striped')
  })

  it('keeps returned lanes visible under jobs incomplete and suppresses striping', () => {
    render(<LaneBar jobs={CI_TRAIN.gateRun!.jobs} jobsComplete={false} />)
    expect(screen.getByText('jobs incomplete')).toBeInTheDocument()
    expect(screen.getAllByTestId('lane-segment')).toHaveLength(2)
    expect(screen.getByLabelText('Integration database lanes')).not.toHaveAttribute(
      'data-infra-striped',
    )
  })

  it('renders a named empty lane state', () => {
    render(<LaneBar jobs={[]} jobsComplete />)
    expect(screen.getByText('No gate jobs')).toBeInTheDocument()
  })
})
