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 383 lines 13 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 { extractErrorDetails } from 'src/effect-errors/logic/errors/extract-error-details';
5 import type { ConnectedAccountItem } from 'src/models/connected-accounts';
6 import { MissingActingUserError } from 'src/commands/surface';
7 import { ComposioUserContext } from 'src/services/user-context';
8 import { cli, TestLive, MockConsole } from 'test/__utils__';
9 import { routeRequests, stubClientResources } from 'test/__utils__/services/stub-composio-client';
10 import type { TestLiveInput } from 'test/__utils__/services/test-layer';
11 import {
12 terminalUIWithConfirm,
13 terminalUIWithoutPrompt,
14 } from 'test/__utils__/services/terminal-ui-test';
15
16 const testConnections: ConnectedAccountItem[] = [
17 {
18 id: 'con_gmail_active',
19 alias: null,
20 word_id: null,
21 status: 'ACTIVE',
22 status_reason: null,
23 is_disabled: false,
24 user_id: 'consumer-user-org_test',
25 toolkit: { slug: 'gmail' },
26 auth_config: {
27 id: 'ac_gmail_oauth',
28 auth_scheme: 'OAUTH2',
29 is_composio_managed: true,
30 is_disabled: false,
31 },
32 created_at: '2026-01-01T00:00:00Z',
33 updated_at: '2026-01-15T00:00:00Z',
34 test_request_endpoint: '',
35 },
36 {
37 id: 'con_github_work',
38 alias: 'work',
39 word_id: 'castle',
40 status: 'ACTIVE',
41 status_reason: null,
42 is_disabled: false,
43 user_id: 'consumer-user-org_test',
44 toolkit: { slug: 'github' },
45 auth_config: {
46 id: 'ac_github_oauth',
47 auth_scheme: 'OAUTH2',
48 is_composio_managed: true,
49 is_disabled: false,
50 },
51 created_at: '2026-02-01T00:00:00Z',
52 updated_at: '2026-02-10T00:00:00Z',
53 test_request_endpoint: '',
54 },
55 {
56 id: 'con_github_personal',
57 alias: 'personal',
58 word_id: 'forest',
59 status: 'FAILED',
60 status_reason: 'Token expired',
61 is_disabled: false,
62 user_id: 'consumer-user-org_test',
63 toolkit: { slug: 'github' },
64 auth_config: {
65 id: 'ac_github_oauth_2',
66 auth_scheme: 'OAUTH2',
67 is_composio_managed: true,
68 is_disabled: false,
69 },
70 created_at: '2026-02-03T00:00:00Z',
71 updated_at: '2026-02-12T00:00:00Z',
72 test_request_endpoint: '',
73 },
74 ];
75
76 const connectedAccountsData = {
77 items: testConnections,
78 } satisfies TestLiveInput['connectedAccountsData'];
79
80 const testConfigProvider = ConfigProvider.fromEnv({
81 env: { COMPOSIO_USER_API_KEY: 'test_api_key' },
82 });
83
84 describe('CLI: composio connections remove', () => {
85 const confirmedDeleteCalls: string[] = [];
86 layer(
87 TestLive({
88 baseConfigProvider: testConfigProvider,
89 connectedAccountsData: {
90 items: testConnections,
91 onDelete: nanoid => confirmedDeleteCalls.push(nanoid),
92 },
93 terminalUI: terminalUIWithConfirm(true),
94 })
95 )(it => {
96 it.effect('[Given] a unique toolkit selector and consent [Then] removes that connection', () =>
97 Effect.gen(function* () {
98 confirmedDeleteCalls.length = 0;
99
100 const userContext = yield* ComposioUserContext;
101 yield* userContext.login('test_api_key', 'org_test');
102 yield* cli(['connections', 'remove', 'gmail']);
103
104 expect(confirmedDeleteCalls).toEqual(['con_gmail_active']);
105
106 const lines = yield* MockConsole.getLines({ stripAnsi: true });
107 expect(lines.join('\n')).toContain('Removed gmail connection.');
108 })
109 );
110 });
111
112 layer(
113 TestLive({
114 baseConfigProvider: testConfigProvider,
115 terminalUI: terminalUIWithConfirm(true),
116 })
117 )(it => {
118 it.effect('[Given] the API rejects the removal [Then] fails naming the connected account', () =>
119 Effect.gen(function* () {
120 const stub = stubClientResources(
121 ['connectedAccounts'],
122 routeRequests(
123 [
124 {
125 method: 'DELETE',
126 path: '/api/v3.1/connected_accounts/',
127 respond: () =>
128 Response.json(
129 {
130 error: { message: 'Not allowed', code: 1403, slug: 'Forbidden', status: 403 },
131 },
132 { status: 403 }
133 ),
134 },
135 ],
136 () =>
137 Response.json({
138 items: testConnections,
139 total_items: testConnections.length,
140 total_pages: 1,
141 current_page: 1,
142 next_cursor: null,
143 })
144 )
145 );
146 const userContext = yield* ComposioUserContext;
147 yield* userContext.login('test_api_key', 'org_test');
148 const failure = yield* cli(['connections', 'remove', 'gmail']).pipe(
149 Effect.provide(stub.layer),
150 Effect.flip
151 );
152
153 expect(failure).toMatchObject({ _tag: 'commands/ConnectionsRemovalRequestError' });
154 expect(extractErrorDetails(failure).message).toBe(
155 'Failed to remove connected account "con_gmail_active".\nCaused by: 403 {"error":{"message":"Not allowed","code":1403,"slug":"Forbidden","status":403}}'
156 );
157 })
158 );
159 });
160
161 const deniedDeleteCalls: string[] = [];
162 layer(
163 TestLive({
164 baseConfigProvider: testConfigProvider,
165 connectedAccountsData: {
166 items: testConnections,
167 onDelete: nanoid => deniedDeleteCalls.push(nanoid),
168 },
169 terminalUI: terminalUIWithConfirm(false),
170 })
171 )(it => {
172 it.effect('[Given] consent is denied [Then] does not remove the connection', () =>
173 Effect.gen(function* () {
174 deniedDeleteCalls.length = 0;
175
176 const userContext = yield* ComposioUserContext;
177 yield* userContext.login('test_api_key', 'org_test');
178 yield* cli(['connections', 'remove', 'work']);
179
180 expect(deniedDeleteCalls).toEqual([]);
181
182 // Declining is not a failure: the command succeeds and the line says nothing changed.
183 const lines = yield* MockConsole.getLines({ stripAnsi: true });
184 expect(lines.at(-1)).toBe('No connection removed.');
185 })
186 );
187 });
188
189 const ambiguousDeleteCalls: string[] = [];
190 layer(
191 TestLive({
192 baseConfigProvider: testConfigProvider,
193 connectedAccountsData: {
194 items: testConnections,
195 onDelete: nanoid => ambiguousDeleteCalls.push(nanoid),
196 },
197 terminalUI: terminalUIWithConfirm(true),
198 })
199 )(it => {
200 it.effect(
201 '[Given] a selector matches multiple accounts [Then] asks for a unique selector',
202 () =>
203 Effect.gen(function* () {
204 ambiguousDeleteCalls.length = 0;
205
206 const userContext = yield* ComposioUserContext;
207 yield* userContext.login('test_api_key', 'org_test');
208 const failure = yield* cli(['connections', 'remove', 'github']).pipe(Effect.flip);
209
210 expect(ambiguousDeleteCalls).toEqual([]);
211 expect(failure).toMatchObject({
212 _tag: 'commands/CommandInputError',
213 command: 'connections remove',
214 option: '<account>',
215 message: expect.stringContaining('Multiple connections matched "github"'),
216 });
217 })
218 );
219
220 it.effect('[Given] a selector matches no account [Then] fails with a usage error', () =>
221 Effect.gen(function* () {
222 ambiguousDeleteCalls.length = 0;
223
224 const userContext = yield* ComposioUserContext;
225 yield* userContext.login('test_api_key', 'org_test');
226 const failure = yield* cli(['connections', 'remove', 'nothing-matches']).pipe(Effect.flip);
227
228 expect(ambiguousDeleteCalls).toEqual([]);
229 expect(failure).toMatchObject({
230 _tag: 'commands/CommandInputError',
231 command: 'connections remove',
232 option: '<account>',
233 message:
234 'No connection matched "nothing-matches". Run `composio connections list` to inspect available connections.',
235 });
236 })
237 );
238 });
239
240 const headlessDeleteCalls: string[] = [];
241 layer(
242 TestLive({
243 baseConfigProvider: testConfigProvider,
244 connectedAccountsData: {
245 items: testConnections,
246 onDelete: nanoid => headlessDeleteCalls.push(nanoid),
247 },
248 })
249 )(it => {
250 it.effect(
251 '[Given] no terminal and no --yes [Then] fails with a usage error before any request',
252 () =>
253 Effect.gen(function* () {
254 headlessDeleteCalls.length = 0;
255
256 const userContext = yield* ComposioUserContext;
257 yield* userContext.login('test_api_key', 'org_test');
258 const failure = yield* cli(['connections', 'remove', 'gmail']).pipe(Effect.flip);
259
260 expect(failure).toMatchObject({
261 _tag: 'commands/CommandInputError',
262 command: 'connections remove',
263 option: '--yes',
264 message: '`composio connections remove` needs --yes when not running interactively.',
265 });
266 expect(headlessDeleteCalls).toEqual([]);
267 // Not even the connection list is fetched.
268 // The invocation prints the message on stderr, and nothing else.
269 assert(isUsageFailure(failure));
270 expect(yield* MockConsole.getLines()).toEqual([failure.message]);
271 })
272 );
273
274 it.effect('[Given] no terminal and --yes [Then] removes the connection without asking', () =>
275 Effect.gen(function* () {
276 headlessDeleteCalls.length = 0;
277
278 const userContext = yield* ComposioUserContext;
279 yield* userContext.login('test_api_key', 'org_test');
280 // The headless test terminal answers a prompt with its default, "no", so a removal
281 // proves no prompt ran.
282 yield* cli(['connections', 'remove', 'gmail', '--yes']);
283
284 expect(headlessDeleteCalls).toEqual(['con_gmail_active']);
285 })
286 );
287
288 it.effect(
289 '[Given] --yes and a selector that matches several connections [Then] removes nothing and fails with a usage error',
290 () =>
291 Effect.gen(function* () {
292 headlessDeleteCalls.length = 0;
293
294 const userContext = yield* ComposioUserContext;
295 yield* userContext.login('test_api_key', 'org_test');
296 const failure = yield* cli(['connections', 'remove', 'github', '--yes']).pipe(
297 Effect.flip
298 );
299
300 // The invocation prints the message on stderr and exits 1.
301 expect(failure).toMatchObject({
302 _tag: 'commands/CommandInputError',
303 command: 'connections remove',
304 option: '<account>',
305 message: expect.stringContaining('Multiple connections matched "github"'),
306 });
307 expect(headlessDeleteCalls).toEqual([]);
308 })
309 );
310
311 it.effect(
312 '[Given] --yes and a selector that matches no connection [Then] removes nothing and fails with a usage error',
313 () =>
314 Effect.gen(function* () {
315 headlessDeleteCalls.length = 0;
316
317 const userContext = yield* ComposioUserContext;
318 yield* userContext.login('test_api_key', 'org_test');
319 const failure = yield* cli(['connections', 'remove', 'nothing-matches', '-y']).pipe(
320 Effect.flip
321 );
322
323 expect(failure).toMatchObject({
324 _tag: 'commands/CommandInputError',
325 command: 'connections remove',
326 option: '<account>',
327 message:
328 'No connection matched "nothing-matches". Run `composio connections list` to inspect available connections.',
329 });
330 expect(headlessDeleteCalls).toEqual([]);
331 })
332 );
333 });
334
335 const interactiveYesDeleteCalls: string[] = [];
336 layer(
337 TestLive({
338 baseConfigProvider: testConfigProvider,
339 connectedAccountsData: {
340 items: testConnections,
341 onDelete: nanoid => interactiveYesDeleteCalls.push(nanoid),
342 },
343 terminalUI: terminalUIWithoutPrompt,
344 })
345 )(it => {
346 it.effect('[Given] a terminal and -y [Then] removes the connection without asking', () =>
347 Effect.gen(function* () {
348 interactiveYesDeleteCalls.length = 0;
349
350 const userContext = yield* ComposioUserContext;
351 yield* userContext.login('test_api_key', 'org_test');
352 yield* cli(['connections', 'remove', 'gmail', '-y']);
353
354 expect(interactiveYesDeleteCalls).toEqual(['con_gmail_active']);
355 const lines = yield* MockConsole.getLines({ stripAnsi: true });
356 expect(lines.at(-1)).toBe('Removed gmail connection.');
357 })
358 );
359 });
360
361 layer(
362 TestLive({
363 baseConfigProvider: testConfigProvider,
364 accountData: { consumerUserId: '' },
365 })
366 )(it => {
367 it.effect(
368 '[Given] an empty consumer user id [Then] fails with the missing-user error and its message',
369 () =>
370 Effect.gen(function* () {
371 const userContext = yield* ComposioUserContext;
372 yield* userContext.login('test_api_key', 'org_test');
373 const failure = yield* cli(['connections', 'remove', 'gmail', '--yes']).pipe(Effect.flip);
374
375 // An empty id counts as no user, so the removal fails before resolving any account.
376 expect(failure).toBeInstanceOf(MissingActingUserError);
377 expect(failure).toMatchObject({
378 message: 'Missing consumer user id. Run `composio login` and try again.',
379 });
380 })
381 );
382 });
383 });