This commit is contained in:
seb
2026-10-08 04:51:28 +02:00
parent 7829addbdd
commit be02e531c5
3 changed files with 61 additions and 10 deletions

View File

@@ -159,13 +159,17 @@ function toRgba(parsed) {
return out;
}
function compareFiles(refFile, curFile) {
const ref = parsePng(fs.readFileSync(refFile));
const cur = parsePng(fs.readFileSync(curFile));
export function compareBuffers(refBuf, curBuf, { tolerance = TOLERANCE, channelTol = CHANNEL_TOL } = {}) {
const ref = parsePng(refBuf);
const cur = parsePng(curBuf);
if (ref.width !== cur.width || ref.height !== cur.height) {
return {
ok: false,
reason: `size mismatch ref=${ref.width}x${ref.height} cur=${cur.width}x${cur.height}`,
frac: 1,
maxDelta: 255,
diffPixels: ref.width * ref.height,
totalPixels: ref.width * ref.height,
};
}
const a = toRgba(ref);
@@ -182,7 +186,7 @@ function compareFiles(refFile, curFile) {
Math.abs(a[o + 2] - b[o + 2]),
);
if (d > maxDelta) maxDelta = d;
if (d > CHANNEL_TOL) {
if (d > channelTol) {
diff++;
diffImg[o] = 255;
diffImg[o + 1] = 0;
@@ -199,7 +203,7 @@ function compareFiles(refFile, curFile) {
}
const frac = diff / total;
return {
ok: frac <= TOLERANCE,
ok: frac <= tolerance,
diffPixels: diff,
totalPixels: total,
frac,
@@ -208,6 +212,12 @@ function compareFiles(refFile, curFile) {
};
}
export function compareFiles(refFile, curFile, opts = {}) {
return compareBuffers(fs.readFileSync(refFile), fs.readFileSync(curFile), opts);
}
export { parsePng, toRgba, encodePng };
function main() {
if (!fs.existsSync(REF_DIR)) {
console.error(`[compare] no reference dir ${REF_DIR} — run: npm run rig:reference`);
@@ -260,4 +270,7 @@ function main() {
console.log(`[compare] all ${files.length} screenshots match (tolerance ${(TOLERANCE * 100).toFixed(2)}%, channel±${CHANNEL_TOL})`);
}
main();
const isMain = process.argv[1] && fileURLToPath(import.meta.url) === path.resolve(process.argv[1]);
if (isMain) {
main();
}