web-search.test.ts

 1import { describe, test, expect, mock } from "bun:test";
 2import { FetchError, ToolInputError } from "../src/util/errors.js";
 3
 4// Mock kagi-ken so the search function throws, exercising the FetchError wrapping
 5mock.module("kagi-ken", () => ({
 6  search: async () => {
 7    throw new Error("Unauthorized");
 8  },
 9}));
10
11import { createWebSearchTool } from "../src/agent/tools/web-search.js";
12
13describe("web_search error handling (issue #11)", () => {
14  test("missing session token throws ToolInputError", async () => {
15    const tool = createWebSearchTool("");
16    await expect(tool.execute("id", { query: "test" })).rejects.toThrow(ToolInputError);
17  });
18
19  test("search API failure is wrapped as FetchError", async () => {
20    const tool = createWebSearchTool("invalid-token-xxx");
21    await expect(tool.execute("id", { query: "test query" })).rejects.toThrow(FetchError);
22  });
23});