|
| 1 | +import { test, expect, beforeEach, describe } from "bun:test"; |
| 2 | +import ToolManager, { ToolNotFoundError, ToolConverter } from "./ToolManager"; |
| 3 | +import type { EasyMCPTool } from "../types"; |
| 4 | +import { createMockTool } from "./mocks"; |
| 5 | + |
| 6 | +describe("ToolManager", () => { |
| 7 | + let toolManager: ToolManager; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + toolManager = ToolManager.create(); |
| 11 | + }); |
| 12 | + |
| 13 | + test("create() should return a new instance of ToolManager", () => { |
| 14 | + expect(toolManager).toBeInstanceOf(ToolManager); |
| 15 | + }); |
| 16 | + |
| 17 | + test("add() should add a tool with a function", async () => { |
| 18 | + const mockTool = createMockTool(); |
| 19 | + const mockFn = async () => ({ result: "Mock result" }); |
| 20 | + toolManager.add(mockTool.name, mockFn, mockTool); |
| 21 | + const result = await toolManager.get(mockTool.name); |
| 22 | + expect(result).toEqual(expect.objectContaining(mockTool)); |
| 23 | + }); |
| 24 | + |
| 25 | + test("get() should return the added tool", async () => { |
| 26 | + const mockTool1 = createMockTool(); |
| 27 | + const mockTool2 = createMockTool(); |
| 28 | + const mockFn1 = async () => ({ result: "Result 1" }); |
| 29 | + const mockFn2 = async () => ({ result: "Result 2" }); |
| 30 | + |
| 31 | + toolManager.add(mockTool1.name, mockFn1, mockTool1); |
| 32 | + toolManager.add(mockTool2.name, mockFn2, mockTool2); |
| 33 | + |
| 34 | + const result1 = await toolManager.get(mockTool1.name); |
| 35 | + const result2 = await toolManager.get(mockTool2.name); |
| 36 | + expect(result1).toEqual(expect.objectContaining(mockTool1)); |
| 37 | + expect(result2).toEqual(expect.objectContaining(mockTool2)); |
| 38 | + }); |
| 39 | + |
| 40 | + test("get() should throw ToolNotFoundError for non-existent tool", async () => { |
| 41 | + expect(toolManager.get("non-existent-tool")).rejects.toThrow( |
| 42 | + ToolNotFoundError, |
| 43 | + ); |
| 44 | + }); |
| 45 | + |
| 46 | + test("list() should return all added tools", () => { |
| 47 | + const mockTools = [createMockTool(), createMockTool(), createMockTool()]; |
| 48 | + |
| 49 | + mockTools.forEach((tool) => |
| 50 | + toolManager.add(tool.name, async () => ({}), tool), |
| 51 | + ); |
| 52 | + |
| 53 | + const listedTools = toolManager.list(); |
| 54 | + expect(listedTools).toHaveLength(mockTools.length); |
| 55 | + expect(listedTools).toEqual( |
| 56 | + expect.arrayContaining( |
| 57 | + mockTools.map((tool) => expect.objectContaining(tool)), |
| 58 | + ), |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + test("list() should return an empty array when no tools are added", () => { |
| 63 | + expect(toolManager.list()).toEqual([]); |
| 64 | + }); |
| 65 | +}); |
| 66 | + |
| 67 | +describe("ToolConverter", () => { |
| 68 | + test("fromMCPTool should convert EasyMCPTool to Tool", () => { |
| 69 | + const mockTool = createMockTool(); |
| 70 | + const easyMCPTool: EasyMCPTool = { |
| 71 | + ...mockTool, |
| 72 | + fn: async () => ({}), |
| 73 | + }; |
| 74 | + |
| 75 | + const result = ToolConverter.fromMCPTool(easyMCPTool); |
| 76 | + expect(result).toEqual(mockTool); |
| 77 | + expect(result).not.toHaveProperty("fn"); |
| 78 | + }); |
| 79 | + |
| 80 | + test("toMCPTool should convert Tool to EasyMCPTool", () => { |
| 81 | + const mockTool = createMockTool(); |
| 82 | + const mockFn = async () => ({}); |
| 83 | + |
| 84 | + const result = ToolConverter.toMCPTool(mockTool, mockFn); |
| 85 | + expect(result).toEqual({ |
| 86 | + ...mockTool, |
| 87 | + fn: mockFn, |
| 88 | + }); |
| 89 | + }); |
| 90 | +}); |
0 commit comments