import { Alert, Button, Group, Stack, Text, TextInput } from '@mantine/core'; import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; import { useEffect, useRef, useState, type KeyboardEvent, type MouseEvent } from 'react'; import { api, unwrap, type Schemas } from '../../api/client'; import { Can, ErrorAlert } from '../../components/common'; // Assisted sign-in (coworker/platform/assisted_signin.py): the run's pod signs in through the company's identity provider, // so a person signs in here, in a browser Astra runs on the pod (on the browser runner when there is one). The picture is // that browser; clicks and keys go to it. Once the Fusion home page opens Astra keeps the session and the run goes on. type SignInEvent = Schemas['SignInEvent']; type Status = Schemas['SignInStatusOut']; /** The sign-in browser's own size: clicks are sent in its pixels, whatever size the picture is shown at. */ export const BROWSER = { width: 1280, height: 800 }; /** Where a click on the picture lands in the browser (the picture is scaled to fit the page). */ export function toBrowser(clientX: number, clientY: number, rect: { left: number; top: number; width: number; height: number }) { const x = ((clientX - rect.left) / Math.max(1, rect.width)) * BROWSER.width; const y = ((clientY - rect.top) / Math.max(1, rect.height)) * BROWSER.height; return { x: Math.round(Math.max(0, Math.min(BROWSER.width, x))), y: Math.round(Math.max(0, Math.min(BROWSER.height, y))) }; } const SPECIAL = new Set(['Enter', 'Tab', 'Backspace', 'Delete', 'Escape', 'ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'Home', 'End', 'PageUp', 'PageDown']); /** A key pressed on the picture as the browser's event: a printable character is text, a named key is a key press. */ export function keyEvent(e: { key: string; ctrlKey?: boolean; metaKey?: boolean; altKey?: boolean }): SignInEvent | null { if (e.ctrlKey || e.metaKey || e.altKey) return /^[A-Za-z0-9]$/.test(e.key) ? { type: 'press', key: `${e.ctrlKey ? 'Control+' : e.metaKey ? 'Meta+' : 'Alt+'}${e.key}` } : null; if (e.key.length === 1) return { type: 'type', text: e.key }; return SPECIAL.has(e.key) ? { type: 'press', key: e.key } : null; } const PAGE_WORDS: Record = { pod: 'the Oracle Fusion pod', 'identity provider': "your company's sign-in page", blank: 'opening…' }; export function SignInPanel({ runId }: { runId: string }) { const qc = useQueryClient(); const [sid, setSid] = useState(null); const [text, setText] = useState(''); const [tick, setTick] = useState(0); const img = useRef(null); const status = useQuery({ queryKey: ['run', runId, 'sign-in'], queryFn: () => unwrap(api.GET('/api/runs/{run_id}/sign-in', { params: { path: { run_id: runId } } })), refetchInterval: 2000, }); const s: Status | undefined = status.data; const open = !!s?.active && !!s?.mine && !!sid; // the picture refreshes about twice a second while the person's own session is open useEffect(() => { if (!open) return; const t = window.setInterval(() => setTick((n) => n + 1), 600); return () => window.clearInterval(t); }, [open]); useEffect(() => { // the run went on (signed in) or the session closed: the page follows if (s && !s.waiting) qc.invalidateQueries({ queryKey: ['run', runId] }); if (s && !s.active) setSid(null); }, [s?.waiting, s?.active]); // eslint-disable-line react-hooks/exhaustive-deps const path = { params: { path: { run_id: runId } } }; const start = useMutation({ mutationFn: () => unwrap(api.POST('/api/runs/{run_id}/sign-in/session', path)), onSuccess: (d) => { setSid(d.session_id ?? null); qc.setQueryData(['run', runId, 'sign-in'], d); }, }); const send = useMutation({ mutationFn: (events: SignInEvent[]) => unwrap(api.POST('/api/runs/{run_id}/sign-in/input', { ...path, body: { session_id: sid ?? '', events } })), }); const finish = useMutation({ mutationFn: () => unwrap(api.POST('/api/runs/{run_id}/sign-in/finish', { ...path, body: { session_id: sid ?? '' } })), onSuccess: (d) => { qc.setQueryData(['run', runId, 'sign-in'], d); qc.invalidateQueries({ queryKey: ['run', runId] }); }, }); const cancel = useMutation({ mutationFn: () => unwrap(api.POST('/api/runs/{run_id}/sign-in/cancel', { ...path, body: { session_id: sid ?? '' } })), onSuccess: (d) => { setSid(null); qc.setQueryData(['run', runId, 'sign-in'], d); }, }); const click = (e: MouseEvent) => { const r = e.currentTarget.getBoundingClientRect(); e.currentTarget.parentElement?.focus(); send.mutate([{ type: 'click', ...toBrowser(e.clientX, e.clientY, r) }]); }; const key = (e: KeyboardEvent) => { const ev = keyEvent(e); if (!ev) return; e.preventDefault(); send.mutate([ev]); }; const typeText = () => { if (!text) return; send.mutate([{ type: 'type', text }]); setText(''); }; const gate = s?.gate; const expired = gate?.reason === 'expired'; return (

Sign-in needed

{gate?.pod ?? 'This pod'} signs in through your company's identity provider, so a person signs in before the tests run. {expired ? ` The earlier session expired: ${gate?.remaining ?? 'some'} scenario(s) wait for a new sign-in.` : ''} {' '}Astra keeps the session only for this run, encrypted, and deletes it when the run ends. What you type is never recorded. {s?.active && !s.mine && {s.owner} is signing in to the pod for this run right now.} {!open && (!s?.active || s.mine) && ( Signing in needs the consultant, QA approver or admin role.}> {s?.state === 'expired' && The last sign-in browser closed after a while without input.} {s?.state === 'failed' && The last sign-in browser stopped{s.error ? `: ${s.error}` : ''}.} )} {open && ( <> Showing {PAGE_WORDS[s?.page ?? 'blank'] ?? 'the page'}. Click in the picture, then type. It closes after {Math.ceil((s?.idle_left_s ?? 900) / 60)} minute(s) without input.
The sign-in browser on the pod
setText(e.currentTarget.value)} onKeyDown={(e) => { if (e.key === 'Enter') { e.preventDefault(); typeText(); } }} style={{ flex: '1 1 260px' }} autoComplete="off" /> {['Enter', 'Tab', 'Backspace'].map((k) => )} )}
); }