import { describe, it, expect, vi } from 'vitest'
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
import { WysiwygBuilderPage } from '@/components/wysiwyg/WysiwygBuilderPage'

// minimal mock for URL.createObjectURL used by download
let spyCreate: ReturnType<typeof vi.spyOn> | null = null
let spyRevoke: ReturnType<typeof vi.spyOn> | null = null

describe('WysiwygBuilderPage', () => {
  beforeEach(() => {
    spyCreate = vi.spyOn(global.URL, 'createObjectURL').mockReturnValue('blob:mock')
    spyRevoke = vi.spyOn(global.URL, 'revokeObjectURL').mockImplementation(() => {})
  })
  afterEach(() => {
    spyCreate?.mockRestore()
    spyRevoke?.mockRestore()
    spyCreate = null
    spyRevoke = null
  })

  it('adds a text object to the canvas', async () => {
    render(<WysiwygBuilderPage />)
    const addText = screen.getByRole('button', { name: /^Add Text$/i })
    fireEvent.click(addText)
    await waitFor(() => {
      expect(screen.getByTestId('obj-text-1')).toBeInTheDocument()
    })
  })

  it('has an Export button', () => {
    render(<WysiwygBuilderPage />)
    expect(screen.getByRole('button', { name: /Export/i })).toBeInTheDocument()
  })
})
