mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 08:52:43 +00:00
refactor(test): share media audio fixture across runner tests
This commit is contained in:
@@ -1,41 +1,7 @@
|
|||||||
import fs from "node:fs/promises";
|
|
||||||
import os from "node:os";
|
|
||||||
import path from "node:path";
|
|
||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import type { MsgContext } from "../auto-reply/templating.js";
|
|
||||||
import type { OpenClawConfig } from "../config/config.js";
|
import type { OpenClawConfig } from "../config/config.js";
|
||||||
import {
|
import { buildProviderRegistry, runCapability } from "./runner.js";
|
||||||
buildProviderRegistry,
|
import { withAudioFixture } from "./runner.test-utils.js";
|
||||||
createMediaAttachmentCache,
|
|
||||||
normalizeMediaAttachments,
|
|
||||||
runCapability,
|
|
||||||
} from "./runner.js";
|
|
||||||
|
|
||||||
async function withAudioFixture(
|
|
||||||
run: (params: {
|
|
||||||
ctx: MsgContext;
|
|
||||||
media: ReturnType<typeof normalizeMediaAttachments>;
|
|
||||||
cache: ReturnType<typeof createMediaAttachmentCache>;
|
|
||||||
}) => Promise<void>,
|
|
||||||
) {
|
|
||||||
const originalPath = process.env.PATH;
|
|
||||||
process.env.PATH = "";
|
|
||||||
const tmpPath = path.join(os.tmpdir(), `openclaw-auto-audio-${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 run({ ctx, media, cache });
|
|
||||||
} finally {
|
|
||||||
process.env.PATH = originalPath;
|
|
||||||
await cache.cleanup();
|
|
||||||
await fs.unlink(tmpPath).catch(() => {});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function createOpenAiAudioProvider(
|
function createOpenAiAudioProvider(
|
||||||
transcribeAudio: (req: { model?: string }) => Promise<{ text: string; model: string }>,
|
transcribeAudio: (req: { model?: string }) => Promise<{ text: string; model: string }>,
|
||||||
@@ -65,7 +31,7 @@ function createOpenAiAudioCfg(extra?: Partial<OpenClawConfig>): OpenClawConfig {
|
|||||||
|
|
||||||
describe("runCapability auto audio entries", () => {
|
describe("runCapability auto audio entries", () => {
|
||||||
it("uses provider keys to auto-enable audio transcription", async () => {
|
it("uses provider keys to auto-enable audio transcription", async () => {
|
||||||
await withAudioFixture(async ({ ctx, media, cache }) => {
|
await withAudioFixture("openclaw-auto-audio", async ({ ctx, media, cache }) => {
|
||||||
let seenModel: string | undefined;
|
let seenModel: string | undefined;
|
||||||
const providerRegistry = createOpenAiAudioProvider(async (req) => {
|
const providerRegistry = createOpenAiAudioProvider(async (req) => {
|
||||||
seenModel = req.model;
|
seenModel = req.model;
|
||||||
@@ -88,7 +54,7 @@ describe("runCapability auto audio entries", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("skips auto audio when disabled", async () => {
|
it("skips auto audio when disabled", async () => {
|
||||||
await withAudioFixture(async ({ ctx, media, cache }) => {
|
await withAudioFixture("openclaw-auto-audio", async ({ ctx, media, cache }) => {
|
||||||
const providerRegistry = createOpenAiAudioProvider(async () => ({
|
const providerRegistry = createOpenAiAudioProvider(async () => ({
|
||||||
text: "ok",
|
text: "ok",
|
||||||
model: "whisper-1",
|
model: "whisper-1",
|
||||||
@@ -117,7 +83,7 @@ describe("runCapability auto audio entries", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("prefers explicitly configured audio model entries", async () => {
|
it("prefers explicitly configured audio model entries", async () => {
|
||||||
await withAudioFixture(async ({ ctx, media, cache }) => {
|
await withAudioFixture("openclaw-auto-audio", async ({ ctx, media, cache }) => {
|
||||||
let seenModel: string | undefined;
|
let seenModel: string | undefined;
|
||||||
const providerRegistry = createOpenAiAudioProvider(async (req) => {
|
const providerRegistry = createOpenAiAudioProvider(async (req) => {
|
||||||
seenModel = req.model;
|
seenModel = req.model;
|
||||||
|
|||||||
@@ -1,45 +1,11 @@
|
|||||||
import fs from "node:fs/promises";
|
|
||||||
import os from "node:os";
|
|
||||||
import path from "node:path";
|
|
||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import type { MsgContext } from "../auto-reply/templating.js";
|
|
||||||
import type { OpenClawConfig } from "../config/config.js";
|
import type { OpenClawConfig } from "../config/config.js";
|
||||||
import {
|
import { buildProviderRegistry, runCapability } from "./runner.js";
|
||||||
buildProviderRegistry,
|
import { withAudioFixture } from "./runner.test-utils.js";
|
||||||
createMediaAttachmentCache,
|
|
||||||
normalizeMediaAttachments,
|
|
||||||
runCapability,
|
|
||||||
} from "./runner.js";
|
|
||||||
|
|
||||||
async function withAudioFixture(
|
|
||||||
run: (params: {
|
|
||||||
ctx: MsgContext;
|
|
||||||
media: ReturnType<typeof normalizeMediaAttachments>;
|
|
||||||
cache: ReturnType<typeof createMediaAttachmentCache>;
|
|
||||||
}) => Promise<void>,
|
|
||||||
) {
|
|
||||||
const originalPath = process.env.PATH;
|
|
||||||
process.env.PATH = "";
|
|
||||||
const tmpPath = path.join(os.tmpdir(), `openclaw-deepgram-${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 run({ ctx, media, cache });
|
|
||||||
} finally {
|
|
||||||
process.env.PATH = originalPath;
|
|
||||||
await cache.cleanup();
|
|
||||||
await fs.unlink(tmpPath).catch(() => {});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("runCapability deepgram provider options", () => {
|
describe("runCapability deepgram provider options", () => {
|
||||||
it("merges provider options, headers, and baseUrl overrides", async () => {
|
it("merges provider options, headers, and baseUrl overrides", async () => {
|
||||||
await withAudioFixture(async ({ ctx, media, cache }) => {
|
await withAudioFixture("openclaw-deepgram", async ({ ctx, media, cache }) => {
|
||||||
let seenQuery: Record<string, string | number | boolean> | undefined;
|
let seenQuery: Record<string, string | number | boolean> | undefined;
|
||||||
let seenBaseUrl: string | undefined;
|
let seenBaseUrl: string | undefined;
|
||||||
let seenHeaders: Record<string, string> | undefined;
|
let seenHeaders: Record<string, string> | undefined;
|
||||||
|
|||||||
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