UNPKG

@measey/mycoder-agent

Version:

Agent module for mycoder - an AI-powered software development assistant

87 lines 2.82 kB
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; import { z } from 'zod'; import { getMockToolContext } from '../tools/getTools.test.js'; import { executeToolCall } from './executeToolCall.js'; const toolContext = getMockToolContext(); // Mock tool for testing const mockTool = { name: 'mockTool', description: 'A mock tool for testing', parameters: z.object({ input: z.string().describe('Test input'), }), returns: z.string().describe('The processed result'), parametersJsonSchema: { type: 'object', properties: { input: { type: 'string', description: 'Test input', }, }, required: ['input'], }, returnsJsonSchema: { type: 'string', description: 'The processed result', }, execute: ({ input }) => Promise.resolve(`Processed: ${input}`), }; const errorTool = { name: 'errorTool', description: 'A tool that always fails', parameters: z.object({}), returns: z.string().describe('Error message'), parametersJsonSchema: { type: 'object', properties: {}, required: [], }, returnsJsonSchema: { type: 'string', description: 'Error message', }, execute: () => { throw new Error('Deliberate failure'); }, }; describe('toolAgent', () => { beforeEach(() => { vi.clearAllMocks(); }); afterEach(() => { vi.clearAllMocks(); }); it('should execute tool calls', async () => { const result = await executeToolCall({ id: '1', name: 'mockTool', content: JSON.stringify({ input: 'test' }), }, [mockTool], toolContext); expect(result.includes('Processed: test')).toBeTruthy(); }); it('should handle unknown tools', async () => { const result = await executeToolCall({ id: '1', name: 'nonexistentTool', content: JSON.stringify({}), }, [mockTool], toolContext); // Parse the result as JSON const parsedResult = JSON.parse(result); // Check that it contains the expected error properties expect(parsedResult.error).toBe(true); }); it('should handle tool execution errors', async () => { const result = await executeToolCall({ id: '1', name: 'errorTool', content: JSON.stringify({}), }, [errorTool], toolContext); // Parse the result as JSON const parsedResult = JSON.parse(result); // Check that it contains the expected error properties expect(parsedResult.error).toBe(true); expect(parsedResult.message).toContain('Deliberate failure'); }); }); //# sourceMappingURL=toolAgent.test.js.map