@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 286 lines 10.3 KB
Raw
1 import { describe, expect, layer } from '@effect/vitest';
2 import { ConfigProvider, Effect } from 'effect';
3 import { extractErrorDetails } from 'src/effect-errors/logic/errors/extract-error-details';
4 import { cli, MockConsole, TestLive } from 'test/__utils__';
5 import { exitCodeOf } from 'test/__utils__/exit-code';
6 import { redirectedTerminalUI } from 'test/__utils__/services/redirected-terminal-ui';
7 import { routeRequests, stubClientResources } from 'test/__utils__/services/stub-composio-client';
8
9 const sendEmailInput = {
10 type: 'object',
11 properties: { recipient_email: { type: 'string' }, body: { type: 'string' } },
12 required: ['recipient_email'],
13 };
14
15 const testLayer = TestLive({
16 baseConfigProvider: ConfigProvider.fromEnv({ env: { COMPOSIO_USER_API_KEY: 'test_api_key' } }),
17 fixture: 'global-test-user-id',
18 toolkitsData: {
19 tools: [
20 {
21 name: 'Send Email',
22 slug: 'GMAIL_SEND_EMAIL',
23 description: 'Send an email',
24 tags: [],
25 available_versions: ['20260101_00'],
26 input_parameters: sendEmailInput,
27 output_parameters: { type: 'object', properties: {} },
28 },
29 {
30 name: 'Create Draft',
31 slug: 'GMAIL_CREATE_EMAIL_DRAFT',
32 description: 'Create a draft',
33 tags: [],
34 available_versions: ['20260101_00'],
35 input_parameters: { type: 'object', properties: {} },
36 output_parameters: { type: 'object', properties: {} },
37 },
38 ],
39 },
40 });
41
42 const searchResponse = {
43 success: true,
44 error: null,
45 results: [
46 {
47 index: 1,
48 use_case: 'send an email',
49 difficulty: 'easy',
50 primary_tool_slugs: ['GMAIL_SEND_EMAIL'],
51 related_tool_slugs: ['GMAIL_CREATE_EMAIL_DRAFT'],
52 toolkits: ['gmail'],
53 },
54 ],
55 toolkit_connection_statuses: [
56 {
57 toolkit: 'gmail',
58 description: 'Gmail toolkit',
59 has_active_connection: true,
60 status_message: 'Connected',
61 },
62 ],
63 tool_schemas: {
64 GMAIL_SEND_EMAIL: {
65 toolkit: 'gmail',
66 tool_slug: 'GMAIL_SEND_EMAIL',
67 description: 'Send an email',
68 hasFullSchema: true,
69 input_schema: sendEmailInput,
70 },
71 GMAIL_CREATE_EMAIL_DRAFT: {
72 toolkit: 'gmail',
73 tool_slug: 'GMAIL_CREATE_EMAIL_DRAFT',
74 description: 'Create a draft',
75 hasFullSchema: true,
76 input_schema: { type: 'object', properties: {} },
77 },
78 },
79 time_info: {
80 current_time_utc: '2026-01-01T00:00:00.000Z',
81 current_time_utc_epoch_seconds: 1767225600,
82 message: 'UTC time',
83 },
84 session: { id: 'trs_stub_session', generate_id: false, instructions: 'Reuse this session id.' },
85 next_steps_guidance: [],
86 };
87
88 const SEARCH_PATH = '/api/v3.1/tool_router/session/trs_stub_session/search';
89
90 /** Searches answered by `respond`; the session they run in is created normally. */
91 const searchingOver = (respond: () => Response) =>
92 stubClientResources(
93 ['toolRouter'],
94 routeRequests([{ method: 'POST', path: SEARCH_PATH, respond }], () =>
95 Response.json({
96 session_id: 'trs_stub_session',
97 config: { user_id: 'consumer-user-org_test' },
98 mcp: { type: 'http', url: 'https://mcp.stub.invalid' },
99 tool_router_tools: [],
100 })
101 )
102 );
103
104 describe('CLI: composio search over the API client', () => {
105 layer(testLayer)('[Given] a search response', it => {
106 it.effect('[Then] the JSON payload lists the tools, their schemas and the next steps', () =>
107 Effect.gen(function* () {
108 const stub = searchingOver(() => Response.json(searchResponse));
109 yield* cli(['search', 'send an email']).pipe(Effect.provide(stub.layer));
110 const lines = yield* MockConsole.getLines({ stripAnsi: true });
111
112 const payload = {
113 results: [
114 {
115 index: 1,
116 use_case: 'send an email',
117 difficulty: 'easy',
118 primary_tool_slugs: ['GMAIL_SEND_EMAIL'],
119 related_tool_slugs: ['GMAIL_CREATE_EMAIL_DRAFT'],
120 toolkits: ['gmail'],
121 },
122 ],
123 tool_schemas: {
124 primary: { GMAIL_SEND_EMAIL: '~/.composio/tool_definitions/GMAIL_SEND_EMAIL.json' },
125 related_tools_path_format: '~/.composio/tool_definitions/<TOOL_SLUG>.json',
126 },
127 connected_toolkits: ['gmail'],
128 next_steps: {
129 guidance:
130 'You can directly proceed with these steps without waiting for the user to ask. Link accounts first if needed, then execute tools.',
131 steps: [
132 { action: 'Link a user account', command: 'composio link gmail' },
133 {
134 action: 'Execute a tool',
135 command: `composio execute "GMAIL_SEND_EMAIL" -d '{"recipient_email":"","body":""}'`,
136 },
137 ],
138 },
139 };
140 // Once as the log line and once as the piped output.
141 expect(lines).toEqual([JSON.stringify(payload, null, 2), JSON.stringify(payload, null, 2)]);
142 // A result keeps the API's key order on stdout.
143 expect(Object.keys(JSON.parse(String(lines.at(-1))).results[0])).toEqual([
144 'index',
145 'use_case',
146 'difficulty',
147 'primary_tool_slugs',
148 'related_tool_slugs',
149 'toolkits',
150 ]);
151 expect(stub.requests.filter(request => request.path === SEARCH_PATH)).toEqual([
152 {
153 method: 'POST',
154 path: SEARCH_PATH,
155 body: { queries: [{ use_case: 'send an email' }] },
156 },
157 ]);
158 })
159 );
160 });
161
162 layer(testLayer)('[Given] a search response in a shape the API contract does not allow', it => {
163 it.effect('[Then] the tools and connected toolkits are still reported', () =>
164 Effect.gen(function* () {
165 // A sharing model the client does not know, no `status_message`, no
166 // `description`, no `time_info`, and no `session`.
167 const driftedResponse = {
168 success: true,
169 error: null,
170 results: searchResponse.results,
171 toolkit_connection_statuses: [
172 { toolkit: 'gmail', has_active_connection: true, account_type: 'ORG' },
173 ],
174 tool_schemas: searchResponse.tool_schemas,
175 next_steps_guidance: [],
176 };
177 const stub = searchingOver(() => Response.json(driftedResponse));
178 yield* cli(['search', 'send an email']).pipe(Effect.provide(stub.layer));
179 const lines = yield* MockConsole.getLines({ stripAnsi: true });
180 const payload = JSON.parse(String(lines.at(-1)));
181
182 expect(payload.results).toEqual(searchResponse.results);
183 expect(payload.tool_schemas.primary).toEqual({
184 GMAIL_SEND_EMAIL: '~/.composio/tool_definitions/GMAIL_SEND_EMAIL.json',
185 });
186 expect(payload.connected_toolkits).toEqual(['gmail']);
187 })
188 );
189 });
190
191 layer(testLayer)('[Given] a 200 search response the command cannot read', it => {
192 it.effect.each([
193 { name: 'JSON of another shape', respond: () => Response.json({ unexpected: true }) },
194 {
195 name: 'a result without its tool slugs',
196 respond: () =>
197 Response.json({
198 ...searchResponse,
199 results: [{ index: 1, use_case: 'send an email', toolkits: ['gmail'] }],
200 }),
201 },
202 {
203 name: 'an empty body',
204 respond: () =>
205 new Response(null, {
206 status: 200,
207 headers: { 'content-type': 'application/json', 'content-length': '0' },
208 }),
209 },
210 ])('[Then] $name fails as a response this CLI does not understand', ({ respond }) =>
211 Effect.gen(function* () {
212 const stub = searchingOver(respond);
213 const terminal = redirectedTerminalUI();
214 const exit = yield* Effect.exit(
215 cli(['search', 'send an email']).pipe(Effect.provide([stub.layer, terminal.layer]))
216 );
217
218 expect(exitCodeOf(exit)).toBe(1);
219 expect(terminal.stdout()).toBe('');
220 expect(terminal.stderr()).toContain(
221 "Failed to search tools.\nCaused by: The API response for 'toolRouter.session.search' is not in the shape this version of the CLI expects.\nUpgrade the CLI, then retry:\n> composio upgrade"
222 );
223 expect(terminal.stderr()).not.toContain('TypeError');
224 })
225 );
226 });
227
228 layer(testLayer)('[Given] the API rejects the search', it => {
229 it.effect('[Then] the command fails with the status and response body as the cause', () =>
230 Effect.gen(function* () {
231 const stub = searchingOver(() =>
232 Response.json(
233 {
234 error: { message: 'Query too long', code: 1400, slug: 'Search_Invalid', status: 400 },
235 },
236 { status: 400 }
237 )
238 );
239 const failure = yield* cli(['search', 'send an email']).pipe(
240 Effect.provide(stub.layer),
241 Effect.flip
242 );
243
244 expect(failure).toMatchObject({ _tag: 'commands/ToolsSearchRequestError' });
245 expect(extractErrorDetails(failure).message).toBe(
246 'Failed to search tools.\nCaused by: 400 {"error":{"message":"Query too long","code":1400,"slug":"Search_Invalid","status":400}}'
247 );
248 })
249 );
250 });
251
252 // A stored login, not COMPOSIO_USER_API_KEY, whose key the backend no longer accepts.
253 layer(TestLive({ fixture: 'global-test-user-id' }))(
254 '[Given] the API rejects the stored key',
255 it => {
256 it.effect('[Then] the rejection is on stderr with `composio login` as the way out', () =>
257 Effect.gen(function* () {
258 // Every request is rejected, the project lookup before the search first.
259 const stub = stubClientResources(['_internal', 'toolRouter', 'connectedAccounts'], () =>
260 Response.json(
261 {
262 error: {
263 message: 'Invalid API key',
264 code: 401,
265 slug: 'Auth_InvalidApiKey',
266 status: 401,
267 },
268 },
269 { status: 401 }
270 )
271 );
272 const terminal = redirectedTerminalUI();
273 const exit = yield* Effect.exit(
274 cli(['search', 'send an email']).pipe(Effect.provide([stub.layer, terminal.layer]))
275 );
276
277 expect(exitCodeOf(exit)).toBe(1);
278 expect(terminal.stdout()).toBe('');
279 expect(terminal.stderr()).toBe(
280 'Invalid API key\nIf your login expired, log in again, then retry:\n> composio logout && COMPOSIO_WEB_URL=https://platform.composio.dev composio login\n'
281 );
282 })
283 );
284 }
285 );
286 });