refactor: share memory ssrf test helper

This commit is contained in:
Peter Steinberger 2026-03-13 19:17:49 +00:00
parent d904f37f1c
commit e6a26e82ca
3 changed files with 16 additions and 26 deletions

View File

@ -1,8 +1,8 @@
import { afterEach, describe, expect, it, vi } from "vitest"; import { afterEach, describe, expect, it, vi } from "vitest";
import * as authModule from "../agents/model-auth.js"; import * as authModule from "../agents/model-auth.js";
import * as ssrf from "../infra/net/ssrf.js";
import { type FetchMock, withFetchPreconnect } from "../test-utils/fetch-mock.js"; import { type FetchMock, withFetchPreconnect } from "../test-utils/fetch-mock.js";
import { createVoyageEmbeddingProvider, normalizeVoyageModel } from "./embeddings-voyage.js"; import { createVoyageEmbeddingProvider, normalizeVoyageModel } from "./embeddings-voyage.js";
import { mockPublicPinnedHostname } from "./test-helpers/ssrf.js";
vi.mock("../agents/model-auth.js", async () => { vi.mock("../agents/model-auth.js", async () => {
const { createModelAuthMockModule } = await import("../test-utils/model-auth-mock.js"); const { createModelAuthMockModule } = await import("../test-utils/model-auth-mock.js");
@ -28,18 +28,6 @@ function mockVoyageApiKey() {
}); });
} }
function mockPublicPinnedHostname() {
return vi.spyOn(ssrf, "resolvePinnedHostnameWithPolicy").mockImplementation(async (hostname) => {
const normalized = hostname.trim().toLowerCase().replace(/\.$/, "");
const addresses = ["93.184.216.34"];
return {
hostname: normalized,
addresses,
lookup: ssrf.createPinnedLookup({ hostname: normalized, addresses }),
};
});
}
async function createDefaultVoyageProvider( async function createDefaultVoyageProvider(
model: string, model: string,
fetchMock: ReturnType<typeof createFetchMock>, fetchMock: ReturnType<typeof createFetchMock>,

View File

@ -1,8 +1,8 @@
import { afterEach, describe, expect, it, vi } from "vitest"; import { afterEach, describe, expect, it, vi } from "vitest";
import * as authModule from "../agents/model-auth.js"; import * as authModule from "../agents/model-auth.js";
import * as ssrf from "../infra/net/ssrf.js";
import { DEFAULT_GEMINI_EMBEDDING_MODEL } from "./embeddings-gemini.js"; import { DEFAULT_GEMINI_EMBEDDING_MODEL } from "./embeddings-gemini.js";
import { createEmbeddingProvider, DEFAULT_LOCAL_MODEL } from "./embeddings.js"; import { createEmbeddingProvider, DEFAULT_LOCAL_MODEL } from "./embeddings.js";
import { mockPublicPinnedHostname } from "./test-helpers/ssrf.js";
vi.mock("../agents/model-auth.js", async () => { vi.mock("../agents/model-auth.js", async () => {
const { createModelAuthMockModule } = await import("../test-utils/model-auth-mock.js"); const { createModelAuthMockModule } = await import("../test-utils/model-auth-mock.js");
@ -33,18 +33,6 @@ function readFirstFetchRequest(fetchMock: { mock: { calls: unknown[][] } }) {
return { url, init: init as RequestInit | undefined }; return { url, init: init as RequestInit | undefined };
} }
function mockPublicPinnedHostname() {
return vi.spyOn(ssrf, "resolvePinnedHostnameWithPolicy").mockImplementation(async (hostname) => {
const normalized = hostname.trim().toLowerCase().replace(/\.$/, "");
const addresses = ["93.184.216.34"];
return {
hostname: normalized,
addresses,
lookup: ssrf.createPinnedLookup({ hostname: normalized, addresses }),
};
});
}
afterEach(() => { afterEach(() => {
vi.resetAllMocks(); vi.resetAllMocks();
vi.unstubAllGlobals(); vi.unstubAllGlobals();

View File

@ -0,0 +1,14 @@
import { vi } from "vitest";
import * as ssrf from "../../infra/net/ssrf.js";
export function mockPublicPinnedHostname() {
return vi.spyOn(ssrf, "resolvePinnedHostnameWithPolicy").mockImplementation(async (hostname) => {
const normalized = hostname.trim().toLowerCase().replace(/\.$/, "");
const addresses = ["93.184.216.34"];
return {
hostname: normalized,
addresses,
lookup: ssrf.createPinnedLookup({ hostname: normalized, addresses }),
};
});
}