import { describe, it, expect } from 'vitest'
import { mountSuspended, registerEndpoint } from '@nuxt/test-utils/runtime'
import Login from '../../app/pages/login.vue'

const wait = (ms = 50) => new Promise(r => setTimeout(r, ms))

describe('login page', () => {
  it('renders sign in form (local auth always shown)', async () => {
    registerEndpoint('/api/auth/config', () => ({ oidc: { enabled: false, label: '' } }), { method: 'GET' })
    const wrapper = await mountSuspended(Login)
    await wait()
    expect(wrapper.text()).toContain('Sign in')
    expect(wrapper.find('input[type="text"]').exists()).toBe(true)
    expect(wrapper.find('input[type="password"]').exists()).toBe(true)
  })

  it.each([
    [{ enabled: true, label: 'Sign in with Authentik' }, true, false],
    [{ enabled: true, label: 'Sign in with OIDC' }, true, true],
  ])('shows OIDC when enabled: %j', async (oidcConfig, shouldShowButton, shouldShowPassword) => {
    registerEndpoint('/api/auth/config', () => ({ oidc: oidcConfig }), { method: 'GET' })
    await clearNuxtData('auth-config')
    const wrapper = await mountSuspended(Login)
    await wait(150)
    if (shouldShowButton) {
      expect(wrapper.find('a[href*="/api/auth/oidc/authorize"]').exists()).toBe(true)
      if (oidcConfig.label) {
        expect(wrapper.text()).toContain(oidcConfig.label)
      }
    }
    if (shouldShowPassword) {
      expect(wrapper.find('input[type="password"]').exists()).toBe(true)
    }
  })
})
