diff --git a/src/media-understanding/providers/audio.test-helpers.ts b/src/media-understanding/providers/audio.test-helpers.ts new file mode 100644 index 000000000..190465a45 --- /dev/null +++ b/src/media-understanding/providers/audio.test-helpers.ts @@ -0,0 +1,42 @@ +import type { MockInstance } from "vitest"; +import { afterEach, beforeEach, vi } from "vitest"; +import * as ssrf from "../../infra/net/ssrf.js"; + +export function resolveRequestUrl(input: RequestInfo | URL): string { + if (typeof input === "string") { + return input; + } + if (input instanceof URL) { + return input.toString(); + } + return input.url; +} + +export function installPinnedHostnameTestHooks(): void { + const resolvePinnedHostname = ssrf.resolvePinnedHostname; + const resolvePinnedHostnameWithPolicy = ssrf.resolvePinnedHostnameWithPolicy; + + const lookupMock = vi.fn(); + let resolvePinnedHostnameSpy: MockInstance | null = null; + let resolvePinnedHostnameWithPolicySpy: MockInstance | null = null; + + beforeEach(() => { + lookupMock.mockResolvedValue([{ address: "93.184.216.34", family: 4 }]); + resolvePinnedHostnameSpy = vi + .spyOn(ssrf, "resolvePinnedHostname") + .mockImplementation((hostname) => resolvePinnedHostname(hostname, lookupMock)); + resolvePinnedHostnameWithPolicySpy = vi + .spyOn(ssrf, "resolvePinnedHostnameWithPolicy") + .mockImplementation((hostname, params) => + resolvePinnedHostnameWithPolicy(hostname, { ...params, lookupFn: lookupMock }), + ); + }); + + afterEach(() => { + lookupMock.mockReset(); + resolvePinnedHostnameSpy?.mockRestore(); + resolvePinnedHostnameWithPolicySpy?.mockRestore(); + resolvePinnedHostnameSpy = null; + resolvePinnedHostnameWithPolicySpy = null; + }); +} diff --git a/src/media-understanding/providers/deepgram/audio.test.ts b/src/media-understanding/providers/deepgram/audio.test.ts index 5d40b9305..1ad4d6a92 100644 --- a/src/media-understanding/providers/deepgram/audio.test.ts +++ b/src/media-understanding/providers/deepgram/audio.test.ts @@ -1,44 +1,10 @@ -import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; -import * as ssrf from "../../../infra/net/ssrf.js"; +import { describe, expect, it } from "vitest"; +import { installPinnedHostnameTestHooks, resolveRequestUrl } from "../audio.test-helpers.js"; import { transcribeDeepgramAudio } from "./audio.js"; -const resolvePinnedHostname = ssrf.resolvePinnedHostname; -const resolvePinnedHostnameWithPolicy = ssrf.resolvePinnedHostnameWithPolicy; -const lookupMock = vi.fn(); -let resolvePinnedHostnameSpy: ReturnType = null; -let resolvePinnedHostnameWithPolicySpy: ReturnType = null; - -const resolveRequestUrl = (input: RequestInfo | URL) => { - if (typeof input === "string") { - return input; - } - if (input instanceof URL) { - return input.toString(); - } - return input.url; -}; +installPinnedHostnameTestHooks(); describe("transcribeDeepgramAudio", () => { - beforeEach(() => { - lookupMock.mockResolvedValue([{ address: "93.184.216.34", family: 4 }]); - resolvePinnedHostnameSpy = vi - .spyOn(ssrf, "resolvePinnedHostname") - .mockImplementation((hostname) => resolvePinnedHostname(hostname, lookupMock)); - resolvePinnedHostnameWithPolicySpy = vi - .spyOn(ssrf, "resolvePinnedHostnameWithPolicy") - .mockImplementation((hostname, params) => - resolvePinnedHostnameWithPolicy(hostname, { ...params, lookupFn: lookupMock }), - ); - }); - - afterEach(() => { - lookupMock.mockReset(); - resolvePinnedHostnameSpy?.mockRestore(); - resolvePinnedHostnameWithPolicySpy?.mockRestore(); - resolvePinnedHostnameSpy = null; - resolvePinnedHostnameWithPolicySpy = null; - }); - it("respects lowercase authorization header overrides", async () => { let seenAuth: string | null = null; const fetchFn = async (_input: RequestInfo | URL, init?: RequestInit) => { diff --git a/src/media-understanding/providers/openai/audio.test.ts b/src/media-understanding/providers/openai/audio.test.ts index 4ea7a8408..c0b986881 100644 --- a/src/media-understanding/providers/openai/audio.test.ts +++ b/src/media-understanding/providers/openai/audio.test.ts @@ -1,44 +1,10 @@ -import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; -import * as ssrf from "../../../infra/net/ssrf.js"; +import { describe, expect, it } from "vitest"; +import { installPinnedHostnameTestHooks, resolveRequestUrl } from "../audio.test-helpers.js"; import { transcribeOpenAiCompatibleAudio } from "./audio.js"; -const resolvePinnedHostname = ssrf.resolvePinnedHostname; -const resolvePinnedHostnameWithPolicy = ssrf.resolvePinnedHostnameWithPolicy; -const lookupMock = vi.fn(); -let resolvePinnedHostnameSpy: ReturnType = null; -let resolvePinnedHostnameWithPolicySpy: ReturnType = null; - -const resolveRequestUrl = (input: RequestInfo | URL) => { - if (typeof input === "string") { - return input; - } - if (input instanceof URL) { - return input.toString(); - } - return input.url; -}; +installPinnedHostnameTestHooks(); describe("transcribeOpenAiCompatibleAudio", () => { - beforeEach(() => { - lookupMock.mockResolvedValue([{ address: "93.184.216.34", family: 4 }]); - resolvePinnedHostnameSpy = vi - .spyOn(ssrf, "resolvePinnedHostname") - .mockImplementation((hostname) => resolvePinnedHostname(hostname, lookupMock)); - resolvePinnedHostnameWithPolicySpy = vi - .spyOn(ssrf, "resolvePinnedHostnameWithPolicy") - .mockImplementation((hostname, params) => - resolvePinnedHostnameWithPolicy(hostname, { ...params, lookupFn: lookupMock }), - ); - }); - - afterEach(() => { - lookupMock.mockReset(); - resolvePinnedHostnameSpy?.mockRestore(); - resolvePinnedHostnameWithPolicySpy?.mockRestore(); - resolvePinnedHostnameSpy = null; - resolvePinnedHostnameWithPolicySpy = null; - }); - it("respects lowercase authorization header overrides", async () => { let seenAuth: string | null = null; const fetchFn = async (_input: RequestInfo | URL, init?: RequestInit) => {