test(perf): cache media fixtures and trim timeout waits

This commit is contained in:
Peter Steinberger
2026-03-02 10:52:58 +00:00
parent 8a1465c314
commit 4a8ada662e
3 changed files with 28 additions and 5 deletions

View File

@@ -1,3 +1,4 @@
import crypto from "node:crypto";
import fs from "node:fs/promises";
import path from "node:path";
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
@@ -36,6 +37,8 @@ let applyMediaUnderstanding: typeof import("./apply.js").applyMediaUnderstanding
const TEMP_MEDIA_PREFIX = "openclaw-media-";
let suiteTempMediaRootDir = "";
let tempMediaDirCounter = 0;
let sharedTempMediaCacheDir = "";
const tempMediaFileCache = new Map<string, string>();
async function createTempMediaDir() {
if (!suiteTempMediaRootDir) {
@@ -47,6 +50,13 @@ async function createTempMediaDir() {
return dir;
}
async function getSharedTempMediaCacheDir() {
if (!sharedTempMediaCacheDir) {
sharedTempMediaCacheDir = await createTempMediaDir();
}
return sharedTempMediaCacheDir;
}
function createGroqAudioConfig(): OpenClawConfig {
return {
tools: {
@@ -111,9 +121,20 @@ function createMediaDisabledConfigWithAllowedMimes(allowedMimes: string[]): Open
}
async function createTempMediaFile(params: { fileName: string; content: Buffer | string }) {
const dir = await createTempMediaDir();
const mediaPath = path.join(dir, params.fileName);
const normalizedContent =
typeof params.content === "string" ? Buffer.from(params.content) : params.content;
const contentHash = crypto.createHash("sha1").update(normalizedContent).digest("hex");
const cacheKey = `${params.fileName}:${contentHash}`;
const cachedPath = tempMediaFileCache.get(cacheKey);
if (cachedPath) {
return cachedPath;
}
const cacheRootDir = await getSharedTempMediaCacheDir();
const cacheDir = path.join(cacheRootDir, contentHash);
await fs.mkdir(cacheDir, { recursive: true });
const mediaPath = path.join(cacheDir, params.fileName);
await fs.writeFile(mediaPath, params.content);
tempMediaFileCache.set(cacheKey, mediaPath);
return mediaPath;
}
@@ -234,6 +255,8 @@ describe("applyMediaUnderstanding", () => {
}
await fs.rm(suiteTempMediaRootDir, { recursive: true, force: true });
suiteTempMediaRootDir = "";
sharedTempMediaCacheDir = "";
tempMediaFileCache.clear();
});
it("sets Transcript and replaces Body when audio transcription succeeds", async () => {