test: share repeated boundary preambles to restore the jscpd gate (#2691)

Squash merge of https://github.com/morluto/rea/pull/2691.

Co-authored-by: NIU-123370 <191000457+NIU-123370@users.noreply.github.com>
This commit is contained in:
The one -Niu
2026-10-13 23:47:55 +08:00
committed by GitHub
co-authored by NIU-123370
parent 782cc3b060
commit 8b1549a264
6 changed files with 167 additions and 281 deletions
+31 -95
View File
@@ -35,6 +35,28 @@ const validPng = Buffer.from(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jv4sAAAAASUVORK5CYII=",
"base64",
);
/** Screenshot payload as the native helper reports one PNG buffer. */
const screenshotOf = (bytes: Buffer) => ({
mime_type: "image/png",
base64: bytes.toString("base64"),
sha256: createHash("sha256").update(bytes).digest("hex"),
width: bytes.readUInt32BE(16),
height: bytes.readUInt32BE(20),
});
/** Observe the fixture snapshot carrying one screenshot payload. */
const observeWithScreenshot = (screenshot: Record<string, unknown>) =>
observeNativeUi(
target,
"observe_native_ui",
{ ...scope, screenshot: true },
{
environment: {},
invoke: async () => ({ ok: true, result: { ...snapshot, screenshot } }),
},
);
it.each([
{ offset: 8, value: 12 },
{ offset: 16, value: 0x8000_0000 },
@@ -44,49 +66,16 @@ it.each([
async ({ offset, value }) => {
const bytes = Buffer.from(validPng);
bytes.writeUInt32BE(value, offset);
const screenshot = {
mime_type: "image/png",
base64: bytes.toString("base64"),
sha256: createHash("sha256").update(bytes).digest("hex"),
width: bytes.readUInt32BE(16),
height: bytes.readUInt32BE(20),
};
const result = await observeNativeUi(
target,
"observe_native_ui",
{ ...scope, screenshot: true },
{
environment: {},
invoke: async () => ({
ok: true,
result: { ...snapshot, screenshot },
}),
},
);
const result = await observeWithScreenshot(screenshotOf(bytes));
expect(result.ok).toBe(false);
},
);
describe("native UI screenshot validation", () => {
it("rejects malformed or mismatched screenshot bytes from the helper", async () => {
const screenshot = {
mime_type: "image/png",
base64: validPng.toString("base64"),
const result = await observeWithScreenshot({
...screenshotOf(validPng),
sha256: "0".repeat(64),
width: 1,
height: 1,
};
const result = await observeNativeUi(
target,
"observe_native_ui",
{ ...scope, screenshot: true },
{
environment: {},
invoke: async () => ({
ok: true,
result: { ...snapshot, screenshot },
}),
},
);
});
expect(result.ok).toBe(false);
});
@@ -97,26 +86,10 @@ describe("native UI screenshot validation", () => {
])(
"rejects invalid screenshot field variants from the helper",
async (change) => {
const screenshot = {
mime_type: "image/png",
base64: validPng.toString("base64"),
sha256: createHash("sha256").update(validPng).digest("hex"),
width: 1,
height: 1,
const result = await observeWithScreenshot({
...screenshotOf(validPng),
...change,
};
const result = await observeNativeUi(
target,
"observe_native_ui",
{ ...scope, screenshot: true },
{
environment: {},
invoke: async () => ({
ok: true,
result: { ...snapshot, screenshot },
}),
},
);
});
expect(result.ok).toBe(false);
},
);
@@ -126,49 +99,12 @@ describe("native UI screenshot validation", () => {
const type = bytes.subarray(12, 16);
type[0] = (type[0] ?? 0) | 0x80;
expect(type.toString("ascii")).toBe("IHDR");
const screenshot = {
mime_type: "image/png",
base64: bytes.toString("base64"),
sha256: createHash("sha256").update(bytes).digest("hex"),
width: 1,
height: 1,
};
const result = await observeNativeUi(
target,
"observe_native_ui",
{ ...scope, screenshot: true },
{
environment: {},
invoke: async () => ({
ok: true,
result: { ...snapshot, screenshot },
}),
},
);
const result = await observeWithScreenshot(screenshotOf(bytes));
expect(result.ok).toBe(false);
});
it("accepts a self-consistent PNG screenshot from the helper", async () => {
const bytes = validPng;
const screenshot = {
mime_type: "image/png",
base64: bytes.toString("base64"),
sha256: createHash("sha256").update(bytes).digest("hex"),
width: 1,
height: 1,
};
const result = await observeNativeUi(
target,
"observe_native_ui",
{ ...scope, screenshot: true },
{
environment: {},
invoke: async () => ({
ok: true,
result: { ...snapshot, screenshot },
}),
},
);
const result = await observeWithScreenshot(screenshotOf(validPng));
expect(result.ok).toBe(true);
});