-
Notifications
You must be signed in to change notification settings - Fork 325
Expand file tree
/
Copy pathtest.js
More file actions
82 lines (69 loc) · 3.34 KB
/
test.js
File metadata and controls
82 lines (69 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import assert from 'node:assert/strict';
import test from 'node:test';
import fs from 'node:fs';
import {PNG} from 'pngjs';
import match from '../index.js';
const options = {threshold: 0.05};
diffTest('1a', '1b', '1diff', options, 143);
diffTest('1a', '1b', '1diffdefaultthreshold', {threshold: undefined}, 106);
diffTest('1a', '1b', '1diffmask', {threshold: 0.05, includeAA: false, diffMask: true}, 143);
diffTest('1a', '1a', '1emptydiffmask', {threshold: 0, diffMask: true}, 0);
diffTest('2a', '2b', '2diff', {
threshold: 0.05,
alpha: 0.5,
aaColor: [0, 192, 0],
diffColor: [255, 0, 255]
}, 12437);
diffTest('3a', '3b', '3diff', options, 212);
diffTest('4a', '4b', '4diff', options, 36049);
diffTest('5a', '5b', '5diff', options, 6);
diffTest('6a', '6b', '6diff', options, 51);
diffTest('6a', '6a', '6empty', {threshold: 0}, 0);
diffTest('7a', '7b', '7diff', {diffColorAlt: [0, 255, 0]}, 2448);
diffTest('8a', '5b', '8diff', options, 32896);
test('checkerboard: false blends semi-transparent pixels against white', () => {
// These two pixels are visually identical composited on white but differ on a dark checkerboard
const img1 = new Uint8Array([0, 0, 0, 128]); // 50% transparent black
const img2 = new Uint8Array([127, 127, 127, 255]); // opaque gray
assert.equal(match(img1, img2, null, 1, 1, {checkerboard: false}), 0);
assert.equal(match(img1, img2, null, 1, 1), 1);
});
test('throws error if image sizes do not match', () => {
assert.throws(() => match(new Uint8Array(8), new Uint8Array(9), null, 2, 1), 'Image sizes do not match');
});
test('throws error if image sizes do not match width and height', () => {
assert.throws(() => match(new Uint8Array(9), new Uint8Array(9), null, 2, 1), 'Image data size does not match width/height');
});
test('throws error if provided wrong image data format', () => {
const err = 'Image data: Uint8Array, Uint8ClampedArray or Buffer expected';
const arr = new Uint8Array(4 * 20 * 20);
const bad = new Array(arr.length).fill(0);
assert.throws(() => match(bad, arr, null, 20, 20), err);
assert.throws(() => match(arr, bad, null, 20, 20), err);
assert.throws(() => match(arr, arr, bad, 20, 20), err);
});
function diffTest(imgPath1, imgPath2, diffPath, options, expectedMismatch) {
const name = `comparing ${imgPath1} to ${imgPath2}, ${JSON.stringify(options)}`;
test(name, () => {
const img1 = readImage(imgPath1);
const img2 = readImage(imgPath2);
const {width, height} = img1;
const diff = new PNG({width, height});
const mismatch = match(img1.data, img2.data, diff.data, width, height, options);
const mismatch2 = match(img1.data, img2.data, null, width, height, options);
if (process.env.UPDATE) {
writeImage(diffPath, diff);
} else {
const expectedDiff = readImage(diffPath);
assert.ok(diff.data.equals(expectedDiff.data), 'diff image');
}
assert.equal(mismatch, expectedMismatch, 'number of mismatched pixels');
assert.equal(mismatch, mismatch2, 'number of mismatched pixels without diff');
});
}
function readImage(name) {
return PNG.sync.read(fs.readFileSync(new URL(`fixtures/${name}.png`, import.meta.url)));
}
function writeImage(name, image) {
fs.writeFileSync(new URL(`fixtures/${name}.png`, import.meta.url), PNG.sync.write(image));
}