chore: Fix types in tests 5/N.

This commit is contained in:
cpojer
2026-02-17 10:51:25 +09:00
parent c49234cbfb
commit b6d4f7c00e
8 changed files with 155 additions and 143 deletions

View File

@@ -2,13 +2,12 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { createWebFetchTool, createWebSearchTool } from "./web-tools.js";
function installMockFetch(payload: unknown) {
const mockFetch = vi.fn(() =>
const mockFetch = vi.fn((_input?: unknown, _init?: unknown) =>
Promise.resolve({
ok: true,
json: () => Promise.resolve(payload),
} as Response),
);
// @ts-expect-error mock fetch
global.fetch = mockFetch;
return mockFetch;
}
@@ -55,7 +54,7 @@ async function executePerplexitySearch(
const mockFetch = installPerplexitySuccessFetch();
const tool = createPerplexitySearchTool(options?.perplexityConfig);
await tool?.execute?.(
1,
"call-1",
options?.freshness ? { query, freshness: options.freshness } : { query },
);
return mockFetch;
@@ -90,7 +89,6 @@ describe("web_search country and language parameters", () => {
afterEach(() => {
vi.unstubAllEnvs();
// @ts-expect-error global fetch cleanup
global.fetch = priorFetch;
});
@@ -105,7 +103,7 @@ describe("web_search country and language parameters", () => {
const mockFetch = installMockFetch({ web: { results: [] } });
const tool = createWebSearchTool({ config: undefined, sandboxed: true });
expect(tool).not.toBeNull();
await tool?.execute?.(1, { query: "test", ...params });
await tool?.execute?.("call-1", { query: "test", ...params });
expect(mockFetch).toHaveBeenCalled();
return new URL(mockFetch.mock.calls[0][0] as string);
}
@@ -123,7 +121,7 @@ describe("web_search country and language parameters", () => {
it("rejects invalid freshness values", async () => {
const mockFetch = installMockFetch({ web: { results: [] } });
const tool = createWebSearchTool({ config: undefined, sandboxed: true });
const result = await tool?.execute?.(1, { query: "test", freshness: "yesterday" });
const result = await tool?.execute?.("call-1", { query: "test", freshness: "yesterday" });
expect(mockFetch).not.toHaveBeenCalled();
expect(result?.details).toMatchObject({ error: "invalid_freshness" });
@@ -135,7 +133,6 @@ describe("web_search perplexity baseUrl defaults", () => {
afterEach(() => {
vi.unstubAllEnvs();
// @ts-expect-error global fetch cleanup
global.fetch = priorFetch;
});
@@ -213,7 +210,6 @@ describe("web_search external content wrapping", () => {
afterEach(() => {
vi.unstubAllEnvs();
// @ts-expect-error global fetch cleanup
global.fetch = priorFetch;
});
@@ -236,11 +232,10 @@ describe("web_search external content wrapping", () => {
}),
} as Response),
);
// @ts-expect-error mock fetch
global.fetch = mockFetch;
const tool = createWebSearchTool({ config: undefined, sandboxed: true });
const result = await tool?.execute?.(1, { query: "test" });
const result = await tool?.execute?.("call-1", { query: "test" });
const details = result?.details as {
externalContent?: { untrusted?: boolean; source?: string; wrapped?: boolean };
results?: Array<{ description?: string }>;
@@ -275,11 +270,10 @@ describe("web_search external content wrapping", () => {
}),
} as Response),
);
// @ts-expect-error mock fetch
global.fetch = mockFetch;
const tool = createWebSearchTool({ config: undefined, sandboxed: true });
const result = await tool?.execute?.(1, { query: "unique-test-url-not-wrapped" });
const result = await tool?.execute?.("call-1", { query: "unique-test-url-not-wrapped" });
const details = result?.details as { results?: Array<{ url?: string }> };
// URL should NOT be wrapped - kept raw for tool chaining (e.g., web_fetch)
@@ -306,11 +300,10 @@ describe("web_search external content wrapping", () => {
}),
} as Response),
);
// @ts-expect-error mock fetch
global.fetch = mockFetch;
const tool = createWebSearchTool({ config: undefined, sandboxed: true });
const result = await tool?.execute?.(1, { query: "unique-test-site-name-wrapping" });
const result = await tool?.execute?.("call-1", { query: "unique-test-site-name-wrapping" });
const details = result?.details as { results?: Array<{ siteName?: string }> };
expect(details.results?.[0]?.siteName).toBe("example.com");
@@ -337,11 +330,12 @@ describe("web_search external content wrapping", () => {
}),
} as Response),
);
// @ts-expect-error mock fetch
global.fetch = mockFetch;
const tool = createWebSearchTool({ config: undefined, sandboxed: true });
const result = await tool?.execute?.(1, { query: "unique-test-brave-published-wrapping" });
const result = await tool?.execute?.("call-1", {
query: "unique-test-brave-published-wrapping",
});
const details = result?.details as { results?: Array<{ published?: string }> };
expect(details.results?.[0]?.published).toBe("2 days ago");
@@ -360,14 +354,13 @@ describe("web_search external content wrapping", () => {
}),
} as Response),
);
// @ts-expect-error mock fetch
global.fetch = mockFetch;
const tool = createWebSearchTool({
config: { tools: { web: { search: { provider: "perplexity" } } } },
sandboxed: true,
});
const result = await tool?.execute?.(1, { query: "test" });
const result = await tool?.execute?.("call-1", { query: "test" });
const details = result?.details as { content?: string };
expect(details.content).toContain("<<<EXTERNAL_UNTRUSTED_CONTENT>>>");
@@ -377,7 +370,7 @@ describe("web_search external content wrapping", () => {
it("does not wrap Perplexity citations (raw for tool chaining)", async () => {
vi.stubEnv("PERPLEXITY_API_KEY", "pplx-test");
const citation = "https://example.com/some-article";
const mockFetch = vi.fn(() =>
const mockFetch = vi.fn((_input?: unknown, _init?: unknown) =>
Promise.resolve({
ok: true,
json: () =>
@@ -387,14 +380,15 @@ describe("web_search external content wrapping", () => {
}),
} as Response),
);
// @ts-expect-error mock fetch
global.fetch = mockFetch;
const tool = createWebSearchTool({
config: { tools: { web: { search: { provider: "perplexity" } } } },
sandboxed: true,
});
const result = await tool?.execute?.(1, { query: "unique-test-perplexity-citations-raw" });
const result = await tool?.execute?.("call-1", {
query: "unique-test-perplexity-citations-raw",
});
const details = result?.details as { citations?: string[] };
// Citations are URLs - should NOT be wrapped for tool chaining