// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
//
// SPDX-License-Identifier: GPL-3.0-or-later

import { describe, test, expect, mock } from "bun:test";
import { FetchError, ToolInputError } from "../src/util/errors.js";

// Mock kagi-ken so the search function throws, exercising the FetchError wrapping
mock.module("kagi-ken", () => ({
  search: async () => {
    throw new Error("Unauthorized");
  },
}));

import { createWebSearchTool } from "../src/agent/tools/web-search.js";

describe("web_search error handling (issue #11)", () => {
  test("missing session token throws ToolInputError", async () => {
    const tool = createWebSearchTool("");
    await expect(tool.execute("id", { query: "test" })).rejects.toThrow(ToolInputError);
  });

  test("search API failure is wrapped as FetchError", async () => {
    const tool = createWebSearchTool("invalid-token-xxx");
    await expect(tool.execute("id", { query: "test query" })).rejects.toThrow(FetchError);
  });
});
