mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-07 09:21:26 +00:00
test(media): dedupe audio provider request assertions
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import type { MockInstance } from "vitest";
|
import type { MockInstance } from "vitest";
|
||||||
import { afterEach, beforeEach, vi } from "vitest";
|
import { afterEach, beforeEach, vi } from "vitest";
|
||||||
import * as ssrf from "../../infra/net/ssrf.js";
|
import * as ssrf from "../../infra/net/ssrf.js";
|
||||||
|
import { withFetchPreconnect } from "../../test-utils/fetch-mock.js";
|
||||||
|
|
||||||
export function resolveRequestUrl(input: RequestInfo | URL): string {
|
export function resolveRequestUrl(input: RequestInfo | URL): string {
|
||||||
if (typeof input === "string") {
|
if (typeof input === "string") {
|
||||||
@@ -40,3 +41,36 @@ export function installPinnedHostnameTestHooks(): void {
|
|||||||
resolvePinnedHostnameWithPolicySpy = null;
|
resolvePinnedHostnameWithPolicySpy = null;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function createAuthCaptureJsonFetch(responseBody: unknown) {
|
||||||
|
let seenAuth: string | null = null;
|
||||||
|
const fetchFn = withFetchPreconnect(async (_input: RequestInfo | URL, init?: RequestInit) => {
|
||||||
|
const headers = new Headers(init?.headers);
|
||||||
|
seenAuth = headers.get("authorization");
|
||||||
|
return new Response(JSON.stringify(responseBody), {
|
||||||
|
status: 200,
|
||||||
|
headers: { "content-type": "application/json" },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
fetchFn,
|
||||||
|
getAuthHeader: () => seenAuth,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createRequestCaptureJsonFetch(responseBody: unknown) {
|
||||||
|
let seenUrl: string | null = null;
|
||||||
|
let seenInit: RequestInit | undefined;
|
||||||
|
const fetchFn = withFetchPreconnect(async (input: RequestInfo | URL, init?: RequestInit) => {
|
||||||
|
seenUrl = resolveRequestUrl(input);
|
||||||
|
seenInit = init;
|
||||||
|
return new Response(JSON.stringify(responseBody), {
|
||||||
|
status: 200,
|
||||||
|
headers: { "content-type": "application/json" },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
fetchFn,
|
||||||
|
getRequest: () => ({ url: seenUrl, init: seenInit }),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,25 +1,17 @@
|
|||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import { withFetchPreconnect } from "../../../test-utils/fetch-mock.js";
|
import {
|
||||||
import { installPinnedHostnameTestHooks, resolveRequestUrl } from "../audio.test-helpers.js";
|
createAuthCaptureJsonFetch,
|
||||||
|
createRequestCaptureJsonFetch,
|
||||||
|
installPinnedHostnameTestHooks,
|
||||||
|
} from "../audio.test-helpers.js";
|
||||||
import { transcribeDeepgramAudio } from "./audio.js";
|
import { transcribeDeepgramAudio } from "./audio.js";
|
||||||
|
|
||||||
installPinnedHostnameTestHooks();
|
installPinnedHostnameTestHooks();
|
||||||
|
|
||||||
describe("transcribeDeepgramAudio", () => {
|
describe("transcribeDeepgramAudio", () => {
|
||||||
it("respects lowercase authorization header overrides", async () => {
|
it("respects lowercase authorization header overrides", async () => {
|
||||||
let seenAuth: string | null = null;
|
const { fetchFn, getAuthHeader } = createAuthCaptureJsonFetch({
|
||||||
const fetchFn = withFetchPreconnect(async (_input: RequestInfo | URL, init?: RequestInit) => {
|
results: { channels: [{ alternatives: [{ transcript: "ok" }] }] },
|
||||||
const headers = new Headers(init?.headers);
|
|
||||||
seenAuth = headers.get("authorization");
|
|
||||||
return new Response(
|
|
||||||
JSON.stringify({
|
|
||||||
results: { channels: [{ alternatives: [{ transcript: "ok" }] }] },
|
|
||||||
}),
|
|
||||||
{
|
|
||||||
status: 200,
|
|
||||||
headers: { "content-type": "application/json" },
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = await transcribeDeepgramAudio({
|
const result = await transcribeDeepgramAudio({
|
||||||
@@ -31,25 +23,13 @@ describe("transcribeDeepgramAudio", () => {
|
|||||||
fetchFn,
|
fetchFn,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(seenAuth).toBe("Token override");
|
expect(getAuthHeader()).toBe("Token override");
|
||||||
expect(result.text).toBe("ok");
|
expect(result.text).toBe("ok");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("builds the expected request payload", async () => {
|
it("builds the expected request payload", async () => {
|
||||||
let seenUrl: string | null = null;
|
const { fetchFn, getRequest } = createRequestCaptureJsonFetch({
|
||||||
let seenInit: RequestInit | undefined;
|
results: { channels: [{ alternatives: [{ transcript: "hello" }] }] },
|
||||||
const fetchFn = withFetchPreconnect(async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
||||||
seenUrl = resolveRequestUrl(input);
|
|
||||||
seenInit = init;
|
|
||||||
return new Response(
|
|
||||||
JSON.stringify({
|
|
||||||
results: { channels: [{ alternatives: [{ transcript: "hello" }] }] },
|
|
||||||
}),
|
|
||||||
{
|
|
||||||
status: 200,
|
|
||||||
headers: { "content-type": "application/json" },
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = await transcribeDeepgramAudio({
|
const result = await transcribeDeepgramAudio({
|
||||||
@@ -68,6 +48,7 @@ describe("transcribeDeepgramAudio", () => {
|
|||||||
},
|
},
|
||||||
fetchFn,
|
fetchFn,
|
||||||
});
|
});
|
||||||
|
const { url: seenUrl, init: seenInit } = getRequest();
|
||||||
|
|
||||||
expect(result.model).toBe("nova-3");
|
expect(result.model).toBe("nova-3");
|
||||||
expect(result.text).toBe("hello");
|
expect(result.text).toBe("hello");
|
||||||
@@ -83,4 +64,20 @@ describe("transcribeDeepgramAudio", () => {
|
|||||||
expect(headers.get("content-type")).toBe("audio/wav");
|
expect(headers.get("content-type")).toBe("audio/wav");
|
||||||
expect(seenInit?.body).toBeInstanceOf(Uint8Array);
|
expect(seenInit?.body).toBeInstanceOf(Uint8Array);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("throws when the provider response omits transcript", async () => {
|
||||||
|
const { fetchFn } = createRequestCaptureJsonFetch({
|
||||||
|
results: { channels: [{ alternatives: [{}] }] },
|
||||||
|
});
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
transcribeDeepgramAudio({
|
||||||
|
buffer: Buffer.from("audio-bytes"),
|
||||||
|
fileName: "voice.wav",
|
||||||
|
apiKey: "test-key",
|
||||||
|
timeoutMs: 1234,
|
||||||
|
fetchFn,
|
||||||
|
}),
|
||||||
|
).rejects.toThrow("Audio transcription response missing transcript");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,21 +1,16 @@
|
|||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import { withFetchPreconnect } from "../../../test-utils/fetch-mock.js";
|
import {
|
||||||
import { installPinnedHostnameTestHooks, resolveRequestUrl } from "../audio.test-helpers.js";
|
createAuthCaptureJsonFetch,
|
||||||
|
createRequestCaptureJsonFetch,
|
||||||
|
installPinnedHostnameTestHooks,
|
||||||
|
} from "../audio.test-helpers.js";
|
||||||
import { transcribeOpenAiCompatibleAudio } from "./audio.js";
|
import { transcribeOpenAiCompatibleAudio } from "./audio.js";
|
||||||
|
|
||||||
installPinnedHostnameTestHooks();
|
installPinnedHostnameTestHooks();
|
||||||
|
|
||||||
describe("transcribeOpenAiCompatibleAudio", () => {
|
describe("transcribeOpenAiCompatibleAudio", () => {
|
||||||
it("respects lowercase authorization header overrides", async () => {
|
it("respects lowercase authorization header overrides", async () => {
|
||||||
let seenAuth: string | null = null;
|
const { fetchFn, getAuthHeader } = createAuthCaptureJsonFetch({ text: "ok" });
|
||||||
const fetchFn = withFetchPreconnect(async (_input: RequestInfo | URL, init?: RequestInit) => {
|
|
||||||
const headers = new Headers(init?.headers);
|
|
||||||
seenAuth = headers.get("authorization");
|
|
||||||
return new Response(JSON.stringify({ text: "ok" }), {
|
|
||||||
status: 200,
|
|
||||||
headers: { "content-type": "application/json" },
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = await transcribeOpenAiCompatibleAudio({
|
const result = await transcribeOpenAiCompatibleAudio({
|
||||||
buffer: Buffer.from("audio"),
|
buffer: Buffer.from("audio"),
|
||||||
@@ -26,21 +21,12 @@ describe("transcribeOpenAiCompatibleAudio", () => {
|
|||||||
fetchFn,
|
fetchFn,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(seenAuth).toBe("Bearer override");
|
expect(getAuthHeader()).toBe("Bearer override");
|
||||||
expect(result.text).toBe("ok");
|
expect(result.text).toBe("ok");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("builds the expected request payload", async () => {
|
it("builds the expected request payload", async () => {
|
||||||
let seenUrl: string | null = null;
|
const { fetchFn, getRequest } = createRequestCaptureJsonFetch({ text: "hello" });
|
||||||
let seenInit: RequestInit | undefined;
|
|
||||||
const fetchFn = withFetchPreconnect(async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
||||||
seenUrl = resolveRequestUrl(input);
|
|
||||||
seenInit = init;
|
|
||||||
return new Response(JSON.stringify({ text: "hello" }), {
|
|
||||||
status: 200,
|
|
||||||
headers: { "content-type": "application/json" },
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = await transcribeOpenAiCompatibleAudio({
|
const result = await transcribeOpenAiCompatibleAudio({
|
||||||
buffer: Buffer.from("audio-bytes"),
|
buffer: Buffer.from("audio-bytes"),
|
||||||
@@ -55,6 +41,7 @@ describe("transcribeOpenAiCompatibleAudio", () => {
|
|||||||
headers: { "X-Custom": "1" },
|
headers: { "X-Custom": "1" },
|
||||||
fetchFn,
|
fetchFn,
|
||||||
});
|
});
|
||||||
|
const { url: seenUrl, init: seenInit } = getRequest();
|
||||||
|
|
||||||
expect(result.model).toBe("gpt-4o-mini-transcribe");
|
expect(result.model).toBe("gpt-4o-mini-transcribe");
|
||||||
expect(result.text).toBe("hello");
|
expect(result.text).toBe("hello");
|
||||||
@@ -80,4 +67,18 @@ describe("transcribeOpenAiCompatibleAudio", () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("throws when the provider response omits text", async () => {
|
||||||
|
const { fetchFn } = createRequestCaptureJsonFetch({});
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
transcribeOpenAiCompatibleAudio({
|
||||||
|
buffer: Buffer.from("audio-bytes"),
|
||||||
|
fileName: "voice.wav",
|
||||||
|
apiKey: "test-key",
|
||||||
|
timeoutMs: 1234,
|
||||||
|
fetchFn,
|
||||||
|
}),
|
||||||
|
).rejects.toThrow("Audio transcription response missing text");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user