@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, 04:45 UTC

next
ts 312 lines 10.3 KB
Raw
1 import { assert, describe, expect, layer } from '@effect/vitest';
2 import { isUsageFailure } from 'src/commands/errors';
3 import { ConfigProvider, DateTime, Effect, Layer } from 'effect';
4 import { cli, TestLive, MockConsole } from 'test/__utils__';
5 import { exitCodeOf } from 'test/__utils__/exit-code';
6 import { redirectedTerminalUI } from 'test/__utils__/services/redirected-terminal-ui';
7 import { stubClientResources } from 'test/__utils__/services/stub-composio-client';
8 import type { TestLiveInput } from 'test/__utils__/services/test-layer';
9 import type { Toolkits, ToolkitDetailed } from 'src/models/toolkits';
10
11 const testToolkits: Toolkits = [
12 {
13 name: 'Gmail',
14 slug: 'gmail',
15 auth_schemes: ['OAUTH2', 'BEARER_TOKEN'],
16 composio_managed_auth_schemes: ['OAUTH2'],
17 is_local_toolkit: false,
18 no_auth: false,
19 meta: {
20 description: 'Email service to send and receive emails',
21 categories: [],
22 created_at: DateTime.makeUnsafe('2024-05-03T11:44:32.061Z'),
23 updated_at: DateTime.makeUnsafe('2024-05-03T11:44:32.061Z'),
24 available_versions: ['20250101', '20250601', '20250909'],
25 tools_count: 36,
26 triggers_count: 2,
27 },
28 },
29 {
30 name: 'Code Interpreter',
31 slug: 'codeinterpreter',
32 auth_schemes: [],
33 composio_managed_auth_schemes: [],
34 is_local_toolkit: false,
35 no_auth: true,
36 meta: {
37 description: 'Execute code in a sandboxed environment',
38 categories: [],
39 created_at: DateTime.makeUnsafe('2024-05-03T11:44:32.061Z'),
40 updated_at: DateTime.makeUnsafe('2024-05-03T11:44:32.061Z'),
41 available_versions: [],
42 tools_count: 1,
43 triggers_count: 0,
44 },
45 },
46 ];
47
48 const detailedToolkits: ToolkitDetailed[] = [
49 {
50 name: 'Gmail',
51 slug: 'gmail',
52 is_local_toolkit: false,
53 composio_managed_auth_schemes: ['OAUTH2'],
54 no_auth: false,
55 meta: {
56 description: 'Email service to send and receive emails',
57 categories: [],
58 created_at: DateTime.makeUnsafe('2024-05-03T11:44:32.061Z'),
59 updated_at: DateTime.makeUnsafe('2024-05-03T11:44:32.061Z'),
60 available_versions: ['20250101', '20250601', '20250909'],
61 tools_count: 36,
62 triggers_count: 2,
63 },
64 auth_config_details: [
65 {
66 mode: 'OAUTH2',
67 name: 'OAuth 2.0',
68 fields: {
69 auth_config_creation: { required: [], optional: [] },
70 connected_account_initiation: { required: [], optional: [] },
71 },
72 },
73 {
74 mode: 'BEARER_TOKEN',
75 name: 'Bearer Token',
76 fields: {
77 auth_config_creation: {
78 required: [
79 {
80 name: 'apiKey',
81 displayName: 'API Key',
82 description: 'Your API key',
83 type: 'string',
84 required: true,
85 default: null,
86 },
87 ],
88 optional: [],
89 },
90 connected_account_initiation: {
91 required: [
92 {
93 name: 'apiKey',
94 displayName: 'API Key',
95 description: 'Your API key',
96 type: 'string',
97 required: true,
98 default: null,
99 },
100 ],
101 optional: [],
102 },
103 },
104 },
105 ],
106 },
107 {
108 name: 'Code Interpreter',
109 slug: 'codeinterpreter',
110 is_local_toolkit: false,
111 composio_managed_auth_schemes: [],
112 no_auth: true,
113 meta: {
114 description: 'Execute code snippets',
115 categories: [],
116 created_at: DateTime.makeUnsafe('2024-05-03T11:44:32.061Z'),
117 updated_at: DateTime.makeUnsafe('2024-05-03T11:44:32.061Z'),
118 available_versions: [],
119 tools_count: 5,
120 triggers_count: 0,
121 },
122 auth_config_details: [],
123 },
124 ];
125
126 const toolkitsData = {
127 toolkits: testToolkits,
128 detailedToolkits,
129 } satisfies TestLiveInput['toolkitsData'];
130
131 const testConfigProvider = ConfigProvider.fromEnv({
132 env: { COMPOSIO_USER_API_KEY: 'test_api_key' },
133 });
134
135 describe('CLI: composio dev toolkits info', () => {
136 layer(TestLive({ baseConfigProvider: testConfigProvider, toolkitsData }))(
137 '[Given] valid slug "gmail"',
138 it => {
139 it.effect('shows detailed info with connection status', () =>
140 Effect.gen(function* () {
141 yield* cli(['dev', 'toolkits', 'info', 'gmail']);
142 const lines = yield* MockConsole.getLines({ stripAnsi: true });
143 const output = lines.join('\n');
144
145 expect(output).toContain('Gmail');
146 expect(output).toContain('gmail');
147 expect(output).toContain('Email service to send and receive emails');
148 expect(output).toContain('Latest Version:');
149 expect(output).toContain('20250909');
150 expect(output).toContain('Tools Count: 36');
151 expect(output).toContain('Triggers Count: 2');
152 expect(output).toContain('Auth Modes: OAUTH2, BEARER_TOKEN');
153 // Connection status
154 expect(output).toContain('Not connected');
155 })
156 );
157 }
158 );
159
160 layer(
161 TestLive({
162 baseConfigProvider: testConfigProvider,
163 toolkitsData,
164 fixture: 'global-test-user-id',
165 })
166 )(
167 '[Given] no --user-id and no project test_user_id [Then] falls back to global test_user_id',
168 it => {
169 it.effect('uses global test user id for toolkit info', () =>
170 Effect.gen(function* () {
171 yield* cli(['dev', 'toolkits', 'info', 'gmail']);
172 const lines = yield* MockConsole.getLines({ stripAnsi: true });
173 const output = lines.join('\n');
174
175 expect(output).toContain('Using global test user id "global-default"');
176 expect(output).toContain('Gmail');
177 })
178 );
179 }
180 );
181
182 layer(TestLive({ baseConfigProvider: testConfigProvider, toolkitsData }))(
183 '[Given] toolkit with no_auth=true',
184 it => {
185 it.effect('shows "no auth"', () =>
186 Effect.gen(function* () {
187 yield* cli(['dev', 'toolkits', 'info', 'codeinterpreter']);
188 const lines = yield* MockConsole.getLines({ stripAnsi: true });
189 const output = lines.join('\n');
190
191 expect(output).toContain('Code Interpreter');
192 expect(output).toContain('Execute code snippets');
193 expect(output).toContain('Tools Count: 5');
194 expect(output).toContain('No authentication required');
195 })
196 );
197 }
198 );
199
200 layer(TestLive({ baseConfigProvider: testConfigProvider, toolkitsData }))(
201 '[Given] --all flag',
202 it => {
203 it.effect('shows full auth config setup fields', () =>
204 Effect.gen(function* () {
205 yield* cli(['dev', 'toolkits', 'info', 'gmail', '--all']);
206 const lines = yield* MockConsole.getLines({ stripAnsi: true });
207 const output = lines.join('\n');
208
209 expect(output).toContain('Auth Config Details:');
210 expect(output).toContain('auth_config_creation.required');
211 expect(output).toContain('connected_account_initiation.required');
212 expect(output).toContain('apiKey');
213 expect(output).toContain('API Key');
214 })
215 );
216 }
217 );
218
219 layer(TestLive({ baseConfigProvider: testConfigProvider, toolkitsData }))(
220 '[Given] invalid slug',
221 it => {
222 it.effect('prints the error on a redirected stderr and exits 1', () =>
223 Effect.gen(function* () {
224 const terminal = redirectedTerminalUI();
225 const exit = yield* Effect.exit(
226 cli(['dev', 'toolkits', 'info', 'gmal']).pipe(Effect.provide(terminal.layer))
227 );
228
229 expect(terminal.stderr()).toContain('Toolkit "gmal" not found.\n');
230 expect(terminal.stdout()).toBe('');
231 expect(exitCodeOf(exit)).toBe(1);
232 })
233 );
234 }
235 );
236
237 layer(TestLive({ baseConfigProvider: testConfigProvider, toolkitsData }))(
238 '[Given] invalid slug with substring match',
239 it => {
240 it.effect('shows error with hint and exits 1', () =>
241 Effect.gen(function* () {
242 const exit = yield* Effect.exit(cli(['dev', 'toolkits', 'info', 'gma']));
243 const lines = yield* MockConsole.getLines({ stripAnsi: true });
244 const output = lines.join('\n');
245
246 expect(output).toContain('Toolkit "gma" not found');
247 expect(output).toContain('composio dev toolkits info "gmail"');
248 expect(exitCodeOf(exit)).toBe(1);
249 })
250 );
251 }
252 );
253
254 layer(TestLive({ baseConfigProvider: testConfigProvider, toolkitsData }))(
255 '[Given] no slug argument',
256 it => {
257 it.effect('fails with a usage error naming <slug>, with a tip', () =>
258 Effect.gen(function* () {
259 const failure = yield* cli(['dev', 'toolkits', 'info']).pipe(Effect.flip);
260
261 expect(failure).toMatchObject({
262 _tag: 'commands/CommandInputError',
263 command: 'dev toolkits info',
264 option: '<slug>',
265 message:
266 'Missing required argument: <slug>\nTry specifying a toolkit slug, e.g.:\n> composio dev toolkits info "gmail"',
267 });
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 );
275
276 layer(TestLive())('[Given] no API key', it => {
277 it.effect('warns user to login', () =>
278 Effect.gen(function* () {
279 const error = yield* cli(['dev', 'toolkits', 'info', 'gmail']).pipe(Effect.flip);
280 expect(error).toMatchObject({ _tag: 'commands/NotLoggedInError' });
281 })
282 );
283 });
284
285 layer(
286 TestLive({
287 baseConfigProvider: testConfigProvider,
288 toolkitsData,
289 fixture: 'global-test-user-id',
290 })
291 )('[Given] the tool-router fetch fails', it => {
292 it.effect('reports the failure on a redirected stderr and exits 1', () =>
293 Effect.gen(function* () {
294 const stub = stubClientResources(['toolRouter'], () =>
295 Response.json({ error: { message: 'Tool Router is down' } }, { status: 500 })
296 );
297 const terminal = redirectedTerminalUI();
298 const exit = yield* Effect.exit(
299 cli(['dev', 'toolkits', 'info', 'gmail']).pipe(
300 Effect.provide(Layer.mergeAll(stub.layer, terminal.layer))
301 )
302 );
303
304 expect(terminal.stderr()).toBe(
305 'Tool Router is down\nBrowse available toolkits:\n> composio dev toolkits list\n'
306 );
307 expect(terminal.stdout()).toBe('');
308 expect(exitCodeOf(exit)).toBe(1);
309 })
310 );
311 });
312 });