// @vitest-environment jsdom
//
// The sign-in page. Asserts config + that it renders the credential form and the
// footer navigation to the other flows. The network submit (CSRF + POST
// /auth/sign-in) is exercised against a running api by `voltro dev`, not here.

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: Login, meta, renderMode } = await import('./page')

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

let container: HTMLDivElement
let root: Root

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('login — config', () => {
  test('is a static page titled Sign in', () => {
    expect(renderMode).toBe('static')
    expect(meta({ locale: 'en' }).title).toBe('Sign in')
  })
})

describe('login — render', () => {
  test('renders email + password inputs and the flow links', () => {
    render(createElement(Login))
    expect(container.querySelector('input[type="email"]')).not.toBeNull()
    expect(container.querySelector('input[type="password"]')).not.toBeNull()
    expect(container.querySelector('a[href="/signup"]')).not.toBeNull()
    expect(container.querySelector('a[href="/forgot"]')).not.toBeNull()
    expect(container.querySelector('a[href="/magic"]')).not.toBeNull()
  })
})
