Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

report.ts

90 lines4.1 KB

1// @effect-diagnostics nodeBuiltinImport:off - Writes a static HTML report.
2​
3import * as NodePath from "node:path";
4​
5import type { StepResult } from "./journey.ts";
6import type { Pose } from "./poses.ts";
7​
8export interface Finding {
9 readonly step: string;
10 readonly issue: string;
11 readonly severity: "high" | "medium" | "low";
13​
14export interface PoseResult {
15 readonly pose: Pose;
16 readonly steps: ReadonlyArray<StepResult>;
17 readonly error?: string;
18 /** Screenshot taken when the journey stopped early. */
19 readonly stopped?: string;
20 readonly findings?: ReadonlyArray<Finding>;
22​
23export const failedChecks = (result: PoseResult) =>
24 result.steps.flatMap((step) =>
25 step.checks.filter((item) => !item.pass).map((item) => ({ step: step.name, ...item })),
26 );
27​
28export const poseFailed = (result: PoseResult) =>
29 result.error !== undefined || failedChecks(result).length > 0;
30​
31const escapeHtml = (value: string) =>
32 value.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
33​
34const describePose = (pose: Pose) =>
35 [
36 pose.device,
37 pose.appearance,
38 pose.landscape ? "landscape" : "",
39 pose.fold ? `fold ${pose.fold}` : "",
40 pose.textSize ? "large text" : "",
41 ]
42 .filter(Boolean)
43 .join(" · ");
44​
45export function renderReport(results: ReadonlyArray<PoseResult>, outputDirectory: string): string {
46 const relative = (path: string | undefined) =>
47 path ? escapeHtml(NodePath.relative(outputDirectory, path)) : "";
48 const rows = results
49 .map((result) => {
50 const cells = result.steps
51 .map((step) => {
52 const failed = step.checks.filter((item) => !item.pass);
53 const findings = (result.findings ?? []).filter((finding) => finding.step === step.name);
54 const notes = [
55 ...failed.map(
56 (item) =>
57 `<li class="fail">✗ ${escapeHtml(item.name)} <small>${escapeHtml(item.detail)}</small></li>`,
58 ),
59 ...findings.map(
60 (finding) =>
61 `<li class="review ${finding.severity}">◆ ${escapeHtml(finding.issue)}</li>`,
62 ),
63 ].join("");
64 const passed = step.checks.length - failed.length;
65 const media = [
66 step.screenshot
67 ? `<a href="${relative(step.screenshot)}"><img src="${relative(step.screenshot)}"></a>`
68 : "",
69 step.lagFrame
70 ? `<a href="${relative(step.lagFrame)}"><img class="lag" src="${relative(step.lagFrame)}"></a>`
71 : "",
72 step.video ? `<a href="${relative(step.video)}">video</a>` : "",
73 ].join("");
74 return `<td class="${failed.length ? "bad" : ""}"><h3>${escapeHtml(step.name)}</h3>${media}<small class="pass">${passed}/${step.checks.length} checks</small><ul>${notes}</ul></td>`;
75 })
76 .join("");
77 const error = result.error
78 ? `<td class="bad"><h3>stopped</h3>${result.stopped ? `<a href="${relative(result.stopped)}"><img src="${relative(result.stopped)}"></a>` : ""}<p class="fail">${escapeHtml(result.error)}</p></td>`
79 : "";
80 return `<tr><th>${escapeHtml(result.pose.id)}<br><small>${escapeHtml(describePose(result.pose))}</small></th>${cells}${error}</tr>`;
81 })
82 .join("");
83 const failed = results.filter(poseFailed).length;
84 const findings = results.reduce((count, result) => count + (result.findings?.length ?? 0), 0);
85 return `<!doctype html><meta charset="utf-8"><title>Mobile E2E</title>
86<style>body{font:14px system-ui;margin:24px}table{border-collapse:collapse}td,th{border:1px solid #ddd;padding:8px;vertical-align:top;text-align:left}td{width:190px}td.bad{background:#fff5f4}h3{margin:0 0 6px;font-size:13px}img{width:180px;display:block;margin:0 0 6px}img.lag{outline:3px solid #b42318}.pass{color:#16794a}.fail{color:#b42318}.review{color:#7a4b00}.review.high{color:#b42318}ul{padding-left:16px;margin:4px 0}small{color:#666}</style>
87<h1>Mobile E2E · ${results.length} poses · ${failed === 0 ? "all checks passed" : `${failed} with failing checks`} · ${findings} review findings</h1>
88<p><span class="fail">✗</span> failed check <span class="review">◆</span> agent review finding</p>
89<table>${rows}</table>`;