Skip to content

Commit

Permalink
Tests: assertSimilar support for Promise and callback
Browse files Browse the repository at this point in the history
  • Loading branch information
lovell committed Jul 20, 2024
1 parent 2672de2 commit f128ebd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 45 deletions.
77 changes: 36 additions & 41 deletions test/fixtures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,26 @@ const getPath = function (filename) {

// Generates a 64-bit-as-binary-string image fingerprint
// Based on the dHash gradient method - see http://www.hackerfactor.com/blog/index.php?/archives/529-Kind-of-Like-That.html
const fingerprint = function (image, callback) {
sharp(image)
async function fingerprint (image) {
return sharp(image)
.flatten('gray')
.greyscale()
.normalise()
.resize(9, 8, { fit: sharp.fit.fill })
.raw()
.toBuffer(function (err, data) {
if (err) {
callback(err);
} else {
let fingerprint = '';
for (let col = 0; col < 8; col++) {
for (let row = 0; row < 8; row++) {
const left = data[(row * 8) + col];
const right = data[(row * 8) + col + 1];
fingerprint = fingerprint + (left < right ? '1' : '0');
}
.toBuffer()
.then(function (data) {
let fingerprint = '';
for (let col = 0; col < 8; col++) {
for (let row = 0; row < 8; row++) {
const left = data[(row * 8) + col];
const right = data[(row * 8) + col + 1];
fingerprint = fingerprint + (left < right ? '1' : '0');
}
callback(null, fingerprint);
}
return fingerprint;
});
};
}

module.exports = {

Expand Down Expand Up @@ -151,46 +148,44 @@ module.exports = {
// Verify similarity of expected vs actual images via fingerprint
// Specify distance threshold using `options={threshold: 42}`, default
// `threshold` is 5;
assertSimilar: function (expectedImage, actualImage, options, callback) {
assertSimilar: async function (expectedImage, actualImage, options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}

if (typeof options === 'undefined' && options === null) {
if (typeof options === 'undefined' || options === null) {
options = {};
}

if (options.threshold === null || typeof options.threshold === 'undefined') {
options.threshold = 5; // ~7% threshold
}

if (typeof options.threshold !== 'number') {
throw new TypeError('`options.threshold` must be a number');
}

if (typeof callback !== 'function') {
throw new TypeError('`callback` must be a function');
}

fingerprint(expectedImage, function (err, expectedFingerprint) {
if (err) return callback(err);
fingerprint(actualImage, function (err, actualFingerprint) {
if (err) return callback(err);
let distance = 0;
for (let i = 0; i < 64; i++) {
if (expectedFingerprint[i] !== actualFingerprint[i]) {
distance++;
}
}

if (distance > options.threshold) {
return callback(new Error('Expected maximum similarity distance: ' + options.threshold + '. Actual: ' + distance + '.'));
try {
const [expectedFingerprint, actualFingerprint] = await Promise.all([
fingerprint(expectedImage),
fingerprint(actualImage)
]);
let distance = 0;
for (let i = 0; i < 64; i++) {
if (expectedFingerprint[i] !== actualFingerprint[i]) {
distance++;
}

callback();
});
});
}
if (distance > options.threshold) {
throw new Error(`Expected maximum similarity distance: ${options.threshold}. Actual: ${distance}.`);
}
} catch (err) {
if (callback) {
return callback(err);
}
throw err;
}
if (callback) {
callback();
}
},

assertMaxColourDistance: function (actualImagePath, expectedImagePath, acceptedDistance) {
Expand Down
5 changes: 1 addition & 4 deletions test/unit/blur.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,7 @@ describe('Blur', function () {
.toBuffer();

assert.notDeepEqual(approximate, integer);

await new Promise(resolve => {
fixtures.assertSimilar(fixtures.expected('blur-10.jpg'), approximate, resolve);
});
await fixtures.assertSimilar(fixtures.expected('blur-10.jpg'), approximate);
});

it('options.sigma is required if options object is passed', function () {
Expand Down

0 comments on commit f128ebd

Please sign in to comment.