@keboola/validate-ui 0.5.0 → 0.5.3

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/dist/index.js CHANGED
@@ -1,1066 +1,12 @@
1
- import { chromium } from 'playwright';
2
- import { createReadStream } from 'fs';
3
- import { stat, writeFile, readFile } from 'fs/promises';
4
- import { createServer } from 'http';
5
- import { join, resolve, normalize, sep, extname } from 'path';
6
- import axe from 'axe-core';
7
- import { z } from 'zod';
8
- import Anthropic from '@anthropic-ai/sdk';
9
- import pixelmatch from 'pixelmatch';
10
- import { PNG } from 'pngjs';
11
- import { brands, checkInteractiveContrast } from '@keboola/brand-registry';
12
-
13
- // src/types.ts
14
- var SEVERITIES = ["critical", "serious", "moderate", "minor"];
15
-
16
- // src/prepare-screenshot.ts
17
- var disableAnimations = async (page) => {
18
- await page.addStyleTag({
19
- content: `*, *::before, *::after {
20
- animation-duration: 0s !important;
21
- animation-delay: 0s !important;
22
- transition-duration: 0s !important;
23
- transition-delay: 0s !important;
24
- caret-color: transparent !important;
25
- }`
26
- });
27
- };
28
- var parkMouseOffElements = async (page) => {
29
- await page.mouse.move(0, 0);
30
- };
31
- var waitForFonts = async (page) => {
32
- await page.evaluate(() => document.fonts.ready.then(() => void 0));
33
- };
34
- var prepareForScreenshot = async (page) => {
35
- await disableAnimations(page);
36
- await parkMouseOffElements(page);
37
- await waitForFonts(page);
38
- };
39
-
40
- // src/capture.ts
41
- var DESKTOP_VIEWPORT = { label: "desktop", width: 1280, height: 800 };
42
- var MOBILE_VIEWPORT = { label: "mobile", width: 390, height: 844 };
43
- var DEFAULT_VIEWPORTS = [DESKTOP_VIEWPORT, MOBILE_VIEWPORT];
44
- var DEFAULT_TIMEOUT_MS = 3e4;
45
- var attachRecorders = (page) => {
46
- const consoleEntries = [];
47
- const networkEntries = [];
48
- page.on("console", (message) => {
49
- consoleEntries.push({
50
- type: message.type(),
51
- text: message.text(),
52
- location: message.location().url || void 0
53
- });
54
- });
55
- page.on("pageerror", (error) => {
56
- consoleEntries.push({ type: "pageerror", text: error.message });
57
- });
58
- page.on("requestfailed", (request) => {
59
- networkEntries.push({
60
- url: request.url(),
61
- method: request.method(),
62
- status: 0,
63
- failure: request.failure()?.errorText
64
- });
65
- });
66
- page.on("response", (response) => {
67
- networkEntries.push({
68
- url: response.url(),
69
- method: response.request().method(),
70
- status: response.status()
71
- });
72
- });
73
- return { console: consoleEntries, network: networkEntries };
74
- };
75
- var capturePage = async (page, url, viewport, timeoutMs = DEFAULT_TIMEOUT_MS) => {
76
- const recorders = attachRecorders(page);
77
- await page.setViewportSize({ width: viewport.width, height: viewport.height });
78
- await page.goto(url, { waitUntil: "networkidle", timeout: timeoutMs });
79
- await prepareForScreenshot(page);
80
- const screenshot = await page.screenshot({ fullPage: true });
81
- const dom = await page.content();
82
- const renderedText = (await page.evaluate(() => document.body.innerText)).trim();
83
- return {
84
- url,
85
- viewport,
86
- screenshot,
87
- dom,
88
- renderedText,
89
- console: recorders.console,
90
- network: recorders.network
91
- };
92
- };
93
- var capture = async (url, options = {}) => {
94
- const viewports = options.viewports ?? DEFAULT_VIEWPORTS;
95
- const browser = options.browser ?? await chromium.launch();
96
- const ownsBrowser = !options.browser;
97
- try {
98
- const artifacts = [];
99
- for (const viewport of viewports) {
100
- const context = await browser.newContext({
101
- viewport: { width: viewport.width, height: viewport.height }
102
- });
103
- const page = await context.newPage();
104
- try {
105
- artifacts.push(await capturePage(page, url, viewport, options.timeoutMs));
106
- } finally {
107
- await context.close();
108
- }
109
- }
110
- return artifacts;
111
- } finally {
112
- if (ownsBrowser) await browser.close();
113
- }
114
- };
115
- var CONTENT_TYPES = {
116
- ".html": "text/html; charset=utf-8",
117
- ".js": "text/javascript; charset=utf-8",
118
- ".css": "text/css; charset=utf-8",
119
- ".json": "application/json; charset=utf-8",
120
- ".svg": "image/svg+xml",
121
- ".png": "image/png",
122
- ".jpg": "image/jpeg",
123
- ".woff2": "font/woff2",
124
- ".woff": "font/woff"
125
- };
126
- var serveStatic = async (rootDir) => {
127
- const indexPath = join(rootDir, "index.html");
128
- const rootResolved = resolve(rootDir);
129
- const server = createServer((request, response) => {
130
- const send = (filePath, status = 200) => {
131
- response.writeHead(status, {
132
- "content-type": CONTENT_TYPES[extname(filePath)] ?? "application/octet-stream"
133
- });
134
- const stream = createReadStream(filePath);
135
- stream.on("error", (error) => {
136
- console.error(`serveStatic: failed to read ${filePath}:`, error);
137
- if (!response.headersSent) response.writeHead(500);
138
- response.end();
139
- });
140
- stream.pipe(response);
141
- };
142
- const notFound = () => {
143
- response.writeHead(404);
144
- response.end();
145
- };
146
- let rawPath;
147
- try {
148
- rawPath = decodeURIComponent((request.url ?? "/").split("?")[0] ?? "/");
149
- } catch {
150
- response.writeHead(400);
151
- response.end();
152
- return;
153
- }
154
- const relative = normalize(rawPath).replace(/^(\.\.[/\\])+/, "");
155
- const candidate = join(rootDir, relative);
156
- const candidateResolved = resolve(candidate);
157
- if (candidateResolved !== rootResolved && !candidateResolved.startsWith(rootResolved + sep)) {
158
- notFound();
159
- return;
160
- }
161
- stat(candidate).then((info) => {
162
- send(info.isDirectory() ? indexPath : candidate);
163
- }).catch(() => {
164
- if (extname(candidate) === "") {
165
- send(indexPath);
166
- return;
167
- }
168
- notFound();
169
- });
170
- });
171
- await new Promise((resolve2) => server.listen(0, "127.0.0.1", resolve2));
172
- const address = server.address();
173
- if (address === null || typeof address === "string") {
174
- throw new Error("serveStatic: failed to bind an ephemeral port");
175
- }
176
- return {
177
- url: `http://127.0.0.1:${address.port}`,
178
- close: () => new Promise((resolve2, reject) => {
179
- server.close((error) => error ? reject(error) : resolve2());
180
- })
181
- };
182
- };
183
-
184
- // src/axes/clipped-text.ts
185
- var CLIP_TOLERANCE_PX = 2;
186
- var MAX_REPORTED = 10;
187
- var findClippedElements = ({
188
- tolerance,
189
- max
190
- }) => {
191
- const describe = (el) => {
192
- const tag = el.tagName.toLowerCase();
193
- const label = el.getAttribute("aria-label");
194
- if (label) return `${tag}[aria-label="${label}"]`;
195
- if (el.id) return `${tag}#${el.id}`;
196
- const name = el.getAttribute("name");
197
- if (name) return `${tag}[name="${name}"]`;
198
- const cls = typeof el.className === "string" && el.className.trim() ? `.${el.className.trim().split(/\s+/).slice(0, 2).join(".")}` : "";
199
- return `${tag}${cls}`;
200
- };
201
- const results = [];
202
- const reported = /* @__PURE__ */ new Set();
203
- const hasReportedAncestor = (el) => {
204
- let parent = el.parentElement;
205
- while (parent) {
206
- if (reported.has(parent)) return true;
207
- parent = parent.parentElement;
208
- }
209
- return false;
210
- };
211
- for (const el of document.querySelectorAll("*")) {
212
- if (results.length >= max) break;
213
- if (hasReportedAncestor(el)) continue;
214
- const style = window.getComputedStyle(el);
215
- if (style.display === "none" || style.visibility === "hidden" || style.opacity === "0") {
216
- continue;
217
- }
218
- if (el.clientWidth <= 1 || el.clientHeight <= 1) continue;
219
- const overflowX = style.overflowX;
220
- const isFormValue = el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement;
221
- const isSingleLineInput = el instanceof HTMLInputElement;
222
- if (!isSingleLineInput && overflowX !== "hidden" && overflowX !== "clip") continue;
223
- if (!isSingleLineInput && style.textOverflow === "ellipsis") continue;
224
- const overflowBy = el.scrollWidth - el.clientWidth;
225
- if (overflowBy <= tolerance) continue;
226
- const text = isFormValue ? el.value : (el.textContent ?? "").trim();
227
- if (!text) continue;
228
- reported.add(el);
229
- results.push({
230
- selector: describe(el),
231
- text: text.length > 40 ? `${text.slice(0, 40)}\u2026` : text,
232
- visibleWidth: Math.round(el.clientWidth),
233
- contentWidth: Math.round(el.scrollWidth),
234
- isInput: isFormValue
235
- });
236
- }
237
- return results;
238
- };
239
- var toFinding = (clipped) => ({
240
- message: `Content is cut off with no way to reveal it (${clipped.selector})`,
241
- severity: "serious",
242
- detail: `${clipped.isInput ? "Input value" : "Text"} "${clipped.text}" needs ${clipped.contentWidth}px but has ${clipped.visibleWidth}px. Widen the element, or make the truncation explicit with \`text-overflow: ellipsis\`.`
243
- });
244
- var clippedTextFindings = async (page) => {
245
- try {
246
- const clipped = await page.evaluate(findClippedElements, {
247
- tolerance: CLIP_TOLERANCE_PX,
248
- max: MAX_REPORTED
249
- });
250
- return clipped.map(toFinding);
251
- } catch (error) {
252
- console.error("clipped-text: could not inspect the page", error);
253
- return [];
254
- }
255
- };
256
-
257
- // src/axes/accessibility.ts
258
- var IMPACTS = ["critical", "serious", "moderate", "minor"];
259
- var toSeverity = (impact) => impact && IMPACTS.includes(impact) ? impact : "minor";
260
- var wcagTag = (tags) => tags.find((tag) => tag.startsWith("wcag")) ?? tags[0];
261
- var toFinding2 = (violation) => {
262
- const targets = violation.nodes.map((node) => node.target.join(" ")).filter((target) => target.length > 0).join(", ");
263
- const wcag = wcagTag(violation.tags);
264
- return {
265
- message: `${violation.help || violation.description}${targets ? ` (${targets})` : ""}`,
266
- severity: toSeverity(violation.impact),
267
- ref: [wcag, violation.id].filter(Boolean).join(" \xB7 ")
268
- };
269
- };
270
- var notAssessed = (message) => ({
271
- axis: "a11y",
272
- pass: true,
273
- findings: [{ message, severity: "minor" }]
274
- });
275
- var accessibilityAxis = {
276
- name: "a11y",
277
- run: async (context) => {
278
- const { page } = context;
279
- if (!page) return notAssessed("a11y skipped: no live page provided");
280
- await page.addScriptTag({ content: axe.source });
281
- const results = await page.evaluate(() => {
282
- const injected = window.axe;
283
- return injected ? injected.run(document) : null;
284
- });
285
- if (results === null) {
286
- return notAssessed("a11y not assessed: axe-core failed to inject (page CSP?)");
287
- }
288
- const findings = [...results.violations.map(toFinding2), ...await clippedTextFindings(page)];
289
- const pass = !findings.some(
290
- (finding) => finding.severity === "critical" || finding.severity === "serious"
291
- );
292
- return { axis: "a11y", pass, findings };
293
- }
294
- };
295
- var textFromMessage = (message) => message.content.filter((block) => block.type === "text").map((block) => block.text).join("");
296
- var makeProvider = (backend, clientOptions) => {
297
- const client = new Anthropic(clientOptions);
298
- return {
299
- backend,
300
- judge: async ({ screenshotBase64, prompt, model }) => {
301
- const message = await client.messages.create({
302
- model,
303
- max_tokens: 1024,
304
- messages: [
305
- {
306
- role: "user",
307
- content: [
308
- {
309
- type: "image",
310
- source: { type: "base64", media_type: "image/png", data: screenshotBase64 }
311
- },
312
- { type: "text", text: prompt }
313
- ]
314
- }
315
- ]
316
- });
317
- return textFromMessage(message);
318
- }
319
- };
320
- };
321
- var nonEmpty = (value) => value !== void 0 && value !== "";
322
- var resolveVlmProvider = (env = process.env) => {
323
- const baseUrl = env.VALIDATE_UI_LLM_BASE_URL ?? env.ANTHROPIC_BASE_URL;
324
- const proxyToken = env.VALIDATE_UI_LLM_TOKEN ?? env.KBC_TOKEN ?? env.ANTHROPIC_API_KEY;
325
- if (nonEmpty(baseUrl) && nonEmpty(proxyToken)) {
326
- return makeProvider("keboola-llm", { baseURL: baseUrl, apiKey: proxyToken });
327
- }
328
- if (nonEmpty(env.ANTHROPIC_API_KEY)) {
329
- return makeProvider("raw-anthropic", { apiKey: env.ANTHROPIC_API_KEY });
330
- }
331
- return void 0;
332
- };
333
-
334
- // src/axes/brief-conformance.ts
335
- var JudgeResult = z.object({
336
- matchesBrief: z.boolean(),
337
- missingStates: z.array(z.string()),
338
- reasons: z.array(z.string())
339
- });
340
- var MODEL = process.env.VALIDATE_UI_MODEL ?? "claude-sonnet-4-6";
341
- var DOM_EXCERPT_LIMIT = 12e3;
342
- var interpretJudgeResult = (result) => {
343
- const findings = [];
344
- if (!result.matchesBrief) {
345
- findings.push({
346
- message: `UI does not match the brief: ${result.reasons.join("; ")}`,
347
- severity: "serious"
348
- });
349
- }
350
- for (const state of result.missingStates) {
351
- findings.push({ message: `missing state: ${state}`, severity: "moderate" });
352
- }
353
- const pass = result.matchesBrief && result.missingStates.length === 0;
354
- return { axis: "brief-conformance", pass, findings };
355
- };
356
- var skip = (message) => ({
357
- axis: "brief-conformance",
358
- pass: true,
359
- findings: [{ message, severity: "minor" }]
360
- });
361
- var RUBRIC = [
362
- "You are judging whether a rendered UI conforms to the brief it was generated from.",
363
- "Assess two things:",
364
- "1. matchesBrief \u2014 does the screenshot + DOM implement what the brief describes?",
365
- "2. missingStates \u2014 which UI states the brief implies are NOT represented.",
366
- ' Use short lowercase labels like "empty", "loading", "error".',
367
- "Reply with ONLY a JSON object, no prose, matching exactly:",
368
- '{ "matchesBrief": boolean, "missingStates": string[], "reasons": string[] }',
369
- "reasons: brief explanations for any mismatch (empty array if it matches)."
370
- ].join("\n");
371
- var briefConformanceAxis = {
372
- name: "brief-conformance",
373
- run: async (context) => {
374
- const brief = context.brief?.trim();
375
- if (brief === void 0 || brief === "") {
376
- return skip("no brief provided; conformance not assessed");
377
- }
378
- const provider = resolveVlmProvider();
379
- if (provider === void 0) {
380
- return skip(
381
- "brief-conformance skipped: no VLM backend configured (set VALIDATE_UI_LLM_BASE_URL + VALIDATE_UI_LLM_TOKEN for the Keboola LLM path, or ANTHROPIC_API_KEY for a raw key)"
382
- );
383
- }
384
- const { artifact } = context;
385
- const screenshotBase64 = Buffer.from(artifact.screenshot).toString("base64");
386
- const domExcerpt = artifact.dom.slice(0, DOM_EXCERPT_LIMIT);
387
- const reply = await provider.judge({
388
- model: MODEL,
389
- screenshotBase64,
390
- prompt: `${RUBRIC}
391
-
392
- Brief:
393
- ${brief}
394
-
395
- DOM excerpt:
396
- ${domExcerpt}`
397
- });
398
- try {
399
- const parsed = JudgeResult.parse(JSON.parse(reply));
400
- return interpretJudgeResult(parsed);
401
- } catch (error) {
402
- console.error("brief-conformance: failed to parse judge response", error);
403
- return {
404
- axis: "brief-conformance",
405
- pass: false,
406
- findings: [{ message: "could not parse judge response", severity: "moderate" }]
407
- };
408
- }
409
- }
410
- };
411
-
412
- // src/compare/diff.ts
413
- var matchesGlob = (pattern, value) => {
414
- if (!pattern.includes("*")) return pattern === value;
415
- const escaped = pattern.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*");
416
- return new RegExp(`^${escaped}$`).test(value);
417
- };
418
- var multisetMissing = (from, to) => {
419
- const remaining = /* @__PURE__ */ new Map();
420
- for (const value of to) remaining.set(value, (remaining.get(value) ?? 0) + 1);
421
- const missing = [];
422
- for (const value of from) {
423
- const count = remaining.get(value) ?? 0;
424
- if (count > 0) remaining.set(value, count - 1);
425
- else missing.push(value);
426
- }
427
- return missing;
428
- };
429
- var signature = (regionKey, tokens) => `${regionKey}::${[...tokens].sort().join("|")}`;
430
- var regionRemovalRule = (rules, key) => rules.find(
431
- (rule) => rule.value === void 0 && rule.region !== void 0 && matchesGlob(rule.region, key)
432
- );
433
- var valueRuleMatches = (rule, key, value) => rule.value !== void 0 && matchesGlob(rule.value, value) && (rule.region === void 0 || matchesGlob(rule.region, key));
434
- var removedFinding = (region, rule) => ({
435
- regionKey: region.key,
436
- kind: "region-removed",
437
- valueSignature: signature(region.key, region.values),
438
- message: rule === void 0 ? `region "${region.label}" was removed` : `region "${region.label}" removed as expected`,
439
- severity: rule === void 0 ? "serious" : "minor",
440
- ...rule !== void 0 && { suppressed: { reason: rule.reason } }
441
- });
442
- var degradedFinding = (region) => ({
443
- regionKey: region.key,
444
- kind: "region-degraded",
445
- valueSignature: signature(region.key, [region.state]),
446
- message: `region "${region.label}" degraded to ${region.state} state \u2014 not compared as value loss`,
447
- severity: "minor"
448
- });
449
- var addedFinding = (region) => ({
450
- regionKey: region.key,
451
- kind: "region-added",
452
- valueSignature: signature(region.key, []),
453
- message: `region "${region.label}" is new`,
454
- severity: "minor"
455
- });
456
- var diffRegionValues = (oldRegion, newRegion, rules) => {
457
- const missing = multisetMissing(oldRegion.values, newRegion.values);
458
- if (missing.length === 0) return [];
459
- const active = [];
460
- const suppressed = [];
461
- const reasons = /* @__PURE__ */ new Set();
462
- for (const value of missing) {
463
- const rule = rules.find((r) => valueRuleMatches(r, newRegion.key, value));
464
- if (rule === void 0) active.push(value);
465
- else {
466
- suppressed.push(value);
467
- reasons.add(rule.reason);
468
- }
469
- }
470
- const findings = [];
471
- if (active.length > 0) {
472
- findings.push({
473
- regionKey: newRegion.key,
474
- kind: "value-missing",
475
- valueSignature: signature(newRegion.key, active),
476
- message: `region "${newRegion.label}" is missing ${active.length} value(s)`,
477
- severity: "moderate",
478
- detail: active.join(", ")
479
- });
480
- }
481
- if (suppressed.length > 0) {
482
- findings.push({
483
- regionKey: newRegion.key,
484
- kind: "value-missing",
485
- valueSignature: signature(newRegion.key, suppressed),
486
- message: `region "${newRegion.label}" is missing ${suppressed.length} expected value(s)`,
487
- severity: "minor",
488
- detail: suppressed.join(", "),
489
- suppressed: { reason: [...reasons].join("; ") }
490
- });
491
- }
492
- return findings;
493
- };
494
- var diffSnapshots = (oldSnap, newSnap, config = {}) => {
495
- const rules = config.expectedAbsent ?? [];
496
- const newByKey = new Map(newSnap.regions.map((region) => [region.key, region]));
497
- const oldKeys = new Set(oldSnap.regions.map((region) => region.key));
498
- const findings = [];
499
- for (const oldRegion of oldSnap.regions) {
500
- const newRegion = newByKey.get(oldRegion.key);
501
- if (newRegion === void 0) {
502
- findings.push(removedFinding(oldRegion, regionRemovalRule(rules, oldRegion.key)));
503
- continue;
504
- }
505
- if (oldRegion.state === "ok" && newRegion.state !== "ok") {
506
- findings.push(degradedFinding(newRegion));
507
- continue;
508
- }
509
- findings.push(...diffRegionValues(oldRegion, newRegion, rules));
510
- }
511
- for (const newRegion of newSnap.regions) {
512
- if (!oldKeys.has(newRegion.key)) findings.push(addedFinding(newRegion));
513
- }
514
- return findings;
515
- };
516
- var comparePass = (findings) => !findings.some(
517
- (f) => f.suppressed === void 0 && (f.severity === "serious" || f.severity === "critical")
518
- );
519
-
520
- // src/compare/regions.ts
521
- var CANDIDATE_SELECTOR = [
522
- "[data-region]",
523
- "main",
524
- "nav",
525
- "header",
526
- "aside",
527
- "footer",
528
- "section[aria-label]",
529
- "[role=main]",
530
- "[role=navigation]",
531
- "[role=banner]",
532
- "[role=complementary]",
533
- "[role=contentinfo]",
534
- "[role=region]"
535
- ].join(",");
536
- var DEFAULT_EMPTY_MARKERS = [
537
- "no data",
538
- "no results",
539
- "nothing to show",
540
- "nothing here",
541
- "get started"
542
- ];
543
- var DEFAULT_ERROR_MARKERS = [
544
- "something went wrong",
545
- "failed to load",
546
- "could not load",
547
- "unavailable",
548
- "try again"
549
- ];
550
- var extractRegions = async (page, config = {}) => {
551
- const emptyMarkers = [...DEFAULT_EMPTY_MARKERS, ...config.emptyStateMarkers ?? []];
552
- const errorMarkers = [...DEFAULT_ERROR_MARKERS, ...config.errorStateMarkers ?? []];
553
- return page.evaluate(
554
- ({ selector, emptyMarkers: emptyMarkers2, errorMarkers: errorMarkers2 }) => {
555
- const STATES = ["ok", "empty", "error", "loading"];
556
- const isState = (value) => STATES.includes(value);
557
- const slugify = (raw) => raw.toLowerCase().trim().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 60) || "region";
558
- const roleOf = (el) => {
559
- const explicit = el.getAttribute("role");
560
- if (explicit !== null && explicit !== "") return explicit;
561
- const tag = el.tagName.toLowerCase();
562
- if (tag === "main") return "main";
563
- if (tag === "nav") return "navigation";
564
- if (tag === "aside") return "complementary";
565
- if (tag === "header" || tag === "footer") {
566
- if (el.closest("main,nav,section,article,aside") !== null) return void 0;
567
- return tag === "header" ? "banner" : "contentinfo";
568
- }
569
- return void 0;
570
- };
571
- const headingOf = (el) => {
572
- const heading = el.querySelector("h1,h2,h3,h4,h5,h6");
573
- const text = heading?.textContent?.trim();
574
- return text !== void 0 && text !== "" ? text : void 0;
575
- };
576
- const keyOf = (el) => {
577
- const region = el.getAttribute("data-region");
578
- if (region !== null && region !== "") return slugify(region);
579
- const testid = el.getAttribute("data-testid");
580
- if (testid !== null && testid !== "") return slugify(testid);
581
- const role = roleOf(el);
582
- if (role !== void 0) return role;
583
- const label = el.getAttribute("aria-label");
584
- if (label !== null && label !== "") return slugify(label);
585
- const heading = headingOf(el);
586
- if (heading !== void 0) return slugify(heading);
587
- return slugify(el.tagName);
588
- };
589
- const labelOf = (el, key) => {
590
- const label = el.getAttribute("aria-label");
591
- if (label !== null && label.trim() !== "") return label.trim();
592
- const heading = headingOf(el);
593
- if (heading !== void 0) return heading;
594
- const testid = el.getAttribute("data-testid");
595
- if (testid !== null && testid !== "") return testid;
596
- return key;
597
- };
598
- const textOf = (el) => (el.innerText || el.textContent || "").trim();
599
- const stateOf = (el, text) => {
600
- const explicit = (el.getAttribute("data-state") ?? "").toLowerCase();
601
- if (isState(explicit)) return explicit;
602
- const lower = text.toLowerCase();
603
- if (errorMarkers2.some((marker) => lower.includes(marker))) return "error";
604
- if (lower.includes("loading")) return "loading";
605
- if (emptyMarkers2.some((marker) => lower.includes(marker))) return "empty";
606
- return "ok";
607
- };
608
- const valuesOf = (text) => (text.match(/-?\$?\s?\d[\d,]*(?:\.\d+)?%?/g) ?? []).map((token) => token.replace(/[$\s,]/g, "")).filter((token) => token !== "" && token !== "-");
609
- const qualifies = (el) => {
610
- const dataRegion = el.getAttribute("data-region");
611
- if (dataRegion !== null && dataRegion !== "") {
612
- const parent = el.parentElement;
613
- return parent === null || parent.closest("[data-region]") === null;
614
- }
615
- return el.querySelector(selector) === null && el.closest("[data-region]") === null;
616
- };
617
- const candidates = Array.from(document.querySelectorAll(selector));
618
- const regions = candidates.filter(qualifies);
619
- const elements = regions.length > 0 ? regions : [document.body];
620
- const used = {};
621
- const uniqueKey = (key) => {
622
- const count = (used[key] ?? 0) + 1;
623
- used[key] = count;
624
- return count === 1 ? key : `${key}-${count}`;
625
- };
626
- return elements.map((el) => {
627
- const text = textOf(el);
628
- const key = uniqueKey(keyOf(el));
629
- return {
630
- key,
631
- label: labelOf(el, key),
632
- state: stateOf(el, text),
633
- values: valuesOf(text)
634
- };
635
- });
636
- },
637
- { selector: CANDIDATE_SELECTOR, emptyMarkers, errorMarkers }
638
- );
639
- };
640
- var REGION_STATES = ["ok", "empty", "error", "loading"];
641
- var Region = z.object({
642
- /** Stable id: data-region → data-testid → role → aria-label → heading slug. */
643
- key: z.string(),
644
- /** Human label for the report. */
645
- label: z.string(),
646
- state: z.enum(REGION_STATES),
647
- /** Normalized numeric / currency / percent tokens found in the region. */
648
- values: z.array(z.string())
649
- });
650
- var CompareSnapshot = z.object({
651
- /** Route the snapshot was captured at, e.g. `/financials`. */
652
- route: z.string(),
653
- regions: z.array(Region)
654
- }).refine(
655
- (snap) => new Set(snap.regions.map((region) => region.key)).size === snap.regions.length,
656
- {
657
- message: "region keys must be unique"
658
- }
659
- );
660
- var ExpectedAbsentRule = z.object({
661
- region: z.string().optional(),
662
- value: z.string().optional(),
663
- reason: z.string().min(1)
664
- }).refine((rule) => rule.region !== void 0 || rule.value !== void 0, {
665
- message: "expectedAbsent rule needs at least one of `region` or `value`"
666
- });
667
- var CompareConfig = z.object({
668
- expectedAbsent: z.array(ExpectedAbsentRule).optional(),
669
- emptyStateMarkers: z.array(z.string()).optional(),
670
- errorStateMarkers: z.array(z.string()).optional()
671
- });
672
-
673
- // src/compare/snapshot.ts
674
- var captureSnapshot = async (page, route, config) => ({
675
- route,
676
- regions: await extractRegions(page, config)
677
- });
678
- var saveSnapshot = async (snapshot, path) => {
679
- await writeFile(path, JSON.stringify(snapshot, null, 2));
680
- };
681
- var loadSnapshot = async (path) => {
682
- const raw = await readFile(path, "utf8");
683
- return CompareSnapshot.parse(JSON.parse(raw));
684
- };
685
-
686
- // src/axes/compare.ts
687
- var skip2 = (message) => ({
688
- axis: "compare",
689
- pass: true,
690
- findings: [{ message, severity: "minor" }]
691
- });
692
- var compareAxis = {
693
- name: "compare",
694
- run: async (context) => {
695
- const { comparison, compareConfig, page } = context;
696
- if (comparison === void 0) {
697
- return skip2("no comparison snapshot provided; old-vs-new not assessed");
698
- }
699
- if (page === void 0) {
700
- return skip2("compare needs a live page to extract the new snapshot");
701
- }
702
- const newSnapshot = await captureSnapshot(page, comparison.route, compareConfig);
703
- const findings = diffSnapshots(comparison, newSnapshot, compareConfig);
704
- return { axis: "compare", pass: comparePass(findings), findings };
705
- }
706
- };
707
-
708
- // src/axes/runtime-health.ts
709
- var ERROR_MARKERS = [
710
- "something went wrong",
711
- "vite-error-overlay",
712
- "vite-plugin-checker-error-overlay",
713
- "react-error-overlay"
714
- ];
715
- var pageErrorFindings = (console2) => console2.filter((entry) => entry.type === "pageerror").map((entry) => ({
716
- message: `Uncaught page error: ${entry.text}`,
717
- severity: "critical",
718
- detail: entry.location
719
- }));
720
- var RESOURCE_LOAD_ECHO = "failed to load resource";
721
- var isResourceLoadEcho = (entry) => entry.text.toLowerCase().includes(RESOURCE_LOAD_ECHO);
722
- var consoleErrorFindings = (console2) => {
723
- const seen = /* @__PURE__ */ new Set();
724
- const findings = [];
725
- for (const entry of console2) {
726
- if (entry.type !== "error" || isResourceLoadEcho(entry) || seen.has(entry.text)) continue;
727
- seen.add(entry.text);
728
- findings.push({
729
- message: `Console error: ${entry.text}`,
730
- severity: "serious",
731
- detail: entry.location
732
- });
733
- }
734
- return findings;
735
- };
736
- var toNetworkFinding = (entry) => {
737
- if (entry.status === 0) {
738
- return {
739
- message: `Request failed: ${entry.method} ${entry.url}`,
740
- severity: "serious",
741
- detail: entry.failure,
742
- ref: entry.url
743
- };
744
- }
745
- if (entry.status >= 500) {
746
- return {
747
- message: `Server error ${entry.status}: ${entry.method} ${entry.url}`,
748
- severity: "serious",
749
- ref: entry.url
750
- };
751
- }
752
- if (entry.status >= 400) {
753
- return {
754
- message: `Client error ${entry.status}: ${entry.method} ${entry.url}`,
755
- severity: "moderate",
756
- ref: entry.url
757
- };
758
- }
759
- return null;
760
- };
761
- var SEVERITY_RANK = { critical: 3, serious: 2, moderate: 1, minor: 0 };
762
- var networkFindings = (network) => {
763
- const byUrl = /* @__PURE__ */ new Map();
764
- for (const entry of network) {
765
- const finding = toNetworkFinding(entry);
766
- if (finding === null) continue;
767
- const existing = byUrl.get(entry.url);
768
- if (existing === void 0 || SEVERITY_RANK[finding.severity] > SEVERITY_RANK[existing.severity]) {
769
- byUrl.set(entry.url, finding);
770
- }
771
- }
772
- return [...byUrl.values()];
773
- };
774
- var errorMarkerFinding = (dom) => {
775
- const lower = dom.toLowerCase();
776
- const marker = ERROR_MARKERS.find((m) => lower.includes(m));
777
- return marker === void 0 ? null : {
778
- message: "Error boundary / error overlay rendered instead of the app",
779
- severity: "critical",
780
- detail: marker
781
- };
782
- };
783
- var whiteScreenFinding = (renderedText) => renderedText !== void 0 && renderedText.length === 0 ? { message: "White screen: page rendered no visible text", severity: "critical" } : null;
784
- var runtimeHealthAxis = {
785
- name: "runtime-health",
786
- run: (context) => {
787
- const { artifact } = context;
788
- const findings = [
789
- ...pageErrorFindings(artifact.console),
790
- ...consoleErrorFindings(artifact.console),
791
- ...networkFindings(artifact.network)
792
- ];
793
- const errorMarker = errorMarkerFinding(artifact.dom);
794
- if (errorMarker !== null) findings.push(errorMarker);
795
- const whiteScreen = whiteScreenFinding(artifact.renderedText);
796
- if (whiteScreen !== null) findings.push(whiteScreen);
797
- const fatal = ["critical", "serious"];
798
- const pass = !findings.some((f) => fatal.includes(f.severity));
799
- return Promise.resolve({ axis: "runtime-health", pass, findings });
800
- }
801
- };
802
- var DRIFT_THRESHOLD = 0.01;
803
- var RESKIN_THRESHOLD = 5e-3;
804
- var DRIFT_PIXEL_THRESHOLD = 0.1;
805
- var RESKIN_PIXEL_THRESHOLD = 0.02;
806
- var DEFAULT_ALT_BRAND = "example-customer";
807
- var pngMismatchRatio = (a, b, pixelThreshold = DRIFT_PIXEL_THRESHOLD) => {
808
- const imgA = PNG.sync.read(Buffer.from(a));
809
- const imgB = PNG.sync.read(Buffer.from(b));
810
- if (imgA.width !== imgB.width || imgA.height !== imgB.height) {
811
- return { ratio: 1, sizeMismatch: true };
812
- }
813
- const total = imgA.width * imgA.height;
814
- if (total === 0) return { ratio: 0, sizeMismatch: false };
815
- const mismatched = pixelmatch(imgA.data, imgB.data, void 0, imgA.width, imgA.height, {
816
- threshold: pixelThreshold
817
- });
818
- return { ratio: mismatched / total, sizeMismatch: false };
819
- };
820
- var baselineFinding = (screenshot, baseline) => {
821
- const { ratio, sizeMismatch } = pngMismatchRatio(screenshot, baseline);
822
- if (sizeMismatch) {
823
- return {
824
- message: "screenshot dimensions differ from baseline",
825
- severity: "moderate"
826
- };
827
- }
828
- if (ratio > DRIFT_THRESHOLD) {
829
- return {
830
- message: `visual drift ${(ratio * 100).toFixed(1)}% vs baseline`,
831
- severity: "moderate",
832
- detail: `${(ratio * 100).toFixed(2)}% of pixels changed`
833
- };
834
- }
835
- return null;
836
- };
837
- var altBrandWasApplied = (altDom, brand) => altDom.includes(`data-brand="${brand}"`);
838
- var altBrandFinding = (screenshot, altScreenshot, brand, altDom) => {
839
- if (!altBrandWasApplied(altDom, brand)) {
840
- return {
841
- message: `alt-brand re-skin not assessed: the app did not apply '${brand}' from ?brand=`,
842
- severity: "minor",
843
- detail: "Brand is usually build configuration rather than a runtime switch. To have this axis assess re-skinning, register the alternate brand and resolve the active id from the `brand` query parameter."
844
- };
845
- }
846
- const { ratio } = pngMismatchRatio(screenshot, altScreenshot, RESKIN_PIXEL_THRESHOLD);
847
- if (ratio < RESKIN_THRESHOLD) {
848
- return {
849
- message: `chrome did not re-skin under alternate brand '${brand}'`,
850
- severity: "serious",
851
- detail: `${(ratio * 100).toFixed(2)}% of pixels changed`
852
- };
853
- }
854
- return null;
855
- };
856
- var interactiveContrastFindings = (brand) => checkInteractiveContrast(brand).map((warning) => ({
857
- message: `brand '${brand.id}' ${warning.message}`,
858
- severity: "moderate",
859
- detail: `${warning.foreground} on ${warning.background} (${warning.palette})`
860
- }));
861
- var overflowFinding = async (page) => {
862
- const overflows = await page.evaluate(
863
- () => document.documentElement.scrollWidth > document.documentElement.clientWidth
864
- );
865
- if (overflows) {
866
- return { message: "horizontal layout overflow", severity: "moderate" };
867
- }
868
- return null;
869
- };
870
- var visualBrandAxis = {
871
- name: "visual-brand",
872
- run: async (context) => {
873
- const { artifact, baseline, captureUnderBrand, page } = context;
874
- const findings = [];
875
- if (baseline !== void 0) {
876
- const finding = baselineFinding(artifact.screenshot, baseline);
877
- if (finding !== null) findings.push(finding);
878
- }
879
- if (captureUnderBrand !== void 0) {
880
- const altArtifact = await captureUnderBrand(DEFAULT_ALT_BRAND);
881
- const finding = altBrandFinding(
882
- artifact.screenshot,
883
- altArtifact.screenshot,
884
- DEFAULT_ALT_BRAND,
885
- altArtifact.dom
886
- );
887
- if (finding !== null) findings.push(finding);
888
- const altBrand = brands.find((brand) => brand.id === DEFAULT_ALT_BRAND);
889
- if (altBrand !== void 0) findings.push(...interactiveContrastFindings(altBrand));
890
- }
891
- if (page !== void 0) {
892
- const finding = await overflowFinding(page);
893
- if (finding !== null) findings.push(finding);
894
- }
895
- const pass = !findings.some((f) => f.severity === "critical" || f.severity === "serious");
896
- return { axis: "visual-brand", pass, findings };
897
- }
898
- };
899
-
900
- // src/axes/index.ts
901
- var ALL_AXES = [
902
- runtimeHealthAxis,
903
- accessibilityAxis,
904
- visualBrandAxis,
905
- briefConformanceAxis,
906
- compareAxis
907
- ];
908
-
909
- // src/aggregate.ts
910
- var runAxes = async (axes, context) => {
911
- const verdicts = [];
912
- for (const axis of axes) {
913
- try {
914
- verdicts.push(await axis.run(context));
915
- } catch (error) {
916
- const message = error instanceof Error ? error.message : String(error);
917
- console.error(`validate-ui: axis "${axis.name}" threw:`, error);
918
- verdicts.push({
919
- axis: axis.name,
920
- pass: false,
921
- findings: [{ message: `Axis crashed: ${message}`, severity: "critical" }]
922
- });
923
- }
924
- }
925
- return verdicts;
926
- };
927
- var aggregatePass = (verdicts) => verdicts.every((v) => v.pass);
928
- var withBrandParam = (url, brand) => {
929
- const parsed = new URL(url);
930
- parsed.searchParams.set("brand", brand);
931
- return parsed.toString();
932
- };
933
- var validate = async (options) => {
934
- const viewport = options.viewport ?? DESKTOP_VIEWPORT;
935
- const axes = options.axes ?? ALL_AXES;
936
- const browser = options.browser ?? await chromium.launch();
937
- const ownsBrowser = !options.browser;
938
- const context = await browser.newContext({
939
- viewport: { width: viewport.width, height: viewport.height }
940
- });
941
- const page = await context.newPage();
942
- try {
943
- const artifact = await capturePage(page, options.url, viewport, options.timeoutMs);
944
- const axisContext = {
945
- artifact,
946
- page,
947
- browser,
948
- brief: options.brief,
949
- baseline: options.baseline,
950
- comparison: options.comparison,
951
- compareConfig: options.compareConfig,
952
- captureUnderBrand: async (brand) => {
953
- const brandContext = await browser.newContext({
954
- viewport: { width: viewport.width, height: viewport.height }
955
- });
956
- const brandPage = await brandContext.newPage();
957
- try {
958
- return await capturePage(
959
- brandPage,
960
- withBrandParam(options.url, brand),
961
- viewport,
962
- options.timeoutMs
963
- );
964
- } finally {
965
- await brandContext.close();
966
- }
967
- }
968
- };
969
- const verdicts = await runAxes(axes, axisContext);
970
- return { url: options.url, pass: aggregatePass(verdicts), verdicts };
971
- } finally {
972
- await context.close();
973
- if (ownsBrowser) await browser.close();
974
- }
975
- };
976
-
977
- // src/compare/dedup.ts
978
- var groupKey = (finding) => `${finding.kind}::${finding.valueSignature}`;
979
- var dedupeSharedChrome = (perRoute) => {
980
- const routesByGroup = /* @__PURE__ */ new Map();
981
- for (const { route, findings } of perRoute) {
982
- for (const finding of findings) {
983
- const key = groupKey(finding);
984
- const routes = routesByGroup.get(key) ?? /* @__PURE__ */ new Set();
985
- routes.add(route);
986
- routesByGroup.set(key, routes);
987
- }
988
- }
989
- const sharedGroups = new Set(
990
- [...routesByGroup].filter(([, routes]) => routes.size >= 2).map(([key]) => key)
991
- );
992
- const sharedChrome = [];
993
- const seenShared = /* @__PURE__ */ new Set();
994
- const trimmedPerRoute = perRoute.map(({ route, findings }) => ({
995
- route,
996
- findings: findings.filter((finding) => !sharedGroups.has(groupKey(finding)))
997
- }));
998
- for (const { findings } of perRoute) {
999
- for (const finding of findings) {
1000
- const key = groupKey(finding);
1001
- if (!sharedGroups.has(key) || seenShared.has(key)) continue;
1002
- seenShared.add(key);
1003
- const routes = [...routesByGroup.get(key) ?? []].sort();
1004
- sharedChrome.push({
1005
- ...finding,
1006
- message: `${finding.message} (shared across ${routes.length} routes: ${routes.join(", ")})`
1007
- });
1008
- }
1009
- }
1010
- return { sharedChrome, perRoute: trimmedPerRoute };
1011
- };
1012
- var DEFAULT_TIMEOUT_MS2 = 3e4;
1013
- var joinUrl = (base, route) => `${base.replace(/\/$/, "")}/${route.replace(/^\//, "")}`;
1014
- var routeSnapshotFile = (route) => `${route.replace(/^\//, "").replace(/\/+$/, "").replace(/\//g, "_") || "index"}.json`;
1015
- var compareRoutes = async (options) => {
1016
- if (options.oldBaseUrl === void 0 === (options.oldSnapshotDir === void 0)) {
1017
- throw new Error("compareRoutes: provide exactly one of oldBaseUrl or oldSnapshotDir");
1018
- }
1019
- const viewport = options.viewport ?? DESKTOP_VIEWPORT;
1020
- const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS2;
1021
- const browser = options.browser ?? await chromium.launch();
1022
- const ownsBrowser = options.browser === void 0;
1023
- const { oldBaseUrl, oldSnapshotDir } = options;
1024
- const snapshotUrl = async (url, route) => {
1025
- const context = await browser.newContext({
1026
- viewport: { width: viewport.width, height: viewport.height }
1027
- });
1028
- const page = await context.newPage();
1029
- try {
1030
- await page.goto(url, { waitUntil: "networkidle", timeout: timeoutMs });
1031
- await prepareForScreenshot(page);
1032
- return await captureSnapshot(page, route, options.config);
1033
- } finally {
1034
- await context.close();
1035
- }
1036
- };
1037
- const oldSnapshotFor = (route) => {
1038
- if (oldBaseUrl !== void 0) return snapshotUrl(joinUrl(oldBaseUrl, route), route);
1039
- if (oldSnapshotDir !== void 0) {
1040
- return loadSnapshot(join(oldSnapshotDir, routeSnapshotFile(route)));
1041
- }
1042
- throw new Error("compareRoutes: provide exactly one of oldBaseUrl or oldSnapshotDir");
1043
- };
1044
- try {
1045
- const perRoute = [];
1046
- for (const route of options.routes) {
1047
- const oldSnapshot = await oldSnapshotFor(route);
1048
- const newSnapshot = await snapshotUrl(joinUrl(options.newBaseUrl, route), route);
1049
- perRoute.push({ route, findings: diffSnapshots(oldSnapshot, newSnapshot, options.config) });
1050
- }
1051
- const { sharedChrome, perRoute: trimmed } = dedupeSharedChrome(perRoute);
1052
- const routes = trimmed.map(({ route, findings }) => ({
1053
- route,
1054
- findings,
1055
- pass: comparePass(findings)
1056
- }));
1057
- const pass = comparePass(sharedChrome) && routes.every((route) => route.pass);
1058
- return { routes, sharedChrome, pass };
1059
- } finally {
1060
- if (ownsBrowser) await browser.close();
1061
- }
1062
- };
1063
-
1
+ import { A as prepareForScreenshot, C as accessibilityAxis, D as MOBILE_VIEWPORT, E as DESKTOP_VIEWPORT, O as capture, S as briefConformanceAxis, T as DEFAULT_VIEWPORTS, _ as REGION_STATES, a as runAxes, b as comparePass, c as visualBrandAxis, d as captureSnapshot, f as loadSnapshot, g as ExpectedAbsentRule, h as CompareSnapshot, i as aggregatePass, k as capturePage, l as runtimeHealthAxis, m as CompareConfig, n as routeSnapshotFile, o as validate, p as saveSnapshot, r as dedupeSharedChrome, s as ALL_AXES, t as compareRoutes, u as compareAxis, v as Region, w as serveStatic, x as diffSnapshots, y as extractRegions } from "./runner-CLpaiCIG.js";
2
+ //#region src/types.ts
3
+ const SEVERITIES = [
4
+ "critical",
5
+ "serious",
6
+ "moderate",
7
+ "minor"
8
+ ];
9
+ //#endregion
1064
10
  export { ALL_AXES, CompareConfig, CompareSnapshot, DEFAULT_VIEWPORTS, DESKTOP_VIEWPORT, ExpectedAbsentRule, MOBILE_VIEWPORT, REGION_STATES, Region, SEVERITIES, accessibilityAxis, aggregatePass, briefConformanceAxis, capture, capturePage, captureSnapshot, compareAxis, comparePass, compareRoutes, dedupeSharedChrome, diffSnapshots, extractRegions, loadSnapshot, prepareForScreenshot, routeSnapshotFile, runAxes, runtimeHealthAxis, saveSnapshot, serveStatic, validate, visualBrandAxis };
1065
- //# sourceMappingURL=index.js.map
11
+
1066
12
  //# sourceMappingURL=index.js.map