mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 09:02:45 +00:00
refactor(media): dedupe runner proxy and video test fixtures
This commit is contained in:
@@ -1,26 +1,57 @@
|
|||||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
import type { OpenClawConfig } from "../config/config.js";
|
import type { OpenClawConfig } from "../config/config.js";
|
||||||
import { buildProviderRegistry, runCapability } from "./runner.js";
|
import { buildProviderRegistry, runCapability } from "./runner.js";
|
||||||
import { withAudioFixture, withMediaFixture } from "./runner.test-utils.js";
|
import { withAudioFixture, withVideoFixture } from "./runner.test-utils.js";
|
||||||
import type { AudioTranscriptionRequest, VideoDescriptionRequest } from "./types.js";
|
import type { AudioTranscriptionRequest, VideoDescriptionRequest } from "./types.js";
|
||||||
|
|
||||||
async function withVideoFixture(
|
async function runAudioCapabilityWithFetchCapture(params: {
|
||||||
filePrefix: string,
|
fixturePrefix: string;
|
||||||
run: (params: {
|
outputText: string;
|
||||||
ctx: { MediaPath: string; MediaType: string };
|
}): Promise<typeof fetch | undefined> {
|
||||||
media: ReturnType<typeof import("./runner.js").normalizeMediaAttachments>;
|
let seenFetchFn: typeof fetch | undefined;
|
||||||
cache: ReturnType<typeof import("./runner.js").createMediaAttachmentCache>;
|
await withAudioFixture(params.fixturePrefix, async ({ ctx, media, cache }) => {
|
||||||
}) => Promise<void>,
|
const providerRegistry = buildProviderRegistry({
|
||||||
) {
|
openai: {
|
||||||
await withMediaFixture(
|
id: "openai",
|
||||||
{
|
capabilities: ["audio"],
|
||||||
filePrefix,
|
transcribeAudio: async (req: AudioTranscriptionRequest) => {
|
||||||
extension: "mp4",
|
seenFetchFn = req.fetchFn;
|
||||||
mediaType: "video/mp4",
|
return { text: params.outputText, model: req.model };
|
||||||
fileContents: Buffer.from("video"),
|
},
|
||||||
},
|
},
|
||||||
run,
|
});
|
||||||
);
|
|
||||||
|
const cfg = {
|
||||||
|
models: {
|
||||||
|
providers: {
|
||||||
|
openai: {
|
||||||
|
apiKey: "test-key",
|
||||||
|
models: [],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
tools: {
|
||||||
|
media: {
|
||||||
|
audio: {
|
||||||
|
enabled: true,
|
||||||
|
models: [{ provider: "openai", model: "whisper-1" }],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as unknown as OpenClawConfig;
|
||||||
|
|
||||||
|
const result = await runCapability({
|
||||||
|
capability: "audio",
|
||||||
|
cfg,
|
||||||
|
ctx,
|
||||||
|
attachments: cache,
|
||||||
|
media,
|
||||||
|
providerRegistry,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.outputs[0]?.text).toBe(params.outputText);
|
||||||
|
});
|
||||||
|
return seenFetchFn;
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("runCapability proxy fetch passthrough", () => {
|
describe("runCapability proxy fetch passthrough", () => {
|
||||||
@@ -29,53 +60,12 @@ describe("runCapability proxy fetch passthrough", () => {
|
|||||||
|
|
||||||
it("passes fetchFn to audio provider when HTTPS_PROXY is set", async () => {
|
it("passes fetchFn to audio provider when HTTPS_PROXY is set", async () => {
|
||||||
vi.stubEnv("HTTPS_PROXY", "http://proxy.test:8080");
|
vi.stubEnv("HTTPS_PROXY", "http://proxy.test:8080");
|
||||||
|
const seenFetchFn = await runAudioCapabilityWithFetchCapture({
|
||||||
await withAudioFixture("openclaw-audio-proxy", async ({ ctx, media, cache }) => {
|
fixturePrefix: "openclaw-audio-proxy",
|
||||||
let seenFetchFn: typeof fetch | undefined;
|
outputText: "transcribed",
|
||||||
|
|
||||||
const providerRegistry = buildProviderRegistry({
|
|
||||||
openai: {
|
|
||||||
id: "openai",
|
|
||||||
capabilities: ["audio"],
|
|
||||||
transcribeAudio: async (req: AudioTranscriptionRequest) => {
|
|
||||||
seenFetchFn = req.fetchFn;
|
|
||||||
return { text: "transcribed", model: req.model };
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const cfg = {
|
|
||||||
models: {
|
|
||||||
providers: {
|
|
||||||
openai: {
|
|
||||||
apiKey: "test-key",
|
|
||||||
models: [],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
tools: {
|
|
||||||
media: {
|
|
||||||
audio: {
|
|
||||||
enabled: true,
|
|
||||||
models: [{ provider: "openai", model: "whisper-1" }],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
} as unknown as OpenClawConfig;
|
|
||||||
|
|
||||||
const result = await runCapability({
|
|
||||||
capability: "audio",
|
|
||||||
cfg,
|
|
||||||
ctx,
|
|
||||||
attachments: cache,
|
|
||||||
media,
|
|
||||||
providerRegistry,
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(result.outputs[0]?.text).toBe("transcribed");
|
|
||||||
expect(seenFetchFn).toBeDefined();
|
|
||||||
expect(seenFetchFn).not.toBe(globalThis.fetch);
|
|
||||||
});
|
});
|
||||||
|
expect(seenFetchFn).toBeDefined();
|
||||||
|
expect(seenFetchFn).not.toBe(globalThis.fetch);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("passes fetchFn to video provider when HTTPS_PROXY is set", async () => {
|
it("passes fetchFn to video provider when HTTPS_PROXY is set", async () => {
|
||||||
@@ -134,50 +124,10 @@ describe("runCapability proxy fetch passthrough", () => {
|
|||||||
vi.stubEnv("https_proxy", "");
|
vi.stubEnv("https_proxy", "");
|
||||||
vi.stubEnv("http_proxy", "");
|
vi.stubEnv("http_proxy", "");
|
||||||
|
|
||||||
await withAudioFixture("openclaw-audio-no-proxy", async ({ ctx, media, cache }) => {
|
const seenFetchFn = await runAudioCapabilityWithFetchCapture({
|
||||||
let seenFetchFn: typeof fetch | undefined;
|
fixturePrefix: "openclaw-audio-no-proxy",
|
||||||
|
outputText: "ok",
|
||||||
const providerRegistry = buildProviderRegistry({
|
|
||||||
openai: {
|
|
||||||
id: "openai",
|
|
||||||
capabilities: ["audio"],
|
|
||||||
transcribeAudio: async (req: AudioTranscriptionRequest) => {
|
|
||||||
seenFetchFn = req.fetchFn;
|
|
||||||
return { text: "ok", model: req.model };
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const cfg = {
|
|
||||||
models: {
|
|
||||||
providers: {
|
|
||||||
openai: {
|
|
||||||
apiKey: "test-key",
|
|
||||||
models: [],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
tools: {
|
|
||||||
media: {
|
|
||||||
audio: {
|
|
||||||
enabled: true,
|
|
||||||
models: [{ provider: "openai", model: "whisper-1" }],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
} as unknown as OpenClawConfig;
|
|
||||||
|
|
||||||
const result = await runCapability({
|
|
||||||
capability: "audio",
|
|
||||||
cfg,
|
|
||||||
ctx,
|
|
||||||
attachments: cache,
|
|
||||||
media,
|
|
||||||
providerRegistry,
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(result.outputs[0]?.text).toBe("ok");
|
|
||||||
expect(seenFetchFn).toBeUndefined();
|
|
||||||
});
|
});
|
||||||
|
expect(seenFetchFn).toBeUndefined();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -54,3 +54,18 @@ export async function withAudioFixture(
|
|||||||
run,
|
run,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function withVideoFixture(
|
||||||
|
filePrefix: string,
|
||||||
|
run: (params: MediaFixtureParams) => Promise<void>,
|
||||||
|
) {
|
||||||
|
await withMediaFixture(
|
||||||
|
{
|
||||||
|
filePrefix,
|
||||||
|
extension: "mp4",
|
||||||
|
mediaType: "video/mp4",
|
||||||
|
fileContents: Buffer.from("video"),
|
||||||
|
},
|
||||||
|
run,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,26 +2,7 @@ import { describe, expect, it } from "vitest";
|
|||||||
import type { OpenClawConfig } from "../config/config.js";
|
import type { OpenClawConfig } from "../config/config.js";
|
||||||
import { withEnvAsync } from "../test-utils/env.js";
|
import { withEnvAsync } from "../test-utils/env.js";
|
||||||
import { runCapability } from "./runner.js";
|
import { runCapability } from "./runner.js";
|
||||||
import { withMediaFixture } from "./runner.test-utils.js";
|
import { withVideoFixture } from "./runner.test-utils.js";
|
||||||
|
|
||||||
async function withVideoFixture(
|
|
||||||
filePrefix: string,
|
|
||||||
run: (params: {
|
|
||||||
ctx: { MediaPath: string; MediaType: string };
|
|
||||||
media: ReturnType<typeof import("./runner.js").normalizeMediaAttachments>;
|
|
||||||
cache: ReturnType<typeof import("./runner.js").createMediaAttachmentCache>;
|
|
||||||
}) => Promise<void>,
|
|
||||||
) {
|
|
||||||
await withMediaFixture(
|
|
||||||
{
|
|
||||||
filePrefix,
|
|
||||||
extension: "mp4",
|
|
||||||
mediaType: "video/mp4",
|
|
||||||
fileContents: Buffer.from("video"),
|
|
||||||
},
|
|
||||||
run,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("runCapability video provider wiring", () => {
|
describe("runCapability video provider wiring", () => {
|
||||||
it("merges video baseUrl and headers with entry precedence", async () => {
|
it("merges video baseUrl and headers with entry precedence", async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user