test(media): dedupe audio provider request assertions

This commit is contained in:
Peter Steinberger
2026-02-18 13:12:35 +00:00
parent e1b491d961
commit eb775ff24b
3 changed files with 84 additions and 52 deletions

View File

@@ -1,21 +1,16 @@
import { describe, expect, it } from "vitest";
import { withFetchPreconnect } from "../../../test-utils/fetch-mock.js";
import { installPinnedHostnameTestHooks, resolveRequestUrl } from "../audio.test-helpers.js";
import {
createAuthCaptureJsonFetch,
createRequestCaptureJsonFetch,
installPinnedHostnameTestHooks,
} from "../audio.test-helpers.js";
import { transcribeOpenAiCompatibleAudio } from "./audio.js";
installPinnedHostnameTestHooks();
describe("transcribeOpenAiCompatibleAudio", () => {
it("respects lowercase authorization header overrides", async () => {
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({ text: "ok" }), {
status: 200,
headers: { "content-type": "application/json" },
});
});
const { fetchFn, getAuthHeader } = createAuthCaptureJsonFetch({ text: "ok" });
const result = await transcribeOpenAiCompatibleAudio({
buffer: Buffer.from("audio"),
@@ -26,21 +21,12 @@ describe("transcribeOpenAiCompatibleAudio", () => {
fetchFn,
});
expect(seenAuth).toBe("Bearer override");
expect(getAuthHeader()).toBe("Bearer override");
expect(result.text).toBe("ok");
});
it("builds the expected request payload", async () => {
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({ text: "hello" }), {
status: 200,
headers: { "content-type": "application/json" },
});
});
const { fetchFn, getRequest } = createRequestCaptureJsonFetch({ text: "hello" });
const result = await transcribeOpenAiCompatibleAudio({
buffer: Buffer.from("audio-bytes"),
@@ -55,6 +41,7 @@ describe("transcribeOpenAiCompatibleAudio", () => {
headers: { "X-Custom": "1" },
fetchFn,
});
const { url: seenUrl, init: seenInit } = getRequest();
expect(result.model).toBe("gpt-4o-mini-transcribe");
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");
});
});