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 { beforeEach, vi } from 'vitest'; |
| 3 | import { ConfigProvider, Effect, Exit } from 'effect'; |
| 4 | import { CLI_ANALYTICS_EVENTS } from 'src/analytics/events'; |
| 5 | import { cli, TestLive, MockConsole } from 'test/__utils__'; |
| 6 | import { stubComposioClient } from 'test/__utils__/services/stub-composio-client'; |
| 7 | import { eventsNamed, trackedEvents } from 'test/__utils__/tracked-events'; |
| 8 | |
| 9 | vi.mock('src/analytics/dispatch', async importOriginal => { |
| 10 | const { recordTrackedEvent } = await import('test/__utils__/tracked-events'); |
| 11 | return { |
| 12 | ...(await importOriginal<typeof import('src/analytics/dispatch')>()), |
| 13 | trackCliEventEffect: recordTrackedEvent, |
| 14 | }; |
| 15 | }); |
| 16 | |
| 17 | // Disable CI redaction so tests see raw values; `src/ui/redact` reads `CI` once |
| 18 | // at import time. |
| 19 | vi.hoisted(() => { |
| 20 | delete process.env.CI; |
| 21 | }); |
| 22 | |
| 23 | const testConfigProvider = ConfigProvider.fromEnv({ |
| 24 | env: { COMPOSIO_USER_API_KEY: 'test_api_key' }, |
| 25 | }); |
| 26 | |
| 27 | const executeArgs = ['execute', 'GITHUB_STAR_REPO', '-d', '{"owner":"composio","repo":"composio"}']; |
| 28 | |
| 29 | /** |
| 30 | * A test layer whose Tool Router execution goes through the real client, with |
| 31 | * the request options the executor chose, to a transport answering `respond`. |
| 32 | */ |
| 33 | const executingOver = (respond: () => Response) => { |
| 34 | const stub = stubComposioClient(respond); |
| 35 | const testLayer = TestLive({ |
| 36 | baseConfigProvider: testConfigProvider, |
| 37 | fixture: 'global-test-user-id', |
| 38 | stdin: { isTTY: true, data: '' }, |
| 39 | toolRouter: { |
| 40 | execute: (sessionId, params, options) => |
| 41 | stub.client.toolRouter.session.execute(sessionId, params, options), |
| 42 | }, |
| 43 | }); |
| 44 | return { requests: stub.requests, testLayer }; |
| 45 | }; |
| 46 | |
| 47 | describe('CLI: composio execute over the API client', () => { |
| 48 | beforeEach(() => { |
| 49 | trackedEvents.length = 0; |
| 50 | }); |
| 51 | |
| 52 | const rejected = executingOver(() => |
| 53 | Response.json( |
| 54 | { |
| 55 | error: { |
| 56 | message: 'The upstream app refused the request', |
| 57 | code: 4310, |
| 58 | slug: 'ToolRouterV2_UpstreamRefused', |
| 59 | status: 400, |
| 60 | }, |
| 61 | }, |
| 62 | { status: 400, headers: { 'x-request-id': 'req_refused' } } |
| 63 | ) |
| 64 | ); |
| 65 | layer(rejected.testLayer)('[Given] the API rejects the execution with an error envelope', it => { |
| 66 | it.effect('[Then] the failure is printed and tracked with its status, code and slug', () => |
| 67 | Effect.gen(function* () { |
| 68 | const exit = yield* cli(executeArgs).pipe(Effect.exit); |
| 69 | const output = (yield* MockConsole.getLines({ stripAnsi: true })).join('\n'); |
| 70 | |
| 71 | expect(Exit.isFailure(exit)).toBe(true); |
| 72 | expect(output).toContain('The upstream app refused the request'); |
| 73 | expect(output).toContain('Error details'); |
| 74 | expect(eventsNamed(CLI_ANALYTICS_EVENTS.CLI_TOOL_INVOCATION_FAILED)).toMatchObject([ |
| 75 | { |
| 76 | properties: { |
| 77 | tool_slug: 'GITHUB_STAR_REPO', |
| 78 | http_status: 400, |
| 79 | api_error_code: 4310, |
| 80 | error_slug: 'ToolRouterV2_UpstreamRefused', |
| 81 | }, |
| 82 | }, |
| 83 | ]); |
| 84 | }) |
| 85 | ); |
| 86 | }); |
| 87 | |
| 88 | const unavailable = executingOver(() => |
| 89 | Response.json( |
| 90 | { error: { message: 'Internal error', code: 500, slug: 'Internal', status: 500 } }, |
| 91 | // A retry would wait this long, so one that did happen shows up as a second request. |
| 92 | { status: 500, headers: { 'retry-after-ms': '0' } } |
| 93 | ) |
| 94 | ); |
| 95 | // On the live clock: a retry waits on the clock, and under the test clock a |
| 96 | // retried call would hang instead of failing this test. |
| 97 | layer(unavailable.testLayer, { excludeTestServices: true })( |
| 98 | '[Given] the API answers the execution with a 500', |
| 99 | it => { |
| 100 | it.effect('[Then] the execution is attempted once', () => |
| 101 | Effect.gen(function* () { |
| 102 | const exit = yield* cli(executeArgs).pipe(Effect.exit); |
| 103 | |
| 104 | expect(Exit.isFailure(exit)).toBe(true); |
| 105 | expect(unavailable.requests).toMatchObject([ |
| 106 | { method: 'POST', body: { tool_slug: 'GITHUB_STAR_REPO' } }, |
| 107 | ]); |
| 108 | }) |
| 109 | ); |
| 110 | } |
| 111 | ); |
| 112 | |
| 113 | const incomplete = executingOver(() => |
| 114 | // No `log_id`, which the API's response schema requires. |
| 115 | Response.json({ result_type: 'completed', data: { starred: true }, error: null }) |
| 116 | ); |
| 117 | layer(incomplete.testLayer)('[Given] the execution response lacks a required field', it => { |
| 118 | it.effect('[Then] the result is still reported', () => |
| 119 | Effect.gen(function* () { |
| 120 | yield* cli(executeArgs); |
| 121 | const lines = yield* MockConsole.getLines({ stripAnsi: true }); |
| 122 | const result = lines.map(line => line.trim()).findLast(line => line.startsWith('{')); |
| 123 | |
| 124 | expect(JSON.parse(result ?? '')).toMatchObject({ |
| 125 | successful: true, |
| 126 | data: { starred: true }, |
| 127 | }); |
| 128 | }) |
| 129 | ); |
| 130 | }); |
| 131 | }); |