import { describe, it, expect } from 'vitest'
import { render, fireEvent, waitFor } from '@testing-library/react'
import { Canvas } from '@/components/wysiwyg/Canvas'
import type { Frame } from '@/lib/wysiwyg/schema'

function makeFrame(): Frame {
  return {
    id: 'f1',
    width: 200,
    height: 160,
    background: '#ffffff',
    layers: [
      {
        id: 'l1',
        name: 'L1',
        visible: true,
        locked: false,
        objects: [
          { id: 'o1', type: 'image', x: 10, y: 12, width: 32, height: 24, rotate: 0, zIndex: 0, props: { src: 'a', alt: '' } },
        ],
      },
    ],
  }
}

describe('WYSIWYG Canvas a11y live region for keyboard nudge', () => {
  it('announces selected object coordinates after Arrow key nudge', async () => {
    const frame = makeFrame()
    const { getByTestId } = render(<Canvas frame={frame} />)

    const canvas = getByTestId('wysiwyg-canvas')
    const live = getByTestId('aria-live')

    // Focus and nudge to the right by 1 (snap8 applies → from 10 to 8 or 16 depending on rounding)
    canvas.focus()
    fireEvent.keyDown(canvas, { key: 'ArrowRight' })

    await waitFor(() => {
      const t = live.textContent || ''
      expect(t).toMatch(/선택\s*1개/)
      expect(t).toMatch(/위치\s*x=\d+/)
      expect(t).toMatch(/y=\d+/)
    })

    // Nudge down
    fireEvent.keyDown(canvas, { key: 'ArrowDown' })
    await waitFor(() => {
      const t = live.textContent || ''
      expect(t).toMatch(/위치\s*x=\d+,\s*y=\d+/)
    })
  })
})
