Files
dotfiles/.pi/agent/extensions/web-search/src/tools/web-fetch.test.ts
2026-04-09 11:16:53 +01:00

63 lines
1.8 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import { createWebFetchTool } from "./web-fetch.ts";
test("web_fetch prepareArguments folds a single url into urls", () => {
const tool = createWebFetchTool({
resolveProvider: async () => {
throw new Error("not used");
},
});
assert.deepEqual(tool.prepareArguments?.({ url: "https://exa.ai/docs" }), {
url: "https://exa.ai/docs",
urls: ["https://exa.ai/docs"],
});
});
test("web_fetch defaults to text and returns formatted fetch results", async () => {
let capturedRequest: Record<string, unknown> | undefined;
const tool = createWebFetchTool({
resolveProvider: async () => ({
name: "exa-main",
type: "exa",
async search() {
throw new Error("not used");
},
async fetch(request) {
capturedRequest = request as unknown as Record<string, unknown>;
return {
providerName: "exa-main",
results: [
{
url: "https://exa.ai/docs",
title: "Docs",
text: "Body",
},
],
};
},
}),
});
const result = await tool.execute("tool-1", { urls: ["https://exa.ai/docs"] }, undefined, undefined, undefined);
assert.equal(capturedRequest?.text, true);
assert.match((result.content[0] as { text: string }).text, /Body/);
assert.equal((result.details as { results: Array<{ title: string }> }).results[0]?.title, "Docs");
});
test("web_fetch rejects malformed URLs", async () => {
const tool = createWebFetchTool({
resolveProvider: async () => {
throw new Error("should not resolve provider for invalid URLs");
},
});
await assert.rejects(
() => tool.execute("tool-1", { urls: ["not-a-url"] }, undefined, undefined, undefined),
/Invalid URL/,
);
});