mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 23:38:27 +00:00
refactor(test): share media audio fixture across runner tests
This commit is contained in:
34
src/media-understanding/runner.test-utils.ts
Normal file
34
src/media-understanding/runner.test-utils.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import type { MsgContext } from "../auto-reply/templating.js";
|
||||
import { withEnvAsync } from "../test-utils/env.js";
|
||||
import { createMediaAttachmentCache, normalizeMediaAttachments } from "./runner.js";
|
||||
|
||||
type AudioFixtureParams = {
|
||||
ctx: MsgContext;
|
||||
media: ReturnType<typeof normalizeMediaAttachments>;
|
||||
cache: ReturnType<typeof createMediaAttachmentCache>;
|
||||
};
|
||||
|
||||
export async function withAudioFixture(
|
||||
filePrefix: string,
|
||||
run: (params: AudioFixtureParams) => Promise<void>,
|
||||
) {
|
||||
const tmpPath = path.join(os.tmpdir(), `${filePrefix}-${Date.now()}.wav`);
|
||||
await fs.writeFile(tmpPath, Buffer.from("RIFF"));
|
||||
const ctx: MsgContext = { MediaPath: tmpPath, MediaType: "audio/wav" };
|
||||
const media = normalizeMediaAttachments(ctx);
|
||||
const cache = createMediaAttachmentCache(media, {
|
||||
localPathRoots: [path.dirname(tmpPath)],
|
||||
});
|
||||
|
||||
try {
|
||||
await withEnvAsync({ PATH: "" }, async () => {
|
||||
await run({ ctx, media, cache });
|
||||
});
|
||||
} finally {
|
||||
await cache.cleanup();
|
||||
await fs.unlink(tmpPath).catch(() => {});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user