@cappa/core 0.2.5 → 0.3.1
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/.turbo/turbo-build.log +23 -25
- package/CHANGELOG.md +12 -0
- package/dist/index.d.mts +63 -13
- package/dist/index.d.ts +63 -13
- package/dist/index.js +112 -100
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +108 -98
- package/dist/index.mjs.map +1 -1
- package/package.json +12 -8
- package/src/config.ts +3 -3
- package/src/index.ts +7 -0
- package/src/types.ts +35 -0
- package/.turbo/turbo-test.log +0 -53
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,25 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
>
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
[34mℹ[39m tsdown
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
[34mℹ[39m
|
|
13
|
-
[34mℹ[39m
|
|
14
|
-
[34mℹ[39m
|
|
15
|
-
[34mℹ[39m
|
|
16
|
-
[34mℹ[39m [33m[CJS][39m
|
|
17
|
-
[34mℹ[39m [
|
|
18
|
-
[34mℹ[39m [
|
|
19
|
-
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[1mindex.
|
|
20
|
-
[34mℹ[39m [34m[ESM][39m
|
|
21
|
-
[34mℹ[39m [
|
|
22
|
-
[34mℹ[39m [
|
|
23
|
-
[
|
|
24
|
-
[34mℹ[39m [33m[CJS][39m 1 files, total: 10.53 kB
|
|
25
|
-
[32m✔[39m Build complete in [32m1124ms[39m
|
|
1
|
+
|
|
2
|
+
> @cappa/core@0.3.1 build /home/runner/work/cappa/cappa/packages/core
|
|
3
|
+
> tsdown
|
|
4
|
+
|
|
5
|
+
[34mℹ[39m tsdown [2mv0.15.12[22m powered by rolldown [2mv1.0.0-beta.45[22m
|
|
6
|
+
[34mℹ[39m Using tsdown config: [4m/home/runner/work/cappa/cappa/packages/core/tsdown.config.ts[24m
|
|
7
|
+
(node:2361) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///home/runner/work/cappa/cappa/packages/core/tsdown.config.ts is not specified and it doesn't parse as CommonJS.
|
|
8
|
+
Reparsing as ES module because module syntax was detected. This incurs a performance overhead.
|
|
9
|
+
To eliminate this warning, add "type": "module" to /home/runner/work/cappa/cappa/packages/core/package.json.
|
|
10
|
+
(Use `node --trace-warnings ...` to show where the warning was created)
|
|
11
|
+
[34mℹ[39m entry: [34msrc/index.ts[39m
|
|
12
|
+
[34mℹ[39m tsconfig: [34mtsconfig.json[39m
|
|
13
|
+
[34mℹ[39m Build start
|
|
14
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[1mindex.js[22m [2m25.68 kB[22m [2m│ gzip: 6.22 kB[22m
|
|
15
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22mindex.js.map [2m46.35 kB[22m [2m│ gzip: 10.92 kB[22m
|
|
16
|
+
[34mℹ[39m [33m[CJS][39m 2 files, total: 72.03 kB
|
|
17
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[1mindex.mjs[22m [2m23.80 kB[22m [2m│ gzip: 5.71 kB[22m
|
|
18
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22mindex.mjs.map [2m46.24 kB[22m [2m│ gzip: 10.88 kB[22m
|
|
19
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[32m[1mindex.d.mts[22m[39m [2m12.96 kB[22m [2m│ gzip: 3.67 kB[22m
|
|
20
|
+
[34mℹ[39m [34m[ESM][39m 3 files, total: 82.99 kB
|
|
21
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[32m[1mindex.d.ts[22m[39m [2m12.96 kB[22m [2m│ gzip: 3.66 kB[22m
|
|
22
|
+
[34mℹ[39m [33m[CJS][39m 1 files, total: 12.96 kB
|
|
23
|
+
[32m✔[39m Build complete in [32m2794ms[39m
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @cappa/core
|
|
2
2
|
|
|
3
|
+
## 0.3.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 772d0f6: Ensure approving screenshots only updates baselines when actual and expected images differ.
|
|
8
|
+
|
|
9
|
+
## 0.3.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- e0f7f08: Add an `onFail` configuration callback for failed screenshots and forward environment details to configuration functions.
|
|
14
|
+
|
|
3
15
|
## 0.2.5
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
package/dist/index.d.mts
CHANGED
|
@@ -1,17 +1,6 @@
|
|
|
1
1
|
import { Logger } from "@cappa/logger";
|
|
2
2
|
import { Browser, BrowserContext, Locator, Page } from "playwright-core";
|
|
3
3
|
|
|
4
|
-
//#region src/compare.d.ts
|
|
5
|
-
interface CompareResult {
|
|
6
|
-
numDiffPixels: number;
|
|
7
|
-
totalPixels: number;
|
|
8
|
-
percentDifference: number;
|
|
9
|
-
diffBuffer?: Buffer;
|
|
10
|
-
passed: boolean;
|
|
11
|
-
error?: string;
|
|
12
|
-
differentSizes: boolean;
|
|
13
|
-
}
|
|
14
|
-
//#endregion
|
|
15
4
|
//#region src/filesystem.d.ts
|
|
16
5
|
/**
|
|
17
6
|
* Class for interacting with the local file system to store the screenshots.
|
|
@@ -259,6 +248,20 @@ type Plugin<Config = any> = (config?: Config) => PluginDef;
|
|
|
259
248
|
//#endregion
|
|
260
249
|
//#region src/types.d.ts
|
|
261
250
|
type PossiblePromise<T> = Promise<T> | T;
|
|
251
|
+
interface ConfigEnv {
|
|
252
|
+
/**
|
|
253
|
+
* The CLI command being executed, for example `capture` or `review`.
|
|
254
|
+
*/
|
|
255
|
+
command?: string;
|
|
256
|
+
/**
|
|
257
|
+
* The mode the CLI is running in, defaults to `process.env.NODE_ENV`.
|
|
258
|
+
*/
|
|
259
|
+
mode: string;
|
|
260
|
+
/**
|
|
261
|
+
* The environment variables available when the configuration is loaded.
|
|
262
|
+
*/
|
|
263
|
+
env: NodeJS.ProcessEnv;
|
|
264
|
+
}
|
|
262
265
|
/**
|
|
263
266
|
* Configuration for the comparison of images
|
|
264
267
|
*/
|
|
@@ -334,6 +337,11 @@ type UserConfig = {
|
|
|
334
337
|
* Each plugin may have additional configurable options (defined within the plugin itself).
|
|
335
338
|
*/
|
|
336
339
|
plugins?: Array<Plugin | PluginDef>;
|
|
340
|
+
/**
|
|
341
|
+
* Callback executed when screenshots fail comparison.
|
|
342
|
+
* Receives the failing screenshots so they can be uploaded or processed.
|
|
343
|
+
*/
|
|
344
|
+
onFail?: (screenshots: FailedScreenshot[]) => PossiblePromise<void>;
|
|
337
345
|
};
|
|
338
346
|
type Viewport = {
|
|
339
347
|
width: number;
|
|
@@ -372,12 +380,54 @@ interface Screenshot {
|
|
|
372
380
|
diffPath?: string;
|
|
373
381
|
approved?: boolean;
|
|
374
382
|
}
|
|
383
|
+
type FailedScreenshot = Screenshot & {
|
|
384
|
+
/**
|
|
385
|
+
* Absolute path to the actual screenshot on disk.
|
|
386
|
+
*/
|
|
387
|
+
absoluteActualPath?: string;
|
|
388
|
+
/**
|
|
389
|
+
* Absolute path to the expected screenshot on disk.
|
|
390
|
+
*/
|
|
391
|
+
absoluteExpectedPath?: string;
|
|
392
|
+
/**
|
|
393
|
+
* Absolute path to the diff screenshot on disk.
|
|
394
|
+
*/
|
|
395
|
+
absoluteDiffPath?: string;
|
|
396
|
+
};
|
|
397
|
+
//#endregion
|
|
398
|
+
//#region src/compare.d.ts
|
|
399
|
+
interface CompareResult {
|
|
400
|
+
numDiffPixels: number;
|
|
401
|
+
totalPixels: number;
|
|
402
|
+
percentDifference: number;
|
|
403
|
+
diffBuffer?: Buffer;
|
|
404
|
+
passed: boolean;
|
|
405
|
+
error?: string;
|
|
406
|
+
differentSizes: boolean;
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Compare two PNG images and return the difference
|
|
410
|
+
* @param image1 - First image (file path or Buffer)
|
|
411
|
+
* @param image2 - Second image (file path or Buffer)
|
|
412
|
+
* @param withDiff - Whether to create a diff image
|
|
413
|
+
* @param options - Comparison options
|
|
414
|
+
* @returns Comparison result
|
|
415
|
+
*/
|
|
416
|
+
declare function compareImages(image1: string | Buffer, image2: string | Buffer, withDiff?: boolean, options?: DiffConfig): Promise<CompareResult>;
|
|
417
|
+
/**
|
|
418
|
+
* Quick comparison function that returns only pass/fail
|
|
419
|
+
* @param image1 - First image (file path or Buffer)
|
|
420
|
+
* @param image2 - Second image (file path or Buffer)
|
|
421
|
+
* @param options - Comparison options
|
|
422
|
+
* @returns Whether the images match
|
|
423
|
+
*/
|
|
424
|
+
declare function imagesMatch(image1: string | Buffer, image2: string | Buffer, options?: DiffConfig): Promise<boolean>;
|
|
375
425
|
//#endregion
|
|
376
426
|
//#region src/config.d.ts
|
|
377
427
|
/**
|
|
378
428
|
* Type helper to make it easier to use cappa.config.ts, or a function that returns it. The function receives a ConfigEnv object.
|
|
379
429
|
*/
|
|
380
|
-
declare function defineConfig(options: PossiblePromise<UserConfig |
|
|
430
|
+
declare function defineConfig(options: PossiblePromise<UserConfig> | ((env: ConfigEnv) => PossiblePromise<UserConfig>)): typeof options;
|
|
381
431
|
//#endregion
|
|
382
|
-
export { type Plugin, type PluginDef, type PluginFunction, type Screenshot, type ScreenshotCaptureDetails, type ScreenshotCaptureExtras, type ScreenshotCaptureResult, ScreenshotFileSystem, type ScreenshotOptions, type ScreenshotSettings, ScreenshotTool, type ScreenshotVariant, type ScreenshotVariantCaptureDetails, type ScreenshotVariantWithUrl, type UserConfig, type Viewport, defineConfig };
|
|
432
|
+
export { type ConfigEnv, type FailedScreenshot, type Plugin, type PluginDef, type PluginFunction, type Screenshot, type ScreenshotCaptureDetails, type ScreenshotCaptureExtras, type ScreenshotCaptureResult, ScreenshotFileSystem, type ScreenshotOptions, type ScreenshotSettings, ScreenshotTool, type ScreenshotVariant, type ScreenshotVariantCaptureDetails, type ScreenshotVariantWithUrl, type UserConfig, type Viewport, compareImages, defineConfig, imagesMatch };
|
|
383
433
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.ts
CHANGED
|
@@ -1,17 +1,6 @@
|
|
|
1
1
|
import { Browser, BrowserContext, Locator, Page } from "playwright-core";
|
|
2
2
|
import { Logger } from "@cappa/logger";
|
|
3
3
|
|
|
4
|
-
//#region src/compare.d.ts
|
|
5
|
-
interface CompareResult {
|
|
6
|
-
numDiffPixels: number;
|
|
7
|
-
totalPixels: number;
|
|
8
|
-
percentDifference: number;
|
|
9
|
-
diffBuffer?: Buffer;
|
|
10
|
-
passed: boolean;
|
|
11
|
-
error?: string;
|
|
12
|
-
differentSizes: boolean;
|
|
13
|
-
}
|
|
14
|
-
//#endregion
|
|
15
4
|
//#region src/filesystem.d.ts
|
|
16
5
|
/**
|
|
17
6
|
* Class for interacting with the local file system to store the screenshots.
|
|
@@ -259,6 +248,20 @@ type Plugin<Config = any> = (config?: Config) => PluginDef;
|
|
|
259
248
|
//#endregion
|
|
260
249
|
//#region src/types.d.ts
|
|
261
250
|
type PossiblePromise<T> = Promise<T> | T;
|
|
251
|
+
interface ConfigEnv {
|
|
252
|
+
/**
|
|
253
|
+
* The CLI command being executed, for example `capture` or `review`.
|
|
254
|
+
*/
|
|
255
|
+
command?: string;
|
|
256
|
+
/**
|
|
257
|
+
* The mode the CLI is running in, defaults to `process.env.NODE_ENV`.
|
|
258
|
+
*/
|
|
259
|
+
mode: string;
|
|
260
|
+
/**
|
|
261
|
+
* The environment variables available when the configuration is loaded.
|
|
262
|
+
*/
|
|
263
|
+
env: NodeJS.ProcessEnv;
|
|
264
|
+
}
|
|
262
265
|
/**
|
|
263
266
|
* Configuration for the comparison of images
|
|
264
267
|
*/
|
|
@@ -334,6 +337,11 @@ type UserConfig = {
|
|
|
334
337
|
* Each plugin may have additional configurable options (defined within the plugin itself).
|
|
335
338
|
*/
|
|
336
339
|
plugins?: Array<Plugin | PluginDef>;
|
|
340
|
+
/**
|
|
341
|
+
* Callback executed when screenshots fail comparison.
|
|
342
|
+
* Receives the failing screenshots so they can be uploaded or processed.
|
|
343
|
+
*/
|
|
344
|
+
onFail?: (screenshots: FailedScreenshot[]) => PossiblePromise<void>;
|
|
337
345
|
};
|
|
338
346
|
type Viewport = {
|
|
339
347
|
width: number;
|
|
@@ -372,12 +380,54 @@ interface Screenshot {
|
|
|
372
380
|
diffPath?: string;
|
|
373
381
|
approved?: boolean;
|
|
374
382
|
}
|
|
383
|
+
type FailedScreenshot = Screenshot & {
|
|
384
|
+
/**
|
|
385
|
+
* Absolute path to the actual screenshot on disk.
|
|
386
|
+
*/
|
|
387
|
+
absoluteActualPath?: string;
|
|
388
|
+
/**
|
|
389
|
+
* Absolute path to the expected screenshot on disk.
|
|
390
|
+
*/
|
|
391
|
+
absoluteExpectedPath?: string;
|
|
392
|
+
/**
|
|
393
|
+
* Absolute path to the diff screenshot on disk.
|
|
394
|
+
*/
|
|
395
|
+
absoluteDiffPath?: string;
|
|
396
|
+
};
|
|
397
|
+
//#endregion
|
|
398
|
+
//#region src/compare.d.ts
|
|
399
|
+
interface CompareResult {
|
|
400
|
+
numDiffPixels: number;
|
|
401
|
+
totalPixels: number;
|
|
402
|
+
percentDifference: number;
|
|
403
|
+
diffBuffer?: Buffer;
|
|
404
|
+
passed: boolean;
|
|
405
|
+
error?: string;
|
|
406
|
+
differentSizes: boolean;
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Compare two PNG images and return the difference
|
|
410
|
+
* @param image1 - First image (file path or Buffer)
|
|
411
|
+
* @param image2 - Second image (file path or Buffer)
|
|
412
|
+
* @param withDiff - Whether to create a diff image
|
|
413
|
+
* @param options - Comparison options
|
|
414
|
+
* @returns Comparison result
|
|
415
|
+
*/
|
|
416
|
+
declare function compareImages(image1: string | Buffer, image2: string | Buffer, withDiff?: boolean, options?: DiffConfig): Promise<CompareResult>;
|
|
417
|
+
/**
|
|
418
|
+
* Quick comparison function that returns only pass/fail
|
|
419
|
+
* @param image1 - First image (file path or Buffer)
|
|
420
|
+
* @param image2 - Second image (file path or Buffer)
|
|
421
|
+
* @param options - Comparison options
|
|
422
|
+
* @returns Whether the images match
|
|
423
|
+
*/
|
|
424
|
+
declare function imagesMatch(image1: string | Buffer, image2: string | Buffer, options?: DiffConfig): Promise<boolean>;
|
|
375
425
|
//#endregion
|
|
376
426
|
//#region src/config.d.ts
|
|
377
427
|
/**
|
|
378
428
|
* Type helper to make it easier to use cappa.config.ts, or a function that returns it. The function receives a ConfigEnv object.
|
|
379
429
|
*/
|
|
380
|
-
declare function defineConfig(options: PossiblePromise<UserConfig |
|
|
430
|
+
declare function defineConfig(options: PossiblePromise<UserConfig> | ((env: ConfigEnv) => PossiblePromise<UserConfig>)): typeof options;
|
|
381
431
|
//#endregion
|
|
382
|
-
export { type Plugin, type PluginDef, type PluginFunction, type Screenshot, type ScreenshotCaptureDetails, type ScreenshotCaptureExtras, type ScreenshotCaptureResult, ScreenshotFileSystem, type ScreenshotOptions, type ScreenshotSettings, ScreenshotTool, type ScreenshotVariant, type ScreenshotVariantCaptureDetails, type ScreenshotVariantWithUrl, type UserConfig, type Viewport, defineConfig };
|
|
432
|
+
export { type ConfigEnv, type FailedScreenshot, type Plugin, type PluginDef, type PluginFunction, type Screenshot, type ScreenshotCaptureDetails, type ScreenshotCaptureExtras, type ScreenshotCaptureResult, ScreenshotFileSystem, type ScreenshotOptions, type ScreenshotSettings, ScreenshotTool, type ScreenshotVariant, type ScreenshotVariantCaptureDetails, type ScreenshotVariantWithUrl, type UserConfig, type Viewport, compareImages, defineConfig, imagesMatch };
|
|
383
433
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -23,17 +23,121 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
23
23
|
//#endregion
|
|
24
24
|
let node_fs = require("node:fs");
|
|
25
25
|
node_fs = __toESM(node_fs);
|
|
26
|
-
let node_path = require("node:path");
|
|
27
|
-
node_path = __toESM(node_path);
|
|
28
|
-
let __cappa_logger = require("@cappa/logger");
|
|
29
|
-
__cappa_logger = __toESM(__cappa_logger);
|
|
30
|
-
let playwright_core = require("playwright-core");
|
|
31
|
-
playwright_core = __toESM(playwright_core);
|
|
32
26
|
let __blazediff_core = require("@blazediff/core");
|
|
33
27
|
__blazediff_core = __toESM(__blazediff_core);
|
|
28
|
+
let __cappa_logger = require("@cappa/logger");
|
|
29
|
+
__cappa_logger = __toESM(__cappa_logger);
|
|
34
30
|
let pngjs = require("pngjs");
|
|
35
31
|
pngjs = __toESM(pngjs);
|
|
32
|
+
let node_path = require("node:path");
|
|
33
|
+
node_path = __toESM(node_path);
|
|
34
|
+
let playwright_core = require("playwright-core");
|
|
35
|
+
playwright_core = __toESM(playwright_core);
|
|
36
|
+
|
|
37
|
+
//#region src/compare.ts
|
|
38
|
+
const compare = (image1, image2, diff, width, height, options) => {
|
|
39
|
+
return (0, __blazediff_core.default)(image1, image2, diff, width, height, options);
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Compare two PNG images and return the difference
|
|
43
|
+
* @param image1 - First image (file path or Buffer)
|
|
44
|
+
* @param image2 - Second image (file path or Buffer)
|
|
45
|
+
* @param withDiff - Whether to create a diff image
|
|
46
|
+
* @param options - Comparison options
|
|
47
|
+
* @returns Comparison result
|
|
48
|
+
*/
|
|
49
|
+
async function compareImages(image1, image2, withDiff = false, options = {}) {
|
|
50
|
+
const png1 = await loadPNG(image1);
|
|
51
|
+
const png2 = await loadPNG(image2);
|
|
52
|
+
const { width, height } = png1;
|
|
53
|
+
if (width !== png2.width || height !== png2.height) return {
|
|
54
|
+
numDiffPixels: 0,
|
|
55
|
+
totalPixels: 0,
|
|
56
|
+
percentDifference: 0,
|
|
57
|
+
passed: false,
|
|
58
|
+
differentSizes: true
|
|
59
|
+
};
|
|
60
|
+
const diff = withDiff ? new pngjs.PNG({
|
|
61
|
+
width,
|
|
62
|
+
height
|
|
63
|
+
}) : void 0;
|
|
64
|
+
try {
|
|
65
|
+
const numDiffPixels = compare(png1.data, png2.data, diff?.data, width, height, options);
|
|
66
|
+
const totalPixels = width * height;
|
|
67
|
+
const percentDifference = numDiffPixels / totalPixels * 100;
|
|
68
|
+
return {
|
|
69
|
+
numDiffPixels,
|
|
70
|
+
totalPixels,
|
|
71
|
+
percentDifference,
|
|
72
|
+
diffBuffer: diff ? pngjs.PNG.sync.write(diff) : void 0,
|
|
73
|
+
passed: isPassed(percentDifference, numDiffPixels, options),
|
|
74
|
+
differentSizes: false
|
|
75
|
+
};
|
|
76
|
+
} catch (error) {
|
|
77
|
+
(0, __cappa_logger.getLogger)().error(error.message || "Unknown error");
|
|
78
|
+
return {
|
|
79
|
+
numDiffPixels: 0,
|
|
80
|
+
totalPixels: 0,
|
|
81
|
+
percentDifference: 0,
|
|
82
|
+
passed: false,
|
|
83
|
+
differentSizes: false,
|
|
84
|
+
error: error.message || "Unknown error"
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Load a PNG image from file path or Buffer
|
|
90
|
+
*/
|
|
91
|
+
async function loadPNG(source) {
|
|
92
|
+
if (Buffer.isBuffer(source)) return pngjs.PNG.sync.read(source);
|
|
93
|
+
else {
|
|
94
|
+
const buffer = node_fs.default.readFileSync(source);
|
|
95
|
+
return pngjs.PNG.sync.read(buffer);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Quick comparison function that returns only pass/fail
|
|
100
|
+
* @param image1 - First image (file path or Buffer)
|
|
101
|
+
* @param image2 - Second image (file path or Buffer)
|
|
102
|
+
* @param options - Comparison options
|
|
103
|
+
* @returns Whether the images match
|
|
104
|
+
*/
|
|
105
|
+
async function imagesMatch(image1, image2, options = {}) {
|
|
106
|
+
return (await compareImages(image1, image2, false, options)).passed;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Check if the comparison passed based on the options
|
|
110
|
+
* @param percentDifference - Percentage difference between the images
|
|
111
|
+
* @param numDiffPixels - Number of different pixels
|
|
112
|
+
* @param options - Comparison options
|
|
113
|
+
* @returns Whether the comparison passed
|
|
114
|
+
*/
|
|
115
|
+
const isPassed = (percentDifference, numDiffPixels, options) => {
|
|
116
|
+
if (options.maxDiffPercentage && options.maxDiffPixels) return percentDifference <= options.maxDiffPercentage && numDiffPixels <= options.maxDiffPixels;
|
|
117
|
+
if (options.maxDiffPercentage) return percentDifference <= options.maxDiffPercentage;
|
|
118
|
+
if (options.maxDiffPixels) return numDiffPixels <= options.maxDiffPixels;
|
|
119
|
+
return numDiffPixels === 0;
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* Create different size png image with all pixels red
|
|
123
|
+
* @returns
|
|
124
|
+
*/
|
|
125
|
+
function createDiffSizePngImage(width, height) {
|
|
126
|
+
const png = new pngjs.PNG({
|
|
127
|
+
width,
|
|
128
|
+
height
|
|
129
|
+
});
|
|
130
|
+
for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) {
|
|
131
|
+
const idx = width * y + x << 2;
|
|
132
|
+
png.data[idx] = 255;
|
|
133
|
+
png.data[idx + 1] = 0;
|
|
134
|
+
png.data[idx + 2] = 0;
|
|
135
|
+
png.data[idx + 3] = 255;
|
|
136
|
+
}
|
|
137
|
+
return pngjs.PNG.sync.write(png);
|
|
138
|
+
}
|
|
36
139
|
|
|
140
|
+
//#endregion
|
|
37
141
|
//#region src/config.ts
|
|
38
142
|
/**
|
|
39
143
|
* Type helper to make it easier to use cappa.config.ts, or a function that returns it. The function receives a ConfigEnv object.
|
|
@@ -203,100 +307,6 @@ var ScreenshotFileSystem = class {
|
|
|
203
307
|
}
|
|
204
308
|
};
|
|
205
309
|
|
|
206
|
-
//#endregion
|
|
207
|
-
//#region src/compare.ts
|
|
208
|
-
const compare = (image1, image2, diff, width, height, options) => {
|
|
209
|
-
return (0, __blazediff_core.default)(image1, image2, diff, width, height, options);
|
|
210
|
-
};
|
|
211
|
-
/**
|
|
212
|
-
* Compare two PNG images and return the difference
|
|
213
|
-
* @param image1 - First image (file path or Buffer)
|
|
214
|
-
* @param image2 - Second image (file path or Buffer)
|
|
215
|
-
* @param withDiff - Whether to create a diff image
|
|
216
|
-
* @param options - Comparison options
|
|
217
|
-
* @returns Comparison result
|
|
218
|
-
*/
|
|
219
|
-
async function compareImages(image1, image2, withDiff = false, options = {}) {
|
|
220
|
-
const png1 = await loadPNG(image1);
|
|
221
|
-
const png2 = await loadPNG(image2);
|
|
222
|
-
const { width, height } = png1;
|
|
223
|
-
if (width !== png2.width || height !== png2.height) return {
|
|
224
|
-
numDiffPixels: 0,
|
|
225
|
-
totalPixels: 0,
|
|
226
|
-
percentDifference: 0,
|
|
227
|
-
passed: false,
|
|
228
|
-
differentSizes: true
|
|
229
|
-
};
|
|
230
|
-
const diff = withDiff ? new pngjs.PNG({
|
|
231
|
-
width,
|
|
232
|
-
height
|
|
233
|
-
}) : void 0;
|
|
234
|
-
try {
|
|
235
|
-
const numDiffPixels = compare(png1.data, png2.data, diff?.data, width, height, options);
|
|
236
|
-
const totalPixels = width * height;
|
|
237
|
-
const percentDifference = numDiffPixels / totalPixels * 100;
|
|
238
|
-
return {
|
|
239
|
-
numDiffPixels,
|
|
240
|
-
totalPixels,
|
|
241
|
-
percentDifference,
|
|
242
|
-
diffBuffer: diff ? pngjs.PNG.sync.write(diff) : void 0,
|
|
243
|
-
passed: isPassed(percentDifference, numDiffPixels, options),
|
|
244
|
-
differentSizes: false
|
|
245
|
-
};
|
|
246
|
-
} catch (error) {
|
|
247
|
-
(0, __cappa_logger.getLogger)().error(error.message || "Unknown error");
|
|
248
|
-
return {
|
|
249
|
-
numDiffPixels: 0,
|
|
250
|
-
totalPixels: 0,
|
|
251
|
-
percentDifference: 0,
|
|
252
|
-
passed: false,
|
|
253
|
-
differentSizes: false,
|
|
254
|
-
error: error.message || "Unknown error"
|
|
255
|
-
};
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
/**
|
|
259
|
-
* Load a PNG image from file path or Buffer
|
|
260
|
-
*/
|
|
261
|
-
async function loadPNG(source) {
|
|
262
|
-
if (Buffer.isBuffer(source)) return pngjs.PNG.sync.read(source);
|
|
263
|
-
else {
|
|
264
|
-
const buffer = node_fs.default.readFileSync(source);
|
|
265
|
-
return pngjs.PNG.sync.read(buffer);
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
/**
|
|
269
|
-
* Check if the comparison passed based on the options
|
|
270
|
-
* @param percentDifference - Percentage difference between the images
|
|
271
|
-
* @param numDiffPixels - Number of different pixels
|
|
272
|
-
* @param options - Comparison options
|
|
273
|
-
* @returns Whether the comparison passed
|
|
274
|
-
*/
|
|
275
|
-
const isPassed = (percentDifference, numDiffPixels, options) => {
|
|
276
|
-
if (options.maxDiffPercentage && options.maxDiffPixels) return percentDifference <= options.maxDiffPercentage && numDiffPixels <= options.maxDiffPixels;
|
|
277
|
-
if (options.maxDiffPercentage) return percentDifference <= options.maxDiffPercentage;
|
|
278
|
-
if (options.maxDiffPixels) return numDiffPixels <= options.maxDiffPixels;
|
|
279
|
-
return numDiffPixels === 0;
|
|
280
|
-
};
|
|
281
|
-
/**
|
|
282
|
-
* Create different size png image with all pixels red
|
|
283
|
-
* @returns
|
|
284
|
-
*/
|
|
285
|
-
function createDiffSizePngImage(width, height) {
|
|
286
|
-
const png = new pngjs.PNG({
|
|
287
|
-
width,
|
|
288
|
-
height
|
|
289
|
-
});
|
|
290
|
-
for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) {
|
|
291
|
-
const idx = width * y + x << 2;
|
|
292
|
-
png.data[idx] = 255;
|
|
293
|
-
png.data[idx + 1] = 0;
|
|
294
|
-
png.data[idx + 2] = 0;
|
|
295
|
-
png.data[idx + 3] = 255;
|
|
296
|
-
}
|
|
297
|
-
return pngjs.PNG.sync.write(png);
|
|
298
|
-
}
|
|
299
|
-
|
|
300
310
|
//#endregion
|
|
301
311
|
//#region src/screenshot.ts
|
|
302
312
|
const defaultDiffConfig = {
|
|
@@ -730,5 +740,7 @@ var screenshot_default = ScreenshotTool;
|
|
|
730
740
|
//#endregion
|
|
731
741
|
exports.ScreenshotFileSystem = ScreenshotFileSystem;
|
|
732
742
|
exports.ScreenshotTool = screenshot_default;
|
|
743
|
+
exports.compareImages = compareImages;
|
|
733
744
|
exports.defineConfig = defineConfig;
|
|
745
|
+
exports.imagesMatch = imagesMatch;
|
|
734
746
|
//# sourceMappingURL=index.js.map
|