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 3,680 lines 123 KB
Raw
1 import * as fs from 'node:fs';
2 import * as os from 'node:os';
3 import * as path from 'node:path';
4 import { PassThrough } from 'node:stream';
5 import { describe, expect, it, layer } from '@effect/vitest';
6 import { vi, beforeEach, afterEach } from 'vitest';
7 import { Config, ConfigProvider, DateTime, Effect, Exit, Option, Schema } from 'effect';
8 import { APIConnectionError, BadRequestError } from '@composio/client/effect';
9 import { ComposioNoActiveConnectionError } from 'src/services/composio-error-overrides';
10 import { setupCacheDir } from 'src/effects/setup-cache-dir';
11 import { getOrFetchToolInputDefinition } from 'src/services/tool-input-validation';
12 import { clearInProcessMemos } from 'src/utils/memoize-in-process';
13 import * as consumerShortTermCache from 'src/services/consumer-short-term-cache';
14 import * as composioAccount from 'src/services/composio-account';
15 import type { ToolkitProjectScope } from 'src/services/composio-clients';
16 import { MissingActingUserError } from 'src/commands/surface';
17 import { ComposioUserContext } from 'src/services/user-context';
18 import * as toolPermissions from 'src/services/tool-permissions';
19 import * as redactModule from 'src/ui/redact';
20 import { cli, TestLive, MockConsole } from 'test/__utils__';
21 import { liveEnvConfigProvider } from 'test/__utils__/live-env-config-provider';
22 import type { TestLiveInput } from 'test/__utils__/services/test-layer';
23 import { parseParallelExecuteArgs } from 'src/commands/tools/execute/tools.execute.parallel';
24 import { ComposioCliUserConfig } from 'src/services/cli-user-config';
25 import type { ToolkitDetailed } from 'src/models/toolkits';
26 import { makeToolkitFixture } from 'test/__utils__/models/toolkits';
27 import { redirectedTerminalUI } from 'test/__utils__/services/redirected-terminal-ui';
28
29 vi.mock('node:dns/promises', () => ({
30 lookup: vi.fn().mockResolvedValue([{ address: '93.184.216.34', family: 4 }]),
31 }));
32
33 // Disable CI redaction so tests see raw values. `src/ui/redact` reads `CI` once
34 // at import time, so the variable has to be gone before the imports above run —
35 // `vi.hoisted` executes ahead of them. The explicit CI-redaction test overrides
36 // via `vi.spyOn` and is unaffected.
37 vi.hoisted(() => {
38 delete process.env.CI;
39 });
40
41 const testConfigProvider = ConfigProvider.fromEnv({
42 env: { COMPOSIO_USER_API_KEY: 'test_api_key' },
43 });
44
45 type ApiErrorEnvelope = NonNullable<BadRequestError['details']>;
46
47 // The client's error for a 400 answered with `envelope`.
48 const clientBadRequest = (envelope: ApiErrorEnvelope) =>
49 new BadRequestError({
50 message: `400 ${JSON.stringify({ error: envelope })}`,
51 method: 'POST',
52 url: 'https://mock.invalid/api/v3.1/tool_router/session/{session_id}/execute',
53 status: 400,
54 requestId: envelope.request_id,
55 details: envelope,
56 body: { error: envelope },
57 });
58
59 const noActiveConnectionEnvelope: ApiErrorEnvelope = {
60 message: "No active connection found for toolkit(s) 'gmail' in this session",
61 code: 4302,
62 slug: 'ToolRouterV2_NoActiveConnection',
63 status: 400,
64 request_id: 'test-request-id',
65 };
66
67 // `testConfigProvider` pins its env, so it bypasses the per-test
68 // `COMPOSIO_CACHE_DIR` stub, and the learned toolkit slugs resolve against the
69 // developer's real `~/.composio`. Suites that assert how a toolkit is learned
70 // need a cache directory of their own, seeded with the fixture's user data.
71 const isolatedCacheConfigProvider = (fixture: string) => {
72 const cacheDir = fs.mkdtempSync(path.join(os.tmpdir(), 'composio-cli-test-cache-'));
73 fs.cpSync(new URL(`../../../../__fixtures__/${fixture}/.composio`, import.meta.url), cacheDir, {
74 recursive: true,
75 });
76 // Fresh learned slugs keep the background catalog refresh from running, so
77 // every toolkit lookup a suite observes comes from the command itself.
78 fs.writeFileSync(
79 path.join(cacheDir, 'known-toolkit-slugs.json'),
80 JSON.stringify({ slugs: [], refreshedAt: new Date().toISOString() })
81 );
82 return ConfigProvider.fromEnv({
83 env: { COMPOSIO_USER_API_KEY: 'test_api_key', COMPOSIO_CACHE_DIR: cacheDir },
84 });
85 };
86
87 const runInvocationConfigProvider = ConfigProvider.fromEnvRecord({
88 COMPOSIO_USER_API_KEY: 'test_api_key',
89 COMPOSIO_CLI_INVOCATION_ORIGIN: 'run',
90 });
91
92 // Reuse the suite-managed cache directory for artifacts without exposing the
93 // empty test cache as the CLI's authenticated config directory: only
94 // `COMPOSIO_SESSION_DIR` resolves, to the stubbed `COMPOSIO_CACHE_DIR`.
95 const testArtifactConfigProvider = liveEnvConfigProvider.pipe(
96 ConfigProvider.mapInput(configPath =>
97 configPath.map(segment =>
98 segment === 'COMPOSIO_SESSION_DIR'
99 ? 'COMPOSIO_CACHE_DIR'
100 : typeof segment === 'string'
101 ? `UNSET_${segment}`
102 : segment
103 )
104 )
105 );
106
107 const largeOutputConfigProvider = ConfigProvider.fromEnvRecord({
108 COMPOSIO_USER_API_KEY: 'test_api_key',
109 }).pipe(ConfigProvider.orElse(testArtifactConfigProvider));
110
111 const expectInvalidValueMessage = (failure: unknown, message: string) =>
112 expect(failure).toMatchObject({
113 _tag: 'commands/CommandInputError',
114 message: expect.stringContaining(message),
115 });
116
117 /** The command's JSON result: `ui.output` prints it as the last console line. */
118 const parseLastJson = (lines: ReadonlyArray<string>) =>
119 JSON.parse(String(lines.at(-1))) as {
120 successful: boolean;
121 data: Record<string, unknown>;
122 error: string | null;
123 logId: string;
124 };
125
126 describe('CLI: composio execute', () => {
127 layer(
128 TestLive({
129 baseConfigProvider: testConfigProvider,
130 fixture: 'global-test-user-id',
131 stdin: { isTTY: true, data: '' },
132 toolkitsData: {
133 tools: [
134 {
135 name: 'Nested Image Edit',
136 slug: 'NESTED_IMAGE_EDIT_TOOL',
137 description: 'Edit fixture',
138 tags: ['test'],
139 available_versions: ['20260316_00'],
140 input_parameters: {
141 type: 'object',
142 properties: {
143 item_id: { type: 'string' },
144 operations: {
145 type: 'array',
146 items: {
147 oneOf: [
148 {
149 type: 'object',
150 properties: { type: { const: 'set_text' }, text: { type: 'string' } },
151 required: ['type', 'text'],
152 },
153 {
154 type: 'object',
155 properties: {
156 type: { const: 'add_image' },
157 image: { type: 'object', file_uploadable: true },
158 },
159 required: ['type', 'image'],
160 },
161 {
162 type: 'object',
163 properties: {
164 type: { const: 'replace_image' },
165 image: { type: 'object', file_uploadable: true },
166 },
167 required: ['type', 'image'],
168 },
169 ],
170 },
171 },
172 },
173 },
174 output_parameters: { type: 'object', properties: {} },
175 },
176 ],
177 } satisfies TestLiveInput['toolkitsData'],
178 })
179 )('nested --file array injection', it => {
180 it.effect('preserves sibling operations and injects into the selected array element', () =>
181 Effect.gen(function* () {
182 const operations = [{ type: 'set_text', text: 'Preserve me' }, { type: 'add_image' }];
183 yield* cli([
184 'execute',
185 'NESTED_IMAGE_EDIT_TOOL',
186 '--skip-connection-check',
187 '--dry-run',
188 '--file',
189 '/tmp/dogfood.png',
190 '-d',
191 JSON.stringify({ item_id: 'deck', operations }),
192 ]);
193 const output = Schema.decodeUnknownSync(Schema.Struct({ arguments: Schema.Unknown }))(
194 parseLastJson(yield* MockConsole.getLines({ stripAnsi: true }))
195 );
196 expect(output.arguments).toEqual({
197 item_id: 'deck',
198 operations: [
199 { type: 'set_text', text: 'Preserve me' },
200 { type: 'add_image', image: '/tmp/dogfood.png' },
201 ],
202 });
203 expect(operations).toEqual([
204 { type: 'set_text', text: 'Preserve me' },
205 { type: 'add_image' },
206 ]);
207 })
208 );
209
210 it.effect.each([
211 {
212 operations: [{ type: 'add_image' }, { type: 'replace_image' }],
213 message: 'multiple file_uploadable inputs',
214 },
215 {
216 operations: [{ type: 'add_image', image: '/tmp/existing.png' }],
217 message: 'already set in -d',
218 },
219 { operations: [], message: 'no matching file_uploadable input' },
220 {
221 operations: [{ type: 'set_text', text: 'Keep' }],
222 message: 'no matching file_uploadable input',
223 },
224 ])('rejects without overwriting: $message ($operations)', scenario =>
225 Effect.gen(function* () {
226 const failure = yield* cli([
227 'execute',
228 'NESTED_IMAGE_EDIT_TOOL',
229 '--skip-connection-check',
230 '--dry-run',
231 '--file',
232 '/tmp/dogfood.png',
233 '-d',
234 JSON.stringify({ operations: scenario.operations }),
235 ]).pipe(Effect.flip);
236 expect(String(failure)).toContain(scenario.message);
237 })
238 );
239 });
240
241 it.effect('returns a CLI validation error for malformed parallel arguments', () =>
242 Effect.gen(function* () {
243 const failure = yield* parseParallelExecuteArgs(['--parallel', '--data', '{}'], {
244 surface: 'root',
245 projectMode: 'consumer',
246 allowUserId: false,
247 allowProjectName: false,
248 }).pipe(Effect.flip);
249
250 expectInvalidValueMessage(failure, 'Expected a tool slug before --data');
251 })
252 );
253
254 beforeEach(() => {
255 vi.spyOn(composioAccount, 'getLatestToolVersion').mockImplementation(() =>
256 Effect.fail(
257 new APIConnectionError({
258 message: 'Connection error.',
259 method: 'GET',
260 url: 'https://backend.composio.dev/api/v3/tools/{tool_slug}/get_latest_version',
261 reason: 'TransportError',
262 description: undefined,
263 })
264 )
265 );
266 vi.spyOn(consumerShortTermCache, 'getFreshConsumerConnectedToolkitsFromCache').mockReturnValue(
267 Effect.succeed(Option.some(['gmail', 'github']))
268 );
269 vi.spyOn(consumerShortTermCache, 'refreshConsumerConnectedToolkitsCache').mockImplementation(
270 () => Effect.succeed(['gmail', 'github'])
271 );
272 });
273 afterEach(() => {
274 vi.restoreAllMocks();
275 });
276
277 let recordedSessionCreateParams: Array<Record<string, unknown>> = [];
278 let recordedExecuteParams: Array<Record<string, unknown>> = [];
279 // Records what one execution call carried; the account travels with the call, not the session.
280 const recordExecute = <T>(params: unknown, result: T) =>
281 Effect.sync(() => {
282 recordedExecuteParams.push(params as Record<string, unknown>);
283 return result;
284 });
285 let recordedProjectToolkitScopes: Array<ToolkitProjectScope | undefined> = [];
286 beforeEach(() => {
287 recordedSessionCreateParams = [];
288 recordedExecuteParams = [];
289 recordedProjectToolkitScopes = [];
290 });
291
292 layer(
293 TestLive({
294 baseConfigProvider: testConfigProvider,
295 fixture: 'global-test-user-id',
296 stdin: { isTTY: true, data: '' },
297 toolsExecutor: {
298 respondWith: {
299 successful: true,
300 data: { ok: true },
301 error: null,
302 logId: 'log_approval_status',
303 permissionApproval: 'approved_once',
304 },
305 },
306 })
307 )('[Given] an approval status [Then] execute includes it in the success line', it => {
308 it.effect('prints approval status before the log id', () =>
309 Effect.gen(function* () {
310 yield* cli([
311 'execute',
312 'GMAIL_SEND_EMAIL',
313 '--skip-connection-check',
314 '-d',
315 '{"recipient":"a"}',
316 ]);
317 const lines = yield* MockConsole.getLines({ stripAnsi: true });
318 const text = lines.join('\n');
319 const output = parseLastJson(lines) as ReturnType<typeof parseLastJson> & {
320 readonly permissionApproval?: string;
321 };
322
323 expect(text).toContain('Execution successful (approved once, logId: log_approval_status)');
324 expect(output.permissionApproval).toBe('approved_once');
325 })
326 );
327 });
328
329 layer(
330 TestLive({
331 baseConfigProvider: testConfigProvider,
332 fixture: 'global-test-user-id',
333 stdin: { isTTY: true, data: '' },
334 toolRouter: {
335 execute: (_sessionId, params) =>
336 Effect.succeed({
337 result_type: 'completed' as const,
338 data: { tool_slug: params.tool_slug },
339 error: null,
340 log_id: 'log_instant_charge',
341 instant_charge: {
342 amount: '0.005',
343 currency: 'USD' as const,
344 charged_by: 'composio' as const,
345 },
346 }),
347 },
348 })
349 )('[Given] an instant charge [Then] execute reports it in the success line and output', it => {
350 it.effect('prints the charge before the log id', () =>
351 Effect.gen(function* () {
352 yield* cli(['execute', 'EXA_SEARCH', '--skip-connection-check', '-d', '{"query":"a"}']);
353 const lines = yield* MockConsole.getLines({ stripAnsi: true });
354 const text = lines.join('\n');
355 const output = parseLastJson(lines);
356
357 expect(text).toContain(
358 'Execution successful (instant charge: 0.005 USD, logId: log_instant_charge)'
359 );
360 expect(output).toHaveProperty('instantCharge', {
361 amount: '0.005',
362 currency: 'USD',
363 chargedBy: 'composio',
364 });
365 })
366 );
367 });
368
369 layer(
370 TestLive({
371 baseConfigProvider: testConfigProvider,
372 fixture: 'global-test-user-id',
373 stdin: { isTTY: true, data: '' },
374 })
375 )('[Given] -d inline JSON [Then] executes via Tool Router with defaults', it => {
376 it.effect('executes via Tool Router with defaults', () =>
377 Effect.gen(function* () {
378 yield* cli([
379 'execute',
380 'GMAIL_SEND_EMAIL',
381 '--skip-connection-check',
382 '-d',
383 '{"recipient":"a"}',
384 ]);
385 const lines = yield* MockConsole.getLines({ stripAnsi: true });
386 const outputText = lines.join('\n');
387 const output = parseLastJson(lines);
388
389 expect(outputText).not.toContain('Response\n{');
390 // Response flows through real ToolsExecutorLive → test Dashboard → mock session.execute
391 expect(output.successful).toBe(true);
392 expect(output.data.tool_slug).toBe('GMAIL_SEND_EMAIL');
393 expect(output.data.arguments).toEqual({ recipient: 'a' });
394 expect(output.logId).toBe('log_test');
395 expect(output).not.toHaveProperty('instantCharge');
396 })
397 );
398 });
399
400 layer(
401 TestLive({
402 baseConfigProvider: runInvocationConfigProvider,
403 fixture: 'global-test-user-id',
404 stdin: { isTTY: true, data: '' },
405 toolsExecutor: {
406 respondWith: {
407 data: {
408 content: 'token '.repeat(20_000),
409 },
410 error: null,
411 successful: true,
412 logId: 'log_large_output',
413 },
414 },
415 })
416 )(
417 '[Given] a large execution response during composio run [Then] it stays inline instead of storing a temp file',
418 it => {
419 it.effect('returns the full JSON payload when invocation origin is run', () =>
420 Effect.gen(function* () {
421 yield* cli(['execute', 'GMAIL_SEND_EMAIL', '-d', '{"recipient":"a"}']);
422 const lines = yield* MockConsole.getLines({ stripAnsi: true });
423 const output = parseLastJson(lines) as unknown as {
424 successful: boolean;
425 storedInFile?: boolean;
426 outputFilePath?: string;
427 data: {
428 content: string;
429 };
430 };
431
432 expect(output.successful).toBe(true);
433 expect(output.storedInFile).not.toBe(true);
434 expect(output.outputFilePath).toBeUndefined();
435 expect(output.data.content).toContain('token token token');
436 })
437 );
438 }
439 );
440
441 layer(
442 TestLive({
443 baseConfigProvider: testConfigProvider,
444 fixture: 'global-test-user-id',
445 stdin: { isTTY: true, data: '' },
446 toolkitsData: {
447 tools: [
448 {
449 name: 'Send Email',
450 slug: 'GMAIL_SEND_EMAIL',
451 description: 'Send an email',
452 tags: ['email'],
453 available_versions: ['20260115_00'],
454 input_parameters: {
455 type: 'object',
456 properties: {
457 recipient: { type: 'string' },
458 },
459 },
460 output_parameters: { type: 'object', properties: {} },
461 },
462 ],
463 } satisfies TestLiveInput['toolkitsData'],
464 connectedAccountsData: {
465 items: [
466 {
467 id: 'con_gmail_default',
468 alias: 'default',
469 word_id: 'castle',
470 status: 'ACTIVE',
471 status_reason: null,
472 is_disabled: false,
473 user_id: 'consumer-user-org_test',
474 toolkit: { slug: 'gmail' },
475 auth_config: {
476 id: 'ac_gmail_oauth',
477 auth_scheme: 'OAUTH2',
478 is_composio_managed: true,
479 is_disabled: false,
480 },
481 created_at: '2026-01-01T00:00:00.000Z',
482 updated_at: '2026-01-01T00:00:00.000Z',
483 test_request_endpoint: '',
484 },
485 {
486 id: 'con_gmail_secondary',
487 alias: 'work',
488 word_id: 'forest',
489 status: 'ACTIVE',
490 status_reason: null,
491 is_disabled: false,
492 user_id: 'consumer-user-org_test',
493 toolkit: { slug: 'gmail' },
494 auth_config: {
495 id: 'ac_gmail_oauth',
496 auth_scheme: 'OAUTH2',
497 is_composio_managed: true,
498 is_disabled: false,
499 },
500 created_at: '2026-01-02T00:00:00.000Z',
501 updated_at: '2026-01-02T00:00:00.000Z',
502 test_request_endpoint: '',
503 },
504 ],
505 },
506 toolRouter: {
507 create: params =>
508 Effect.sync(() => {
509 recordedSessionCreateParams.push(params as unknown as Record<string, unknown>);
510 return {
511 session_id: 'trs_gmail_default_session',
512 config: {
513 user_id: params.user_id,
514 execute: {},
515 search: {},
516 preload: { tools: [] },
517 instant: false,
518 },
519 config_version: 1,
520 mcp: { type: 'http' as const, url: 'https://mcp.test.composio.dev' },
521 tool_router_tools: ['COMPOSIO_SEARCH_TOOLS', 'COMPOSIO_MANAGE_CONNECTIONS'],
522 };
523 }),
524 execute: (_sessionId, params) =>
525 recordExecute(params, {
526 result_type: 'completed' as const,
527 data: { tool_slug: params.tool_slug, arguments: params.arguments },
528 error: null,
529 log_id: 'log_gmail_default',
530 }),
531 },
532 })
533 )('[Given] default alias exists [Then] execute sends the default connected account', it => {
534 it.effect('sends the default alias account with the call', () =>
535 Effect.gen(function* () {
536 yield* cli([
537 'execute',
538 'GMAIL_SEND_EMAIL',
539 '--skip-connection-check',
540 '-d',
541 '{"recipient":"a"}',
542 ]);
543
544 expect(recordedExecuteParams[0]?.account).toBe('con_gmail_default');
545 })
546 );
547
548 it.effect('asks for the latest tool version once per execute on a schema cache hit', () =>
549 Effect.gen(function* () {
550 const latestVersion = vi
551 .spyOn(composioAccount, 'getLatestToolVersion')
552 .mockImplementation(({ toolSlug }) =>
553 Effect.succeed({ tool_slug: toolSlug, version: '20260115_00' })
554 );
555 // Warm the on-disk schema cache, then forget the memoized version so
556 // the next run has to ask the server again.
557 yield* getOrFetchToolInputDefinition('GMAIL_SEND_EMAIL');
558 clearInProcessMemos();
559 latestVersion.mockClear();
560
561 yield* cli([
562 'execute',
563 'GMAIL_SEND_EMAIL',
564 '--skip-connection-check',
565 '-d',
566 '{"recipient":"a"}',
567 ]);
568
569 // The command's own version check and the executor's schema lookup
570 // both run on this path; they must share one request.
571 expect(latestVersion).toHaveBeenCalledTimes(1);
572 })
573 );
574 });
575
576 layer(
577 TestLive({
578 baseConfigProvider: testConfigProvider,
579 fixture: 'global-test-user-id',
580 stdin: { isTTY: true, data: '' },
581 connectedAccountsData: {
582 items: [
583 {
584 id: 'con_gmail_default',
585 alias: 'default',
586 word_id: 'castle',
587 status: 'ACTIVE',
588 status_reason: null,
589 is_disabled: false,
590 user_id: 'consumer-user-org_test',
591 toolkit: { slug: 'gmail' },
592 auth_config: {
593 id: 'ac_gmail_oauth',
594 auth_scheme: 'OAUTH2',
595 is_composio_managed: true,
596 is_disabled: false,
597 },
598 created_at: '2026-01-01T00:00:00.000Z',
599 updated_at: '2026-01-01T00:00:00.000Z',
600 test_request_endpoint: '',
601 },
602 {
603 id: 'con_gmail_secondary',
604 alias: 'work',
605 word_id: 'forest',
606 status: 'ACTIVE',
607 status_reason: null,
608 is_disabled: false,
609 user_id: 'consumer-user-org_test',
610 toolkit: { slug: 'gmail' },
611 auth_config: {
612 id: 'ac_gmail_oauth',
613 auth_scheme: 'OAUTH2',
614 is_composio_managed: true,
615 is_disabled: false,
616 },
617 created_at: '2026-01-02T00:00:00.000Z',
618 updated_at: '2026-01-02T00:00:00.000Z',
619 test_request_endpoint: '',
620 },
621 ],
622 },
623 toolRouter: {
624 create: params =>
625 Effect.sync(() => {
626 recordedSessionCreateParams.push(params as unknown as Record<string, unknown>);
627 return {
628 session_id: 'trs_gmail_explicit_session',
629 config: {
630 user_id: params.user_id,
631 execute: {},
632 search: {},
633 preload: { tools: [] },
634 instant: false,
635 },
636 config_version: 1,
637 mcp: { type: 'http' as const, url: 'https://mcp.test.composio.dev' },
638 tool_router_tools: ['COMPOSIO_SEARCH_TOOLS', 'COMPOSIO_MANAGE_CONNECTIONS'],
639 };
640 }),
641 execute: (_sessionId, params) =>
642 recordExecute(params, {
643 result_type: 'completed' as const,
644 data: { tool_slug: params.tool_slug, arguments: params.arguments },
645 error: null,
646 log_id: 'log_gmail_explicit',
647 }),
648 },
649 })
650 )('[Given] --account selector [Then] execute sends the matched connected account', it => {
651 it.effect('matches even when a stale config disables the former experiment', () =>
652 Effect.gen(function* () {
653 const cliConfig = yield* ComposioCliUserConfig;
654 yield* cliConfig.update({
655 experimentalFeatures: { multi_account: false },
656 });
657
658 yield* cli([
659 'execute',
660 'GMAIL_SEND_EMAIL',
661 '--account',
662 'forest',
663 '--skip-connection-check',
664 '-d',
665 '{"recipient":"a"}',
666 ]);
667
668 expect(recordedExecuteParams[0]?.account).toBe('con_gmail_secondary');
669 })
670 );
671
672 it.effect('applies a trailing account selector to the current parallel tool only', () =>
673 Effect.gen(function* () {
674 yield* cli([
675 'execute',
676 '--parallel',
677 '--skip-checks',
678 'GMAIL_SEND_EMAIL',
679 '-d',
680 '{"recipient":"work@example.com"}',
681 '--account',
682 'forest',
683 'GMAIL_SEND_EMAIL',
684 '-d',
685 '{"recipient":"default@example.com"}',
686 ]);
687
688 expect(recordedSessionCreateParams).toHaveLength(2);
689 expect(recordedExecuteParams.map(params => params.account)).toEqual(
690 expect.arrayContaining(['con_gmail_secondary', 'con_gmail_default'])
691 );
692 })
693 );
694
695 it.effect('keeps every repeated --data and --account with its own parallel tool', () =>
696 Effect.gen(function* () {
697 yield* cli([
698 'execute',
699 '--parallel',
700 '--skip-checks',
701 'GMAIL_SEND_EMAIL',
702 '-d',
703 '{"recipient":"repeat-first@example.com"}',
704 '--account',
705 'forest',
706 'GMAIL_SEND_EMAIL',
707 '-d',
708 '{"recipient":"repeat-second@example.com"}',
709 '--account',
710 'castle',
711 ]);
712
713 // Read this invocation's own aggregate: the console is shared with the
714 // sibling tests in this layer.
715 const lines = yield* MockConsole.getLines({ stripAnsi: true });
716 const output = parseLastJson(lines) as unknown as {
717 results: Array<{ data: { arguments: Record<string, unknown> } }>;
718 };
719 expect(output.results.map(result => result.data.arguments)).toEqual([
720 { recipient: 'repeat-first@example.com' },
721 { recipient: 'repeat-second@example.com' },
722 ]);
723 expect(recordedExecuteParams.map(params => params.account)).toEqual(
724 expect.arrayContaining(['con_gmail_secondary', 'con_gmail_default'])
725 );
726 expect(recordedSessionCreateParams).toHaveLength(2);
727 })
728 );
729 });
730
731 layer(
732 TestLive({
733 baseConfigProvider: testConfigProvider,
734 fixture: 'global-test-user-id',
735 stdin: { isTTY: true, data: '' },
736 toolkitsData: {
737 toolkits: [
738 makeToolkitFixture('google', 'Google'),
739 makeToolkitFixture('google_analytics', 'Google Analytics'),
740 makeToolkitFixture('microsoft', 'Microsoft'),
741 makeToolkitFixture('microsoft_teams', 'Microsoft Teams'),
742 ],
743 },
744 connectedAccountsData: {
745 items: [
746 {
747 id: 'con_google_analytics_default',
748 alias: 'default',
749 word_id: 'meadow',
750 status: 'ACTIVE',
751 status_reason: null,
752 is_disabled: false,
753 user_id: 'consumer-user-org_test',
754 toolkit: { slug: 'google_analytics' },
755 auth_config: {
756 id: 'ac_google_analytics_oauth',
757 auth_scheme: 'OAUTH2',
758 is_composio_managed: true,
759 is_disabled: false,
760 },
761 created_at: '2026-01-01T00:00:00.000Z',
762 updated_at: '2026-01-01T00:00:00.000Z',
763 test_request_endpoint: '',
764 },
765 {
766 id: 'con_microsoft_teams_default',
767 alias: 'default',
768 word_id: 'harbor',
769 status: 'ACTIVE',
770 status_reason: null,
771 is_disabled: false,
772 user_id: 'consumer-user-org_test',
773 toolkit: { slug: 'microsoft_teams' },
774 auth_config: {
775 id: 'ac_microsoft_teams_oauth',
776 auth_scheme: 'OAUTH2',
777 is_composio_managed: true,
778 is_disabled: false,
779 },
780 created_at: '2026-01-01T00:00:00.000Z',
781 updated_at: '2026-01-01T00:00:00.000Z',
782 test_request_endpoint: '',
783 },
784 ],
785 },
786 toolRouter: {
787 create: params =>
788 Effect.sync(() => {
789 recordedSessionCreateParams.push(params as unknown as Record<string, unknown>);
790 return {
791 session_id: 'trs_google_analytics_session',
792 config: {
793 user_id: params.user_id,
794 execute: {},
795 search: {},
796 preload: { tools: [] },
797 instant: false,
798 },
799 config_version: 1,
800 mcp: { type: 'http' as const, url: 'https://mcp.test.composio.dev' },
801 tool_router_tools: ['COMPOSIO_SEARCH_TOOLS', 'COMPOSIO_MANAGE_CONNECTIONS'],
802 };
803 }),
804 execute: (_sessionId, params) =>
805 recordExecute(params, {
806 result_type: 'completed' as const,
807 data: { tool_slug: params.tool_slug, arguments: params.arguments },
808 error: null,
809 log_id: 'log_google_analytics',
810 }),
811 },
812 })
813 )('[Given] a multi-word toolkit [Then] execute resolves it from the known toolkit list', it => {
814 it.effect('pins the google_analytics connected account for GOOGLE_ANALYTICS_RUN_REPORT', () =>
815 Effect.gen(function* () {
816 yield* cli([
817 'execute',
818 'GOOGLE_ANALYTICS_RUN_REPORT',
819 '--account',
820 'meadow',
821 '--skip-connection-check',
822 '-d',
823 '{"property_id":"1"}',
824 ]);
825
826 expect(recordedExecuteParams[0]?.account).toBe('con_google_analytics_default');
827 })
828 );
829
830 it.effect('pins the microsoft_teams connected account for MICROSOFT_TEAMS_SEND_MESSAGE', () =>
831 Effect.gen(function* () {
832 yield* cli([
833 'execute',
834 'MICROSOFT_TEAMS_SEND_MESSAGE',
835 '--account',
836 'harbor',
837 '--skip-connection-check',
838 '-d',
839 '{"message":"hi"}',
840 ]);
841
842 expect(recordedExecuteParams[0]?.account).toBe('con_microsoft_teams_default');
843 })
844 );
845 });
846
847 layer(
848 TestLive({
849 baseConfigProvider: isolatedCacheConfigProvider('global-test-user-id'),
850 fixture: 'global-test-user-id',
851 stdin: { isTTY: true, data: '' },
852 toolkitsData: {
853 // Custom toolkits belong to the project: the Composio-managed catalog
854 // never lists them, and the baked one cannot.
855 toolkits: [makeToolkitFixture('gmail', 'Gmail')],
856 projectToolkits: [makeToolkitFixture('custom_grain', 'Grain')],
857 // The consumer project execute resolves, not the fixture's developer
858 // project that the project context would pick without a scope.
859 projectToolkitsScope: { orgId: 'org_test', projectId: 'consumer_project_test' },
860 onGetProjectToolkits: scope => recordedProjectToolkitScopes.push(scope),
861 },
862 connectedAccountsData: {
863 items: [
864 {
865 id: 'ca_1',
866 alias: 'grain-work',
867 word_id: 'lantern',
868 status: 'ACTIVE',
869 status_reason: null,
870 is_disabled: false,
871 user_id: 'consumer-user-org_test',
872 toolkit: { slug: 'custom_grain' },
873 auth_config: {
874 id: 'ac_custom_grain',
875 auth_scheme: 'API_KEY',
876 is_composio_managed: false,
877 is_disabled: false,
878 },
879 created_at: '2026-01-01T00:00:00.000Z',
880 updated_at: '2026-01-01T00:00:00.000Z',
881 test_request_endpoint: '',
882 },
883 ],
884 },
885 toolRouter: {
886 create: params =>
887 Effect.sync(() => {
888 recordedSessionCreateParams.push(params as unknown as Record<string, unknown>);
889 return {
890 session_id: 'trs_custom_grain_session',
891 config: {
892 user_id: params.user_id,
893 execute: {},
894 search: {},
895 preload: { tools: [] },
896 instant: false,
897 },
898 config_version: 1,
899 mcp: { type: 'http' as const, url: 'https://mcp.test.composio.dev' },
900 tool_router_tools: ['COMPOSIO_SEARCH_TOOLS', 'COMPOSIO_MANAGE_CONNECTIONS'],
901 };
902 }),
903 execute: (_sessionId, params) =>
904 recordExecute(params, {
905 result_type: 'completed' as const,
906 data: { tool_slug: params.tool_slug, arguments: params.arguments },
907 error: null,
908 log_id: 'log_custom_grain',
909 }),
910 },
911 })
912 )('[Given] a project custom toolkit [Then] execute resolves it from the project list', it => {
913 const connectedToolkitsCache = (toolkits: ReadonlyArray<string>) => {
914 vi.spyOn(
915 consumerShortTermCache,
916 'getFreshConsumerConnectedToolkitsFromCache'
917 ).mockReturnValue(Effect.succeed(Option.some([...toolkits])));
918 };
919
920 it.effect.each(['ca_1', 'grain-work', 'lantern'])(
921 'pins the custom_grain connected account selected by %s',
922 selector =>
923 Effect.gen(function* () {
924 connectedToolkitsCache(['gmail', 'custom_grain']);
925
926 yield* cli([
927 'execute',
928 'CUSTOM_GRAIN_SEARCH_PERSONS',
929 '--account',
930 selector,
931 '-d',
932 '{"query":"ada"}',
933 ]);
934
935 expect(recordedExecuteParams[0]?.account).toBe('ca_1');
936 // The invocation's telemetry resolves the tool's toolkit first, before execute knows
937 // its project. Every lookup execute makes after it — account selection, the
938 // permission gate, error mapping — must ask for the project execute resolved.
939 const [telemetryScope, ...executeScopes] = recordedProjectToolkitScopes;
940 expect(telemetryScope).toBeUndefined();
941 expect(executeScopes.length).toBeGreaterThan(0);
942 expect(executeScopes).toEqual(
943 executeScopes.map(() => ({
944 orgId: 'org_test',
945 projectId: 'consumer_project_test',
946 }))
947 );
948 })
949 );
950
951 it.effect('names the custom toolkit when the selector matches no account', () =>
952 Effect.gen(function* () {
953 const failure = yield* cli([
954 'execute',
955 'CUSTOM_GRAIN_SEARCH_PERSONS',
956 '--account',
957 'nope',
958 '--skip-connection-check',
959 '-d',
960 '{"query":"ada"}',
961 ]).pipe(Effect.flip);
962
963 expect(String((failure as { message?: unknown }).message)).toContain(
964 'No connected account matched "nope" for toolkit "custom_grain". Available accounts:'
965 );
966 expect(String((failure as { message?: unknown }).message)).toContain('ca_1');
967 expect(recordedSessionCreateParams).toHaveLength(0);
968 })
969 );
970
971 it.effect('fails the connection pre-check under the custom toolkit slug', () =>
972 Effect.gen(function* () {
973 connectedToolkitsCache(['gmail']);
974
975 yield* cli(['execute', 'CUSTOM_GRAIN_SEARCH_PERSONS', '-d', '{"query":"ada"}']).pipe(
976 Effect.flip
977 );
978 const output = (yield* MockConsole.getLines({ stripAnsi: true })).join('\n');
979
980 expect(output).toContain('Toolkit "custom_grain" is not connected for this user');
981 expect(output).toContain('composio link custom_grain');
982 expect(recordedSessionCreateParams).toHaveLength(0);
983 })
984 );
985
986 it.effect('uses the default custom_grain account without --account', () =>
987 Effect.gen(function* () {
988 connectedToolkitsCache(['gmail', 'custom_grain']);
989
990 yield* cli(['execute', 'CUSTOM_GRAIN_SEARCH_PERSONS', '-d', '{"query":"ada"}']);
991
992 expect(recordedExecuteParams[0]?.account).toBe('ca_1');
993 })
994 );
995 });
996
997 layer(
998 TestLive({
999 baseConfigProvider: testConfigProvider,
1000 fixture: 'global-test-user-id',
1001 stdin: { isTTY: true, data: '' },
1002 connectedAccountsData: {
1003 items: [
1004 {
1005 id: 'con_posthog_active',
1006 status: 'ACTIVE',
1007 status_reason: null,
1008 is_disabled: false,
1009 user_id: 'consumer-user-org_test',
1010 toolkit: { slug: 'posthog' },
1011 auth_config: {
1012 id: 'ac_posthog_custom',
1013 auth_scheme: 'BEARER_TOKEN',
1014 is_composio_managed: false,
1015 is_disabled: false,
1016 },
1017 created_at: '2026-01-01T00:00:00.000Z',
1018 updated_at: '2026-01-02T00:00:00.000Z',
1019 test_request_endpoint: '',
1020 },
1021 ],
1022 },
1023 toolRouter: {
1024 create: params =>
1025 Effect.sync(() => {
1026 recordedSessionCreateParams.push(params as unknown as Record<string, unknown>);
1027 return {
1028 session_id: 'trs_posthog_test_session',
1029 config: {
1030 user_id: params.user_id,
1031 execute: {},
1032 search: {},
1033 preload: { tools: [] },
1034 instant: false,
1035 },
1036 config_version: 1,
1037 mcp: { type: 'http' as const, url: 'https://mcp.test.composio.dev' },
1038 tool_router_tools: ['COMPOSIO_SEARCH_TOOLS', 'COMPOSIO_MANAGE_CONNECTIONS'],
1039 };
1040 }),
1041 execute: (_sessionId, params) =>
1042 recordExecute(params, {
1043 result_type: 'completed' as const,
1044 data: { tool_slug: params.tool_slug, arguments: params.arguments },
1045 error: null,
1046 log_id: 'log_posthog_test',
1047 }),
1048 },
1049 })
1050 )(
1051 '[Given] a non-managed connected account [Then] execute leaves the session to the Dashboard',
1052 it => {
1053 it.effect('sends the connected account and no auth configs for custom auth toolkits', () =>
1054 Effect.gen(function* () {
1055 yield* cli([
1056 'execute',
1057 'POSTHOG_RUN_ENDPOINT',
1058 '--skip-checks',
1059 '-d',
1060 '{"project_id":"196278","name":"test"}',
1061 ]);
1062 const lines = yield* MockConsole.getLines({ stripAnsi: true });
1063 const output = parseLastJson(lines);
1064
1065 expect(output.successful).toBe(true);
1066 expect(recordedSessionCreateParams).toHaveLength(1);
1067 expect(recordedSessionCreateParams[0]?.user_id).toEqual(expect.any(String));
1068 // The Dashboard owns the session, so the CLI sends it no auth configs.
1069 expect(recordedSessionCreateParams[0]?.auth_configs).toBeUndefined();
1070 expect(recordedExecuteParams[0]?.account).toBe('con_posthog_active');
1071 })
1072 );
1073 }
1074 );
1075
1076 layer(
1077 TestLive({
1078 baseConfigProvider: testConfigProvider,
1079 fixture: 'global-test-user-id',
1080 stdin: { isTTY: true, data: '' },
1081 toolRouter: {
1082 create: params =>
1083 Effect.sync(() => {
1084 recordedSessionCreateParams.push(params as unknown as Record<string, unknown>);
1085 return {
1086 session_id: 'trs_posthog_cached_session',
1087 config: {
1088 user_id: params.user_id,
1089 execute: {},
1090 search: {},
1091 preload: { tools: [] },
1092 instant: false,
1093 },
1094 config_version: 1,
1095 mcp: { type: 'http' as const, url: 'https://mcp.test.composio.dev' },
1096 tool_router_tools: ['COMPOSIO_SEARCH_TOOLS', 'COMPOSIO_MANAGE_CONNECTIONS'],
1097 };
1098 }),
1099 execute: (_sessionId, params) =>
1100 recordExecute(params, {
1101 result_type: 'completed' as const,
1102 data: { tool_slug: params.tool_slug, arguments: params.arguments },
1103 error: null,
1104 log_id: 'log_posthog_cached',
1105 }),
1106 },
1107 })
1108 )('[Given] cached auth configs [Then] execute sends no cached session settings', it => {
1109 it.effect('sends no cached auth configs on consumer executions', () =>
1110 Effect.gen(function* () {
1111 vi.spyOn(
1112 consumerShortTermCache,
1113 'getFreshConsumerToolRouterAuthConfigsFromCache'
1114 ).mockReturnValue(
1115 Effect.succeed(
1116 Option.some({
1117 authConfigs: {
1118 posthog: 'ac_posthog_cached',
1119 },
1120 })
1121 )
1122 );
1123
1124 yield* cli([
1125 'execute',
1126 'POSTHOG_RUN_ENDPOINT',
1127 '--skip-checks',
1128 '-d',
1129 '{"project_id":"196278","name":"test"}',
1130 ]);
1131
1132 expect(recordedSessionCreateParams).toHaveLength(1);
1133 // The Dashboard owns the session, so the CLI sends it no auth configs.
1134 expect(recordedSessionCreateParams[0]?.auth_configs).toBeUndefined();
1135 })
1136 );
1137 });
1138
1139 layer(
1140 TestLive({
1141 baseConfigProvider: testConfigProvider,
1142 fixture: 'global-test-user-id',
1143 stdin: { isTTY: true, data: '' },
1144 toolkitsData: {
1145 tools: [
1146 {
1147 name: 'Upload Slack File',
1148 slug: 'SLACK_UPLOAD_OR_CREATE_A_FILE_IN_SLACK',
1149 description: 'Uploads a file to Slack',
1150 tags: ['slack'],
1151 available_versions: ['20260316_00'],
1152 input_parameters: {
1153 type: 'object',
1154 properties: {
1155 channels: { type: 'string' },
1156 file: {
1157 type: 'object',
1158 file_uploadable: true,
1159 title: 'File',
1160 description: 'Local file accepted by CLI',
1161 properties: {
1162 name: { type: 'string' },
1163 mimetype: { type: 'string' },
1164 s3key: { type: 'string' },
1165 },
1166 required: ['name', 'mimetype', 's3key'],
1167 },
1168 },
1169 },
1170 output_parameters: {
1171 type: 'object',
1172 properties: {},
1173 },
1174 },
1175 ],
1176 } satisfies TestLiveInput['toolkitsData'],
1177 })
1178 )('[Given] file_uploadable schema [Then] execute --get-schema shows a path string input', it => {
1179 it.effect('renders the CLI-facing schema instead of the raw FileUploadable object', () =>
1180 Effect.gen(function* () {
1181 yield* cli(['execute', 'SLACK_UPLOAD_OR_CREATE_A_FILE_IN_SLACK', '--get-schema']);
1182 const lines = yield* MockConsole.getLines({ stripAnsi: true });
1183 const output = parseLastJson(lines) as unknown as {
1184 inputSchema: Record<string, unknown>;
1185 };
1186
1187 expect(output.inputSchema).toEqual({
1188 type: 'object',
1189 properties: {
1190 channels: { type: 'string' },
1191 file: {
1192 title: 'File',
1193 description: 'Local file accepted by CLI',
1194 format: 'path',
1195 type: 'string',
1196 file_uploadable: true,
1197 },
1198 },
1199 });
1200 })
1201 );
1202 });
1203
1204 layer(
1205 TestLive({
1206 baseConfigProvider: testConfigProvider,
1207 toolkitsData: {
1208 tools: [
1209 {
1210 name: 'Send Email',
1211 slug: 'GMAIL_SEND_EMAIL',
1212 description: 'Send an email',
1213 tags: ['email'],
1214 available_versions: ['20260115_00', '20260101_00'],
1215 input_parameters: {
1216 type: 'object',
1217 properties: {
1218 recipient_email: { type: 'string' },
1219 },
1220 },
1221 output_parameters: {
1222 type: 'object',
1223 properties: {
1224 message_id: { type: 'string' },
1225 },
1226 },
1227 },
1228 ],
1229 detailedToolkits: [
1230 {
1231 name: 'Gmail',
1232 slug: 'gmail',
1233 is_local_toolkit: false,
1234 composio_managed_auth_schemes: ['OAUTH2'],
1235 no_auth: false,
1236 meta: {
1237 description: 'Email service',
1238 categories: [],
1239 created_at: DateTime.makeUnsafe('2024-05-03T11:44:32.061Z'),
1240 updated_at: DateTime.makeUnsafe('2024-05-03T11:44:32.061Z'),
1241 available_versions: ['20260115_00', '20260101_00'],
1242 tools_count: 36,
1243 triggers_count: 2,
1244 },
1245 auth_config_details: [],
1246 },
1247 ] satisfies ToolkitDetailed[],
1248 } satisfies TestLiveInput['toolkitsData'],
1249 })
1250 )(
1251 '[Given] a cache miss [Then] the stored schema file includes the latest available version',
1252 it => {
1253 it.effect('writes version metadata at the top of the cache file', () =>
1254 Effect.gen(function* () {
1255 const definition = yield* getOrFetchToolInputDefinition('GMAIL_SEND_EMAIL');
1256 const raw = fs.readFileSync(definition.schemaPath, 'utf8');
1257 const parsed = JSON.parse(raw) as {
1258 version: string | null;
1259 inputSchema: Record<string, unknown>;
1260 };
1261
1262 expect(parsed.version).toBe('20260115_00');
1263 expect(parsed.inputSchema).toEqual({
1264 type: 'object',
1265 properties: {
1266 recipient_email: { type: 'string' },
1267 },
1268 });
1269 })
1270 );
1271 }
1272 );
1273
1274 layer(
1275 TestLive({
1276 baseConfigProvider: testConfigProvider,
1277 toolkitsData: {
1278 tools: [
1279 {
1280 name: 'Send Email',
1281 slug: 'GMAIL_SEND_EMAIL',
1282 description: 'Send an email',
1283 tags: ['email'],
1284 available_versions: ['00000000_00'],
1285 input_parameters: {
1286 type: 'object',
1287 properties: {
1288 recipient_email: { type: 'string' },
1289 },
1290 },
1291 output_parameters: {
1292 type: 'object',
1293 properties: {
1294 message_id: { type: 'string' },
1295 },
1296 },
1297 },
1298 ],
1299 detailedToolkits: [
1300 {
1301 name: 'Gmail',
1302 slug: 'gmail',
1303 is_local_toolkit: false,
1304 composio_managed_auth_schemes: ['OAUTH2'],
1305 no_auth: false,
1306 meta: {
1307 description: 'Email service',
1308 categories: [],
1309 created_at: DateTime.makeUnsafe('2024-05-03T11:44:32.061Z'),
1310 updated_at: DateTime.makeUnsafe('2024-05-03T11:44:32.061Z'),
1311 available_versions: ['20260316_00'],
1312 tools_count: 36,
1313 triggers_count: 2,
1314 },
1315 auth_config_details: [],
1316 },
1317 ] satisfies ToolkitDetailed[],
1318 } satisfies TestLiveInput['toolkitsData'],
1319 })
1320 )(
1321 '[Given] a placeholder tool version [Then] it stores the toolkit latest version instead',
1322 it => {
1323 it.effect('prefers toolkit latest version over 00000000_00', () =>
1324 Effect.gen(function* () {
1325 const definition = yield* getOrFetchToolInputDefinition('GMAIL_SEND_EMAIL');
1326 const raw = fs.readFileSync(definition.schemaPath, 'utf8');
1327 const parsed = JSON.parse(raw) as {
1328 version: string | null;
1329 inputSchema: Record<string, unknown>;
1330 };
1331
1332 expect(parsed.version).toBe(null);
1333 })
1334 );
1335 }
1336 );
1337
1338 layer(
1339 TestLive({
1340 baseConfigProvider: testConfigProvider,
1341 fixture: 'global-test-user-id',
1342 stdin: { isTTY: true, data: '' },
1343 toolsExecutor: {
1344 respondWith: {
1345 data: {
1346 ok: true,
1347 },
1348 error: null,
1349 successful: true,
1350 logId: 'log_stale_cache',
1351 },
1352 },
1353 toolkitsData: {
1354 tools: [
1355 {
1356 name: 'Send Email',
1357 slug: 'GMAIL_SEND_EMAIL',
1358 description: 'Send an email',
1359 tags: ['email'],
1360 available_versions: ['20260115_00', '20260101_00'],
1361 input_parameters: {
1362 type: 'object',
1363 properties: {
1364 recipient_email: { type: 'string' },
1365 subject: { type: 'string' },
1366 body: { type: 'string' },
1367 },
1368 },
1369 output_parameters: {
1370 type: 'object',
1371 properties: {
1372 message_id: { type: 'string' },
1373 },
1374 },
1375 },
1376 ],
1377 detailedToolkits: [
1378 {
1379 name: 'Gmail',
1380 slug: 'gmail',
1381 is_local_toolkit: false,
1382 composio_managed_auth_schemes: ['OAUTH2'],
1383 no_auth: false,
1384 meta: {
1385 description: 'Email service',
1386 categories: [],
1387 created_at: DateTime.makeUnsafe('2024-05-03T11:44:32.061Z'),
1388 updated_at: DateTime.makeUnsafe('2024-05-03T11:44:32.061Z'),
1389 available_versions: ['20260115_00', '20260101_00'],
1390 tools_count: 36,
1391 triggers_count: 2,
1392 },
1393 auth_config_details: [],
1394 },
1395 ] satisfies ToolkitDetailed[],
1396 } satisfies TestLiveInput['toolkitsData'],
1397 })
1398 )(
1399 '[Given] a stale cached schema [Then] it does not block execution and refreshes the cache',
1400 it => {
1401 it.effect('uses tool execution instead of stale validation failure', () =>
1402 Effect.gen(function* () {
1403 const cacheDir = yield* setupCacheDir;
1404 const schemaPath = `${cacheDir}/tool_definitions/GMAIL_SEND_EMAIL.json`;
1405 fs.mkdirSync(`${cacheDir}/tool_definitions`, { recursive: true });
1406 fs.writeFileSync(
1407 schemaPath,
1408 JSON.stringify({
1409 version: '20260101_00',
1410 inputSchema: {
1411 type: 'object',
1412 properties: {
1413 to: { type: 'string' },
1414 },
1415 },
1416 }),
1417 'utf8'
1418 );
1419 vi.spyOn(composioAccount, 'getLatestToolVersion').mockImplementation(({ toolSlug }) =>
1420 Effect.succeed({ tool_slug: toolSlug, version: '20260115_00' })
1421 );
1422
1423 yield* cli([
1424 'execute',
1425 'GMAIL_SEND_EMAIL',
1426 '-d',
1427 '{"recipient_email":"karan@composio.dev","subject":"Hi","body":"Hello"}',
1428 ]);
1429
1430 const lines = yield* MockConsole.getLines({ stripAnsi: true });
1431 const output = parseLastJson(lines);
1432 expect(output.successful).toBe(true);
1433 expect(output.logId).toBe('log_stale_cache');
1434
1435 const refreshed = JSON.parse(fs.readFileSync(schemaPath, 'utf8')) as {
1436 version: string | null;
1437 inputSchema: Record<string, unknown>;
1438 };
1439 expect(refreshed.version).toBe('20260115_00');
1440 expect(refreshed.inputSchema.type).toBe('object');
1441 expect(refreshed.inputSchema.properties).toHaveProperty('recipient_email');
1442 })
1443 );
1444 }
1445 );
1446
1447 layer(
1448 TestLive({
1449 baseConfigProvider: testConfigProvider,
1450 fixture: 'global-test-user-id',
1451 stdin: { isTTY: true, data: '' },
1452 toolkitsData: {
1453 tools: [
1454 {
1455 name: 'Send Email',
1456 slug: 'GMAIL_SEND_EMAIL',
1457 description: 'Send an email',
1458 tags: ['email'],
1459 available_versions: ['20260101_00'],
1460 input_parameters: {
1461 type: 'object',
1462 required: ['recipient'],
1463 properties: {
1464 recipient: { type: 'string', description: 'Recipient email' },
1465 },
1466 },
1467 output_parameters: {
1468 type: 'object',
1469 properties: {
1470 message_id: { type: 'string' },
1471 },
1472 },
1473 },
1474 ],
1475 } satisfies TestLiveInput['toolkitsData'],
1476 })
1477 )('[Given] invalid tool input [Then] it fails fast with the cached schema path', it => {
1478 it.effect('prints validation issues and writes the schema to tool_definitions', () =>
1479 Effect.gen(function* () {
1480 const cacheDir = yield* setupCacheDir;
1481 const schemaPath = `${cacheDir}/tool_definitions/GMAIL_SEND_EMAIL.json`;
1482 fs.mkdirSync(`${cacheDir}/tool_definitions`, { recursive: true });
1483 fs.writeFileSync(
1484 schemaPath,
1485 JSON.stringify({
1486 version: '20260101_00',
1487 inputSchema: {
1488 type: 'object',
1489 required: ['recipient_email'],
1490 properties: {
1491 recipient_email: { type: 'string', description: 'Recipient email' },
1492 subject: { type: 'string' },
1493 body: { type: 'string' },
1494 },
1495 },
1496 }),
1497 'utf8'
1498 );
1499
1500 const failure = yield* cli([
1501 'execute',
1502 'GMAIL_SEND_EMAIL',
1503 '--dry-run',
1504 '-d',
1505 '{"recipient":42}',
1506 ]).pipe(
1507 Effect.flip,
1508 Effect.map(error => (error as Error).message)
1509 );
1510
1511 expect(failure).toContain('Input validation failed for GMAIL_SEND_EMAIL');
1512 expect(failure).toContain(schemaPath);
1513 expect(failure).toContain('Unknown key "recipient"');
1514 expect(failure).toContain('Use "recipient_email" instead.');
1515 expect(failure).toContain('Allowed top-level keys: recipient_email, subject, body');
1516 expect(fs.existsSync(schemaPath)).toBe(true);
1517 })
1518 );
1519 });
1520
1521 const executedToolSlugs: Array<string> = [];
1522
1523 layer(
1524 TestLive({
1525 baseConfigProvider: testConfigProvider,
1526 fixture: 'global-test-user-id',
1527 stdin: { isTTY: true, data: '' },
1528 toolkitsData: {
1529 tools: [
1530 {
1531 name: 'Send Email',
1532 slug: 'GMAIL_SEND_EMAIL',
1533 description: 'Send an email',
1534 tags: ['email'],
1535 available_versions: ['20260101_00'],
1536 input_parameters: {
1537 type: 'object',
1538 required: ['recipient_email'],
1539 properties: {
1540 recipient_email: { type: 'string', description: 'Recipient email' },
1541 },
1542 },
1543 output_parameters: { type: 'object', properties: {} },
1544 },
1545 ],
1546 } satisfies TestLiveInput['toolkitsData'],
1547 toolsExecutor: {
1548 onExecute: slug => {
1549 executedToolSlugs.push(slug);
1550 },
1551 },
1552 })
1553 )('[Given] invalid tool input [Then] validation gates the tool call and keeps the schema', it => {
1554 // The schema lookup answers after the mock executor would have finished,
1555 // so a tool call that does not wait for validation always gets through.
1556 const slowSchemaLookup = () =>
1557 vi
1558 .spyOn(composioAccount, 'getLatestToolVersion')
1559 .mockImplementation(({ toolSlug }) =>
1560 Effect.promise(() => new Promise<void>(resolve => setTimeout(resolve, 25))).pipe(
1561 Effect.as({ tool_slug: toolSlug, version: '20260101_00' })
1562 )
1563 );
1564
1565 const executeWithInvalidInput = cli([
1566 'execute',
1567 'GMAIL_SEND_EMAIL',
1568 '--skip-connection-check',
1569 '-d',
1570 '{"recipient":42}',
1571 ]).pipe(
1572 Effect.flip,
1573 Effect.map(error => (error as Error).message)
1574 );
1575
1576 it.effect('never sends the tool call on a schema cache miss', () =>
1577 Effect.gen(function* () {
1578 slowSchemaLookup();
1579 executedToolSlugs.length = 0;
1580
1581 const failure = yield* executeWithInvalidInput;
1582
1583 expect(failure).toContain('Input validation failed for GMAIL_SEND_EMAIL');
1584 expect(executedToolSlugs).toEqual([]);
1585 const cacheDir = yield* setupCacheDir;
1586 const schemaPath = `${cacheDir}/tool_definitions/GMAIL_SEND_EMAIL.json`;
1587 expect(failure).toContain(schemaPath);
1588 expect(fs.existsSync(schemaPath)).toBe(true);
1589 })
1590 );
1591
1592 it.effect('never sends the tool call on a schema cache hit', () =>
1593 Effect.gen(function* () {
1594 yield* getOrFetchToolInputDefinition('GMAIL_SEND_EMAIL');
1595 clearInProcessMemos();
1596 slowSchemaLookup();
1597 executedToolSlugs.length = 0;
1598
1599 const failure = yield* executeWithInvalidInput;
1600
1601 expect(failure).toContain('Input validation failed for GMAIL_SEND_EMAIL');
1602 expect(executedToolSlugs).toEqual([]);
1603 const cacheDir = yield* setupCacheDir;
1604 const schemaPath = `${cacheDir}/tool_definitions/GMAIL_SEND_EMAIL.json`;
1605 expect(failure).toContain(schemaPath);
1606 expect(fs.existsSync(schemaPath)).toBe(true);
1607 })
1608 );
1609
1610 it.effect('sends the tool call when the cached schema cannot be compiled', () =>
1611 Effect.gen(function* () {
1612 const cacheDir = yield* setupCacheDir;
1613 fs.mkdirSync(`${cacheDir}/tool_definitions`, { recursive: true });
1614 fs.writeFileSync(
1615 `${cacheDir}/tool_definitions/GMAIL_SEND_EMAIL.json`,
1616 JSON.stringify({
1617 version: '20260101_00',
1618 inputSchema: {
1619 type: 'object',
1620 properties: { recipient_email: { type: 'string' } },
1621 // Not a JavaScript regular expression, so the validator cannot be built.
1622 patternProperties: { '(?i)^x-': { type: 'string' } },
1623 },
1624 }),
1625 'utf8'
1626 );
1627 clearInProcessMemos();
1628 slowSchemaLookup();
1629 executedToolSlugs.length = 0;
1630
1631 yield* cli([
1632 'execute',
1633 'GMAIL_SEND_EMAIL',
1634 '--skip-connection-check',
1635 '-d',
1636 '{"recipient_email":"karan"}',
1637 ]);
1638
1639 expect(executedToolSlugs).toEqual(['GMAIL_SEND_EMAIL']);
1640
1641 const dryRunFailure = yield* cli([
1642 'execute',
1643 'GMAIL_SEND_EMAIL',
1644 '--dry-run',
1645 '-d',
1646 '{"recipient_email":"karan"}',
1647 ]).pipe(
1648 Effect.flip,
1649 Effect.map(error => (error as Error).message)
1650 );
1651 expect(dryRunFailure).toContain('Could not compile the cached JSON schema');
1652 })
1653 );
1654 });
1655
1656 layer(
1657 TestLive({
1658 baseConfigProvider: largeOutputConfigProvider,
1659 fixture: 'global-test-user-id',
1660 stdin: { isTTY: true, data: '' },
1661 toolsExecutor: {
1662 respondWith: {
1663 data: {
1664 content: 'token '.repeat(20_000),
1665 },
1666 error: null,
1667 successful: true,
1668 logId: 'log_large_output',
1669 },
1670 },
1671 })
1672 )('[Given] a large execution response [Then] it stores the payload in a temp file', it => {
1673 it.effect('returns a file reference instead of the full inline payload', () =>
1674 Effect.gen(function* () {
1675 yield* cli(['execute', 'GMAIL_SEND_EMAIL', '-d', '{"recipient":"a"}']);
1676 const lines = yield* MockConsole.getLines({ stripAnsi: true });
1677 const output = parseLastJson(lines) as unknown as {
1678 successful: boolean;
1679 error: string | null;
1680 logId: string;
1681 storedInFile: boolean;
1682 tokenCount: number;
1683 sizeBytes: number;
1684 outputFilePath: string;
1685 };
1686
1687 expect(output.successful).toBe(true);
1688 expect(output.storedInFile).toBe(true);
1689 expect(output.logId).toBe('log_large_output');
1690 expect(output.sizeBytes).toBeGreaterThan(40_000);
1691 expect(output.tokenCount).toBe(Math.ceil(output.sizeBytes / 4));
1692 // Session artifacts fall back to COMPOSIO_CACHE_DIR, which the shared
1693 // vitest setup pins to a per-test temp directory.
1694 const cacheDir = yield* Config.String('COMPOSIO_CACHE_DIR').parse(ConfigProvider.fromEnv());
1695 expect(output.outputFilePath).toMatch(/\/[^/]+\/GMAIL_SEND_EMAIL_OUTPUT_[^.]+\.json$/);
1696 expect(output.outputFilePath.startsWith(`${cacheDir}/`)).toBe(true);
1697 expect(fs.existsSync(output.outputFilePath)).toBe(true);
1698 const storedJson = fs.readFileSync(output.outputFilePath, 'utf8');
1699 expect(storedJson).toContain('token token token');
1700 expect(Buffer.byteLength(storedJson, 'utf8')).toBe(output.sizeBytes);
1701
1702 fs.rmSync(output.outputFilePath.slice(0, output.outputFilePath.lastIndexOf('/')), {
1703 recursive: true,
1704 force: true,
1705 });
1706 })
1707 );
1708 });
1709
1710 layer(
1711 TestLive({
1712 baseConfigProvider: largeOutputConfigProvider,
1713 fixture: 'global-test-user-id',
1714 stdin: { isTTY: true, data: '' },
1715 toolsExecutor: {
1716 respondWith: {
1717 data: {
1718 // ~36KB: under the 40KB threshold once serialized.
1719 content: 'composio '.repeat(4_000),
1720 },
1721 error: null,
1722 successful: true,
1723 logId: 'log_dense_output',
1724 },
1725 },
1726 })
1727 )('[Given] a response under the byte threshold [Then] it prints inline', it => {
1728 it.effect('does not store the payload in a file', () =>
1729 Effect.gen(function* () {
1730 yield* cli(['execute', 'GMAIL_SEND_EMAIL', '-d', '{"recipient":"a"}']);
1731 const lines = yield* MockConsole.getLines({ stripAnsi: true });
1732 const output = parseLastJson(lines) as unknown as {
1733 successful: boolean;
1734 storedInFile?: boolean;
1735 data: { content: string };
1736 };
1737
1738 expect(output.successful).toBe(true);
1739 expect(output.storedInFile).toBeUndefined();
1740 expect(output.data.content).toHaveLength(36_000);
1741 })
1742 );
1743 });
1744
1745 layer(
1746 TestLive({
1747 baseConfigProvider: testConfigProvider,
1748 fixture: 'global-test-user-id',
1749 stdin: { isTTY: true, data: '' },
1750 })
1751 )('[Given] composio execute [Then] it works for the consumer flow', it => {
1752 it.effect('root execute works for consumer flow without developer-only flags', () =>
1753 Effect.gen(function* () {
1754 yield* cli(['execute', 'GMAIL_SEND_EMAIL', '-d', '{"recipient":"a"}']);
1755 const lines = yield* MockConsole.getLines({ stripAnsi: true });
1756 const output = parseLastJson(lines);
1757
1758 expect(output.successful).toBe(true);
1759 expect(output.data.tool_slug).toBe('GMAIL_SEND_EMAIL');
1760 expect(output.data.arguments).toEqual({ recipient: 'a' });
1761 })
1762 );
1763 });
1764
1765 layer(
1766 TestLive({
1767 baseConfigProvider: testConfigProvider,
1768 fixture: 'global-test-user-id',
1769 stdin: { isTTY: true, data: '' },
1770 })
1771 )('[Given] composio execute --parallel [Then] it executes repeated slug/data groups', it => {
1772 it.effect('aggregates results from multiple tool calls', () =>
1773 Effect.gen(function* () {
1774 yield* cli([
1775 '--tool-debug=false',
1776 'execute',
1777 '--parallel',
1778 '--log-level',
1779 'Info',
1780 '--skip-checks',
1781 'GMAIL_SEND_EMAIL',
1782 '-d',
1783 '{"recipient":"a"}',
1784 'GITHUB_CREATE_ISSUE',
1785 '-d',
1786 '{"title":"Bug"}',
1787 ]);
1788 const lines = yield* MockConsole.getLines({ stripAnsi: true });
1789 const output = parseLastJson(lines) as unknown as {
1790 successful: boolean;
1791 parallel: boolean;
1792 results: Array<{
1793 slug: string;
1794 successful: boolean;
1795 data?: Record<string, unknown>;
1796 }>;
1797 };
1798
1799 expect(output.successful).toBe(true);
1800 expect(output.parallel).toBe(true);
1801 expect(output.results).toHaveLength(2);
1802 expect(output.results[0]).toMatchObject({
1803 slug: 'GMAIL_SEND_EMAIL',
1804 successful: true,
1805 data: {
1806 tool_slug: 'GMAIL_SEND_EMAIL',
1807 arguments: { recipient: 'a' },
1808 },
1809 });
1810 expect(output.results[1]).toMatchObject({
1811 slug: 'GITHUB_CREATE_ISSUE',
1812 successful: true,
1813 data: {
1814 tool_slug: 'GITHUB_CREATE_ISSUE',
1815 arguments: { title: 'Bug' },
1816 },
1817 });
1818 })
1819 );
1820 });
1821
1822 layer(
1823 TestLive({
1824 baseConfigProvider: testConfigProvider,
1825 fixture: 'global-test-user-id',
1826 stdin: { isTTY: true, data: '' },
1827 toolRouter: {
1828 execute: (_sessionId, params) =>
1829 params.tool_slug === 'GMAIL_SEND_EMAIL'
1830 ? Effect.fail(
1831 clientBadRequest({
1832 message: 'gmail execution failed',
1833 code: 4310,
1834 slug: 'ToolRouterV2_ExecutionFailed',
1835 status: 400,
1836 })
1837 )
1838 : Effect.succeed({
1839 result_type: 'completed' as const,
1840 data: { tool_slug: params.tool_slug, arguments: params.arguments },
1841 error: null,
1842 log_id: 'log_parallel_success',
1843 }),
1844 },
1845 })
1846 )('[Given] one parallel execution fails [Then] sibling results are retained', it => {
1847 it.effect('returns an aggregate result for every requested tool', () =>
1848 Effect.gen(function* () {
1849 yield* cli([
1850 'execute',
1851 '--parallel',
1852 '--skip-checks',
1853 'GMAIL_SEND_EMAIL',
1854 '-d',
1855 '{"recipient":"a"}',
1856 'GITHUB_CREATE_ISSUE',
1857 '-d',
1858 '{"title":"Bug"}',
1859 ]).pipe(Effect.flip);
1860
1861 const lines = yield* MockConsole.getLines({ stripAnsi: true });
1862 const output = parseLastJson(lines) as unknown as {
1863 successful: boolean;
1864 results: ReadonlyArray<unknown>;
1865 };
1866
1867 expect(output.successful).toBe(false);
1868 expect(output.results).toHaveLength(2);
1869 expect(output.results).toEqual(
1870 expect.arrayContaining([
1871 expect.objectContaining({ slug: 'GMAIL_SEND_EMAIL', successful: false }),
1872 expect.objectContaining({ slug: 'GITHUB_CREATE_ISSUE', successful: true }),
1873 ])
1874 );
1875 })
1876 );
1877 });
1878
1879 layer(
1880 TestLive({
1881 baseConfigProvider: testConfigProvider,
1882 fixture: 'global-test-user-id',
1883 stdin: { isTTY: true, data: '' },
1884 toolRouter: {
1885 executeMeta: () =>
1886 Effect.succeed({
1887 result_type: 'failed' as const,
1888 data: { results: [], total_count: 2, success_count: 1, error_count: 1 },
1889 error: '1 out of 2 tools failed',
1890 log_id: 'log_parallel_batch',
1891 instant_charge: {
1892 amount: '0.025000',
1893 currency: 'USD' as const,
1894 charged_by: 'composio' as const,
1895 },
1896 }),
1897 },
1898 })
1899 )('[Given] a parallel multi-execute batch fails with a charge [Then] the charge is kept', it => {
1900 it.effect('reports instantCharge on the failed parallel result', () =>
1901 Effect.gen(function* () {
1902 const error = yield* cli([
1903 'execute',
1904 '--parallel',
1905 '--skip-checks',
1906 'COMPOSIO_MULTI_EXECUTE_TOOL',
1907 '-d',
1908 '{"tools":[]}',
1909 ]).pipe(Effect.flip);
1910
1911 const lines = yield* MockConsole.getLines({ stripAnsi: true });
1912 const output = parseLastJson(lines);
1913 expect(error).toMatchObject({
1914 _tag: 'ReportedToolExecutionError',
1915 reason: 'parallel_failed',
1916 });
1917 expect(output).toMatchObject({
1918 successful: false,
1919 results: [
1920 {
1921 slug: 'COMPOSIO_MULTI_EXECUTE_TOOL',
1922 successful: false,
1923 error: '1 out of 2 tools failed',
1924 logId: 'log_parallel_batch',
1925 instantCharge: { amount: '0.025000', currency: 'USD', chargedBy: 'composio' },
1926 },
1927 ],
1928 });
1929 })
1930 );
1931 });
1932
1933 layer(
1934 TestLive({
1935 baseConfigProvider: testConfigProvider,
1936 fixture: 'global-test-user-id',
1937 stdin: { isTTY: true, data: '' },
1938 toolkitsData: {
1939 tools: [
1940 {
1941 name: 'Send Email',
1942 slug: 'GMAIL_SEND_EMAIL',
1943 description: 'Send an email',
1944 tags: ['email'],
1945 available_versions: ['20260316_00'],
1946 input_parameters: {
1947 type: 'object',
1948 properties: {
1949 recipient_email: { type: 'string' },
1950 body: { type: 'string' },
1951 },
1952 },
1953 output_parameters: {
1954 type: 'object',
1955 properties: {
1956 message_id: { type: 'string' },
1957 },
1958 },
1959 },
1960 ],
1961 } satisfies TestLiveInput['toolkitsData'],
1962 })
1963 )('[Given] composio execute --dry-run [Then] it validates without executing the tool', it => {
1964 it.effect('returns a dry-run summary instead of calling the tool', () =>
1965 Effect.gen(function* () {
1966 yield* cli([
1967 'execute',
1968 'GMAIL_SEND_EMAIL',
1969 '--dry-run',
1970 '-d',
1971 '{ recipient_email: "a@b.com", body: "Hello" }',
1972 ]);
1973 const lines = yield* MockConsole.getLines({ stripAnsi: true });
1974 const output = parseLastJson(lines) as unknown as {
1975 successful: boolean;
1976 dryRun: boolean;
1977 slug: string;
1978 schemaPath: string;
1979 schemaVersion: string | null;
1980 arguments: Record<string, unknown>;
1981 };
1982
1983 expect(output.successful).toBe(true);
1984 expect(output.dryRun).toBe(true);
1985 expect(output.slug).toBe('GMAIL_SEND_EMAIL');
1986 expect(output.schemaVersion).toBeTruthy();
1987 expect(output.arguments).toEqual({ recipient_email: 'a@b.com', body: 'Hello' });
1988 expect(output.schemaPath).toContain('/tool_definitions/GMAIL_SEND_EMAIL.json');
1989 })
1990 );
1991 });
1992
1993 layer(
1994 TestLive({
1995 baseConfigProvider: testConfigProvider,
1996 fixture: 'global-test-user-id',
1997 stdin: { isTTY: true, data: '' },
1998 toolkitsData: {
1999 tools: [
2000 {
2001 name: 'Send Email',
2002 slug: 'GMAIL_SEND_EMAIL',
2003 description: 'Send an email with attachment',
2004 tags: ['email'],
2005 available_versions: ['20260316_00'],
2006 input_parameters: {
2007 type: 'object',
2008 properties: {
2009 recipient_email: { type: 'string' },
2010 attachment: {
2011 file_uploadable: true,
2012 title: 'Attachment',
2013 description: 'Local path or URL to upload',
2014 },
2015 },
2016 },
2017 output_parameters: {
2018 type: 'object',
2019 properties: {
2020 message_id: { type: 'string' },
2021 },
2022 },
2023 },
2024 ],
2025 } satisfies TestLiveInput['toolkitsData'],
2026 })
2027 )(
2028 '[Given] file_uploadable input as a local path [Then] execute uploads and sends s3key data',
2029 it => {
2030 it.effect('uploads local file paths before Tool Router execution', () =>
2031 Effect.gen(function* () {
2032 const tempFile = path.join(os.tmpdir(), `composio-upload-${crypto.randomUUID()}.txt`);
2033 fs.writeFileSync(tempFile, 'hello from cli upload', 'utf8');
2034
2035 // The S3 upload is the only request that reaches `fetch`.
2036 const fetchSpy = vi
2037 .spyOn(globalThis, 'fetch')
2038 .mockImplementation(async () => new Response(null, { status: 200 }));
2039
2040 yield* cli([
2041 'execute',
2042 'GMAIL_SEND_EMAIL',
2043 '-d',
2044 JSON.stringify({
2045 recipient_email: 'a@b.com',
2046 attachment: tempFile,
2047 }),
2048 ]);
2049
2050 const lines = yield* MockConsole.getLines({ stripAnsi: true });
2051 const output = parseLastJson(lines);
2052
2053 expect(output.successful).toBe(true);
2054 expect(output.data.arguments).toEqual({
2055 recipient_email: 'a@b.com',
2056 attachment: {
2057 name: path.basename(tempFile),
2058 mimetype: 'text/plain',
2059 s3key: `uploads/${path.basename(tempFile)}`,
2060 },
2061 });
2062 expect(fetchSpy).toHaveBeenCalledWith(
2063 'https://s3.test.composio.dev/upload',
2064 expect.objectContaining({
2065 method: 'PUT',
2066 headers: expect.objectContaining({
2067 'Content-Type': 'text/plain',
2068 }),
2069 })
2070 );
2071
2072 fs.rmSync(tempFile, { force: true });
2073 })
2074 );
2075 }
2076 );
2077
2078 layer(
2079 TestLive({
2080 baseConfigProvider: testConfigProvider,
2081 fixture: 'global-test-user-id',
2082 stdin: { isTTY: true, data: '' },
2083 toolkitsData: {
2084 tools: [
2085 {
2086 name: 'Upload Slack File',
2087 slug: 'SLACK_UPLOAD_OR_CREATE_A_FILE_IN_SLACK',
2088 description: 'Uploads a file to Slack',
2089 tags: ['slack'],
2090 available_versions: ['20260316_00'],
2091 input_parameters: {
2092 type: 'object',
2093 properties: {
2094 channels: { type: 'string' },
2095 file: {
2096 type: 'object',
2097 file_uploadable: true,
2098 title: 'File',
2099 description: 'Local file accepted by CLI',
2100 properties: {
2101 name: { type: 'string' },
2102 mimetype: { type: 'string' },
2103 s3key: { type: 'string' },
2104 },
2105 required: ['name', 'mimetype', 's3key'],
2106 },
2107 },
2108 },
2109 output_parameters: {
2110 type: 'object',
2111 properties: {},
2112 },
2113 },
2114 ],
2115 } satisfies TestLiveInput['toolkitsData'],
2116 })
2117 )(
2118 '[Given] --file for a tool with one file_uploadable input [Then] execute injects it automatically',
2119 it => {
2120 it.effect('injects into the single file_uploadable field before upload hydration', () =>
2121 Effect.gen(function* () {
2122 const tempFile = path.join(os.tmpdir(), `composio-inject-${crypto.randomUUID()}.png`);
2123 fs.writeFileSync(tempFile, 'png-binary-ish', 'utf8');
2124
2125 // The S3 upload is the only request that reaches `fetch`.
2126 const fetchSpy = vi
2127 .spyOn(globalThis, 'fetch')
2128 .mockImplementation(async () => new Response(null, { status: 200 }));
2129
2130 yield* cli([
2131 'execute',
2132 'SLACK_UPLOAD_OR_CREATE_A_FILE_IN_SLACK',
2133 '--skip-connection-check',
2134 '--file',
2135 tempFile,
2136 '-d',
2137 JSON.stringify({ channels: 'C123' }),
2138 ]);
2139
2140 const lines = yield* MockConsole.getLines({ stripAnsi: true });
2141 const output = parseLastJson(lines);
2142
2143 expect(output.successful).toBe(true);
2144 expect(output.data.arguments).toEqual({
2145 channels: 'C123',
2146 file: {
2147 name: path.basename(tempFile),
2148 mimetype: 'image/png',
2149 s3key: `uploads/${path.basename(tempFile)}`,
2150 },
2151 });
2152 expect(fetchSpy).toHaveBeenCalledExactlyOnceWith(
2153 'https://s3.test.composio.dev/upload',
2154 expect.objectContaining({ method: 'PUT' })
2155 );
2156
2157 fs.rmSync(tempFile, { force: true });
2158 })
2159 );
2160 }
2161 );
2162
2163 layer(
2164 TestLive({
2165 baseConfigProvider: testConfigProvider,
2166 fixture: 'global-test-user-id',
2167 stdin: { isTTY: true, data: '' },
2168 toolkitsData: {
2169 tools: [
2170 {
2171 name: 'Nested Upload',
2172 slug: 'NESTED_UPLOAD_TOOL',
2173 description: 'Uploads a nested file',
2174 tags: ['test'],
2175 available_versions: ['20260316_00'],
2176 input_parameters: {
2177 type: 'object',
2178 properties: {
2179 payload: {
2180 properties: {
2181 file: {
2182 file_uploadable: true,
2183 title: 'File',
2184 description: 'Nested file path',
2185 properties: {
2186 name: { type: 'string' },
2187 mimetype: { type: 'string' },
2188 s3key: { type: 'string' },
2189 },
2190 },
2191 },
2192 },
2193 },
2194 },
2195 output_parameters: {
2196 type: 'object',
2197 properties: {},
2198 },
2199 },
2200 ],
2201 } satisfies TestLiveInput['toolkitsData'],
2202 })
2203 )(
2204 '[Given] a nested file_uploadable path under properties without explicit object type [Then] hydration still uploads it',
2205 it => {
2206 it.effect('treats property-bearing schema nodes as object-like during upload hydration', () =>
2207 Effect.gen(function* () {
2208 const tempFile = path.join(os.tmpdir(), `composio-nested-${crypto.randomUUID()}.png`);
2209 fs.writeFileSync(tempFile, 'nested-png-binary-ish', 'utf8');
2210
2211 // The S3 upload is the only request that reaches `fetch`.
2212 const fetchSpy = vi
2213 .spyOn(globalThis, 'fetch')
2214 .mockImplementation(async () => new Response(null, { status: 200 }));
2215
2216 yield* cli([
2217 'execute',
2218 'NESTED_UPLOAD_TOOL',
2219 '--skip-connection-check',
2220 '--file',
2221 tempFile,
2222 '-d',
2223 JSON.stringify({ payload: {} }),
2224 ]);
2225
2226 const lines = yield* MockConsole.getLines({ stripAnsi: true });
2227 const output = parseLastJson(lines);
2228
2229 expect(output.successful).toBe(true);
2230 expect(output.data.arguments).toEqual({
2231 payload: {
2232 file: {
2233 name: path.basename(tempFile),
2234 mimetype: 'image/png',
2235 s3key: `uploads/${path.basename(tempFile)}`,
2236 },
2237 },
2238 });
2239 expect(fetchSpy).toHaveBeenCalledExactlyOnceWith(
2240 'https://s3.test.composio.dev/upload',
2241 expect.objectContaining({ method: 'PUT' })
2242 );
2243
2244 fs.rmSync(tempFile, { force: true });
2245 })
2246 );
2247 }
2248 );
2249
2250 layer(
2251 TestLive({
2252 baseConfigProvider: testConfigProvider,
2253 fixture: 'global-test-user-id',
2254 stdin: { isTTY: true, data: '' },
2255 toolkitsData: {
2256 tools: [
2257 {
2258 name: 'Send Email',
2259 slug: 'GMAIL_SEND_EMAIL',
2260 description: 'Send an email with attachment',
2261 tags: ['email'],
2262 available_versions: ['20260316_00'],
2263 input_parameters: {
2264 type: 'object',
2265 properties: {
2266 recipient_email: { type: 'string' },
2267 attachment: {
2268 file_uploadable: true,
2269 title: 'Attachment',
2270 description: 'Local path or URL to upload',
2271 properties: {
2272 name: { type: 'string' },
2273 mimetype: { type: 'string' },
2274 s3key: { type: 'string' },
2275 },
2276 },
2277 },
2278 },
2279 output_parameters: {
2280 type: 'object',
2281 properties: {},
2282 },
2283 },
2284 ],
2285 } satisfies TestLiveInput['toolkitsData'],
2286 })
2287 )(
2288 '[Given] an S3 upload failure [Then] execute surfaces the upload error instead of sending a raw path',
2289 it => {
2290 it.effect('propagates upload failures from file hydration', () =>
2291 Effect.gen(function* () {
2292 const tempFile = path.join(
2293 os.tmpdir(),
2294 `composio-upload-fail-${crypto.randomUUID()}.txt`
2295 );
2296 fs.writeFileSync(tempFile, 'hello from failed cli upload', 'utf8');
2297
2298 // The S3 upload is the only request that reaches `fetch`.
2299 const fetchSpy = vi
2300 .spyOn(globalThis, 'fetch')
2301 .mockImplementation(
2302 async () =>
2303 new Response('Upload failed', { status: 500, statusText: 'Upload Failed' })
2304 );
2305
2306 const failure = yield* cli([
2307 'execute',
2308 'GMAIL_SEND_EMAIL',
2309 '--skip-connection-check',
2310 '-d',
2311 JSON.stringify({
2312 recipient_email: 'a@b.com',
2313 attachment: tempFile,
2314 }),
2315 ]).pipe(
2316 Effect.flip,
2317 Effect.map(error => (error as Error).message)
2318 );
2319
2320 expect(failure).toContain('Failed to upload file to S3');
2321 expect(fetchSpy).toHaveBeenCalledExactlyOnceWith(
2322 'https://s3.test.composio.dev/upload',
2323 expect.objectContaining({ method: 'PUT' })
2324 );
2325
2326 fs.rmSync(tempFile, { force: true });
2327 })
2328 );
2329 }
2330 );
2331
2332 layer(
2333 TestLive({
2334 baseConfigProvider: testConfigProvider,
2335 fixture: 'global-test-user-id',
2336 stdin: { isTTY: true, data: '' },
2337 toolkitsData: {
2338 tools: [
2339 {
2340 name: 'Add attachment to card',
2341 slug: 'TRELLO_ADD_CARDS_ATTACHMENTS_BY_ID_CARD',
2342 description: 'Adds an attachment to a Trello card',
2343 tags: ['trello'],
2344 available_versions: ['20261007_01'],
2345 input_parameters: {
2346 type: 'object',
2347 required: ['idCard'],
2348 properties: {
2349 idCard: { type: 'string' },
2350 // As the API serves it: a local path that is not file_uploadable.
2351 file: {
2352 type: 'string',
2353 title: 'File',
2354 description: 'Path to the local file to attach if not linking a `url`.',
2355 },
2356 },
2357 },
2358 output_parameters: { type: 'object', properties: {} },
2359 },
2360 ],
2361 } satisfies TestLiveInput['toolkitsData'],
2362 })
2363 )(
2364 '[Given] a local file for an input that is not file_uploadable [Then] execute fails instead of sending the path (issue #4965)',
2365 it => {
2366 it.effect('refuses before the tool runs', () =>
2367 Effect.gen(function* () {
2368 const tempFile = path.join(os.tmpdir(), `composio-trello-${crypto.randomUUID()}.jpg`);
2369 fs.writeFileSync(tempFile, 'jpeg bytes', 'utf8');
2370 const fetchSpy = vi.spyOn(globalThis, 'fetch');
2371
2372 const failure = yield* cli([
2373 'execute',
2374 'TRELLO_ADD_CARDS_ATTACHMENTS_BY_ID_CARD',
2375 '--skip-connection-check',
2376 '-d',
2377 JSON.stringify({ idCard: 'card', file: tempFile }),
2378 ]).pipe(
2379 Effect.flip,
2380 Effect.map(error => (error as Error).message),
2381 Effect.ensuring(Effect.sync(() => fs.rmSync(tempFile, { force: true })))
2382 );
2383
2384 expect(failure).toContain('cannot upload local files');
2385 expect(failure).toContain(`"file" (${tempFile})`);
2386 expect(fetchSpy).not.toHaveBeenCalled();
2387 const lines = yield* MockConsole.getLines({ stripAnsi: true });
2388 expect(lines.join('\n')).not.toContain('"successful": true');
2389 })
2390 );
2391
2392 it.effect('reports the same problem under --dry-run', () =>
2393 Effect.gen(function* () {
2394 const tempFile = path.join(os.tmpdir(), `composio-trello-${crypto.randomUUID()}.jpg`);
2395 fs.writeFileSync(tempFile, 'jpeg bytes', 'utf8');
2396
2397 const failure = yield* cli([
2398 'execute',
2399 'TRELLO_ADD_CARDS_ATTACHMENTS_BY_ID_CARD',
2400 '--skip-connection-check',
2401 '--dry-run',
2402 '-d',
2403 JSON.stringify({ idCard: 'card', file: tempFile }),
2404 ]).pipe(
2405 Effect.flip,
2406 Effect.map(error => (error as Error).message),
2407 Effect.ensuring(Effect.sync(() => fs.rmSync(tempFile, { force: true })))
2408 );
2409
2410 expect(failure).toContain('cannot upload local files');
2411 const lines = yield* MockConsole.getLines({ stripAnsi: true });
2412 expect(lines.join('\n')).not.toContain('Dry run successful');
2413 })
2414 );
2415
2416 // The real call refuses the file whatever the skip flags say, so the
2417 // dry run must too.
2418 it.effect('reports the same problem under --dry-run --skip-checks', () =>
2419 Effect.gen(function* () {
2420 const tempFile = path.join(os.tmpdir(), `composio-trello-${crypto.randomUUID()}.jpg`);
2421 fs.writeFileSync(tempFile, 'jpeg bytes', 'utf8');
2422
2423 const failure = yield* cli([
2424 'execute',
2425 'TRELLO_ADD_CARDS_ATTACHMENTS_BY_ID_CARD',
2426 '--skip-checks',
2427 '--dry-run',
2428 '-d',
2429 JSON.stringify({ idCard: 'card', file: tempFile }),
2430 ]).pipe(
2431 Effect.flip,
2432 Effect.map(error => (error as Error).message),
2433 Effect.ensuring(Effect.sync(() => fs.rmSync(tempFile, { force: true })))
2434 );
2435
2436 expect(failure).toContain('cannot upload local files');
2437 const lines = yield* MockConsole.getLines({ stripAnsi: true });
2438 expect(lines.join('\n')).not.toContain('Dry run successful');
2439 })
2440 );
2441
2442 it.effect('reports the same problem under --parallel --dry-run', () =>
2443 Effect.gen(function* () {
2444 const tempFile = path.join(os.tmpdir(), `composio-trello-${crypto.randomUUID()}.jpg`);
2445 fs.writeFileSync(tempFile, 'jpeg bytes', 'utf8');
2446
2447 yield* cli([
2448 'execute',
2449 '--parallel',
2450 '--skip-connection-check',
2451 '--dry-run',
2452 'TRELLO_ADD_CARDS_ATTACHMENTS_BY_ID_CARD',
2453 '-d',
2454 JSON.stringify({ idCard: 'card', file: tempFile }),
2455 ]).pipe(
2456 Effect.flip,
2457 Effect.ensuring(Effect.sync(() => fs.rmSync(tempFile, { force: true })))
2458 );
2459
2460 const output = parseLastJson(yield* MockConsole.getLines({ stripAnsi: true }));
2461 expect(output).toMatchObject({
2462 successful: false,
2463 results: [
2464 {
2465 slug: 'TRELLO_ADD_CARDS_ATTACHMENTS_BY_ID_CARD',
2466 successful: false,
2467 error: expect.stringContaining('cannot upload local files'),
2468 },
2469 ],
2470 });
2471 })
2472 );
2473 }
2474 );
2475
2476 layer(
2477 TestLive({
2478 baseConfigProvider: testConfigProvider,
2479 fixture: 'global-test-user-id',
2480 stdin: { isTTY: true, data: '' },
2481 toolkitsData: {
2482 tools: [
2483 {
2484 name: 'Send Email',
2485 slug: 'GMAIL_SEND_EMAIL',
2486 description: 'Send an email',
2487 tags: ['email'],
2488 available_versions: ['20260316_00'],
2489 input_parameters: {
2490 type: 'object',
2491 properties: {
2492 recipient_email: { type: 'string' },
2493 },
2494 },
2495 output_parameters: {
2496 type: 'object',
2497 properties: {},
2498 },
2499 },
2500 ],
2501 } satisfies TestLiveInput['toolkitsData'],
2502 })
2503 )('[Given] --file for a tool with no file_uploadable input [Then] execute fails clearly', it => {
2504 it.effect('rejects the convenience flag when the schema has no file input', () =>
2505 Effect.gen(function* () {
2506 const failure = yield* cli([
2507 'execute',
2508 'GMAIL_SEND_EMAIL',
2509 '--file',
2510 '/tmp/example.txt',
2511 ]).pipe(
2512 Effect.flip,
2513 Effect.map(error => (error as Error).message)
2514 );
2515
2516 expect(failure).toContain('has no file_uploadable input');
2517 })
2518 );
2519 });
2520
2521 layer(
2522 TestLive({
2523 baseConfigProvider: testConfigProvider,
2524 fixture: 'global-test-user-id',
2525 stdin: { isTTY: true, data: '' },
2526 toolkitsData: {
2527 tools: [
2528 {
2529 name: 'Multi Upload',
2530 slug: 'MULTI_UPLOAD_TOOL',
2531 description: 'Tool with two uploadable fields',
2532 tags: ['test'],
2533 available_versions: ['20260316_00'],
2534 input_parameters: {
2535 type: 'object',
2536 properties: {
2537 avatar: {
2538 type: 'object',
2539 file_uploadable: true,
2540 properties: {
2541 name: { type: 'string' },
2542 mimetype: { type: 'string' },
2543 s3key: { type: 'string' },
2544 },
2545 },
2546 resume: {
2547 type: 'object',
2548 file_uploadable: true,
2549 properties: {
2550 name: { type: 'string' },
2551 mimetype: { type: 'string' },
2552 s3key: { type: 'string' },
2553 },
2554 },
2555 },
2556 },
2557 output_parameters: {
2558 type: 'object',
2559 properties: {},
2560 },
2561 },
2562 ],
2563 } satisfies TestLiveInput['toolkitsData'],
2564 })
2565 )(
2566 '[Given] --file for a tool with multiple file_uploadable inputs [Then] execute asks for explicit JSON',
2567 it => {
2568 it.effect('fails instead of guessing which file field to use', () =>
2569 Effect.gen(function* () {
2570 const failure = yield* cli([
2571 'execute',
2572 'MULTI_UPLOAD_TOOL',
2573 '--file',
2574 '/tmp/example.txt',
2575 ]).pipe(
2576 Effect.flip,
2577 Effect.map(error => (error as Error).message)
2578 );
2579
2580 expect(failure).toContain('has multiple file_uploadable inputs');
2581 expect(failure).toContain('avatar');
2582 expect(failure).toContain('resume');
2583 })
2584 );
2585 }
2586 );
2587
2588 layer(
2589 TestLive({
2590 baseConfigProvider: testConfigProvider,
2591 fixture: 'global-test-user-id',
2592 toolkitsData: {
2593 tools: [
2594 {
2595 name: 'Send Email',
2596 slug: 'GMAIL_SEND_EMAIL',
2597 description: 'Send an email',
2598 tags: ['email'],
2599 available_versions: ['20260316_00'],
2600 input_parameters: {
2601 type: 'object',
2602 properties: {
2603 recipient_email: { type: 'string' },
2604 body: { type: 'string' },
2605 },
2606 },
2607 output_parameters: {
2608 type: 'object',
2609 properties: {
2610 message_id: { type: 'string' },
2611 },
2612 },
2613 },
2614 ],
2615 detailedToolkits: [
2616 {
2617 name: 'Gmail',
2618 slug: 'gmail',
2619 is_local_toolkit: false,
2620 composio_managed_auth_schemes: ['OAUTH2'],
2621 no_auth: false,
2622 meta: {
2623 description: 'Email service',
2624 categories: [],
2625 created_at: DateTime.makeUnsafe('2024-05-03T11:44:32.061Z'),
2626 updated_at: DateTime.makeUnsafe('2024-05-03T11:44:32.061Z'),
2627 available_versions: ['20260316_00'],
2628 tools_count: 36,
2629 triggers_count: 2,
2630 },
2631 auth_config_details: [],
2632 },
2633 ] satisfies ToolkitDetailed[],
2634 } satisfies TestLiveInput['toolkitsData'],
2635 })
2636 )('[Given] composio execute --get-schema [Then] it caches and prints the input schema', it => {
2637 it.effect('fetches schema without executing the tool', () =>
2638 Effect.gen(function* () {
2639 const cacheDir = yield* setupCacheDir;
2640 const schemaPath = `${cacheDir}/tool_definitions/GMAIL_SEND_EMAIL.json`;
2641
2642 yield* cli(['execute', 'GMAIL_SEND_EMAIL', '--get-schema']);
2643 const lines = yield* MockConsole.getLines({ stripAnsi: true });
2644 const output = parseLastJson(lines) as unknown as {
2645 slug: string;
2646 version: string | null;
2647 schemaPath: string;
2648 inputSchema: Record<string, unknown>;
2649 };
2650
2651 expect(output.slug).toBe('GMAIL_SEND_EMAIL');
2652 expect(output.version).toBe('20260316_00');
2653 expect(output.schemaPath).toBe(schemaPath);
2654 expect(output.inputSchema).toEqual({
2655 type: 'object',
2656 properties: {
2657 recipient_email: { type: 'string' },
2658 body: { type: 'string' },
2659 },
2660 });
2661 expect(fs.existsSync(schemaPath)).toBe(true);
2662 })
2663 );
2664 });
2665
2666 layer(
2667 TestLive({
2668 baseConfigProvider: testConfigProvider,
2669 fixture: 'user-config-with-global-context',
2670 stdin: { isTTY: true, data: '' },
2671 toolkitsData: {
2672 tools: [
2673 {
2674 name: 'Create Issue',
2675 slug: 'GITHUB_CREATE_AN_ISSUE',
2676 description: 'Create a GitHub issue',
2677 tags: ['github'],
2678 available_versions: ['20260316_00'],
2679 input_parameters: {
2680 type: 'object',
2681 properties: {
2682 owner: { type: 'string' },
2683 repo: { type: 'string' },
2684 title: { type: 'string' },
2685 body: { type: 'string' },
2686 },
2687 },
2688 output_parameters: {
2689 type: 'object',
2690 properties: {
2691 html_url: { type: 'string' },
2692 },
2693 },
2694 },
2695 ],
2696 },
2697 })
2698 )(
2699 '[Given] composio execute --get-schema without a configured test user id [Then] it still works',
2700 it => {
2701 it.effect('does not require execution user context for schema fetches', () =>
2702 Effect.gen(function* () {
2703 const cacheDir = yield* setupCacheDir;
2704 const schemaPath = `${cacheDir}/tool_definitions/GITHUB_CREATE_AN_ISSUE.json`;
2705
2706 yield* cli(['execute', 'GITHUB_CREATE_AN_ISSUE', '--get-schema']);
2707 const lines = yield* MockConsole.getLines({ stripAnsi: true });
2708 const output = parseLastJson(lines) as unknown as {
2709 slug: string;
2710 version: string | null;
2711 schemaPath: string;
2712 inputSchema: Record<string, unknown>;
2713 };
2714
2715 expect(output.slug).toBe('GITHUB_CREATE_AN_ISSUE');
2716 expect(output.version).toBe('20260316_00');
2717 expect(output.schemaPath).toBe(schemaPath);
2718 expect(output.inputSchema).toEqual({
2719 type: 'object',
2720 properties: {
2721 owner: { type: 'string' },
2722 repo: { type: 'string' },
2723 title: { type: 'string' },
2724 body: { type: 'string' },
2725 },
2726 });
2727 expect(fs.existsSync(schemaPath)).toBe(true);
2728 })
2729 );
2730 }
2731 );
2732
2733 layer(
2734 TestLive({
2735 baseConfigProvider: testConfigProvider,
2736 fixture: 'global-test-user-id',
2737 stdin: { isTTY: true, data: '' },
2738 })
2739 )(
2740 '[Given] composio execute with a JS-style object literal [Then] it parses and executes successfully',
2741 it => {
2742 it.effect('accepts object literal syntax for -d input', () =>
2743 Effect.gen(function* () {
2744 yield* cli(['execute', 'GMAIL_SEND_EMAIL', '-d', '{ recipient: "a", subject: "Hello" }']);
2745 const lines = yield* MockConsole.getLines({ stripAnsi: true });
2746 const output = parseLastJson(lines);
2747
2748 expect(output.successful).toBe(true);
2749 expect(output.data.tool_slug).toBe('GMAIL_SEND_EMAIL');
2750 expect(output.data.arguments).toEqual({ recipient: 'a', subject: 'Hello' });
2751 })
2752 );
2753 }
2754 );
2755
2756 layer(
2757 TestLive({
2758 baseConfigProvider: testConfigProvider,
2759 fixture: 'global-test-user-id',
2760 stdin: { isTTY: true, data: '' },
2761 })
2762 )('[Given] composio execute --perf-debug [Then] it is accepted on the root command', it => {
2763 it.effect('parses and executes with perf debug enabled', () =>
2764 Effect.gen(function* () {
2765 yield* cli(['execute', '--perf-debug', 'GMAIL_SEND_EMAIL', '-d', '{"recipient":"a"}']);
2766 const lines = yield* MockConsole.getLines({ stripAnsi: true });
2767 const output = parseLastJson(lines);
2768
2769 expect(output.successful).toBe(true);
2770 expect(output.data.tool_slug).toBe('GMAIL_SEND_EMAIL');
2771 }).pipe(
2772 Effect.ensuring(
2773 Effect.sync(() => {
2774 delete process.env.COMPOSIO_PERF_DEBUG;
2775 })
2776 )
2777 )
2778 );
2779 });
2780
2781 layer(
2782 TestLive({
2783 baseConfigProvider: testConfigProvider,
2784 fixture: 'global-test-user-id',
2785 stdin: { isTTY: true, data: '' },
2786 })
2787 )(
2788 '[Given] no --user-id and no project test_user_id [Then] falls back to global test_user_id',
2789 it => {
2790 it.effect('executes without printing global test user diagnostics', () =>
2791 Effect.gen(function* () {
2792 yield* cli(['execute', 'GMAIL_SEND_EMAIL', '-d', '{"recipient":"a"}']);
2793 const lines = yield* MockConsole.getLines({ stripAnsi: true });
2794 const output = parseLastJson(lines);
2795 const text = lines.join('\n');
2796
2797 expect(output.successful).toBe(true);
2798 expect(output.data.tool_slug).toBe('GMAIL_SEND_EMAIL');
2799 expect(text).not.toContain('Using global test user id');
2800 })
2801 );
2802 }
2803 );
2804
2805 layer(
2806 TestLive({
2807 baseConfigProvider: testConfigProvider,
2808 toolkitsData: {
2809 tools: [
2810 {
2811 name: 'Send Email',
2812 slug: 'GMAIL_SEND_EMAIL',
2813 description: 'Send an email',
2814 tags: ['email'],
2815 available_versions: ['20260101_00'],
2816 input_parameters: {
2817 type: 'object',
2818 required: ['recipient'],
2819 properties: {
2820 recipient: { type: 'string', description: 'Recipient email' },
2821 subject: { type: 'string', description: 'Subject line' },
2822 },
2823 },
2824 output_parameters: {
2825 type: 'object',
2826 properties: {
2827 message_id: { type: 'string' },
2828 },
2829 },
2830 },
2831 ],
2832 } satisfies TestLiveInput['toolkitsData'],
2833 stdin: { isTTY: true, data: '' },
2834 })
2835 )('[Given] execute --help with a slug [Then] it shows command help', it => {
2836 it.effect('shows the root execute help text', () =>
2837 Effect.gen(function* () {
2838 yield* cli(['execute', 'GMAIL_SEND_EMAIL', '--help']);
2839 const lines = yield* MockConsole.getLines({ stripAnsi: true });
2840 const output = lines.join('\n');
2841
2842 expect(output).toContain('USAGE');
2843 expect(output).toContain('composio execute');
2844 expect(output).toContain('--get-schema');
2845 expect(output).not.toContain('Fetching input parameters');
2846 expect(output).toContain('composio execute GMAIL_SEND_EMAIL --get-schema');
2847 expect(output).toContain('--parallel');
2848 expect(output).toContain('--account');
2849 expect(output).toContain('GITHUB_CREATE_AN_ISSUE');
2850 })
2851 );
2852 });
2853
2854 layer(
2855 TestLive({
2856 baseConfigProvider: testConfigProvider,
2857 fixture: 'global-test-user-id',
2858 stdin: { isTTY: false, data: '{"owner":"composio"}' },
2859 })
2860 )('[Given] stdin is piped [Then] reads input from stdin', it => {
2861 it.effect('reads stdin input', () =>
2862 Effect.gen(function* () {
2863 yield* cli(['execute', 'GITHUB_GET_REPOS']);
2864 const lines = yield* MockConsole.getLines({ stripAnsi: true });
2865 const output = parseLastJson(lines);
2866
2867 expect(output.successful).toBe(true);
2868 expect(output.data.tool_slug).toBe('GITHUB_GET_REPOS');
2869 expect(output.data.arguments).toEqual({ owner: 'composio' });
2870 })
2871 );
2872 });
2873
2874 describe('[Given] no -d and a stdin pipe that sends nothing within 1 second', () => {
2875 const silentPipe = new PassThrough();
2876 const slowPipe = new PassThrough();
2877 const executedToolSlugs: string[] = [];
2878 const recordExecutions = { onExecute: (slug: string) => void executedToolSlugs.push(slug) };
2879
2880 layer(
2881 TestLive({
2882 baseConfigProvider: testConfigProvider,
2883 fixture: 'global-test-user-id',
2884 stdin: { isTTY: false, data: '', stream: silentPipe },
2885 toolsExecutor: recordExecutions,
2886 })
2887 )('[When] the pipe stays open and silent', it => {
2888 it.effect('[Then] fails without running the tool', () =>
2889 Effect.gen(function* () {
2890 const failure = yield* cli(['execute', 'GITHUB_GET_REPOS']).pipe(Effect.flip);
2891
2892 expectInvalidValueMessage(failure, 'No input arrived on stdin within 1 second.');
2893 expect(executedToolSlugs).toEqual([]);
2894 })
2895 );
2896 });
2897
2898 layer(
2899 TestLive({
2900 baseConfigProvider: testConfigProvider,
2901 fixture: 'global-test-user-id',
2902 stdin: { isTTY: false, data: '', stream: slowPipe },
2903 toolsExecutor: recordExecutions,
2904 })
2905 )('[When] the JSON arrives after the timeout', it => {
2906 it.effect('[Then] fails instead of running the tool with empty arguments', () =>
2907 Effect.gen(function* () {
2908 // Time the write from when the reader starts listening, not from test start, so slow
2909 // setup cannot move it inside the 1 second window.
2910 let lateWrite: ReturnType<typeof setTimeout> | undefined;
2911 slowPipe.once('resume', () => {
2912 lateWrite = setTimeout(() => slowPipe.end('{"owner":"x"}'), 1500);
2913 });
2914 const failure = yield* cli(['execute', 'GITHUB_GET_REPOS']).pipe(
2915 Effect.flip,
2916 Effect.ensuring(Effect.sync(() => clearTimeout(lateWrite)))
2917 );
2918
2919 expectInvalidValueMessage(failure, 'No input arrived on stdin within 1 second.');
2920 expect(executedToolSlugs).toEqual([]);
2921 })
2922 );
2923 });
2924 });
2925
2926 layer(
2927 TestLive({
2928 baseConfigProvider: testConfigProvider,
2929 fixture: 'global-test-user-id',
2930 stdin: { isTTY: true, data: '' },
2931 toolsExecutor: {
2932 failWith: new ComposioNoActiveConnectionError({
2933 details: {
2934 slug: 'ActionExecute_ConnectedAccountNotFound',
2935 message: 'No connected account found for entity ID default for toolkit gmail',
2936 },
2937 toolSlug: 'GMAIL_CREATE_EMAIL_DRAFT',
2938 }),
2939 },
2940 })
2941 )('[Given] connected account not found slug (legacy) [Then] prints tips', it => {
2942 it.effect('prints connected account tips for legacy slug', () =>
2943 Effect.gen(function* () {
2944 yield* cli([
2945 'execute',
2946 'GMAIL_CREATE_EMAIL_DRAFT',
2947 '-d',
2948 '{\"recipient\":\"to@example.com\"}',
2949 ]).pipe(Effect.flip);
2950 const lines = yield* MockConsole.getLines({ stripAnsi: true });
2951 const output = lines.join('\n');
2952
2953 expect(output).toContain('Run `composio link gmail`, then retry.');
2954 expect(output).toContain('Tips');
2955 expect(output).toContain('composio link gmail');
2956 expect(output).not.toContain('COMPOSIO_MANAGE_CONNECTIONS');
2957 })
2958 );
2959 });
2960
2961 // --- Execution order (flows through real ToolsExecutorLive) ---
2962
2963 // The backend transport creates the session before the gate; the Dashboard
2964 // transport has no session to create. A denied tool is sent nowhere on either.
2965 it.effect.each<{
2966 readonly transport: string;
2967 readonly env: Record<string, string>;
2968 readonly calls: ReadonlyArray<string>;
2969 }>([
2970 {
2971 transport: 'backend',
2972 // A custom backend with no web URL keeps the backend transport.
2973 env: { COMPOSIO_BASE_URL: 'https://composio.internal.example' },
2974 calls: ['create'],
2975 },
2976 { transport: 'dashboard', env: {}, calls: [] },
2977 ])(
2978 'on the $transport transport, prepares in order and sends nothing once the permission gate denies',
2979 ({ env, calls }) => {
2980 const toolRouterCalls: Array<string> = [];
2981 // Enhanced controls ask before every call; with no one to answer the
2982 // prompt (as under test), the gate denies.
2983 vi.spyOn(toolPermissions, 'getConsumerPermissionSnapshot').mockImplementation(params =>
2984 Effect.succeed({
2985 orgId: params.orgId,
2986 projectId: params.projectId,
2987 consumerUserId: params.consumerUserId,
2988 enhancedControlsEnabled: true,
2989 permissions: { default: 'ask_every_call' as const, overrides: {} },
2990 connectedAccountIds: [],
2991 fetchedAt: 1_767_225_600_000,
2992 })
2993 );
2994
2995 return Effect.gen(function* () {
2996 const exit = yield* cli([
2997 'execute',
2998 'GMAIL_SEND_EMAIL',
2999 '--skip-connection-check',
3000 '-d',
3001 '{"recipient":"a"}',
3002 ]).pipe(Effect.exit);
3003 const lines = yield* MockConsole.getLines({ stripAnsi: true });
3004
3005 expect(Exit.isFailure(exit)).toBe(true);
3006 expect(lines.join('\n')).toContain('requires interactive approval');
3007 expect(toolRouterCalls).toEqual(calls);
3008 }).pipe(
3009 Effect.provide(
3010 TestLive({
3011 baseConfigProvider: ConfigProvider.fromEnv({
3012 env: { COMPOSIO_USER_API_KEY: 'test_api_key', ...env },
3013 }),
3014 fixture: 'global-test-user-id',
3015 stdin: { isTTY: true, data: '' },
3016 dashboard: {
3017 respond: () => {
3018 toolRouterCalls.push('dashboard');
3019 return Response.error();
3020 },
3021 },
3022 toolRouter: {
3023 create: params =>
3024 Effect.sync(() => {
3025 toolRouterCalls.push('create');
3026 return {
3027 session_id: 'trs_backend_session',
3028 config: {
3029 user_id: params.user_id,
3030 execute: {},
3031 search: {},
3032 preload: { tools: [] },
3033 instant: false,
3034 },
3035 config_version: 1,
3036 mcp: { type: 'http' as const, url: 'https://mcp.test.composio.dev' },
3037 tool_router_tools: [],
3038 };
3039 }),
3040 execute: () =>
3041 Effect.sync(() => {
3042 toolRouterCalls.push('execute');
3043 return {
3044 result_type: 'completed' as const,
3045 data: {},
3046 error: null,
3047 log_id: 'log_test',
3048 };
3049 }),
3050 },
3051 })
3052 ),
3053 Effect.scoped
3054 );
3055 }
3056 );
3057
3058 // --- Tool Router error path (flows through real ToolsExecutorLive) ---
3059
3060 layer(
3061 TestLive({
3062 baseConfigProvider: testConfigProvider,
3063 fixture: 'global-test-user-id',
3064 stdin: { isTTY: true, data: '' },
3065 toolRouter: {
3066 execute: () => Effect.fail(clientBadRequest(noActiveConnectionEnvelope)),
3067 },
3068 })
3069 )('[Given] Tool Router NoActiveConnection error [Then] prints connection tips', it => {
3070 it.effect('prints connection tips with toolkit name derived from tool slug', () =>
3071 Effect.gen(function* () {
3072 yield* cli([
3073 'execute',
3074 'GMAIL_CREATE_EMAIL_DRAFT',
3075 '-d',
3076 '{"recipient":"to@example.com"}',
3077 ]).pipe(Effect.flip);
3078 const lines = yield* MockConsole.getLines({ stripAnsi: true });
3079 const output = lines.join('\n');
3080
3081 expect(output).toContain('Run `composio link gmail`, then retry.');
3082 expect(output).toContain('Tips');
3083 expect(output).toContain('composio link gmail');
3084 expect(output).not.toContain('COMPOSIO_MANAGE_CONNECTIONS');
3085 })
3086 );
3087 });
3088
3089 layer(
3090 TestLive({
3091 baseConfigProvider: testConfigProvider,
3092 fixture: 'global-test-user-id',
3093 stdin: { isTTY: true, data: '' },
3094 toolRouter: {
3095 execute: (_sessionId, params) =>
3096 Effect.succeed({
3097 result_type: 'completed' as const,
3098 data: { tool_slug: params.tool_slug, custom: 'response' },
3099 error: null,
3100 log_id: 'log_custom',
3101 }),
3102 },
3103 })
3104 )('[Given] custom Tool Router execute mock [Then] returns custom response', it => {
3105 it.effect('flows through real ToolsExecutorLive with custom mock', () =>
3106 Effect.gen(function* () {
3107 yield* cli(['execute', 'GITHUB_STAR_REPO', '-d', '{"owner":"composio","repo":"composio"}']);
3108 const lines = yield* MockConsole.getLines({ stripAnsi: true });
3109 const output = parseLastJson(lines);
3110
3111 expect(output.successful).toBe(true);
3112 expect(output.data.tool_slug).toBe('GITHUB_STAR_REPO');
3113 expect(output.data.custom).toBe('response');
3114 expect(output.logId).toBe('log_custom');
3115 })
3116 );
3117 });
3118
3119 layer(
3120 TestLive({
3121 baseConfigProvider: testConfigProvider,
3122 fixture: 'global-test-user-id',
3123 stdin: { isTTY: true, data: '' },
3124 toolRouter: {
3125 execute: () =>
3126 Effect.succeed({
3127 result_type: 'failed' as const,
3128 data: {},
3129 error: null,
3130 log_id: 'log_failed',
3131 }),
3132 },
3133 })
3134 )('[Given] Tool Router reports a failed execution with no error text', it => {
3135 it.effect('[Then] execute fails instead of reporting success', () =>
3136 Effect.gen(function* () {
3137 const failure = yield* cli([
3138 'execute',
3139 'GITHUB_STAR_REPO',
3140 '-d',
3141 '{"owner":"composio","repo":"composio"}',
3142 ]).pipe(Effect.flip);
3143 const lines = yield* MockConsole.getLines({ stripAnsi: true });
3144 const output = parseLastJson(lines);
3145
3146 expect(failure).toMatchObject({ _tag: 'ReportedToolExecutionError' });
3147 expect(output.successful).toBe(false);
3148 expect(output.error).toBeNull();
3149 expect(output.logId).toBe('log_failed');
3150 })
3151 );
3152 });
3153
3154 layer(
3155 TestLive({
3156 baseConfigProvider: testConfigProvider,
3157 fixture: 'global-test-user-id',
3158 stdin: { isTTY: true, data: '' },
3159 toolRouter: {
3160 execute: () =>
3161 Effect.succeed({
3162 result_type: 'input_required' as const,
3163 input_requests: {
3164 approval_1: {
3165 type: 'elicitation' as const,
3166 mode: 'form' as const,
3167 message: 'Allow GITHUB_STAR_REPO to star this repository?',
3168 requested_schema: { type: 'object', properties: { approved: { type: 'boolean' } } },
3169 },
3170 },
3171 request_state: 'opaque-state-token',
3172 }),
3173 },
3174 })
3175 )('[Given] Tool Router asks for user input [Then] execute fails without a result', it => {
3176 it.effect('reports that the tool requires user input', () =>
3177 Effect.gen(function* () {
3178 const failure = yield* cli([
3179 'execute',
3180 'GITHUB_STAR_REPO',
3181 '-d',
3182 '{"owner":"composio","repo":"composio"}',
3183 ]).pipe(Effect.flip);
3184 const lines = yield* MockConsole.getLines({ stripAnsi: true });
3185 const output = parseLastJson(lines);
3186
3187 expect(failure).toMatchObject({ _tag: 'ReportedToolExecutionError' });
3188 expect(output.successful).toBe(false);
3189 expect(output.error).toContain(
3190 'Tool GITHUB_STAR_REPO requires user input before it can run (1 input request) and was not executed.'
3191 );
3192 expect(lines.join('\n')).not.toContain('opaque-state-token');
3193 })
3194 );
3195 });
3196
3197 layer(
3198 TestLive({
3199 baseConfigProvider: testConfigProvider,
3200 fixture: 'global-test-user-id',
3201 stdin: { isTTY: true, data: '' },
3202 toolsExecutor: {
3203 failWith: {
3204 error: {
3205 message: 'Error executing the tool GMAIL_CREATE_EMAIL_DRAFT',
3206 },
3207 },
3208 },
3209 })
3210 )('[Given] executor throws wrapped error [Then] prints actionable message', it => {
3211 it.effect('prints actionable error details', () =>
3212 Effect.gen(function* () {
3213 yield* cli([
3214 'execute',
3215 'GMAIL_CREATE_EMAIL_DRAFT',
3216 '-d',
3217 '{\"recipient\":\"to@example.com\"}',
3218 ]).pipe(Effect.flip);
3219 const lines = yield* MockConsole.getLines({ stripAnsi: true });
3220 const output = lines.join('\n');
3221
3222 expect(output).toContain('Error executing the tool GMAIL_CREATE_EMAIL_DRAFT');
3223 })
3224 );
3225 });
3226
3227 layer(
3228 TestLive({
3229 baseConfigProvider: testConfigProvider,
3230 fixture: 'global-test-user-id',
3231 stdin: { isTTY: true, data: '' },
3232 toolsExecutor: {
3233 failWith: clientBadRequest({
3234 message: 'API error: invalid input',
3235 code: 1001,
3236 slug: 'Validation_Failed',
3237 status: 400,
3238 }),
3239 },
3240 })
3241 )('[Given] executor throws an API error [Then] prints message and details', it => {
3242 it.effect('prints API error message and details', () =>
3243 Effect.gen(function* () {
3244 yield* cli([
3245 'execute',
3246 'GMAIL_CREATE_EMAIL_DRAFT',
3247 '-d',
3248 '{\"recipient\":\"to@example.com\"}',
3249 ]).pipe(Effect.flip);
3250 const lines = yield* MockConsole.getLines({ stripAnsi: true });
3251 const output = lines.join('\n');
3252
3253 expect(output).toContain('API error: invalid input');
3254 expect(output).toContain('Error details');
3255 })
3256 );
3257 });
3258
3259 // --- Soft failure tests (API returns { successful: false }) ---
3260
3261 layer(
3262 TestLive({
3263 baseConfigProvider: testConfigProvider,
3264 fixture: 'global-test-user-id',
3265 stdin: { isTTY: true, data: '' },
3266 toolsExecutor: {
3267 respondWith: {
3268 data: {},
3269 error:
3270 "Invalid request data provided\n- Following fields are missing: {'recipient_email'}",
3271 successful: false,
3272 logId: 'log_test123',
3273 },
3274 },
3275 })
3276 )('[Given] tool returns soft failure with logId [Then] shows error and logId', it => {
3277 it.effect('shows error and logId for soft failure', () =>
3278 Effect.gen(function* () {
3279 yield* cli([
3280 'execute',
3281 'GMAIL_CREATE_EMAIL_DRAFT',
3282 '-d',
3283 '{\"recipient\":\"to@example.com\"}',
3284 ]).pipe(Effect.flip);
3285 const lines = yield* MockConsole.getLines({ stripAnsi: true });
3286 const output = lines.join('\n');
3287
3288 // Spinner should error, not succeed — no success message for the spinner
3289 expect(output).not.toContain('Executing tool');
3290 // Error message and logId should appear
3291 expect(output).toContain('Execution failed');
3292 expect(output).toContain('log_test123');
3293 expect(output).toContain('Invalid request data provided');
3294 })
3295 );
3296 });
3297
3298 layer(
3299 TestLive({
3300 baseConfigProvider: testConfigProvider,
3301 fixture: 'global-test-user-id',
3302 stdin: { isTTY: true, data: '' },
3303 toolsExecutor: {
3304 respondWith: {
3305 data: {},
3306 error: 'Tool execution failed',
3307 successful: false,
3308 logId: '',
3309 },
3310 },
3311 })
3312 )('[Given] tool returns soft failure without logId [Then] shows error without logId', it => {
3313 it.effect('shows error without logId for soft failure', () =>
3314 Effect.gen(function* () {
3315 yield* cli([
3316 'execute',
3317 'GMAIL_CREATE_EMAIL_DRAFT',
3318 '-d',
3319 '{\"recipient\":\"to@example.com\"}',
3320 ]).pipe(Effect.flip);
3321 const lines = yield* MockConsole.getLines({ stripAnsi: true });
3322 const output = lines.join('\n');
3323
3324 // Spinner should error, not succeed
3325 expect(output).not.toContain('Executing tool');
3326 expect(output).toContain('Execution failed');
3327 expect(output).toContain('Tool execution failed');
3328 // logId is empty, so the spinner line should not show "(logId: ...)"
3329 expect(output).not.toContain('(logId:');
3330 })
3331 );
3332 });
3333
3334 // --- Meta tool error tests ---
3335
3336 layer(
3337 TestLive({
3338 baseConfigProvider: testConfigProvider,
3339 fixture: 'global-test-user-id',
3340 stdin: { isTTY: true, data: '' },
3341 toolsExecutor: {
3342 failWith: new ComposioNoActiveConnectionError({
3343 details: {
3344 slug: 'ToolRouterV2_NoActiveConnection',
3345 message: 'No active connection found for toolkit(s) in this session',
3346 },
3347 toolSlug: 'COMPOSIO_SEARCH_TOOLS',
3348 }),
3349 },
3350 })
3351 )('[Given] meta tool NoActiveConnection error [Then] does not suggest "link composio"', it => {
3352 it.effect('omits connection tips for meta tool slugs', () =>
3353 Effect.gen(function* () {
3354 yield* cli(['execute', 'COMPOSIO_SEARCH_TOOLS', '-d', '{"query":"email"}']).pipe(
3355 Effect.flip
3356 );
3357 const lines = yield* MockConsole.getLines({ stripAnsi: true });
3358 const output = lines.join('\n');
3359
3360 expect(output).toContain('Link the required toolkit/app, then retry.');
3361 // Should NOT produce a misleading tip like "link composio"
3362 expect(output).not.toContain('link composio');
3363 expect(output).not.toContain('COMPOSIO_MANAGE_CONNECTIONS');
3364 })
3365 );
3366 });
3367
3368 // --- Edge case tests ---
3369
3370 layer(
3371 TestLive({
3372 baseConfigProvider: testConfigProvider,
3373 fixture: 'global-test-user-id',
3374 stdin: { isTTY: true, data: '' },
3375 })
3376 )('[Given] -d with invalid JSON [Then] fails with parse error', it => {
3377 it.effect('fails with invalid JSON error', () =>
3378 Effect.gen(function* () {
3379 const failure = yield* cli(['execute', 'GMAIL_SEND_EMAIL', '-d', 'not-valid-json']).pipe(
3380 Effect.flip
3381 );
3382 expectInvalidValueMessage(failure, 'Invalid JSON input');
3383 })
3384 );
3385 });
3386
3387 layer(
3388 TestLive({
3389 baseConfigProvider: testConfigProvider,
3390 fixture: 'global-test-user-id',
3391 stdin: { isTTY: true, data: '' },
3392 })
3393 )('[Given] -d with JSON array [Then] fails with expected-object error', it => {
3394 it.effect('fails with expected object error', () =>
3395 Effect.gen(function* () {
3396 const failure = yield* cli(['execute', 'GMAIL_SEND_EMAIL', '-d', '[1,2,3]']).pipe(
3397 Effect.flip
3398 );
3399 expectInvalidValueMessage(failure, 'Expected a JSON object');
3400 })
3401 );
3402 });
3403
3404 layer(
3405 TestLive({
3406 baseConfigProvider: testConfigProvider,
3407 fixture: 'global-test-user-id',
3408 stdin: { isTTY: true, data: '' },
3409 })
3410 )('[Given] -d with JSON string [Then] fails with expected-object error', it => {
3411 it.effect('fails with expected object error for string', () =>
3412 Effect.gen(function* () {
3413 const failure = yield* cli(['execute', 'GMAIL_SEND_EMAIL', '-d', '"just a string"']).pipe(
3414 Effect.flip
3415 );
3416 expectInvalidValueMessage(failure, 'Expected a JSON object');
3417 })
3418 );
3419 });
3420
3421 layer(
3422 TestLive({
3423 baseConfigProvider: testConfigProvider,
3424 fixture: 'global-test-user-id',
3425 stdin: { isTTY: true, data: '' },
3426 toolsExecutor: {
3427 respondWith: {
3428 data: { executed: true },
3429 error: null,
3430 successful: true,
3431 logId: 'log_test',
3432 },
3433 },
3434 })
3435 )('[Given] no -d and TTY stdin [Then] defaults to empty object and executes', it => {
3436 it.effect('defaults to {} when no data provided', () =>
3437 Effect.gen(function* () {
3438 yield* cli(['execute', 'GMAIL_SEND_EMAIL']);
3439 const lines = yield* MockConsole.getLines({ stripAnsi: true });
3440 const output = lines.join('\n');
3441
3442 expect(output).toContain('successful');
3443 expect(output).toContain('executed');
3444 })
3445 );
3446 });
3447
3448 layer(
3449 TestLive({
3450 baseConfigProvider: testConfigProvider,
3451 fixture: 'global-test-user-id',
3452 stdin: { isTTY: false, data: '' },
3453 })
3454 )('[Given] empty piped stdin [Then] fails with parse error', it => {
3455 it.effect('fails with error for empty stdin', () =>
3456 Effect.gen(function* () {
3457 const failure = yield* cli(['execute', 'GMAIL_SEND_EMAIL']).pipe(Effect.flip);
3458 expectInvalidValueMessage(failure, 'Invalid JSON input');
3459 })
3460 );
3461 });
3462
3463 // --- CI redaction tests ---
3464
3465 layer(
3466 TestLive({
3467 baseConfigProvider: testConfigProvider,
3468 fixture: 'global-test-user-id',
3469 stdin: { isTTY: true, data: '' },
3470 toolsExecutor: {
3471 respondWith: {
3472 data: {
3473 id: '19c8ab00a2e35870',
3474 labelIds: ['SENT'],
3475 threadId: '19c8ab00a2e35870',
3476 },
3477 error: null,
3478 successful: true,
3479 logId: 'log_uDPw0g_w8QCa',
3480 },
3481 },
3482 })
3483 )('[Given] CI redaction enabled [Then] redacts id-like fields and logId', it => {
3484 it.effect('redacts id, threadId, logId but preserves labelIds', () =>
3485 Effect.gen(function* () {
3486 // The suite's `afterEach` restores this spy.
3487 vi.spyOn(redactModule, 'redact').mockImplementation(
3488 (({ prefix }: { value: string; prefix?: string }) =>
3489 `${prefix ?? ''}<REDACTED>`) as typeof redactModule.redact
3490 );
3491
3492 yield* cli(['execute', 'GMAIL_SEND_EMAIL', '-d', '{"recipient_email":"to@example.com"}']);
3493 const lines = yield* MockConsole.getLines({ stripAnsi: true });
3494 const output = parseLastJson(lines) as unknown as {
3495 data: { id: string; labelIds: string[]; threadId: string };
3496 logId: string;
3497 successful: boolean;
3498 };
3499
3500 expect(output.data.id).toBe('<REDACTED>');
3501 expect(output.data.threadId).toBe('<REDACTED>');
3502 expect(output.data.labelIds).toEqual(['SENT']);
3503 expect(output.logId).toBe('log_<REDACTED>');
3504 expect(output.successful).toBe(true);
3505 })
3506 );
3507 });
3508 });
3509
3510 describe('CLI: composio execute without a resolvable acting user', () => {
3511 layer(
3512 TestLive({
3513 baseConfigProvider: testConfigProvider,
3514 fixture: 'global-test-user-id',
3515 stdin: { isTTY: true, data: '' },
3516 accountData: { consumerUserId: null },
3517 })
3518 )('[Given] the consumer project has no user id at all [Then] the root tree fails', it => {
3519 it.effect('[Then] the missing-user error carries the root message', () =>
3520 Effect.gen(function* () {
3521 const failure = yield* cli(['execute', 'GMAIL_SEND_EMAIL', '-d', '{}']).pipe(Effect.flip);
3522
3523 expect(failure).toBeInstanceOf(MissingActingUserError);
3524 expect(failure).toMatchObject({
3525 message: 'Missing consumer user id. Run `composio login` and try again.',
3526 });
3527 })
3528 );
3529 });
3530
3531 layer(
3532 TestLive({
3533 baseConfigProvider: ConfigProvider.fromEnv({
3534 env: {
3535 COMPOSIO_USER_API_KEY: 'test_api_key',
3536 COMPOSIO_ORG_ID: 'org_test',
3537 COMPOSIO_PROJECT_ID: 'proj_test',
3538 },
3539 }),
3540 stdin: { isTTY: true, data: '' },
3541 })
3542 )('[Given] a directory project and no user candidates [Then] the dev tree fails', it => {
3543 it.effect('[Then] the missing-user error carries the dev message', () =>
3544 Effect.gen(function* () {
3545 const userContext = yield* ComposioUserContext;
3546 yield* userContext.login('test_api_key', 'org_test');
3547 const failure = yield* cli([
3548 'dev',
3549 'playground-execute',
3550 'GMAIL_SEND_EMAIL',
3551 '-d',
3552 '{}',
3553 ]).pipe(Effect.flip);
3554
3555 expect(failure).toBeInstanceOf(MissingActingUserError);
3556 expect(failure).toMatchObject({
3557 message:
3558 'Missing user id. Provide --user-id or run `composio dev init` to set a playground test user id.',
3559 });
3560 })
3561 );
3562 });
3563
3564 layer(
3565 TestLive({
3566 baseConfigProvider: testConfigProvider,
3567 fixture: 'global-test-user-id',
3568 stdin: { isTTY: true, data: '' },
3569 accountData: { consumerUserId: '' },
3570 })
3571 )('[Given] an empty consumer user id [Then] the root tree treats it as no user', it => {
3572 it.effect('[Then] the execution fails with the missing-user error', () =>
3573 Effect.gen(function* () {
3574 const failure = yield* cli([
3575 'execute',
3576 'GMAIL_SEND_EMAIL',
3577 '--skip-connection-check',
3578 '-d',
3579 '{"recipient":"a"}',
3580 ]).pipe(Effect.flip);
3581
3582 expect(failure).toBeInstanceOf(MissingActingUserError);
3583 expect(failure).toMatchObject({
3584 message: 'Missing consumer user id. Run `composio login` and try again.',
3585 });
3586 })
3587 );
3588 });
3589
3590 // Formatting fixtures: standalone controls (BEL, DEL, NEL), CR/LF and a tab, in keys and values.
3591 const remoteText = 'first\r\nsecond\tcol\u0007 \u007f \u0085 end';
3592 /** DEL and C1, which JSON leaves as they are, and any C0 but LF in the printed text. */
3593 const shownControl = /[\u0000-\u0009\u000b-\u001f\u007f-\u009f]/u;
3594
3595 layer(
3596 TestLive({
3597 baseConfigProvider: testConfigProvider,
3598 fixture: 'global-test-user-id',
3599 stdin: { isTTY: true, data: '' },
3600 toolsExecutor: {
3601 respondWith: {
3602 successful: true,
3603 data: { status: 'failed', error: remoteText, ['note\u0085']: remoteText },
3604 error: null,
3605 logId: 'log_formatting',
3606 },
3607 },
3608 })
3609 )('[Given] a response whose data carries controls', it => {
3610 it.effect('[Then] the printed JSON escapes them and decodes to the same data', () =>
3611 Effect.gen(function* () {
3612 const terminal = redirectedTerminalUI({ decoratedStderr: true });
3613 yield* cli([
3614 'execute',
3615 'GMAIL_SEND_EMAIL',
3616 '--skip-connection-check',
3617 '-d',
3618 '{"recipient":"a"}',
3619 ]).pipe(Effect.provide(terminal.layer));
3620
3621 const stdout = terminal.stdout();
3622 expect(stdout).not.toMatch(shownControl);
3623 expect(stdout).toContain('"note\\u0085"');
3624 const output = JSON.parse(stdout) as { readonly data: unknown };
3625 expect(output.data).toEqual({
3626 status: 'failed',
3627 error: remoteText,
3628 ['note\u0085']: remoteText,
3629 });
3630 // The in-band warning quotes the response on one escaped line.
3631 expect(terminal.stderr()).toContain(
3632 'the response may contain an error: first\\u000d\\u000asecond\\u0009col\\u0007 \\u007f \\u0085 end'
3633 );
3634 expect(terminal.stderr()).not.toMatch(/[\u0007\u007f\u0085]/u);
3635 })
3636 );
3637 });
3638
3639 layer(
3640 TestLive({
3641 baseConfigProvider: testConfigProvider,
3642 fixture: 'global-test-user-id',
3643 stdin: { isTTY: true, data: '' },
3644 toolsExecutor: {
3645 failWith: new BadRequestError({
3646 message: `400 ${remoteText}`,
3647 method: 'POST',
3648 url: 'https://mock.invalid/api/v3.1/tools/execute/GMAIL_SEND_EMAIL',
3649 status: 400,
3650 requestId: undefined,
3651 details: { message: remoteText, code: 400, slug: 'Tool_Failed', status: 400 },
3652 body: undefined,
3653 }),
3654 },
3655 })
3656 )('[Given] an execution error whose message carries controls', it => {
3657 it.effect('[Then] stderr shows it escaped and the JSON result keeps the message', () =>
3658 Effect.gen(function* () {
3659 const terminal = redirectedTerminalUI({ decoratedStderr: true });
3660 const exit = yield* Effect.exit(
3661 cli([
3662 'execute',
3663 'GMAIL_SEND_EMAIL',
3664 '--skip-connection-check',
3665 '-d',
3666 '{"recipient":"a"}',
3667 ]).pipe(Effect.provide(terminal.layer))
3668 );
3669
3670 expect(Exit.isFailure(exit)).toBe(true);
3671 const stdout = terminal.stdout();
3672 expect(stdout).not.toMatch(shownControl);
3673 expect(JSON.parse(stdout)).toMatchObject({ successful: false, error: remoteText });
3674 const stderr = terminal.stderr();
3675 expect(stderr).toContain('first\\u000d\\u000asecond\\u0009col\\u0007 \\u007f \\u0085 end');
3676 expect(stderr).not.toMatch(/[\u0007\u007f\u0085]/u);
3677 })
3678 );
3679 });
3680 });