File size: 7,142 Bytes
fb62a97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Anchor, Button, Group, SegmentedControl, Stack, Table, Text, Title } from '@mantine/core';
import { useEffect, useState } from 'react';
import { useQueryClient } from '@tanstack/react-query';
import { IconFileReport } from '@/components/icons';
import { api, errorMessage, unwrap } from '../../api/client';
import { useEvidence, useReport } from '../../api/hooks';
import { Can, DecisionBadge, ErrorAlert } from '../../components/common';
import { EmptyState, LoadingState } from '../../components/states';
import { Heals } from './Heals';
import './report.css';

/** A file's media type in plain words ("Screenshot" rather than "image/png"). */
function fileKind(t?: string | null): string {
  if (!t) return 'File';
  if (t.startsWith('image/')) return 'Screenshot';
  if (t.startsWith('video/')) return 'Video';
  if (t.includes('zip')) return 'Trace';
  if (t.includes('json')) return 'Data (JSON)';
  if (t.startsWith('text/')) return 'Text';
  return t;
}

/** The technical report (admin view): the exact rule result per area, the counts, the criteria, the open gaps, the
 * generated report itself and the run's evidence files. */
export function RunReport({ id, done }: { id: string; done: boolean }) {
  const q = useReport(id);
  const ev = useEvidence(id);
  const qc = useQueryClient();
  const [deleting, setDeleting] = useState(false);
  const [delMsg, setDelMsg] = useState<string | null>(null);
  const deleteEvidence = async () => {
    if (!window.confirm('Delete every screenshot, video and trace of this test run? This cannot be undone.')) return;
    setDeleting(true);
    try {
      const r = await unwrap(api.DELETE('/api/runs/{run_id}/evidence', { params: { path: { run_id: id } } })) as { files: number };
      setDelMsg(`Deleted ${r.files} evidence ${r.files === 1 ? 'file' : 'files'}.`);
      await qc.invalidateQueries({ queryKey: ['run', id] });
      void ev.refetch();
    } catch (x) {
      setDelMsg(errorMessage(x));
    } finally {
      setDeleting(false);
    }
  };
  const mods = Object.keys(q.data?.decisions ?? {});
  const [mod, setMod] = useState<string | null>(null);
  useEffect(() => { if (!mod && mods.length) setMod(mods[0]); }, [mods, mod]);
  const refetch = q.refetch;
  useEffect(() => { if (done) void refetch(); }, [done, refetch]);
  if (q.isLoading) return <LoadingState label="Loading the technical report" rows={4} />;
  if (q.error) return <ErrorAlert error={q.error} onRetry={() => void q.refetch()} />;
  const r = q.data!;
  if (!mods.length) {
    return <EmptyState icon={<IconFileReport size={24} />} title="No report yet" body="The reporter writes the technical report at the end of the test run." />;
  }
  const m = mod ?? mods[0];
  const base = `/api/runs/${encodeURIComponent(id)}/report/${m}`;
  return (
    <Stack gap="md" className="rpt">
      <div className="pw-card rpt-notice" role="note">
        <b>{r.target}</b>
        <span>Go/no-go decisions come only from the fixed QA rules (C1–C6), never from the AI. {r.run_mode === 'stub' ? 'Quick simulation: example results, no browser was opened.' : r.target?.startsWith('Oracle Fusion') ? `Live test run against the ${r.target}.` : 'Live test run against the practice app.'}</span>
      </div>
      {mods.length > 1 && <SegmentedControl aria-label="Business area" value={m} onChange={setMod} data={mods} className="rpt-areas" />}
      <section className="pw-card rpt-section" aria-labelledby="rpt-tech-h">
        <Group justify="space-between" wrap="wrap" gap="sm">
          <Group gap="sm"><Title order={2} className="rpt-area" id="rpt-tech-h">{m}</Title><DecisionBadge decision={r.decisions[m]} size="xl" /></Group>
          <Group gap="md" className="rpt-links">
            <Anchor href={`${base}/md?download=true`} download>Markdown</Anchor>
            <Anchor href={`${base}/html?download=true`} download>HTML</Anchor>
            {r.files[m]?.pdf ? <Anchor href={`${base}/pdf`} download data-testid="pdf-link">PDF</Anchor> : <Text size="sm" c="dimmed">PDF not available</Text>}
            <Anchor href={`${base}/summary?download=true`} download>Summary (JSON)</Anchor>
          </Group>
        </Group>
        <Text size="sm" data-testid="exec-summary">{r.executive_summary[m]}</Text>
        <Group gap={6}>{Object.entries(r.counts[m] ?? {}).map(([k, v]) => <span key={k} className="pw-status" data-tone="neutral">{k}: {v}</span>)}</Group>
        <Table fz="sm" aria-label="criteria" className="pw-table">
          <Table.Thead><Table.Tr><Table.Th>Criterion</Table.Th><Table.Th>Met</Table.Th><Table.Th>Detail</Table.Th></Table.Tr></Table.Thead>
          <Table.Tbody>
            {Object.entries(r.criteria[m] ?? {}).map(([k, c]) => (
              <Table.Tr key={k}><Table.Td>{k}</Table.Td><Table.Td><span className="pw-status" data-tone={c.ok ? 'green' : 'red'}>{c.ok ? 'Met' : 'Not met'}</span></Table.Td><Table.Td><Text size="xs">{c.detail}</Text></Table.Td></Table.Tr>
            ))}
          </Table.Tbody>
        </Table>
        <Title order={3} className="rpt-h3">Open gaps</Title>
        {(r.gaps[m] ?? []).length === 0 ? <Text size="xs" c="dimmed">No open gaps.</Text> : (
          <ul className="rpt-list">{(r.gaps[m] ?? []).map((g, i) => <li key={i}>{g}</li>)}</ul>
        )}
      </section>
      <div className="pw-hatch" aria-hidden />
      <div className="pw-card rpt-frame-card">
        <iframe title={`${m} go/no-go report`} src={`${base}/bundle`} sandbox="" className="rpt-frame rpt-frame-tall" />
      </div>
      <Heals runId={id} />
      <Can perm="evidence.read">
        <section className="pw-card rpt-section" aria-labelledby="rpt-evidence-h">
          <Group justify="space-between" wrap="wrap">
            <Title order={2} className="rpt-h2" id="rpt-evidence-h">Evidence (encrypted at rest; every read is audited)</Title>
            <Can perm="admin">
              {(ev.data ?? []).length > 0 && <Button size="xs" color="red" variant="light" loading={deleting} onClick={() => void deleteEvidence()} data-testid="delete-evidence">Delete this run's evidence</Button>}
            </Can>
          </Group>
          {delMsg && <Text size="xs" role="status">{delMsg}</Text>}
          {(ev.data ?? []).length === 0 ? <Text size="xs" c="dimmed">No screenshots, videos or traces for this test run. Quick simulations don&apos;t open a browser, so they have none.</Text> : (
            <Table fz="xs" aria-label="evidence" className="pw-table"><Table.Tbody>
              {(ev.data ?? []).slice(0, 200).map((e) => (
                <Table.Tr key={e.evidence_id}><Table.Td>{e.case_id}</Table.Td><Table.Td>Attempt {e.attempt + 1}</Table.Td><Table.Td>{e.available === false ? <Text span size="xs" c="dimmed" title="No longer stored on the server">{e.name} (no longer stored)</Text> : <Anchor href={`/api/evidence/${e.evidence_id}`} target="_blank" rel="noopener">{e.name}</Anchor>}</Table.Td><Table.Td>{fileKind(e.content_type)}</Table.Td><Table.Td className="pw-num">{Math.round(e.size / 1024)} KB</Table.Td></Table.Tr>
              ))}
            </Table.Tbody></Table>
          )}
        </section>
      </Can>
    </Stack>
  );
}