File size: 12,114 Bytes
af1bee9 fb43301 af1bee9 fb43301 af1bee9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | import { Button, Group, Stack, Text } from '@mantine/core';
import { IconCheck, IconExternalLink } from '@/components/icons';
import { useEffect, useRef, useState } from 'react';
import { Link } from 'react-router-dom';
import { errorMessage } from '../api/client';
import { useRun, useRunReadiness } from '../api/hooks';
import { Pill } from '../components/common';
import { ProcessFlow } from '../components/ProcessFlow';
import { RUN_STATUS_WORDS, VERDICT_TONE } from '../domain/glossary';
import { ScenarioCard } from '../pages/run/ScenarioReview';
import { runTitle } from '../domain/runName';
import { cancelAction, confirmAction, type ChatAction, type ChatCard, type ChatLink, type ChatSource } from './api';
const hhmm = (iso?: string | null) => {
if (!iso) return '';
const d = new Date(iso);
return Number.isNaN(d.getTime()) ? '' : `${String(d.getHours()).padStart(2, '0')}:${String(d.getMinutes()).padStart(2, '0')}`;
};
/** Answer cards: the existing components at compact size. Built only from structured tool results, never from text. */
export function AnswerCard({ card }: { card: ChatCard }) {
if (card.type === 'readiness') {
return (
<div className="fpt-card fpt-chat-card" data-testid="chat-card-readiness">
<Group justify="space-between" gap={6} mb={6} wrap="wrap">
<Text size="sm" fw={600}>{card.name}</Text>
<Pill tone={VERDICT_TONE[card.verdict] ?? 'neutral'}>{card.verdict}</Pill>
</Group>
<ProcessFlow steps={card.steps} />
</div>
);
}
if (card.type === 'run') {
return (
<div className="fpt-card fpt-chat-card" data-testid="chat-card-run">
<Text size="sm" fw={600}>{card.title}</Text>
<Group gap={4} mt={4}>
<Pill tone={card.pending_gate ? 'amber' : card.status === 'completed' ? 'green' : 'ink'}>{card.status_words}</Pill>
<Pill>{card.counts.tested}/{card.counts.scenarios} tested</Pill>
{card.counts.failing > 0 && <Pill tone="red">{card.counts.failing} failing</Pill>}
</Group>
</div>
);
}
if (card.type === 'failures') {
return (
<Stack gap={6} data-testid="chat-card-failures">
{card.items.map((f) => (
<div key={f.case_id} className="fpt-card fpt-chat-card">
<Text size="sm" fw={600}>{f.title ?? f.case_id}</Text>
{f.step && <Pill tone="mint">failed at: {f.step}</Pill>}
<Text size="xs" c="dimmed" mt={6}>Expected</Text>
<Text size="sm">{f.expected ?? 'not stated in the test output'}</Text>
<Text size="xs" c="dimmed" mt={4}>Actual</Text>
<Text size="sm">{f.actual ?? 'not stated in the test output'}</Text>
<Text size="xs" mt={6}>Suggested cause: {f.suggestion}</Text>
</div>
))}
</Stack>
);
}
if (card.type === 'scenario') return <div className="fpt-card fpt-chat-card" data-testid="chat-card-scenario"><ScenarioCard s={card.scenario} editable={false} /></div>;
if (card.type === 'changes') {
return (
<Stack gap={6} data-testid="chat-card-changes">
{card.items.map((c) => (
<div key={c.id} className="fpt-card fpt-chat-card">
<Text size="sm" fw={600}>{c.area}</Text>
<Text size="xs">{c.summary}</Text>
<Group gap={4} mt={4}>
{c.affected_steps.slice(0, 3).map((s) => <Pill key={s} tone="mint">{s}</Pill>)}
{c.gap ? <Pill tone="amber">No test covers this yet</Pill> : <Pill tone="green">{c.covering} scenario(s) cover it</Pill>}
</Group>
</div>
))}
</Stack>
);
}
if (card.type === 'runs') {
return (
<div className="fpt-card fpt-chat-card" data-testid="chat-card-runs">
<Stack gap={4}>
{card.items.map((r) => (
<Group key={r.run_id} gap={6} wrap="nowrap" justify="space-between">
<Link to={`/runs/${r.run_id}`} style={{ fontSize: 12, minWidth: 0, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>{r.title}</Link>
<Pill tone={r.pending_gate ? 'amber' : 'neutral'}>{r.pending_gate ? 'your review' : r.status_words}</Pill>
</Group>
))}
</Stack>
</div>
);
}
return (
<div className="fpt-card fpt-chat-card" data-testid="chat-card-pods">
<Stack gap={4}>{card.items.map((p) => <Group key={p.id} gap={6} justify="space-between"><Text size="sm">{p.name}</Text><Pill tone={p.runnable ? 'green' : 'neutral'}>{p.runnable ? 'can run tests' : 'not set up yet'}</Pill></Group>)}</Stack>
</div>
);
}
export function ScreenLinks({ links }: { links: ChatLink[] }) {
if (!links.length) return null;
return <Group gap={6}>{links.map((l) => <Button key={l.to + l.label} component={Link} to={l.to} size="compact-xs" variant="default" rightSection={<IconExternalLink size={11} />}>{l.label}</Button>)}</Group>;
}
/** Where the answer comes from. No source = say so. */
export function Sources({ sources, noSource }: { sources: ChatSource[]; noSource?: boolean }) {
if (!sources.length) return noSource ? <Text size="xs" c="dimmed" data-testid="chat-source">No source for this answer.</Text> : null;
return (
<Text size="xs" c="dimmed" data-testid="chat-source">
Source: {sources.map((s, i) => <span key={s.label}>{i > 0 ? '; ' : ''}{s.to ? <Link to={s.to}>{s.label}</Link> : s.label}</span>)}
</Text>
);
}
/** One live line for a test run started from chat; it updates in place, never one message per step. */
export function RunProgress({ runId }: { runId: string }) {
const run = useRun(runId);
const rd = useRunReadiness(runId);
const r = run.data;
if (!r) return null;
const c = (rd.data?.modules ?? []).reduce((a, m) => ({ scen: a.scen + m.counts.scenarios, done: a.done + m.counts.passed + m.counts.passed_on_retry + m.counts.failing, fail: a.fail + m.counts.failing }), { scen: 0, done: 0, fail: 0 });
const waiting = r.pending_gate === 'approve_cases' ? 'scenarios' : r.pending_gate === 'human_review' ? 'failures' : null;
return (
<div className="fpt-card fpt-chat-card" data-testid="chat-run-progress" role="status">
<Text size="sm"><b>{runTitle(r)}</b></Text>
<Text size="xs" c="dimmed">{RUN_STATUS_WORDS[r.status] ?? r.status} 路 {c.done}/{c.scen} tested{c.fail ? ` 路 ${c.fail} failing` : ''}</Text>
<Group gap={6} mt={6}>
<Button component={Link} to={`/runs/${runId}`} size="compact-xs" variant="default">Open the test run</Button>
{waiting && <Button component={Link} to={`/runs/${runId}/${waiting}`} size="compact-xs">{waiting === 'scenarios' ? 'Open scenario review' : 'Open failure review'}</Button>}
{r.status === 'running' && r.playwright_mode !== 'stub' && r.live_view && <Button component={Link} to={`/runs/${runId}/live`} size="compact-xs" variant="default">Watch live</Button>}
</Group>
</div>
);
}
const DONE_WORDS: Record<ChatAction['tool'], (a: ChatAction) => string> = {
start_run: () => 'test run started',
approve_scenarios: (a) => `approved ${a.result?.approved ?? a.counts?.approve ?? ''}${a.result?.skipped ? `, skipped ${a.result.skipped}` : ''}`,
cancel_run: () => 'test run cancelled',
add_note: () => 'note saved',
};
/** A proposed action: nothing changes until the user confirms. Cancel has the default focus; the primary button names
* the verb and the number. After execution the card collapses to one "Done" line. */
export function ConfirmCard({ action, onChange }: { action: ChatAction; onChange: (a: ChatAction) => void }) {
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
const [open, setOpen] = useState(false);
const boxRef = useRef<HTMLDivElement>(null);
useEffect(() => { if (open) boxRef.current?.scrollIntoView?.({ block: 'nearest' }); }, [open]);
const cancelRef = useRef<HTMLButtonElement>(null);
useEffect(() => { if (action.status === 'proposed') cancelRef.current?.focus({ preventScroll: true }); }, [action.action_id, action.status]);
if (action.status === 'confirmed') {
return (
<Stack gap={6}>
<Text size="sm" data-testid="chat-action-done" role="status"><IconCheck size={13} style={{ verticalAlign: -2 }} aria-hidden /> Done 路 {DONE_WORDS[action.tool](action)} 路 by {action.result?.by ?? 'you'} 路 {hhmm(action.result?.at ?? action.executed_at)}</Text>
{action.tool === 'start_run' && action.result?.run_id && <RunProgress runId={action.result.run_id} />}
{action.tool !== 'start_run' && action.result?.to && <ScreenLinks links={[{ label: 'Open the test run', to: action.result.to }]} />}
</Stack>
);
}
if (action.status !== 'proposed') {
const why = action.status === 'cancelled' ? 'Cancelled. Nothing was changed.' : action.status === 'expired' ? 'This expired after 10 minutes. Nothing was changed; ask again if you still want it.'
: `It could not be done${action.result?.detail ? `: ${action.result.detail}` : '.'} Nothing else was changed.`;
return <Text size="sm" c="dimmed" data-testid="chat-action-closed">{why}</Text>;
}
const run = async (fn: () => Promise<void>) => { setBusy(true); setError(null); try { await fn(); } catch (e) { setError(errorMessage(e)); } finally { setBusy(false); } };
const confirm = () => run(async () => {
try {
const r = await confirmAction(action.action_id);
onChange({ ...action, status: 'confirmed', result: r.result });
} catch (e) {
const code = (e as { code?: string }).code;
if (code === 'EXPIRED') onChange({ ...action, status: 'expired' });
else if (code === 'ALREADY_USED') onChange({ ...action, status: 'failed', result: { detail: 'it was already used' } });
else if (code && code !== 'FORBIDDEN' && code !== 'HTTP_ERROR') onChange({ ...action, status: 'failed', result: { code, detail: errorMessage(e) } });
else throw e;
}
});
const cancel = () => run(async () => { await cancelAction(action.action_id); onChange({ ...action, status: 'cancelled' }); });
const approve = action.items.filter((i) => i.decision !== 'skip');
const skip = action.items.filter((i) => i.decision === 'skip');
return (
<div ref={boxRef} className="fpt-card fpt-chat-confirm" data-testid="chat-confirm" role="group" aria-label="Confirm this action">
{action.tool === 'approve_scenarios' ? (
<>
<Text size="xs"><b>{approve.length}</b> to approve{skip.length ? <> 路 <b>{skip.length}</b> skipped for this patch</> : null}</Text>
<Button variant="subtle" size="compact-xs" px={0} onClick={() => setOpen((o) => !o)} aria-expanded={open}>{open ? 'Hide the list' : 'Show the list'}</Button>
{open && (
<ul className="fpt-chat-list">
{approve.map((i) => <li key={i.id ?? i.value}><b>Approve</b> 路 {i.label}: {i.value}</li>)}
{skip.map((i) => <li key={i.id ?? i.value}><b>Skip</b> 路 {i.label}: {i.value}</li>)}
</ul>
)}
</>
) : (
<dl className="fpt-chat-dl">{action.items.map((i) => <div key={i.label} style={{ display: 'contents' }}><dt>{i.label}</dt><dd>{i.value}</dd></div>)}</dl>
)}
{error && <Text size="xs" c="red" role="alert">{error}</Text>}
<Group gap={6} mt={8} wrap="wrap">
<Button size="compact-sm" variant="filled" onClick={() => void confirm()} loading={busy} data-testid="chat-confirm-button">{action.verb}</Button>
{action.review && <Button size="compact-sm" variant="default" component={Link} to={action.review.to}>{action.review.label}</Button>}
<Button size="compact-sm" variant="default" ref={cancelRef} onClick={() => void cancel()} disabled={busy}>Cancel</Button>
</Group>
<Text size="xs" c="dimmed" mt={6}>Expires at {hhmm(action.expires_at)}. You can do the same on the screens.</Text>
</div>
);
}
|