@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 184 lines 6.63 KB
Raw
1 import { assert, describe, expect, layer } from '@effect/vitest';
2 import { isUsageFailure } from 'src/commands/errors';
3 import { ConfigProvider, Effect, Layer } from 'effect';
4 import { ComposioToolkitsRepository } from 'src/services/composio-clients';
5 import { cli, MockConsole, TestLive } from 'test/__utils__';
6 import { notFoundError } from 'test/__utils__/layers/not-found';
7 import { exitCodeOf } from 'test/__utils__/exit-code';
8 import { makeToolkitsRepositoryStub } from 'test/__utils__/services/toolkits-repository-stub';
9 import {
10 DIRECTORY_PROJECT_SCOPE,
11 DEFAULT_PROJECT_SCOPE,
12 defaultDeveloperProjectAccountData,
13 inDeveloperProjectDirectory,
14 loginWithoutDeveloperProject,
15 OTHER_DEVELOPER_PROJECT,
16 OTHER_PROJECT_SCOPE,
17 runRecordingRepositoryScopes,
18 } from 'test/__utils__/layers/developer-project';
19
20 const testConfigProvider = ConfigProvider.fromEnv({
21 env: { COMPOSIO_USER_API_KEY: 'test_api_key' },
22 });
23
24 const devConfigWithoutProject = {
25 baseConfigProvider: testConfigProvider,
26 cliUserConfig: { developerDangerousCommandsEnabled: true },
27 } as const;
28
29 const dangerousDevConfig = { ...inDeveloperProjectDirectory, ...devConfigWithoutProject } as const;
30
31 describe('CLI: composio dev triggers disable', () => {
32 layer(TestLive(dangerousDevConfig))('[Given] disable with ID [Then] disables trigger', it => {
33 it.effect('disables trigger successfully', () =>
34 Effect.gen(function* () {
35 yield* cli(['dev', 'triggers', 'disable', 'trg_123', '--dangerously-allow']);
36 const lines = yield* MockConsole.getLines({ stripAnsi: true });
37 const output = lines.join('\n');
38 expect(output).toContain('disabled');
39 })
40 );
41 });
42
43 layer(TestLive(dangerousDevConfig))(
44 '[Given] missing ID for disable [Then] fails with a usage error',
45 it => {
46 it.effect('raises a CommandInputError naming <id>', () =>
47 Effect.gen(function* () {
48 const failure = yield* cli(['dev', 'triggers', 'disable', '--dangerously-allow']).pipe(
49 Effect.flip
50 );
51
52 expect(failure).toMatchObject({
53 _tag: 'commands/CommandInputError',
54 command: 'dev triggers disable',
55 option: '<id>',
56 message: expect.stringMatching(
57 /^Missing required argument: <id>\nTry specifying a trigger ID, e\.g\.:\n> composio dev triggers disable "trg_123"/
58 ),
59 });
60 // The invocation prints the message on stderr, and nothing else.
61 assert(isUsageFailure(failure));
62 expect(yield* MockConsole.getLines()).toEqual([failure.message]);
63 })
64 );
65 }
66 );
67
68 layer(TestLive(dangerousDevConfig))(
69 '[Given] disable without --dangerously-allow [Then] fails on that flag',
70 it => {
71 it.effect('raises a CommandInputError naming --dangerously-allow', () =>
72 Effect.gen(function* () {
73 const failure = yield* cli(['dev', 'triggers', 'disable', 'trg_123']).pipe(Effect.flip);
74
75 expect(failure).toMatchObject({
76 _tag: 'commands/CommandInputError',
77 option: '--dangerously-allow',
78 message: expect.stringContaining('requires explicit acknowledgement'),
79 });
80 })
81 );
82 }
83 );
84
85 layer(TestLive({ ...inDeveloperProjectDirectory, baseConfigProvider: testConfigProvider }))(
86 '[Given] destructive dev commands disabled by config [Then] fails before the flag check',
87 it => {
88 it.effect('raises a CommandInputError naming the config setting', () =>
89 Effect.gen(function* () {
90 const failure = yield* cli([
91 'dev',
92 'triggers',
93 'disable',
94 'trg_123',
95 '--dangerously-allow',
96 ]).pipe(Effect.flip);
97
98 expect(failure).toMatchObject({
99 _tag: 'commands/CommandInputError',
100 message: expect.stringContaining('developer.destructive_actions'),
101 });
102 })
103 );
104 }
105 );
106
107 layer(TestLive(dangerousDevConfig))(
108 '[Given] the API rejects the disable [Then] reports the failure and exits 1',
109 it => {
110 it.effect('prints the fallback message and the status hint', () =>
111 Effect.gen(function* () {
112 const repository = Layer.succeed(
113 ComposioToolkitsRepository,
114 makeToolkitsRepositoryStub({
115 disableTrigger: () => Effect.fail(notFoundError('Trigger not found')),
116 })
117 );
118 const exit = yield* Effect.exit(
119 cli(['dev', 'triggers', 'disable', 'trg_missing', '--dangerously-allow']).pipe(
120 Effect.provide(repository)
121 )
122 );
123 const output = (yield* MockConsole.getLines({ stripAnsi: true })).join('\n');
124
125 expect(output).toContain('Failed to disable trigger "trg_missing".');
126 expect(output).toContain('composio dev triggers status');
127 expect(output).not.toContain('Trigger "trg_missing" disabled.');
128 expect(exitCodeOf(exit)).toBe(1);
129 })
130 );
131 }
132 );
133 });
134
135 // The request goes to the developer project the command resolved, never to the one the backend
136 // would pick for the key.
137 describe('CLI: composio dev triggers disable acts on the developer project', () => {
138 layer(TestLive(dangerousDevConfig))(it => {
139 it.effect.each([
140 { given: 'a directory set up for a project', args: [], scope: DIRECTORY_PROJECT_SCOPE },
141 {
142 given: '--project-name',
143 args: ['--project-name', OTHER_DEVELOPER_PROJECT.name],
144 scope: OTHER_PROJECT_SCOPE,
145 },
146 ])('[Given] $given [Then] disables the trigger in that project', ({ args, scope }) =>
147 Effect.gen(function* () {
148 const { exitCode, calls } = yield* runRecordingRepositoryScopes([
149 ...['dev', 'triggers', 'disable', 'trg_123', '--dangerously-allow'],
150 ...args,
151 ]);
152
153 expect(exitCode).toBe(0);
154 expect(calls).toEqual([
155 { method: 'disableTrigger', scope: expect.objectContaining(scope) },
156 ]);
157 })
158 );
159 });
160
161 layer(
162 TestLive({ ...devConfigWithoutProject, accountData: defaultDeveloperProjectAccountData() })
163 )(it => {
164 it.effect(
165 '[Given] a directory set up for no project [Then] acts on the default developer project',
166 () =>
167 Effect.gen(function* () {
168 yield* loginWithoutDeveloperProject;
169 const { exitCode, calls } = yield* runRecordingRepositoryScopes([
170 'dev',
171 'triggers',
172 'disable',
173 'trg_123',
174 '--dangerously-allow',
175 ]);
176
177 expect(exitCode).toBe(0);
178 expect(calls).toEqual([
179 { method: 'disableTrigger', scope: expect.objectContaining(DEFAULT_PROJECT_SCOPE) },
180 ]);
181 })
182 );
183 });
184 });