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

function makeFrame(): Frame {
  return {
    id: 'f1',
    width: 400,
    height: 300,
    background: '#ffffff',
    layers: [
      {
        id: 'l1',
        name: 'L1',
        visible: true,
        locked: false,
        objects: [
          { id: 'o1', type: 'image', x: 100, y: 100, width: 50, height: 40, rotate: 0, zIndex: 0, props: { src: 'x', alt: '' } },
        ],
      },
    ],
  }
}

describe('WYSIWYG Canvas resize with 8px snap', () => {
  it('dragging bottom-right handle resizes and snaps to nearest 8px', () => {
    const frame = makeFrame()
    const { getByTestId } = render(<Canvas frame={frame} />)
    const handle = getByTestId('resize-handle-o1')
    const canvas = getByTestId('wysiwyg-canvas')

    // start resize
    fireEvent.mouseDown(handle, { clientX: 150, clientY: 140 })
    // move by +7,+9 → width 50-> 57 -> snap8 => 56, height 40->49 -> snap8 => 48
    fireEvent.mouseMove(canvas, { clientX: 157, clientY: 149 })
    fireEvent.mouseUp(canvas)

    const obj = getByTestId('obj-o1')
    expect(obj).toHaveStyle({ width: '56px', height: '48px' })
  })
})
