import { describe, expect, it, vi } from 'vitest'
import { render, fireEvent, screen } from '@testing-library/react'
import { Checkbox } from './Checkbox'
import { spokenPhraseUntil } from './sr-test-utils'

describe('Checkbox [SR]', () => {
  it('announces unchecked then checked state with name', async () => {
    render(<Checkbox aria-label="Accept terms" />)

    const offPhrase = await spokenPhraseUntil(document.body, (value) => value.toLowerCase().includes('accept terms'))
    expect(offPhrase.toLowerCase()).toContain('accept terms')

    fireEvent.click(screen.getByRole('checkbox', { name: 'Accept terms' }))
    const onPhrase = await spokenPhraseUntil(document.body, (value) => {
      const lowered = value.toLowerCase()
      return lowered.includes('accept terms') && lowered.includes('checked')
    })
    expect(onPhrase.toLowerCase()).toContain('checked')
  })

  it('calls onCheckedChange on interaction', async () => {
    const onCheckedChange = vi.fn()
    render(<Checkbox aria-label="Subscribe" onCheckedChange={onCheckedChange} />)
    fireEvent.click(screen.getByRole('checkbox', { name: 'Subscribe' }))
    expect(onCheckedChange).toHaveBeenCalledWith(true)
  })
})

