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

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 04:45 UTC

next
ts 831 lines 30.8 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 { describe, expect, it } from '@effect/vitest';
5 import { afterEach, beforeEach, vi } from 'vitest';
6 import {
7 APIConnectionError,
8 APIConnectionTimeoutError,
9 APIError,
10 RateLimitError,
11 type ComposioError,
12 } from '@composio/client/effect';
13 import type { SessionExecuteResponse } from '@composio/client/resources/tool-router';
14 import { ConfigProvider, Effect, Exit, Fiber, Option, Schema } from 'effect';
15 import { TestClock } from 'effect/testing';
16 import { TEMPORARY_FAILURE_EXIT_CODE } from 'src/effects/retry-advice';
17 import * as composioAccount from 'src/services/composio-account';
18 import * as consumerShortTermCache from 'src/services/consumer-short-term-cache';
19 import * as toolPermissions from 'src/services/tool-permissions';
20 import { ComposioUserContext } from 'src/services/user-context';
21 import {
22 cli,
23 dashboardProcedureError,
24 dashboardProcedureResult,
25 MockConsole,
26 TestLive,
27 type DashboardTestRequest,
28 } from 'test/__utils__';
29 import { exitCodeOf } from 'test/__utils__/exit-code';
30 import type { TestLiveInput } from 'test/__utils__/services/test-layer';
31
32 vi.mock('node:dns/promises', () => ({
33 lookup: vi.fn().mockResolvedValue([{ address: '93.184.216.34', family: 4 }]),
34 }));
35
36 // `src/ui/redact` reads `CI` once at import time; clear it before the imports
37 // above run so both transports print the same unredacted values everywhere.
38 vi.hoisted(() => {
39 delete process.env.CI;
40 });
41
42 const DEFAULT_DASHBOARD = 'https://dashboard.composio.dev';
43
44 const configProvider = (env: Record<string, string> = {}) =>
45 ConfigProvider.fromEnv({ env: { COMPOSIO_USER_API_KEY: 'test_api_key', ...env } });
46
47 const temporaryCacheDirs: Array<string> = [];
48
49 /**
50 * A cache directory whose `user_data.json` stores the given URLs, as a login
51 * made under other `COMPOSIO_*_URL` values would have left it.
52 */
53 const cacheDirWithStoredURLs = (stored: { base_url?: string; web_url?: string }) => {
54 const cacheDir = fs.mkdtempSync(path.join(os.tmpdir(), 'composio-cli-test-cache-'));
55 temporaryCacheDirs.push(cacheDir);
56 fs.writeFileSync(
57 path.join(cacheDir, 'user_data.json'),
58 JSON.stringify({
59 api_key: 'test_api_key',
60 // Distinct from every fixture, so a request carrying it proves this file was read.
61 org_id: 'org_stored',
62 project_id: null,
63 test_user_id: 'global-default',
64 base_url: null,
65 web_url: null,
66 ...stored,
67 })
68 );
69 return cacheDir;
70 };
71
72 /** A custom backend with no web URL: the one consumer setup that stays on the backend. */
73 const backendOnlyEnv = { COMPOSIO_BASE_URL: 'https://composio.internal.example' };
74
75 type ToolRouterCall = { readonly method: 'create' | 'execute' | 'executeMeta' } & Record<
76 string,
77 unknown
78 >;
79
80 /**
81 * One isolated CLI world: a Dashboard that records what it is sent, and a
82 * backend Tool Router that records what it is asked to do.
83 */
84 const makeWorld = (options: {
85 readonly env?: Record<string, string>;
86 readonly dashboard?: (request: DashboardTestRequest) => Response | Promise<Response>;
87 readonly backendExecute?: () => Effect.Effect<SessionExecuteResponse, ComposioError>;
88 readonly input?: Partial<TestLiveInput>;
89 }) => {
90 const dashboardRequests: Array<DashboardTestRequest> = [];
91 const toolRouterCalls: Array<ToolRouterCall> = [];
92 const backendResponse = (
93 data: Record<string, unknown>
94 ): Effect.Effect<SessionExecuteResponse, ComposioError> =>
95 options.backendExecute
96 ? options.backendExecute()
97 : Effect.succeed({ result_type: 'completed', data, error: null, log_id: 'log_test' });
98
99 const layer = TestLive({
100 baseConfigProvider: configProvider(options.env),
101 fixture: 'global-test-user-id',
102 stdin: { isTTY: true, data: '' },
103 dashboard: {
104 respond: request => {
105 dashboardRequests.push(request);
106 return options.dashboard
107 ? options.dashboard(request)
108 : dashboardProcedureResult({
109 ok: true,
110 response: {
111 result_type: 'completed',
112 data: { id: 'msg_1' },
113 error: null,
114 log_id: 'log_test',
115 },
116 });
117 },
118 },
119 toolRouter: {
120 create: params =>
121 Effect.sync(() => {
122 toolRouterCalls.push({ method: 'create', params });
123 return {
124 session_id: 'trs_backend_session',
125 config: { user_id: params.user_id },
126 mcp: { type: 'http' as const, url: 'https://mcp.test.composio.dev' },
127 tool_router_tools: [],
128 } as never;
129 }),
130 execute: (sessionId, params, requestOptions) =>
131 Effect.suspend(() => {
132 toolRouterCalls.push({ method: 'execute', sessionId, params, requestOptions });
133 return backendResponse({ id: 'msg_1' });
134 }),
135 executeMeta: (sessionId, params, requestOptions) =>
136 Effect.suspend(() => {
137 toolRouterCalls.push({ method: 'executeMeta', sessionId, params, requestOptions });
138 return backendResponse({ id: 'msg_1' });
139 }),
140 },
141 ...options.input,
142 });
143
144 /**
145 * Runs the CLI and returns everything it printed, how it exited, and the
146 * URLs in effect: `baseURL` is what the backend client is built with,
147 * `webURL` what the Dashboard request is built from.
148 */
149 const run = (args: ReadonlyArray<string>) =>
150 Effect.gen(function* () {
151 const exit = yield* cli(args).pipe(Effect.exit);
152 const lines = yield* MockConsole.getLines({ stripAnsi: true });
153 const { baseURL, webURL } = (yield* ComposioUserContext).data;
154 return { exit, lines, output: lines.join('\n'), baseURL, webURL };
155 }).pipe(Effect.provide(layer), Effect.scoped);
156
157 return { run, dashboardRequests, toolRouterCalls };
158 };
159
160 const JsonRecord = Schema.Record(Schema.String, Schema.Unknown);
161 const decodeJsonRecord = Schema.decodeUnknownSync(Schema.fromJsonString(JsonRecord));
162 const decodeProcedureBody = Schema.decodeUnknownSync(Schema.Struct({ json: JsonRecord }));
163
164 const lastJson = (lines: ReadonlyArray<string>): Record<string, unknown> => {
165 const line = lines.findLast(candidate => candidate.trimStart().startsWith('{'));
166 if (line === undefined) throw new Error('Expected JSON output but none found');
167 return decodeJsonRecord(line);
168 };
169
170 const requestJson = (request: DashboardTestRequest | undefined) =>
171 request === undefined ? undefined : decodeProcedureBody(request.body).json;
172
173 const EXECUTE_GMAIL = [
174 'execute',
175 'GMAIL_SEND_EMAIL',
176 '--skip-connection-check',
177 '-d',
178 '{"recipient":"a"}',
179 ] as const;
180
181 const walletExhausted = {
182 message: 'Your wallet balance is too low to run this tool.',
183 code: 4020,
184 slug: 'Wallet_InsufficientBalance',
185 status: 402,
186 request_id: 'req_wallet',
187 suggested_fix: 'Add credits in the dashboard, then retry.',
188 };
189
190 describe('CLI: composio execute transport', () => {
191 beforeEach(() => {
192 vi.spyOn(composioAccount, 'getLatestToolVersion').mockImplementation(() =>
193 Effect.fail(
194 new APIConnectionError({
195 message: 'Connection error.',
196 method: 'GET',
197 url: 'https://backend.composio.dev/api/v3/tools/{tool_slug}/get_latest_version',
198 reason: 'TransportError',
199 description: undefined,
200 })
201 )
202 );
203 vi.spyOn(consumerShortTermCache, 'getFreshConsumerConnectedToolkitsFromCache').mockReturnValue(
204 Effect.succeed(Option.some(['gmail', 'github']))
205 );
206 vi.spyOn(consumerShortTermCache, 'refreshConsumerConnectedToolkitsCache').mockImplementation(
207 () => Effect.succeed(['gmail', 'github'])
208 );
209 });
210 afterEach(() => {
211 vi.restoreAllMocks();
212 temporaryCacheDirs.forEach(directory => fs.rmSync(directory, { recursive: true, force: true }));
213 temporaryCacheDirs.length = 0;
214 });
215
216 it.effect('sends a consumer execution to the Dashboard and prints what the backend prints', () =>
217 Effect.gen(function* () {
218 const viaDashboard = makeWorld({});
219 const viaBackend = makeWorld({ env: backendOnlyEnv });
220
221 const dashboardRun = yield* viaDashboard.run(EXECUTE_GMAIL);
222 const backendRun = yield* viaBackend.run(EXECUTE_GMAIL);
223
224 expect(viaDashboard.dashboardRequests).toHaveLength(1);
225 const [request] = viaDashboard.dashboardRequests;
226 expect(request?.method).toBe('POST');
227 expect(request?.url).toBe(`${DEFAULT_DASHBOARD}/api/cli/trpc/execute`);
228 expect(request?.headers['authorization']).toBe('Bearer test_api_key');
229 expect(request?.headers['x-org-id']).toBe('org_test');
230 expect(requestJson(request)).toEqual({
231 tool_slug: 'GMAIL_SEND_EMAIL',
232 arguments: { recipient: 'a' },
233 });
234 // The Dashboard owns the session: none is created or executed on the backend.
235 expect(viaDashboard.toolRouterCalls).toEqual([]);
236
237 expect(Exit.isSuccess(dashboardRun.exit)).toBe(true);
238 expect(lastJson(dashboardRun.lines)).toEqual({
239 successful: true,
240 data: { id: 'msg_1' },
241 error: null,
242 logId: 'log_test',
243 });
244 expect(dashboardRun.lines).toEqual(backendRun.lines);
245 })
246 );
247
248 describe.each(['GMAIL_SEND_EMAIL', 'COMPOSIO_SEARCH_TOOLS'])('%s', slug => {
249 describe.each(['failed', 'input_required'] as const)('%s', resultType => {
250 it.effect(`preserves ${resultType} replies for ${slug} on both transports`, () =>
251 Effect.gen(function* () {
252 const response: SessionExecuteResponse =
253 resultType === 'failed'
254 ? { result_type: 'failed', data: {}, error: null, log_id: 'log_failed' }
255 : {
256 result_type: 'input_required',
257 input_requests: {
258 approval: {
259 type: 'elicitation',
260 mode: 'form',
261 message: 'Approve this tool?',
262 requested_schema: { type: 'object' },
263 },
264 },
265 request_state: 'opaque-state-token',
266 };
267 const viaDashboard = makeWorld({
268 dashboard: () => dashboardProcedureResult({ ok: true, response }),
269 });
270 const viaBackend = makeWorld({
271 env: backendOnlyEnv,
272 backendExecute: () => Effect.succeed(response),
273 });
274 const args = ['execute', slug, '--skip-connection-check', '-d', '{}'];
275 const dashboardRun = yield* viaDashboard.run(args);
276 const backendRun = yield* viaBackend.run(args);
277
278 expect(Exit.isFailure(dashboardRun.exit)).toBe(true);
279 expect(Exit.isFailure(backendRun.exit)).toBe(true);
280 expect(dashboardRun.lines).toEqual(backendRun.lines);
281 expect(lastJson(dashboardRun.lines)).toMatchObject({ successful: false });
282 if (resultType === 'input_required') {
283 expect(dashboardRun.output).toContain('requires user input before it can run');
284 expect(dashboardRun.output).not.toContain('opaque-state-token');
285 } else {
286 expect(lastJson(dashboardRun.lines)).toMatchObject({
287 error: null,
288 logId: 'log_failed',
289 });
290 }
291 expect(viaDashboard.dashboardRequests).toHaveLength(1);
292 expect(viaDashboard.toolRouterCalls).toEqual([]);
293 expect(viaBackend.toolRouterCalls.map(call => call.method)).toEqual([
294 'create',
295 slug === 'COMPOSIO_SEARCH_TOOLS' ? 'executeMeta' : 'execute',
296 ]);
297 expect(viaBackend.toolRouterCalls.at(-1)?.requestOptions).toBeUndefined();
298 })
299 );
300 });
301 });
302
303 it.effect('keeps the backend transport for a custom backend with no web URL', () =>
304 Effect.gen(function* () {
305 const world = makeWorld({ env: backendOnlyEnv });
306
307 const { exit } = yield* world.run(EXECUTE_GMAIL);
308
309 expect(Exit.isSuccess(exit)).toBe(true);
310 expect(world.dashboardRequests).toEqual([]);
311 expect(world.toolRouterCalls.map(call => call.method)).toEqual(['create', 'execute']);
312 // An execution does not ask for retries, so the client sends it once: a
313 // retry after the backend already acted would duplicate the side effect.
314 expect(world.toolRouterCalls.at(-1)?.requestOptions).toBeUndefined();
315 })
316 );
317
318 it.effect('never retries a meta tool execution on the backend transport', () =>
319 Effect.gen(function* () {
320 const world = makeWorld({ env: backendOnlyEnv });
321
322 yield* world.run(['execute', 'COMPOSIO_SEARCH_TOOLS', '-d', '{"query":"email"}']);
323
324 expect(world.dashboardRequests).toEqual([]);
325 expect(world.toolRouterCalls.at(-1)).toMatchObject({ method: 'executeMeta' });
326 expect(world.toolRouterCalls.at(-1)?.requestOptions).toBeUndefined();
327 })
328 );
329
330 describe.each(['https://dashboard.internal.example', 'https://dashboard.internal.example/'])(
331 '%s',
332 webURL => {
333 it.effect(`uses the Dashboard at ${webURL} when both URLs are overridden`, () =>
334 Effect.gen(function* () {
335 // This is also the environment a `composio run` script's `execute()`
336 // gives its child CLI: the parent forwards both URLs.
337 const world = makeWorld({ env: { ...backendOnlyEnv, COMPOSIO_WEB_URL: webURL } });
338
339 yield* world.run(EXECUTE_GMAIL);
340
341 expect(world.dashboardRequests.map(request => request.url)).toEqual([
342 'https://dashboard.internal.example/api/cli/trpc/execute',
343 ]);
344 expect(world.toolRouterCalls).toEqual([]);
345 })
346 );
347 }
348 );
349
350 it.effect('sends a meta tool to the executeMeta procedure', () =>
351 Effect.gen(function* () {
352 const world = makeWorld({});
353
354 const { exit } = yield* world.run([
355 'execute',
356 'COMPOSIO_SEARCH_TOOLS',
357 '-d',
358 '{"query":"email"}',
359 ]);
360
361 expect(Exit.isSuccess(exit)).toBe(true);
362 expect(world.dashboardRequests.map(request => request.url)).toEqual([
363 `${DEFAULT_DASHBOARD}/api/cli/trpc/executeMeta`,
364 ]);
365 expect(requestJson(world.dashboardRequests[0])).toMatchObject({
366 slug: 'COMPOSIO_SEARCH_TOOLS',
367 arguments: { query: 'email' },
368 });
369 expect(world.toolRouterCalls).toEqual([]);
370 })
371 );
372
373 it.effect('sends each parallel tool on its own and keeps the siblings of a failed one', () =>
374 Effect.gen(function* () {
375 const world = makeWorld({
376 dashboard: request =>
377 requestJson(request)?.tool_slug === 'GITHUB_CREATE_ISSUE'
378 ? dashboardProcedureResult({
379 ok: false,
380 status: 400,
381 error: {
382 message: 'Repository not found',
383 code: 1803,
384 slug: 'Tool_Failed',
385 status: 400,
386 request_id: 'req_parallel',
387 },
388 })
389 : dashboardProcedureResult({
390 ok: true,
391 response: {
392 result_type: 'completed',
393 data: { ran: requestJson(request)?.tool_slug },
394 error: null,
395 log_id: 'log_parallel',
396 },
397 }),
398 });
399
400 const { exit, lines } = yield* world.run([
401 'execute',
402 '--parallel',
403 '--skip-checks',
404 'GMAIL_SEND_EMAIL',
405 '-d',
406 '{"recipient":"a"}',
407 'GITHUB_CREATE_ISSUE',
408 '-d',
409 '{"title":"Bug"}',
410 'GMAIL_CREATE_EMAIL_DRAFT',
411 '-d',
412 '{"recipient":"b"}',
413 ]);
414
415 expect(Exit.isFailure(exit)).toBe(true);
416 expect(
417 world.dashboardRequests.map(request => requestJson(request)?.tool_slug).sort()
418 ).toEqual(['GITHUB_CREATE_ISSUE', 'GMAIL_CREATE_EMAIL_DRAFT', 'GMAIL_SEND_EMAIL']);
419 expect(world.toolRouterCalls).toEqual([]);
420 expect(lastJson(lines).results).toEqual([
421 expect.objectContaining({ slug: 'GMAIL_SEND_EMAIL', successful: true }),
422 { slug: 'GITHUB_CREATE_ISSUE', successful: false, error: 'Repository not found' },
423 expect.objectContaining({ slug: 'GMAIL_CREATE_EMAIL_DRAFT', successful: true }),
424 ]);
425 })
426 );
427
428 it.effect('keeps `dev playground-execute` on the backend', () =>
429 Effect.gen(function* () {
430 const world = makeWorld({});
431
432 const { exit, output } = yield* world.run([
433 'dev',
434 'playground-execute',
435 'GMAIL_SEND_EMAIL',
436 '--skip-connection-check',
437 '-d',
438 '{"recipient":"a"}',
439 ]);
440
441 expect(output).toContain('msg_1');
442 expect(Exit.isSuccess(exit)).toBe(true);
443 expect(world.dashboardRequests).toEqual([]);
444 expect(world.toolRouterCalls.map(call => call.method)).toEqual(['create', 'execute']);
445 })
446 );
447
448 it.effect('resolves the project and uploads files on the backend before the Dashboard call', () =>
449 Effect.gen(function* () {
450 const events: Array<string> = [];
451 const tempFile = path.join(os.tmpdir(), `composio-upload-${crypto.randomUUID()}.txt`);
452 fs.writeFileSync(tempFile, 'hello from cli upload', 'utf8');
453 const originalFetch = globalThis.fetch;
454 vi.spyOn(globalThis, 'fetch').mockImplementation((input, init) => {
455 const url =
456 typeof input === 'string' ? input : input instanceof URL ? input.toString() : input.url;
457 if (url === 'https://s3.test.composio.dev/upload') {
458 events.push('backend:file-upload');
459 return Promise.resolve(new Response(null, { status: 200 }));
460 }
461 return originalFetch(input, init);
462 });
463
464 const world = makeWorld({
465 dashboard: () => {
466 events.push('dashboard:execute');
467 return dashboardProcedureResult({
468 ok: true,
469 response: { result_type: 'completed', data: {}, error: null, log_id: 'log_upload' },
470 });
471 },
472 input: {
473 accountData: { onRequest: request => events.push(`backend:${request.operation}`) },
474 toolkitsData: {
475 tools: [
476 {
477 name: 'Send Email',
478 slug: 'GMAIL_SEND_EMAIL',
479 description: 'Send an email with attachment',
480 tags: ['email'],
481 available_versions: ['20260316_00'],
482 input_parameters: {
483 type: 'object',
484 properties: {
485 recipient_email: { type: 'string' },
486 attachment: { file_uploadable: true, title: 'Attachment' },
487 },
488 },
489 output_parameters: { type: 'object', properties: {} },
490 },
491 ],
492 } satisfies TestLiveInput['toolkitsData'],
493 },
494 });
495
496 const { exit } = yield* world.run([
497 'execute',
498 'GMAIL_SEND_EMAIL',
499 '-d',
500 JSON.stringify({ recipient_email: 'a@b.com', attachment: tempFile }),
501 ]);
502 fs.rmSync(tempFile, { force: true });
503
504 expect(Exit.isSuccess(exit)).toBe(true);
505 expect(events).toContain('backend:org.consumer.project.resolve');
506 expect(events).toContain('backend:file-upload');
507 expect(events.at(-1)).toBe('dashboard:execute');
508 expect(events.filter(event => event === 'dashboard:execute')).toHaveLength(1);
509 // The Dashboard receives the uploaded file reference, not the local path.
510 expect(requestJson(world.dashboardRequests[0])?.arguments).toEqual({
511 recipient_email: 'a@b.com',
512 attachment: {
513 name: path.basename(tempFile),
514 mimetype: 'text/plain',
515 s3key: `uploads/${path.basename(tempFile)}`,
516 },
517 });
518 })
519 );
520
521 it.effect('sends nothing to the Dashboard when the permission gate denies the tool', () =>
522 Effect.gen(function* () {
523 // Enhanced controls ask before every call; with no one to answer the
524 // prompt (as under test), the gate denies.
525 vi.spyOn(toolPermissions, 'getConsumerPermissionSnapshot').mockImplementation(params =>
526 Effect.succeed({
527 orgId: params.orgId,
528 projectId: params.projectId,
529 consumerUserId: params.consumerUserId,
530 enhancedControlsEnabled: true,
531 permissions: { default: 'ask_every_call' as const, overrides: {} },
532 connectedAccountIds: [],
533 fetchedAt: 1_767_225_600_000,
534 })
535 );
536 const world = makeWorld({});
537
538 const { exit, output } = yield* world.run(EXECUTE_GMAIL);
539
540 expect(Exit.isFailure(exit)).toBe(true);
541 expect(output).toContain('requires interactive approval');
542 expect(world.dashboardRequests).toEqual([]);
543 expect(world.toolRouterCalls).toEqual([]);
544 })
545 );
546
547 it.effect('says to check first when an execution timed out, since the tool may have run', () =>
548 Effect.gen(function* () {
549 const world = makeWorld({
550 env: backendOnlyEnv,
551 backendExecute: () =>
552 Effect.fail(
553 new APIConnectionTimeoutError({
554 message: 'Request timed out.',
555 method: 'POST',
556 url: 'https://mock.invalid/api/v3.1/tool_router/session/{session_id}/execute',
557 })
558 ),
559 });
560
561 const { exit, lines } = yield* world.run(EXECUTE_GMAIL);
562
563 expect(lastJson(lines)).toMatchObject({ successful: false, retry: 'check-first' });
564 expect(exitCodeOf(exit)).toBe(1);
565 // Sent once: the client never repeats an execution.
566 expect(world.toolRouterCalls.filter(call => call.method === 'execute')).toHaveLength(1);
567 })
568 );
569
570 it.effect('says a rate-limited execution is safe to run again and exits 75', () =>
571 Effect.gen(function* () {
572 const world = makeWorld({
573 env: backendOnlyEnv,
574 backendExecute: () =>
575 Effect.fail(
576 new RateLimitError({
577 message: '429 Too many requests',
578 method: 'POST',
579 url: 'https://mock.invalid/api/v3.1/tool_router/session/{session_id}/execute',
580 status: 429,
581 requestId: 'req_rate',
582 details: undefined,
583 body: undefined,
584 })
585 ),
586 });
587
588 const { exit, lines } = yield* world.run(EXECUTE_GMAIL);
589
590 expect(lastJson(lines)).toMatchObject({ successful: false, retry: 'safe' });
591 expect(exitCodeOf(exit)).toBe(TEMPORARY_FAILURE_EXIT_CODE);
592 })
593 );
594
595 it.effect('reports a relayed wallet failure exactly as the backend transport does', () =>
596 Effect.gen(function* () {
597 const viaDashboard = makeWorld({
598 dashboard: () =>
599 dashboardProcedureResult({ ok: false, status: 402, error: walletExhausted }),
600 });
601 const viaBackend = makeWorld({
602 env: backendOnlyEnv,
603 backendExecute: () =>
604 Effect.fail(
605 new APIError({
606 message: `402 ${JSON.stringify({ error: walletExhausted })}`,
607 method: 'POST',
608 url: 'https://mock.invalid/api/v3.1/tool_router/session/{session_id}/execute',
609 status: 402,
610 requestId: walletExhausted.request_id,
611 details: walletExhausted,
612 body: { error: walletExhausted },
613 })
614 ),
615 });
616
617 const dashboardRun = yield* viaDashboard.run(EXECUTE_GMAIL);
618 const backendRun = yield* viaBackend.run(EXECUTE_GMAIL);
619
620 expect(Exit.isFailure(dashboardRun.exit)).toBe(true);
621 expect(dashboardRun.output).toContain(walletExhausted.message);
622 expect(dashboardRun.output).toContain('Error details');
623 expect(dashboardRun.output).toContain(walletExhausted.suggested_fix);
624 expect(lastJson(dashboardRun.lines)).toEqual({
625 successful: false,
626 error: walletExhausted.message,
627 slug: walletExhausted.slug,
628 });
629 expect(dashboardRun.lines).toEqual(backendRun.lines);
630 expect(viaDashboard.dashboardRequests).toHaveLength(1);
631 })
632 );
633
634 it.effect('shows the `composio link` tip for a relayed missing-connection error', () =>
635 Effect.gen(function* () {
636 const world = makeWorld({
637 dashboard: () =>
638 dashboardProcedureResult({
639 ok: false,
640 status: 400,
641 error: {
642 message: "No active connection found for toolkit(s) 'gmail' in this session",
643 code: 4302,
644 slug: 'ToolRouterV2_NoActiveConnection',
645 status: 400,
646 request_id: 'req_no_connection',
647 },
648 }),
649 });
650
651 const { exit, output } = yield* world.run(EXECUTE_GMAIL);
652
653 expect(Exit.isFailure(exit)).toBe(true);
654 expect(output).toContain('Run `composio link gmail`, then retry.');
655 expect(output).toContain('Tips');
656 })
657 );
658
659 it.effect('suggests `composio login` when the Dashboard rejects the credentials', () =>
660 Effect.gen(function* () {
661 const world = makeWorld({
662 dashboard: () =>
663 dashboardProcedureError({
664 code: 'UNAUTHORIZED',
665 httpStatus: 401,
666 message: 'Invalid API key',
667 }),
668 });
669
670 const { exit, lines, output } = yield* world.run(EXECUTE_GMAIL);
671
672 expect(Exit.isFailure(exit)).toBe(true);
673 expect(output).toContain('Authentication failed: Invalid API key');
674 expect(output).toContain('composio login');
675 expect(lastJson(lines)).toMatchObject({ successful: false });
676 expect(world.dashboardRequests).toHaveLength(1);
677 expect(world.toolRouterCalls).toEqual([]);
678 })
679 );
680
681 // Stored URLs never take effect: the backend client and the Dashboard request
682 // both use the environment (or the defaults), which is also what the
683 // transport choice reads. These pin that the three cannot disagree.
684 const STORED_BACKEND = 'https://stored-backend.internal.example';
685 const STORED_DASHBOARD = 'https://stored-dashboard.internal.example';
686 const storedURLCases: ReadonlyArray<{
687 readonly name: string;
688 readonly stored: { base_url?: string; web_url?: string };
689 readonly env: Record<string, string>;
690 readonly baseURL: string;
691 /** The one Dashboard origin that receives the key, or none on the backend transport. */
692 readonly dashboard: string | undefined;
693 }> = [
694 {
695 name: 'a stored custom backend URL and no environment override',
696 stored: { base_url: STORED_BACKEND },
697 env: {},
698 baseURL: 'https://backend.composio.dev',
699 dashboard: DEFAULT_DASHBOARD,
700 },
701 {
702 name: 'stored custom backend and web URLs and no environment override',
703 stored: { base_url: STORED_BACKEND, web_url: STORED_DASHBOARD },
704 env: {},
705 baseURL: 'https://backend.composio.dev',
706 dashboard: DEFAULT_DASHBOARD,
707 },
708 {
709 name: 'a custom backend URL in the environment and a stored default web URL',
710 stored: { web_url: 'https://dashboard.composio.dev/' },
711 env: backendOnlyEnv,
712 baseURL: backendOnlyEnv.COMPOSIO_BASE_URL,
713 dashboard: undefined,
714 },
715 {
716 name: 'a stored web URL that differs from the one in the environment',
717 stored: { web_url: STORED_DASHBOARD },
718 env: { COMPOSIO_WEB_URL: 'https://env-dashboard.internal.example' },
719 baseURL: 'https://backend.composio.dev',
720 dashboard: 'https://env-dashboard.internal.example',
721 },
722 ];
723
724 describe.each(storedURLCases)('$name', testCase => {
725 it.effect(`sends the key to one consistent environment given ${testCase.name}`, () =>
726 Effect.gen(function* () {
727 const world = makeWorld({
728 env: { ...testCase.env, COMPOSIO_CACHE_DIR: cacheDirWithStoredURLs(testCase.stored) },
729 });
730
731 const { exit, baseURL, webURL } = yield* world.run(EXECUTE_GMAIL);
732
733 expect(Exit.isSuccess(exit)).toBe(true);
734 expect(baseURL).toBe(testCase.baseURL);
735 expect(webURL).not.toContain('stored-dashboard');
736 if (testCase.dashboard === undefined) {
737 expect(world.dashboardRequests).toEqual([]);
738 expect(world.toolRouterCalls.map(call => call.method)).toEqual(['create', 'execute']);
739 } else {
740 expect(
741 world.dashboardRequests.map(request => [
742 request.url,
743 request.headers['authorization'],
744 request.headers['x-org-id'],
745 ])
746 ).toEqual([
747 [`${testCase.dashboard}/api/cli/trpc/execute`, 'Bearer test_api_key', 'org_stored'],
748 ]);
749 expect(world.toolRouterCalls).toEqual([]);
750 }
751 })
752 );
753 });
754
755 const unsafeWebURLs: ReadonlyArray<{ readonly name: string; readonly webURL: string }> = [
756 { name: 'plain http to a remote host', webURL: 'http://dashboard.internal.example' },
757 {
758 name: 'credentials, a query string and a fragment',
759 webURL: 'https://user:s3cret-pass@dashboard.internal.example/?token=s3cret-token#s3cret-frag',
760 },
761 ];
762
763 describe.each(unsafeWebURLs)('$name', testCase => {
764 it.effect(`refuses a web URL with ${testCase.name} without sending or echoing it`, () =>
765 Effect.gen(function* () {
766 const world = makeWorld({ env: { COMPOSIO_WEB_URL: testCase.webURL } });
767
768 const { exit, lines, output } = yield* world.run(EXECUTE_GMAIL);
769
770 expect(Exit.isFailure(exit)).toBe(true);
771 expect(world.dashboardRequests).toEqual([]);
772 expect(world.toolRouterCalls).toEqual([]);
773 // Covers both the human lines and the JSON summary.
774 expect(output).toContain('COMPOSIO_WEB_URL');
775 expect(output).not.toContain('s3cret');
776 expect(output).not.toContain('dashboard.internal.example');
777 expect(lastJson(lines)).toMatchObject({ successful: false });
778 })
779 );
780 });
781
782 it.effect('reports a Dashboard timeout once, saying the tool may still have run', () =>
783 Effect.gen(function* () {
784 const world = makeWorld({ dashboard: () => new Promise<Response>(() => undefined) });
785
786 const fiber = yield* Effect.forkChild(world.run(EXECUTE_GMAIL));
787 // Let the command reach the Dashboard call, then pass the deadline.
788 yield* Effect.promise(() =>
789 vi.waitFor(() => expect(world.dashboardRequests).toHaveLength(1))
790 );
791 yield* TestClock.adjust('15 minutes');
792 const { exit, output } = yield* Fiber.join(fiber);
793
794 expect(Exit.isFailure(exit)).toBe(true);
795 expect(output).toContain('did not answer within 15 minutes');
796 expect(output).toContain('The tool may still have run; check before running it again.');
797 expect(world.dashboardRequests).toHaveLength(1);
798 expect(world.toolRouterCalls).toEqual([]);
799 })
800 );
801
802 const unreachable: ReadonlyArray<{
803 readonly name: string;
804 readonly dashboard: () => Response | Promise<Response>;
805 }> = [
806 {
807 name: 'the Dashboard cannot be reached',
808 dashboard: () => Promise.reject(new Error('connect ECONNREFUSED')),
809 },
810 {
811 name: 'the Dashboard answers with something other than JSON',
812 dashboard: () => new Response('<html>Bad gateway</html>', { status: 502 }),
813 },
814 ];
815
816 describe.each(unreachable)('$name', testCase => {
817 it.effect(`names the Dashboard and does not resend or fall back when ${testCase.name}`, () =>
818 Effect.gen(function* () {
819 const world = makeWorld({ dashboard: testCase.dashboard });
820
821 const { exit, output } = yield* world.run(EXECUTE_GMAIL);
822
823 expect(Exit.isFailure(exit)).toBe(true);
824 expect(output).toContain('dashboard.composio.dev');
825 expect(output).toContain('was not retried');
826 expect(world.dashboardRequests).toHaveLength(1);
827 expect(world.toolRouterCalls).toEqual([]);
828 })
829 );
830 });
831 });