refactor(test): share audio provider ssrf hooks
This commit is contained in:
42
src/media-understanding/providers/audio.test-helpers.ts
Normal file
42
src/media-understanding/providers/audio.test-helpers.ts
Normal 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;
|
||||
});
|
||||
}
|
||||
@@ -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) => {
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
Reference in New Issue
Block a user