test(agents): dedupe tool image fixture setup

This commit is contained in:
Peter Steinberger
2026-02-19 08:20:52 +00:00
parent 69e6da0e28
commit 53a4e5151d

View File

@@ -3,6 +3,27 @@ import { describe, expect, it } from "vitest";
import { sanitizeContentBlocksImages, sanitizeImageBlocks } from "./tool-images.js";
describe("tool image sanitizing", () => {
const getImageBlock = (
blocks: Awaited<ReturnType<typeof sanitizeContentBlocksImages>>,
): (typeof blocks)[number] & { type: "image"; data: string; mimeType?: string } => {
const image = blocks.find((block) => block.type === "image");
if (!image || image.type !== "image") {
throw new Error("expected image block");
}
return image;
};
const createWidePng = async () => {
const width = 2600;
const height = 400;
const raw = Buffer.alloc(width * height * 3, 0x7f);
return sharp(raw, {
raw: { width, height, channels: 3 },
})
.png({ compressionLevel: 9 })
.toBuffer();
};
it("shrinks oversized images to <=5MB", async () => {
const width = 2800;
const height = 2800;
@@ -23,24 +44,14 @@ describe("tool image sanitizing", () => {
];
const out = await sanitizeContentBlocksImages(blocks, "test");
const image = out.find((b) => b.type === "image");
if (!image || image.type !== "image") {
throw new Error("expected image block");
}
const image = getImageBlock(out);
const size = Buffer.from(image.data, "base64").byteLength;
expect(size).toBeLessThanOrEqual(5 * 1024 * 1024);
expect(image.mimeType).toBe("image/jpeg");
}, 20_000);
it("sanitizes image arrays and reports drops", async () => {
const width = 2600;
const height = 400;
const raw = Buffer.alloc(width * height * 3, 0x7f);
const png = await sharp(raw, {
raw: { width, height, channels: 3 },
})
.png({ compressionLevel: 9 })
.toBuffer();
const png = await createWidePng();
const images = [
{ type: "image" as const, data: png.toString("base64"), mimeType: "image/png" },
@@ -54,14 +65,7 @@ describe("tool image sanitizing", () => {
}, 20_000);
it("shrinks images that exceed max dimension even if size is small", async () => {
const width = 2600;
const height = 400;
const raw = Buffer.alloc(width * height * 3, 0x7f);
const png = await sharp(raw, {
raw: { width, height, channels: 3 },
})
.png({ compressionLevel: 9 })
.toBuffer();
const png = await createWidePng();
const blocks = [
{
@@ -72,10 +76,7 @@ describe("tool image sanitizing", () => {
];
const out = await sanitizeContentBlocksImages(blocks, "test");
const image = out.find((b) => b.type === "image");
if (!image || image.type !== "image") {
throw new Error("expected image block");
}
const image = getImageBlock(out);
const meta = await sharp(Buffer.from(image.data, "base64")).metadata();
expect(meta.width).toBeLessThanOrEqual(1200);
expect(meta.height).toBeLessThanOrEqual(1200);
@@ -103,10 +104,7 @@ describe("tool image sanitizing", () => {
];
const out = await sanitizeContentBlocksImages(blocks, "test");
const image = out.find((b) => b.type === "image");
if (!image || image.type !== "image") {
throw new Error("expected image block");
}
const image = getImageBlock(out);
expect(image.mimeType).toBe("image/jpeg");
});
});