import { render, screen, within } from '@testing-library/react'
import { describe, expect, it } from 'vitest'
import { RUN_TASK_CELLS, RUN_TASK_CELLS_ALARMED } from './fixtures/distance-to-done'
import { DistanceToDone } from './DistanceToDone'

const TRUTH_NOTE =
  '“Landed” is verified, never claimed: build & test gate passed, review passed, merged to the integration branch — read from the run journal, not from agent reports.'

describe('DistanceToDone', () => {
  it('renders verified counts, cells, ETA, fix-loop share, and truth note', () => {
    render(
      <DistanceToDone
        landed={1}
        total={5}
        cells={RUN_TASK_CELLS}
        etaMs={7_200_000}
        fixLoop={{ percent: 27 }}
        truthNote={TRUTH_NOTE}
      />,
    )

    expect(screen.getByText('1 / 5')).toBeInTheDocument()
    expect(screen.getByText('tasks truly landed')).toBeInTheDocument()
    expect(screen.getAllByRole('listitem')).toHaveLength(5)
    expect(screen.getByText('ETA 2.0h')).toBeInTheDocument()
    expect(screen.getByText('27%')).toBeInTheDocument()
    expect(screen.getByText(TRUTH_NOTE)).toBeInTheDocument()
  })

  it('renders labeled gaps without metric numbers when ETA and fix-loop data are absent', () => {
    render(
      <DistanceToDone
        landed={0}
        total={0}
        cells={[]}
        etaMs={null}
        fixLoop={null}
        truthNote={TRUTH_NOTE}
      />,
    )

    const trajectory = screen.getByTestId('run-trajectory')
    expect(within(trajectory).getByText('ETA — needs ≥1 measured task')).toBeInTheDocument()
    expect(within(trajectory).getByText('Fix-loop share — not recorded yet')).toBeInTheDocument()
    expect(trajectory).not.toHaveTextContent('%')
  })

  it('renders a task-cell alarm ring only when the cell is alarmed', () => {
    render(
      <DistanceToDone
        landed={1}
        total={RUN_TASK_CELLS_ALARMED.length}
        cells={RUN_TASK_CELLS_ALARMED}
        etaMs={null}
        fixLoop={null}
        truthNote={TRUTH_NOTE}
      />,
    )

    expect(screen.getByTitle('Read-path validation · active')).toHaveAttribute('data-task-alarmed', 'true')
    expect(screen.getByTitle('Negative DB guards · landed')).not.toHaveAttribute('data-task-alarmed')
  })
})
