2018-10-24 00:39:02 +08:00
|
|
|
'use strict';
|
|
|
|
|
2012-07-09 22:49:31 +08:00
|
|
|
// Cribbed from the ever prolific Konstantin Kaefer
|
|
|
|
// https://github.com/mapbox/tilelive-mapnik/blob/master/test/support/assert.js
|
|
|
|
|
2015-09-17 08:06:46 +08:00
|
|
|
var fs = require('fs');
|
|
|
|
var path = require('path');
|
|
|
|
var util = require('util');
|
|
|
|
|
2020-04-04 23:34:22 +08:00
|
|
|
const mapnik = require('@carto/mapnik');
|
2015-09-23 19:04:08 +08:00
|
|
|
|
2015-09-17 08:06:46 +08:00
|
|
|
var request = require('request');
|
2012-07-09 22:49:31 +08:00
|
|
|
|
|
|
|
var assert = module.exports = exports = require('assert');
|
|
|
|
|
2014-08-04 07:30:24 +08:00
|
|
|
/**
|
2016-02-22 23:05:12 +08:00
|
|
|
* Takes an image data as an input and an image path and compare them using mapnik.Image.compare mechanism, in case the
|
2014-08-04 07:30:24 +08:00
|
|
|
* similarity is not within the tolerance limit it will callback with an error.
|
|
|
|
*
|
|
|
|
* @param buffer The image data to compare from
|
|
|
|
* @param {string} referenceImageRelativeFilePath The relative file to compare against
|
|
|
|
* @param {number} tolerance tolerated mean color distance, as a per mil (‰)
|
|
|
|
* @param {function} callback Will call to home with null in case there is no error, otherwise with the error itself
|
|
|
|
* @see FUZZY in http://www.imagemagick.org/script/command-line-options.php#metric
|
|
|
|
*/
|
2019-10-22 01:07:24 +08:00
|
|
|
assert.imageBufferIsSimilarToFile = function (buffer, referenceImageRelativeFilePath, tolerance, callback) {
|
|
|
|
callback = callback || function (err) { assert.ifError(err); };
|
2015-07-05 02:41:22 +08:00
|
|
|
|
2016-02-22 23:00:30 +08:00
|
|
|
var referenceImageFilePath = path.resolve(referenceImageRelativeFilePath);
|
2016-02-22 23:41:55 +08:00
|
|
|
var referenceImageBuffer = fs.readFileSync(referenceImageFilePath, { encoding: null });
|
2015-07-05 02:41:22 +08:00
|
|
|
|
2016-02-22 23:41:55 +08:00
|
|
|
assert.imageBuffersAreSimilar(buffer, referenceImageBuffer, tolerance, callback);
|
2015-07-05 02:41:22 +08:00
|
|
|
};
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
assert.imageBuffersAreSimilar = function (bufferA, bufferB, tolerance, callback) {
|
2019-10-22 02:05:51 +08:00
|
|
|
var testImage = mapnik.Image.fromBytes(Buffer.isBuffer(bufferA) ? bufferA : Buffer.from(bufferA, 'binary'));
|
|
|
|
var referenceImage = mapnik.Image.fromBytes(Buffer.isBuffer(bufferB) ? bufferB : Buffer.from(bufferB, 'binary'));
|
2016-02-22 22:38:29 +08:00
|
|
|
|
2016-02-22 23:07:26 +08:00
|
|
|
imagesAreSimilar(testImage, referenceImage, tolerance, callback);
|
2016-02-22 23:00:30 +08:00
|
|
|
};
|
2012-07-09 22:49:31 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
assert.imageIsSimilarToFile = function (testImage, referenceImageRelativeFilePath, tolerance, callback, format = 'png') {
|
|
|
|
callback = callback || function (err) { assert.ifError(err); };
|
2016-02-22 23:07:55 +08:00
|
|
|
|
|
|
|
var referenceImageFilePath = path.resolve(referenceImageRelativeFilePath);
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
var referenceImage = mapnik.Image.fromBytes(fs.readFileSync(referenceImageFilePath, { encoding: null }));
|
2016-02-22 23:07:55 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
imagesAreSimilar(testImage, referenceImage, tolerance, function (err) {
|
2016-02-22 23:07:55 +08:00
|
|
|
if (err) {
|
2017-12-12 20:20:22 +08:00
|
|
|
var testImageFilePath = randomImagePath(format);
|
|
|
|
testImage.save(testImageFilePath, format);
|
2016-02-22 23:07:55 +08:00
|
|
|
}
|
|
|
|
callback(err);
|
2017-12-12 20:20:22 +08:00
|
|
|
}, format);
|
2016-02-22 23:07:55 +08:00
|
|
|
};
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function imagesAreSimilar (testImage, referenceImage, tolerance, callback, format = 'png') {
|
2015-09-23 19:04:08 +08:00
|
|
|
if (testImage.width() !== referenceImage.width() || testImage.height() !== referenceImage.height()) {
|
|
|
|
return callback(new Error('Images are not the same size'));
|
|
|
|
}
|
|
|
|
|
2017-12-12 20:20:22 +08:00
|
|
|
var options = {};
|
|
|
|
if (format === 'jpeg') {
|
|
|
|
options.alpha = false;
|
|
|
|
}
|
|
|
|
var pixelsDifference = referenceImage.compare(testImage, options);
|
2015-09-23 19:04:08 +08:00
|
|
|
var similarity = pixelsDifference / (referenceImage.width() * referenceImage.height());
|
|
|
|
var tolerancePerMil = (tolerance / 1000);
|
|
|
|
|
|
|
|
if (similarity > tolerancePerMil) {
|
|
|
|
var err = new Error(
|
|
|
|
util.format('Images are not similar (got %d similarity, expected %d)', similarity, tolerancePerMil)
|
|
|
|
);
|
|
|
|
err.similarity = similarity;
|
|
|
|
callback(err, similarity);
|
|
|
|
} else {
|
|
|
|
callback(null, similarity);
|
|
|
|
}
|
2016-02-22 23:07:26 +08:00
|
|
|
}
|
2015-09-23 19:04:08 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function randomImagePath (format = 'png') {
|
2017-12-12 20:20:22 +08:00
|
|
|
return path.resolve('test/results/' + format + '/image-test-' + Date.now() + '.' + format);
|
2015-09-23 19:04:08 +08:00
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
assert.response = function (server, req, res, callback) {
|
2015-09-17 08:06:46 +08:00
|
|
|
if (!callback) {
|
|
|
|
callback = res;
|
|
|
|
res = {};
|
2012-07-09 22:49:31 +08:00
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
var port = 0; // let the OS to choose a free port
|
|
|
|
var host = '127.0.0.1';
|
2012-07-09 22:49:31 +08:00
|
|
|
|
2018-04-17 16:44:44 +08:00
|
|
|
var listener = server.listen(port, host);
|
|
|
|
listener.on('error', callback);
|
|
|
|
listener.on('listening', function onServerListening () {
|
|
|
|
const { address: host, port } = listener.address();
|
|
|
|
const address = `${host}:${port}`;
|
2015-09-17 08:06:46 +08:00
|
|
|
|
|
|
|
var requestParams = {
|
2018-04-17 16:44:44 +08:00
|
|
|
url: 'http://' + address + req.url,
|
2015-09-17 08:06:46 +08:00
|
|
|
method: req.method || 'GET',
|
|
|
|
headers: req.headers || {},
|
|
|
|
timeout: req.timeout || 0,
|
|
|
|
encoding: req.encoding || 'utf8'
|
2012-07-09 22:49:31 +08:00
|
|
|
};
|
|
|
|
|
2015-09-17 08:06:46 +08:00
|
|
|
if (req.body || req.data) {
|
|
|
|
requestParams.body = req.body || req.data;
|
2015-07-05 02:41:22 +08:00
|
|
|
}
|
2012-07-09 22:49:31 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
request(requestParams, function assert$response$requestHandler (error, response, body) {
|
2018-04-17 16:44:44 +08:00
|
|
|
listener.close(function () {
|
|
|
|
if (error) {
|
|
|
|
return callback(null, error);
|
|
|
|
}
|
|
|
|
|
2015-09-17 08:06:46 +08:00
|
|
|
response.body = response.body || body;
|
2017-10-06 21:15:16 +08:00
|
|
|
var err = validateResponse(response, res);
|
|
|
|
return callback(response, err);
|
2012-07-09 22:49:31 +08:00
|
|
|
});
|
|
|
|
});
|
2018-04-17 16:44:44 +08:00
|
|
|
});
|
2015-07-05 02:41:22 +08:00
|
|
|
};
|
2017-10-06 21:15:16 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function validateResponseBody (response, expected) {
|
2017-10-06 21:15:16 +08:00
|
|
|
if (expected.body) {
|
2019-10-22 01:07:24 +08:00
|
|
|
var eql = expected.body instanceof RegExp
|
|
|
|
? expected.body.test(response.body)
|
|
|
|
: expected.body === response.body;
|
2017-10-06 21:15:16 +08:00
|
|
|
if (!eql) {
|
|
|
|
return new Error(colorize(
|
|
|
|
'[red]{Invalid response body.}\n' +
|
|
|
|
' Expected: [green]{' + expected.body + '}\n' +
|
|
|
|
' Got: [red]{' + response.body + '}')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function validateResponseStatus (response, expected) {
|
2017-10-06 21:15:16 +08:00
|
|
|
var status = expected.status || expected.statusCode;
|
2019-01-30 22:15:07 +08:00
|
|
|
const message = colorize('[red]{Invalid response status code.}\n' +
|
|
|
|
' Expected: [green]{' + status + '}\n' +
|
|
|
|
' Got: [red]{' + response.statusCode + '}\n' +
|
|
|
|
' Body: ' + response.body);
|
|
|
|
|
2017-10-06 21:15:16 +08:00
|
|
|
// Assert response status
|
2019-01-30 22:15:07 +08:00
|
|
|
if (typeof status === 'number' && response.statusCode !== status) {
|
|
|
|
return new Error(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(status) && !status.includes(response.statusCode)) {
|
|
|
|
return new Error(message);
|
2017-10-06 21:15:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function validateResponseHeaders (response, expected) {
|
2017-10-06 21:15:16 +08:00
|
|
|
// Assert response headers
|
|
|
|
if (expected.headers) {
|
|
|
|
var keys = Object.keys(expected.headers);
|
|
|
|
for (var i = 0, len = keys.length; i < len; ++i) {
|
2019-10-22 01:07:24 +08:00
|
|
|
var name = keys[i];
|
|
|
|
var actual = response.headers[name.toLowerCase()];
|
|
|
|
var expectedHeader = expected.headers[name];
|
|
|
|
var headerEql = expectedHeader instanceof RegExp ? expectedHeader.test(actual) : expectedHeader === actual;
|
2017-10-06 21:15:16 +08:00
|
|
|
if (!headerEql) {
|
|
|
|
return new Error(colorize(
|
|
|
|
'Invalid response header [bold]{' + name + '}.\n' +
|
|
|
|
' Expected: [green]{' + expectedHeader + '}\n' +
|
|
|
|
' Got: [red]{' + actual + '}')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function validateResponse (response, expected) {
|
2017-10-06 21:15:16 +08:00
|
|
|
// Assert response body
|
|
|
|
return validateResponseBody(response, expected) ||
|
|
|
|
validateResponseStatus(response, expected) ||
|
|
|
|
validateResponseHeaders(response, expected);
|
|
|
|
}
|
2015-07-05 02:41:22 +08:00
|
|
|
|
|
|
|
// @param tolerance number of tolerated grid cell differences
|
2019-11-06 23:44:17 +08:00
|
|
|
assert.utfgridEqualsFile = function (buffer, fileB, tolerance, callback) {
|
2015-07-05 02:41:22 +08:00
|
|
|
fs.writeFileSync('/tmp/grid.json', buffer, 'binary'); // <-- to debug/update
|
2019-11-06 23:44:17 +08:00
|
|
|
var expectedJson = JSON.parse(fs.readFileSync(fileB, 'utf8'));
|
2015-07-05 02:41:22 +08:00
|
|
|
|
|
|
|
var err = null;
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
var Celldiff = function (x, y, ev, ov) {
|
2015-07-05 02:41:22 +08:00
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
|
|
|
this.ev = ev;
|
|
|
|
this.ov = ov;
|
|
|
|
};
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
Celldiff.prototype.toString = function () {
|
2015-07-05 02:41:22 +08:00
|
|
|
return '(' + this.x + ',' + this.y + ')["' + this.ev + '" != "' + this.ov + '"]';
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2019-11-06 23:44:17 +08:00
|
|
|
var obtainedJson = Object.prototype.toString() === buffer.toString() ? buffer : JSON.parse(buffer);
|
2015-07-05 02:41:22 +08:00
|
|
|
|
|
|
|
// compare grid
|
2019-11-06 23:44:17 +08:00
|
|
|
var obtainedGrid = obtainedJson.grid;
|
|
|
|
var expectedGrid = expectedJson.grid;
|
|
|
|
var nrows = obtainedGrid.length;
|
|
|
|
if (nrows !== expectedGrid.length) {
|
2019-10-22 01:07:24 +08:00
|
|
|
throw new Error('Obtained grid rows (' + nrows +
|
2019-11-06 23:44:17 +08:00
|
|
|
') != expected grid rows (' + expectedGrid.length + ')');
|
2015-07-05 02:41:22 +08:00
|
|
|
}
|
|
|
|
var celldiff = [];
|
2019-10-22 01:07:24 +08:00
|
|
|
for (var i = 0; i < nrows; ++i) {
|
2019-11-06 23:44:17 +08:00
|
|
|
var ocols = obtainedGrid[i];
|
|
|
|
var ecols = expectedGrid[i];
|
2015-07-05 02:41:22 +08:00
|
|
|
var ncols = ocols.length;
|
2019-10-22 01:07:24 +08:00
|
|
|
if (ncols !== ecols.length) {
|
|
|
|
throw new Error('Obtained grid cols (' + ncols +
|
|
|
|
') != expected grid cols (' + ecols.length +
|
|
|
|
') on row ' + i);
|
2015-07-05 02:41:22 +08:00
|
|
|
}
|
2019-10-22 01:07:24 +08:00
|
|
|
for (var j = 0; j < ncols; ++j) {
|
2015-07-05 02:41:22 +08:00
|
|
|
var ocell = ocols[j];
|
|
|
|
var ecell = ecols[j];
|
2019-10-22 01:07:24 +08:00
|
|
|
if (ocell !== ecell) {
|
2015-07-05 02:41:22 +08:00
|
|
|
celldiff.push(new Celldiff(i, j, ecell, ocell));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
if (celldiff.length > tolerance) {
|
|
|
|
throw new Error(celldiff.length + ' cell differences: ' + celldiff);
|
2015-07-05 02:41:22 +08:00
|
|
|
}
|
|
|
|
|
2019-11-06 23:44:17 +08:00
|
|
|
assert.deepStrictEqual(obtainedJson.keys, expectedJson.keys);
|
2015-07-05 02:41:22 +08:00
|
|
|
} catch (e) { err = e; }
|
|
|
|
|
|
|
|
callback(err);
|
2012-07-09 22:49:31 +08:00
|
|
|
};
|
|
|
|
|
2015-02-05 01:57:12 +08:00
|
|
|
/**
|
|
|
|
* Colorize the given string using ansi-escape sequences.
|
|
|
|
* Disabled when --boring is set.
|
|
|
|
*
|
|
|
|
* @param {String} str
|
|
|
|
* @return {String}
|
|
|
|
*/
|
2019-10-22 01:07:24 +08:00
|
|
|
function colorize (str) {
|
2015-02-05 01:57:12 +08:00
|
|
|
var colors = { bold: 1, red: 31, green: 32, yellow: 33 };
|
2019-10-22 01:07:24 +08:00
|
|
|
return str.replace(/\[(\w+)\]\{([^]*?)\}/g, function (_, color, str) {
|
2015-02-05 01:57:12 +08:00
|
|
|
return '\x1B[' + colors[color] + 'm' + str + '\x1B[0m';
|
|
|
|
});
|
2015-07-05 02:41:22 +08:00
|
|
|
}
|