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 { runConnectedAccountsInfo } from 'src/commands/connected-accounts/info/connected-accounts.info.handler'; |
| 4 | import { MISSING_ID_MESSAGE, missingIdHint } from 'src/commands/connected-accounts/format'; |
| 5 | import { isUsageFailure } from 'src/commands/errors'; |
| 6 | import type { ConnectedAccountItem } from 'src/models/connected-accounts'; |
| 7 | import { TerminalUI } from 'src/services/terminal-ui'; |
| 8 | import { redact } from 'src/ui/redact'; |
| 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 EXPIRED_ACCOUNT: ConnectedAccountItem = { |
| 15 | id: 'con_github_expired', |
| 16 | alias: 'work', |
| 17 | word_id: 'castle', |
| 18 | status: 'EXPIRED', |
| 19 | status_reason: 'Token expired', |
| 20 | is_disabled: false, |
| 21 | user_id: 'default', |
| 22 | toolkit: { slug: 'github' }, |
| 23 | auth_config: { |
| 24 | id: 'ac_github_oauth', |
| 25 | auth_scheme: 'OAUTH2', |
| 26 | is_composio_managed: false, |
| 27 | is_disabled: false, |
| 28 | }, |
| 29 | created_at: '2026-02-01T00:00:00Z', |
| 30 | updated_at: '2026-02-10T00:00:00Z', |
| 31 | test_request_endpoint: 'https://api.github.com/user', |
| 32 | }; |
| 33 | |
| 34 | /** |
| 35 | * What `runConnectedAccountsInfo` reads: the connected-account repository and a headless |
| 36 | * terminal whose stdout data lands in `outputs`. |
| 37 | */ |
| 38 | const infoTest = (items: ReadonlyArray<ConnectedAccountItem> = [EXPIRED_ACCOUNT]) => { |
| 39 | const outputs: Array<string> = []; |
| 40 | const layer = Layer.mergeAll( |
| 41 | Layer.effect(Console.Console, MockConsole.make), |
| 42 | DirectoryProjectTest, |
| 43 | Layer.succeed( |
| 44 | TerminalUI, |
| 45 | TerminalUI.of({ |
| 46 | ...terminalUITestImpl, |
| 47 | output: data => |
| 48 | Effect.sync(() => { |
| 49 | outputs.push(data); |
| 50 | }), |
| 51 | }) |
| 52 | ), |
| 53 | ConnectedAccountsTest({ connectedAccountsData: { items: [...items] } }) |
| 54 | ); |
| 55 | return { layer, outputs }; |
| 56 | }; |
| 57 | |
| 58 | const stdoutLines = MockConsole.getLines({ stream: 'stdout', stripAnsi: true }); |
| 59 | const stderrLines = MockConsole.getLines({ stream: 'stderr', stripAnsi: true }); |
| 60 | |
| 61 | describe('runConnectedAccountsInfo', () => { |
| 62 | it.effect('[Given] an existing id [Then] prints the whole account as JSON on stdout', () => { |
| 63 | const { layer, outputs } = infoTest(); |
| 64 | return Effect.gen(function* () { |
| 65 | yield* runConnectedAccountsInfo({ id: Option.some('con_github_expired') }); |
| 66 | |
| 67 | expect(outputs.map(output => JSON.parse(output))).toEqual([EXPIRED_ACCOUNT]); |
| 68 | }).pipe(Effect.provide(layer)); |
| 69 | }); |
| 70 | |
| 71 | it.effect( |
| 72 | '[Given] an account with a status reason and test endpoint [Then] the note shows both', |
| 73 | () => { |
| 74 | const { layer } = infoTest(); |
| 75 | return Effect.gen(function* () { |
| 76 | yield* runConnectedAccountsInfo({ id: Option.some('con_github_expired') }); |
| 77 | const note = (yield* stdoutLines).find(line => |
| 78 | line.startsWith('[Connected Account: github]') |
| 79 | ); |
| 80 | |
| 81 | expect(note).toBeDefined(); |
| 82 | expect(note).toContain('Alias: work'); |
| 83 | expect(note).toContain('Word Id: castle'); |
| 84 | expect(note).toContain('Status Reason: Token expired'); |
| 85 | // CI redacts ids (`CI=true`), so expect the id the way the formatter prints it. |
| 86 | expect(note).toContain( |
| 87 | `Auth Config: ${redact({ value: 'ac_github_oauth', prefix: 'ac_' })} (OAUTH2)` |
| 88 | ); |
| 89 | expect(note).toContain('Composio Managed: No'); |
| 90 | expect(note).toContain('Test Endpoint: https://api.github.com/user'); |
| 91 | }).pipe(Effect.provide(layer)); |
| 92 | } |
| 93 | ); |
| 94 | |
| 95 | it.effect('[Given] no id [Then] fails with a usage error before any request', () => { |
| 96 | const { layer, outputs } = infoTest(); |
| 97 | return Effect.gen(function* () { |
| 98 | const failure = yield* runConnectedAccountsInfo({ id: Option.none() }).pipe(Effect.flip); |
| 99 | |
| 100 | expect(failure).toMatchObject({ |
| 101 | _tag: 'commands/CommandInputError', |
| 102 | command: 'dev connected-accounts info', |
| 103 | option: '<id>', |
| 104 | message: `${MISSING_ID_MESSAGE}\n${missingIdHint('info')}`, |
| 105 | }); |
| 106 | // The invocation prints a usage failure on stderr and exits 1. |
| 107 | expect(isUsageFailure(failure)).toBe(true); |
| 108 | expect(yield* stderrLines).toEqual([]); |
| 109 | expect(yield* stdoutLines).toEqual([]); |
| 110 | expect(outputs).toEqual([]); |
| 111 | }).pipe(Effect.provide(layer)); |
| 112 | }); |
| 113 | |
| 114 | it.effect('[Given] an unknown id [Then] reports it, prints no data, and exits 1', () => { |
| 115 | const { layer, outputs } = infoTest(); |
| 116 | return Effect.gen(function* () { |
| 117 | const exit = yield* Effect.exit(runConnectedAccountsInfo({ id: Option.some('con_missing') })); |
| 118 | const stderr = (yield* stderrLines).join('\n'); |
| 119 | |
| 120 | expect(stderr).toContain('Connected account "con_missing" not found.'); |
| 121 | expect(stderr).toContain('composio dev connected-accounts list'); |
| 122 | expect(outputs).toEqual([]); |
| 123 | expect(exitCodeOf(exit)).toBe(1); |
| 124 | }).pipe(Effect.provide(layer)); |
| 125 | }); |
| 126 | }); |