refactor(test): share audio provider ssrf hooks

This commit is contained in:
Peter Steinberger
2026-02-15 14:33:30 +00:00
parent 85b267aae9
commit 893d2fb862
3 changed files with 48 additions and 74 deletions

View File

@@ -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;
});
}

View File

@@ -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<typeof vi.spyOn> = null;
let resolvePinnedHostnameWithPolicySpy: ReturnType<typeof vi.spyOn> = 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) => {

View File

@@ -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<typeof vi.spyOn> = null;
let resolvePinnedHostnameWithPolicySpy: ReturnType<typeof vi.spyOn> = 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) => {