|
| 1 | +import { z } from "zod"; |
| 2 | +import { getMastraTools } from "./getMastraTools"; |
| 3 | +import { AgentKit } from "@coinbase/agentkit"; |
| 4 | + |
| 5 | +// Mock AgentKit before importing - this prevents loading ES-only dependencies |
| 6 | +jest.mock("@coinbase/agentkit", () => ({ |
| 7 | + AgentKit: { |
| 8 | + from: jest.fn(), |
| 9 | + }, |
| 10 | +})); |
| 11 | + |
| 12 | +// Mock @mastra/core/tools |
| 13 | +jest.mock("@mastra/core/tools", () => ({ |
| 14 | + createTool: jest.fn((config: Record<string, unknown>) => ({ |
| 15 | + id: config.id, |
| 16 | + description: config.description, |
| 17 | + inputSchema: config.inputSchema, |
| 18 | + execute: config.execute, |
| 19 | + })), |
| 20 | +})); |
| 21 | + |
| 22 | +// Define mock action after imports |
| 23 | +const mockAction = { |
| 24 | + name: "testAction", |
| 25 | + description: "A test action", |
| 26 | + schema: z.object({ test: z.string() }), |
| 27 | + invoke: jest.fn(async (arg: { test: string }) => `Invoked with ${arg.test}`), |
| 28 | +}; |
| 29 | + |
| 30 | +// Configure the mock |
| 31 | +(AgentKit.from as jest.Mock).mockImplementation(() => ({ |
| 32 | + getActions: jest.fn(() => [mockAction]), |
| 33 | +})); |
| 34 | + |
| 35 | +describe("getMastraTools", () => { |
| 36 | + it("should return a record of tools with correct properties", async () => { |
| 37 | + const mockAgentKit = await AgentKit.from({}); |
| 38 | + const tools = getMastraTools(mockAgentKit); |
| 39 | + |
| 40 | + expect(tools).toHaveProperty("testAction"); |
| 41 | + const tool = tools.testAction; |
| 42 | + |
| 43 | + expect(tool.id).toBe(mockAction.name); |
| 44 | + expect(tool.description).toBe(mockAction.description); |
| 45 | + expect(tool.inputSchema).toBe(mockAction.schema); |
| 46 | + |
| 47 | + // Test execution |
| 48 | + const result = await tool.execute!({ test: "data" }); |
| 49 | + expect(result).toBe("Invoked with data"); |
| 50 | + }); |
| 51 | + |
| 52 | + it("should handle multiple actions", async () => { |
| 53 | + const secondAction = { |
| 54 | + name: "secondAction", |
| 55 | + description: "A second test action", |
| 56 | + schema: z.object({ value: z.number() }), |
| 57 | + invoke: jest.fn(async (arg: { value: number }) => `Value is ${arg.value}`), |
| 58 | + }; |
| 59 | + |
| 60 | + (AgentKit.from as jest.Mock).mockImplementation(() => ({ |
| 61 | + getActions: jest.fn(() => [mockAction, secondAction]), |
| 62 | + })); |
| 63 | + |
| 64 | + const mockAgentKit = await AgentKit.from({}); |
| 65 | + const tools = getMastraTools(mockAgentKit); |
| 66 | + |
| 67 | + expect(Object.keys(tools)).toHaveLength(2); |
| 68 | + expect(tools).toHaveProperty("testAction"); |
| 69 | + expect(tools).toHaveProperty("secondAction"); |
| 70 | + |
| 71 | + const result = await tools.secondAction.execute!({ value: 42 }); |
| 72 | + expect(result).toBe("Value is 42"); |
| 73 | + }); |
| 74 | + |
| 75 | + it("should return an empty record when no actions are available", async () => { |
| 76 | + (AgentKit.from as jest.Mock).mockImplementation(() => ({ |
| 77 | + getActions: jest.fn(() => []), |
| 78 | + })); |
| 79 | + |
| 80 | + const mockAgentKit = await AgentKit.from({}); |
| 81 | + const tools = getMastraTools(mockAgentKit); |
| 82 | + |
| 83 | + expect(Object.keys(tools)).toHaveLength(0); |
| 84 | + }); |
| 85 | +}); |
0 commit comments