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 211 lines 7.75 KB
Raw
1 import { describe, expect, it, layer } from '@effect/vitest';
2 import { Cause, ConfigProvider, Data, Effect, Exit, Layer, Runtime } from 'effect';
3 import { ExpectedFailure } from 'src/effects/expected-failure';
4 import { ReportedFailure } from 'src/effects/report-failure';
5 import { runnerCause } from 'src/invocation/invocation';
6 import { ComposioToolkitsRepository } from 'src/services/composio-clients';
7 import {
8 cliExit,
9 exitCodeOf,
10 MockConsole,
11 recordedInvocationCalls,
12 resetInvocationCalls,
13 TestLive,
14 } from 'test/__utils__';
15 import type { TestLiveInput } from 'test/__utils__/services/test-layer';
16 import { makeToolkitsRepositoryStub } from 'test/__utils__/services/toolkits-repository-stub';
17
18 /** Logged in through the environment, with no current org stored. */
19 const loggedInWithoutOrg: TestLiveInput = {
20 baseConfigProvider: ConfigProvider.fromEnvRecord({ COMPOSIO_USER_API_KEY: 'uak_test' }),
21 };
22
23 /**
24 * Runs `args` over a fresh `TestLive`, with `overrides` replacing its services, and returns its
25 * exit code and output.
26 */
27 const invoke = (
28 args: ReadonlyArray<string>,
29 input?: TestLiveInput,
30 overrides: Layer.Layer<never> = Layer.empty
31 ) =>
32 Effect.gen(function* () {
33 const { exit, exitCode } = yield* cliExit(args).pipe(Effect.provide(overrides));
34 const stdout = yield* MockConsole.getLines({ stream: 'stdout', stripAnsi: true });
35 const stderr = yield* MockConsole.getLines({ stream: 'stderr', stripAnsi: true });
36 return { exit, exitCode, stdout, stderr };
37 }).pipe(Effect.provide(TestLive(input)));
38
39 /** The services the recording doubles saw, and the lifecycle event names, in order. */
40 const recordedSequence = Effect.map(recordedInvocationCalls, calls =>
41 calls.map(call =>
42 call.service === 'CommandLifecycleTelemetry' ? `${call.service}.${call.event}` : call.service
43 )
44 );
45
46 describe('runInvocation', () => {
47 layer(TestLive())('lifecycle', it => {
48 it.effect.each([
49 { args: ['version'], exitCode: 0, outcome: 'succeeded', hooks: true },
50 { args: [], exitCode: 0, outcome: 'succeeded', hooks: false },
51 { args: ['help'], exitCode: 0, outcome: 'succeeded', hooks: false },
52 { args: ['--help'], exitCode: 0, outcome: 'succeeded', hooks: false },
53 { args: ['--version'], exitCode: 0, outcome: 'succeeded', hooks: false },
54 { args: ['help', 'orgz'], exitCode: 1, outcome: 'failed', hooks: false },
55 ])(
56 '[Given] `composio $args` [Then] records the command as $outcome, with hooks: $hooks',
57 ({ args, exitCode, outcome, hooks }) =>
58 Effect.gen(function* () {
59 // The suite shares one recorder.
60 yield* resetInvocationCalls;
61
62 const result = yield* cliExit(args);
63
64 expect(result.exitCode).toBe(exitCode);
65 expect(yield* recordedSequence).toEqual([
66 ...(hooks
67 ? ['BackendMismatchWarning', 'UpdateNotice', 'PluginHint', 'BackgroundUpdateCheck']
68 : []),
69 'CommandLifecycleTelemetry.invoked',
70 `CommandLifecycleTelemetry.${outcome}`,
71 ]);
72 })
73 );
74 });
75
76 // Each of these used to print its message only to a terminal and exit 0, so a script, CI or
77 // an agent saw nothing and a successful exit; `connections remove` used to answer its own
78 // confirmation prompt with "no".
79 it.effect.each([
80 {
81 name: 'whoami without a login',
82 args: ['whoami'],
83 input: {},
84 message: 'You are not logged in yet. Please run `composio login`.',
85 },
86 {
87 name: 'tools list without a login',
88 args: ['tools', 'list', 'gmail'],
89 input: {},
90 message: 'You are not logged in yet. Please run `composio login`.',
91 },
92 {
93 name: 'connections list without a current org',
94 args: ['connections', 'list'],
95 input: loggedInWithoutOrg,
96 message: 'No current org configured. Run `composio login` or `composio orgs switch`.',
97 },
98 {
99 name: 'dev connected-accounts info without an id',
100 args: ['dev', 'connected-accounts', 'info'],
101 input: loggedInWithoutOrg,
102 message: 'Missing required argument: <id>',
103 },
104 {
105 name: 'triggers info without a slug',
106 args: ['triggers', 'info'],
107 input: loggedInWithoutOrg,
108 message: 'Missing required argument: <slug>',
109 },
110 {
111 name: 'connections remove without --yes',
112 args: ['connections', 'remove', 'gmail'],
113 input: { fixture: 'user-config-with-global-context' },
114 message: '`composio connections remove` needs --yes when not running interactively.',
115 },
116 {
117 name: 'dev projects list without a current org',
118 args: ['dev', 'projects', 'list'],
119 input: loggedInWithoutOrg,
120 message: 'No current org is configured.',
121 },
122 {
123 name: 'a bare run',
124 args: ['run'],
125 input: {},
126 message: 'Provide inline code or use --file to run a script file.',
127 },
128 {
129 name: 'execute with arguments that are not an object',
130 args: ['execute', 'GITHUB_GET_THE_AUTHENTICATED_USER', '-d', '[]'],
131 input: { fixture: 'user-config-with-global-context' },
132 message: 'Expected a JSON object for tool arguments',
133 },
134 ])(
135 '[Given] $name [Then] prints the message alone on stderr and exits 1',
136 ({ args, input, message }) =>
137 Effect.gen(function* () {
138 const { exit, exitCode, stdout, stderr } = yield* invoke(args, input);
139
140 expect(exitCode).toBe(1);
141 // The command's own error, not one the invocation made up.
142 expect(Exit.isFailure(exit) && Cause.hasFails(exit.cause)).toBe(true);
143 expect(stdout).toEqual([]);
144 expect(stderr.at(-1)).toContain(message);
145 const output = stderr.join('\n');
146 expect(output).not.toContain('Error');
147 expect(output).not.toContain('Sources');
148 expect(output).not.toContain('RUN_LOG_FILE=');
149 })
150 );
151
152 it.effect('[Given] a defect [Then] prints the defect report once and exits 1', () =>
153 Effect.gen(function* () {
154 const { exit, exitCode, stderr } = yield* invoke(
155 ['dev', 'toolkits', 'list'],
156 { fixture: 'user-config-with-global-context' },
157 // Every repository method dies.
158 Layer.succeed(ComposioToolkitsRepository, makeToolkitsRepositoryStub({}))
159 );
160
161 expect(exitCode).toBe(1);
162 expect(Exit.isFailure(exit) && Cause.hasDies(exit.cause)).toBe(true);
163 expect(stderr.filter(line => line.includes('💥'))).toHaveLength(1);
164 })
165 );
166 });
167
168 class UsageMistake extends Data.TaggedError('test/UsageMistake')<{ readonly message: string }> {
169 get [ExpectedFailure]() {
170 return true;
171 }
172 }
173
174 class PrintedWithCode extends Data.TaggedError('test/PrintedWithCode')<{}> {
175 get [Runtime.errorReported]() {
176 return false;
177 }
178
179 get [Runtime.errorExitCode]() {
180 return 7;
181 }
182 }
183
184 describe('runnerCause', () => {
185 it('[Given] a failure the invocation printed [Then] the runner gets it already reported', () => {
186 const cause = runnerCause(Cause.fail(new UsageMistake({ message: 'no' })));
187
188 const error = Cause.squash(cause);
189 expect(error).toBeInstanceOf(ReportedFailure);
190 expect(Runtime.getErrorReported(error)).toBe(false);
191 expect(exitCodeOf(Exit.fail(new UsageMistake({ message: 'no' })))).toBe(1);
192 });
193
194 it('[Given] a defect [Then] the runner gets it already reported', () => {
195 expect(Cause.squash(runnerCause(Cause.die(new Error('boom'))))).toBeInstanceOf(ReportedFailure);
196 });
197
198 it('[Given] a failure already on screen [Then] it keeps its markers and exit code', () => {
199 const cause = Cause.fail(new PrintedWithCode());
200
201 expect(runnerCause(cause)).toBe(cause);
202 expect(exitCodeOf(Exit.failCause(cause))).toBe(7);
203 });
204
205 it('[Given] an interruption [Then] it stays one, so a signal can still decide the code', () => {
206 const cause = Cause.interrupt();
207
208 expect(runnerCause(cause)).toBe(cause);
209 expect(exitCodeOf(Exit.failCause(cause))).toBe(130);
210 });
211 });