@cappa/core 0.13.1 → 0.14.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
@@ -262,8 +262,8 @@ const compare = (image1, image2, diff, width, height, options) => {
262
262
  };
263
263
  /**
264
264
  * Compare two PNG images and return the difference
265
- * @param image1 - First image (file path or Buffer)
266
- * @param image2 - Second image (file path or Buffer)
265
+ * @param image1 - Baseline (expected) image (file path or Buffer)
266
+ * @param image2 - Current (actual) image (file path or Buffer)
267
267
  * @param withDiff - Whether to create a diff image
268
268
  * @param options - Comparison options
269
269
  * @returns Comparison result
@@ -311,8 +311,8 @@ function annotateDiffImage(image, options) {
311
311
  }
312
312
  /**
313
313
  * Quick comparison function that returns only pass/fail
314
- * @param image1 - First image (file path or Buffer)
315
- * @param image2 - Second image (file path or Buffer)
314
+ * @param image1 - Baseline (expected) image (file path or Buffer)
315
+ * @param image2 - Current (actual) image (file path or Buffer)
316
316
  * @param options - Comparison options
317
317
  * @returns Whether the images match
318
318
  */
@@ -349,8 +349,9 @@ function toNativeOptions(config) {
349
349
  }
350
350
  /**
351
351
  * Compare two PNG images and return the difference
352
- * @param image1 - First image (file path or Buffer)
353
- * @param image2 - Second image (file path or Buffer)
352
+ * @param image1 - Baseline (expected) image (file path or Buffer)
353
+ * @param image2 - Current (actual) image (file path or Buffer). Order matters
354
+ * for `interpret`: content only in `image2` is reported as an addition.
354
355
  * @param withDiff - Whether to create a diff image
355
356
  * @param options - Comparison options
356
357
  * @returns Comparison result
@@ -435,8 +436,9 @@ async function compareImages(image1, image2, withDiff = false, options = {}) {
435
436
  }
436
437
  /**
437
438
  * Quick comparison function that returns only pass/fail
438
- * @param image1 - First image (file path or Buffer)
439
- * @param image2 - Second image (file path or Buffer)
439
+ * @param image1 - Baseline (expected) image (file path or Buffer)
440
+ * @param image2 - Current (actual) image (file path or Buffer). Order matters
441
+ * for `interpret`: content only in `image2` is reported as an addition.
440
442
  * @param options - Comparison options
441
443
  * @returns Whether the images match
442
444
  */
@@ -480,6 +482,48 @@ function defineConfig(options) {
480
482
  return options;
481
483
  }
482
484
  //#endregion
485
+ //#region src/consoleEvents.ts
486
+ const CONSOLE_LOG_LEVELS = [
487
+ "error",
488
+ "warn",
489
+ "info",
490
+ "log",
491
+ "debug"
492
+ ];
493
+ /** Maps a Playwright `ConsoleMessage.type()` onto a {@link ConsoleLogLevel}. */
494
+ const toConsoleLogLevel = (type) => {
495
+ switch (type) {
496
+ case "error":
497
+ case "assert": return "error";
498
+ case "warning": return "warn";
499
+ case "info": return "info";
500
+ case "debug":
501
+ case "trace": return "debug";
502
+ default: return "log";
503
+ }
504
+ };
505
+ const severity = (level) => CONSOLE_LOG_LEVELS.indexOf(level);
506
+ /**
507
+ * Wires a page's `console` and `pageerror` events to `logger` according to
508
+ * `setting`. Uncaught page errors are always forwarded — at error level when
509
+ * a severity threshold is set, at debug level otherwise.
510
+ */
511
+ const attachConsoleLogging = (page, setting, logger) => {
512
+ const threshold = setting ?? true;
513
+ if (threshold === true) page.on("console", (message) => {
514
+ logger.debug("console", message.text());
515
+ });
516
+ else if (threshold !== false) page.on("console", (message) => {
517
+ const level = toConsoleLogLevel(message.type());
518
+ if (severity(level) > severity(threshold)) return;
519
+ logger[level](`[browser ${level}]`, message.text());
520
+ });
521
+ page.on("pageerror", (error) => {
522
+ if (typeof threshold === "string") logger.error("[browser pageerror]", error);
523
+ else logger.debug("pageerror", error);
524
+ });
525
+ };
526
+ //#endregion
483
527
  //#region src/mapWithConcurrency.ts
484
528
  /**
485
529
  * Run async work over `items` with at most `concurrency` tasks in flight.
@@ -655,7 +699,7 @@ var ScreenshotFileSystem = class {
655
699
  }
656
700
  if (screenshot.category === "passed") {
657
701
  if (!screenshot.expectedPath) throw new Error("Expected path is required for passed screenshots");
658
- if (!await imagesMatch(node_path.default.resolve(outputDir, screenshot.actualPath), node_path.default.resolve(outputDir, screenshot.expectedPath), diff.type === "gmsd" ? { threshold: diff.threshold } : diff)) await this.approveFromActualPath(node_path.default.resolve(outputDir, screenshot.actualPath));
702
+ if (!await imagesMatch(node_path.default.resolve(outputDir, screenshot.expectedPath), node_path.default.resolve(outputDir, screenshot.actualPath), diff.type === "gmsd" ? { threshold: diff.threshold } : diff)) await this.approveFromActualPath(node_path.default.resolve(outputDir, screenshot.actualPath));
659
703
  }
660
704
  }
661
705
  /**
@@ -1752,7 +1796,7 @@ var ScreenshotTool = class {
1752
1796
  delay: this.getIncBackoffDelay(i, options.delay || 0)
1753
1797
  });
1754
1798
  if (!screenshotBuffer) throw new Error("Screenshot buffer is undefined");
1755
- const comparisonResult = diffConfig.type === "gmsd" ? await compareImagesGMSD(screenshotBuffer, referenceImage, true, diffConfig) : await compareImages(screenshotBuffer, referenceImage, true, diffConfig);
1799
+ const comparisonResult = diffConfig.type === "gmsd" ? await compareImagesGMSD(referenceImage, screenshotBuffer, true, diffConfig) : await compareImages(referenceImage, screenshotBuffer, true, diffConfig);
1756
1800
  if (i === this.retries - 1 && !comparisonResult.passed) {
1757
1801
  this.logger.error(`Failed to match screenshot after ${this.retries} retries`);
1758
1802
  return {
@@ -3138,6 +3182,7 @@ var FsScreenshotStore = class {
3138
3182
  }
3139
3183
  };
3140
3184
  //#endregion
3185
+ exports.CONSOLE_LOG_LEVELS = CONSOLE_LOG_LEVELS;
3141
3186
  exports.CaptureRunner = CaptureRunner;
3142
3187
  exports.DEFAULT_DEBOUNCE_MS = DEFAULT_DEBOUNCE_MS;
3143
3188
  exports.DEFAULT_IGNORED_DIRS = DEFAULT_IGNORED_DIRS;
@@ -3155,6 +3200,7 @@ exports.UnknownTargetsError = UnknownTargetsError;
3155
3200
  exports.WarmBrowser = WarmBrowser;
3156
3201
  exports.WatchInProgressError = WatchInProgressError;
3157
3202
  exports.WatchSession = WatchSession;
3203
+ exports.attachConsoleLogging = attachConsoleLogging;
3158
3204
  exports.collectScreenshots = collectScreenshots;
3159
3205
  exports.compareImages = compareImages;
3160
3206
  exports.compareImagesGMSD = compareImagesGMSD;
@@ -3175,6 +3221,7 @@ exports.readDiffMeta = readDiffMeta;
3175
3221
  exports.recordCaptureOrigins = recordCaptureOrigins;
3176
3222
  exports.sanitizeScreenshotFilename = sanitizeScreenshotFilename;
3177
3223
  exports.selectTasks = selectTasks;
3224
+ exports.toConsoleLogLevel = toConsoleLogLevel;
3178
3225
  exports.toDiffMetaPath = toDiffMetaPath;
3179
3226
  exports.toManifestKey = toManifestKey;
3180
3227
  exports.toSerializedError = toSerializedError;