web-search.test.ts

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