From 98ca0858f4e98faba34050507ebbad3b82dcee6e Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Tue, 12 May 2026 21:47:10 +0100 Subject: [PATCH] test: dedupe web guarded fetch mock reads --- src/agents/tools/web-guarded-fetch.test.ts | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/agents/tools/web-guarded-fetch.test.ts b/src/agents/tools/web-guarded-fetch.test.ts index a5c035ecf42..6027f46dafa 100644 --- a/src/agents/tools/web-guarded-fetch.test.ts +++ b/src/agents/tools/web-guarded-fetch.test.ts @@ -25,6 +25,14 @@ vi.mock("../../infra/net/fetch-guard.js", () => { }; }); +function firstFetchCall(): Record { + const call = vi.mocked(fetchWithSsrFGuard).mock.calls[0]?.[0]; + if (!call || typeof call !== "object") { + throw new Error("Expected guarded fetch call"); + } + return call as Record; +} + describe("web-guarded-fetch", () => { afterEach(() => { vi.clearAllMocks(); @@ -39,7 +47,7 @@ describe("web-guarded-fetch", () => { await withTrustedWebToolsEndpoint({ url: "https://example.com" }, async () => undefined); - const call = vi.mocked(fetchWithSsrFGuard).mock.calls.at(0)?.[0]; + const call = firstFetchCall(); expect(call?.url).toBe("https://example.com"); expect(call?.policy).toEqual({ allowRfc2544BenchmarkRange: true, @@ -58,11 +66,12 @@ describe("web-guarded-fetch", () => { await withSelfHostedWebToolsEndpoint({ url: "http://127.0.0.1:8080" }, async () => undefined); - const call = vi.mocked(fetchWithSsrFGuard).mock.calls.at(0)?.[0]; + const call = firstFetchCall(); expect(call?.url).toBe("http://127.0.0.1:8080"); - expect(call?.policy?.dangerouslyAllowPrivateNetwork).toBe(true); - expect(call?.policy?.allowRfc2544BenchmarkRange).toBe(true); - expect(call?.policy?.allowIpv6UniqueLocalRange).toBe(true); + const policy = call.policy as Record | undefined; + expect(policy?.dangerouslyAllowPrivateNetwork).toBe(true); + expect(policy?.allowRfc2544BenchmarkRange).toBe(true); + expect(policy?.allowIpv6UniqueLocalRange).toBe(true); expect(call?.mode).toBe(GUARDED_FETCH_MODE.TRUSTED_ENV_PROXY); }); @@ -75,7 +84,7 @@ describe("web-guarded-fetch", () => { await withStrictWebToolsEndpoint({ url: "https://example.com" }, async () => undefined); - const call = vi.mocked(fetchWithSsrFGuard).mock.calls.at(0)?.[0]; + const call = firstFetchCall(); expect(call?.url).toBe("https://example.com"); expect(call?.policy).toBeUndefined(); expect(call?.mode).toBe(GUARDED_FETCH_MODE.STRICT);