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, it } from '@effect/vitest'; |
| 2 | import { Effect, Option } from 'effect'; |
| 3 | import { |
| 4 | buildAuthConfigCreateRequest, |
| 5 | parseScopes, |
| 6 | runAuthConfigsCreate, |
| 7 | type AuthConfigsCreateParams, |
| 8 | } from 'src/commands/auth-configs/create/auth-configs.create.handler'; |
| 9 | import { isUsageFailure } from 'src/commands/errors'; |
| 10 | import { MockConsole } from 'test/__utils__'; |
| 11 | import { exitCodeOf } from 'test/__utils__/exit-code'; |
| 12 | import { AuthConfigsHandlerTest } from 'test/__utils__/layers/auth-configs'; |
| 13 | import { notFoundError } from 'test/__utils__/layers/not-found'; |
| 14 | |
| 15 | const params: AuthConfigsCreateParams = { |
| 16 | name: Option.none(), |
| 17 | toolkit: 'gmail', |
| 18 | authScheme: Option.none(), |
| 19 | scopes: Option.none(), |
| 20 | customCredentials: Option.none(), |
| 21 | }; |
| 22 | |
| 23 | const createResponse = { |
| 24 | auth_config: { id: 'ac_created', auth_scheme: 'OAUTH2', is_composio_managed: false }, |
| 25 | toolkit: { slug: 'gmail' }, |
| 26 | }; |
| 27 | |
| 28 | describe('parseScopes', () => { |
| 29 | it.each([ |
| 30 | { name: 'no flag', input: Option.none<string>(), expected: undefined }, |
| 31 | { name: 'one scope', input: Option.some('send_email'), expected: ['send_email'] }, |
| 32 | { |
| 33 | name: 'padded scopes and blanks', |
| 34 | input: Option.some(' send_email, ,read_email ,'), |
| 35 | expected: ['send_email', 'read_email'], |
| 36 | }, |
| 37 | // An empty flag still sends an empty list, as before the move. |
| 38 | { name: 'an empty flag', input: Option.some(''), expected: [] }, |
| 39 | ])('[Given] $name [Then] returns $expected', ({ input, expected }) => { |
| 40 | expect(parseScopes(input)).toEqual(expected); |
| 41 | }); |
| 42 | }); |
| 43 | |
| 44 | describe('buildAuthConfigCreateRequest', () => { |
| 45 | const base = { |
| 46 | name: undefined, |
| 47 | toolkit: 'gmail', |
| 48 | authScheme: Option.none<string>(), |
| 49 | scopes: undefined, |
| 50 | customCredentials: undefined, |
| 51 | }; |
| 52 | |
| 53 | it.each([ |
| 54 | { |
| 55 | name: 'no scheme', |
| 56 | input: { ...base, name: 'mine' }, |
| 57 | expected: { |
| 58 | toolkit: { slug: 'gmail' }, |
| 59 | auth_config: { type: 'use_composio_managed_auth', name: 'mine', credentials: undefined }, |
| 60 | }, |
| 61 | }, |
| 62 | { |
| 63 | name: 'no scheme with scopes and custom credentials', |
| 64 | input: { ...base, scopes: ['send_email'], customCredentials: { client_id: 'ignored' } }, |
| 65 | expected: { |
| 66 | toolkit: { slug: 'gmail' }, |
| 67 | auth_config: { |
| 68 | type: 'use_composio_managed_auth', |
| 69 | name: undefined, |
| 70 | credentials: { scopes: ['send_email'] }, |
| 71 | }, |
| 72 | }, |
| 73 | }, |
| 74 | { |
| 75 | name: 'a scheme alone', |
| 76 | input: { ...base, authScheme: Option.some('API_KEY') }, |
| 77 | expected: { |
| 78 | toolkit: { slug: 'gmail' }, |
| 79 | auth_config: { |
| 80 | type: 'use_custom_auth', |
| 81 | authScheme: 'API_KEY', |
| 82 | name: undefined, |
| 83 | credentials: undefined, |
| 84 | }, |
| 85 | }, |
| 86 | }, |
| 87 | { |
| 88 | name: 'a scheme with custom credentials and scopes', |
| 89 | input: { |
| 90 | ...base, |
| 91 | authScheme: Option.some('OAUTH2'), |
| 92 | scopes: ['send_email'], |
| 93 | customCredentials: { client_id: 'id', scopes: ['overridden'] }, |
| 94 | }, |
| 95 | expected: { |
| 96 | toolkit: { slug: 'gmail' }, |
| 97 | auth_config: { |
| 98 | type: 'use_custom_auth', |
| 99 | authScheme: 'OAUTH2', |
| 100 | name: undefined, |
| 101 | credentials: { client_id: 'id', scopes: ['send_email'] }, |
| 102 | }, |
| 103 | }, |
| 104 | }, |
| 105 | ])('[Given] $name [Then] builds the matching request', ({ input, expected }) => { |
| 106 | expect(buildAuthConfigCreateRequest(input)).toStrictEqual(expected); |
| 107 | }); |
| 108 | }); |
| 109 | |
| 110 | describe('runAuthConfigsCreate', () => { |
| 111 | it.effect('[Given] a successful create [Then] prints the API response JSON to stdout', () => { |
| 112 | const { layer, outputs } = AuthConfigsHandlerTest({ |
| 113 | authConfigsData: { createResponse }, |
| 114 | }); |
| 115 | return Effect.gen(function* () { |
| 116 | yield* runAuthConfigsCreate(params); |
| 117 | |
| 118 | expect(outputs).toEqual([JSON.stringify(createResponse, null, 2)]); |
| 119 | const decoration = (yield* MockConsole.getLines({ stripAnsi: true })).join('\n'); |
| 120 | expect(decoration).toContain('Auth config created.'); |
| 121 | expect(decoration).toContain('[New Auth Config]'); |
| 122 | }).pipe(Effect.provide(layer)); |
| 123 | }); |
| 124 | |
| 125 | it.effect.each([ |
| 126 | { name: 'malformed JSON', customCredentials: '{invalid json}' }, |
| 127 | { name: 'a JSON array', customCredentials: '["client_id"]' }, |
| 128 | ])( |
| 129 | '[Given] $name in --custom-credentials [Then] fails with a usage error before any request', |
| 130 | ({ customCredentials }) => { |
| 131 | const { layer, outputs } = AuthConfigsHandlerTest({ |
| 132 | repository: { createAuthConfig: () => Effect.die('createAuthConfig must not be called') }, |
| 133 | }); |
| 134 | return Effect.gen(function* () { |
| 135 | const failure = yield* runAuthConfigsCreate({ |
| 136 | ...params, |
| 137 | authScheme: Option.some('OAUTH2'), |
| 138 | customCredentials: Option.some(customCredentials), |
| 139 | }).pipe(Effect.flip); |
| 140 | |
| 141 | expect(failure).toMatchObject({ |
| 142 | _tag: 'commands/CommandInputError', |
| 143 | command: 'dev auth-configs create', |
| 144 | option: '--custom-credentials', |
| 145 | message: expect.stringMatching( |
| 146 | /^Invalid JSON in --custom-credentials\. Please provide valid JSON\.\nExample:\n> composio dev auth-configs create / |
| 147 | ), |
| 148 | }); |
| 149 | // The invocation prints a usage failure on stderr and exits 1. |
| 150 | expect(isUsageFailure(failure)).toBe(true); |
| 151 | expect(yield* MockConsole.getLines()).toEqual([]); |
| 152 | expect(outputs).toEqual([]); |
| 153 | }).pipe(Effect.provide(layer)); |
| 154 | } |
| 155 | ); |
| 156 | |
| 157 | it.effect('[Given] the API rejects the create [Then] hints at the toolkit and exits 1', () => { |
| 158 | const { layer, outputs } = AuthConfigsHandlerTest({ |
| 159 | repository: { |
| 160 | createAuthConfig: () => Effect.fail(notFoundError('Toolkit not found')), |
| 161 | }, |
| 162 | }); |
| 163 | return Effect.gen(function* () { |
| 164 | const exit = yield* Effect.exit(runAuthConfigsCreate({ ...params, toolkit: 'nope' })); |
| 165 | |
| 166 | const stderr = (yield* MockConsole.getLines({ stream: 'stderr', stripAnsi: true })).join( |
| 167 | '\n' |
| 168 | ); |
| 169 | expect(stderr).toContain('Failed to create auth config.'); |
| 170 | expect(stderr).toContain( |
| 171 | 'Check available auth schemes for "nope":\n> composio dev toolkits info "nope"' |
| 172 | ); |
| 173 | expect(outputs).toEqual([]); |
| 174 | expect(exitCodeOf(exit)).toBe(1); |
| 175 | }).pipe(Effect.provide(layer)); |
| 176 | }); |
| 177 | }); |