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 { TestClock } from 'effect/testing'; |
| 4 | import { cli, cliExit, MockConsole, TestLive } from 'test/__utils__'; |
| 5 | import { |
| 6 | DIRECTORY_PROJECT_SCOPE, |
| 7 | DEFAULT_PROJECT_SCOPE, |
| 8 | defaultDeveloperProjectAccountData, |
| 9 | inDeveloperProjectDirectory, |
| 10 | loginWithoutDeveloperProject, |
| 11 | OTHER_DEVELOPER_PROJECT, |
| 12 | OTHER_PROJECT_SCOPE, |
| 13 | } from 'test/__utils__/layers/developer-project'; |
| 14 | import { stubClientResources } from 'test/__utils__/services/stub-composio-client'; |
| 15 | import { log } from './logs.tools.fixtures'; |
| 16 | |
| 17 | const testLayer = TestLive({ |
| 18 | baseConfigProvider: ConfigProvider.fromEnv({ env: { COMPOSIO_USER_API_KEY: 'test_api_key' } }), |
| 19 | fixture: 'global-test-user-id', |
| 20 | }); |
| 21 | |
| 22 | describe('CLI: composio dev logs tools', () => { |
| 23 | layer(testLayer)(it => { |
| 24 | it.effect( |
| 25 | '[Given] every supported filter [Then] searches the public API with at most 100 results', |
| 26 | () => |
| 27 | Effect.gen(function* () { |
| 28 | const stub = stubClientResources(['logs'], () => |
| 29 | Response.json({ logs: [], next_cursor: null }) |
| 30 | ); |
| 31 | yield* cli([ |
| 32 | 'dev', |
| 33 | 'logs', |
| 34 | 'tools', |
| 35 | '--tool-router-session-id', |
| 36 | 'trs_1', |
| 37 | '--log-id', |
| 38 | 'log_a', |
| 39 | '--user-id', |
| 40 | 'user_1', |
| 41 | '--status', |
| 42 | 'failed', |
| 43 | '--auth-config-id', |
| 44 | 'ac_1', |
| 45 | '--connected-account-id', |
| 46 | 'ca_1', |
| 47 | '--toolkit', |
| 48 | 'gmail', |
| 49 | '--tool', |
| 50 | 'GMAIL_SEND_EMAIL', |
| 51 | '--cursor', |
| 52 | '7', |
| 53 | '--from', |
| 54 | '1', |
| 55 | '--to', |
| 56 | '2', |
| 57 | '--limit', |
| 58 | '5000', |
| 59 | ]).pipe(Effect.provide(stub.layer)); |
| 60 | expect(stub.requests).toEqual([ |
| 61 | { |
| 62 | method: 'POST', |
| 63 | path: '/api/v3.1/logs/tool_execution', |
| 64 | body: { |
| 65 | cursor: '7', |
| 66 | limit: 100, |
| 67 | time_range: { from: 1, to: 2 }, |
| 68 | filters: [ |
| 69 | { field: 'tool_slug', operator: '==', value: 'GMAIL_SEND_EMAIL' }, |
| 70 | { field: 'toolkit_slug', operator: '==', value: 'gmail' }, |
| 71 | { field: 'connected_account_id', operator: '==', value: 'ca_1' }, |
| 72 | { field: 'auth_config_id', operator: '==', value: 'ac_1' }, |
| 73 | { field: 'status', operator: '==', value: 'failed' }, |
| 74 | { field: 'user_id', operator: '==', value: 'user_1' }, |
| 75 | { field: 'log_id', operator: '==', value: 'log_a' }, |
| 76 | { field: 'session_id', operator: '==', value: 'trs_1' }, |
| 77 | ], |
| 78 | }, |
| 79 | }, |
| 80 | ]); |
| 81 | }) |
| 82 | ); |
| 83 | |
| 84 | it.effect.each([ |
| 85 | { args: ['--limit', '0'], expected: { limit: 1 } }, |
| 86 | { args: ['--from', '10'], expected: { time_range: { from: 10, to: 1000 } } }, |
| 87 | { args: ['--to', '20'], expected: { time_range: { from: 0, to: 20 } } }, |
| 88 | ])('[Given] $args [Then] builds a bounded public request', ({ args, expected }) => |
| 89 | Effect.gen(function* () { |
| 90 | yield* TestClock.setTime(1000); |
| 91 | const stub = stubClientResources(['logs'], () => Response.json({ logs: [] })); |
| 92 | yield* cli(['dev', 'logs', 'tools', ...args]).pipe(Effect.provide(stub.layer)); |
| 93 | expect(stub.requests[0]?.body).toMatchObject(expected); |
| 94 | }) |
| 95 | ); |
| 96 | |
| 97 | it.effect.each([ |
| 98 | { args: ['dev', 'logs', 'tools', '--user-id', 'user_1'] }, |
| 99 | { args: ['dev', '--user-id', 'user_1', 'logs', 'tools'] }, |
| 100 | ])( |
| 101 | '[Given] a shared user filter at either position [Then] includes the user in the public filters', |
| 102 | ({ args }) => |
| 103 | Effect.gen(function* () { |
| 104 | const stub = stubClientResources(['logs'], () => Response.json({ logs: [] })); |
| 105 | yield* cli(args).pipe(Effect.provide(stub.layer)); |
| 106 | expect(stub.requests[0]?.body).toMatchObject({ |
| 107 | filters: [{ field: 'user_id', operator: '==', value: 'user_1' }], |
| 108 | }); |
| 109 | }) |
| 110 | ); |
| 111 | |
| 112 | it.effect.each([ |
| 113 | { args: ['--case-sensitive'], option: '--case-sensitive' }, |
| 114 | { args: ['--session-id', 'cli_session'], option: '--session-id' }, |
| 115 | { args: ['--status', 'unknown'], option: '--status' }, |
| 116 | { args: ['--status', 'success,failed'], option: '--status' }, |
| 117 | { args: ['--toolkit', 'gmail,slack'], option: '--toolkit' }, |
| 118 | { args: ['--log-id', 'log_a,log_b'], option: '--log-id' }, |
| 119 | { args: ['--user-id', 'user_1,user_2'], option: '--user-id' }, |
| 120 | ])( |
| 121 | '[Given] $args [Then] rejects unsupported filtering before requesting logs', |
| 122 | ({ args, option }) => |
| 123 | Effect.gen(function* () { |
| 124 | const stub = stubClientResources(['logs'], () => Response.json({ logs: [] })); |
| 125 | const failure = yield* cli(['dev', 'logs', 'tools', ...args]).pipe( |
| 126 | Effect.provide(stub.layer), |
| 127 | Effect.flip |
| 128 | ); |
| 129 | expect(failure).toMatchObject({ _tag: 'commands/CommandInputError', option }); |
| 130 | expect(stub.requests).toEqual([]); |
| 131 | }) |
| 132 | ); |
| 133 | |
| 134 | it.effect( |
| 135 | '[Given] an empty public page [Then] warns on stderr and returns the page on stdout', |
| 136 | () => |
| 137 | Effect.gen(function* () { |
| 138 | const stdoutBefore = (yield* MockConsole.getLines({ stream: 'stdout' })).length; |
| 139 | const page = { logs: [], next_cursor: null }; |
| 140 | const stub = stubClientResources(['logs'], () => Response.json(page)); |
| 141 | yield* cli(['dev', 'logs', 'tools']).pipe(Effect.provide(stub.layer)); |
| 142 | expect( |
| 143 | (yield* MockConsole.getLines({ stream: 'stderr', stripAnsi: true })).join('\n') |
| 144 | ).toContain('No tool logs found for the given filters.'); |
| 145 | expect((yield* MockConsole.getLines({ stream: 'stdout' })).slice(stdoutBefore)).toEqual([ |
| 146 | JSON.stringify(page, null, 2), |
| 147 | ]); |
| 148 | expect(stub.requests).toEqual([ |
| 149 | { |
| 150 | method: 'POST', |
| 151 | path: '/api/v3.1/logs/tool_execution', |
| 152 | body: { cursor: null, limit: 30 }, |
| 153 | }, |
| 154 | ]); |
| 155 | }) |
| 156 | ); |
| 157 | |
| 158 | it.effect('[Given] a page cursor [Then] displays it and sends it back unchanged', () => |
| 159 | Effect.gen(function* () { |
| 160 | const stdoutBefore = (yield* MockConsole.getLines({ stream: 'stdout' })).length; |
| 161 | const page = { logs: [log], next_cursor: '1767225600000' }; |
| 162 | const stub = stubClientResources(['logs'], () => Response.json(page)); |
| 163 | yield* cli(['dev', 'logs', 'tools']).pipe(Effect.provide(stub.layer)); |
| 164 | const stderr = yield* MockConsole.getLines({ stream: 'stderr', stripAnsi: true }); |
| 165 | expect(stderr).toContain('Next cursor: 1767225600000'); |
| 166 | expect(stderr).toContain( |
| 167 | 'To view full details for a log:\n> composio dev logs tools "log_send_1"' |
| 168 | ); |
| 169 | expect((yield* MockConsole.getLines({ stream: 'stdout' })).slice(stdoutBefore)).toEqual([ |
| 170 | JSON.stringify(page, null, 2), |
| 171 | ]); |
| 172 | yield* cli(['dev', 'logs', 'tools', '--cursor', page.next_cursor]).pipe( |
| 173 | Effect.provide(stub.layer) |
| 174 | ); |
| 175 | expect(stub.requests[1]?.body).toMatchObject({ cursor: page.next_cursor }); |
| 176 | }) |
| 177 | ); |
| 178 | }); |
| 179 | |
| 180 | layer(TestLive())(it => { |
| 181 | it.effect('[Given] no API key [Then] fails with NotLoggedInError', () => |
| 182 | Effect.gen(function* () { |
| 183 | expect(yield* cli(['dev', 'logs', 'tools']).pipe(Effect.flip)).toMatchObject({ |
| 184 | _tag: 'commands/NotLoggedInError', |
| 185 | }); |
| 186 | }) |
| 187 | ); |
| 188 | }); |
| 189 | }); |
| 190 | |
| 191 | // The request goes to the developer project the command resolved, never to the one the backend |
| 192 | // would pick for the key. |
| 193 | describe('CLI: composio dev logs tools acts on the developer project', () => { |
| 194 | const config = ConfigProvider.fromEnv({ env: { COMPOSIO_USER_API_KEY: 'test_api_key' } }); |
| 195 | |
| 196 | layer(TestLive({ ...inDeveloperProjectDirectory, baseConfigProvider: config }))(it => { |
| 197 | it.effect.each([ |
| 198 | { given: 'a directory set up for a project', args: [], scope: DIRECTORY_PROJECT_SCOPE }, |
| 199 | { |
| 200 | given: '--project-name', |
| 201 | args: ['--project-name', OTHER_DEVELOPER_PROJECT.name], |
| 202 | scope: OTHER_PROJECT_SCOPE, |
| 203 | }, |
| 204 | ])('[Given] $given [Then] reads the logs of that project', ({ args, scope }) => |
| 205 | Effect.gen(function* () { |
| 206 | const stub = stubClientResources(['logs'], () => Response.json({ logs: [], next_cursor: null })); |
| 207 | yield* cli([...['dev', 'logs', 'tools'], ...args]).pipe(Effect.provide(stub.layer)); |
| 208 | |
| 209 | expect(stub.scopes).toEqual([scope]); |
| 210 | }) |
| 211 | ); |
| 212 | }); |
| 213 | |
| 214 | layer(TestLive({ baseConfigProvider: config, accountData: defaultDeveloperProjectAccountData() }))(it => { |
| 215 | it.effect('[Given] a directory set up for no project [Then] reads the logs of the default developer project', () => |
| 216 | Effect.gen(function* () { |
| 217 | yield* loginWithoutDeveloperProject; |
| 218 | const stub = stubClientResources(['logs'], () => Response.json({ logs: [], next_cursor: null })); |
| 219 | const { exitCode } = yield* cliExit(['dev', 'logs', 'tools']).pipe(Effect.provide(stub.layer)); |
| 220 | |
| 221 | expect(exitCode).toBe(0); |
| 222 | expect(stub.scopes).toEqual([DEFAULT_PROJECT_SCOPE]); |
| 223 | }) |
| 224 | ); |
| 225 | }); |
| 226 | }); |