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 { describe, expect, it } from '@effect/vitest'; |
| 2 | import { Console, Effect, Layer, Option } from 'effect'; |
| 3 | import { runConnectedAccountsWhoami } from 'src/commands/connected-accounts/whoami/connected-accounts.whoami.handler'; |
| 4 | import { inactiveStatusMessage } from 'src/commands/connected-accounts/whoami/connected-accounts.whoami.format'; |
| 5 | import { MISSING_ID_MESSAGE, missingIdHint } from 'src/commands/connected-accounts/format'; |
| 6 | import { isUsageFailure } from 'src/commands/errors'; |
| 7 | import type { ConnectedAccountItem } from 'src/models/connected-accounts'; |
| 8 | import { TerminalUI } from 'src/services/terminal-ui'; |
| 9 | import { ConnectedAccountsTest, MockConsole } from 'test/__utils__'; |
| 10 | import { exitCodeOf } from 'test/__utils__/exit-code'; |
| 11 | import { DirectoryProjectTest } from 'test/__utils__/layers/developer-project'; |
| 12 | import { terminalUITestImpl } from 'test/__utils__/services/terminal-ui-test'; |
| 13 | |
| 14 | const account = (overrides: Partial<ConnectedAccountItem> = {}): ConnectedAccountItem => ({ |
| 15 | id: 'con_gmail_active', |
| 16 | alias: 'personal', |
| 17 | word_id: 'harbor', |
| 18 | status: 'ACTIVE', |
| 19 | status_reason: null, |
| 20 | is_disabled: false, |
| 21 | user_id: 'default', |
| 22 | toolkit: { slug: 'gmail' }, |
| 23 | auth_config: { |
| 24 | id: 'ac_gmail_oauth', |
| 25 | auth_scheme: 'OAUTH2', |
| 26 | is_composio_managed: true, |
| 27 | is_disabled: false, |
| 28 | }, |
| 29 | created_at: '2026-01-01T00:00:00Z', |
| 30 | updated_at: '2026-01-15T00:00:00Z', |
| 31 | test_request_endpoint: '', |
| 32 | ...overrides, |
| 33 | }); |
| 34 | |
| 35 | const ACTIVE_ACCOUNT = account(); |
| 36 | const EXPIRED_ACCOUNT = account({ id: 'con_gmail_expired', status: 'EXPIRED' }); |
| 37 | |
| 38 | /** |
| 39 | * What `runConnectedAccountsWhoami` reads: the connected-account repository and a headless |
| 40 | * terminal whose stdout data lands in `outputs`. |
| 41 | */ |
| 42 | const whoamiTest = () => { |
| 43 | const outputs: Array<string> = []; |
| 44 | const layer = Layer.mergeAll( |
| 45 | Layer.effect(Console.Console, MockConsole.make), |
| 46 | DirectoryProjectTest, |
| 47 | Layer.succeed( |
| 48 | TerminalUI, |
| 49 | TerminalUI.of({ |
| 50 | ...terminalUITestImpl, |
| 51 | output: data => |
| 52 | Effect.sync(() => { |
| 53 | outputs.push(data); |
| 54 | }), |
| 55 | }) |
| 56 | ), |
| 57 | ConnectedAccountsTest({ connectedAccountsData: { items: [ACTIVE_ACCOUNT, EXPIRED_ACCOUNT] } }) |
| 58 | ); |
| 59 | return { layer, outputs }; |
| 60 | }; |
| 61 | |
| 62 | const stdoutLines = MockConsole.getLines({ stream: 'stdout', stripAnsi: true }); |
| 63 | const stderrLines = MockConsole.getLines({ stream: 'stderr', stripAnsi: true }); |
| 64 | |
| 65 | describe('runConnectedAccountsWhoami', () => { |
| 66 | it.effect('[Given] an active account [Then] prints its profile fields as JSON on stdout', () => { |
| 67 | const { layer, outputs } = whoamiTest(); |
| 68 | return Effect.gen(function* () { |
| 69 | yield* runConnectedAccountsWhoami({ id: Option.some('con_gmail_active') }); |
| 70 | |
| 71 | expect(outputs.map(output => JSON.parse(output))).toEqual([ |
| 72 | { |
| 73 | id: 'con_gmail_active', |
| 74 | alias: 'personal', |
| 75 | word_id: 'harbor', |
| 76 | toolkit: 'gmail', |
| 77 | user_id: 'default', |
| 78 | status: 'ACTIVE', |
| 79 | auth_config_id: 'ac_gmail_oauth', |
| 80 | auth_scheme: 'OAUTH2', |
| 81 | }, |
| 82 | ]); |
| 83 | expect(yield* stderrLines).toEqual([]); |
| 84 | }).pipe(Effect.provide(layer)); |
| 85 | }); |
| 86 | |
| 87 | it.effect('[Given] an account that is not ACTIVE [Then] warns before the profile', () => { |
| 88 | const { layer, outputs } = whoamiTest(); |
| 89 | return Effect.gen(function* () { |
| 90 | yield* runConnectedAccountsWhoami({ id: Option.some('con_gmail_expired') }); |
| 91 | |
| 92 | expect(yield* stderrLines).toEqual([inactiveStatusMessage('EXPIRED')]); |
| 93 | expect((yield* stdoutLines).some(line => line.startsWith('[whoami: gmail]'))).toBe(true); |
| 94 | expect(JSON.parse(outputs[0] ?? '{}')).toMatchObject({ status: 'EXPIRED' }); |
| 95 | }).pipe(Effect.provide(layer)); |
| 96 | }); |
| 97 | |
| 98 | it.effect('[Given] no id [Then] fails with a usage error before any request', () => { |
| 99 | const { layer, outputs } = whoamiTest(); |
| 100 | return Effect.gen(function* () { |
| 101 | const failure = yield* runConnectedAccountsWhoami({ id: Option.none() }).pipe(Effect.flip); |
| 102 | |
| 103 | expect(failure).toMatchObject({ |
| 104 | _tag: 'commands/CommandInputError', |
| 105 | command: 'dev connected-accounts whoami', |
| 106 | option: '<id>', |
| 107 | message: `${MISSING_ID_MESSAGE}\n${missingIdHint('whoami')}`, |
| 108 | }); |
| 109 | // The invocation prints a usage failure on stderr and exits 1. |
| 110 | expect(isUsageFailure(failure)).toBe(true); |
| 111 | expect(yield* stderrLines).toEqual([]); |
| 112 | expect(yield* stdoutLines).toEqual([]); |
| 113 | expect(outputs).toEqual([]); |
| 114 | }).pipe(Effect.provide(layer)); |
| 115 | }); |
| 116 | |
| 117 | it.effect('[Given] an unknown id [Then] reports it, prints no data, and exits 1', () => { |
| 118 | const { layer, outputs } = whoamiTest(); |
| 119 | return Effect.gen(function* () { |
| 120 | const exit = yield* Effect.exit( |
| 121 | runConnectedAccountsWhoami({ id: Option.some('con_missing') }) |
| 122 | ); |
| 123 | |
| 124 | expect((yield* stderrLines).join('\n')).toContain( |
| 125 | 'Connected account "con_missing" not found.' |
| 126 | ); |
| 127 | expect(outputs).toEqual([]); |
| 128 | expect(exitCodeOf(exit)).toBe(1); |
| 129 | }).pipe(Effect.provide(layer)); |
| 130 | }); |
| 131 | }); |