@dcaldeira/tooling 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.
Files changed (56) hide show
  1. package/LICENSE +9 -0
  2. package/README.md +33 -2
  3. package/checks/a11y.mjs +206 -0
  4. package/checks/architecture.mjs +137 -0
  5. package/checks/audit.mjs +121 -0
  6. package/checks/bundle.mjs +126 -0
  7. package/checks/dependencies.mjs +277 -0
  8. package/checks/duplication.mjs +135 -0
  9. package/checks/lint-scss.mjs +363 -0
  10. package/checks/package.mjs +30 -0
  11. package/checks/playwright-summary.mjs +53 -0
  12. package/checks/sass.mjs +67 -0
  13. package/checks/scan-secrets.mjs +58 -0
  14. package/checks/shared.mjs +86 -0
  15. package/checks/style-property-order.json +498 -0
  16. package/checks/translations.mjs +55 -0
  17. package/checks/visual-compare.mjs +148 -0
  18. package/checks/visual-report-html.mjs +470 -0
  19. package/checks/visual-report.mjs +13 -0
  20. package/config.schema.json +977 -0
  21. package/configs/eslint.mjs +27 -0
  22. package/configs/knip.json +11 -0
  23. package/configs/playwright.mjs +15 -0
  24. package/configs/prettier.mjs +1 -0
  25. package/configs/tsconfig.json +16 -0
  26. package/configs/vitest.mjs +16 -0
  27. package/package.json +103 -4
  28. package/release/evidence.mjs +62 -0
  29. package/release/finalize.mjs +65 -0
  30. package/release/gh.mjs +25 -0
  31. package/release/github.mjs +64 -0
  32. package/release/prepare.mjs +30 -0
  33. package/runner/browsers.mjs +73 -0
  34. package/runner/cache.mjs +121 -0
  35. package/runner/ci-plan.mjs +54 -0
  36. package/runner/cli.mjs +251 -0
  37. package/runner/config.mjs +148 -0
  38. package/runner/docs-install.mjs +93 -0
  39. package/runner/execute.mjs +298 -0
  40. package/runner/generate.mjs +122 -0
  41. package/runner/git.mjs +122 -0
  42. package/runner/graph.mjs +250 -0
  43. package/runner/index.mjs +4 -0
  44. package/runner/insights.mjs +313 -0
  45. package/runner/provision.mjs +53 -0
  46. package/runner/repo-standard.mjs +104 -0
  47. package/runner/report.mjs +35 -0
  48. package/runner/serve-static.mjs +76 -0
  49. package/runner/setup.mjs +241 -0
  50. package/runner/summary.mjs +112 -0
  51. package/runner/watch.mjs +82 -0
  52. package/templates/ci.yml +25 -0
  53. package/templates/dependabot-merge.yml +13 -0
  54. package/templates/dependabot.yml +26 -0
  55. package/templates/pre-push +13 -0
  56. package/templates/release.yml +23 -0
@@ -0,0 +1,470 @@
1
+ // Builds the self-contained interactive HTML report: images are embedded, the
2
+ // highlight, slider, and pixel diff run in the browser.
3
+
4
+ const escape = (value) =>
5
+ value.replace(/[&<>"']/g, (char) => `&#${char.charCodeAt(0)};`);
6
+
7
+ const dataUri = (buffer) =>
8
+ `data:image/png;base64,${buffer.toString('base64')}`;
9
+
10
+ const labels = {
11
+ added: 'New: no previous snapshot',
12
+ changed: 'Changed',
13
+ removed: 'Removed: no new snapshot',
14
+ };
15
+
16
+ // Snapshot paths look like `<project>/<name>.png` (desktop or mobile).
17
+ const splitName = (file) => {
18
+ const name = file.replace(/\.png$/, '');
19
+ const at = name.lastIndexOf('/');
20
+ return [name.slice(at + 1), name.slice(0, at)];
21
+ };
22
+
23
+ // Project names decide the device label: anything with "mobile" or "tablet" is flagged.
24
+ const deviceOf = (project) => {
25
+ const lower = project.toLowerCase();
26
+ if (lower.includes('mobile')) return { id: 'mobile', label: 'Mobile' };
27
+ if (lower.includes('tablet')) return { id: 'tablet', label: 'Tablet' };
28
+ return { id: 'desktop', label: 'Desktop' };
29
+ };
30
+
31
+ const icons = {
32
+ mobile:
33
+ '<svg viewBox="0 0 24 24" width="16" height="16" aria-hidden="true"><rect x="7" y="2.5" width="10" height="19" rx="2.5" fill="none" stroke="currentColor" stroke-width="2"/><path d="M10.5 18.5h3" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>',
34
+ tablet:
35
+ '<svg viewBox="0 0 24 24" width="16" height="16" aria-hidden="true"><rect x="4" y="2.5" width="16" height="19" rx="2.5" fill="none" stroke="currentColor" stroke-width="2"/><path d="M10.5 18.5h3" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>',
36
+ desktop:
37
+ '<svg viewBox="0 0 24 24" width="16" height="16" aria-hidden="true"><rect x="2.5" y="4" width="19" height="12.5" rx="2" fill="none" stroke="currentColor" stroke-width="2"/><path d="M8.5 20.5h7M12 16.5v4" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>',
38
+ };
39
+
40
+ const styles = `
41
+ :root {
42
+ color-scheme: light;
43
+ --bg: #f4f5f9; --fg: #181825; --muted: #5d5f73; --border: #dcdde8;
44
+ --surface: #ffffff; --surface-2: #f0f1f7;
45
+ --accent: #6d28d9; --accent-fg: #ffffff; --accent-soft: #ede9fe;
46
+ --added: #15803d; --added-bg: #dcfce7;
47
+ --changed: #a16207; --changed-bg: #fef3c7;
48
+ --removed: #b91c1c; --removed-bg: #fee2e2;
49
+ --desktop: #1d4ed8; --desktop-bg: #dbeafe;
50
+ --tablet: #0f766e; --tablet-bg: #ccfbf1;
51
+ --mobile: #be185d; --mobile-bg: #fce7f3;
52
+ --shadow: 0 1px 2px rgb(20 20 40 / .06), 0 4px 16px rgb(20 20 40 / .05);
53
+ }
54
+ @media (prefers-color-scheme: dark) {
55
+ :root:not([data-theme="light"]) {
56
+ color-scheme: dark;
57
+ --bg: #101018; --fg: #ececf4; --muted: #a3a4b8; --border: #30303f;
58
+ --surface: #1a1a25; --surface-2: #232331;
59
+ --accent: #a78bfa; --accent-fg: #14141c; --accent-soft: #2c2447;
60
+ --added: #4ade80; --added-bg: #14391f;
61
+ --changed: #fbbf24; --changed-bg: #3d2a08;
62
+ --removed: #f87171; --removed-bg: #471414;
63
+ --desktop: #93c5fd; --desktop-bg: #1c2a4d;
64
+ --tablet: #5eead4; --tablet-bg: #0f3a36;
65
+ --mobile: #f9a8d4; --mobile-bg: #4a1a35;
66
+ --shadow: 0 1px 2px rgb(0 0 0 / .4);
67
+ }
68
+ }
69
+ * { box-sizing: border-box; }
70
+ body { margin: 0; padding: 0 0 4rem; background: var(--bg); color: var(--fg); font: 14px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif; }
71
+ .page { max-width: 1180px; margin: 0 auto; padding: 0 max(16px, env(safe-area-inset-left, 0px)); }
72
+ header.top { background: var(--surface); border-bottom: 1px solid var(--border); padding: 1.5rem 0 1.25rem; margin-bottom: 1.25rem; }
73
+ h1 { margin: 0; font-size: 1.5rem; letter-spacing: -.01em; text-wrap: balance; }
74
+ .sub { color: var(--muted); margin: .25rem 0 1rem; }
75
+ code { font-family: ui-monospace, "Cascadia Code", monospace; background: var(--surface-2); padding: .1rem .4rem; border-radius: 4px; font-size: .9em; }
76
+ .totals { display: flex; gap: .75rem; flex-wrap: wrap; }
77
+ .total { min-width: 7.5rem; padding: .6rem 1rem; border-radius: 10px; border: 1px solid var(--border); }
78
+ .total strong { display: block; font-size: 1.6rem; line-height: 1.1; }
79
+ .total span { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
80
+ .total.changed { background: var(--changed-bg); color: var(--changed); border-color: transparent; }
81
+ .total.added { background: var(--added-bg); color: var(--added); border-color: transparent; }
82
+ .total.removed { background: var(--removed-bg); color: var(--removed); border-color: transparent; }
83
+ .controls { display: flex; flex-wrap: wrap; gap: .75rem 1.5rem; align-items: center; margin-bottom: 1rem; }
84
+ .group { display: flex; gap: .35rem; flex-wrap: wrap; align-items: center; }
85
+ .group > .label { color: var(--muted); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; margin-right: .25rem; }
86
+ .bulk { margin-left: auto; }
87
+ button { font: inherit; padding: .3rem .8rem; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); color: var(--fg); cursor: pointer; }
88
+ button:hover { border-color: var(--accent); }
89
+ button:focus-visible, .handle:focus-visible, summary:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
90
+ button[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); }
91
+ .segmented { display: inline-flex; padding: 3px; gap: 2px; background: var(--surface-2); border-radius: 10px; }
92
+ .segmented button { border: 0; background: transparent; border-radius: 7px; }
93
+ .segmented button[aria-pressed="true"] { background: var(--surface); color: var(--accent); box-shadow: var(--shadow); font-weight: 600; }
94
+ .card { background: var(--surface); border: 1px solid var(--border); border-left-width: 4px; border-radius: 12px; margin-bottom: .85rem; box-shadow: var(--shadow); overflow: clip; }
95
+ .card[data-device="mobile"] { border-left-color: var(--mobile); }
96
+ .card[data-device="tablet"] { border-left-color: var(--tablet); }
97
+ .card[data-device="desktop"] { border-left-color: var(--desktop); }
98
+ .card summary { display: flex; align-items: center; gap: .75rem; flex-wrap: wrap; padding: .8rem 1rem; cursor: pointer; list-style: none; }
99
+ .card summary::-webkit-details-marker { display: none; }
100
+ .card summary::before { content: ""; width: .5rem; height: .5rem; border-right: 2px solid var(--muted); border-bottom: 2px solid var(--muted); transform: rotate(-45deg); transition: transform .15s; flex: none; }
101
+ .card[open] summary::before { transform: rotate(45deg); }
102
+ .card h2 { margin: 0; font-size: 1rem; font-family: ui-monospace, "Cascadia Code", monospace; overflow-wrap: anywhere; min-width: 0; }
103
+ .chip { display: inline-flex; align-items: center; gap: .35rem; padding: .1rem .6rem .1rem .5rem; border-radius: 999px; font-size: 12px; font-weight: 700; }
104
+ .chip.mobile { background: var(--mobile-bg); color: var(--mobile); }
105
+ .chip.tablet { background: var(--tablet-bg); color: var(--tablet); }
106
+ .chip.desktop { background: var(--desktop-bg); color: var(--desktop); }
107
+ .badge { padding: .1rem .6rem; border-radius: 999px; font-size: 12px; font-weight: 600; }
108
+ .badge.added { background: var(--added-bg); color: var(--added); }
109
+ .badge.changed { background: var(--changed-bg); color: var(--changed); }
110
+ .badge.failure { background: var(--removed-bg); color: var(--removed); border: 1px solid var(--removed); }
111
+ .badge.removed { background: var(--removed-bg); color: var(--removed); }
112
+ .stats { color: var(--muted); font-size: 12px; margin-left: auto; }
113
+ .body { padding: .25rem 1rem 1.25rem; border-top: 1px solid var(--border); background: var(--surface-2); }
114
+ .toolbar { display: flex; flex-wrap: wrap; gap: .5rem 1rem; align-items: center; justify-content: space-between; padding: .75rem 0; }
115
+ .legend { color: var(--muted); font-size: 12px; }
116
+ .stage { display: grid; justify-items: center; }
117
+ .frame { width: 100%; max-width: var(--frame-max, 100%); background: var(--surface); border: 1px solid var(--border); border-radius: 10px; overflow: clip; box-shadow: var(--shadow); }
118
+ .frame-bar { display: flex; align-items: center; gap: .4rem; padding: .35rem .75rem; font-size: 11px; font-weight: 700; letter-spacing: .05em; text-transform: uppercase; color: var(--muted); border-bottom: 1px solid var(--border); background: var(--surface-2); }
119
+ .frame-bar i { width: .55rem; height: .55rem; border-radius: 50%; background: var(--border); }
120
+ .frame-bar .size { margin-left: auto; font-weight: 500; text-transform: none; letter-spacing: 0; }
121
+ [data-device="mobile"] .frame { --frame-max: 340px; border-radius: 28px; border: 6px solid var(--mobile); }
122
+ [data-device="mobile"] .frame-bar { justify-content: center; color: var(--mobile); background: var(--mobile-bg); }
123
+ [data-device="mobile"] .frame-bar i { display: none; }
124
+ [data-device="mobile"] .frame-bar .size { margin-left: .5rem; }
125
+ [data-device="tablet"] .frame { --frame-max: 560px; border-radius: 20px; border: 6px solid var(--tablet); }
126
+ [data-device="tablet"] .frame-bar { justify-content: center; color: var(--tablet); background: var(--tablet-bg); }
127
+ [data-device="tablet"] .frame-bar i { display: none; }
128
+ .side { display: flex; gap: 1rem; flex-wrap: wrap; justify-content: center; width: 100%; }
129
+ .side figure { margin: 0; min-width: 0; flex: 1 1 min(100%, var(--frame-max, 420px)); max-width: var(--frame-max, 100%); }
130
+ .side figcaption { font-weight: 600; margin-bottom: .35rem; }
131
+ .side figure.previous figcaption { color: var(--removed); }
132
+ .side figure.next figcaption { color: var(--added); }
133
+ .side .frame { max-width: 100%; }
134
+ [data-device="mobile"] .side { --frame-max: 340px; }
135
+ [data-device="tablet"] .side { --frame-max: 560px; }
136
+ img { max-width: 100%; display: block; background: #fff; }
137
+ .missing { display: grid; place-items: center; min-height: 8rem; border: 2px dashed var(--border); border-radius: 10px; color: var(--muted); }
138
+ .compare { position: relative; line-height: 0; user-select: none; -webkit-user-select: none; touch-action: pan-y; }
139
+ .compare img { width: 100%; height: auto; }
140
+ .compare .after { position: absolute; inset: 0; clip-path: inset(0 0 0 var(--split, 50%)); }
141
+ .compare.slide { cursor: ew-resize; }
142
+ .compare .tag { position: absolute; top: .5rem; padding: .1rem .5rem; border-radius: 6px; font: 700 11px/1.5 system-ui, sans-serif; background: rgb(20 20 40 / .75); color: #fff; pointer-events: none; }
143
+ .compare .tag.l { left: .5rem; }
144
+ .compare .tag.r { right: .5rem; }
145
+ .handle { position: absolute; top: 0; bottom: 0; left: var(--split, 50%); width: 44px; margin-left: -22px; cursor: ew-resize; touch-action: none; }
146
+ .handle::before { content: ""; position: absolute; top: 0; bottom: 0; left: 50%; width: 3px; margin-left: -1.5px; background: var(--accent); box-shadow: 0 0 0 1px rgb(255 255 255 / .6); }
147
+ .handle::after { content: "\\2194"; position: absolute; top: 50%; left: 50%; width: 34px; height: 34px; margin: -17px; border-radius: 50%; background: var(--accent); color: var(--accent-fg); display: grid; place-items: center; font: 700 16px/1 system-ui, sans-serif; border: 2px solid var(--accent-fg); box-shadow: 0 2px 8px rgb(0 0 0 / .35); }
148
+ .hl img { filter: grayscale(1) opacity(.5); }
149
+ .hl canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
150
+ [hidden] { display: none !important; }
151
+ @media (max-width: 640px) { .bulk { margin-left: 0; } .stats { margin-left: 0; } }
152
+ `;
153
+
154
+ const script = `
155
+ var THRESHOLD = 16;
156
+
157
+ function press(group, active) {
158
+ group.querySelectorAll('button').forEach(function (b) {
159
+ b.setAttribute('aria-pressed', String(b === active));
160
+ });
161
+ }
162
+
163
+ var filters = { status: 'all', device: 'all' };
164
+
165
+ function applyFilters() {
166
+ document.querySelectorAll('.card').forEach(function (card) {
167
+ card.hidden = (filters.status !== 'all' && card.dataset.status !== filters.status) ||
168
+ (filters.device !== 'all' && card.dataset.device !== filters.device);
169
+ });
170
+ }
171
+
172
+ document.querySelector('.bulk').addEventListener('click', function (event) {
173
+ var button = event.target.closest('button');
174
+ if (!button) return;
175
+ document.querySelectorAll('.card:not([hidden])').forEach(function (card) {
176
+ card.open = button.dataset.action === 'expand';
177
+ });
178
+ });
179
+
180
+ document.querySelectorAll('[data-filter-group]').forEach(function (group) {
181
+ group.addEventListener('click', function (event) {
182
+ var button = event.target.closest('button');
183
+ if (!button) return;
184
+ press(group, button);
185
+ filters[group.dataset.filterGroup] = button.dataset.value;
186
+ applyFilters();
187
+ });
188
+ });
189
+
190
+ function load(src) {
191
+ return new Promise(function (resolve) {
192
+ var image = new Image();
193
+ image.onload = function () { resolve(image); };
194
+ image.src = src;
195
+ });
196
+ }
197
+
198
+ function readPixels(image, width, height) {
199
+ var canvas = document.createElement('canvas');
200
+ canvas.width = width;
201
+ canvas.height = height;
202
+ var context = canvas.getContext('2d', { willReadFrequently: true });
203
+ context.drawImage(image, 0, 0);
204
+ return context.getImageData(0, 0, width, height);
205
+ }
206
+
207
+ function computeDiff(card) {
208
+ if (card.diff) return card.diff;
209
+ var sources = card.querySelectorAll('[data-view="side"] img');
210
+ card.diff = Promise.all([load(sources[0].src), load(sources[1].src)]).then(function (images) {
211
+ var width = Math.max(images[0].naturalWidth, images[1].naturalWidth);
212
+ var height = Math.max(images[0].naturalHeight, images[1].naturalHeight);
213
+ var before = readPixels(images[0], width, height);
214
+ var after = readPixels(images[1], width, height);
215
+ var overlay = new ImageData(width, height);
216
+ var count = 0;
217
+ for (var i = 0; i < before.data.length; i += 4) {
218
+ var delta = Math.max(
219
+ Math.abs(before.data[i] - after.data[i]),
220
+ Math.abs(before.data[i + 1] - after.data[i + 1]),
221
+ Math.abs(before.data[i + 2] - after.data[i + 2]),
222
+ Math.abs(before.data[i + 3] - after.data[i + 3])
223
+ );
224
+ if (delta > THRESHOLD) {
225
+ overlay.data[i] = 255;
226
+ overlay.data[i + 1] = 0;
227
+ overlay.data[i + 2] = 80;
228
+ overlay.data[i + 3] = 235;
229
+ count += 1;
230
+ }
231
+ }
232
+ var canvas = card.querySelector('canvas');
233
+ canvas.width = width;
234
+ canvas.height = height;
235
+ canvas.getContext('2d').putImageData(overlay, 0, 0);
236
+ var percent = (count / (width * height)) * 100;
237
+ card.querySelector('.stats').textContent = count === 0
238
+ ? 'No visible pixel differences'
239
+ : count.toLocaleString() + ' px differ (' + percent.toFixed(2) + '%)';
240
+ return count;
241
+ });
242
+ return card.diff;
243
+ }
244
+
245
+ function setMode(card, mode) {
246
+ card.querySelectorAll('[data-view]').forEach(function (view) {
247
+ view.hidden = view.dataset.view !== mode;
248
+ });
249
+ }
250
+
251
+ document.querySelectorAll('.modes').forEach(function (modes) {
252
+ modes.addEventListener('click', function (event) {
253
+ var button = event.target.closest('button');
254
+ if (!button) return;
255
+ press(modes, button);
256
+ setMode(modes.closest('.card'), button.dataset.mode);
257
+ });
258
+ });
259
+
260
+ // The slider is dragged directly on the image; the handle is also keyboard operable.
261
+ document.querySelectorAll('.compare.slide').forEach(function (compare) {
262
+ var handle = compare.querySelector('.handle');
263
+ function set(percent) {
264
+ var value = Math.min(100, Math.max(0, percent));
265
+ compare.style.setProperty('--split', value + '%');
266
+ handle.setAttribute('aria-valuenow', String(Math.round(value)));
267
+ }
268
+ function fromPointer(event) {
269
+ var rect = compare.getBoundingClientRect();
270
+ set(((event.clientX - rect.left) / rect.width) * 100);
271
+ }
272
+ compare.addEventListener('pointerdown', function (event) {
273
+ compare.setPointerCapture(event.pointerId);
274
+ compare.dragging = true;
275
+ fromPointer(event);
276
+ });
277
+ compare.addEventListener('pointermove', function (event) {
278
+ if (compare.dragging) fromPointer(event);
279
+ });
280
+ function stop() { compare.dragging = false; }
281
+ compare.addEventListener('pointerup', stop);
282
+ compare.addEventListener('pointercancel', stop);
283
+ handle.addEventListener('keydown', function (event) {
284
+ var current = Number(handle.getAttribute('aria-valuenow'));
285
+ var step = event.shiftKey ? 10 : 2;
286
+ if (event.key === 'ArrowLeft') set(current - step);
287
+ else if (event.key === 'ArrowRight') set(current + step);
288
+ else if (event.key === 'Home') set(0);
289
+ else if (event.key === 'End') set(100);
290
+ else return;
291
+ event.preventDefault();
292
+ });
293
+ });
294
+
295
+ var observer = new IntersectionObserver(function (entries) {
296
+ entries.forEach(function (entry) {
297
+ if (!entry.isIntersecting) return;
298
+ observer.unobserve(entry.target);
299
+ computeDiff(entry.target);
300
+ });
301
+ }, { rootMargin: '300px' });
302
+
303
+ document.querySelectorAll('.card[data-status="changed"]').forEach(function (card) {
304
+ observer.observe(card);
305
+ });
306
+ `;
307
+
308
+ const imageTag = (src, alt) => `<img src="${src}" alt="${alt}">`;
309
+
310
+ const missing = (text) => `<div class="missing">${text}</div>`;
311
+
312
+ const modeButton = (mode, label, pressed) =>
313
+ `<button type="button" data-mode="${mode}" aria-pressed="${pressed}">${label}</button>`;
314
+
315
+ const sizeOf = (buffer) =>
316
+ buffer ? `${buffer.readUInt32BE(16)} × ${buffer.readUInt32BE(20)}` : '';
317
+
318
+ const frame = (device, inner, buffer) => `<div class="frame">
319
+ <div class="frame-bar"><i></i><i></i><i></i>${device.label}<span class="size">${sizeOf(buffer)}</span></div>
320
+ ${inner}
321
+ </div>`;
322
+
323
+ const summaryRow = (
324
+ story,
325
+ device,
326
+ status,
327
+ extra = '',
328
+ failure = false
329
+ ) => `<summary>
330
+ <span class="chip ${device.id}">${icons[device.id]}${device.label}</span>
331
+ <h2>${escape(story)}</h2>
332
+ <span class="badge ${status}">${labels[status]}</span>
333
+ ${failure ? '<span class="badge failure" title="The snapshot test fails: expected is the committed baseline, actual is what the test rendered">Failing test</span>' : ''}
334
+ ${extra}
335
+ </summary>`;
336
+
337
+ const changedCard = ({ file, before, after, failure }) => {
338
+ const [previousLabel, nextLabel] = failure
339
+ ? ['Expected (baseline)', 'Actual (test)']
340
+ : ['Previous', 'New'];
341
+ const [story, project] = splitName(file);
342
+ const device = deviceOf(project);
343
+ const beforeSrc = dataUri(before);
344
+ const afterSrc = dataUri(after);
345
+
346
+ return `<details class="card" data-status="changed" data-device="${device.id}" open>
347
+ ${summaryRow(story, device, 'changed', '<span class="stats" aria-live="polite">Comparing…</span>', failure)}
348
+ <div class="body">
349
+ <div class="toolbar">
350
+ <div class="segmented modes" role="group" aria-label="Comparison mode">
351
+ ${modeButton('highlight', 'Highlight differences', true)}
352
+ ${modeButton('slider', 'Slider', false)}
353
+ ${modeButton('side', 'Side by side', false)}
354
+ </div>
355
+ <span class="legend">Project: <code>${escape(project)}</code></span>
356
+ </div>
357
+ <div class="stage">
358
+ <div data-view="highlight" style="width:100%;display:grid;justify-items:center">
359
+ ${frame(
360
+ device,
361
+ `<div class="compare hl">${imageTag(afterSrc, 'New snapshot with differences highlighted')}<canvas aria-hidden="true"></canvas></div>`,
362
+ after
363
+ )}
364
+ <div class="legend" style="margin-top:.5rem">${nextLabel}, dimmed. Differing pixels are marked in red.</div>
365
+ </div>
366
+ <div data-view="slider" hidden style="width:100%;display:grid;justify-items:center">
367
+ ${frame(
368
+ device,
369
+ `<div class="compare slide">
370
+ ${imageTag(beforeSrc, 'Previous snapshot')}
371
+ <img class="after" src="${afterSrc}" alt="New snapshot">
372
+ <span class="tag l">${previousLabel}</span><span class="tag r">${nextLabel}</span>
373
+ <div class="handle" role="slider" tabindex="0" aria-label="Reveal the new snapshot from the right" aria-valuemin="0" aria-valuemax="100" aria-valuenow="50"></div>
374
+ </div>`,
375
+ after
376
+ )}
377
+ <div class="legend" style="margin-top:.5rem">Drag on the image (or use the arrow keys on the handle). Left: ${previousLabel.toLowerCase()}. Right: ${nextLabel.toLowerCase()}.</div>
378
+ </div>
379
+ <div data-view="side" class="side" hidden>
380
+ <figure class="previous"><figcaption>${previousLabel}</figcaption>${frame(device, imageTag(beforeSrc, 'Previous snapshot'), before)}</figure>
381
+ <figure class="next"><figcaption>${nextLabel}</figcaption>${frame(device, imageTag(afterSrc, 'New snapshot'), after)}</figure>
382
+ </div>
383
+ </div>
384
+ </div>
385
+ </details>`;
386
+ };
387
+
388
+ const singleCard = ({ file, status, before, after }) => {
389
+ const [story, project] = splitName(file);
390
+ const device = deviceOf(project);
391
+ const previous =
392
+ status === 'added'
393
+ ? missing('No previous snapshot')
394
+ : frame(device, imageTag(dataUri(before), 'Previous snapshot'), before);
395
+ const next =
396
+ status === 'removed'
397
+ ? missing('No new snapshot')
398
+ : frame(device, imageTag(dataUri(after), 'New snapshot'), after);
399
+
400
+ return `<details class="card" data-status="${status}" data-device="${device.id}">
401
+ ${summaryRow(story, device, status)}
402
+ <div class="body">
403
+ <div class="toolbar"><span class="legend">Project: <code>${escape(project)}</code></span></div>
404
+ <div class="stage"><div class="side">
405
+ <figure class="previous"><figcaption>Previous</figcaption>${previous}</figure>
406
+ <figure class="next"><figcaption>New</figcaption>${next}</figure>
407
+ </div></div>
408
+ </div>
409
+ </details>`;
410
+ };
411
+
412
+ export const buildHtml = ({ entries, counts, baseRef }) => {
413
+ const total = entries.length;
414
+ const devices = [
415
+ ...new Set(entries.map(({ file }) => deviceOf(splitName(file)[1]).id)),
416
+ ];
417
+ const deviceCount = (id) =>
418
+ entries.filter(({ file }) => deviceOf(splitName(file)[1]).id === id).length;
419
+ const filter = (value, label, pressed) =>
420
+ `<button type="button" data-value="${value}" aria-pressed="${pressed}">${label}</button>`;
421
+ const deviceNames = {
422
+ desktop: 'Desktop',
423
+ tablet: 'Tablet',
424
+ mobile: 'Mobile',
425
+ };
426
+
427
+ return `<!doctype html>
428
+ <html lang="en">
429
+ <head>
430
+ <meta charset="utf-8">
431
+ <meta name="viewport" content="width=device-width, initial-scale=1">
432
+ <title>Visual snapshot report</title>
433
+ <style>${styles}</style>
434
+ </head>
435
+ <body>
436
+ <header class="top"><div class="page">
437
+ <h1>Visual snapshot report</h1>
438
+ <p class="sub">${total} snapshot${total === 1 ? '' : 's'} differ from <code>${escape(baseRef)}</code>.</p>
439
+ <div class="totals">
440
+ <div class="total changed"><strong>${counts.changed}</strong><span>Changed</span></div>
441
+ <div class="total added"><strong>${counts.added}</strong><span>New</span></div>
442
+ <div class="total removed"><strong>${counts.removed}</strong><span>Removed</span></div>
443
+ </div>
444
+ </div></header>
445
+ <main class="page">
446
+ <div class="controls">
447
+ <div class="group" data-filter-group="status" role="group" aria-label="Filter by status">
448
+ <span class="label">Status</span>
449
+ ${filter('all', `All (${total})`, true)}
450
+ ${filter('changed', `Changed (${counts.changed})`, false)}
451
+ ${filter('added', `New (${counts.added})`, false)}
452
+ ${filter('removed', `Removed (${counts.removed})`, false)}
453
+ </div>
454
+ <div class="group" data-filter-group="device" role="group" aria-label="Filter by device">
455
+ <span class="label">Device</span>
456
+ ${filter('all', 'All', true)}
457
+ ${devices.map((id) => filter(id, `${deviceNames[id]} (${deviceCount(id)})`, false)).join('\n ')}
458
+ </div>
459
+ <div class="group bulk" role="group" aria-label="Expand or collapse">
460
+ <button type="button" data-action="expand">Expand all</button>
461
+ <button type="button" data-action="collapse">Collapse all</button>
462
+ </div>
463
+ </div>
464
+ ${entries.map((entry) => (entry.status === 'changed' ? changedCard(entry) : singleCard(entry))).join('\n')}
465
+ </main>
466
+ <script>${script}</script>
467
+ </body>
468
+ </html>
469
+ `;
470
+ };
@@ -0,0 +1,13 @@
1
+ import { cpSync, existsSync, mkdirSync } from 'node:fs';
2
+ import { resolve, join } from 'node:path';
3
+
4
+ const [source = 'playwright-report', destination = '.tooling/report/visual'] =
5
+ process.argv.slice(2);
6
+ if (!existsSync(join(source, 'index.html'))) {
7
+ console.error(`Missing Playwright HTML report: ${source}`);
8
+ process.exitCode = 1;
9
+ } else {
10
+ mkdirSync(destination, { recursive: true });
11
+ cpSync(resolve(source), resolve(destination), { recursive: true });
12
+ console.log(`Visual report: ${join(destination, 'index.html')}`);
13
+ }