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 { env, createExecutionContext, waitOnExecutionContext } from 'cloudflare:test'; |
| 2 | import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 3 | import { Composio, type Tool } from '@composio/core'; |
| 4 | import app from '../src/index'; |
| 5 | |
| 6 | const IncomingRequest = Request<unknown, IncomingRequestCfProperties>; |
| 7 | |
| 8 | describe('@composio/core Files - Cloudflare Workers compatibility', () => { |
| 9 | afterEach(() => { |
| 10 | vi.restoreAllMocks(); |
| 11 | }); |
| 12 | |
| 13 | it('should list the available endpoints', async () => { |
| 14 | const request = new IncomingRequest('http://localhost/'); |
| 15 | const ctx = createExecutionContext(); |
| 16 | const response = await app.fetch(request, env, ctx); |
| 17 | await waitOnExecutionContext(ctx); |
| 18 | |
| 19 | expect(response.status).toBe(200); |
| 20 | |
| 21 | const body = (await response.json()) as { |
| 22 | message: string; |
| 23 | endpoints: string[]; |
| 24 | }; |
| 25 | |
| 26 | expect(body.message).toContain('Composio Core Files E2E Test Worker'); |
| 27 | expect(body.endpoints).toMatchInlineSnapshot(` |
| 28 | [ |
| 29 | "/test/files/upload", |
| 30 | "/test/files/download", |
| 31 | "/test/auto-upload-disabled", |
| 32 | "/test/default-config", |
| 33 | ] |
| 34 | `); |
| 35 | }); |
| 36 | |
| 37 | it('should throw an error when calling files.upload() in Cloudflare Workers', async () => { |
| 38 | const request = new IncomingRequest('http://localhost/test/files/upload'); |
| 39 | const ctx = createExecutionContext(); |
| 40 | const response = await app.fetch(request, env, ctx); |
| 41 | await waitOnExecutionContext(ctx); |
| 42 | |
| 43 | expect(response.status).toBe(200); |
| 44 | |
| 45 | const body = (await response.json()) as { |
| 46 | error?: string; |
| 47 | }; |
| 48 | |
| 49 | expect(body.error).toContain('not supported in Cloudflare Workers'); |
| 50 | }); |
| 51 | |
| 52 | it('should throw an error when calling files.download() in Cloudflare Workers', async () => { |
| 53 | const request = new IncomingRequest('http://localhost/test/files/download'); |
| 54 | const ctx = createExecutionContext(); |
| 55 | const response = await app.fetch(request, env, ctx); |
| 56 | await waitOnExecutionContext(ctx); |
| 57 | |
| 58 | expect(response.status).toBe(200); |
| 59 | |
| 60 | const body = (await response.json()) as { |
| 61 | error?: string; |
| 62 | }; |
| 63 | |
| 64 | expect(body.error).toContain('not supported in Cloudflare Workers'); |
| 65 | }); |
| 66 | |
| 67 | it('should surface the edge-runtime FileToolModifier error from execute() when auto-upload is enabled', async () => { |
| 68 | // Runs inside workerd (vitest-pool-workers), so `#file_tool_modifier` resolves to the |
| 69 | // .workerd variant — the exact module under test. We drive the real public execute() |
| 70 | // pipeline and stub only the network tool lookup, so the before-execute file modifier |
| 71 | // is reached without depending on a remote tool registry or network access. |
| 72 | const composio = new Composio({ |
| 73 | apiKey: env.COMPOSIO_API_KEY, |
| 74 | dangerouslyAllowAutoUploadDownloadFiles: true, |
| 75 | }); |
| 76 | |
| 77 | const fileTool: Tool = { |
| 78 | slug: 'CUSTOM_FILE_TOOL', |
| 79 | name: 'Custom File Tool', |
| 80 | description: 'Fixture tool with a file-uploadable input', |
| 81 | inputParameters: { |
| 82 | type: 'object', |
| 83 | properties: { |
| 84 | file: { |
| 85 | type: 'string', |
| 86 | file_uploadable: true, |
| 87 | }, |
| 88 | }, |
| 89 | required: ['file'], |
| 90 | }, |
| 91 | toolkit: { slug: 'custom', name: 'custom' }, |
| 92 | }; |
| 93 | |
| 94 | vi.spyOn(composio.tools, 'getRawComposioToolBySlug').mockResolvedValue(fileTool); |
| 95 | |
| 96 | await expect( |
| 97 | composio.tools.execute('CUSTOM_FILE_TOOL', { |
| 98 | arguments: { |
| 99 | file: 'https://example.com/test.pdf', |
| 100 | }, |
| 101 | dangerouslySkipVersionCheck: true, |
| 102 | }) |
| 103 | ).rejects.toThrow('not available on edge runtimes'); |
| 104 | }); |
| 105 | |
| 106 | it('should successfully initialize Composio with automatic file handling disabled', async () => { |
| 107 | const request = new IncomingRequest('http://localhost/test/auto-upload-disabled'); |
| 108 | const ctx = createExecutionContext(); |
| 109 | const response = await app.fetch(request, env, ctx); |
| 110 | await waitOnExecutionContext(ctx); |
| 111 | |
| 112 | expect(response.status).toBe(200); |
| 113 | |
| 114 | const body = (await response.json()) as { |
| 115 | success: boolean; |
| 116 | message?: string; |
| 117 | hasProvider?: boolean; |
| 118 | hasTools?: boolean; |
| 119 | hasFiles?: boolean; |
| 120 | recommendation?: string; |
| 121 | }; |
| 122 | |
| 123 | expect(body.success).toBe(true); |
| 124 | expect(body.message).toContain('dangerouslyAllowAutoUploadDownloadFiles: false'); |
| 125 | expect(body.hasProvider).toBe(true); |
| 126 | expect(body.hasTools).toBe(true); |
| 127 | expect(body.hasFiles).toBe(true); |
| 128 | }); |
| 129 | |
| 130 | it('should successfully initialize Composio with default configuration', async () => { |
| 131 | const request = new IncomingRequest('http://localhost/test/default-config'); |
| 132 | const ctx = createExecutionContext(); |
| 133 | const response = await app.fetch(request, env, ctx); |
| 134 | await waitOnExecutionContext(ctx); |
| 135 | |
| 136 | expect(response.status).toBe(200); |
| 137 | |
| 138 | const body = (await response.json()) as { |
| 139 | success: boolean; |
| 140 | message?: string; |
| 141 | hasProvider?: boolean; |
| 142 | hasTools?: boolean; |
| 143 | hasFiles?: boolean; |
| 144 | note?: string; |
| 145 | }; |
| 146 | |
| 147 | expect(body.success).toBe(true); |
| 148 | expect(body.message).toContain('default configuration'); |
| 149 | expect(body.hasProvider).toBe(true); |
| 150 | expect(body.hasTools).toBe(true); |
| 151 | expect(body.hasFiles).toBe(true); |
| 152 | expect(body.note).toContain('dangerouslyAllowAutoUploadDownloadFiles defaults to false'); |
| 153 | }); |
| 154 | }); |