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, expect, layer } from '@effect/vitest'; |
| 2 | import { ConfigProvider, Effect } from 'effect'; |
| 3 | import { cli, MockConsole, TestLive } from 'test/__utils__'; |
| 4 | import { |
| 5 | routeRequests, |
| 6 | stubClientResources, |
| 7 | type StubRequest, |
| 8 | } from 'test/__utils__/services/stub-composio-client'; |
| 9 | |
| 10 | const testConfigProvider = ConfigProvider.fromEnv({ |
| 11 | env: { COMPOSIO_USER_API_KEY: 'test_api_key' }, |
| 12 | }); |
| 13 | |
| 14 | const testLayer = TestLive({ |
| 15 | baseConfigProvider: testConfigProvider, |
| 16 | fixture: 'global-test-user-id', |
| 17 | }); |
| 18 | |
| 19 | const ENDPOINT = 'https://gmail.googleapis.com/gmail/v1/users/me/profile'; |
| 20 | const proxyArgs = ['proxy', ENDPOINT, '--toolkit', 'gmail', '--skip-connection-check']; |
| 21 | |
| 22 | const PROXY_EXECUTE = '/api/v3.1/tool_router/session/trs_stub_session/proxy_execute'; |
| 23 | |
| 24 | const isProxyExecute = (request: StubRequest) => request.path === PROXY_EXECUTE; |
| 25 | |
| 26 | /** Proxy calls answered by `respond`; the session they run in is created normally. */ |
| 27 | const proxyingOver = (respond: () => Response) => |
| 28 | stubClientResources( |
| 29 | ['toolRouter'], |
| 30 | routeRequests([{ method: 'POST', path: PROXY_EXECUTE, respond }], () => |
| 31 | Response.json({ |
| 32 | session_id: 'trs_stub_session', |
| 33 | config: { user_id: 'consumer-user-org_test' }, |
| 34 | mcp: { type: 'http', url: 'https://mcp.stub.invalid' }, |
| 35 | tool_router_tools: [], |
| 36 | }) |
| 37 | ) |
| 38 | ); |
| 39 | |
| 40 | const consoleOutput = MockConsole.getLines({ stripAnsi: true }).pipe( |
| 41 | Effect.map(lines => lines.join('\n')) |
| 42 | ); |
| 43 | |
| 44 | describe('CLI: composio proxy over the API client', () => { |
| 45 | layer(testLayer)('[Given] the API rejects the proxied call with an error envelope', it => { |
| 46 | it.effect('[Then] the envelope message and slug are printed and the command fails', () => |
| 47 | Effect.gen(function* () { |
| 48 | const stub = proxyingOver(() => |
| 49 | Response.json( |
| 50 | { |
| 51 | error: { |
| 52 | message: 'The endpoint is not allowed for this toolkit', |
| 53 | code: 4311, |
| 54 | slug: 'ToolRouterV2_ProxyEndpointNotAllowed', |
| 55 | status: 403, |
| 56 | }, |
| 57 | }, |
| 58 | { status: 403 } |
| 59 | ) |
| 60 | ); |
| 61 | const failure = yield* cli(proxyArgs).pipe(Effect.provide(stub.layer), Effect.flip); |
| 62 | const output = yield* consoleOutput; |
| 63 | |
| 64 | expect(failure).toMatchObject({ |
| 65 | _tag: 'commands/ProxyRequestError', |
| 66 | message: `Failed to proxy GET ${ENDPOINT} via "gmail".`, |
| 67 | }); |
| 68 | expect(output).toContain('The endpoint is not allowed for this toolkit'); |
| 69 | expect(output).toContain( |
| 70 | JSON.stringify( |
| 71 | { |
| 72 | successful: false, |
| 73 | error: 'The endpoint is not allowed for this toolkit', |
| 74 | toolkit: 'gmail', |
| 75 | endpoint: ENDPOINT, |
| 76 | slug: 'ToolRouterV2_ProxyEndpointNotAllowed', |
| 77 | }, |
| 78 | null, |
| 79 | 2 |
| 80 | ) |
| 81 | ); |
| 82 | expect(output).not.toContain('Status:'); |
| 83 | }) |
| 84 | ); |
| 85 | }); |
| 86 | |
| 87 | layer(testLayer)('[Given] the proxied API answers with a 404', it => { |
| 88 | it.effect('[Then] the status and body are printed and the command succeeds', () => |
| 89 | Effect.gen(function* () { |
| 90 | const stub = proxyingOver(() => |
| 91 | Response.json({ |
| 92 | status: 404, |
| 93 | data: { error: { code: 404, message: 'Requested entity was not found.' } }, |
| 94 | headers: {}, |
| 95 | }) |
| 96 | ); |
| 97 | yield* cli(proxyArgs).pipe(Effect.provide(stub.layer)); |
| 98 | const output = yield* consoleOutput; |
| 99 | |
| 100 | expect(output).toContain('Status: 404'); |
| 101 | expect(output).toContain('"message": "Requested entity was not found."'); |
| 102 | }) |
| 103 | ); |
| 104 | }); |
| 105 | |
| 106 | // On the live clock: a retry waits on the clock, and under the test clock a |
| 107 | // retried call would hang instead of failing this test. |
| 108 | layer(testLayer, { excludeTestServices: true })( |
| 109 | '[Given] the API answers the proxied call with a 500', |
| 110 | it => { |
| 111 | it.effect('[Then] the call is attempted once', () => |
| 112 | Effect.gen(function* () { |
| 113 | const stub = proxyingOver(() => |
| 114 | Response.json( |
| 115 | { error: { message: 'Internal error', code: 500, slug: 'Internal', status: 500 } }, |
| 116 | // A retry would wait this long, so one that did happen shows up as a second request. |
| 117 | { status: 500, headers: { 'retry-after-ms': '0' } } |
| 118 | ) |
| 119 | ); |
| 120 | const failure = yield* cli(proxyArgs).pipe(Effect.provide(stub.layer), Effect.flip); |
| 121 | const output = yield* consoleOutput; |
| 122 | |
| 123 | expect(failure).toMatchObject({ |
| 124 | _tag: 'commands/ProxyRequestError', |
| 125 | message: `Failed to proxy GET ${ENDPOINT} via "gmail".`, |
| 126 | }); |
| 127 | expect(output).toContain('Internal error'); |
| 128 | expect(stub.requests.filter(isProxyExecute)).toMatchObject([ |
| 129 | { method: 'POST', body: { toolkit_slug: 'gmail', endpoint: ENDPOINT, method: 'GET' } }, |
| 130 | ]); |
| 131 | }) |
| 132 | ); |
| 133 | } |
| 134 | ); |
| 135 | }); |