mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 01:31:23 +00:00
test(agents): dedupe tool image fixture setup
This commit is contained in:
@@ -3,6 +3,27 @@ import { describe, expect, it } from "vitest";
|
|||||||
import { sanitizeContentBlocksImages, sanitizeImageBlocks } from "./tool-images.js";
|
import { sanitizeContentBlocksImages, sanitizeImageBlocks } from "./tool-images.js";
|
||||||
|
|
||||||
describe("tool image sanitizing", () => {
|
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 () => {
|
it("shrinks oversized images to <=5MB", async () => {
|
||||||
const width = 2800;
|
const width = 2800;
|
||||||
const height = 2800;
|
const height = 2800;
|
||||||
@@ -23,24 +44,14 @@ describe("tool image sanitizing", () => {
|
|||||||
];
|
];
|
||||||
|
|
||||||
const out = await sanitizeContentBlocksImages(blocks, "test");
|
const out = await sanitizeContentBlocksImages(blocks, "test");
|
||||||
const image = out.find((b) => b.type === "image");
|
const image = getImageBlock(out);
|
||||||
if (!image || image.type !== "image") {
|
|
||||||
throw new Error("expected image block");
|
|
||||||
}
|
|
||||||
const size = Buffer.from(image.data, "base64").byteLength;
|
const size = Buffer.from(image.data, "base64").byteLength;
|
||||||
expect(size).toBeLessThanOrEqual(5 * 1024 * 1024);
|
expect(size).toBeLessThanOrEqual(5 * 1024 * 1024);
|
||||||
expect(image.mimeType).toBe("image/jpeg");
|
expect(image.mimeType).toBe("image/jpeg");
|
||||||
}, 20_000);
|
}, 20_000);
|
||||||
|
|
||||||
it("sanitizes image arrays and reports drops", async () => {
|
it("sanitizes image arrays and reports drops", async () => {
|
||||||
const width = 2600;
|
const png = await createWidePng();
|
||||||
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 images = [
|
const images = [
|
||||||
{ type: "image" as const, data: png.toString("base64"), mimeType: "image/png" },
|
{ type: "image" as const, data: png.toString("base64"), mimeType: "image/png" },
|
||||||
@@ -54,14 +65,7 @@ describe("tool image sanitizing", () => {
|
|||||||
}, 20_000);
|
}, 20_000);
|
||||||
|
|
||||||
it("shrinks images that exceed max dimension even if size is small", async () => {
|
it("shrinks images that exceed max dimension even if size is small", async () => {
|
||||||
const width = 2600;
|
const png = await createWidePng();
|
||||||
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 blocks = [
|
const blocks = [
|
||||||
{
|
{
|
||||||
@@ -72,10 +76,7 @@ describe("tool image sanitizing", () => {
|
|||||||
];
|
];
|
||||||
|
|
||||||
const out = await sanitizeContentBlocksImages(blocks, "test");
|
const out = await sanitizeContentBlocksImages(blocks, "test");
|
||||||
const image = out.find((b) => b.type === "image");
|
const image = getImageBlock(out);
|
||||||
if (!image || image.type !== "image") {
|
|
||||||
throw new Error("expected image block");
|
|
||||||
}
|
|
||||||
const meta = await sharp(Buffer.from(image.data, "base64")).metadata();
|
const meta = await sharp(Buffer.from(image.data, "base64")).metadata();
|
||||||
expect(meta.width).toBeLessThanOrEqual(1200);
|
expect(meta.width).toBeLessThanOrEqual(1200);
|
||||||
expect(meta.height).toBeLessThanOrEqual(1200);
|
expect(meta.height).toBeLessThanOrEqual(1200);
|
||||||
@@ -103,10 +104,7 @@ describe("tool image sanitizing", () => {
|
|||||||
];
|
];
|
||||||
|
|
||||||
const out = await sanitizeContentBlocksImages(blocks, "test");
|
const out = await sanitizeContentBlocksImages(blocks, "test");
|
||||||
const image = out.find((b) => b.type === "image");
|
const image = getImageBlock(out);
|
||||||
if (!image || image.type !== "image") {
|
|
||||||
throw new Error("expected image block");
|
|
||||||
}
|
|
||||||
expect(image.mimeType).toBe("image/jpeg");
|
expect(image.mimeType).toBe("image/jpeg");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user