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, 06:00 UTC
| 1 | import { assert, describe, expect, layer } from '@effect/vitest'; |
| 2 | import { isUsageFailure } from 'src/commands/errors'; |
| 3 | import { ConfigProvider, Effect } from 'effect'; |
| 4 | import { cli, TestLive, MockConsole } from 'test/__utils__'; |
| 5 | import { exitCodeOf } from 'test/__utils__/exit-code'; |
| 6 | import type { TestLiveInput } from 'test/__utils__/services/test-layer'; |
| 7 | import type { ConnectedAccountItem } from 'src/models/connected-accounts'; |
| 8 | import { |
| 9 | DIRECTORY_PROJECT_SCOPE, |
| 10 | DEFAULT_PROJECT_SCOPE, |
| 11 | defaultDeveloperProjectAccountData, |
| 12 | inDeveloperProjectDirectory, |
| 13 | loginWithoutDeveloperProject, |
| 14 | OTHER_DEVELOPER_PROJECT, |
| 15 | OTHER_PROJECT_SCOPE, |
| 16 | runRecordingRepositoryScopes, |
| 17 | } from 'test/__utils__/layers/developer-project'; |
| 18 | |
| 19 | const testConnectedAccounts: ConnectedAccountItem[] = [ |
| 20 | { |
| 21 | id: 'con_gmail_active', |
| 22 | status: 'ACTIVE', |
| 23 | status_reason: null, |
| 24 | is_disabled: false, |
| 25 | user_id: 'default', |
| 26 | toolkit: { slug: 'gmail' }, |
| 27 | auth_config: { |
| 28 | id: 'ac_gmail_oauth', |
| 29 | auth_scheme: 'OAUTH2', |
| 30 | is_composio_managed: true, |
| 31 | is_disabled: false, |
| 32 | }, |
| 33 | created_at: '2026-01-01T00:00:00Z', |
| 34 | updated_at: '2026-01-15T00:00:00Z', |
| 35 | test_request_endpoint: 'https://gmail.googleapis.com/gmail/v1/users/me/profile', |
| 36 | }, |
| 37 | { |
| 38 | id: 'con_github_expired', |
| 39 | status: 'EXPIRED', |
| 40 | status_reason: 'Token expired', |
| 41 | is_disabled: false, |
| 42 | user_id: 'default', |
| 43 | toolkit: { slug: 'github' }, |
| 44 | auth_config: { |
| 45 | id: 'ac_github_oauth', |
| 46 | auth_scheme: 'OAUTH2', |
| 47 | is_composio_managed: true, |
| 48 | is_disabled: false, |
| 49 | }, |
| 50 | created_at: '2026-02-01T00:00:00Z', |
| 51 | updated_at: '2026-02-10T00:00:00Z', |
| 52 | test_request_endpoint: '', |
| 53 | }, |
| 54 | ]; |
| 55 | |
| 56 | const connectedAccountsData = { |
| 57 | items: testConnectedAccounts, |
| 58 | } satisfies TestLiveInput['connectedAccountsData']; |
| 59 | |
| 60 | const testConfigProvider = ConfigProvider.fromEnv({ |
| 61 | env: { COMPOSIO_USER_API_KEY: 'test_api_key' }, |
| 62 | }); |
| 63 | |
| 64 | // Logged in, in a directory set up for a developer project. |
| 65 | const inProject = { ...inDeveloperProjectDirectory, baseConfigProvider: testConfigProvider }; |
| 66 | |
| 67 | describe('CLI: composio dev connected-accounts whoami', () => { |
| 68 | layer(TestLive({ ...inProject, connectedAccountsData }))( |
| 69 | '[Given] active connection [Then] shows account details', |
| 70 | it => { |
| 71 | it.effect('displays whoami for active connection', () => |
| 72 | Effect.gen(function* () { |
| 73 | yield* cli(['dev', 'connected-accounts', 'whoami', 'con_gmail_active']); |
| 74 | const lines = yield* MockConsole.getLines({ stripAnsi: true }); |
| 75 | const output = lines.join('\n'); |
| 76 | |
| 77 | expect(output).toContain('con_gmail_active'); |
| 78 | expect(output).toContain('gmail'); |
| 79 | expect(output).toContain('default'); |
| 80 | expect(output).toContain('ACTIVE'); |
| 81 | }) |
| 82 | ); |
| 83 | } |
| 84 | ); |
| 85 | |
| 86 | layer(TestLive({ ...inProject, connectedAccountsData }))( |
| 87 | '[Given] expired connection [Then] shows details with status warning', |
| 88 | it => { |
| 89 | it.effect('shows warning for non-ACTIVE connection', () => |
| 90 | Effect.gen(function* () { |
| 91 | yield* cli(['dev', 'connected-accounts', 'whoami', 'con_github_expired']); |
| 92 | const lines = yield* MockConsole.getLines({ stripAnsi: true }); |
| 93 | const output = lines.join('\n'); |
| 94 | |
| 95 | expect(output).toContain('con_github_expired'); |
| 96 | expect(output).toContain('EXPIRED'); |
| 97 | expect(output).toContain('status EXPIRED'); |
| 98 | }) |
| 99 | ); |
| 100 | } |
| 101 | ); |
| 102 | |
| 103 | layer(TestLive({ ...inProject, connectedAccountsData }))( |
| 104 | '[Given] nonexistent ID [Then] shows error with hint', |
| 105 | it => { |
| 106 | it.effect('shows error for nonexistent account', () => |
| 107 | Effect.gen(function* () { |
| 108 | const exit = yield* Effect.exit( |
| 109 | cli(['dev', 'connected-accounts', 'whoami', 'con_nonexistent']) |
| 110 | ); |
| 111 | const lines = yield* MockConsole.getLines({ stripAnsi: true }); |
| 112 | const output = lines.join('\n'); |
| 113 | |
| 114 | expect(output).toContain('not found'); |
| 115 | expect(output).toContain('composio dev connected-accounts list'); |
| 116 | expect(exitCodeOf(exit)).toBe(1); |
| 117 | }) |
| 118 | ); |
| 119 | } |
| 120 | ); |
| 121 | |
| 122 | layer(TestLive({ ...inProject, connectedAccountsData }))( |
| 123 | '[Given] no ID argument [Then] fails with a usage error', |
| 124 | it => { |
| 125 | it.effect('raises a CommandInputError naming <id>', () => |
| 126 | Effect.gen(function* () { |
| 127 | const failure = yield* cli(['dev', 'connected-accounts', 'whoami']).pipe(Effect.flip); |
| 128 | |
| 129 | expect(failure).toMatchObject({ |
| 130 | _tag: 'commands/CommandInputError', |
| 131 | option: '<id>', |
| 132 | message: expect.stringMatching( |
| 133 | /^Missing required argument: <id>\n[^]*composio dev connected-accounts whoami / |
| 134 | ), |
| 135 | }); |
| 136 | // The invocation prints the message on stderr, and nothing else. |
| 137 | assert(isUsageFailure(failure)); |
| 138 | expect(yield* MockConsole.getLines()).toEqual([failure.message]); |
| 139 | }) |
| 140 | ); |
| 141 | } |
| 142 | ); |
| 143 | |
| 144 | layer(TestLive())('[Given] no API key [Then] warns user to login', it => { |
| 145 | it.effect('warns user to login', () => |
| 146 | Effect.gen(function* () { |
| 147 | const error = yield* cli(['dev', 'connected-accounts', 'whoami', 'con_gmail_active']).pipe( |
| 148 | Effect.flip |
| 149 | ); |
| 150 | expect(error).toMatchObject({ _tag: 'commands/NotLoggedInError' }); |
| 151 | }) |
| 152 | ); |
| 153 | }); |
| 154 | }); |
| 155 | |
| 156 | // The request goes to the developer project the command resolved, never to the one the backend |
| 157 | // would pick for the key. |
| 158 | describe('CLI: composio dev connected-accounts whoami acts on the developer project', () => { |
| 159 | layer(TestLive({ ...inProject, connectedAccountsData }))(it => { |
| 160 | it.effect.each([ |
| 161 | { given: 'a directory set up for a project', args: [], scope: DIRECTORY_PROJECT_SCOPE }, |
| 162 | { |
| 163 | given: '--project-name', |
| 164 | args: ['--project-name', OTHER_DEVELOPER_PROJECT.name], |
| 165 | scope: OTHER_PROJECT_SCOPE, |
| 166 | }, |
| 167 | ])('[Given] $given [Then] reads the account from that project', ({ args, scope }) => |
| 168 | Effect.gen(function* () { |
| 169 | const { exitCode, calls } = yield* runRecordingRepositoryScopes([ |
| 170 | ...['dev', 'connected-accounts', 'whoami', 'con_gmail_active'], |
| 171 | ...args, |
| 172 | ]); |
| 173 | |
| 174 | expect(exitCode).toBe(0); |
| 175 | expect(calls).toEqual([ |
| 176 | { method: 'getConnectedAccount', scope: expect.objectContaining(scope) }, |
| 177 | ]); |
| 178 | }) |
| 179 | ); |
| 180 | }); |
| 181 | |
| 182 | layer( |
| 183 | TestLive({ |
| 184 | baseConfigProvider: testConfigProvider, |
| 185 | connectedAccountsData, |
| 186 | accountData: defaultDeveloperProjectAccountData(), |
| 187 | }) |
| 188 | )(it => { |
| 189 | it.effect( |
| 190 | '[Given] a directory set up for no project [Then] acts on the default developer project', |
| 191 | () => |
| 192 | Effect.gen(function* () { |
| 193 | yield* loginWithoutDeveloperProject; |
| 194 | const { exitCode, calls } = yield* runRecordingRepositoryScopes([ |
| 195 | 'dev', |
| 196 | 'connected-accounts', |
| 197 | 'whoami', |
| 198 | 'con_gmail_active', |
| 199 | ]); |
| 200 | |
| 201 | expect(exitCode).toBe(0); |
| 202 | expect(calls).toEqual([ |
| 203 | { |
| 204 | method: 'getConnectedAccount', |
| 205 | scope: expect.objectContaining(DEFAULT_PROJECT_SCOPE), |
| 206 | }, |
| 207 | ]); |
| 208 | }) |
| 209 | ); |
| 210 | }); |
| 211 | }); |