// @vitest-environment jsdom
//
// The reset page. Asserts config + that submitting with NO `?token=` in the URL
// (jsdom's default location) surfaces the invalid-link message without a network
// call — the guard that a reset can't proceed tokenless.

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

describe('reset — tokenless guard', () => {
  test('submitting without a ?token= shows the invalid-link message', () => {
    render(createElement(Reset))
    const form = container.querySelector('form')!
    act(() => { form.dispatchEvent(new Event('submit', { bubbles: true, cancelable: true })) })
    expect(container.querySelector('.error-text')?.textContent).toContain('invalid or has expired')
  })
})
