mirror of
https://github.com/morluto/rea.git
synced 2026-12-06 14:59:05 +00:00
test(png): share screenshot comparison fixture setup (#16836)
This commit is contained in:
1 parent
ab066cac08
commit
9c898c8fdf
1 file changed
+82
-127
@@ -1,6 +1,7 @@
|
||||
import { crc32, deflateSync } from "node:zlib";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { z } from "zod";
|
||||
|
||||
import { readPngHeaderDimensions } from "./pngHeader.js";
|
||||
import { pngChunk } from "../../tests/fixtures/pngChunk.js";
|
||||
@@ -16,6 +17,10 @@ import {
|
||||
webScreenshotDiffSchema,
|
||||
} from "./webScreenshot.js";
|
||||
|
||||
const compareFixture = (
|
||||
input: z.input<typeof compareWebScreenshotsInputSchema>,
|
||||
) => comparePngScreenshots(compareWebScreenshotsInputSchema.parse(input));
|
||||
|
||||
describe("PNG header admission", () => {
|
||||
it.each([16, 20])(
|
||||
"shares dimension admission with header sniffing at %s",
|
||||
@@ -27,12 +32,10 @@ describe("PNG header admission", () => {
|
||||
expect(readPngHeaderDimensions(bytes)).toBeUndefined();
|
||||
const image = createWebScreenshotArtifact(bytes);
|
||||
expect(() =>
|
||||
comparePngScreenshots(
|
||||
compareWebScreenshotsInputSchema.parse({
|
||||
before: image,
|
||||
after: image,
|
||||
}),
|
||||
),
|
||||
compareFixture({
|
||||
before: image,
|
||||
after: image,
|
||||
}),
|
||||
).toThrow("Invalid PNG header");
|
||||
}
|
||||
},
|
||||
@@ -47,12 +50,10 @@ describe("PNG header admission", () => {
|
||||
expect(readPngHeaderDimensions(bytes)).toBeUndefined();
|
||||
const image = createWebScreenshotArtifact(bytes);
|
||||
expect(() =>
|
||||
comparePngScreenshots(
|
||||
compareWebScreenshotsInputSchema.parse({
|
||||
before: image,
|
||||
after: image,
|
||||
}),
|
||||
),
|
||||
compareFixture({
|
||||
before: image,
|
||||
after: image,
|
||||
}),
|
||||
).toThrow("Invalid PNG header");
|
||||
},
|
||||
);
|
||||
@@ -62,9 +63,7 @@ describe("PNG visual diff", () => {
|
||||
it("reports exact changed-pixel and channel metrics", () => {
|
||||
const before = artifact(1, 1, [0, 0, 0, 255]);
|
||||
const after = artifact(1, 1, [10, 0, 0, 255]);
|
||||
const result = comparePngScreenshots(
|
||||
compareWebScreenshotsInputSchema.parse({ before, after }),
|
||||
);
|
||||
const result = compareFixture({ before, after });
|
||||
|
||||
expect(result).toMatchObject({
|
||||
status: "different",
|
||||
@@ -80,21 +79,17 @@ describe("PNG visual diff", () => {
|
||||
const one = artifact(1, 1, [0, 0, 0, 255]);
|
||||
const near = artifact(1, 1, [2, 0, 0, 255]);
|
||||
expect(
|
||||
comparePngScreenshots(
|
||||
compareWebScreenshotsInputSchema.parse({
|
||||
before: one,
|
||||
after: near,
|
||||
channel_threshold: 2,
|
||||
}),
|
||||
).status,
|
||||
compareFixture({
|
||||
before: one,
|
||||
after: near,
|
||||
channel_threshold: 2,
|
||||
}).status,
|
||||
).toBe("identical");
|
||||
expect(
|
||||
comparePngScreenshots(
|
||||
compareWebScreenshotsInputSchema.parse({
|
||||
before: one,
|
||||
after: artifact(2, 1, [0, 0, 0, 255, 0, 0, 0, 255]),
|
||||
}),
|
||||
),
|
||||
compareFixture({
|
||||
before: one,
|
||||
after: artifact(2, 1, [0, 0, 0, 255, 0, 0, 0, 255]),
|
||||
}),
|
||||
).toMatchObject({ status: "dimension_mismatch", compared_pixels: 0 });
|
||||
});
|
||||
|
||||
@@ -110,11 +105,7 @@ describe("PNG visual diff", () => {
|
||||
}),
|
||||
);
|
||||
const rgba = artifact(2, 1, [10, 20, 30, 0, 10, 20, 31, 255]);
|
||||
expect(
|
||||
comparePngScreenshots(
|
||||
compareWebScreenshotsInputSchema.parse({ before: rgb, after: rgba }),
|
||||
),
|
||||
).toMatchObject({
|
||||
expect(compareFixture({ before: rgb, after: rgba })).toMatchObject({
|
||||
status: "identical",
|
||||
changed_pixels: 0,
|
||||
maximum_channel_delta: 0,
|
||||
@@ -127,12 +118,10 @@ describe("PNG visual diff", () => {
|
||||
key.writeUInt16BE(0xfe14, 2);
|
||||
key.writeUInt16BE(0xff1e, 4);
|
||||
const rgb = transparencyArtifact(2, key, [10, 20, 30]);
|
||||
const result = comparePngScreenshots(
|
||||
compareWebScreenshotsInputSchema.parse({
|
||||
before: rgb,
|
||||
after: artifact(1, 1, [10, 20, 30, 0]),
|
||||
}),
|
||||
);
|
||||
const result = compareFixture({
|
||||
before: rgb,
|
||||
after: artifact(1, 1, [10, 20, 30, 0]),
|
||||
});
|
||||
expect(result).toMatchObject({ status: "identical", changed_pixels: 0 });
|
||||
expect(result.limitations).toContainEqual(
|
||||
expect.stringMatching(
|
||||
@@ -143,39 +132,31 @@ describe("PNG visual diff", () => {
|
||||
|
||||
it.each([0, 5, 7])("rejects an RGB tRNS chunk of length %i", (length) => {
|
||||
const image = transparencyArtifact(2, Buffer.alloc(length), [10, 20, 30]);
|
||||
expect(() =>
|
||||
comparePngScreenshots(
|
||||
compareWebScreenshotsInputSchema.parse({ before: image, after: image }),
|
||||
),
|
||||
).toThrow("Unsupported PNG transparency");
|
||||
expect(() => compareFixture({ before: image, after: image })).toThrow(
|
||||
"Unsupported PNG transparency",
|
||||
);
|
||||
});
|
||||
|
||||
it("rejects tRNS on an RGBA image instead of dropping its transparency data", () => {
|
||||
const image = transparencyArtifact(6, Buffer.alloc(6), [10, 20, 30, 255]);
|
||||
expect(() =>
|
||||
comparePngScreenshots(
|
||||
compareWebScreenshotsInputSchema.parse({ before: image, after: image }),
|
||||
),
|
||||
).toThrow("Unsupported PNG transparency");
|
||||
expect(() => compareFixture({ before: image, after: image })).toThrow(
|
||||
"Unsupported PNG transparency",
|
||||
);
|
||||
});
|
||||
|
||||
it("rejects malformed PNG dimensions after validating image data", () => {
|
||||
const image = artifact(2, 1, [0, 0, 0, 255]);
|
||||
expect(() =>
|
||||
comparePngScreenshots(
|
||||
compareWebScreenshotsInputSchema.parse({
|
||||
before: image,
|
||||
after: image,
|
||||
}),
|
||||
),
|
||||
compareFixture({
|
||||
before: image,
|
||||
after: image,
|
||||
}),
|
||||
).toThrow("Unexpected PNG size");
|
||||
});
|
||||
|
||||
it("rejects metrics that cannot represent their comparison status", () => {
|
||||
const image = artifact(1, 1, [0, 0, 0, 255]);
|
||||
const result = comparePngScreenshots(
|
||||
compareWebScreenshotsInputSchema.parse({ before: image, after: image }),
|
||||
);
|
||||
const result = compareFixture({ before: image, after: image });
|
||||
|
||||
expect(
|
||||
webScreenshotDiffSchema.safeParse({
|
||||
@@ -218,11 +199,9 @@ describe("isOneColourPng", () => {
|
||||
describe("PNG comparison memory budget", () => {
|
||||
it("admits compacted scanlines while still validating the inflated length", () => {
|
||||
const image = createWebScreenshotArtifact(headerOnlyPng(4480, 4000));
|
||||
expect(() =>
|
||||
comparePngScreenshots(
|
||||
compareWebScreenshotsInputSchema.parse({ before: image, after: image }),
|
||||
),
|
||||
).toThrow("Unexpected PNG size");
|
||||
expect(() => compareFixture({ before: image, after: image })).toThrow(
|
||||
"Unexpected PNG size",
|
||||
);
|
||||
});
|
||||
|
||||
it("classifies legal huge dimensions as a memory constraint before inflate", () => {
|
||||
@@ -286,12 +265,10 @@ describe("PNG chunk integrity", () => {
|
||||
);
|
||||
const image = createWebScreenshotArtifact(bytes);
|
||||
expect(() =>
|
||||
comparePngScreenshots(
|
||||
compareWebScreenshotsInputSchema.parse({
|
||||
before: image,
|
||||
after: image,
|
||||
}),
|
||||
),
|
||||
compareFixture({
|
||||
before: image,
|
||||
after: image,
|
||||
}),
|
||||
).toThrow("Invalid PNG chunk checksum");
|
||||
},
|
||||
);
|
||||
@@ -306,11 +283,9 @@ describe("PNG chunk integrity", () => {
|
||||
pngChunk("IEND", Buffer.alloc(0)),
|
||||
]),
|
||||
);
|
||||
expect(() =>
|
||||
comparePngScreenshots(
|
||||
compareWebScreenshotsInputSchema.parse({ before: image, after: image }),
|
||||
),
|
||||
).toThrow("Invalid PNG image data");
|
||||
expect(() => compareFixture({ before: image, after: image })).toThrow(
|
||||
"Invalid PNG image data",
|
||||
);
|
||||
});
|
||||
|
||||
it("rejects high-bit bytes in a CRC-valid PNG chunk name", () => {
|
||||
@@ -322,12 +297,10 @@ describe("PNG chunk integrity", () => {
|
||||
Buffer.concat([bytes.subarray(0, 8), headerChunk, bytes.subarray(33)]),
|
||||
);
|
||||
expect(() =>
|
||||
comparePngScreenshots(
|
||||
compareWebScreenshotsInputSchema.parse({
|
||||
before: malformed,
|
||||
after: malformed,
|
||||
}),
|
||||
),
|
||||
compareFixture({
|
||||
before: malformed,
|
||||
after: malformed,
|
||||
}),
|
||||
).toThrow("Invalid PNG chunk type");
|
||||
});
|
||||
|
||||
@@ -346,12 +319,10 @@ describe("PNG chunk integrity", () => {
|
||||
for (const malformed of [duplicate, unknown]) {
|
||||
const image = createWebScreenshotArtifact(malformed);
|
||||
expect(() =>
|
||||
comparePngScreenshots(
|
||||
compareWebScreenshotsInputSchema.parse({
|
||||
before: image,
|
||||
after: image,
|
||||
}),
|
||||
),
|
||||
compareFixture({
|
||||
before: image,
|
||||
after: image,
|
||||
}),
|
||||
).toThrow(/Invalid PNG header order|Unsupported critical PNG chunk/u);
|
||||
}
|
||||
const ancillary = createWebScreenshotArtifact(
|
||||
@@ -362,12 +333,10 @@ describe("PNG chunk integrity", () => {
|
||||
]),
|
||||
);
|
||||
expect(
|
||||
comparePngScreenshots(
|
||||
compareWebScreenshotsInputSchema.parse({
|
||||
before: ancillary,
|
||||
after: createWebScreenshotArtifact(bytes),
|
||||
}),
|
||||
).status,
|
||||
compareFixture({
|
||||
before: ancillary,
|
||||
after: createWebScreenshotArtifact(bytes),
|
||||
}).status,
|
||||
).toBe("identical");
|
||||
});
|
||||
|
||||
@@ -377,12 +346,10 @@ describe("PNG chunk integrity", () => {
|
||||
Buffer.concat([bytes, Buffer.from([0])]),
|
||||
);
|
||||
expect(() =>
|
||||
comparePngScreenshots(
|
||||
compareWebScreenshotsInputSchema.parse({
|
||||
before: trailing,
|
||||
after: trailing,
|
||||
}),
|
||||
),
|
||||
compareFixture({
|
||||
before: trailing,
|
||||
after: trailing,
|
||||
}),
|
||||
).toThrow("Invalid PNG end chunk");
|
||||
|
||||
const grayscaleHeader = Buffer.from(bytes.subarray(16, 29));
|
||||
@@ -399,12 +366,10 @@ describe("PNG chunk integrity", () => {
|
||||
});
|
||||
const grayscale = createWebScreenshotArtifact(grayscaleBytes);
|
||||
expect(() =>
|
||||
comparePngScreenshots(
|
||||
compareWebScreenshotsInputSchema.parse({
|
||||
before: grayscale,
|
||||
after: grayscale,
|
||||
}),
|
||||
),
|
||||
compareFixture({
|
||||
before: grayscale,
|
||||
after: grayscale,
|
||||
}),
|
||||
).toThrow(AnalysisTargetFailureError);
|
||||
});
|
||||
});
|
||||
@@ -447,12 +412,10 @@ describe("PNG chunk ordering", () => {
|
||||
for (const chunks of cases) {
|
||||
const malformed = image(...chunks);
|
||||
expect(() =>
|
||||
comparePngScreenshots(
|
||||
compareWebScreenshotsInputSchema.parse({
|
||||
before: malformed,
|
||||
after: original,
|
||||
}),
|
||||
),
|
||||
compareFixture({
|
||||
before: malformed,
|
||||
after: original,
|
||||
}),
|
||||
).toThrow(
|
||||
/Invalid PNG palette|Invalid PNG transparency order|Nonconsecutive PNG image data|Invalid PNG end chunk/u,
|
||||
);
|
||||
@@ -468,12 +431,10 @@ describe("PNG chunk ordering", () => {
|
||||
iend,
|
||||
);
|
||||
expect(() =>
|
||||
comparePngScreenshots(
|
||||
compareWebScreenshotsInputSchema.parse({
|
||||
before: emptyTransparency,
|
||||
after: original,
|
||||
}),
|
||||
),
|
||||
compareFixture({
|
||||
before: emptyTransparency,
|
||||
after: original,
|
||||
}),
|
||||
).toThrow("Unsupported PNG transparency");
|
||||
for (const valid of [
|
||||
image(ihdr, palette, idat, iend),
|
||||
@@ -486,12 +447,10 @@ describe("PNG chunk ordering", () => {
|
||||
),
|
||||
]) {
|
||||
expect(
|
||||
comparePngScreenshots(
|
||||
compareWebScreenshotsInputSchema.parse({
|
||||
before: valid,
|
||||
after: original,
|
||||
}),
|
||||
).status,
|
||||
compareFixture({
|
||||
before: valid,
|
||||
after: original,
|
||||
}).status,
|
||||
).toBe("identical");
|
||||
}
|
||||
});
|
||||
@@ -622,11 +581,7 @@ describe("PNG scanline reconstruction", () => {
|
||||
[decoded, reference],
|
||||
[reference, decoded],
|
||||
] as const) {
|
||||
expect(
|
||||
comparePngScreenshots(
|
||||
compareWebScreenshotsInputSchema.parse({ before, after }),
|
||||
),
|
||||
).toMatchObject({
|
||||
expect(compareFixture({ before, after })).toMatchObject({
|
||||
status: "identical",
|
||||
compared_pixels: 12,
|
||||
changed_pixels: 0,
|
||||
|
||||
Reference in new issue
Block a user