// @vitest-environment jsdom
//
// `/` public landing — a static page with fixed content. Assert its render-mode
// + meta config and the hero copy / links.

import { afterEach, describe, expect, test } from 'vitest'
import { act, createElement, type ReactNode } from 'react'
import { createRoot, type Root } from 'react-dom/client'
import { I18nProvider } from '@voltro/i18n'
import enCatalog from '../../locales/en'

const { default: Landing, meta, renderMode } = await import('./page')

;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true

let container: HTMLDivElement
let root: Root

// The page uses <T>, so it needs an <I18nProvider> (the framework auto-wires
// this in the running app). Wrap with the real English catalog.
const render = (node: ReactNode): void => {
  container = document.createElement('div')
  document.body.appendChild(container)
  act(() => {
    root = createRoot(container)
    root.render(
      createElement(I18nProvider, { locale: 'en', messages: enCatalog, defaultLocale: 'en', children: node }),
    )
  })
}

afterEach(() => {
  if (root) act(() => root.unmount())
  container?.remove()
  document.body.innerHTML = ''
})

describe('landing — config', () => {
  test('pre-renders as a static page with title + description meta', () => {
    expect(renderMode).toBe('static')
    expect(meta({ locale: 'en' }).title).toBe('Welcome')
    expect(meta({ locale: 'en' }).description).toContain('marketing')
  })
})

describe('landing — render', () => {
  test('renders the hero heading and a sign-in call to action', () => {
    render(createElement(Landing))
    expect(container.querySelector('h1')!.textContent).toContain('boring parts')
    const cta = Array.from(container.querySelectorAll('a')).find(
      (a) => a.getAttribute('href') === '/login' && (a.textContent ?? '').includes('Sign in'),
    )
    expect(cta).toBeTruthy()
  })
})
