@keboola/validate-ui 0.1.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/index.cjs CHANGED
@@ -341,6 +341,302 @@ ${domExcerpt}`
341
341
  }
342
342
  };
343
343
 
344
+ // src/compare/diff.ts
345
+ var matchesGlob = (pattern, value) => {
346
+ if (!pattern.includes("*")) return pattern === value;
347
+ const escaped = pattern.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*");
348
+ return new RegExp(`^${escaped}$`).test(value);
349
+ };
350
+ var multisetMissing = (from, to) => {
351
+ const remaining = /* @__PURE__ */ new Map();
352
+ for (const value of to) remaining.set(value, (remaining.get(value) ?? 0) + 1);
353
+ const missing = [];
354
+ for (const value of from) {
355
+ const count = remaining.get(value) ?? 0;
356
+ if (count > 0) remaining.set(value, count - 1);
357
+ else missing.push(value);
358
+ }
359
+ return missing;
360
+ };
361
+ var signature = (regionKey, tokens) => `${regionKey}::${[...tokens].sort().join("|")}`;
362
+ var regionRemovalRule = (rules, key) => rules.find(
363
+ (rule) => rule.value === void 0 && rule.region !== void 0 && matchesGlob(rule.region, key)
364
+ );
365
+ var valueRuleMatches = (rule, key, value) => rule.value !== void 0 && matchesGlob(rule.value, value) && (rule.region === void 0 || matchesGlob(rule.region, key));
366
+ var removedFinding = (region, rule) => ({
367
+ regionKey: region.key,
368
+ kind: "region-removed",
369
+ valueSignature: signature(region.key, region.values),
370
+ message: rule === void 0 ? `region "${region.label}" was removed` : `region "${region.label}" removed as expected`,
371
+ severity: rule === void 0 ? "serious" : "minor",
372
+ ...rule !== void 0 && { suppressed: { reason: rule.reason } }
373
+ });
374
+ var degradedFinding = (region) => ({
375
+ regionKey: region.key,
376
+ kind: "region-degraded",
377
+ valueSignature: signature(region.key, [region.state]),
378
+ message: `region "${region.label}" degraded to ${region.state} state \u2014 not compared as value loss`,
379
+ severity: "minor"
380
+ });
381
+ var addedFinding = (region) => ({
382
+ regionKey: region.key,
383
+ kind: "region-added",
384
+ valueSignature: signature(region.key, []),
385
+ message: `region "${region.label}" is new`,
386
+ severity: "minor"
387
+ });
388
+ var diffRegionValues = (oldRegion, newRegion, rules) => {
389
+ const missing = multisetMissing(oldRegion.values, newRegion.values);
390
+ if (missing.length === 0) return [];
391
+ const active = [];
392
+ const suppressed = [];
393
+ const reasons = /* @__PURE__ */ new Set();
394
+ for (const value of missing) {
395
+ const rule = rules.find((r) => valueRuleMatches(r, newRegion.key, value));
396
+ if (rule === void 0) active.push(value);
397
+ else {
398
+ suppressed.push(value);
399
+ reasons.add(rule.reason);
400
+ }
401
+ }
402
+ const findings = [];
403
+ if (active.length > 0) {
404
+ findings.push({
405
+ regionKey: newRegion.key,
406
+ kind: "value-missing",
407
+ valueSignature: signature(newRegion.key, active),
408
+ message: `region "${newRegion.label}" is missing ${active.length} value(s)`,
409
+ severity: "moderate",
410
+ detail: active.join(", ")
411
+ });
412
+ }
413
+ if (suppressed.length > 0) {
414
+ findings.push({
415
+ regionKey: newRegion.key,
416
+ kind: "value-missing",
417
+ valueSignature: signature(newRegion.key, suppressed),
418
+ message: `region "${newRegion.label}" is missing ${suppressed.length} expected value(s)`,
419
+ severity: "minor",
420
+ detail: suppressed.join(", "),
421
+ suppressed: { reason: [...reasons].join("; ") }
422
+ });
423
+ }
424
+ return findings;
425
+ };
426
+ var diffSnapshots = (oldSnap, newSnap, config = {}) => {
427
+ const rules = config.expectedAbsent ?? [];
428
+ const newByKey = new Map(newSnap.regions.map((region) => [region.key, region]));
429
+ const oldKeys = new Set(oldSnap.regions.map((region) => region.key));
430
+ const findings = [];
431
+ for (const oldRegion of oldSnap.regions) {
432
+ const newRegion = newByKey.get(oldRegion.key);
433
+ if (newRegion === void 0) {
434
+ findings.push(removedFinding(oldRegion, regionRemovalRule(rules, oldRegion.key)));
435
+ continue;
436
+ }
437
+ if (oldRegion.state === "ok" && newRegion.state !== "ok") {
438
+ findings.push(degradedFinding(newRegion));
439
+ continue;
440
+ }
441
+ findings.push(...diffRegionValues(oldRegion, newRegion, rules));
442
+ }
443
+ for (const newRegion of newSnap.regions) {
444
+ if (!oldKeys.has(newRegion.key)) findings.push(addedFinding(newRegion));
445
+ }
446
+ return findings;
447
+ };
448
+ var comparePass = (findings) => !findings.some(
449
+ (f) => f.suppressed === void 0 && (f.severity === "serious" || f.severity === "critical")
450
+ );
451
+
452
+ // src/compare/regions.ts
453
+ var CANDIDATE_SELECTOR = [
454
+ "[data-region]",
455
+ "main",
456
+ "nav",
457
+ "header",
458
+ "aside",
459
+ "footer",
460
+ "section[aria-label]",
461
+ "[role=main]",
462
+ "[role=navigation]",
463
+ "[role=banner]",
464
+ "[role=complementary]",
465
+ "[role=contentinfo]",
466
+ "[role=region]"
467
+ ].join(",");
468
+ var DEFAULT_EMPTY_MARKERS = [
469
+ "no data",
470
+ "no results",
471
+ "nothing to show",
472
+ "nothing here",
473
+ "get started"
474
+ ];
475
+ var DEFAULT_ERROR_MARKERS = [
476
+ "something went wrong",
477
+ "failed to load",
478
+ "could not load",
479
+ "unavailable",
480
+ "try again"
481
+ ];
482
+ var extractRegions = async (page, config = {}) => {
483
+ const emptyMarkers = [...DEFAULT_EMPTY_MARKERS, ...config.emptyStateMarkers ?? []];
484
+ const errorMarkers = [...DEFAULT_ERROR_MARKERS, ...config.errorStateMarkers ?? []];
485
+ return page.evaluate(
486
+ ({ selector, emptyMarkers: emptyMarkers2, errorMarkers: errorMarkers2 }) => {
487
+ const STATES = ["ok", "empty", "error", "loading"];
488
+ const isState = (value) => STATES.includes(value);
489
+ const slugify = (raw) => raw.toLowerCase().trim().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 60) || "region";
490
+ const roleOf = (el) => {
491
+ const explicit = el.getAttribute("role");
492
+ if (explicit !== null && explicit !== "") return explicit;
493
+ const tag = el.tagName.toLowerCase();
494
+ if (tag === "main") return "main";
495
+ if (tag === "nav") return "navigation";
496
+ if (tag === "aside") return "complementary";
497
+ if (tag === "header" || tag === "footer") {
498
+ if (el.closest("main,nav,section,article,aside") !== null) return void 0;
499
+ return tag === "header" ? "banner" : "contentinfo";
500
+ }
501
+ return void 0;
502
+ };
503
+ const headingOf = (el) => {
504
+ const heading = el.querySelector("h1,h2,h3,h4,h5,h6");
505
+ const text = heading?.textContent?.trim();
506
+ return text !== void 0 && text !== "" ? text : void 0;
507
+ };
508
+ const keyOf = (el) => {
509
+ const region = el.getAttribute("data-region");
510
+ if (region !== null && region !== "") return slugify(region);
511
+ const testid = el.getAttribute("data-testid");
512
+ if (testid !== null && testid !== "") return slugify(testid);
513
+ const role = roleOf(el);
514
+ if (role !== void 0) return role;
515
+ const label = el.getAttribute("aria-label");
516
+ if (label !== null && label !== "") return slugify(label);
517
+ const heading = headingOf(el);
518
+ if (heading !== void 0) return slugify(heading);
519
+ return slugify(el.tagName);
520
+ };
521
+ const labelOf = (el, key) => {
522
+ const label = el.getAttribute("aria-label");
523
+ if (label !== null && label.trim() !== "") return label.trim();
524
+ const heading = headingOf(el);
525
+ if (heading !== void 0) return heading;
526
+ const testid = el.getAttribute("data-testid");
527
+ if (testid !== null && testid !== "") return testid;
528
+ return key;
529
+ };
530
+ const textOf = (el) => (el.innerText || el.textContent || "").trim();
531
+ const stateOf = (el, text) => {
532
+ const explicit = (el.getAttribute("data-state") ?? "").toLowerCase();
533
+ if (isState(explicit)) return explicit;
534
+ const lower = text.toLowerCase();
535
+ if (errorMarkers2.some((marker) => lower.includes(marker))) return "error";
536
+ if (lower.includes("loading")) return "loading";
537
+ if (emptyMarkers2.some((marker) => lower.includes(marker))) return "empty";
538
+ return "ok";
539
+ };
540
+ const valuesOf = (text) => (text.match(/-?\$?\s?\d[\d,]*(?:\.\d+)?%?/g) ?? []).map((token) => token.replace(/[$\s,]/g, "")).filter((token) => token !== "" && token !== "-");
541
+ const qualifies = (el) => {
542
+ const dataRegion = el.getAttribute("data-region");
543
+ if (dataRegion !== null && dataRegion !== "") {
544
+ const parent = el.parentElement;
545
+ return parent === null || parent.closest("[data-region]") === null;
546
+ }
547
+ return el.querySelector(selector) === null && el.closest("[data-region]") === null;
548
+ };
549
+ const candidates = Array.from(document.querySelectorAll(selector));
550
+ const regions = candidates.filter(qualifies);
551
+ const elements = regions.length > 0 ? regions : [document.body];
552
+ const used = {};
553
+ const uniqueKey = (key) => {
554
+ const count = (used[key] ?? 0) + 1;
555
+ used[key] = count;
556
+ return count === 1 ? key : `${key}-${count}`;
557
+ };
558
+ return elements.map((el) => {
559
+ const text = textOf(el);
560
+ const key = uniqueKey(keyOf(el));
561
+ return {
562
+ key,
563
+ label: labelOf(el, key),
564
+ state: stateOf(el, text),
565
+ values: valuesOf(text)
566
+ };
567
+ });
568
+ },
569
+ { selector: CANDIDATE_SELECTOR, emptyMarkers, errorMarkers }
570
+ );
571
+ };
572
+ var REGION_STATES = ["ok", "empty", "error", "loading"];
573
+ var Region = zod.z.object({
574
+ /** Stable id: data-region → data-testid → role → aria-label → heading slug. */
575
+ key: zod.z.string(),
576
+ /** Human label for the report. */
577
+ label: zod.z.string(),
578
+ state: zod.z.enum(REGION_STATES),
579
+ /** Normalized numeric / currency / percent tokens found in the region. */
580
+ values: zod.z.array(zod.z.string())
581
+ });
582
+ var CompareSnapshot = zod.z.object({
583
+ /** Route the snapshot was captured at, e.g. `/financials`. */
584
+ route: zod.z.string(),
585
+ regions: zod.z.array(Region)
586
+ }).refine(
587
+ (snap) => new Set(snap.regions.map((region) => region.key)).size === snap.regions.length,
588
+ {
589
+ message: "region keys must be unique"
590
+ }
591
+ );
592
+ var ExpectedAbsentRule = zod.z.object({
593
+ region: zod.z.string().optional(),
594
+ value: zod.z.string().optional(),
595
+ reason: zod.z.string().min(1)
596
+ }).refine((rule) => rule.region !== void 0 || rule.value !== void 0, {
597
+ message: "expectedAbsent rule needs at least one of `region` or `value`"
598
+ });
599
+ var CompareConfig = zod.z.object({
600
+ expectedAbsent: zod.z.array(ExpectedAbsentRule).optional(),
601
+ emptyStateMarkers: zod.z.array(zod.z.string()).optional(),
602
+ errorStateMarkers: zod.z.array(zod.z.string()).optional()
603
+ });
604
+
605
+ // src/compare/snapshot.ts
606
+ var captureSnapshot = async (page, route, config) => ({
607
+ route,
608
+ regions: await extractRegions(page, config)
609
+ });
610
+ var saveSnapshot = async (snapshot, path) => {
611
+ await promises.writeFile(path, JSON.stringify(snapshot, null, 2));
612
+ };
613
+ var loadSnapshot = async (path) => {
614
+ const raw = await promises.readFile(path, "utf8");
615
+ return CompareSnapshot.parse(JSON.parse(raw));
616
+ };
617
+
618
+ // src/axes/compare.ts
619
+ var skip2 = (message) => ({
620
+ axis: "compare",
621
+ pass: true,
622
+ findings: [{ message, severity: "minor" }]
623
+ });
624
+ var compareAxis = {
625
+ name: "compare",
626
+ run: async (context) => {
627
+ const { comparison, compareConfig, page } = context;
628
+ if (comparison === void 0) {
629
+ return skip2("no comparison snapshot provided; old-vs-new not assessed");
630
+ }
631
+ if (page === void 0) {
632
+ return skip2("compare needs a live page to extract the new snapshot");
633
+ }
634
+ const newSnapshot = await captureSnapshot(page, comparison.route, compareConfig);
635
+ const findings = diffSnapshots(comparison, newSnapshot, compareConfig);
636
+ return { axis: "compare", pass: comparePass(findings), findings };
637
+ }
638
+ };
639
+
344
640
  // src/axes/runtime-health.ts
345
641
  var ERROR_MARKERS = [
346
642
  "something went wrong",
@@ -520,7 +816,8 @@ var ALL_AXES = [
520
816
  runtimeHealthAxis,
521
817
  accessibilityAxis,
522
818
  visualBrandAxis,
523
- briefConformanceAxis
819
+ briefConformanceAxis,
820
+ compareAxis
524
821
  ];
525
822
 
526
823
  // src/aggregate.ts
@@ -564,6 +861,8 @@ var validate = async (options) => {
564
861
  browser,
565
862
  brief: options.brief,
566
863
  baseline: options.baseline,
864
+ comparison: options.comparison,
865
+ compareConfig: options.compareConfig,
567
866
  captureUnderBrand: async (brand) => {
568
867
  const brandContext = await browser.newContext({
569
868
  viewport: { width: viewport.width, height: viewport.height }
@@ -589,19 +888,121 @@ var validate = async (options) => {
589
888
  }
590
889
  };
591
890
 
891
+ // src/compare/dedup.ts
892
+ var groupKey = (finding) => `${finding.kind}::${finding.valueSignature}`;
893
+ var dedupeSharedChrome = (perRoute) => {
894
+ const routesByGroup = /* @__PURE__ */ new Map();
895
+ for (const { route, findings } of perRoute) {
896
+ for (const finding of findings) {
897
+ const key = groupKey(finding);
898
+ const routes = routesByGroup.get(key) ?? /* @__PURE__ */ new Set();
899
+ routes.add(route);
900
+ routesByGroup.set(key, routes);
901
+ }
902
+ }
903
+ const sharedGroups = new Set(
904
+ [...routesByGroup].filter(([, routes]) => routes.size >= 2).map(([key]) => key)
905
+ );
906
+ const sharedChrome = [];
907
+ const seenShared = /* @__PURE__ */ new Set();
908
+ const trimmedPerRoute = perRoute.map(({ route, findings }) => ({
909
+ route,
910
+ findings: findings.filter((finding) => !sharedGroups.has(groupKey(finding)))
911
+ }));
912
+ for (const { findings } of perRoute) {
913
+ for (const finding of findings) {
914
+ const key = groupKey(finding);
915
+ if (!sharedGroups.has(key) || seenShared.has(key)) continue;
916
+ seenShared.add(key);
917
+ const routes = [...routesByGroup.get(key) ?? []].sort();
918
+ sharedChrome.push({
919
+ ...finding,
920
+ message: `${finding.message} (shared across ${routes.length} routes: ${routes.join(", ")})`
921
+ });
922
+ }
923
+ }
924
+ return { sharedChrome, perRoute: trimmedPerRoute };
925
+ };
926
+ var DEFAULT_TIMEOUT_MS2 = 3e4;
927
+ var joinUrl = (base, route) => `${base.replace(/\/$/, "")}/${route.replace(/^\//, "")}`;
928
+ var routeSnapshotFile = (route) => `${route.replace(/^\//, "").replace(/\/+$/, "").replace(/\//g, "_") || "index"}.json`;
929
+ var compareRoutes = async (options) => {
930
+ if (options.oldBaseUrl === void 0 === (options.oldSnapshotDir === void 0)) {
931
+ throw new Error("compareRoutes: provide exactly one of oldBaseUrl or oldSnapshotDir");
932
+ }
933
+ const viewport = options.viewport ?? DESKTOP_VIEWPORT;
934
+ const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS2;
935
+ const browser = options.browser ?? await playwright.chromium.launch();
936
+ const ownsBrowser = options.browser === void 0;
937
+ const { oldBaseUrl, oldSnapshotDir } = options;
938
+ const snapshotUrl = async (url, route) => {
939
+ const context = await browser.newContext({
940
+ viewport: { width: viewport.width, height: viewport.height }
941
+ });
942
+ const page = await context.newPage();
943
+ try {
944
+ await page.goto(url, { waitUntil: "networkidle", timeout: timeoutMs });
945
+ await prepareForScreenshot(page);
946
+ return await captureSnapshot(page, route, options.config);
947
+ } finally {
948
+ await context.close();
949
+ }
950
+ };
951
+ const oldSnapshotFor = (route) => {
952
+ if (oldBaseUrl !== void 0) return snapshotUrl(joinUrl(oldBaseUrl, route), route);
953
+ if (oldSnapshotDir !== void 0) {
954
+ return loadSnapshot(path.join(oldSnapshotDir, routeSnapshotFile(route)));
955
+ }
956
+ throw new Error("compareRoutes: provide exactly one of oldBaseUrl or oldSnapshotDir");
957
+ };
958
+ try {
959
+ const perRoute = [];
960
+ for (const route of options.routes) {
961
+ const oldSnapshot = await oldSnapshotFor(route);
962
+ const newSnapshot = await snapshotUrl(joinUrl(options.newBaseUrl, route), route);
963
+ perRoute.push({ route, findings: diffSnapshots(oldSnapshot, newSnapshot, options.config) });
964
+ }
965
+ const { sharedChrome, perRoute: trimmed } = dedupeSharedChrome(perRoute);
966
+ const routes = trimmed.map(({ route, findings }) => ({
967
+ route,
968
+ findings,
969
+ pass: comparePass(findings)
970
+ }));
971
+ const pass = comparePass(sharedChrome) && routes.every((route) => route.pass);
972
+ return { routes, sharedChrome, pass };
973
+ } finally {
974
+ if (ownsBrowser) await browser.close();
975
+ }
976
+ };
977
+
592
978
  exports.ALL_AXES = ALL_AXES;
979
+ exports.CompareConfig = CompareConfig;
980
+ exports.CompareSnapshot = CompareSnapshot;
593
981
  exports.DEFAULT_VIEWPORTS = DEFAULT_VIEWPORTS;
594
982
  exports.DESKTOP_VIEWPORT = DESKTOP_VIEWPORT;
983
+ exports.ExpectedAbsentRule = ExpectedAbsentRule;
595
984
  exports.MOBILE_VIEWPORT = MOBILE_VIEWPORT;
985
+ exports.REGION_STATES = REGION_STATES;
986
+ exports.Region = Region;
596
987
  exports.SEVERITIES = SEVERITIES;
597
988
  exports.accessibilityAxis = accessibilityAxis;
598
989
  exports.aggregatePass = aggregatePass;
599
990
  exports.briefConformanceAxis = briefConformanceAxis;
600
991
  exports.capture = capture;
601
992
  exports.capturePage = capturePage;
993
+ exports.captureSnapshot = captureSnapshot;
994
+ exports.compareAxis = compareAxis;
995
+ exports.comparePass = comparePass;
996
+ exports.compareRoutes = compareRoutes;
997
+ exports.dedupeSharedChrome = dedupeSharedChrome;
998
+ exports.diffSnapshots = diffSnapshots;
999
+ exports.extractRegions = extractRegions;
1000
+ exports.loadSnapshot = loadSnapshot;
602
1001
  exports.prepareForScreenshot = prepareForScreenshot;
1002
+ exports.routeSnapshotFile = routeSnapshotFile;
603
1003
  exports.runAxes = runAxes;
604
1004
  exports.runtimeHealthAxis = runtimeHealthAxis;
1005
+ exports.saveSnapshot = saveSnapshot;
605
1006
  exports.serveStatic = serveStatic;
606
1007
  exports.validate = validate;
607
1008
  exports.visualBrandAxis = visualBrandAxis;