@devries-cameron20 / composio Mirror · read-only
Fork 0

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

next
ts 216 lines 7.4 KB
Raw
1 import { describe, expect, layer } from '@effect/vitest';
2 import { ConfigProvider, Effect } from 'effect';
3 import type { ConnectedAccountItem } from 'src/models/connected-accounts';
4 import { cli, MockConsole, TestLive } from 'test/__utils__';
5 import { exitCodeOf } from 'test/__utils__/exit-code';
6 import { routeRequests, stubClientResources } from 'test/__utils__/services/stub-composio-client';
7
8 const testLayer = TestLive({
9 baseConfigProvider: ConfigProvider.fromEnv({ env: { COMPOSIO_USER_API_KEY: 'test_api_key' } }),
10 fixture: 'global-test-user-id',
11 });
12
13 const rejection = (status: number, message: string, slug: string) =>
14 Response.json({ error: { message, code: 1000 + status, slug, status } }, { status });
15
16 const account = (id: string): ConnectedAccountItem => ({
17 id,
18 alias: null,
19 status: 'ACTIVE',
20 status_reason: null,
21 is_disabled: false,
22 user_id: 'consumer-user-org_test',
23 toolkit: { slug: 'gmail' },
24 auth_config: {
25 id: 'ac_gmail',
26 auth_scheme: 'OAUTH2',
27 is_composio_managed: true,
28 is_disabled: false,
29 },
30 created_at: '2026-01-01T00:00:00Z',
31 updated_at: '2026-01-01T00:00:00Z',
32 test_request_endpoint: '',
33 });
34
35 describe('CLI: composio link over the API client', () => {
36 layer(testLayer)(it => {
37 it.effect('lists active accounts beyond the first server page', () =>
38 Effect.gen(function* () {
39 const firstPage = Array.from({ length: 50 }, (_, index) => account(`con_${index}`));
40 const stub = stubClientResources(['connectedAccounts'], request => {
41 const cursor = new URL(request.path, 'https://stub.invalid').searchParams.get('cursor');
42 return Response.json({
43 items: cursor === 'page-2' ? [account('con_last')] : firstPage,
44 next_cursor: cursor === 'page-2' ? null : 'page-2',
45 });
46 });
47
48 yield* cli(['link', 'gmail', '--list']).pipe(Effect.provide(stub.layer));
49
50 const lines = yield* MockConsole.getLines({ stripAnsi: true });
51 const result = JSON.parse(String(lines.at(-1)));
52 expect(result.total).toBe(51);
53 expect(result.items.at(-1).id).toBe('con_last');
54 expect(stub.requests).toHaveLength(2);
55 const query = {
56 user_ids: 'consumer-user-org_test',
57 toolkit_slugs: 'gmail',
58 statuses: 'ACTIVE',
59 limit: '50',
60 };
61 expect(
62 stub.requests.map(request =>
63 Object.fromEntries(new URL(request.path, 'https://stub.invalid').searchParams)
64 )
65 ).toEqual([query, { ...query, cursor: 'page-2' }]);
66 })
67 );
68 });
69
70 describe.each([
71 {
72 scope: 'toolkit',
73 args: ['link', 'gmail', '--alias', 'work', '--no-wait'],
74 userId: 'consumer-user-org_test',
75 filter: 'toolkit_slugs',
76 value: 'gmail',
77 },
78 {
79 scope: 'auth config',
80 args: [
81 'dev',
82 'connected-accounts',
83 'link',
84 '--auth-config',
85 'ac_1',
86 '--user-id',
87 'user_1',
88 '--alias',
89 'work',
90 '--no-wait',
91 ],
92 userId: 'user_1',
93 filter: 'auth_config_ids',
94 value: 'ac_1',
95 },
96 ])('alias conflicts in a $scope', ({ args, userId, filter, value }) => {
97 layer(testLayer)(it => {
98 it.effect('rejects an alias held by an account on a later page before creating a link', () =>
99 Effect.gen(function* () {
100 const pages = [
101 {
102 items: Array.from({ length: 50 }, (_, i) => account(`con_${i}`)),
103 next_cursor: 'page-2',
104 },
105 { items: [{ ...account('con_hidden'), alias: 'Work' }], next_cursor: null },
106 ];
107 const stub = stubClientResources(['connectedAccounts', 'toolRouter', 'link'], () => {
108 const page = pages.shift();
109 return page === undefined
110 ? rejection(403, 'Unexpected request', 'Forbidden')
111 : Response.json(page);
112 });
113
114 const exit = yield* Effect.exit(cli(args).pipe(Effect.provide(stub.layer)));
115
116 expect(exitCodeOf(exit)).toBe(1);
117 expect((yield* MockConsole.getLines({ stripAnsi: true })).join('\n')).toContain(
118 'Alias "work" is already in use by connected account "con_hidden".'
119 );
120 expect(stub.requests).toHaveLength(2);
121 expect(stub.requests.every(request => request.method === 'GET')).toBe(true);
122 const query = { user_ids: userId, [filter]: value, statuses: 'ACTIVE', limit: '50' };
123 expect(
124 stub.requests.map(request =>
125 Object.fromEntries(new URL(request.path, 'https://stub.invalid').searchParams)
126 )
127 ).toEqual([query, { ...query, cursor: 'page-2' }]);
128 })
129 );
130 });
131 });
132
133 layer(testLayer)('[Given] the API rejects the link for a toolkit', it => {
134 it.effect('[Then] the API message and the browse hint are printed', () =>
135 Effect.gen(function* () {
136 const stub = stubClientResources(
137 ['toolRouter'],
138 routeRequests(
139 [
140 {
141 method: 'POST',
142 path: '/api/v3.1/tool_router/session/trs_stub_session/link',
143 respond: () =>
144 rejection(
145 400,
146 'Toolkit gmail is disabled for this project',
147 'ToolRouterV2_ToolkitDisabled'
148 ),
149 },
150 ],
151 () =>
152 Response.json({
153 session_id: 'trs_stub_session',
154 config: { user_id: 'consumer-user-org_test' },
155 mcp: { type: 'http', url: 'https://mcp.stub.invalid' },
156 tool_router_tools: [],
157 })
158 )
159 );
160 const exit = yield* Effect.exit(
161 cli(['link', 'gmail', '--no-wait']).pipe(Effect.provide(stub.layer))
162 );
163 const lines = yield* MockConsole.getLines({ stripAnsi: true });
164
165 expect(exitCodeOf(exit)).toBe(1);
166
167 expect(lines).toEqual([
168 'Toolkit gmail is disabled for this project',
169 'Browse available toolkits:\n> composio dev toolkits list',
170 ]);
171 expect(stub.requests.at(-1)).toEqual({
172 method: 'POST',
173 path: '/api/v3.1/tool_router/session/trs_stub_session/link',
174 body: { toolkit: 'gmail' },
175 });
176 })
177 );
178 });
179
180 layer(testLayer)('[Given] the API rejects the link for an auth config', it => {
181 it.effect('[Then] the API message and the browse hint are printed', () =>
182 Effect.gen(function* () {
183 const stub = stubClientResources(['link'], () =>
184 rejection(404, 'Auth config ac_missing not found', 'AuthConfig_NotFound')
185 );
186 const exit = yield* Effect.exit(
187 cli([
188 'dev',
189 'connected-accounts',
190 'link',
191 '--auth-config',
192 'ac_missing',
193 '--user-id',
194 'user_1',
195 '--no-wait',
196 ]).pipe(Effect.provide(stub.layer))
197 );
198 const lines = yield* MockConsole.getLines({ stripAnsi: true });
199
200 expect(exitCodeOf(exit)).toBe(1);
201
202 expect(lines).toEqual([
203 'Auth config ac_missing not found',
204 'Browse available auth configs:\n> composio dev auth-configs list',
205 ]);
206 expect(stub.requests).toEqual([
207 {
208 method: 'POST',
209 path: '/api/v3.1/connected_accounts/link',
210 body: { auth_config_id: 'ac_missing', user_id: 'user_1' },
211 },
212 ]);
213 })
214 );
215 });
216 });