Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 04:45 UTC
| 1 | import { describe, expect, layer } from '@effect/vitest'; |
| 2 | import { ConfigProvider, DateTime, Effect } from 'effect'; |
| 3 | import type { ToolkitDetailed } from 'src/models/toolkits'; |
| 4 | import { cli, cliExit, MockConsole, TestLive } from 'test/__utils__'; |
| 5 | import { |
| 6 | DEFAULT_PROJECT_SCOPE, |
| 7 | defaultDeveloperProjectAccountData, |
| 8 | DIRECTORY_PROJECT_SCOPE, |
| 9 | inDeveloperProjectDirectory, |
| 10 | loginWithoutDeveloperProject, |
| 11 | NO_DIRECTORY_PROJECT_NOTICE, |
| 12 | OTHER_DEVELOPER_PROJECT, |
| 13 | OTHER_PROJECT_SCOPE, |
| 14 | } from 'test/__utils__/layers/developer-project'; |
| 15 | import { routeRequests, stubClientResources } from 'test/__utils__/services/stub-composio-client'; |
| 16 | |
| 17 | const gmail: ToolkitDetailed = { |
| 18 | name: 'Gmail', |
| 19 | slug: 'gmail', |
| 20 | is_local_toolkit: false, |
| 21 | composio_managed_auth_schemes: ['OAUTH2'], |
| 22 | no_auth: false, |
| 23 | meta: { |
| 24 | description: 'Email service to send and receive emails', |
| 25 | categories: [], |
| 26 | created_at: DateTime.makeUnsafe('2024-05-03T11:44:32.061Z'), |
| 27 | updated_at: DateTime.makeUnsafe('2024-05-03T11:44:32.061Z'), |
| 28 | available_versions: ['20250101'], |
| 29 | tools_count: 36, |
| 30 | triggers_count: 2, |
| 31 | }, |
| 32 | auth_config_details: [], |
| 33 | }; |
| 34 | |
| 35 | const testLayer = TestLive({ |
| 36 | baseConfigProvider: ConfigProvider.fromEnv({ env: { COMPOSIO_USER_API_KEY: 'test_api_key' } }), |
| 37 | fixture: 'global-test-user-id', |
| 38 | toolkitsData: { detailedToolkits: [gmail] }, |
| 39 | }); |
| 40 | |
| 41 | const SESSION_TOOLKITS = '/api/v3.1/tool_router/session/trs_stub_session/toolkits'; |
| 42 | |
| 43 | /** Session toolkit reads answered by `respond`; the session they run in is created normally. */ |
| 44 | const toolkitsOver = (respond: () => Response) => |
| 45 | stubClientResources( |
| 46 | ['toolRouter'], |
| 47 | routeRequests([{ method: 'GET', path: SESSION_TOOLKITS, respond }], () => |
| 48 | Response.json({ |
| 49 | session_id: 'trs_stub_session', |
| 50 | config: { user_id: 'global-default' }, |
| 51 | mcp: { type: 'http', url: 'https://mcp.stub.invalid' }, |
| 52 | tool_router_tools: [], |
| 53 | }) |
| 54 | ) |
| 55 | ); |
| 56 | |
| 57 | describe('CLI: composio dev toolkits info over the API client', () => { |
| 58 | layer(testLayer)('[Given] a session toolkit in a shape the API contract does not allow', it => { |
| 59 | it.effect('[Then] the toolkit and its connection are shown', () => |
| 60 | Effect.gen(function* () { |
| 61 | // No `meta.logo`, no page counts, and a connected account without |
| 62 | // `created_at` or `auth_config`. |
| 63 | const page = { |
| 64 | items: [ |
| 65 | { |
| 66 | name: 'Gmail', |
| 67 | slug: 'gmail', |
| 68 | enabled: true, |
| 69 | is_no_auth: false, |
| 70 | composio_managed_auth_schemes: ['OAUTH2'], |
| 71 | meta: { description: 'Email service to send and receive emails' }, |
| 72 | connected_account: { id: 'ca_gmail_1', user_id: 'global-default', status: 'ACTIVE' }, |
| 73 | }, |
| 74 | ], |
| 75 | next_cursor: null, |
| 76 | }; |
| 77 | const stub = toolkitsOver(() => Response.json(page)); |
| 78 | yield* cli(['dev', 'toolkits', 'info', 'gmail']).pipe(Effect.provide(stub.layer)); |
| 79 | const lines = yield* MockConsole.getLines({ stripAnsi: true }); |
| 80 | const output = lines.join('\n'); |
| 81 | |
| 82 | expect(output).toContain('Connection Status: ACTIVE'); |
| 83 | expect(output).toContain('Connected Account ID: ca_gmail_1'); |
| 84 | expect(output).toContain('Auth Scheme: -'); |
| 85 | expect(JSON.parse(lines.at(-1) ?? '{}')).toMatchObject({ |
| 86 | slug: 'gmail', |
| 87 | connected_account: { |
| 88 | status: 'ACTIVE', |
| 89 | id: 'ca_gmail_1', |
| 90 | auth_scheme: null, |
| 91 | is_composio_managed: null, |
| 92 | }, |
| 93 | }); |
| 94 | expect(stub.requests.filter(request => request.path.startsWith(SESSION_TOOLKITS))).toEqual([ |
| 95 | { method: 'GET', path: `${SESSION_TOOLKITS}?toolkits=gmail` }, |
| 96 | ]); |
| 97 | }) |
| 98 | ); |
| 99 | }); |
| 100 | }); |
| 101 | |
| 102 | // The connection status comes from a session in the developer project the command resolved, |
| 103 | // never in the one the backend would pick for the key. |
| 104 | describe('CLI: composio dev toolkits info reads connection status in the developer project', () => { |
| 105 | const config = ConfigProvider.fromEnv({ env: { COMPOSIO_USER_API_KEY: 'test_api_key' } }); |
| 106 | const gmailPage = () => |
| 107 | Response.json({ |
| 108 | items: [ |
| 109 | { |
| 110 | name: 'Gmail', |
| 111 | slug: 'gmail', |
| 112 | enabled: true, |
| 113 | is_no_auth: false, |
| 114 | composio_managed_auth_schemes: ['OAUTH2'], |
| 115 | meta: { description: 'Email service to send and receive emails', logo: '' }, |
| 116 | connected_account: null, |
| 117 | }, |
| 118 | ], |
| 119 | next_cursor: null, |
| 120 | }); |
| 121 | |
| 122 | layer( |
| 123 | TestLive({ |
| 124 | ...inDeveloperProjectDirectory, |
| 125 | baseConfigProvider: config, |
| 126 | toolkitsData: { detailedToolkits: [gmail] }, |
| 127 | }) |
| 128 | )(it => { |
| 129 | it.effect.each([ |
| 130 | { given: 'a directory set up for a project', args: [], scope: DIRECTORY_PROJECT_SCOPE }, |
| 131 | { |
| 132 | given: '--project-name', |
| 133 | args: ['--project-name', OTHER_DEVELOPER_PROJECT.name], |
| 134 | scope: OTHER_PROJECT_SCOPE, |
| 135 | }, |
| 136 | ])('[Given] $given [Then] opens its session in that project', ({ args, scope }) => |
| 137 | Effect.gen(function* () { |
| 138 | const stub = toolkitsOver(gmailPage); |
| 139 | yield* cli(['dev', 'toolkits', 'info', 'gmail', ...args]).pipe(Effect.provide(stub.layer)); |
| 140 | |
| 141 | expect(stub.requests.map(request => request.path)).toEqual([ |
| 142 | '/api/v3.1/tool_router/session', |
| 143 | `${SESSION_TOOLKITS}?toolkits=gmail`, |
| 144 | ]); |
| 145 | expect(stub.scopes).toEqual([scope, scope]); |
| 146 | }) |
| 147 | ); |
| 148 | }); |
| 149 | |
| 150 | layer( |
| 151 | TestLive({ |
| 152 | baseConfigProvider: config, |
| 153 | toolkitsData: { detailedToolkits: [gmail] }, |
| 154 | accountData: defaultDeveloperProjectAccountData(), |
| 155 | }) |
| 156 | )(it => { |
| 157 | it.effect( |
| 158 | '[Given] --user-id in a directory set up for no project [Then] reads its status in the default developer project, saying so', |
| 159 | () => |
| 160 | Effect.gen(function* () { |
| 161 | yield* loginWithoutDeveloperProject; |
| 162 | const stub = toolkitsOver(gmailPage); |
| 163 | const { exitCode } = yield* cliExit([ |
| 164 | 'dev', |
| 165 | 'toolkits', |
| 166 | 'info', |
| 167 | 'gmail', |
| 168 | '--user-id', |
| 169 | 'alice', |
| 170 | ]).pipe(Effect.provide(stub.layer)); |
| 171 | const lines = yield* MockConsole.getLines({ stripAnsi: true }); |
| 172 | |
| 173 | expect(exitCode).toBe(0); |
| 174 | expect(stub.scopes).toEqual([DEFAULT_PROJECT_SCOPE, DEFAULT_PROJECT_SCOPE]); |
| 175 | expect(lines).toContain(NO_DIRECTORY_PROJECT_NOTICE); |
| 176 | expect(JSON.parse(lines.at(-1) ?? '{}')).toMatchObject({ slug: 'gmail' }); |
| 177 | }) |
| 178 | ); |
| 179 | }); |
| 180 | |
| 181 | layer( |
| 182 | TestLive({ |
| 183 | baseConfigProvider: config, |
| 184 | toolkitsData: { detailedToolkits: [gmail] }, |
| 185 | }) |
| 186 | )(it => { |
| 187 | it.effect( |
| 188 | '[Given] no user in a directory set up for no project [Then] shows the toolkit without connection status', |
| 189 | () => |
| 190 | Effect.gen(function* () { |
| 191 | yield* loginWithoutDeveloperProject; |
| 192 | const stub = toolkitsOver(gmailPage); |
| 193 | const { exitCode } = yield* cliExit(['dev', 'toolkits', 'info', 'gmail']).pipe( |
| 194 | Effect.provide(stub.layer) |
| 195 | ); |
| 196 | const lines = yield* MockConsole.getLines({ stripAnsi: true }); |
| 197 | |
| 198 | expect(exitCode).toBe(0); |
| 199 | expect(stub.requests).toEqual([]); |
| 200 | expect(lines).toContain( |
| 201 | 'No developer project to read connection status from; showing toolkit details without it. Run `composio dev init`, or pass --user-id or --project-name.' |
| 202 | ); |
| 203 | expect(lines).not.toContain(NO_DIRECTORY_PROJECT_NOTICE); |
| 204 | expect(JSON.parse(lines.at(-1) ?? '{}')).toMatchObject({ slug: 'gmail' }); |
| 205 | }) |
| 206 | ); |
| 207 | }); |
| 208 | }); |