test: tighten provider transport fetch assertions

This commit is contained in:
Peter Steinberger
2026-05-09 18:20:10 +01:00
parent 8fe0956f8e
commit cc6f6bfb02

View File

@@ -43,6 +43,22 @@ vi.mock("./provider-request-config.js", () => ({
resolveProviderRequestPolicyConfig: resolveProviderRequestPolicyConfigMock,
}));
function latestGuardedFetchParams(): Record<string, unknown> {
const params = fetchWithSsrFGuardMock.mock.calls.at(-1)?.[0];
if (!params || typeof params !== "object") {
throw new Error("Expected guarded fetch call");
}
return params;
}
function latestTrustedEnvProxyParams(): Record<string, unknown> {
const params = withTrustedEnvProxyGuardedFetchModeMock.mock.calls.at(-1)?.[0];
if (!params || typeof params !== "object") {
throw new Error("Expected trusted env proxy call");
}
return params;
}
describe("buildGuardedModelFetch", () => {
beforeEach(() => {
fetchWithSsrFGuardMock.mockReset().mockResolvedValue({
@@ -81,18 +97,15 @@ describe("buildGuardedModelFetch", () => {
body: '{"input":"hello"}',
});
expect(fetchWithSsrFGuardMock).toHaveBeenCalledWith(
expect.objectContaining({
url: "https://api.openai.com/v1/responses",
capture: {
meta: {
provider: "openai",
api: "openai-responses",
model: "gpt-5.4",
},
},
}),
);
const params = latestGuardedFetchParams();
expect(params.url).toBe("https://api.openai.com/v1/responses");
expect(params.capture).toEqual({
meta: {
provider: "openai",
api: "openai-responses",
model: "gpt-5.4",
},
});
});
it("scopes fake-IP DNS exemptions to the configured provider host", async () => {
@@ -168,23 +181,18 @@ describe("buildGuardedModelFetch", () => {
expect(shouldUseEnvHttpProxyForUrlMock).toHaveBeenCalledWith(
"https://api.openai.com/v1/responses",
);
expect(withTrustedEnvProxyGuardedFetchModeMock).toHaveBeenCalledWith(
expect.objectContaining({
url: "https://api.openai.com/v1/responses",
dispatcherPolicy: undefined,
policy: {
allowRfc2544BenchmarkRange: true,
allowIpv6UniqueLocalRange: true,
hostnameAllowlist: ["api.openai.com"],
},
}),
);
expect(fetchWithSsrFGuardMock).toHaveBeenCalledWith(
expect.objectContaining({
url: "https://api.openai.com/v1/responses",
mode: "trusted_env_proxy",
}),
);
const trustedParams = latestTrustedEnvProxyParams();
expect(trustedParams.url).toBe("https://api.openai.com/v1/responses");
expect(trustedParams.dispatcherPolicy).toBeUndefined();
expect(trustedParams.policy).toEqual({
allowRfc2544BenchmarkRange: true,
allowIpv6UniqueLocalRange: true,
hostnameAllowlist: ["api.openai.com"],
});
const guardedParams = latestGuardedFetchParams();
expect(guardedParams.url).toBe("https://api.openai.com/v1/responses");
expect(guardedParams.mode).toBe("trusted_env_proxy");
});
it("keeps explicit provider dispatcher policies in strict guarded-fetch mode", async () => {
@@ -201,11 +209,7 @@ describe("buildGuardedModelFetch", () => {
await fetcher("https://api.openai.com/v1/responses", { method: "POST" });
expect(withTrustedEnvProxyGuardedFetchModeMock).not.toHaveBeenCalled();
expect(fetchWithSsrFGuardMock).toHaveBeenCalledWith(
expect.objectContaining({
dispatcherPolicy: { mode: "direct" },
}),
);
expect(latestGuardedFetchParams().dispatcherPolicy).toEqual({ mode: "direct" });
});
it("threads explicit transport timeouts into the shared guarded fetch seam", async () => {
@@ -219,11 +223,7 @@ describe("buildGuardedModelFetch", () => {
const fetcher = buildGuardedModelFetch(model, 123_456);
await fetcher("https://api.openai.com/v1/responses", { method: "POST" });
expect(fetchWithSsrFGuardMock).toHaveBeenCalledWith(
expect.objectContaining({
timeoutMs: 123_456,
}),
);
expect(latestGuardedFetchParams().timeoutMs).toBe(123_456);
});
it("threads resolved provider timeout metadata into the shared guarded fetch seam", async () => {
@@ -238,11 +238,7 @@ describe("buildGuardedModelFetch", () => {
const fetcher = buildGuardedModelFetch(model);
await fetcher("http://127.0.0.1:11434/api/chat", { method: "POST" });
expect(fetchWithSsrFGuardMock).toHaveBeenCalledWith(
expect.objectContaining({
timeoutMs: 300_000,
}),
);
expect(latestGuardedFetchParams().timeoutMs).toBe(300_000);
});
it("does not force explicit debug proxy overrides onto plain HTTP model transports", async () => {