Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 177 lines 5.97 KB
Raw
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
39 const connectedAccountsData = {
40 items: testConnectedAccounts,
41 } satisfies TestLiveInput['connectedAccountsData'];
42
43 const testConfigProvider = ConfigProvider.fromEnv({
44 env: { COMPOSIO_USER_API_KEY: 'test_api_key' },
45 });
46
47 // Logged in, in a directory set up for a developer project.
48 const inProject = { ...inDeveloperProjectDirectory, baseConfigProvider: testConfigProvider };
49
50 describe('CLI: composio dev connected-accounts info', () => {
51 layer(TestLive({ ...inProject, connectedAccountsData }))(
52 '[Given] valid ID [Then] shows connected account info',
53 it => {
54 it.effect('displays info for existing account', () =>
55 Effect.gen(function* () {
56 yield* cli(['dev', 'connected-accounts', 'info', 'con_gmail_active']);
57 const lines = yield* MockConsole.getLines({ stripAnsi: true });
58 const output = lines.join('\n');
59
60 expect(output).toContain('con_gmail_active');
61 expect(output).toContain('gmail');
62 expect(output).toContain('ACTIVE');
63 expect(output).toContain('OAUTH2');
64 })
65 );
66 }
67 );
68
69 layer(TestLive({ ...inProject, connectedAccountsData }))(
70 '[Given] nonexistent ID [Then] shows error with hint',
71 it => {
72 it.effect('shows error for nonexistent account', () =>
73 Effect.gen(function* () {
74 const exit = yield* Effect.exit(
75 cli(['dev', 'connected-accounts', 'info', 'con_nonexistent'])
76 );
77 const lines = yield* MockConsole.getLines({ stripAnsi: true });
78 const output = lines.join('\n');
79
80 expect(output).toContain('not found');
81 expect(output).toContain('composio dev connected-accounts list');
82 expect(exitCodeOf(exit)).toBe(1);
83 })
84 );
85 }
86 );
87
88 layer(TestLive({ ...inProject, connectedAccountsData }))(
89 '[Given] no ID argument [Then] fails with a usage error',
90 it => {
91 it.effect('raises a CommandInputError naming <id>', () =>
92 Effect.gen(function* () {
93 const failure = yield* cli(['dev', 'connected-accounts', 'info']).pipe(Effect.flip);
94
95 expect(failure).toMatchObject({
96 _tag: 'commands/CommandInputError',
97 option: '<id>',
98 message: expect.stringMatching(
99 /^Missing required argument: <id>\n[^]*composio dev connected-accounts info /
100 ),
101 });
102 // The invocation prints the message on stderr, and nothing else.
103 assert(isUsageFailure(failure));
104 expect(yield* MockConsole.getLines()).toEqual([failure.message]);
105 })
106 );
107 }
108 );
109
110 layer(TestLive())('[Given] no API key [Then] warns user to login', it => {
111 it.effect('warns user to login', () =>
112 Effect.gen(function* () {
113 const error = yield* cli(['dev', 'connected-accounts', 'info', 'con_gmail_active']).pipe(
114 Effect.flip
115 );
116 expect(error).toMatchObject({ _tag: 'commands/NotLoggedInError' });
117 })
118 );
119 });
120 });
121
122 // The request goes to the developer project the command resolved, never to the one the backend
123 // would pick for the key.
124 describe('CLI: composio dev connected-accounts info acts on the developer project', () => {
125 layer(TestLive({ ...inProject, connectedAccountsData }))(it => {
126 it.effect.each([
127 { given: 'a directory set up for a project', args: [], scope: DIRECTORY_PROJECT_SCOPE },
128 {
129 given: '--project-name',
130 args: ['--project-name', OTHER_DEVELOPER_PROJECT.name],
131 scope: OTHER_PROJECT_SCOPE,
132 },
133 ])('[Given] $given [Then] reads the account from that project', ({ args, scope }) =>
134 Effect.gen(function* () {
135 const { exitCode, calls } = yield* runRecordingRepositoryScopes([
136 ...['dev', 'connected-accounts', 'info', 'con_gmail_active'],
137 ...args,
138 ]);
139
140 expect(exitCode).toBe(0);
141 expect(calls).toEqual([
142 { method: 'getConnectedAccount', scope: expect.objectContaining(scope) },
143 ]);
144 })
145 );
146 });
147
148 layer(
149 TestLive({
150 baseConfigProvider: testConfigProvider,
151 connectedAccountsData,
152 accountData: defaultDeveloperProjectAccountData(),
153 })
154 )(it => {
155 it.effect(
156 '[Given] a directory set up for no project [Then] acts on the default developer project',
157 () =>
158 Effect.gen(function* () {
159 yield* loginWithoutDeveloperProject;
160 const { exitCode, calls } = yield* runRecordingRepositoryScopes([
161 'dev',
162 'connected-accounts',
163 'info',
164 'con_gmail_active',
165 ]);
166
167 expect(exitCode).toBe(0);
168 expect(calls).toEqual([
169 {
170 method: 'getConnectedAccount',
171 scope: expect.objectContaining(DEFAULT_PROJECT_SCOPE),
172 },
173 ]);
174 })
175 );
176 });
177 });