trykeyline-browser 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +14 -2
- package/audit/probe.mjs +390 -0
- package/audit.mjs +209 -0
- package/bin/trykeyline-browser.mjs +79 -0
- package/browse/args.mjs +44 -0
- package/browse/capture.mjs +181 -0
- package/browse/compare.mjs +260 -0
- package/browse/findings.mjs +125 -0
- package/browse/playwright.mjs +78 -0
- package/browse/report.mjs +265 -0
- package/browse/routes.mjs +252 -0
- package/browse/serve.mjs +61 -0
- package/browse/walk.mjs +117 -0
- package/browse.mjs +175 -0
- package/package.json +53 -4
- package/probe.mjs +26 -0
- package/smoke/clicks.mjs +320 -0
- package/smoke/config.mjs +184 -0
- package/smoke/forms.mjs +329 -0
- package/smoke/keyboard.mjs +100 -0
- package/smoke/links.mjs +120 -0
- package/smoke/page.mjs +575 -0
- package/smoke/phone.mjs +126 -0
- package/smoke/shift.mjs +103 -0
- package/smoke/util.mjs +113 -0
- package/smoke.mjs +412 -0
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One format for everything Keyline reports about a running product: the
|
|
3
|
+
* audit, the smoke test, a plain capture. Each run is a folder:
|
|
4
|
+
*
|
|
5
|
+
* keyline-report/
|
|
6
|
+
* findings.json the run, below
|
|
7
|
+
* index.html the report, rendered from findings.json by report.mjs
|
|
8
|
+
* shots/ one PNG per page × viewport × theme
|
|
9
|
+
*
|
|
10
|
+
* findings.json:
|
|
11
|
+
*
|
|
12
|
+
* {
|
|
13
|
+
* "version": 1,
|
|
14
|
+
* "kind": "audit" | "smoke" | "capture",
|
|
15
|
+
* "title": "Lumen", "base": "http://localhost:3000", "root": "/abs/project",
|
|
16
|
+
* "started": ISO, "finished": ISO,
|
|
17
|
+
* "scores": { "type": 3, … } | null, // 0–4 per domain, audit only
|
|
18
|
+
* "summary": "one paragraph a person reads first" | null,
|
|
19
|
+
* "pages": [ { "route": "/settings", "url": …, "title": …, "status": 200,
|
|
20
|
+
* "shots": { "desktop-light": { "path": "shots/…png", "width": 1440, "height": 3120 } } } ],
|
|
21
|
+
* "findings": [ Finding ]
|
|
22
|
+
* }
|
|
23
|
+
*
|
|
24
|
+
* A Finding (only `rule` and `title` are required):
|
|
25
|
+
*
|
|
26
|
+
* id stable within the run, e.g. "f12"
|
|
27
|
+
* kind "audit" | "smoke" | "capture" | "check"
|
|
28
|
+
* domain type | color | layout | depth | motion | patterns | a11y | slop | flow | copy | states | runtime | visual
|
|
29
|
+
* rule "a11y/name", "flow/dead-click", "slop/purple-hero" …
|
|
30
|
+
* law "patterns law 1" — the law it comes from, when there is one
|
|
31
|
+
* severity "error" | "warning" | "info"
|
|
32
|
+
* title one line a person understands without the rule id
|
|
33
|
+
* detail what's wrong, what the reader feels, what to do
|
|
34
|
+
* evidence the measurement: "2.36:1", "click changed nothing", "404"
|
|
35
|
+
* page the route it was seen on
|
|
36
|
+
* viewport "desktop" | "phone"
|
|
37
|
+
* theme "light" | "dark"
|
|
38
|
+
* shot the key of the page's shot it was seen in, e.g. "desktop-light"
|
|
39
|
+
* box { x, y, w, h } in CSS pixels on that full-page shot — drawn in the report
|
|
40
|
+
* selector a CSS selector for the element
|
|
41
|
+
* file source file, relative to the project root, and `line`
|
|
42
|
+
* fix the command that fixes it: "/keyline color", "/keyline polish --from keyline-report"
|
|
43
|
+
* steps smoke only: the steps that reproduce it, in order
|
|
44
|
+
* status "open" (default) | "accepted" | "dismissed" | "fixed" — set by a person, the auditing agent, or polish --from
|
|
45
|
+
*
|
|
46
|
+
* Compared with an earlier run (`report --compare`, see compare.mjs), each
|
|
47
|
+
* finding also has `change` ("new" | "still" | "returned"), the run has
|
|
48
|
+
* `compare` (the counts) and `fixed` (the earlier run's findings that are
|
|
49
|
+
* gone), and each page has `diffs`, its screenshot diffs by shot key.
|
|
50
|
+
*/
|
|
51
|
+
|
|
52
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
53
|
+
import { join } from "node:path";
|
|
54
|
+
import { SEVERITIES, bySeverity, tally } from "trykeyline/check/findings";
|
|
55
|
+
import { DOMAINS as LAW_DOMAINS } from "trykeyline/check/rules";
|
|
56
|
+
|
|
57
|
+
/** The six law domains, and what only a rendered page shows. */
|
|
58
|
+
export const DOMAINS = [...LAW_DOMAINS, "a11y", "copy", "states", "slop", "flow", "runtime", "visual"];
|
|
59
|
+
|
|
60
|
+
export function newRun({ kind, title = null, base = null, root = null }) {
|
|
61
|
+
return {
|
|
62
|
+
version: 1,
|
|
63
|
+
kind,
|
|
64
|
+
title,
|
|
65
|
+
base,
|
|
66
|
+
root,
|
|
67
|
+
started: new Date().toISOString(),
|
|
68
|
+
finished: null,
|
|
69
|
+
scores: null,
|
|
70
|
+
summary: null,
|
|
71
|
+
pages: [],
|
|
72
|
+
findings: [],
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Add a finding, filling defaults and an id. Returns it. */
|
|
77
|
+
export function add(run, f) {
|
|
78
|
+
if (!f.rule || !f.title) throw new Error(`a finding needs a rule and a title: ${JSON.stringify(f)}`);
|
|
79
|
+
const finding = {
|
|
80
|
+
id: f.id ?? `f${run.findings.length + 1}`,
|
|
81
|
+
kind: f.kind ?? run.kind,
|
|
82
|
+
domain: f.domain ?? f.rule.split("/")[0],
|
|
83
|
+
severity: SEVERITIES.includes(f.severity) ? f.severity : "warning",
|
|
84
|
+
status: f.status ?? "open",
|
|
85
|
+
...f,
|
|
86
|
+
};
|
|
87
|
+
run.findings.push(finding);
|
|
88
|
+
return finding;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** The same problem seen on many pages is one finding per page, but not twice on one page. */
|
|
92
|
+
export function dedupe(findings) {
|
|
93
|
+
const seen = new Set();
|
|
94
|
+
return findings.filter((f) => {
|
|
95
|
+
const key = [f.rule, f.page, f.viewport, f.theme, f.selector ?? f.title].join("|");
|
|
96
|
+
if (seen.has(key)) return false;
|
|
97
|
+
seen.add(key);
|
|
98
|
+
return true;
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function sortFindings(findings) {
|
|
103
|
+
return [...findings].sort((a, b) => bySeverity(a, b) || String(a.page).localeCompare(String(b.page)) || a.rule.localeCompare(b.rule));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Open findings by severity and domain: dismissed and fixed ones don't count. */
|
|
107
|
+
export function counts(findings) {
|
|
108
|
+
return tally(findings, { skip: (f) => f.status === "dismissed" || f.status === "fixed" });
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function writeRun(dir, run) {
|
|
112
|
+
mkdirSync(dir, { recursive: true });
|
|
113
|
+
run.finished ??= new Date().toISOString();
|
|
114
|
+
run.findings = sortFindings(dedupe(run.findings)).map((f, i) => ({ ...f, id: `f${i + 1}` }));
|
|
115
|
+
writeFileSync(join(dir, "findings.json"), JSON.stringify(run, null, 2) + "\n");
|
|
116
|
+
return run;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function readRun(dir) {
|
|
120
|
+
const file = join(dir, "findings.json");
|
|
121
|
+
if (!existsSync(file)) throw new Error(`no findings.json in ${dir}`);
|
|
122
|
+
const run = JSON.parse(readFileSync(file, "utf8"));
|
|
123
|
+
if (run.version !== 1) throw new Error(`findings.json version ${run.version} isn't one this Keyline reads`);
|
|
124
|
+
return run;
|
|
125
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Playwright, only when a browser is needed. It's an optional peer of this
|
|
3
|
+
* package, never a dependency: the scripts that drive a page load it from the
|
|
4
|
+
* project if it has it, or from beside this package, else from Keyline's own
|
|
5
|
+
* cache, installing it there once (`~/.cache/keyline`, or $KEYLINE_CACHE) at
|
|
6
|
+
* the version below. The browser is the installed Chrome when there is one,
|
|
7
|
+
* so nothing large is downloaded; Playwright's own Chromium otherwise.
|
|
8
|
+
*
|
|
9
|
+
* An agent with its own browser tools (Claude's browser, a Playwright MCP) can
|
|
10
|
+
* skip all of this: the commands say how to walk the pages with those tools
|
|
11
|
+
* and write findings.json by hand, then render it with `keyline report`.
|
|
12
|
+
*/
|
|
13
|
+
import { execFileSync } from "node:child_process";
|
|
14
|
+
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
|
|
15
|
+
import { createRequire } from "node:module";
|
|
16
|
+
import { homedir } from "node:os";
|
|
17
|
+
import { dirname, join } from "node:path";
|
|
18
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
19
|
+
import { runNpm } from "trykeyline/npm";
|
|
20
|
+
|
|
21
|
+
export const CACHE = process.env.KEYLINE_CACHE?.trim() || join(homedir(), ".cache", "keyline");
|
|
22
|
+
/** What a first run installs into the cache: one exact version, so every machine drives the same Playwright. */
|
|
23
|
+
export const PLAYWRIGHT_VERSION = "1.65.0";
|
|
24
|
+
const HERE = dirname(fileURLToPath(import.meta.url));
|
|
25
|
+
|
|
26
|
+
function resolveFrom(base) {
|
|
27
|
+
try {
|
|
28
|
+
return createRequire(join(base, "noop.js")).resolve("playwright");
|
|
29
|
+
} catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async function importFrom(path) {
|
|
35
|
+
const mod = await import(pathToFileURL(path).href);
|
|
36
|
+
const pw = mod.chromium ? mod : mod.default;
|
|
37
|
+
if (!pw?.chromium) throw new Error(`${path} didn't export chromium`);
|
|
38
|
+
return { pw, dir: dirname(path) };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Find Playwright, or install it into the cache. `install: false` throws instead. */
|
|
42
|
+
export async function loadPlaywright({ root = process.cwd(), install = true, log = (m) => process.stderr.write(m + "\n") } = {}) {
|
|
43
|
+
for (const base of [root, HERE, CACHE]) {
|
|
44
|
+
const found = resolveFrom(base);
|
|
45
|
+
if (found) return importFrom(found);
|
|
46
|
+
}
|
|
47
|
+
if (!install) {
|
|
48
|
+
throw new Error("Playwright isn't installed. Run with install allowed, or `npm i -D playwright` in the project.");
|
|
49
|
+
}
|
|
50
|
+
log(`Keyline drives a browser with Playwright. Installing it once into ${CACHE} …`);
|
|
51
|
+
mkdirSync(CACHE, { recursive: true });
|
|
52
|
+
const pkg = join(CACHE, "package.json");
|
|
53
|
+
if (!existsSync(pkg)) writeFileSync(pkg, JSON.stringify({ name: "keyline-cache", private: true }) + "\n");
|
|
54
|
+
// npm is npm.cmd on Windows, which Node only spawns through a shell.
|
|
55
|
+
const r = runNpm("npm", ["install", "--no-audit", "--no-fund", "--prefix", CACHE, `playwright@${PLAYWRIGHT_VERSION}`], { stdio: ["ignore", "inherit", "inherit"] });
|
|
56
|
+
if (r.error || r.status !== 0) throw new Error(`couldn't install Playwright into ${CACHE}${r.error ? `: ${r.error.message}` : ""}. Install it in the project instead: npm i -D playwright`);
|
|
57
|
+
const found = resolveFrom(CACHE);
|
|
58
|
+
if (!found) throw new Error(`installed Playwright but can't resolve it from ${CACHE}`);
|
|
59
|
+
return importFrom(found);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Chrome if it's installed, Playwright's Chromium if not (downloaded once). `install: false` never installs anything: it throws instead. */
|
|
63
|
+
export async function launch({ root, headless = true, log, install = true } = {}) {
|
|
64
|
+
const { pw, dir } = await loadPlaywright({ root, log, install });
|
|
65
|
+
try {
|
|
66
|
+
return await pw.chromium.launch({ headless, channel: "chrome" });
|
|
67
|
+
} catch {
|
|
68
|
+
// No installed Chrome; fall through to Playwright's own build.
|
|
69
|
+
}
|
|
70
|
+
try {
|
|
71
|
+
return await pw.chromium.launch({ headless });
|
|
72
|
+
} catch (err) {
|
|
73
|
+
if (!install) throw err;
|
|
74
|
+
(log ?? ((m) => process.stderr.write(m + "\n")))("Downloading Playwright's Chromium once …");
|
|
75
|
+
execFileSync(process.execPath, [join(dir, "cli.js"), "install", "chromium"], { stdio: ["ignore", "inherit", "inherit"] });
|
|
76
|
+
return pw.chromium.launch({ headless });
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Render a run (findings.json) as one HTML page beside it: index.html, with
|
|
3
|
+
* the shots folder next to it. No dependencies and no network: the page opens
|
|
4
|
+
* from disk, prints, and can be published as-is.
|
|
5
|
+
*
|
|
6
|
+
* Each finding shows a crop of the screenshot it was seen in with the element
|
|
7
|
+
* outlined. The crop is CSS: the shot is a background image sized and offset
|
|
8
|
+
* so only the region around the box shows, which keeps the report folder to
|
|
9
|
+
* one PNG per page however many findings point into it.
|
|
10
|
+
*/
|
|
11
|
+
import { writeFileSync } from "node:fs";
|
|
12
|
+
import { join } from "node:path";
|
|
13
|
+
import { counts, readRun, sortFindings } from "./findings.mjs";
|
|
14
|
+
|
|
15
|
+
const esc = (s) => String(s ?? "").replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[c]);
|
|
16
|
+
|
|
17
|
+
const DOMAIN_NAMES = {
|
|
18
|
+
type: "Type",
|
|
19
|
+
color: "Colour",
|
|
20
|
+
layout: "Layout",
|
|
21
|
+
depth: "Depth",
|
|
22
|
+
motion: "Motion",
|
|
23
|
+
patterns: "Patterns",
|
|
24
|
+
a11y: "Accessibility",
|
|
25
|
+
copy: "Copy",
|
|
26
|
+
states: "States",
|
|
27
|
+
slop: "AI slop",
|
|
28
|
+
flow: "Flows",
|
|
29
|
+
runtime: "Runtime",
|
|
30
|
+
visual: "Visual change",
|
|
31
|
+
};
|
|
32
|
+
const CHANGE_WORDS = { new: "New", returned: "Back", still: "Still" };
|
|
33
|
+
const KIND_TITLES = { audit: "Audit", smoke: "Smoke test", capture: "Capture", check: "Detector" };
|
|
34
|
+
const SCORE_WORDS = ["Broken", "Weak", "Mixed", "Good", "Excellent"];
|
|
35
|
+
const PAD = 72;
|
|
36
|
+
|
|
37
|
+
/** The crop around a box, clamped to the shot. All numbers in shot pixels. */
|
|
38
|
+
function crop(box, shot) {
|
|
39
|
+
// Wide enough that the report's 832px column doesn't enlarge it much, tall enough to show context.
|
|
40
|
+
const w = Math.min(shot.width, Math.max(box.w + PAD * 2, 880));
|
|
41
|
+
const h = Math.min(shot.height, Math.max(box.h + PAD * 2, Math.round(w * 0.4)));
|
|
42
|
+
const x = Math.max(0, Math.min(box.x + box.w / 2 - w / 2, shot.width - w));
|
|
43
|
+
const y = Math.max(0, Math.min(box.y + box.h / 2 - h / 2, shot.height - h));
|
|
44
|
+
return { x, y, w, h };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function figure(f, page) {
|
|
48
|
+
const shot = f.shot && page?.shots?.[f.shot];
|
|
49
|
+
if (!shot) return "";
|
|
50
|
+
// An element outside the shot (scrolled away in a strip) can't be outlined; show the page instead.
|
|
51
|
+
if (!f.box || f.box.x >= shot.width || f.box.x + f.box.w <= 0 || f.box.y >= shot.height) {
|
|
52
|
+
return `<a class="shot shot--whole" href="${esc(shot.path)}" target="_blank" rel="noopener"><img src="${esc(shot.path)}" alt="" loading="lazy"></a>`;
|
|
53
|
+
}
|
|
54
|
+
const c = crop(f.box, shot);
|
|
55
|
+
const pct = (n, d) => ((n / d) * 100).toFixed(3) + "%";
|
|
56
|
+
const bgSize = `${pct(shot.width, c.w)} auto`;
|
|
57
|
+
const bgPos = `${c.w >= shot.width ? "0%" : pct(c.x, shot.width - c.w)} ${c.h >= shot.height ? "0%" : pct(c.y, shot.height - c.h)}`;
|
|
58
|
+
const mark = `left:${pct(f.box.x - c.x, c.w)};top:${pct(f.box.y - c.y, c.h)};width:${pct(f.box.w, c.w)};height:${pct(f.box.h, c.h)}`;
|
|
59
|
+
// Never wider than 1.25× the pixels it has, so a phone crop stays sharp.
|
|
60
|
+
return `<a class="shot" href="${esc(shot.path)}" target="_blank" rel="noopener" style="max-width:${Math.round(c.w * 1.25)}px;aspect-ratio:${c.w}/${c.h};background-image:url('${esc(shot.path)}');background-size:${bgSize};background-position:${bgPos}" aria-label="Open the full screenshot"><span class="shot__mark" style="${mark}"></span></a>`;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** A change can span the whole page; the crop shows where it starts, and the full shot and diff are a click away. */
|
|
64
|
+
const VISUAL_CROP = 560;
|
|
65
|
+
const firstPart = (box) => (box && box.h > VISUAL_CROP ? { ...box, h: VISUAL_CROP } : box);
|
|
66
|
+
|
|
67
|
+
/** A visual change shows the diff under the crop: the same region, changed pixels in red. */
|
|
68
|
+
function diffFigure(f) {
|
|
69
|
+
if (!f.diff?.path) return "";
|
|
70
|
+
const box = firstPart(f.diff.box ?? f.box);
|
|
71
|
+
const shown = figure({ ...f, shot: "diff", box }, { shots: { diff: { path: f.diff.path, width: f.diff.width, height: f.diff.height } } });
|
|
72
|
+
return `<p class="f__caption">The diff: red is what changed.</p>${shown}`;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function card(f, page) {
|
|
76
|
+
const where = [f.page, f.viewport, f.theme].filter(Boolean).join(" · ");
|
|
77
|
+
const source = f.file ? `${f.file}${f.line ? `:${f.line}` : ""}` : "";
|
|
78
|
+
const change = f.rule?.startsWith("visual/") ? "changed" : f.change;
|
|
79
|
+
const badge = change === "changed" ? `<span class="chg chg--changed">Changed</span>` : CHANGE_WORDS[change] ? `<span class="chg chg--${esc(change)}">${CHANGE_WORDS[change]}</span>` : "";
|
|
80
|
+
return `<article class="f" data-severity="${esc(f.severity)}" data-domain="${esc(f.domain)}" data-page="${esc(f.page ?? "")}" data-status="${esc(f.status ?? "open")}" data-change="${esc(change ?? "")}" id="${esc(f.id)}">
|
|
81
|
+
<header class="f__head">
|
|
82
|
+
<span class="sev sev--${esc(f.severity)}">${esc(f.severity)}</span>${badge}
|
|
83
|
+
<h3 class="f__title">${esc(f.title)}</h3>
|
|
84
|
+
</header>
|
|
85
|
+
<p class="f__meta"><span>${esc(DOMAIN_NAMES[f.domain] ?? f.domain)}</span><code>${esc(f.rule)}</code>${f.law ? `<span>${esc(f.law)}</span>` : ""}${where ? `<span>${esc(where)}</span>` : ""}</p>
|
|
86
|
+
${f.detail ? `<p class="f__detail">${esc(f.detail)}</p>` : ""}
|
|
87
|
+
${f.evidence ? `<p class="f__evidence"><span>Measured</span> ${esc(f.evidence)}</p>` : ""}
|
|
88
|
+
${f.steps?.length ? `<ol class="f__steps">${f.steps.map((s) => `<li>${esc(s)}</li>`).join("")}</ol>` : ""}
|
|
89
|
+
${figure(f.diff ? { ...f, box: firstPart(f.box) } : f, page)}
|
|
90
|
+
${diffFigure(f)}
|
|
91
|
+
<dl class="f__facts">
|
|
92
|
+
${f.selector ? `<div><dt>Element</dt><dd><code>${esc(f.selector)}</code></dd></div>` : ""}
|
|
93
|
+
${source ? `<div><dt>Source</dt><dd><code>${esc(source)}</code></dd></div>` : ""}
|
|
94
|
+
${f.fix ? `<div><dt>Fix with</dt><dd><code>${esc(f.fix)}</code></dd></div>` : ""}
|
|
95
|
+
</dl>
|
|
96
|
+
</article>`;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function scoreTable(scores, draft) {
|
|
100
|
+
if (!scores) return "";
|
|
101
|
+
const rows = Object.entries(scores)
|
|
102
|
+
.map(([d, s]) => `<tr><th scope="row">${esc(DOMAIN_NAMES[d] ?? d)}</th><td><span class="meter" style="--v:${Number(s) / 4}"></span></td><td>${esc(s)}/4</td><td>${esc(SCORE_WORDS[s] ?? "")}</td></tr>`)
|
|
103
|
+
.join("");
|
|
104
|
+
return `<section class="block"><h2>Scores${draft ? ` <small class="draft">drafted from the measurements; not yet reviewed</small>` : ""}</h2><table class="scores"><tbody>${rows}</tbody></table></section>`;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function sinceSection(run) {
|
|
108
|
+
const c = run.compare;
|
|
109
|
+
if (!c) return "";
|
|
110
|
+
const when = c.previous?.finished ? new Date(c.previous.finished).toLocaleString("en-GB", { dateStyle: "medium", timeStyle: "short" }) : "an earlier run";
|
|
111
|
+
const v = c.visual;
|
|
112
|
+
const total = (n, label) => `<li><b>${n}</b><span>${label}</span></li>`;
|
|
113
|
+
const fixed = run.fixed ?? [];
|
|
114
|
+
const fixedList = fixed.length
|
|
115
|
+
? `<h3>Fixed</h3><ul class="since__list">${fixed
|
|
116
|
+
.map((f) => `<li><span class="sev sev--${esc(f.severity)}">${esc(f.severity)}</span> ${esc(f.title)} <span class="since__where"><code>${esc(f.rule)}</code> ${esc([f.page, f.viewport, f.theme].filter(Boolean).join(" · "))}</span></li>`)
|
|
117
|
+
.join("")}</ul>`
|
|
118
|
+
: "";
|
|
119
|
+
const diffs = run.pages.flatMap((p) => Object.entries(p.diffs ?? {}).map(([key, d]) => ({ route: p.route, key, ...d })));
|
|
120
|
+
diffs.sort((a, b) => b.percent - a.percent);
|
|
121
|
+
const diffRows = diffs.length
|
|
122
|
+
? `<h3>Screenshots</h3><table class="since__diffs"><tbody>${diffs
|
|
123
|
+
.map((d) => `<tr${d.percent > (v?.threshold ?? 0) ? "" : ` class="same"`}><th scope="row">${esc(d.route)}</th><td>${esc(d.key.replace("-", " · "))}</td><td>${d.percent > (v?.threshold ?? 0) ? `${esc(d.percent)}% changed` : d.percent > 0 ? `${esc(d.percent)}%, under the threshold` : "unchanged"}</td><td><a href="${esc(d.path)}" target="_blank" rel="noopener">diff</a></td></tr>`)
|
|
124
|
+
.join("")}</tbody></table>`
|
|
125
|
+
: v?.skipped
|
|
126
|
+
? `<p class="since__note">Screenshots weren't diffed: ${esc(v.skipped)}</p>`
|
|
127
|
+
: "";
|
|
128
|
+
return `<section class="block since"><h2>Since last run</h2>
|
|
129
|
+
<p class="lede">Against <code>${esc(c.against ?? "the previous report")}</code>, ${esc(when)}.</p>
|
|
130
|
+
<ul class="totals">${total(c.new, "New")}${total(c.returned, "Back again")}${total(c.still, "Still open")}${total(c.fixed, "Fixed")}${v ? total(v.changed, v.changed === 1 ? "Page changed" : "Pages changed") : ""}</ul>
|
|
131
|
+
${fixedList}${diffRows}</section>`;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const STYLE = `
|
|
135
|
+
:root{color-scheme:light dark;--bg:#fff;--fg:#171717;--muted:#5e5e5e;--faint:#737373;--line:rgba(0,0,0,.09);--chip:#f2f2f2;--err:#b42318;--err-bg:#fdecea;--warn:#8a5a00;--warn-bg:#fff4dc;--info:#1d4ed8;--info-bg:#e8efff;--mark:#c4321f}
|
|
136
|
+
@media (prefers-color-scheme:dark){:root{--bg:#0b0b0b;--fg:#ededed;--muted:#a0a0a0;--faint:#8f8f8f;--line:rgba(255,255,255,.1);--chip:#1a1a1a;--err:#ff8a80;--err-bg:#3a1512;--warn:#f5c26b;--warn-bg:#33270d;--info:#93b4ff;--info-bg:#152040;--mark:#ff6b57}}
|
|
137
|
+
*{box-sizing:border-box}
|
|
138
|
+
body{margin:0;background:var(--bg);color:var(--fg);font:15px/1.55 ui-sans-serif,system-ui,-apple-system,"Helvetica Neue",sans-serif;-webkit-font-smoothing:antialiased}
|
|
139
|
+
code{font:13px/1.4 ui-monospace,"SF Mono",Menlo,monospace}
|
|
140
|
+
.wrap{max-width:880px;margin:0 auto;padding:64px 24px 96px}
|
|
141
|
+
h1{margin:0;font-size:28px;font-weight:500;letter-spacing:-.02em;line-height:1.2}
|
|
142
|
+
h2{margin:0 0 12px;font-size:16px;font-weight:500;letter-spacing:-.015em}
|
|
143
|
+
.lede{margin:8px 0 0;color:var(--muted)}
|
|
144
|
+
.summary{margin:24px 0 0;max-width:62ch}
|
|
145
|
+
.totals{display:flex;flex-wrap:wrap;gap:24px;margin:32px 0 0;padding:0;list-style:none}
|
|
146
|
+
.totals b{display:block;font-size:20px;font-weight:500;font-variant-numeric:tabular-nums}
|
|
147
|
+
.totals span{font-size:13px;color:var(--faint)}
|
|
148
|
+
.block{margin-top:48px}
|
|
149
|
+
.scores{border-collapse:collapse;width:100%;font-size:14px}
|
|
150
|
+
.scores th,.scores td{padding:8px 12px 8px 0;border-top:.5px solid var(--line);text-align:left;font-weight:400}
|
|
151
|
+
.scores td:nth-child(3){font-variant-numeric:tabular-nums;color:var(--muted);width:4em}
|
|
152
|
+
.scores td:last-child{color:var(--faint);width:7em}
|
|
153
|
+
.meter{display:block;height:6px;border-radius:9px;background:var(--chip);position:relative;overflow:hidden;min-width:120px}
|
|
154
|
+
.meter::after{content:"";position:absolute;inset:0;width:calc(var(--v)*100%);background:var(--fg);border-radius:inherit}
|
|
155
|
+
.filters{position:sticky;top:0;z-index:2;display:flex;flex-wrap:wrap;gap:8px;margin:48px -24px 0;padding:12px 24px;background:var(--bg);border-bottom:.5px solid var(--line)}
|
|
156
|
+
.filters select,.filters button{font:inherit;font-size:13px;color:var(--fg);background:var(--chip);border:0;border-radius:10px;padding:6px 10px;cursor:pointer}
|
|
157
|
+
.filters .count{margin-left:auto;font-size:13px;color:var(--faint);align-self:center}
|
|
158
|
+
.page-group{margin-top:40px}
|
|
159
|
+
.page-group>h2{display:flex;gap:12px;align-items:baseline}
|
|
160
|
+
.page-group>h2 small{font-size:13px;font-weight:400;color:var(--faint)}
|
|
161
|
+
.f{padding:24px 0;border-top:.5px solid var(--line)}
|
|
162
|
+
.f[hidden]{display:none}
|
|
163
|
+
.f__head{display:flex;gap:12px;align-items:baseline}
|
|
164
|
+
.f__title{margin:0;font-size:16px;font-weight:500;letter-spacing:-.01em}
|
|
165
|
+
.sev{flex:none;font-size:12px;padding:1px 8px;border-radius:999px}
|
|
166
|
+
.sev--error{color:var(--err);background:var(--err-bg)}
|
|
167
|
+
.sev--warning{color:var(--warn);background:var(--warn-bg)}
|
|
168
|
+
.sev--info{color:var(--info);background:var(--info-bg)}
|
|
169
|
+
.f__meta{display:flex;flex-wrap:wrap;gap:4px 14px;margin:6px 0 0;font-size:13px;color:var(--faint)}
|
|
170
|
+
.f__detail{margin:10px 0 0;max-width:68ch;color:var(--muted);font-size:14px}
|
|
171
|
+
.f__evidence{margin:8px 0 0;font-size:14px}
|
|
172
|
+
.f__evidence span{color:var(--faint)}
|
|
173
|
+
.f__steps{margin:10px 0 0;padding-left:20px;font-size:14px;color:var(--muted)}
|
|
174
|
+
.shot{position:relative;display:block;margin-top:16px;border-radius:12px;border:.5px solid var(--line);background-repeat:no-repeat;overflow:hidden;max-height:420px}
|
|
175
|
+
.shot--whole img{display:block;width:100%;height:auto}
|
|
176
|
+
.shot__mark{position:absolute;outline:2px solid var(--mark);outline-offset:2px;border-radius:4px;box-shadow:0 0 0 9999px rgba(0,0,0,.18)}
|
|
177
|
+
.f__facts{display:grid;gap:4px;margin:14px 0 0;font-size:13px}
|
|
178
|
+
.f__facts div{display:flex;gap:12px}
|
|
179
|
+
.f__facts dt{width:6em;flex:none;color:var(--faint)}
|
|
180
|
+
.f__facts dd{margin:0;min-width:0;overflow-wrap:anywhere}
|
|
181
|
+
.empty{margin-top:48px;color:var(--muted)}
|
|
182
|
+
.draft{font-size:13px;font-weight:400;color:var(--faint);margin-left:8px}
|
|
183
|
+
.chg{flex:none;font-size:12px;padding:0 8px;border-radius:999px;border:.5px solid currentColor}
|
|
184
|
+
.chg--new{color:var(--err)}
|
|
185
|
+
.chg--returned{color:var(--warn)}
|
|
186
|
+
.chg--still{color:var(--faint)}
|
|
187
|
+
.chg--changed{color:var(--info)}
|
|
188
|
+
.f__caption{margin:12px 0 0;font-size:13px;color:var(--faint)}
|
|
189
|
+
.since h3{margin:28px 0 8px;font-size:14px;font-weight:500}
|
|
190
|
+
.since__list{margin:0;padding:0;list-style:none;font-size:14px}
|
|
191
|
+
.since__list li{display:flex;flex-wrap:wrap;gap:4px 10px;align-items:baseline;padding:8px 0;border-top:.5px solid var(--line)}
|
|
192
|
+
.since__where{font-size:13px;color:var(--faint)}
|
|
193
|
+
.since__diffs{border-collapse:collapse;width:100%;font-size:14px}
|
|
194
|
+
.since__diffs th,.since__diffs td{padding:8px 12px 8px 0;border-top:.5px solid var(--line);text-align:left;font-weight:400}
|
|
195
|
+
.since__diffs td{font-variant-numeric:tabular-nums;color:var(--muted)}
|
|
196
|
+
.since__diffs tr.same{color:var(--faint)}
|
|
197
|
+
.since__diffs a{color:inherit}
|
|
198
|
+
.since__note{font-size:14px;color:var(--muted)}
|
|
199
|
+
footer{margin-top:64px;font-size:13px;color:var(--faint)}
|
|
200
|
+
@media print{.filters{display:none}.f{break-inside:avoid}}
|
|
201
|
+
`;
|
|
202
|
+
|
|
203
|
+
const SCRIPT = `
|
|
204
|
+
const sel=(n)=>document.querySelector('[name="'+n+'"]');
|
|
205
|
+
function apply(){const s=sel('severity').value,d=sel('domain').value,p=sel('page').value,c=sel('change')?sel('change').value:'';let n=0;
|
|
206
|
+
document.querySelectorAll('.f').forEach(f=>{const show=(!s||f.dataset.severity===s)&&(!d||f.dataset.domain===d)&&(!p||f.dataset.page===p)&&(!c||f.dataset.change===c);f.hidden=!show;if(show)n++;});
|
|
207
|
+
document.querySelectorAll('.page-group').forEach(g=>{g.hidden=!g.querySelector('.f:not([hidden])');});
|
|
208
|
+
document.querySelector('.count').textContent=n+(n===1?' finding':' findings');}
|
|
209
|
+
document.querySelectorAll('.filters select').forEach(x=>x.addEventListener('change',apply));apply();
|
|
210
|
+
`;
|
|
211
|
+
|
|
212
|
+
export function render(run) {
|
|
213
|
+
const findings = sortFindings(run.findings.filter((f) => f.status !== "dismissed" && f.status !== "fixed"));
|
|
214
|
+
const c = counts(findings);
|
|
215
|
+
const pagesByRoute = new Map(run.pages.map((p) => [p.route, p]));
|
|
216
|
+
const groups = new Map();
|
|
217
|
+
for (const f of findings) {
|
|
218
|
+
const key = f.page ?? "Whole product";
|
|
219
|
+
if (!groups.has(key)) groups.set(key, []);
|
|
220
|
+
groups.get(key).push(f);
|
|
221
|
+
}
|
|
222
|
+
const option = (v, label) => `<option value="${esc(v)}">${esc(label)}</option>`;
|
|
223
|
+
const domains = [...new Set(findings.map((f) => f.domain))];
|
|
224
|
+
const title = `${KIND_TITLES[run.kind] ?? "Report"}${run.title ? `: ${run.title}` : ""}`;
|
|
225
|
+
const when = run.finished ? new Date(run.finished) : new Date();
|
|
226
|
+
|
|
227
|
+
const body = findings.length
|
|
228
|
+
? [...groups]
|
|
229
|
+
.map(([route, list]) => {
|
|
230
|
+
const page = pagesByRoute.get(route);
|
|
231
|
+
return `<section class="page-group"><h2>${esc(route)}${page?.title ? ` <small>${esc(page.title)}</small>` : ""}</h2>${list.map((f) => card(f, page)).join("\n")}</section>`;
|
|
232
|
+
})
|
|
233
|
+
.join("\n")
|
|
234
|
+
: `<p class="empty">Nothing found. ${run.pages.length} ${run.pages.length === 1 ? "page" : "pages"} checked.</p>`;
|
|
235
|
+
|
|
236
|
+
return `<!doctype html>
|
|
237
|
+
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
|
238
|
+
<title>${esc(title)}</title><style>${STYLE}</style></head>
|
|
239
|
+
<body><main class="wrap">
|
|
240
|
+
<h1>${esc(title)}</h1>
|
|
241
|
+
<p class="lede">${esc(run.base ?? run.root ?? "")} · ${esc(when.toLocaleString("en-GB", { dateStyle: "medium", timeStyle: "short" }))} · ${run.pages.length} ${run.pages.length === 1 ? "page" : "pages"}</p>
|
|
242
|
+
${run.summary ? `<p class="summary">${esc(run.summary)}</p>` : ""}
|
|
243
|
+
<ul class="totals"><li><b>${c.error}</b><span>Errors</span></li><li><b>${c.warning}</b><span>Warnings</span></li><li><b>${c.info}</b><span>Notes</span></li></ul>
|
|
244
|
+
${sinceSection(run)}
|
|
245
|
+
${scoreTable(run.scores, run.scoresDraft)}
|
|
246
|
+
<nav class="filters" aria-label="Filter findings">
|
|
247
|
+
<select name="severity" aria-label="Severity">${option("", "Every severity")}${option("error", "Errors")}${option("warning", "Warnings")}${option("info", "Notes")}</select>
|
|
248
|
+
<select name="domain" aria-label="Area">${option("", "Every area")}${domains.map((d) => option(d, DOMAIN_NAMES[d] ?? d)).join("")}</select>
|
|
249
|
+
<select name="page" aria-label="Page">${option("", "Every page")}${[...groups.keys()].map((p) => option(p, p)).join("")}</select>
|
|
250
|
+
${run.compare ? `<select name="change" aria-label="Since last run">${option("", "Old and new")}${option("new", "New")}${option("returned", "Back again")}${option("still", "Still open")}${run.compare.visual ? option("changed", "Visual changes") : ""}</select>` : ""}
|
|
251
|
+
<span class="count" aria-live="polite"></span>
|
|
252
|
+
</nav>
|
|
253
|
+
${body}
|
|
254
|
+
<footer>Made by Keyline. The data is in findings.json beside this file; <code>/keyline polish --from ${esc(run.kind === "smoke" ? "smoke-report" : "keyline-report")}</code> fixes what's open.</footer>
|
|
255
|
+
</main><script>${SCRIPT}</script></body></html>
|
|
256
|
+
`;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** Render `dir/findings.json` to `dir/index.html`. Returns the path. */
|
|
260
|
+
export function renderDir(dir) {
|
|
261
|
+
const run = readRun(dir);
|
|
262
|
+
const file = join(dir, "index.html");
|
|
263
|
+
writeFileSync(file, render(run));
|
|
264
|
+
return file;
|
|
265
|
+
}
|