File size: 8,277 Bytes
af1bee9
fb43301
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
import { Anchor, Button, Group, Image, Modal, SegmentedControl, SimpleGrid, Stack, Text, TextInput, Title, UnstyledButton } from '@mantine/core';
import { IconExternalLink, IconListNumbers, IconLock, IconPhoto, IconPhotoOff, IconSearch } from '@/components/icons';
import { useMemo, useState } from 'react';
import { useEvidence, useRunScenarios } from '../../api/hooks';
import type { Evidence } from '../../api/types';
import { Can, ErrorAlert, Pager } from '../../components/common';
import { EmptyState, LoadingState } from '../../components/states';
import './report.css';
import { useSidePanelOptional } from '../../components/shell/SidePanel';

const PAGE = 6;
const OUTCOME: Record<string, [string, string]> = { passed: ['green', 'Passed'], failed: ['red', 'Failed'], skipped: ['neutral', 'Not run'] };

/** Proof of what the test browser did: per scenario, the values it entered and the screen it ended on. */
export type ProofFilter = 'all' | 'failed' | 'passed';

export function RunProof({ id, quick, oneColumn = false, fusionPod = null, filter: initialFilter = 'all' }: {

  id: string; quick: boolean; oneColumn?: boolean; fusionPod?: string | null; filter?: ProofFilter;

}) {
  const ev = useEvidence(id);
  const sc = useRunScenarios(id);
  const panel = useSidePanelOptional();
  const [q, setQ] = useState('');
  const [offset, setOffset] = useState(0);
  const [filter, setFilter] = useState<ProofFilter>(initialFilter);
  const [big, setBig] = useState<{ src: string; title: string } | null>(null);

  const rows = useMemo(() => {
    const by = new Map<string, Evidence[]>();
    for (const e of ev.data ?? []) if (e.case_id) by.set(e.case_id, [...(by.get(e.case_id) ?? []), e]);
    const titles = new Map((sc.data?.scenarios ?? []).map((s) => [s.id, s]));
    return [...by.entries()].map(([caseId, files]) => {
      const last = Math.max(...files.map((f) => f.attempt));
      const mine = files.filter((f) => f.attempt === last);
      const shots = mine.filter((f) => f.content_type.startsWith('image/'));
      return { caseId, s: titles.get(caseId), shot: shots[shots.length - 1], video: mine.find((f) => f.content_type.startsWith('video/')) };
    }).filter((r) => r.shot).sort((a, b) => a.caseId.localeCompare(b.caseId));
  }, [ev.data, sc.data]);

  if (quick) return <EmptyState icon={<IconPhoto size={24} />} title="No screens in a quick simulation" body="A quick simulation starts no browser. Start a full browser run to see the screens the test browser ended on." />;
  if (ev.isLoading || sc.isLoading) return <LoadingState label="Loading the screens" rows={3} />;
  if (ev.error) return <ErrorAlert error={ev.error} onRetry={() => void ev.refetch()} />;
  if (!rows.length) return <EmptyState icon={<IconPhoto size={24} />} title="No screens yet" body="They appear here as each group of scenarios finishes." />;

  const needle = q.trim().toLowerCase();
  const shown = rows.filter((r) => (!needle || `${r.caseId} ${r.s?.title ?? ''}`.toLowerCase().includes(needle))
    && (filter === 'all' || (filter === 'failed' ? r.s?.outcome === 'failed' : r.s?.outcome === 'passed' || r.s?.outcome === 'flaky')));
  const failedCount = rows.filter((r) => r.s?.outcome === 'failed').length;
  const where = fusionPod ? `Screens come from the Oracle Fusion pod ${fusionPod}.` : 'Screens come from the practice environment (the mock app), not Oracle Fusion.';
  const page = shown.slice(offset, offset + PAGE);

  return (
    <Can perm="evidence.read" fallback={<EmptyState icon={<IconLock size={24} />} title="Your role cannot open test evidence" />}>

      <Stack gap="sm">

        <Text size="sm" c="dimmed">

          For each scenario: the test data it used and the screen the test browser ended on, saved by the browser itself. Compare the two to check the entry.

          {oneColumn ? '' : ` ${where}`}

        </Text>

        <Group justify="space-between" wrap="wrap">

          <TextInput aria-label="Find a scenario" placeholder="Find a scenario" leftSection={<IconSearch size={14} />} size="xs" w={oneColumn ? '100%' : 260} value={q}

            onChange={(e) => { setQ(e.currentTarget.value); setOffset(0); }} />

          <SegmentedControl size="xs" radius="xl" value={filter} onChange={(v) => { setFilter(v as ProofFilter); setOffset(0); }} aria-label="Show screens"

            data={[{ value: 'all', label: 'All' }, { value: 'failed', label: `Failed ${failedCount}` }, { value: 'passed', label: 'Passed' }]} />

          <Text size="xs" c="dimmed">{shown.length} scenario{shown.length === 1 ? '' : 's'} with a saved screen</Text>

        </Group>

        <SimpleGrid cols={oneColumn ? 1 : { base: 1, lg: 2 }} spacing="sm">

          {page.map(({ caseId, s, shot, video }) => {

            const [tone, word] = OUTCOME[s?.outcome ?? ''] ?? ['neutral', s?.outcome ?? 'tested'];

            const name = s?.title ?? caseId;

            const values = (s?.test_data ?? []).flatMap((d) => Object.entries(d.values)).filter(([, v]) => !/\{\{|POD-REQUIRED/.test(String(v)));

            return (

              <article key={caseId} className="pw-card rpt-proof" data-testid="proof-card" aria-label={name}>

                <div className="rpt-proof-head">

                  <div className="rpt-proof-title">

                    <Title order={3} className="rpt-h3">{name}</Title>

                    <span className="fpt-idpill">{caseId}</span>

                  </div>

                  <div className="rpt-proof-tags">

                    {panel && <Button size="compact-xs" variant="subtle" leftSection={<IconListNumbers size={12} />} onClick={() => panel.show('steps', { scenario: caseId })}>Show steps</Button>}

                    <span className="pw-status" data-tone={tone}>{word}</span>

                  </div>

                </div>

                {shot.available === false ? (

                  <div className="rpt-shot rpt-shot-gone" data-testid="shot-gone">

                    <IconPhotoOff size={22} aria-hidden />

                    <Text size="xs" c="dimmed">This screenshot is no longer stored: the server restarted before screenshots were kept outside it.</Text>

                  </div>

                ) : (

                  <>

                    <UnstyledButton className="rpt-shot" onClick={() => setBig({ src: `/api/evidence/${shot.evidence_id}`, title: name })} title="See the screen full size"

                      aria-label={`See the final screen of ${name} full size`}>

                      <Image src={`/api/evidence/${shot.evidence_id}`} alt={`final screen of ${name}`} radius="sm" fit="contain" loading="lazy" />

                    </UnstyledButton>

                    <Anchor href={`/api/evidence/${shot.evidence_id}`} target="_blank" rel="noopener" size="xs" className="rpt-ext"><IconExternalLink size={11} aria-hidden /> Open in a new tab</Anchor>

                  </>

                )}

                {s?.expected_result && <div className="rpt-kv"><span>Expected result</span><p>{s.expected_result}</p></div>}

                {values.length > 0 && (

                  <div className="rpt-kv"><span>Test data used</span><p>{values.slice(0, 8).map(([k, v]) => `${k.replace(/_/g, ' ')}: ${v}`).join(' · ')}</p></div>

                )}

                {video && video.available !== false && <Anchor href={`/api/evidence/${video.evidence_id}`} target="_blank" rel="noopener" size="xs">Watch the recording</Anchor>}

              </article>

            );

          })}

        </SimpleGrid>

        {shown.length === 0 && <Text size="sm" c="dimmed">No screens match this filter.</Text>}

        <Pager total={shown.length} limit={PAGE} offset={offset} onChange={setOffset} />

        {oneColumn && <Text size="xs" c="dimmed">{where}</Text>}

        <Modal opened={!!big} onClose={() => setBig(null)} title={big ? `Screen: ${big.title}` : ''} size="xl" centered>

          {big && <img src={big.src} alt={`final screen of ${big.title}`} className="rpt-big" />}

          {big && <Anchor href={big.src} target="_blank" rel="noopener" size="sm" className="rpt-ext"><IconExternalLink size={12} aria-hidden /> Open in a new tab</Anchor>}

        </Modal>

      </Stack>
    </Can>
  );
}