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 526 lines 21.5 KB
Raw
1 import path from 'node:path';
2 import { describe, expect, layer } from '@effect/vitest';
3 import { Effect } from 'effect';
4 import * as FileSystem from 'effect/FileSystem';
5 import { cli, liveEnvConfigProvider, TestLive } from 'test/__utils__';
6 import { makeTestToolkits } from 'test/__utils__/models/toolkits';
7 import { NodeProcess } from 'src/services/node-process';
8 import { TRIGGER_TYPES_GMAIL } from 'test/__mocks__/trigger-types-gmail';
9 import { TestLiveInput } from 'test/__utils__/services/test-layer';
10 import { assertPythonIsValid } from 'test/__utils__/python-compiler';
11 import { TOOLS_TYPES_GMAIL } from 'test/__mocks__/tools-types-gmail';
12
13 describe('CLI: composio generate py', () => {
14 const appClientData = {
15 toolkits: makeTestToolkits([
16 {
17 name: 'Gmail',
18 slug: 'gmail',
19 },
20 {
21 name: 'Slack',
22 slug: 'slack',
23 },
24 ]),
25 tools: [...TOOLS_TYPES_GMAIL.slice(0, 3)],
26 triggerTypesAsEnums: [...TRIGGER_TYPES_GMAIL.slice(0, 3).map(triggerType => triggerType.slug)],
27 triggerTypes: [...TRIGGER_TYPES_GMAIL.slice(0, 3)],
28 } satisfies TestLiveInput['toolkitsData'];
29
30 layer(
31 TestLive({
32 fixture: 'python-project-with-composio-core',
33 toolkitsData: appClientData,
34 // The COMPOSIO_TOOLKIT_VERSION_* scenarios below stub the environment.
35 baseConfigProvider: liveEnvConfigProvider,
36 })
37 )(it => {
38 describe('[Given] valid fetched app data', () => {
39 it.effect.skip(
40 '[Given] no arguments [Then] it generates type stubs relative the `composio` module if detectable by uv',
41 () =>
42 Effect.gen(function* () {
43 const process = yield* NodeProcess;
44 const cwd = process.cwd;
45 const fs = yield* FileSystem.FileSystem;
46
47 const args = ['generate', 'py'];
48 yield* cli(args);
49
50 const outputDir = path.join(
51 cwd,
52 '.venv',
53 'lib',
54 'python3.12',
55 'site-packages',
56 'composio'
57 );
58
59 const gmailSourceCode = yield* fs.readFileString(path.join(outputDir, 'gmail.py'));
60 const slackSourceCode = yield* fs.readFileString(path.join(outputDir, 'slack.py'));
61
62 expect(gmailSourceCode).toMatchInlineSnapshot(`
63 "# Auto-generated by Composio CLI via \`composio py generate\`.
64 # Do not modify manually.
65
66 class GMAIL:
67 """Map of Composio's GMAIL toolkit."""
68
69 slug: str = "gmail"
70
71 class tools:
72 CREATE_EMAIL_DRAFT = "GMAIL_CREATE_EMAIL_DRAFT"
73 DELETE_MESSAGE = "GMAIL_DELETE_MESSAGE"
74 FETCH_EMAILS = "GMAIL_FETCH_EMAILS"
75
76 class triggers:
77 NEW_GMAIL_MESSAGE = "GMAIL_NEW_GMAIL_MESSAGE"
78 "
79 `);
80 expect(slackSourceCode).toMatchInlineSnapshot(`
81 "# Auto-generated by Composio CLI via \`composio py generate\`.
82 # Do not modify manually.
83
84 class SLACK:
85 """Map of Composio's SLACK toolkit."""
86
87 slug: str = "slack"
88
89 class tools:
90 pass
91
92 class triggers:
93 pass
94 "
95 `);
96
97 assertPythonIsValid({ files: { 'gmail.py': gmailSourceCode } });
98 assertPythonIsValid({ files: { 'slack.py': slackSourceCode } });
99 assertPythonIsValid({
100 files: {
101 'gmail.py': gmailSourceCode,
102 'slack.py': slackSourceCode,
103 },
104 });
105 })
106 );
107
108 it.effect(
109 '[Given] --output-dir [Then] it generates type stubs relative to the given output directory',
110 Effect.fn(function* () {
111 const process = yield* NodeProcess;
112 const cwd = process.cwd;
113 const fs = yield* FileSystem.FileSystem;
114
115 const outputDir = path.join(cwd, '.generated', 'composio-py');
116
117 const args = ['generate', 'py', '--output-dir', outputDir];
118 yield* cli(args);
119
120 const gmailSourceCode = yield* fs.readFileString(path.join(outputDir, 'gmail.py'));
121 const slackSourceCode = yield* fs.readFileString(path.join(outputDir, 'slack.py'));
122
123 expect(gmailSourceCode).toMatchInlineSnapshot(`
124 "# Auto-generated by Composio CLI. Do not modify manually..
125
126 class GMAIL:
127 """Map of Composio's GMAIL toolkit."""
128
129 slug: str = "gmail"
130
131 class tools:
132 pass
133
134 class triggers:
135 NEW_GMAIL_MESSAGE = {
136 "config": {
137 "properties": {
138 "interval": {
139 "default": 1,
140 "description": "Periodic Interval to Check for Updates & Send a Trigger in Minutes",
141 "title": "Interval",
142 "type": "number"
143 },
144 "labelIds": {
145 "default": "INBOX",
146 "description": "Filter messages by a single label ID. Labels identify the status or category of messages. Supported labels include 'INBOX', 'SPAM', 'TRASH', 'UNREAD', 'STARRED', 'IMPORTANT', 'CATEGORY_PERSONAL', 'CATEGORY_SOCIAL', 'CATEGORY_PROMOTIONS', 'CATEGORY_UPDATES', and 'CATEGORY_FORUMS'. For complex label filtering, use the 'query' parameter instead.",
147 "examples": ["INBOX", "UNREAD", "STARRED"],
148 "title": "Labelids",
149 "type": "string"
150 },
151 "query": {
152 "default": "",
153 "description": "Advanced Gmail search using the same syntax as Gmail's search box. Use 'AND' for messages that match all conditions, 'OR' for any condition. Search by sender (from:email@domain.com), labels (label:inbox), status (is:unread), attachments (has:attachment), dates (after:2023/1/1), and more. If specified, this takes precedence over labelIds.",
154 "examples": ["label:inbox OR label:sent", "from:example@gmail.com is:unread", "has:attachment after:2023/1/1", "is:important is:unread"],
155 "title": "Query",
156 "type": "string"
157 },
158 "userId": {
159 "default": "me",
160 "description": "The user's email address or 'me' for the authenticated user.",
161 "examples": ["me"],
162 "title": "Userid",
163 "type": "string"
164 }
165 },
166 "title": "NewMessageConfig",
167 "type": "object"
168 },
169 "description": "Triggers when a new message is received in Gmail.",
170 "instructions": "\\n **Instructions for Setting Up the Trigger:**\\n\\n - Ensure that the Gmail API is enabled for your Google account.\\n - Provide the user ID (usually 'me' for the authenticated user).\\n - Optionally, provide label IDs to filter messages.\\n ",
171 "name": "New Gmail Message Received Trigger",
172 "payload": {
173 "properties": {
174 "attachment_list": {
175 "anyOf": [{
176 "items": {},
177 "type": "array"
178 }, {
179 "type": "null"
180 }],
181 "default": None,
182 "description": "The list of attachments in the message",
183 "title": "Attachment List"
184 },
185 "message_id": {
186 "anyOf": [{
187 "type": "string"
188 }, {
189 "type": "null"
190 }],
191 "default": None,
192 "description": "The message ID of the message",
193 "title": "Message ID"
194 },
195 "message_text": {
196 "anyOf": [{
197 "type": "string"
198 }, {
199 "type": "null"
200 }],
201 "default": None,
202 "description": "The text of the message",
203 "title": "Message Text"
204 },
205 "message_timestamp": {
206 "anyOf": [{
207 "type": "string"
208 }, {
209 "type": "null"
210 }],
211 "default": None,
212 "description": "The timestamp of the message",
213 "title": "Message Timestamp"
214 },
215 "payload": {
216 "anyOf": [{
217 "type": "object"
218 }, {
219 "type": "null"
220 }],
221 "default": None,
222 "description": "The payload of the message",
223 "title": "Payload"
224 },
225 "sender": {
226 "anyOf": [{
227 "type": "string"
228 }, {
229 "type": "null"
230 }],
231 "default": None,
232 "description": "The sender of the message",
233 "title": "Sender"
234 },
235 "subject": {
236 "anyOf": [{
237 "type": "string"
238 }, {
239 "type": "null"
240 }],
241 "default": None,
242 "description": "The subject of the message",
243 "title": "Subject"
244 },
245 "thread_id": {
246 "anyOf": [{
247 "type": "string"
248 }, {
249 "type": "null"
250 }],
251 "default": None,
252 "description": "The thread ID of the message",
253 "title": "Thread ID"
254 },
255 "to": {
256 "anyOf": [{
257 "type": "string"
258 }, {
259 "type": "null"
260 }],
261 "default": None,
262 "description": "The recipient of the message",
263 "title": "To"
264 }
265 },
266 "title": "NewMessagePayload",
267 "type": "object"
268 },
269 "slug": "GMAIL_NEW_GMAIL_MESSAGE",
270 "type": "poll"
271 }
272 "
273 `);
274 expect(slackSourceCode).toMatchInlineSnapshot(`
275 "# Auto-generated by Composio CLI. Do not modify manually..
276
277 class SLACK:
278 """Map of Composio's SLACK toolkit."""
279
280 slug: str = "slack"
281
282 class tools:
283 pass
284
285 class triggers:
286 pass
287 "
288 `);
289
290 assertPythonIsValid({ files: { 'gmail.py': gmailSourceCode } });
291 assertPythonIsValid({ files: { 'slack.py': slackSourceCode } });
292 assertPythonIsValid({
293 files: {
294 'gmail.py': gmailSourceCode,
295 'slack.py': slackSourceCode,
296 },
297 });
298 })
299 );
300
301 it.effect(
302 '[Given] --toolkits gmail [Then] it generates type stubs only for the gmail toolkit',
303 Effect.fn(function* () {
304 const process = yield* NodeProcess;
305 const cwd = process.cwd;
306 const fs = yield* FileSystem.FileSystem;
307 const outputDir = path.join(cwd, '.generated', 'composio-py-filtered');
308
309 const args = ['generate', 'py', '--toolkits', 'gmail', '--output-dir', outputDir];
310 yield* cli(args);
311
312 // Check generated files - only gmail.py should exist (no slack.py)
313 const files = yield* fs.readDirectory(outputDir);
314 const fileNames = files.map(file => path.basename(file));
315
316 expect(fileNames).toContain('gmail.py');
317 expect(fileNames).not.toContain('slack.py');
318
319 // Verify gmail.py contains GMAIL class
320 const gmailSourceCode = yield* fs.readFileString(path.join(outputDir, 'gmail.py'));
321 expect(gmailSourceCode).toContain('GMAIL');
322 })
323 );
324
325 it.effect(
326 '[Given] --toolkits gmail --toolkits slack [Then] it generates type stubs for both toolkits',
327 Effect.fn(function* () {
328 const process = yield* NodeProcess;
329 const cwd = process.cwd;
330 const fs = yield* FileSystem.FileSystem;
331 const outputDir = path.join(cwd, '.generated', 'composio-py-multi');
332
333 const args = [
334 'generate',
335 'py',
336 '--toolkits',
337 'gmail',
338 '--toolkits',
339 'slack',
340 '--output-dir',
341 outputDir,
342 ];
343 yield* cli(args);
344
345 // Check generated files - both gmail.py and slack.py should exist
346 const files = yield* fs.readDirectory(outputDir);
347 const fileNames = files.map(file => path.basename(file));
348
349 expect(fileNames).toContain('gmail.py');
350 expect(fileNames).toContain('slack.py');
351
352 // Verify both files contain their respective classes
353 const gmailSourceCode = yield* fs.readFileString(path.join(outputDir, 'gmail.py'));
354 const slackSourceCode = yield* fs.readFileString(path.join(outputDir, 'slack.py'));
355 expect(gmailSourceCode).toContain('GMAIL');
356 expect(slackSourceCode).toContain('SLACK');
357 })
358 );
359
360 it.effect(
361 '[Given] --toolkits with invalid toolkit [Then] it fails with an error',
362 Effect.fn(function* () {
363 const process = yield* NodeProcess;
364 const cwd = process.cwd;
365 const outputDir = path.join(cwd, '.generated', 'composio-py-invalid');
366
367 const args = ['generate', 'py', '--toolkits', 'nonexistent', '--output-dir', outputDir];
368 const failure = yield* cli(args).pipe(Effect.flip);
369
370 expect(failure).toMatchObject({
371 _tag: 'commands/CommandInputError',
372 message: expect.stringContaining('Invalid toolkit(s): nonexistent'),
373 });
374 })
375 );
376
377 it.effect(
378 '[Given] --toolkits GMAIL (uppercase) [Then] it handles case-insensitive matching',
379 Effect.fn(function* () {
380 const process = yield* NodeProcess;
381 const cwd = process.cwd;
382 const fs = yield* FileSystem.FileSystem;
383 const outputDir = path.join(cwd, '.generated', 'composio-py-uppercase');
384
385 const args = ['generate', 'py', '--toolkits', 'GMAIL', '--output-dir', outputDir];
386 yield* cli(args);
387
388 // Check generated files - only gmail.py should exist
389 const files = yield* fs.readDirectory(outputDir);
390 const fileNames = files.map(file => path.basename(file));
391
392 expect(fileNames).toContain('gmail.py');
393 expect(fileNames).not.toContain('slack.py');
394 })
395 );
396 });
397
398 describe('[Given] COMPOSIO_TOOLKIT_VERSION_* env vars', () => {
399 it.effect(
400 '[Given] COMPOSIO_TOOLKIT_VERSION_GMAIL env var [Then] it adds version comment to generated file',
401 () =>
402 Effect.gen(function* () {
403 const { vi } = yield* Effect.promise(() => import('vitest'));
404 vi.stubEnv('COMPOSIO_TOOLKIT_VERSION_GMAIL', '20250901_00');
405
406 const process = yield* NodeProcess;
407 const cwd = process.cwd;
408 const fs = yield* FileSystem.FileSystem;
409 const outputDir = path.join(cwd, '.generated', 'composio-py-version');
410
411 const args = ['generate', 'py', '--output-dir', outputDir];
412 yield* cli(args);
413
414 // Check that gmail.py contains the version comment
415 const gmailSourceCode = yield* fs.readFileString(path.join(outputDir, 'gmail.py'));
416 expect(gmailSourceCode).toContain('# @toolkit-version: 20250901_00');
417
418 // Check that slack.py does NOT contain a version comment
419 const slackSourceCode = yield* fs.readFileString(path.join(outputDir, 'slack.py'));
420 expect(slackSourceCode).not.toContain('@toolkit-version');
421
422 vi.unstubAllEnvs();
423 })
424 );
425
426 it.effect(
427 '[Given] multiple COMPOSIO_TOOLKIT_VERSION_* env vars [Then] it adds version comments to both generated files',
428 () =>
429 Effect.gen(function* () {
430 const { vi } = yield* Effect.promise(() => import('vitest'));
431 vi.stubEnv('COMPOSIO_TOOLKIT_VERSION_GMAIL', '20250901_00');
432 vi.stubEnv('COMPOSIO_TOOLKIT_VERSION_SLACK', '20250815_00');
433
434 const process = yield* NodeProcess;
435 const cwd = process.cwd;
436 const fs = yield* FileSystem.FileSystem;
437 const outputDir = path.join(cwd, '.generated', 'composio-py-multi-version');
438
439 const args = ['generate', 'py', '--output-dir', outputDir];
440 yield* cli(args);
441
442 const gmailSourceCode = yield* fs.readFileString(path.join(outputDir, 'gmail.py'));
443 expect(gmailSourceCode).toContain('# @toolkit-version: 20250901_00');
444
445 const slackSourceCode = yield* fs.readFileString(path.join(outputDir, 'slack.py'));
446 expect(slackSourceCode).toContain('# @toolkit-version: 20250815_00');
447
448 vi.unstubAllEnvs();
449 })
450 );
451
452 it.effect(
453 '[Given] COMPOSIO_TOOLKIT_VERSION_GMAIL=latest [Then] it treats same as no override (no version comment)',
454 () =>
455 Effect.gen(function* () {
456 const { vi } = yield* Effect.promise(() => import('vitest'));
457 vi.stubEnv('COMPOSIO_TOOLKIT_VERSION_GMAIL', 'latest');
458
459 const process = yield* NodeProcess;
460 const cwd = process.cwd;
461 const fs = yield* FileSystem.FileSystem;
462 const outputDir = path.join(cwd, '.generated', 'composio-py-latest');
463
464 const args = ['generate', 'py', '--output-dir', outputDir];
465 yield* cli(args);
466
467 const gmailSourceCode = yield* fs.readFileString(path.join(outputDir, 'gmail.py'));
468 expect(gmailSourceCode).not.toContain('@toolkit-version');
469
470 vi.unstubAllEnvs();
471 })
472 );
473
474 it.effect(
475 '[Given] --toolkits gmail and COMPOSIO_TOOLKIT_VERSION_SLACK env var [Then] it ignores env var for non-requested toolkit',
476 () =>
477 Effect.gen(function* () {
478 const { vi } = yield* Effect.promise(() => import('vitest'));
479 vi.stubEnv('COMPOSIO_TOOLKIT_VERSION_SLACK', '20250815_00');
480
481 const process = yield* NodeProcess;
482 const cwd = process.cwd;
483 const fs = yield* FileSystem.FileSystem;
484 const outputDir = path.join(cwd, '.generated', 'composio-py-filtered-env');
485
486 const args = ['generate', 'py', '--toolkits', 'gmail', '--output-dir', outputDir];
487 yield* cli(args);
488
489 // Only gmail.py should exist
490 const files = yield* fs.readDirectory(outputDir);
491 const fileNames = files.map(file => path.basename(file));
492 expect(fileNames).toContain('gmail.py');
493 expect(fileNames).not.toContain('slack.py');
494
495 // gmail.py should NOT have version comment
496 const gmailSourceCode = yield* fs.readFileString(path.join(outputDir, 'gmail.py'));
497 expect(gmailSourceCode).not.toContain('@toolkit-version');
498
499 vi.unstubAllEnvs();
500 })
501 );
502
503 it.effect(
504 '[Given] --toolkits gmail and COMPOSIO_TOOLKIT_VERSION_GMAIL env var [Then] it applies version override to filtered toolkit',
505 () =>
506 Effect.gen(function* () {
507 const { vi } = yield* Effect.promise(() => import('vitest'));
508 vi.stubEnv('COMPOSIO_TOOLKIT_VERSION_GMAIL', '20250901_00');
509
510 const process = yield* NodeProcess;
511 const cwd = process.cwd;
512 const fs = yield* FileSystem.FileSystem;
513 const outputDir = path.join(cwd, '.generated', 'composio-py-filtered-version');
514
515 const args = ['generate', 'py', '--toolkits', 'gmail', '--output-dir', outputDir];
516 yield* cli(args);
517
518 const gmailSourceCode = yield* fs.readFileString(path.join(outputDir, 'gmail.py'));
519 expect(gmailSourceCode).toContain('# @toolkit-version: 20250901_00');
520
521 vi.unstubAllEnvs();
522 })
523 );
524 });
525 });
526 });