Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | import { BaseAgenticProvider, BaseNonAgenticProvider } from '../../src/provider/BaseProvider'; |
| 3 | import { Tool, ToolExecuteParams, ToolExecuteResponse } from '../../src/types/tool.types'; |
| 4 | import { ExecuteToolFn } from '../../src/types/provider.types'; |
| 5 | import { ComposioGlobalExecuteToolFnNotSetError } from '../../src/errors/ToolErrors'; |
| 6 | |
| 7 | // Mock implementations of the abstract classes for testing |
| 8 | class MockNonAgenticProvider extends BaseNonAgenticProvider<string[], string> { |
| 9 | readonly name = 'MockNonAgenticProvider'; |
| 10 | |
| 11 | wrapTool = vi.fn((tool: Tool): string => { |
| 12 | return `wrapped-non-agentic-tool:${tool.slug}`; |
| 13 | }); |
| 14 | |
| 15 | wrapTools = vi.fn((tools: Tool[]): string[] => { |
| 16 | return tools.map(tool => this.wrapTool(tool)); |
| 17 | }); |
| 18 | } |
| 19 | |
| 20 | class MockAgenticProvider extends BaseAgenticProvider<string[], string> { |
| 21 | readonly name = 'MockAgenticProvider'; |
| 22 | |
| 23 | wrapTool = vi.fn((tool: Tool, executeTool: ExecuteToolFn): string => { |
| 24 | return `wrapped-agentic-tool:${tool.slug}`; |
| 25 | }); |
| 26 | |
| 27 | wrapTools = vi.fn((tools: Tool[], executeTool: ExecuteToolFn): string[] => { |
| 28 | return tools.map(tool => this.wrapTool(tool, executeTool)); |
| 29 | }); |
| 30 | } |
| 31 | |
| 32 | describe('BaseProvider', () => { |
| 33 | describe('BaseNonAgenticProvider', () => { |
| 34 | let nonAgenticProvider: MockNonAgenticProvider; |
| 35 | |
| 36 | beforeEach(() => { |
| 37 | nonAgenticProvider = new MockNonAgenticProvider(); |
| 38 | nonAgenticProvider._setExecuteToolFn( |
| 39 | vi.fn().mockResolvedValue({ |
| 40 | data: { result: 'success' }, |
| 41 | error: null, |
| 42 | successful: true, |
| 43 | }) |
| 44 | ); |
| 45 | vi.resetAllMocks(); |
| 46 | }); |
| 47 | |
| 48 | it('should have _isAgentic property set to false', () => { |
| 49 | expect(nonAgenticProvider._isAgentic).toBe(false); |
| 50 | }); |
| 51 | |
| 52 | it('should have the correct name', () => { |
| 53 | expect(nonAgenticProvider.name).toBe('MockNonAgenticProvider'); |
| 54 | }); |
| 55 | |
| 56 | it('should wrap a single tool correctly', () => { |
| 57 | const mockTool: Tool = { |
| 58 | slug: 'test-tool', |
| 59 | name: 'Test Tool', |
| 60 | description: 'A tool for testing', |
| 61 | tags: [], |
| 62 | }; |
| 63 | |
| 64 | const wrapped = nonAgenticProvider.wrapTool(mockTool); |
| 65 | |
| 66 | expect(nonAgenticProvider.wrapTool).toHaveBeenCalledWith(mockTool); |
| 67 | expect(wrapped).toBe('wrapped-non-agentic-tool:test-tool'); |
| 68 | }); |
| 69 | |
| 70 | it('should wrap multiple tools correctly', () => { |
| 71 | const mockTools: Tool[] = [ |
| 72 | { |
| 73 | slug: 'tool-1', |
| 74 | name: 'Tool One', |
| 75 | description: 'First test tool', |
| 76 | tags: [], |
| 77 | }, |
| 78 | { |
| 79 | slug: 'tool-2', |
| 80 | name: 'Tool Two', |
| 81 | description: 'Second test tool', |
| 82 | tags: [], |
| 83 | }, |
| 84 | ]; |
| 85 | |
| 86 | const wrapped = nonAgenticProvider.wrapTools(mockTools); |
| 87 | |
| 88 | expect(nonAgenticProvider.wrapTools).toHaveBeenCalledWith(mockTools); |
| 89 | expect(wrapped).toEqual([ |
| 90 | 'wrapped-non-agentic-tool:tool-1', |
| 91 | 'wrapped-non-agentic-tool:tool-2', |
| 92 | ]); |
| 93 | }); |
| 94 | |
| 95 | it('should execute a tool using the global execute function', async () => { |
| 96 | // Create a mock for the global execute function |
| 97 | const mockGlobalExecute = vi.fn().mockResolvedValue({ |
| 98 | data: { result: 'success' }, |
| 99 | error: null, |
| 100 | successful: true, |
| 101 | }); |
| 102 | |
| 103 | // Set the execute function |
| 104 | nonAgenticProvider._setExecuteToolFn(mockGlobalExecute); |
| 105 | |
| 106 | // Execute a tool |
| 107 | const toolSlug = 'test-tool'; |
| 108 | const toolParams: ToolExecuteParams = { |
| 109 | userId: 'test-user', |
| 110 | arguments: { query: 'test' }, |
| 111 | }; |
| 112 | const modifiers = { |
| 113 | beforeExecute: vi.fn(({ params }) => params), |
| 114 | afterExecute: vi.fn(({ result }) => result), |
| 115 | }; |
| 116 | |
| 117 | const result = await nonAgenticProvider.executeTool(toolSlug, toolParams, modifiers); |
| 118 | |
| 119 | // Verify the global execute function was called with the correct parameters |
| 120 | expect(mockGlobalExecute).toHaveBeenCalledWith(toolSlug, toolParams, modifiers); |
| 121 | expect(result).toEqual({ |
| 122 | data: { result: 'success' }, |
| 123 | error: null, |
| 124 | successful: true, |
| 125 | }); |
| 126 | }); |
| 127 | }); |
| 128 | |
| 129 | describe('BaseAgenticProvider', () => { |
| 130 | let agenticProvider: MockAgenticProvider; |
| 131 | let mockExecuteTool: ExecuteToolFn; |
| 132 | |
| 133 | beforeEach(() => { |
| 134 | agenticProvider = new MockAgenticProvider(); |
| 135 | agenticProvider._setExecuteToolFn( |
| 136 | vi.fn().mockResolvedValue({ |
| 137 | data: { result: 'success' }, |
| 138 | error: null, |
| 139 | successful: true, |
| 140 | }) |
| 141 | ); |
| 142 | mockExecuteTool = vi.fn().mockResolvedValue({ |
| 143 | data: { result: 'success' }, |
| 144 | error: null, |
| 145 | successful: true, |
| 146 | }); |
| 147 | vi.resetAllMocks(); |
| 148 | }); |
| 149 | |
| 150 | it('should have _isAgentic property set to true', () => { |
| 151 | expect(agenticProvider._isAgentic).toBe(true); |
| 152 | }); |
| 153 | |
| 154 | it('should have the correct name', () => { |
| 155 | expect(agenticProvider.name).toBe('MockAgenticProvider'); |
| 156 | }); |
| 157 | |
| 158 | it('should wrap a single tool correctly with execute function', () => { |
| 159 | const mockTool: Tool = { |
| 160 | slug: 'test-tool', |
| 161 | name: 'Test Tool', |
| 162 | description: 'A tool for testing', |
| 163 | tags: [], |
| 164 | }; |
| 165 | |
| 166 | const wrapped = agenticProvider.wrapTool(mockTool, mockExecuteTool); |
| 167 | |
| 168 | expect(agenticProvider.wrapTool).toHaveBeenCalledWith(mockTool, mockExecuteTool); |
| 169 | expect(wrapped).toBe('wrapped-agentic-tool:test-tool'); |
| 170 | }); |
| 171 | |
| 172 | it('should wrap multiple tools correctly with execute function', () => { |
| 173 | const mockTools: Tool[] = [ |
| 174 | { |
| 175 | slug: 'tool-1', |
| 176 | name: 'Tool One', |
| 177 | description: 'First test tool', |
| 178 | tags: [], |
| 179 | }, |
| 180 | { |
| 181 | slug: 'tool-2', |
| 182 | name: 'Tool Two', |
| 183 | description: 'Second test tool', |
| 184 | tags: [], |
| 185 | }, |
| 186 | ]; |
| 187 | |
| 188 | const wrapped = agenticProvider.wrapTools(mockTools, mockExecuteTool); |
| 189 | |
| 190 | expect(agenticProvider.wrapTools).toHaveBeenCalledWith(mockTools, mockExecuteTool); |
| 191 | expect(wrapped).toEqual(['wrapped-agentic-tool:tool-1', 'wrapped-agentic-tool:tool-2']); |
| 192 | }); |
| 193 | |
| 194 | it('should execute a tool using the global execute function', async () => { |
| 195 | // Create a mock for the global execute function |
| 196 | const mockGlobalExecute = vi.fn().mockResolvedValue({ |
| 197 | data: { result: 'success' }, |
| 198 | error: null, |
| 199 | successful: true, |
| 200 | }); |
| 201 | |
| 202 | // Set the execute function |
| 203 | agenticProvider._setExecuteToolFn(mockGlobalExecute); |
| 204 | |
| 205 | // Execute a tool |
| 206 | const toolSlug = 'test-tool'; |
| 207 | const toolParams: ToolExecuteParams = { |
| 208 | userId: 'test-user', |
| 209 | arguments: { query: 'test' }, |
| 210 | }; |
| 211 | const modifiers = { |
| 212 | beforeExecute: vi.fn(({ params }) => params), |
| 213 | afterExecute: vi.fn(({ result }) => result), |
| 214 | }; |
| 215 | |
| 216 | const result = await agenticProvider.executeTool(toolSlug, toolParams, modifiers); |
| 217 | |
| 218 | // Verify the global execute function was called with the correct parameters |
| 219 | expect(mockGlobalExecute).toHaveBeenCalledWith(toolSlug, toolParams, modifiers); |
| 220 | expect(result).toEqual({ |
| 221 | data: { result: 'success' }, |
| 222 | error: null, |
| 223 | successful: true, |
| 224 | }); |
| 225 | }); |
| 226 | }); |
| 227 | |
| 228 | describe('_setExecuteToolFn', () => { |
| 229 | it('should throw an error if trying to execute a tool without setting the execute function', async () => { |
| 230 | const provider = new MockNonAgenticProvider(); |
| 231 | |
| 232 | const toolSlug = 'test-tool'; |
| 233 | const toolParams: ToolExecuteParams = { |
| 234 | userId: 'test-user', |
| 235 | arguments: { query: 'test' }, |
| 236 | }; |
| 237 | |
| 238 | // This should throw an error because _globalExecuteToolFn is not set |
| 239 | try { |
| 240 | await provider.executeTool(toolSlug, toolParams); |
| 241 | } catch (error) { |
| 242 | expect(error).toBeInstanceOf(ComposioGlobalExecuteToolFnNotSetError); |
| 243 | } |
| 244 | }); |
| 245 | }); |
| 246 | }); |