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 { CloudflareProvider } from '../src'; |
| 3 | import { Tool } from '@composio/core'; |
| 4 | |
| 5 | // Instead of importing and mocking the actual Cloudflare types, let's define our own for testing |
| 6 | interface AiTextGenerationFunction { |
| 7 | name: string; |
| 8 | description: string; |
| 9 | parameters: unknown; |
| 10 | } |
| 11 | |
| 12 | interface MockedCloudflareToolInput { |
| 13 | type: 'function'; |
| 14 | function: { |
| 15 | name: string; |
| 16 | description?: string; |
| 17 | parameters?: unknown; |
| 18 | }; |
| 19 | } |
| 20 | |
| 21 | // Mock the types we need from Cloudflare |
| 22 | vi.mock('@cloudflare/workers-types', () => { |
| 23 | return {}; |
| 24 | }); |
| 25 | |
| 26 | describe('CloudflareProvider', () => { |
| 27 | let provider: CloudflareProvider; |
| 28 | let mockTool: Tool; |
| 29 | let mockExecuteToolFn: unknown; |
| 30 | |
| 31 | beforeEach(() => { |
| 32 | provider = new CloudflareProvider(); |
| 33 | |
| 34 | // Mock the global execute tool function |
| 35 | mockExecuteToolFn = vi.fn().mockResolvedValue({ |
| 36 | data: { result: 'success' }, |
| 37 | error: null, |
| 38 | successful: true, |
| 39 | }); |
| 40 | provider._setExecuteToolFn(mockExecuteToolFn); |
| 41 | |
| 42 | // Create a mock Composio tool |
| 43 | mockTool = { |
| 44 | slug: 'test-tool', |
| 45 | name: 'Test Tool', |
| 46 | description: 'A tool for testing', |
| 47 | inputParameters: { |
| 48 | type: 'object', |
| 49 | properties: { |
| 50 | input: { |
| 51 | type: 'string', |
| 52 | description: 'Test input', |
| 53 | }, |
| 54 | }, |
| 55 | required: ['input'], |
| 56 | }, |
| 57 | tags: [], |
| 58 | }; |
| 59 | |
| 60 | // Reset mocks before each test |
| 61 | vi.clearAllMocks(); |
| 62 | }); |
| 63 | |
| 64 | describe('name property', () => { |
| 65 | it('should have the correct name', () => { |
| 66 | expect(provider.name).toBe('cloudflare'); |
| 67 | }); |
| 68 | }); |
| 69 | |
| 70 | describe('_isAgentic property', () => { |
| 71 | it('should be non-agentic', () => { |
| 72 | expect(provider._isAgentic).toBe(false); |
| 73 | }); |
| 74 | }); |
| 75 | |
| 76 | describe('wrapTool', () => { |
| 77 | it('should wrap a tool in Cloudflare AiTextGenerationToolInput format', () => { |
| 78 | const wrapped = provider.wrapTool(mockTool) as MockedCloudflareToolInput; |
| 79 | |
| 80 | expect(wrapped).toEqual({ |
| 81 | type: 'function', |
| 82 | function: { |
| 83 | name: mockTool.slug, |
| 84 | description: mockTool.description, |
| 85 | parameters: mockTool.inputParameters, |
| 86 | }, |
| 87 | }); |
| 88 | }); |
| 89 | |
| 90 | it('should handle tools without input parameters', () => { |
| 91 | const toolWithoutParams: Tool = { |
| 92 | ...mockTool, |
| 93 | inputParameters: undefined, |
| 94 | }; |
| 95 | |
| 96 | // This would normally throw an error due to the type casting in the function, |
| 97 | // but we're testing the behavior, not the types |
| 98 | const wrapped = provider.wrapTool(toolWithoutParams) as MockedCloudflareToolInput; |
| 99 | |
| 100 | expect(wrapped).toEqual({ |
| 101 | type: 'function', |
| 102 | function: { |
| 103 | name: toolWithoutParams.slug, |
| 104 | description: toolWithoutParams.description, |
| 105 | parameters: undefined, |
| 106 | }, |
| 107 | }); |
| 108 | }); |
| 109 | |
| 110 | it('should handle tools without description', () => { |
| 111 | const toolWithoutDescription: Tool = { |
| 112 | ...mockTool, |
| 113 | description: undefined, |
| 114 | }; |
| 115 | |
| 116 | const wrapped = provider.wrapTool(toolWithoutDescription) as MockedCloudflareToolInput; |
| 117 | |
| 118 | expect(wrapped).toEqual({ |
| 119 | type: 'function', |
| 120 | function: { |
| 121 | name: toolWithoutDescription.slug, |
| 122 | description: undefined, |
| 123 | parameters: toolWithoutDescription.inputParameters, |
| 124 | }, |
| 125 | }); |
| 126 | }); |
| 127 | |
| 128 | it('deduplicates required entries for directly wrapped tools', () => { |
| 129 | const wrapped = provider.wrapTool({ |
| 130 | ...mockTool, |
| 131 | inputParameters: { |
| 132 | ...mockTool.inputParameters!, |
| 133 | required: ['input', 'input'], |
| 134 | }, |
| 135 | }) as MockedCloudflareToolInput; |
| 136 | |
| 137 | expect(wrapped.function.parameters.required).toEqual(['input']); |
| 138 | }); |
| 139 | }); |
| 140 | |
| 141 | describe('wrapTools', () => { |
| 142 | it('should wrap multiple tools into a dictionary', () => { |
| 143 | const anotherTool: Tool = { |
| 144 | ...mockTool, |
| 145 | slug: 'another-tool', |
| 146 | name: 'Another Tool', |
| 147 | }; |
| 148 | const tools = [mockTool, anotherTool]; |
| 149 | |
| 150 | const wrapped = provider.wrapTools(tools); |
| 151 | |
| 152 | // Verify the result has the expected properties |
| 153 | expect(Object.keys(wrapped)).toHaveLength(2); |
| 154 | expect(wrapped['test-tool']).toBeDefined(); |
| 155 | expect(wrapped['another-tool']).toBeDefined(); |
| 156 | |
| 157 | // Verify each tool was properly wrapped |
| 158 | expect(wrapped['test-tool']).toEqual({ |
| 159 | type: 'function', |
| 160 | function: { |
| 161 | name: mockTool.slug, |
| 162 | description: mockTool.description, |
| 163 | parameters: mockTool.inputParameters, |
| 164 | }, |
| 165 | }); |
| 166 | |
| 167 | expect(wrapped['another-tool']).toEqual({ |
| 168 | type: 'function', |
| 169 | function: { |
| 170 | name: anotherTool.slug, |
| 171 | description: anotherTool.description, |
| 172 | parameters: anotherTool.inputParameters, |
| 173 | }, |
| 174 | }); |
| 175 | }); |
| 176 | |
| 177 | it('should return an empty object for empty tools array', () => { |
| 178 | const wrapped = provider.wrapTools([]); |
| 179 | expect(wrapped).toEqual({}); |
| 180 | }); |
| 181 | }); |
| 182 | |
| 183 | describe('executeToolCall', () => { |
| 184 | it('should execute a tool call and return the result as string', async () => { |
| 185 | const userId = 'test-user'; |
| 186 | const toolCall = { |
| 187 | name: 'test-tool', |
| 188 | arguments: { input: 'test-value' }, |
| 189 | }; |
| 190 | |
| 191 | const result = await provider.executeToolCall(userId, toolCall, {}); |
| 192 | |
| 193 | expect(mockExecuteToolFn).toHaveBeenCalledWith( |
| 194 | 'test-tool', |
| 195 | { |
| 196 | arguments: { input: 'test-value' }, |
| 197 | userId: 'test-user', |
| 198 | connectedAccountId: undefined, |
| 199 | customAuthParams: undefined, |
| 200 | }, |
| 201 | undefined |
| 202 | ); |
| 203 | expect(result).toBe( |
| 204 | JSON.stringify({ |
| 205 | data: { result: 'success' }, |
| 206 | error: null, |
| 207 | successful: true, |
| 208 | }) |
| 209 | ); |
| 210 | }); |
| 211 | |
| 212 | it('should handle string arguments by parsing them', async () => { |
| 213 | const userId = 'test-user'; |
| 214 | const toolCall = { |
| 215 | name: 'test-tool', |
| 216 | arguments: JSON.stringify({ input: 'test-value' }), |
| 217 | }; |
| 218 | |
| 219 | const result = await provider.executeToolCall(userId, toolCall, {}); |
| 220 | |
| 221 | expect(mockExecuteToolFn).toHaveBeenCalledWith( |
| 222 | 'test-tool', |
| 223 | { |
| 224 | arguments: { input: 'test-value' }, |
| 225 | userId: 'test-user', |
| 226 | connectedAccountId: undefined, |
| 227 | customAuthParams: undefined, |
| 228 | }, |
| 229 | undefined |
| 230 | ); |
| 231 | expect(result).toBe( |
| 232 | JSON.stringify({ |
| 233 | data: { result: 'success' }, |
| 234 | error: null, |
| 235 | successful: true, |
| 236 | }) |
| 237 | ); |
| 238 | }); |
| 239 | |
| 240 | it('should pass options to executeTool', async () => { |
| 241 | const userId = 'test-user'; |
| 242 | const toolCall = { |
| 243 | name: 'test-tool', |
| 244 | arguments: { input: 'test-value' }, |
| 245 | }; |
| 246 | |
| 247 | const options = { |
| 248 | connectedAccountId: 'conn-123', |
| 249 | customAuthParams: { |
| 250 | parameters: [{ name: 'token', value: 'abc123', in: 'header' as const }], |
| 251 | }, |
| 252 | }; |
| 253 | |
| 254 | const modifiers = { |
| 255 | beforeExecute: vi.fn(({ params }) => params), |
| 256 | afterExecute: vi.fn(({ result }) => result), |
| 257 | }; |
| 258 | |
| 259 | await provider.executeToolCall(userId, toolCall, options, modifiers); |
| 260 | |
| 261 | expect(mockExecuteToolFn).toHaveBeenCalledWith( |
| 262 | 'test-tool', |
| 263 | { |
| 264 | arguments: { input: 'test-value' }, |
| 265 | userId: 'test-user', |
| 266 | connectedAccountId: 'conn-123', |
| 267 | customAuthParams: options.customAuthParams, |
| 268 | }, |
| 269 | modifiers |
| 270 | ); |
| 271 | }); |
| 272 | }); |
| 273 | |
| 274 | describe('integration with Cloudflare AI', () => { |
| 275 | it('should produce tools compatible with Cloudflare AI', () => { |
| 276 | const wrapped = provider.wrapTool(mockTool) as MockedCloudflareToolInput; |
| 277 | |
| 278 | // Verify the wrapped tool has the expected structure |
| 279 | expect(wrapped).toHaveProperty('type', 'function'); |
| 280 | expect(wrapped).toHaveProperty('function'); |
| 281 | expect(wrapped.function).toHaveProperty('name', mockTool.slug); |
| 282 | expect(wrapped.function).toHaveProperty('description', mockTool.description); |
| 283 | expect(wrapped.function).toHaveProperty('parameters', mockTool.inputParameters); |
| 284 | |
| 285 | // The structure should match what Cloudflare AI expects |
| 286 | expect(wrapped.type).toBe('function'); |
| 287 | }); |
| 288 | }); |
| 289 | |
| 290 | describe('executeToolCall with a Tool Router session', () => { |
| 291 | it('keeps user ID calls working on @composio/core releases without session helpers', async () => { |
| 292 | // The peer range admits cores that predate executeToolForTarget. |
| 293 | Object.defineProperty(provider, 'executeToolForTarget', { value: undefined }); |
| 294 | |
| 295 | await provider.executeToolCall('test-user', { name: 'test-tool', arguments: {} }); |
| 296 | |
| 297 | expect(mockExecuteToolFn).toHaveBeenCalledWith( |
| 298 | 'test-tool', |
| 299 | expect.objectContaining({ userId: 'test-user' }), |
| 300 | undefined |
| 301 | ); |
| 302 | }); |
| 303 | |
| 304 | it('still executes a user ID call without options', async () => { |
| 305 | await provider.executeToolCall('test-user', { name: 'test-tool', arguments: {} }); |
| 306 | |
| 307 | expect(mockExecuteToolFn).toHaveBeenCalledWith( |
| 308 | 'test-tool', |
| 309 | expect.objectContaining({ arguments: {}, userId: 'test-user' }), |
| 310 | undefined |
| 311 | ); |
| 312 | }); |
| 313 | |
| 314 | it('executes through the session instead of the direct tools API', async () => { |
| 315 | const session = { |
| 316 | execute: vi.fn().mockResolvedValue({ |
| 317 | data: { result: 'session-success' }, |
| 318 | error: null, |
| 319 | logId: 'log-session', |
| 320 | resultType: 'completed', |
| 321 | }), |
| 322 | }; |
| 323 | |
| 324 | const result = await provider.executeToolCall(session, { |
| 325 | name: 'COMPOSIO_SEARCH_TOOLS', |
| 326 | arguments: JSON.stringify({ query: 'send an email' }), |
| 327 | }); |
| 328 | |
| 329 | expect(session.execute).toHaveBeenCalledWith('COMPOSIO_SEARCH_TOOLS', { |
| 330 | query: 'send an email', |
| 331 | }); |
| 332 | expect(mockExecuteToolFn).not.toHaveBeenCalled(); |
| 333 | expect(JSON.parse(result)).toEqual({ |
| 334 | data: { result: 'session-success' }, |
| 335 | error: null, |
| 336 | logId: 'log-session', |
| 337 | resultType: 'completed', |
| 338 | successful: true, |
| 339 | }); |
| 340 | }); |
| 341 | |
| 342 | it('reports a failed session execution in the result', async () => { |
| 343 | const session = { |
| 344 | execute: vi.fn().mockResolvedValue({ |
| 345 | data: {}, |
| 346 | error: 'Tool failed', |
| 347 | logId: 'log-fail', |
| 348 | resultType: 'failed', |
| 349 | }), |
| 350 | }; |
| 351 | |
| 352 | const result = await provider.executeToolCall(session, { |
| 353 | name: 'COMPOSIO_SEARCH_TOOLS', |
| 354 | arguments: {}, |
| 355 | }); |
| 356 | |
| 357 | expect(JSON.parse(result)).toMatchObject({ error: 'Tool failed', successful: false }); |
| 358 | }); |
| 359 | |
| 360 | it('rejects direct execution options with a session', async () => { |
| 361 | const session = { execute: vi.fn() }; |
| 362 | |
| 363 | await expect( |
| 364 | provider.executeToolCall( |
| 365 | // @ts-expect-error direct execution options are not accepted with a session |
| 366 | session, |
| 367 | { name: 'COMPOSIO_SEARCH_TOOLS', arguments: {} }, |
| 368 | { connectedAccountId: 'conn-123' } |
| 369 | ) |
| 370 | ).rejects.toThrow( |
| 371 | 'Direct execution options and modifiers cannot be used with a Tool Router session' |
| 372 | ); |
| 373 | expect(session.execute).not.toHaveBeenCalled(); |
| 374 | }); |
| 375 | }); |
| 376 | }); |