mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 14:38:25 +00:00
test(perf): cache media fixtures and trim timeout waits
This commit is contained in:
@@ -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 () => {
|
||||
|
||||
Reference in New Issue
Block a user