PlotDirector/PlotLine.Tests/Browser/relationship-main-protected-comparison.cjs

79 lines
2.6 KiB
JavaScript

// Compare controlled before/after captures. Only ephemeral SVG marker UUIDs are normalised.
const fs = require("fs"),
path = require("path"),
crypto = require("crypto"),
sharp = require("sharp"),
assert = require("assert/strict");
(async () => {
const before = process.env.PD_BEFORE || "/tmp/pd-main-relationships-before",
after = process.env.PD_AFTER || "/tmp/pd-main-relationships-after",
hashes = JSON.parse(
fs.readFileSync(path.join(before, "protected-source-hashes.json")),
),
results = [];
for (const [file, expected] of Object.entries(hashes))
assert.equal(
crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex"),
expected,
"protected source changed " + file,
);
for (const name of [
"context-family",
"context-story",
"character-relationships",
]) {
const normalise = (s) =>
s.replace(
/((?:pd-context-arrow-\d+-|cw-arrow-))[\da-f-]{36}/g,
"$1INSTANCE",
);
assert.equal(
normalise(fs.readFileSync(path.join(after, name + ".svg"), "utf8")),
normalise(fs.readFileSync(path.join(before, name + ".svg"), "utf8")),
name + " geometry/markup changed",
);
const a = await sharp(path.join(before, name + ".png"))
.raw()
.toBuffer({ resolveWithObject: true }),
b = await sharp(path.join(after, name + ".png"))
.raw()
.toBuffer({ resolveWithObject: true });
assert.deepEqual(a.info, b.info);
let changed = 0,
maxDifference = 0;
for (let i = 0; i < a.data.length; i += a.info.channels) {
let different = false;
for (let j = 0; j < a.info.channels; j++) {
const d = Math.abs(a.data[i + j] - b.data[i + j]);
maxDifference = Math.max(d, maxDifference);
different ||= d > 0;
}
if (different) changed++;
}
assert(maxDifference <= 1, name + " screenshot intensity changed");
assert(changed <= 12, name + " screenshot changed: " + changed + " pixels");
results.push({
name,
sourceHashesUnchanged: true,
svgGeometryAndMarkupIdentical: true,
changedPixels: changed,
totalPixels: a.info.width * a.info.height,
maxDifference,
note: changed
? "Only fringe rasterisation pixels; identical vectors, data, layout and styles."
: "Exact PNG match.",
});
}
fs.writeFileSync(
path.join(after, "protected-comparison.json"),
JSON.stringify(results, null, 2),
);
console.log(
"PASS protected source hashes, identical SVG geometry/markup and screenshot comparisons",
results,
);
})().catch((e) => {
console.error(e.message);
process.exitCode = 1;
});