import { fireEvent, screen } from '@testing-library/react'; import { afterEach, describe, expect, it, vi } from 'vitest'; import { jsonResponse, renderUi } from '../../test/utils'; import { keyEvent, SignInPanel, toBrowser } from './SignInPanel'; const urlOf = (input: RequestInfo | URL) => (typeof input === 'string' ? input : input instanceof URL ? input.href : input.url); const waiting = { waiting: true, gate: { pod: 'Acme UAT', base_url: 'https://acme.fa.example', reason: 'start', remaining: null }, active: false, state: null, owner: null, mine: false, page: null, error: null, idle_left_s: null, started_at: null, session_id: null }; describe('assisted sign-in panel', () => { afterEach(() => vi.restoreAllMocks()); it('maps a click on the scaled picture to the browser pixels', () => { expect(toBrowser(330, 120, { left: 10, top: 20, width: 640, height: 400 })).toEqual({ x: 640, y: 200 }); expect(toBrowser(-50, 9999, { left: 0, top: 0, width: 640, height: 400 })).toEqual({ x: 0, y: 800 }); }); it('sends printable keys as text and named keys as presses', () => { expect(keyEvent({ key: 'a' })).toEqual({ type: 'type', text: 'a' }); expect(keyEvent({ key: 'Enter' })).toEqual({ type: 'press', key: 'Enter' }); expect(keyEvent({ key: 'a', ctrlKey: true })).toEqual({ type: 'press', key: 'Control+a' }); expect(keyEvent({ key: 'Shift' })).toBeNull(); }); it('says a sign-in is needed, opens the session, and sends clicks with its id', async () => { const posts: { url: string; body: unknown }[] = []; vi.spyOn(globalThis, 'fetch').mockImplementation(async (input, init) => { const u = urlOf(input); const req = input instanceof Request ? input : null; const method = req?.method ?? init?.method ?? 'GET'; if (u.endsWith('/api/auth/me')) return jsonResponse({ id: 1, username: 'c', role: 'consultant', permissions: ['read', 'gate.sign_in'] }); if (method === 'POST') { const text = req ? await req.clone().text() : String(init?.body ?? ''); posts.push({ url: u, body: text ? JSON.parse(text) : null }); if (u.endsWith('/sign-in/session')) return jsonResponse({ ...waiting, active: true, state: 'open', owner: 'c', mine: true, page: 'identity provider', idle_left_s: 900, session_id: 'sid-0123456789' }); return jsonResponse({ accepted: 1 }); } if (u.includes('/sign-in/frame')) return new Response(null, { status: 204 }); if (u.endsWith('/sign-in')) return jsonResponse(posts.length ? { ...waiting, active: true, state: 'open', owner: 'c', mine: true, page: 'identity provider', idle_left_s: 900 } : waiting); return jsonResponse({}); }); renderUi(, { auth: true }); expect(await screen.findByText('Sign-in needed')).toBeInTheDocument(); expect(await screen.findByText(/Acme UAT signs in through your company/)).toBeInTheDocument(); fireEvent.click(await screen.findByTestId('sign-in-open')); const picture = await screen.findByAltText('The sign-in browser on the pod'); expect(screen.getByTestId('sign-in-where')).toHaveTextContent("your company's sign-in page"); picture.getBoundingClientRect = () => ({ left: 0, top: 0, width: 640, height: 400, right: 640, bottom: 400, x: 0, y: 0, toJSON: () => ({}) }); fireEvent.click(picture, { clientX: 320, clientY: 200 }); await vi.waitFor(() => expect(posts.some((p) => p.url.endsWith('/sign-in/input'))).toBe(true)); const input = posts.find((p) => p.url.endsWith('/sign-in/input'))!; expect(input.body).toEqual({ session_id: 'sid-0123456789', events: [{ type: 'click', x: 640, y: 400 }] }); }); });