mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 05:01:23 +00:00
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 { describe, expect, it } from "vitest";
|
||||||
import * as ssrf from "../../../infra/net/ssrf.js";
|
import { installPinnedHostnameTestHooks, resolveRequestUrl } from "../audio.test-helpers.js";
|
||||||
import { transcribeDeepgramAudio } from "./audio.js";
|
import { transcribeDeepgramAudio } from "./audio.js";
|
||||||
|
|
||||||
const resolvePinnedHostname = ssrf.resolvePinnedHostname;
|
installPinnedHostnameTestHooks();
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
describe("transcribeDeepgramAudio", () => {
|
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 () => {
|
it("respects lowercase authorization header overrides", async () => {
|
||||||
let seenAuth: string | null = null;
|
let seenAuth: string | null = null;
|
||||||
const fetchFn = async (_input: RequestInfo | URL, init?: RequestInit) => {
|
const fetchFn = async (_input: RequestInfo | URL, init?: RequestInit) => {
|
||||||
|
|||||||
@@ -1,44 +1,10 @@
|
|||||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import * as ssrf from "../../../infra/net/ssrf.js";
|
import { installPinnedHostnameTestHooks, resolveRequestUrl } from "../audio.test-helpers.js";
|
||||||
import { transcribeOpenAiCompatibleAudio } from "./audio.js";
|
import { transcribeOpenAiCompatibleAudio } from "./audio.js";
|
||||||
|
|
||||||
const resolvePinnedHostname = ssrf.resolvePinnedHostname;
|
installPinnedHostnameTestHooks();
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
describe("transcribeOpenAiCompatibleAudio", () => {
|
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 () => {
|
it("respects lowercase authorization header overrides", async () => {
|
||||||
let seenAuth: string | null = null;
|
let seenAuth: string | null = null;
|
||||||
const fetchFn = async (_input: RequestInfo | URL, init?: RequestInit) => {
|
const fetchFn = async (_input: RequestInfo | URL, init?: RequestInit) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user