mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-07 19:41:23 +00:00
perf(test): trim fixture and import overhead in hot suites
This commit is contained in:
@@ -9,6 +9,8 @@ import { loadWebMedia, loadWebMediaRaw, optimizeImageToJpeg } from "./media.js";
|
||||
|
||||
let fixtureRoot = "";
|
||||
let fixtureFileCount = 0;
|
||||
let largeJpegBuffer: Buffer;
|
||||
let tinyPngBuffer: Buffer;
|
||||
|
||||
async function writeTempFile(buffer: Buffer, ext: string): Promise<string> {
|
||||
const file = path.join(fixtureRoot, `media-${fixtureFileCount++}${ext}`);
|
||||
@@ -27,23 +29,27 @@ function buildDeterministicBytes(length: number): Buffer {
|
||||
}
|
||||
|
||||
async function createLargeTestJpeg(): Promise<{ buffer: Buffer; file: string }> {
|
||||
const buffer = await sharp({
|
||||
const file = await writeTempFile(largeJpegBuffer, ".jpg");
|
||||
return { buffer: largeJpegBuffer, file };
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
fixtureRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-media-test-"));
|
||||
largeJpegBuffer = await sharp({
|
||||
create: {
|
||||
width: 1600,
|
||||
height: 1600,
|
||||
width: 1200,
|
||||
height: 1200,
|
||||
channels: 3,
|
||||
background: "#ff0000",
|
||||
},
|
||||
})
|
||||
.jpeg({ quality: 95 })
|
||||
.toBuffer();
|
||||
|
||||
const file = await writeTempFile(buffer, ".jpg");
|
||||
return { buffer, file };
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
fixtureRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-media-test-"));
|
||||
tinyPngBuffer = await sharp({
|
||||
create: { width: 10, height: 10, channels: 3, background: "#00ff00" },
|
||||
})
|
||||
.png()
|
||||
.toBuffer();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
@@ -68,18 +74,7 @@ describe("web media loading", () => {
|
||||
});
|
||||
|
||||
it("compresses large local images under the provided cap", async () => {
|
||||
const buffer = await sharp({
|
||||
create: {
|
||||
width: 1200,
|
||||
height: 1200,
|
||||
channels: 3,
|
||||
background: "#ff0000",
|
||||
},
|
||||
})
|
||||
.jpeg({ quality: 95 })
|
||||
.toBuffer();
|
||||
|
||||
const file = await writeTempFile(buffer, ".jpg");
|
||||
const { buffer, file } = await createLargeTestJpeg();
|
||||
|
||||
const cap = Math.floor(buffer.length * 0.8);
|
||||
const result = await loadWebMedia(file, cap);
|
||||
@@ -109,12 +104,7 @@ describe("web media loading", () => {
|
||||
});
|
||||
|
||||
it("sniffs mime before extension when loading local files", async () => {
|
||||
const pngBuffer = await sharp({
|
||||
create: { width: 2, height: 2, channels: 3, background: "#00ff00" },
|
||||
})
|
||||
.png()
|
||||
.toBuffer();
|
||||
const wrongExt = await writeTempFile(pngBuffer, ".bin");
|
||||
const wrongExt = await writeTempFile(tinyPngBuffer, ".bin");
|
||||
|
||||
const result = await loadWebMedia(wrongExt, 1024 * 1024);
|
||||
|
||||
@@ -292,7 +282,7 @@ describe("web media loading", () => {
|
||||
});
|
||||
|
||||
it("falls back to JPEG when PNG alpha cannot fit under cap", async () => {
|
||||
const sizes = [320, 448, 640];
|
||||
const sizes = [256, 320, 448];
|
||||
let pngBuffer: Buffer | null = null;
|
||||
let smallestPng: Awaited<ReturnType<typeof optimizeImageToPng>> | null = null;
|
||||
let jpegOptimized: Awaited<ReturnType<typeof optimizeImageToJpeg>> | null = null;
|
||||
@@ -333,12 +323,7 @@ describe("web media loading", () => {
|
||||
|
||||
describe("local media root guard", () => {
|
||||
it("rejects local paths outside allowed roots", async () => {
|
||||
const pngBuffer = await sharp({
|
||||
create: { width: 10, height: 10, channels: 3, background: "#00ff00" },
|
||||
})
|
||||
.png()
|
||||
.toBuffer();
|
||||
const file = await writeTempFile(pngBuffer, ".png");
|
||||
const file = await writeTempFile(tinyPngBuffer, ".png");
|
||||
|
||||
// Explicit roots that don't contain the temp file.
|
||||
await expect(
|
||||
@@ -347,24 +332,14 @@ describe("local media root guard", () => {
|
||||
});
|
||||
|
||||
it("allows local paths under an explicit root", async () => {
|
||||
const pngBuffer = await sharp({
|
||||
create: { width: 10, height: 10, channels: 3, background: "#00ff00" },
|
||||
})
|
||||
.png()
|
||||
.toBuffer();
|
||||
const file = await writeTempFile(pngBuffer, ".png");
|
||||
const file = await writeTempFile(tinyPngBuffer, ".png");
|
||||
|
||||
const result = await loadWebMedia(file, 1024 * 1024, { localRoots: [os.tmpdir()] });
|
||||
expect(result.kind).toBe("image");
|
||||
});
|
||||
|
||||
it("allows any path when localRoots is 'any'", async () => {
|
||||
const pngBuffer = await sharp({
|
||||
create: { width: 10, height: 10, channels: 3, background: "#00ff00" },
|
||||
})
|
||||
.png()
|
||||
.toBuffer();
|
||||
const file = await writeTempFile(pngBuffer, ".png");
|
||||
const file = await writeTempFile(tinyPngBuffer, ".png");
|
||||
|
||||
const result = await loadWebMedia(file, 1024 * 1024, { localRoots: "any" });
|
||||
expect(result.kind).toBe("image");
|
||||
|
||||
Reference in New Issue
Block a user