"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Mcp_1 = require("../../src/services/Mcp");
const Tools_1 = require("../../src/services/Tools");
async function simulateReloadConfig(Mcp, Tools, mcpServer, getConfig, toolsToUseRef) {
    const freshConfig = await getConfig();
    await Mcp.closeAll();
    await Mcp.connectToConfigured(Tools);
    const allowedToolNames = freshConfig.worker?.allowedTools ?? Tools.getToolNames();
    toolsToUseRef.value = Tools.getToolsByNames(allowedToolNames);
    mcpServer.withTools(toolsToUseRef.value);
}
describe("Worker reloadConfig handler", () => {
    let Mcp;
    let Tools;
    let mcpServer;
    let toolsToUseRef;
    beforeEach(() => {
        Mcp = new Mcp_1.McpService();
        Tools = new Tools_1.ToolsService();
        mcpServer = { withTools: jest.fn() };
        toolsToUseRef = { value: [] };
        jest.spyOn(Mcp, "closeAll").mockResolvedValue(undefined);
        jest.spyOn(Mcp, "connectToConfigured").mockResolvedValue(undefined);
    });
    afterEach(() => {
        jest.restoreAllMocks();
    });
    it("should call closeAll() to tear down existing MCP connections", async () => {
        const getConfig = jest
            .fn()
            .mockResolvedValue({ worker: { allowedTools: [] } });
        await simulateReloadConfig(Mcp, Tools, mcpServer, getConfig, toolsToUseRef);
        expect(Mcp.closeAll).toHaveBeenCalledTimes(1);
    });
    it("should call connectToConfigured() to reconnect MCPs from fresh config", async () => {
        const getConfig = jest
            .fn()
            .mockResolvedValue({ worker: { allowedTools: [] } });
        await simulateReloadConfig(Mcp, Tools, mcpServer, getConfig, toolsToUseRef);
        expect(Mcp.connectToConfigured).toHaveBeenCalledWith(Tools);
    });
    it("should rebuild the tool list from allowedTools in fresh config", async () => {
        const toolsByNamesSpy = jest
            .spyOn(Tools, "getToolsByNames")
            .mockReturnValue([{ function: { name: "execCommand" } }]);
        const getConfig = jest
            .fn()
            .mockResolvedValue({ worker: { allowedTools: ["execCommand"] } });
        await simulateReloadConfig(Mcp, Tools, mcpServer, getConfig, toolsToUseRef);
        expect(toolsByNamesSpy).toHaveBeenCalledWith(["execCommand"]);
        expect(toolsToUseRef.value).toHaveLength(1);
    });
    it("should fall back to all tool names when allowedTools is not set", async () => {
        const allNames = ["execCommand", "readFile", "writeFileChunk"];
        jest.spyOn(Tools, "getToolNames").mockReturnValue(allNames);
        const toolsByNamesSpy = jest
            .spyOn(Tools, "getToolsByNames")
            .mockReturnValue([]);
        const getConfig = jest.fn().mockResolvedValue({});
        await simulateReloadConfig(Mcp, Tools, mcpServer, getConfig, toolsToUseRef);
        expect(toolsByNamesSpy).toHaveBeenCalledWith(allNames);
    });
    it("should call mcpServer.withTools() with the rebuilt tool list", async () => {
        const fakeTools = [{ function: { name: "readFile" } }];
        jest.spyOn(Tools, "getToolsByNames").mockReturnValue(fakeTools);
        const getConfig = jest
            .fn()
            .mockResolvedValue({ worker: { allowedTools: ["readFile"] } });
        await simulateReloadConfig(Mcp, Tools, mcpServer, getConfig, toolsToUseRef);
        expect(mcpServer.withTools).toHaveBeenCalledWith(fakeTools);
    });
    it("should re-read the config on every reload (not use stale config)", async () => {
        const getConfig = jest
            .fn()
            .mockResolvedValueOnce({ worker: { allowedTools: ["execCommand"] } })
            .mockResolvedValueOnce({ worker: { allowedTools: ["readFile", "writeFileChunk"] } });
        jest.spyOn(Tools, "getToolsByNames").mockReturnValue([]);
        await simulateReloadConfig(Mcp, Tools, mcpServer, getConfig, toolsToUseRef);
        await simulateReloadConfig(Mcp, Tools, mcpServer, getConfig, toolsToUseRef);
        expect(getConfig).toHaveBeenCalledTimes(2);
        expect(Mcp.closeAll).toHaveBeenCalledTimes(2);
        expect(Mcp.connectToConfigured).toHaveBeenCalledTimes(2);
    });
});
//# sourceMappingURL=WorkerReloadConfig.test.js.map