import { describe, it, expect, vi, beforeEach } from 'vitest' import { render, screen, fireEvent, waitFor } from '@testing-library/react' import { I18nProvider } from '../i18n' import { D2SignInPage } from './D2SignInPage' import * as api from '../api/identity' vi.mock('../api/identity') const renderPage = (onNav = () => {}) => render() describe('D2SignInPage', () => { beforeEach(() => vi.resetAllMocks()) it('offers passkey sign-in and no password field', () => { renderPage() expect(screen.getByRole('button', { name: 'Sign in with passkey' })).toBeInTheDocument() expect(document.querySelector('input[type="password"]')).toBeNull() }) it('signs in with a discoverable passkey and navigates to the account', async () => { const onNav = vi.fn() vi.mocked(api.signIn).mockResolvedValue({ account: { id: 'a1', public_handle: 'ali', display_alias: 'Ali', role: 'customer', status: 'active', preferred_language: 'en', }, csrf_token: 'csrf', }) renderPage(onNav) fireEvent.click(screen.getByRole('button', { name: 'Sign in with passkey' })) await waitFor(() => expect(api.signIn).toHaveBeenCalledWith(undefined)) await waitFor(() => expect(onNav).toHaveBeenCalledWith('id/account')) }) it('reveals a handle-assisted fallback and passes the handle', async () => { vi.mocked(api.signIn).mockResolvedValue({ account: { id: 'a1', public_handle: 'ali', display_alias: 'Ali', role: 'customer', status: 'active', preferred_language: 'en', }, csrf_token: 'csrf', }) renderPage() fireEvent.click(screen.getByRole('button', { name: 'Sign in with a handle instead' })) fireEvent.change(await screen.findByLabelText('Public handle'), { target: { value: 'ali' } }) fireEvent.click(screen.getByRole('button', { name: 'Sign in with handle' })) await waitFor(() => expect(api.signIn).toHaveBeenCalledWith('ali')) }) })