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 105 lines 4.55 KB
Raw
1 import { vi } from 'vitest';
2 import { Effect, Layer } from 'effect';
3 import { ComposioClientSingleton, type ClientScope } from 'src/services/composio-clients';
4 import { describe, expect, it } from '@effect/vitest';
5 import { cliExit, MockConsole, recordedInvocationCalls, pkg, TestLive } from 'test/__utils__';
6 import { stubClientResources } from 'test/__utils__/services/stub-composio-client';
7
8 // The example enters the real root tree, registration and runInvocation. No production command changes.
9 vi.mock(
10 'src/commands/connections/list/connections.list.cmd',
11 () => import('./command-example.cmd')
12 );
13
14 const invoke = (args: ReadonlyArray<string>, loggedIn = true) =>
15 Effect.gen(function* () {
16 const stub = stubClientResources(['connectedAccounts'], () =>
17 Response.json({ items: [], next_cursor: null })
18 );
19 const scopes: ClientScope[] = [];
20 const recordingFactory = Layer.effect(
21 ComposioClientSingleton,
22 Effect.gen(function* () {
23 const base = yield* ComposioClientSingleton;
24 return ComposioClientSingleton.of({
25 ...base,
26 getFor: params =>
27 Effect.suspend(() => {
28 scopes.push(params);
29 return base.getFor(params);
30 }),
31 });
32 })
33 );
34 const { exitCode } = yield* cliExit(args).pipe(
35 Effect.provide(recordingFactory),
36 Effect.provide(stub.layer)
37 );
38 const stdout = yield* MockConsole.getLines({ stream: 'stdout', stripAnsi: true });
39 const stderr = yield* MockConsole.getLines({ stream: 'stderr', stripAnsi: true });
40 const calls = yield* recordedInvocationCalls;
41 const lifecycle = calls.flatMap(call =>
42 call.service === 'CommandLifecycleTelemetry' && call.event !== 'pluginHintShown'
43 ? [{ event: call.event, path: call.context.commandPath }]
44 : []
45 );
46 return { exitCode, stdout, stderr, calls, lifecycle, requests: stub.requests, scopes };
47 }).pipe(Effect.provide(TestLive(loggedIn ? { fixture: 'user-config-with-global-context' } : {})));
48
49 describe('CLI skill command example', () => {
50 it.effect('maps an optional flag to a native Effect client request and prints only data', () =>
51 Effect.gen(function* () {
52 const result = yield* invoke(['connections', 'list', '--toolkit', 'github']);
53 expect(result.exitCode).toBe(0);
54 expect(result.stdout).toEqual(['[]']);
55 expect(result.stderr).toEqual([]);
56 expect(result.requests).toHaveLength(1);
57 expect(result.scopes.at(-1)).toEqual({ orgId: 'org_1', projectId: 'consumer_project_test' });
58 const request = result.requests[0]!;
59 expect(request.method).toBe('GET');
60 const url = new URL(request.path, 'https://stub.invalid');
61 expect(url.pathname).toBe('/api/v3.1/connected_accounts');
62 expect(url.searchParams.get('toolkit_slugs')).toBe('github');
63 expect(url.searchParams.get('user_ids')).toBe('consumer-user-org_1');
64 expect(result.lifecycle).toEqual([
65 { event: 'invoked', path: 'connections list' },
66 { event: 'succeeded', path: 'connections list' },
67 ]);
68 })
69 );
70
71 it.effect('omits the filter when absent', () =>
72 Effect.gen(function* () {
73 const result = yield* invoke(['connections', 'list']);
74 expect(result.exitCode).toBe(0);
75 expect(result.requests).toHaveLength(1);
76 const url = new URL(result.requests[0]!.path, 'https://stub.invalid');
77 expect(url.searchParams.has('toolkit_slugs')).toBe(false);
78 })
79 );
80
81 it.effect('enforces inherited login metadata before sending requests', () =>
82 Effect.gen(function* () {
83 const result = yield* invoke(['connections', 'list'], false);
84 expect(result.exitCode).toBe(1);
85 expect(result.requests).toEqual([]);
86 expect(result.stdout).toEqual([]);
87 expect(result.stderr.join('\n')).toContain('composio login');
88 expect(result.lifecycle.at(-1)).toEqual({ event: 'failed', path: 'connections list' });
89 })
90 );
91
92 it.effect.each([
93 { args: ['connections', 'list', '--help'], code: 0, text: '--toolkit' },
94 { args: ['connections', 'list', '--version'], code: 0, text: pkg.version },
95 { args: ['connections', 'list', '--unknown'], code: 1, text: '--unknown' },
96 ])('handles $args without reaching the handler', ({ args, code, text }) =>
97 Effect.gen(function* () {
98 const result = yield* invoke(args, false);
99 expect(result.exitCode).toBe(code);
100 expect(result.requests).toEqual([]);
101 expect([...result.stdout, ...result.stderr].join('\n')).toContain(text);
102 expect(result.calls.filter(call => call.service !== 'CommandLifecycleTelemetry')).toEqual([]);
103 })
104 );
105 });