@keboola/validate-ui 0.2.0 → 0.3.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/dist/cli.cjs +536 -87
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.js +536 -87
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +402 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +196 -5
- package/dist/index.d.ts +196 -5
- package/dist/index.js +389 -3
- package/dist/index.js.map +1 -1
- package/package.json +5 -4
package/dist/cli.cjs
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
|
+
var promises = require('fs/promises');
|
|
4
5
|
var path = require('path');
|
|
5
6
|
var process2 = require('process');
|
|
6
7
|
var playwright = require('playwright');
|
|
7
|
-
var axe = require('axe-core');
|
|
8
8
|
var zod = require('zod');
|
|
9
|
+
var axe = require('axe-core');
|
|
9
10
|
var Anthropic = require('@anthropic-ai/sdk');
|
|
10
11
|
var pixelmatch = require('pixelmatch');
|
|
11
12
|
var pngjs = require('pngjs');
|
|
12
13
|
var fs = require('fs');
|
|
13
|
-
var promises = require('fs/promises');
|
|
14
14
|
var http = require('http');
|
|
15
15
|
|
|
16
16
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
@@ -20,6 +20,441 @@ var axe__default = /*#__PURE__*/_interopDefault(axe);
|
|
|
20
20
|
var Anthropic__default = /*#__PURE__*/_interopDefault(Anthropic);
|
|
21
21
|
var pixelmatch__default = /*#__PURE__*/_interopDefault(pixelmatch);
|
|
22
22
|
|
|
23
|
+
// src/prepare-screenshot.ts
|
|
24
|
+
var disableAnimations = async (page) => {
|
|
25
|
+
await page.addStyleTag({
|
|
26
|
+
content: `*, *::before, *::after {
|
|
27
|
+
animation-duration: 0s !important;
|
|
28
|
+
animation-delay: 0s !important;
|
|
29
|
+
transition-duration: 0s !important;
|
|
30
|
+
transition-delay: 0s !important;
|
|
31
|
+
caret-color: transparent !important;
|
|
32
|
+
}`
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
var parkMouseOffElements = async (page) => {
|
|
36
|
+
await page.mouse.move(0, 0);
|
|
37
|
+
};
|
|
38
|
+
var waitForFonts = async (page) => {
|
|
39
|
+
await page.evaluate(() => document.fonts.ready.then(() => void 0));
|
|
40
|
+
};
|
|
41
|
+
var prepareForScreenshot = async (page) => {
|
|
42
|
+
await disableAnimations(page);
|
|
43
|
+
await parkMouseOffElements(page);
|
|
44
|
+
await waitForFonts(page);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// src/capture.ts
|
|
48
|
+
var DESKTOP_VIEWPORT = { label: "desktop", width: 1280, height: 800 };
|
|
49
|
+
var DEFAULT_TIMEOUT_MS = 3e4;
|
|
50
|
+
var attachRecorders = (page) => {
|
|
51
|
+
const consoleEntries = [];
|
|
52
|
+
const networkEntries = [];
|
|
53
|
+
page.on("console", (message) => {
|
|
54
|
+
consoleEntries.push({
|
|
55
|
+
type: message.type(),
|
|
56
|
+
text: message.text(),
|
|
57
|
+
location: message.location().url || void 0
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
page.on("pageerror", (error) => {
|
|
61
|
+
consoleEntries.push({ type: "pageerror", text: error.message });
|
|
62
|
+
});
|
|
63
|
+
page.on("requestfailed", (request) => {
|
|
64
|
+
networkEntries.push({
|
|
65
|
+
url: request.url(),
|
|
66
|
+
method: request.method(),
|
|
67
|
+
status: 0,
|
|
68
|
+
failure: request.failure()?.errorText
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
page.on("response", (response) => {
|
|
72
|
+
networkEntries.push({
|
|
73
|
+
url: response.url(),
|
|
74
|
+
method: response.request().method(),
|
|
75
|
+
status: response.status()
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
return { console: consoleEntries, network: networkEntries };
|
|
79
|
+
};
|
|
80
|
+
var capturePage = async (page, url, viewport, timeoutMs = DEFAULT_TIMEOUT_MS) => {
|
|
81
|
+
const recorders = attachRecorders(page);
|
|
82
|
+
await page.setViewportSize({ width: viewport.width, height: viewport.height });
|
|
83
|
+
await page.goto(url, { waitUntil: "networkidle", timeout: timeoutMs });
|
|
84
|
+
await prepareForScreenshot(page);
|
|
85
|
+
const screenshot = await page.screenshot({ fullPage: true });
|
|
86
|
+
const dom = await page.content();
|
|
87
|
+
const renderedText = (await page.evaluate(() => document.body.innerText)).trim();
|
|
88
|
+
return {
|
|
89
|
+
url,
|
|
90
|
+
viewport,
|
|
91
|
+
screenshot,
|
|
92
|
+
dom,
|
|
93
|
+
renderedText,
|
|
94
|
+
console: recorders.console,
|
|
95
|
+
network: recorders.network
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
// src/compare/dedup.ts
|
|
100
|
+
var groupKey = (finding) => `${finding.kind}::${finding.valueSignature}`;
|
|
101
|
+
var dedupeSharedChrome = (perRoute) => {
|
|
102
|
+
const routesByGroup = /* @__PURE__ */ new Map();
|
|
103
|
+
for (const { route, findings } of perRoute) {
|
|
104
|
+
for (const finding of findings) {
|
|
105
|
+
const key = groupKey(finding);
|
|
106
|
+
const routes = routesByGroup.get(key) ?? /* @__PURE__ */ new Set();
|
|
107
|
+
routes.add(route);
|
|
108
|
+
routesByGroup.set(key, routes);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
const sharedGroups = new Set(
|
|
112
|
+
[...routesByGroup].filter(([, routes]) => routes.size >= 2).map(([key]) => key)
|
|
113
|
+
);
|
|
114
|
+
const sharedChrome = [];
|
|
115
|
+
const seenShared = /* @__PURE__ */ new Set();
|
|
116
|
+
const trimmedPerRoute = perRoute.map(({ route, findings }) => ({
|
|
117
|
+
route,
|
|
118
|
+
findings: findings.filter((finding) => !sharedGroups.has(groupKey(finding)))
|
|
119
|
+
}));
|
|
120
|
+
for (const { findings } of perRoute) {
|
|
121
|
+
for (const finding of findings) {
|
|
122
|
+
const key = groupKey(finding);
|
|
123
|
+
if (!sharedGroups.has(key) || seenShared.has(key)) continue;
|
|
124
|
+
seenShared.add(key);
|
|
125
|
+
const routes = [...routesByGroup.get(key) ?? []].sort();
|
|
126
|
+
sharedChrome.push({
|
|
127
|
+
...finding,
|
|
128
|
+
message: `${finding.message} (shared across ${routes.length} routes: ${routes.join(", ")})`
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return { sharedChrome, perRoute: trimmedPerRoute };
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
// src/compare/diff.ts
|
|
136
|
+
var matchesGlob = (pattern, value) => {
|
|
137
|
+
if (!pattern.includes("*")) return pattern === value;
|
|
138
|
+
const escaped = pattern.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*");
|
|
139
|
+
return new RegExp(`^${escaped}$`).test(value);
|
|
140
|
+
};
|
|
141
|
+
var multisetMissing = (from, to) => {
|
|
142
|
+
const remaining = /* @__PURE__ */ new Map();
|
|
143
|
+
for (const value of to) remaining.set(value, (remaining.get(value) ?? 0) + 1);
|
|
144
|
+
const missing = [];
|
|
145
|
+
for (const value of from) {
|
|
146
|
+
const count = remaining.get(value) ?? 0;
|
|
147
|
+
if (count > 0) remaining.set(value, count - 1);
|
|
148
|
+
else missing.push(value);
|
|
149
|
+
}
|
|
150
|
+
return missing;
|
|
151
|
+
};
|
|
152
|
+
var signature = (regionKey, tokens) => `${regionKey}::${[...tokens].sort().join("|")}`;
|
|
153
|
+
var regionRemovalRule = (rules, key) => rules.find(
|
|
154
|
+
(rule) => rule.value === void 0 && rule.region !== void 0 && matchesGlob(rule.region, key)
|
|
155
|
+
);
|
|
156
|
+
var valueRuleMatches = (rule, key, value) => rule.value !== void 0 && matchesGlob(rule.value, value) && (rule.region === void 0 || matchesGlob(rule.region, key));
|
|
157
|
+
var removedFinding = (region, rule) => ({
|
|
158
|
+
regionKey: region.key,
|
|
159
|
+
kind: "region-removed",
|
|
160
|
+
valueSignature: signature(region.key, region.values),
|
|
161
|
+
message: rule === void 0 ? `region "${region.label}" was removed` : `region "${region.label}" removed as expected`,
|
|
162
|
+
severity: rule === void 0 ? "serious" : "minor",
|
|
163
|
+
...rule !== void 0 && { suppressed: { reason: rule.reason } }
|
|
164
|
+
});
|
|
165
|
+
var degradedFinding = (region) => ({
|
|
166
|
+
regionKey: region.key,
|
|
167
|
+
kind: "region-degraded",
|
|
168
|
+
valueSignature: signature(region.key, [region.state]),
|
|
169
|
+
message: `region "${region.label}" degraded to ${region.state} state \u2014 not compared as value loss`,
|
|
170
|
+
severity: "minor"
|
|
171
|
+
});
|
|
172
|
+
var addedFinding = (region) => ({
|
|
173
|
+
regionKey: region.key,
|
|
174
|
+
kind: "region-added",
|
|
175
|
+
valueSignature: signature(region.key, []),
|
|
176
|
+
message: `region "${region.label}" is new`,
|
|
177
|
+
severity: "minor"
|
|
178
|
+
});
|
|
179
|
+
var diffRegionValues = (oldRegion, newRegion, rules) => {
|
|
180
|
+
const missing = multisetMissing(oldRegion.values, newRegion.values);
|
|
181
|
+
if (missing.length === 0) return [];
|
|
182
|
+
const active = [];
|
|
183
|
+
const suppressed = [];
|
|
184
|
+
const reasons = /* @__PURE__ */ new Set();
|
|
185
|
+
for (const value of missing) {
|
|
186
|
+
const rule = rules.find((r) => valueRuleMatches(r, newRegion.key, value));
|
|
187
|
+
if (rule === void 0) active.push(value);
|
|
188
|
+
else {
|
|
189
|
+
suppressed.push(value);
|
|
190
|
+
reasons.add(rule.reason);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
const findings = [];
|
|
194
|
+
if (active.length > 0) {
|
|
195
|
+
findings.push({
|
|
196
|
+
regionKey: newRegion.key,
|
|
197
|
+
kind: "value-missing",
|
|
198
|
+
valueSignature: signature(newRegion.key, active),
|
|
199
|
+
message: `region "${newRegion.label}" is missing ${active.length} value(s)`,
|
|
200
|
+
severity: "moderate",
|
|
201
|
+
detail: active.join(", ")
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
if (suppressed.length > 0) {
|
|
205
|
+
findings.push({
|
|
206
|
+
regionKey: newRegion.key,
|
|
207
|
+
kind: "value-missing",
|
|
208
|
+
valueSignature: signature(newRegion.key, suppressed),
|
|
209
|
+
message: `region "${newRegion.label}" is missing ${suppressed.length} expected value(s)`,
|
|
210
|
+
severity: "minor",
|
|
211
|
+
detail: suppressed.join(", "),
|
|
212
|
+
suppressed: { reason: [...reasons].join("; ") }
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
return findings;
|
|
216
|
+
};
|
|
217
|
+
var diffSnapshots = (oldSnap, newSnap, config = {}) => {
|
|
218
|
+
const rules = config.expectedAbsent ?? [];
|
|
219
|
+
const newByKey = new Map(newSnap.regions.map((region) => [region.key, region]));
|
|
220
|
+
const oldKeys = new Set(oldSnap.regions.map((region) => region.key));
|
|
221
|
+
const findings = [];
|
|
222
|
+
for (const oldRegion of oldSnap.regions) {
|
|
223
|
+
const newRegion = newByKey.get(oldRegion.key);
|
|
224
|
+
if (newRegion === void 0) {
|
|
225
|
+
findings.push(removedFinding(oldRegion, regionRemovalRule(rules, oldRegion.key)));
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
if (oldRegion.state === "ok" && newRegion.state !== "ok") {
|
|
229
|
+
findings.push(degradedFinding(newRegion));
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
findings.push(...diffRegionValues(oldRegion, newRegion, rules));
|
|
233
|
+
}
|
|
234
|
+
for (const newRegion of newSnap.regions) {
|
|
235
|
+
if (!oldKeys.has(newRegion.key)) findings.push(addedFinding(newRegion));
|
|
236
|
+
}
|
|
237
|
+
return findings;
|
|
238
|
+
};
|
|
239
|
+
var comparePass = (findings) => !findings.some(
|
|
240
|
+
(f) => f.suppressed === void 0 && (f.severity === "serious" || f.severity === "critical")
|
|
241
|
+
);
|
|
242
|
+
|
|
243
|
+
// src/compare/regions.ts
|
|
244
|
+
var CANDIDATE_SELECTOR = [
|
|
245
|
+
"[data-region]",
|
|
246
|
+
"main",
|
|
247
|
+
"nav",
|
|
248
|
+
"header",
|
|
249
|
+
"aside",
|
|
250
|
+
"footer",
|
|
251
|
+
"section[aria-label]",
|
|
252
|
+
"[role=main]",
|
|
253
|
+
"[role=navigation]",
|
|
254
|
+
"[role=banner]",
|
|
255
|
+
"[role=complementary]",
|
|
256
|
+
"[role=contentinfo]",
|
|
257
|
+
"[role=region]"
|
|
258
|
+
].join(",");
|
|
259
|
+
var DEFAULT_EMPTY_MARKERS = [
|
|
260
|
+
"no data",
|
|
261
|
+
"no results",
|
|
262
|
+
"nothing to show",
|
|
263
|
+
"nothing here",
|
|
264
|
+
"get started"
|
|
265
|
+
];
|
|
266
|
+
var DEFAULT_ERROR_MARKERS = [
|
|
267
|
+
"something went wrong",
|
|
268
|
+
"failed to load",
|
|
269
|
+
"could not load",
|
|
270
|
+
"unavailable",
|
|
271
|
+
"try again"
|
|
272
|
+
];
|
|
273
|
+
var extractRegions = async (page, config = {}) => {
|
|
274
|
+
const emptyMarkers = [...DEFAULT_EMPTY_MARKERS, ...config.emptyStateMarkers ?? []];
|
|
275
|
+
const errorMarkers = [...DEFAULT_ERROR_MARKERS, ...config.errorStateMarkers ?? []];
|
|
276
|
+
return page.evaluate(
|
|
277
|
+
({ selector, emptyMarkers: emptyMarkers2, errorMarkers: errorMarkers2 }) => {
|
|
278
|
+
const STATES = ["ok", "empty", "error", "loading"];
|
|
279
|
+
const isState = (value) => STATES.includes(value);
|
|
280
|
+
const slugify = (raw) => raw.toLowerCase().trim().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 60) || "region";
|
|
281
|
+
const roleOf = (el) => {
|
|
282
|
+
const explicit = el.getAttribute("role");
|
|
283
|
+
if (explicit !== null && explicit !== "") return explicit;
|
|
284
|
+
const tag = el.tagName.toLowerCase();
|
|
285
|
+
if (tag === "main") return "main";
|
|
286
|
+
if (tag === "nav") return "navigation";
|
|
287
|
+
if (tag === "aside") return "complementary";
|
|
288
|
+
if (tag === "header" || tag === "footer") {
|
|
289
|
+
if (el.closest("main,nav,section,article,aside") !== null) return void 0;
|
|
290
|
+
return tag === "header" ? "banner" : "contentinfo";
|
|
291
|
+
}
|
|
292
|
+
return void 0;
|
|
293
|
+
};
|
|
294
|
+
const headingOf = (el) => {
|
|
295
|
+
const heading = el.querySelector("h1,h2,h3,h4,h5,h6");
|
|
296
|
+
const text = heading?.textContent?.trim();
|
|
297
|
+
return text !== void 0 && text !== "" ? text : void 0;
|
|
298
|
+
};
|
|
299
|
+
const keyOf = (el) => {
|
|
300
|
+
const region = el.getAttribute("data-region");
|
|
301
|
+
if (region !== null && region !== "") return slugify(region);
|
|
302
|
+
const testid = el.getAttribute("data-testid");
|
|
303
|
+
if (testid !== null && testid !== "") return slugify(testid);
|
|
304
|
+
const role = roleOf(el);
|
|
305
|
+
if (role !== void 0) return role;
|
|
306
|
+
const label = el.getAttribute("aria-label");
|
|
307
|
+
if (label !== null && label !== "") return slugify(label);
|
|
308
|
+
const heading = headingOf(el);
|
|
309
|
+
if (heading !== void 0) return slugify(heading);
|
|
310
|
+
return slugify(el.tagName);
|
|
311
|
+
};
|
|
312
|
+
const labelOf = (el, key) => {
|
|
313
|
+
const label = el.getAttribute("aria-label");
|
|
314
|
+
if (label !== null && label.trim() !== "") return label.trim();
|
|
315
|
+
const heading = headingOf(el);
|
|
316
|
+
if (heading !== void 0) return heading;
|
|
317
|
+
const testid = el.getAttribute("data-testid");
|
|
318
|
+
if (testid !== null && testid !== "") return testid;
|
|
319
|
+
return key;
|
|
320
|
+
};
|
|
321
|
+
const textOf = (el) => (el.innerText || el.textContent || "").trim();
|
|
322
|
+
const stateOf = (el, text) => {
|
|
323
|
+
const explicit = (el.getAttribute("data-state") ?? "").toLowerCase();
|
|
324
|
+
if (isState(explicit)) return explicit;
|
|
325
|
+
const lower = text.toLowerCase();
|
|
326
|
+
if (errorMarkers2.some((marker) => lower.includes(marker))) return "error";
|
|
327
|
+
if (lower.includes("loading")) return "loading";
|
|
328
|
+
if (emptyMarkers2.some((marker) => lower.includes(marker))) return "empty";
|
|
329
|
+
return "ok";
|
|
330
|
+
};
|
|
331
|
+
const valuesOf = (text) => (text.match(/-?\$?\s?\d[\d,]*(?:\.\d+)?%?/g) ?? []).map((token) => token.replace(/[$\s,]/g, "")).filter((token) => token !== "" && token !== "-");
|
|
332
|
+
const qualifies = (el) => {
|
|
333
|
+
const dataRegion = el.getAttribute("data-region");
|
|
334
|
+
if (dataRegion !== null && dataRegion !== "") {
|
|
335
|
+
const parent = el.parentElement;
|
|
336
|
+
return parent === null || parent.closest("[data-region]") === null;
|
|
337
|
+
}
|
|
338
|
+
return el.querySelector(selector) === null && el.closest("[data-region]") === null;
|
|
339
|
+
};
|
|
340
|
+
const candidates = Array.from(document.querySelectorAll(selector));
|
|
341
|
+
const regions = candidates.filter(qualifies);
|
|
342
|
+
const elements = regions.length > 0 ? regions : [document.body];
|
|
343
|
+
const used = {};
|
|
344
|
+
const uniqueKey = (key) => {
|
|
345
|
+
const count = (used[key] ?? 0) + 1;
|
|
346
|
+
used[key] = count;
|
|
347
|
+
return count === 1 ? key : `${key}-${count}`;
|
|
348
|
+
};
|
|
349
|
+
return elements.map((el) => {
|
|
350
|
+
const text = textOf(el);
|
|
351
|
+
const key = uniqueKey(keyOf(el));
|
|
352
|
+
return {
|
|
353
|
+
key,
|
|
354
|
+
label: labelOf(el, key),
|
|
355
|
+
state: stateOf(el, text),
|
|
356
|
+
values: valuesOf(text)
|
|
357
|
+
};
|
|
358
|
+
});
|
|
359
|
+
},
|
|
360
|
+
{ selector: CANDIDATE_SELECTOR, emptyMarkers, errorMarkers }
|
|
361
|
+
);
|
|
362
|
+
};
|
|
363
|
+
var REGION_STATES = ["ok", "empty", "error", "loading"];
|
|
364
|
+
var Region = zod.z.object({
|
|
365
|
+
/** Stable id: data-region → data-testid → role → aria-label → heading slug. */
|
|
366
|
+
key: zod.z.string(),
|
|
367
|
+
/** Human label for the report. */
|
|
368
|
+
label: zod.z.string(),
|
|
369
|
+
state: zod.z.enum(REGION_STATES),
|
|
370
|
+
/** Normalized numeric / currency / percent tokens found in the region. */
|
|
371
|
+
values: zod.z.array(zod.z.string())
|
|
372
|
+
});
|
|
373
|
+
var CompareSnapshot = zod.z.object({
|
|
374
|
+
/** Route the snapshot was captured at, e.g. `/financials`. */
|
|
375
|
+
route: zod.z.string(),
|
|
376
|
+
regions: zod.z.array(Region)
|
|
377
|
+
}).refine(
|
|
378
|
+
(snap) => new Set(snap.regions.map((region) => region.key)).size === snap.regions.length,
|
|
379
|
+
{
|
|
380
|
+
message: "region keys must be unique"
|
|
381
|
+
}
|
|
382
|
+
);
|
|
383
|
+
var ExpectedAbsentRule = zod.z.object({
|
|
384
|
+
region: zod.z.string().optional(),
|
|
385
|
+
value: zod.z.string().optional(),
|
|
386
|
+
reason: zod.z.string().min(1)
|
|
387
|
+
}).refine((rule) => rule.region !== void 0 || rule.value !== void 0, {
|
|
388
|
+
message: "expectedAbsent rule needs at least one of `region` or `value`"
|
|
389
|
+
});
|
|
390
|
+
var CompareConfig = zod.z.object({
|
|
391
|
+
expectedAbsent: zod.z.array(ExpectedAbsentRule).optional(),
|
|
392
|
+
emptyStateMarkers: zod.z.array(zod.z.string()).optional(),
|
|
393
|
+
errorStateMarkers: zod.z.array(zod.z.string()).optional()
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
// src/compare/snapshot.ts
|
|
397
|
+
var captureSnapshot = async (page, route, config) => ({
|
|
398
|
+
route,
|
|
399
|
+
regions: await extractRegions(page, config)
|
|
400
|
+
});
|
|
401
|
+
var loadSnapshot = async (path) => {
|
|
402
|
+
const raw = await promises.readFile(path, "utf8");
|
|
403
|
+
return CompareSnapshot.parse(JSON.parse(raw));
|
|
404
|
+
};
|
|
405
|
+
|
|
406
|
+
// src/compare/runner.ts
|
|
407
|
+
var DEFAULT_TIMEOUT_MS2 = 3e4;
|
|
408
|
+
var joinUrl = (base, route) => `${base.replace(/\/$/, "")}/${route.replace(/^\//, "")}`;
|
|
409
|
+
var routeSnapshotFile = (route) => `${route.replace(/^\//, "").replace(/\/+$/, "").replace(/\//g, "_") || "index"}.json`;
|
|
410
|
+
var compareRoutes = async (options) => {
|
|
411
|
+
if (options.oldBaseUrl === void 0 === (options.oldSnapshotDir === void 0)) {
|
|
412
|
+
throw new Error("compareRoutes: provide exactly one of oldBaseUrl or oldSnapshotDir");
|
|
413
|
+
}
|
|
414
|
+
const viewport = options.viewport ?? DESKTOP_VIEWPORT;
|
|
415
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS2;
|
|
416
|
+
const browser = options.browser ?? await playwright.chromium.launch();
|
|
417
|
+
const ownsBrowser = options.browser === void 0;
|
|
418
|
+
const { oldBaseUrl, oldSnapshotDir } = options;
|
|
419
|
+
const snapshotUrl = async (url, route) => {
|
|
420
|
+
const context = await browser.newContext({
|
|
421
|
+
viewport: { width: viewport.width, height: viewport.height }
|
|
422
|
+
});
|
|
423
|
+
const page = await context.newPage();
|
|
424
|
+
try {
|
|
425
|
+
await page.goto(url, { waitUntil: "networkidle", timeout: timeoutMs });
|
|
426
|
+
await prepareForScreenshot(page);
|
|
427
|
+
return await captureSnapshot(page, route, options.config);
|
|
428
|
+
} finally {
|
|
429
|
+
await context.close();
|
|
430
|
+
}
|
|
431
|
+
};
|
|
432
|
+
const oldSnapshotFor = (route) => {
|
|
433
|
+
if (oldBaseUrl !== void 0) return snapshotUrl(joinUrl(oldBaseUrl, route), route);
|
|
434
|
+
if (oldSnapshotDir !== void 0) {
|
|
435
|
+
return loadSnapshot(path.join(oldSnapshotDir, routeSnapshotFile(route)));
|
|
436
|
+
}
|
|
437
|
+
throw new Error("compareRoutes: provide exactly one of oldBaseUrl or oldSnapshotDir");
|
|
438
|
+
};
|
|
439
|
+
try {
|
|
440
|
+
const perRoute = [];
|
|
441
|
+
for (const route of options.routes) {
|
|
442
|
+
const oldSnapshot = await oldSnapshotFor(route);
|
|
443
|
+
const newSnapshot = await snapshotUrl(joinUrl(options.newBaseUrl, route), route);
|
|
444
|
+
perRoute.push({ route, findings: diffSnapshots(oldSnapshot, newSnapshot, options.config) });
|
|
445
|
+
}
|
|
446
|
+
const { sharedChrome, perRoute: trimmed } = dedupeSharedChrome(perRoute);
|
|
447
|
+
const routes = trimmed.map(({ route, findings }) => ({
|
|
448
|
+
route,
|
|
449
|
+
findings,
|
|
450
|
+
pass: comparePass(findings)
|
|
451
|
+
}));
|
|
452
|
+
const pass = comparePass(sharedChrome) && routes.every((route) => route.pass);
|
|
453
|
+
return { routes, sharedChrome, pass };
|
|
454
|
+
} finally {
|
|
455
|
+
if (ownsBrowser) await browser.close();
|
|
456
|
+
}
|
|
457
|
+
};
|
|
23
458
|
var IMPACTS = ["critical", "serious", "moderate", "minor"];
|
|
24
459
|
var toSeverity = (impact) => impact && IMPACTS.includes(impact) ? impact : "minor";
|
|
25
460
|
var wcagTag = (tags) => tags.find((tag) => tag.startsWith("wcag")) ?? tags[0];
|
|
@@ -174,6 +609,28 @@ ${domExcerpt}`
|
|
|
174
609
|
}
|
|
175
610
|
};
|
|
176
611
|
|
|
612
|
+
// src/axes/compare.ts
|
|
613
|
+
var skip2 = (message) => ({
|
|
614
|
+
axis: "compare",
|
|
615
|
+
pass: true,
|
|
616
|
+
findings: [{ message, severity: "minor" }]
|
|
617
|
+
});
|
|
618
|
+
var compareAxis = {
|
|
619
|
+
name: "compare",
|
|
620
|
+
run: async (context) => {
|
|
621
|
+
const { comparison, compareConfig, page } = context;
|
|
622
|
+
if (comparison === void 0) {
|
|
623
|
+
return skip2("no comparison snapshot provided; old-vs-new not assessed");
|
|
624
|
+
}
|
|
625
|
+
if (page === void 0) {
|
|
626
|
+
return skip2("compare needs a live page to extract the new snapshot");
|
|
627
|
+
}
|
|
628
|
+
const newSnapshot = await captureSnapshot(page, comparison.route, compareConfig);
|
|
629
|
+
const findings = diffSnapshots(comparison, newSnapshot, compareConfig);
|
|
630
|
+
return { axis: "compare", pass: comparePass(findings), findings };
|
|
631
|
+
}
|
|
632
|
+
};
|
|
633
|
+
|
|
177
634
|
// src/axes/runtime-health.ts
|
|
178
635
|
var ERROR_MARKERS = [
|
|
179
636
|
"something went wrong",
|
|
@@ -353,85 +810,10 @@ var ALL_AXES = [
|
|
|
353
810
|
runtimeHealthAxis,
|
|
354
811
|
accessibilityAxis,
|
|
355
812
|
visualBrandAxis,
|
|
356
|
-
briefConformanceAxis
|
|
813
|
+
briefConformanceAxis,
|
|
814
|
+
compareAxis
|
|
357
815
|
];
|
|
358
816
|
|
|
359
|
-
// src/prepare-screenshot.ts
|
|
360
|
-
var disableAnimations = async (page) => {
|
|
361
|
-
await page.addStyleTag({
|
|
362
|
-
content: `*, *::before, *::after {
|
|
363
|
-
animation-duration: 0s !important;
|
|
364
|
-
animation-delay: 0s !important;
|
|
365
|
-
transition-duration: 0s !important;
|
|
366
|
-
transition-delay: 0s !important;
|
|
367
|
-
caret-color: transparent !important;
|
|
368
|
-
}`
|
|
369
|
-
});
|
|
370
|
-
};
|
|
371
|
-
var parkMouseOffElements = async (page) => {
|
|
372
|
-
await page.mouse.move(0, 0);
|
|
373
|
-
};
|
|
374
|
-
var waitForFonts = async (page) => {
|
|
375
|
-
await page.evaluate(() => document.fonts.ready.then(() => void 0));
|
|
376
|
-
};
|
|
377
|
-
var prepareForScreenshot = async (page) => {
|
|
378
|
-
await disableAnimations(page);
|
|
379
|
-
await parkMouseOffElements(page);
|
|
380
|
-
await waitForFonts(page);
|
|
381
|
-
};
|
|
382
|
-
|
|
383
|
-
// src/capture.ts
|
|
384
|
-
var DESKTOP_VIEWPORT = { label: "desktop", width: 1280, height: 800 };
|
|
385
|
-
var DEFAULT_TIMEOUT_MS = 3e4;
|
|
386
|
-
var attachRecorders = (page) => {
|
|
387
|
-
const consoleEntries = [];
|
|
388
|
-
const networkEntries = [];
|
|
389
|
-
page.on("console", (message) => {
|
|
390
|
-
consoleEntries.push({
|
|
391
|
-
type: message.type(),
|
|
392
|
-
text: message.text(),
|
|
393
|
-
location: message.location().url || void 0
|
|
394
|
-
});
|
|
395
|
-
});
|
|
396
|
-
page.on("pageerror", (error) => {
|
|
397
|
-
consoleEntries.push({ type: "pageerror", text: error.message });
|
|
398
|
-
});
|
|
399
|
-
page.on("requestfailed", (request) => {
|
|
400
|
-
networkEntries.push({
|
|
401
|
-
url: request.url(),
|
|
402
|
-
method: request.method(),
|
|
403
|
-
status: 0,
|
|
404
|
-
failure: request.failure()?.errorText
|
|
405
|
-
});
|
|
406
|
-
});
|
|
407
|
-
page.on("response", (response) => {
|
|
408
|
-
networkEntries.push({
|
|
409
|
-
url: response.url(),
|
|
410
|
-
method: response.request().method(),
|
|
411
|
-
status: response.status()
|
|
412
|
-
});
|
|
413
|
-
});
|
|
414
|
-
return { console: consoleEntries, network: networkEntries };
|
|
415
|
-
};
|
|
416
|
-
var capturePage = async (page, url, viewport, timeoutMs = DEFAULT_TIMEOUT_MS) => {
|
|
417
|
-
const recorders = attachRecorders(page);
|
|
418
|
-
await page.setViewportSize({ width: viewport.width, height: viewport.height });
|
|
419
|
-
await page.goto(url, { waitUntil: "networkidle", timeout: timeoutMs });
|
|
420
|
-
await prepareForScreenshot(page);
|
|
421
|
-
const screenshot = await page.screenshot({ fullPage: true });
|
|
422
|
-
const dom = await page.content();
|
|
423
|
-
const renderedText = (await page.evaluate(() => document.body.innerText)).trim();
|
|
424
|
-
return {
|
|
425
|
-
url,
|
|
426
|
-
viewport,
|
|
427
|
-
screenshot,
|
|
428
|
-
dom,
|
|
429
|
-
renderedText,
|
|
430
|
-
console: recorders.console,
|
|
431
|
-
network: recorders.network
|
|
432
|
-
};
|
|
433
|
-
};
|
|
434
|
-
|
|
435
817
|
// src/aggregate.ts
|
|
436
818
|
var runAxes = async (axes, context) => {
|
|
437
819
|
const verdicts = [];
|
|
@@ -473,6 +855,8 @@ var validate = async (options) => {
|
|
|
473
855
|
browser,
|
|
474
856
|
brief: options.brief,
|
|
475
857
|
baseline: options.baseline,
|
|
858
|
+
comparison: options.comparison,
|
|
859
|
+
compareConfig: options.compareConfig,
|
|
476
860
|
captureUnderBrand: async (brand) => {
|
|
477
861
|
const brandContext = await browser.newContext({
|
|
478
862
|
viewport: { width: viewport.width, height: viewport.height }
|
|
@@ -572,16 +956,24 @@ var HELP = `@keboola/validate-ui \u2014 closed-loop validation for generated Keb
|
|
|
572
956
|
Usage:
|
|
573
957
|
validate-ui --url <url> [--brief <text>] [--json]
|
|
574
958
|
validate-ui --serve <dir> [--route <path>] [--brief <text>] [--json]
|
|
959
|
+
validate-ui compare --new <baseUrl> (--old <baseUrl> | --old-snapshots <dir>) --routes <a,b,c> [--config <file>] [--json]
|
|
575
960
|
|
|
576
961
|
Options:
|
|
577
|
-
--url <url>
|
|
578
|
-
--serve <dir>
|
|
579
|
-
--route <path>
|
|
580
|
-
--brief <text>
|
|
581
|
-
--json
|
|
582
|
-
--help, -h
|
|
962
|
+
--url <url> Render and validate a running URL
|
|
963
|
+
--serve <dir> Serve a built SPA dir (e.g. apps/boilerplate/dist) and validate it
|
|
964
|
+
--route <path> Route to open when using --serve (default: /)
|
|
965
|
+
--brief <text> The brief the UI was generated from (feeds brief-conformance)
|
|
966
|
+
--json Emit the verdict as JSON
|
|
967
|
+
--help, -h Show this help
|
|
583
968
|
|
|
584
|
-
|
|
969
|
+
compare options:
|
|
970
|
+
--new <baseUrl> Base URL of the new build
|
|
971
|
+
--old <baseUrl> Base URL of the old build (booted live)
|
|
972
|
+
--old-snapshots <dir> Directory of recorded old snapshots (alternative to --old)
|
|
973
|
+
--routes <a,b,c> Comma-separated routes to compare
|
|
974
|
+
--config <file> JSON compare config (expectedAbsent allowlist + state markers)
|
|
975
|
+
|
|
976
|
+
Exit code is 0 when every axis / route passes, 1 otherwise.
|
|
585
977
|
`;
|
|
586
978
|
var flagValue = (argv, name) => {
|
|
587
979
|
const index = argv.indexOf(name);
|
|
@@ -599,14 +991,71 @@ var formatText = (verdict) => {
|
|
|
599
991
|
}
|
|
600
992
|
return lines.join("\n");
|
|
601
993
|
};
|
|
994
|
+
var formatCompareFinding = (finding) => {
|
|
995
|
+
const tag = finding.suppressed !== void 0 ? " (suppressed)" : "";
|
|
996
|
+
const detail = finding.detail !== void 0 ? ` \u2014 ${finding.detail}` : "";
|
|
997
|
+
return ` [${finding.severity}] ${finding.message}${detail}${tag}`;
|
|
998
|
+
};
|
|
999
|
+
var formatCompare = (result) => {
|
|
1000
|
+
const lines = [`validate-ui compare ${result.pass ? "PASS" : "FAIL"}`];
|
|
1001
|
+
if (result.sharedChrome.length > 0) {
|
|
1002
|
+
lines.push(" shared across routes:");
|
|
1003
|
+
for (const finding of result.sharedChrome) lines.push(formatCompareFinding(finding));
|
|
1004
|
+
}
|
|
1005
|
+
for (const route of result.routes) {
|
|
1006
|
+
lines.push(` ${route.pass ? "\u2713" : "\u2717"} ${route.route}`);
|
|
1007
|
+
for (const finding of route.findings) lines.push(formatCompareFinding(finding));
|
|
1008
|
+
}
|
|
1009
|
+
return lines.join("\n");
|
|
1010
|
+
};
|
|
1011
|
+
var loadCompareConfig = async (path$1) => {
|
|
1012
|
+
if (path$1 === void 0) return void 0;
|
|
1013
|
+
return CompareConfig.parse(JSON.parse(await promises.readFile(path.resolve(path$1), "utf8")));
|
|
1014
|
+
};
|
|
1015
|
+
var runCompare = async (argv, asJson) => {
|
|
1016
|
+
const newBaseUrl = flagValue(argv, "--new");
|
|
1017
|
+
const oldBaseUrl = flagValue(argv, "--old");
|
|
1018
|
+
const oldSnapshotDir = flagValue(argv, "--old-snapshots");
|
|
1019
|
+
const routes = flagValue(argv, "--routes")?.split(",").map((route) => route.trim()).filter((route) => route !== "");
|
|
1020
|
+
if (newBaseUrl === void 0 || routes === void 0 || routes.length === 0) {
|
|
1021
|
+
process2__default.default.stderr.write("validate-ui compare: --new and --routes are required\n\n" + HELP);
|
|
1022
|
+
process2__default.default.exitCode = 1;
|
|
1023
|
+
return;
|
|
1024
|
+
}
|
|
1025
|
+
if (oldBaseUrl !== void 0 && oldSnapshotDir !== void 0) {
|
|
1026
|
+
process2__default.default.stderr.write(
|
|
1027
|
+
"validate-ui compare: pass only one of --old or --old-snapshots\n\n" + HELP
|
|
1028
|
+
);
|
|
1029
|
+
process2__default.default.exitCode = 1;
|
|
1030
|
+
return;
|
|
1031
|
+
}
|
|
1032
|
+
const config = await loadCompareConfig(flagValue(argv, "--config"));
|
|
1033
|
+
const base = { newBaseUrl, routes, config };
|
|
1034
|
+
let result;
|
|
1035
|
+
if (oldBaseUrl !== void 0) {
|
|
1036
|
+
result = await compareRoutes({ ...base, oldBaseUrl });
|
|
1037
|
+
} else if (oldSnapshotDir !== void 0) {
|
|
1038
|
+
result = await compareRoutes({ ...base, oldSnapshotDir });
|
|
1039
|
+
} else {
|
|
1040
|
+
process2__default.default.stderr.write("validate-ui compare: provide --old or --old-snapshots\n\n" + HELP);
|
|
1041
|
+
process2__default.default.exitCode = 1;
|
|
1042
|
+
return;
|
|
1043
|
+
}
|
|
1044
|
+
process2__default.default.stdout.write((asJson ? JSON.stringify(result, null, 2) : formatCompare(result)) + "\n");
|
|
1045
|
+
process2__default.default.exitCode = result.pass ? 0 : 1;
|
|
1046
|
+
};
|
|
602
1047
|
var main = async () => {
|
|
603
1048
|
const argv = process2__default.default.argv.slice(2);
|
|
604
1049
|
if (argv.includes("--help") || argv.includes("-h") || argv.length === 0) {
|
|
605
1050
|
process2__default.default.stdout.write(HELP);
|
|
606
1051
|
return;
|
|
607
1052
|
}
|
|
608
|
-
const brief = flagValue(argv, "--brief");
|
|
609
1053
|
const asJson = argv.includes("--json");
|
|
1054
|
+
if (argv[0] === "compare") {
|
|
1055
|
+
await runCompare(argv.slice(1), asJson);
|
|
1056
|
+
return;
|
|
1057
|
+
}
|
|
1058
|
+
const brief = flagValue(argv, "--brief");
|
|
610
1059
|
const serveDir = flagValue(argv, "--serve");
|
|
611
1060
|
let url = flagValue(argv, "--url");
|
|
612
1061
|
let server;
|