
























@@ -1,4 +1,6 @@
1-import sharp from "sharp";
1+import fs from "node:fs/promises";
2+import { getImageMetadata } from "openclaw/plugin-sdk/media-runtime";
3+import { createSolidPngBuffer } from "openclaw/plugin-sdk/test-fixtures";
24import { describe, expect, it } from "vitest";
35import { normalizeBrowserScreenshot } from "./screenshot.js";
46@@ -20,41 +22,23 @@ describe("browser screenshot normalization", () => {
2022}
21232224it("shrinks oversized images to <=2000x2000 and <=5MB", async () => {
23-const bigPng = await sharp({
24-create: {
25-width: 2100,
26-height: 2100,
27-channels: 3,
28-background: { r: 12, g: 34, b: 56 },
29-},
30-})
31-.png({ compressionLevel: 0 })
32-.toBuffer();
25+const bigPng = createSolidPngBuffer(2100, 2100, { r: 12, g: 34, b: 56 });
33263427const normalized = await normalizeBrowserScreenshot(bigPng, {
3528maxSide: 2000,
3629maxBytes: 5 * 1024 * 1024,
3730});
38313932expect(normalized.buffer.byteLength).toBeLessThanOrEqual(5 * 1024 * 1024);
40-const meta = await sharp(normalized.buffer).metadata();
41-expect(meta.width).toBeLessThanOrEqual(2000);
42-expect(meta.height).toBeLessThanOrEqual(2000);
33+const meta = await getImageMetadata(normalized.buffer);
34+expect(meta?.width).toBeLessThanOrEqual(2000);
35+expect(meta?.height).toBeLessThanOrEqual(2000);
4336expect(normalized.buffer[0]).toBe(0xff);
4437expect(normalized.buffer[1]).toBe(0xd8);
4538}, 120_000);
46394740it("keeps already-small screenshots unchanged", async () => {
48-const jpeg = await sharp({
49-create: {
50-width: 800,
51-height: 600,
52-channels: 3,
53-background: { r: 255, g: 0, b: 0 },
54-},
55-})
56-.jpeg({ quality: 80 })
57-.toBuffer();
41+const jpeg = await fs.readFile("docs/assets/showcase/roof-camera-sky.jpg");
58425943const normalized = await normalizeBrowserScreenshot(jpeg, {
6044maxSide: 2000,
@@ -65,16 +49,7 @@ describe("browser screenshot normalization", () => {
6549});
66506751it("rejects screenshots above max side when no image processor is available", async () => {
68-const png = await sharp({
69-create: {
70-width: 420,
71-height: 120,
72-channels: 3,
73-background: { r: 12, g: 34, b: 56 },
74-},
75-})
76-.png({ compressionLevel: 9 })
77-.toBuffer();
52+const png = createSolidPngBuffer(420, 120, { r: 12, g: 34, b: 56 });
7853expect(png.byteLength).toBeLessThan(5 * 1024 * 1024);
79548055await withUnavailableImageBackend(async () => {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。