import { describe, it, expect, beforeEach } from 'vitest'
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
import { NextIntlClientProvider } from 'next-intl'
import BuilderClientPage from '@/components/BuilderClientPage'

declare global {
  interface Window { __exportPreview?: () => { html: string } }
}

describe('BuilderClientPage robots meta UI 연동', () => {
  beforeEach(() => {
    window.__exportPreview = undefined
  })

  it('기본값에서는 robots meta가 존재하지 않는다', async () => {
    render(
      <NextIntlClientProvider locale="en" messages={{}}>
        <BuilderClientPage />
      </NextIntlClientProvider>
    )
    // Hero 추가(렌더 내용 명확화)
    const addHeroBtn = screen.getByTestId('btn-add-hero')
    addHeroBtn.click()

    expect(typeof window.__exportPreview).toBe('function')
    await waitFor(() => {
      const out = window.__exportPreview!()
      expect(out.html).not.toContain('<meta name="robots"')
    })
  })

  it('UI 입력값이 head robots meta에 반영된다', async () => {
    render(
      <NextIntlClientProvider locale="en" messages={{}}>
        <BuilderClientPage />
      </NextIntlClientProvider>
    )
    const addHeroBtn = screen.getByTestId('btn-add-hero')
    addHeroBtn.click()

    const robotsInput = screen.getByLabelText('Robots Meta') as HTMLInputElement
    fireEvent.change(robotsInput, { target: { value: 'noindex, nofollow' } })

    await waitFor(() => {
      const out = window.__exportPreview!()
      expect(out.html).toContain('<meta name="robots" content="noindex, nofollow"')
    })
  })
})
