mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 05:11:25 +00:00
refactor: de-duplicate channel runtime and payload helpers
This commit is contained in:
@@ -1,23 +1,30 @@
|
||||
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;
|
||||
type MediaFixtureParams = {
|
||||
ctx: { MediaPath: string; MediaType: string };
|
||||
media: ReturnType<typeof normalizeMediaAttachments>;
|
||||
cache: ReturnType<typeof createMediaAttachmentCache>;
|
||||
};
|
||||
|
||||
export async function withAudioFixture(
|
||||
filePrefix: string,
|
||||
run: (params: AudioFixtureParams) => Promise<void>,
|
||||
export async function withMediaFixture(
|
||||
params: {
|
||||
filePrefix: string;
|
||||
extension: string;
|
||||
mediaType: string;
|
||||
fileContents: Buffer;
|
||||
},
|
||||
run: (params: MediaFixtureParams) => Promise<void>,
|
||||
) {
|
||||
const tmpPath = path.join(os.tmpdir(), filePrefix + "-" + Date.now().toString() + ".wav");
|
||||
await fs.writeFile(tmpPath, Buffer.from("RIFF"));
|
||||
const ctx: MsgContext = { MediaPath: tmpPath, MediaType: "audio/wav" };
|
||||
const tmpPath = path.join(
|
||||
os.tmpdir(),
|
||||
`${params.filePrefix}-${Date.now().toString()}.${params.extension}`,
|
||||
);
|
||||
await fs.writeFile(tmpPath, params.fileContents);
|
||||
const ctx = { MediaPath: tmpPath, MediaType: params.mediaType };
|
||||
const media = normalizeMediaAttachments(ctx);
|
||||
const cache = createMediaAttachmentCache(media, {
|
||||
localPathRoots: [path.dirname(tmpPath)],
|
||||
@@ -32,3 +39,18 @@ export async function withAudioFixture(
|
||||
await fs.unlink(tmpPath).catch(() => {});
|
||||
}
|
||||
}
|
||||
|
||||
export async function withAudioFixture(
|
||||
filePrefix: string,
|
||||
run: (params: MediaFixtureParams) => Promise<void>,
|
||||
) {
|
||||
await withMediaFixture(
|
||||
{
|
||||
filePrefix,
|
||||
extension: "wav",
|
||||
mediaType: "audio/wav",
|
||||
fileContents: Buffer.from("RIFF"),
|
||||
},
|
||||
run,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,34 +1,26 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { OpenClawConfig } from "../config/config.js";
|
||||
import { withEnvAsync } from "../test-utils/env.js";
|
||||
import { createMediaAttachmentCache, normalizeMediaAttachments, runCapability } from "./runner.js";
|
||||
import { runCapability } from "./runner.js";
|
||||
import { withMediaFixture } from "./runner.test-utils.js";
|
||||
|
||||
async function withVideoFixture(
|
||||
filePrefix: string,
|
||||
run: (params: {
|
||||
ctx: { MediaPath: string; MediaType: string };
|
||||
media: ReturnType<typeof normalizeMediaAttachments>;
|
||||
cache: ReturnType<typeof createMediaAttachmentCache>;
|
||||
media: ReturnType<typeof import("./runner.js").normalizeMediaAttachments>;
|
||||
cache: ReturnType<typeof import("./runner.js").createMediaAttachmentCache>;
|
||||
}) => Promise<void>,
|
||||
) {
|
||||
const tmpPath = path.join(os.tmpdir(), `${filePrefix}-${Date.now().toString()}.mp4`);
|
||||
await fs.writeFile(tmpPath, Buffer.from("video"));
|
||||
const ctx = { MediaPath: tmpPath, MediaType: "video/mp4" };
|
||||
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(() => {});
|
||||
}
|
||||
await withMediaFixture(
|
||||
{
|
||||
filePrefix,
|
||||
extension: "mp4",
|
||||
mediaType: "video/mp4",
|
||||
fileContents: Buffer.from("video"),
|
||||
},
|
||||
run,
|
||||
);
|
||||
}
|
||||
|
||||
describe("runCapability video provider wiring", () => {
|
||||
|
||||
Reference in New Issue
Block a user