|
| 1 | +import { describe, test, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { Tool } from './index'; |
| 3 | +import { ExecutionError, InvalidImplementationError, InvalidSecretsError } from '../errors'; |
| 4 | +import { FunctionInput } from '../types'; |
| 5 | + |
| 6 | +describe('Tool', () => { |
| 7 | + let validConfig: { |
| 8 | + properties?: Record<string, FunctionInput>; |
| 9 | + secrets?: string[]; |
| 10 | + }; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + validConfig = { |
| 14 | + properties: { |
| 15 | + requiredProp: { type: 'string', description: 'This is a required prop', required: true }, |
| 16 | + optionalProp: { type: 'string', description: 'This is an optional prop', required: false } |
| 17 | + }, |
| 18 | + secrets: ['apiKey'] |
| 19 | + }; |
| 20 | + }); |
| 21 | + |
| 22 | + describe('constructor', () => { |
| 23 | + test('creates a valid tool with all required properties', () => { |
| 24 | + const tool = new Tool('testTool', 'Test description', validConfig); |
| 25 | + expect(tool.name).toBe('testTool'); |
| 26 | + expect(tool.description).toBe('Test description'); |
| 27 | + expect(tool.config).toEqual(validConfig); |
| 28 | + }); |
| 29 | + |
| 30 | + test('throws error when missing required properties', () => { |
| 31 | + expect(() => { |
| 32 | + new Tool('', 'Test description', validConfig); |
| 33 | + }).toThrow(InvalidImplementationError); |
| 34 | + |
| 35 | + expect(() => { |
| 36 | + // @ts-expect-error Testing invalid constructor params |
| 37 | + new Tool('Test Tool', 'Test description', null); |
| 38 | + }).toThrow(InvalidImplementationError); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + |
| 43 | + describe('execute', () => { |
| 44 | + test('successfully executes a registered function with valid input and secrets', async () => { |
| 45 | + const implementation = vi.fn().mockReturnValue('success'); |
| 46 | + const tool = new Tool('testTool', 'Test description', validConfig); |
| 47 | + tool.registerFunction(implementation); |
| 48 | + |
| 49 | + const result = await tool.execute( |
| 50 | + { requiredProp: 'value' }, |
| 51 | + { apiKey: 'test-key' } |
| 52 | + ); |
| 53 | + |
| 54 | + expect(result).toBe('success'); |
| 55 | + expect(implementation).toHaveBeenCalledWith( |
| 56 | + { requiredProp: 'value' }, |
| 57 | + { apiKey: 'test-key' } |
| 58 | + ); |
| 59 | + }); |
| 60 | + |
| 61 | + test('throws ExecutionError when no implementation is registered', async () => { |
| 62 | + const tool = new Tool('testTool', 'Test description', validConfig); |
| 63 | + |
| 64 | + await expect(tool.execute( |
| 65 | + { requiredProp: 'value' }, |
| 66 | + { apiKey: 'test-key' } |
| 67 | + )).rejects.toThrow(ExecutionError); |
| 68 | + }); |
| 69 | + |
| 70 | + test('throws InvalidSecretsError when required secrets are missing', async () => { |
| 71 | + const implementation = vi.fn(); |
| 72 | + const tool = new Tool('testTool', 'Test description', validConfig, implementation); |
| 73 | + |
| 74 | + await expect(tool.execute( |
| 75 | + { requiredProp: 'value' }, |
| 76 | + {} |
| 77 | + )).rejects.toThrow(InvalidSecretsError); |
| 78 | + }); |
| 79 | + |
| 80 | + test('throws InvalidImplementationError when required properties are missing', async () => { |
| 81 | + const implementation = vi.fn(); |
| 82 | + const tool = new Tool('testTool', 'Test description', validConfig, implementation); |
| 83 | + |
| 84 | + await expect(tool.execute( |
| 85 | + { optionalProp: 'value' }, |
| 86 | + { apiKey: 'test-key' } |
| 87 | + )).rejects.toThrow(InvalidImplementationError); |
| 88 | + }); |
| 89 | + |
| 90 | + test('handles implementation throwing an error', async () => { |
| 91 | + const implementation = vi.fn().mockImplementation(() => { |
| 92 | + throw new Error('Implementation error'); |
| 93 | + }); |
| 94 | + const tool = new Tool('testTool', 'Test description', validConfig, implementation); |
| 95 | + |
| 96 | + await expect(tool.execute( |
| 97 | + { requiredProp: 'value' }, |
| 98 | + { apiKey: 'test-key' } |
| 99 | + )).rejects.toThrow(ExecutionError); |
| 100 | + }); |
| 101 | + |
| 102 | + test('works with no properties configured', async () => { |
| 103 | + const implementation = vi.fn().mockReturnValue('success'); |
| 104 | + const tool = new Tool('testTool', 'Test description', { |
| 105 | + secrets: ['apiKey'] |
| 106 | + }, implementation); |
| 107 | + |
| 108 | + const result = await tool.execute( |
| 109 | + {}, |
| 110 | + { apiKey: 'test-key' } |
| 111 | + ); |
| 112 | + |
| 113 | + expect(result).toBe('success'); |
| 114 | + }); |
| 115 | + |
| 116 | + test('works with no secrets configured', async () => { |
| 117 | + const implementation = vi.fn().mockReturnValue('success'); |
| 118 | + const tool = new Tool('testTool', 'Test description', { |
| 119 | + properties: { |
| 120 | + requiredProp: { type: 'string', description: 'This is a required prop', required: true } |
| 121 | + } |
| 122 | + }, implementation); |
| 123 | + |
| 124 | + const result = await tool.execute( |
| 125 | + { requiredProp: 'value' } |
| 126 | + ); |
| 127 | + |
| 128 | + expect(result).toBe('success'); |
| 129 | + }); |
| 130 | + }); |
| 131 | +}); |
0 commit comments