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
| 1 | import { assert, describe, expect, layer } from '@effect/vitest'; |
| 2 | import { ConfigProvider, Effect, Layer, Option } from 'effect'; |
| 3 | import type { TriggerInstanceUpsertParams } from '@composio/client/resources/trigger-instances'; |
| 4 | import { isUsageFailure } from 'src/commands/errors'; |
| 5 | import { projectKeysFromJSON } from 'src/models/project-keys'; |
| 6 | import { ComposioToolkitsRepository } from 'src/services/composio-clients'; |
| 7 | import { ProjectContext } from 'src/services/project-context'; |
| 8 | import { ComposioUserContext } from 'src/services/user-context'; |
| 9 | import { cli, MockConsole, TestLive } from 'test/__utils__'; |
| 10 | import { notFoundError } from 'test/__utils__/layers/not-found'; |
| 11 | import { exitCodeOf } from 'test/__utils__/exit-code'; |
| 12 | import { makeToolkitsRepositoryStub } from 'test/__utils__/services/toolkits-repository-stub'; |
| 13 | import { |
| 14 | DIRECTORY_PROJECT_SCOPE, |
| 15 | DEFAULT_PROJECT_SCOPE, |
| 16 | defaultDeveloperProjectAccountData, |
| 17 | inDeveloperProjectDirectory, |
| 18 | loginWithoutDeveloperProject, |
| 19 | OTHER_DEVELOPER_PROJECT, |
| 20 | OTHER_PROJECT_SCOPE, |
| 21 | runRecordingRepositoryScopes, |
| 22 | } from 'test/__utils__/layers/developer-project'; |
| 23 | |
| 24 | const testConfigProvider = ConfigProvider.fromEnv({ |
| 25 | env: { COMPOSIO_USER_API_KEY: 'test_api_key' }, |
| 26 | }); |
| 27 | |
| 28 | const devConfigWithoutProject = { |
| 29 | baseConfigProvider: testConfigProvider, |
| 30 | cliUserConfig: { developerDangerousCommandsEnabled: true }, |
| 31 | } as const; |
| 32 | |
| 33 | const dangerousDevConfig = { ...inDeveloperProjectDirectory, ...devConfigWithoutProject } as const; |
| 34 | |
| 35 | const createArgs = [ |
| 36 | 'triggers', |
| 37 | 'create', |
| 38 | 'GMAIL_NEW_GMAIL_MESSAGE', |
| 39 | '--connected-account-id', |
| 40 | 'con_123', |
| 41 | '--trigger-config', |
| 42 | '{"label":"inbox"}', |
| 43 | ]; |
| 44 | |
| 45 | const userCases = [ |
| 46 | { |
| 47 | name: '--user-id before the subcommand overrides both defaults', |
| 48 | args: ['dev', '--user-id', 'alice', ...createArgs], |
| 49 | projectUserId: 'project-user', |
| 50 | globalUserId: 'global-user', |
| 51 | expectedUserId: 'alice', |
| 52 | expectedLog: undefined, |
| 53 | }, |
| 54 | { |
| 55 | name: '--user-id after the subcommand overrides both defaults', |
| 56 | args: ['dev', ...createArgs, '--user-id', 'alice'], |
| 57 | projectUserId: 'project-user', |
| 58 | globalUserId: 'global-user', |
| 59 | expectedUserId: 'alice', |
| 60 | expectedLog: undefined, |
| 61 | }, |
| 62 | { |
| 63 | name: 'the project test user takes precedence over the global one', |
| 64 | args: ['dev', ...createArgs], |
| 65 | projectUserId: 'project-user', |
| 66 | globalUserId: 'global-user', |
| 67 | expectedUserId: 'project-user', |
| 68 | expectedLog: 'Using test user id "project-user"', |
| 69 | }, |
| 70 | { |
| 71 | name: 'the global test user is used without a project test user', |
| 72 | args: ['dev', ...createArgs], |
| 73 | projectUserId: undefined, |
| 74 | globalUserId: 'global-user', |
| 75 | expectedUserId: 'global-user', |
| 76 | expectedLog: 'Using global test user id "global-user"', |
| 77 | }, |
| 78 | { |
| 79 | name: 'a connection can still be used without a configured test user', |
| 80 | args: ['dev', ...createArgs], |
| 81 | projectUserId: undefined, |
| 82 | globalUserId: undefined, |
| 83 | expectedUserId: undefined, |
| 84 | expectedLog: undefined, |
| 85 | }, |
| 86 | ]; |
| 87 | |
| 88 | describe('CLI: composio dev triggers create', () => { |
| 89 | layer(TestLive(dangerousDevConfig))( |
| 90 | '[Given] a user id source [Then] sends the selected user id', |
| 91 | it => { |
| 92 | it.effect.each(userCases)( |
| 93 | '$name', |
| 94 | ({ args, projectUserId, globalUserId, expectedUserId, expectedLog }) => |
| 95 | Effect.gen(function* () { |
| 96 | const previousLines = (yield* MockConsole.getLines()).length; |
| 97 | const userContext = yield* ComposioUserContext; |
| 98 | yield* userContext.login('test_api_key', 'org_test', globalUserId); |
| 99 | const project = yield* projectKeysFromJSON( |
| 100 | JSON.stringify({ |
| 101 | org_id: 'org_test', |
| 102 | project_id: 'proj_test', |
| 103 | test_user_id: projectUserId, |
| 104 | }) |
| 105 | ); |
| 106 | const calls: Array<{ slug: string; params?: TriggerInstanceUpsertParams }> = []; |
| 107 | const repository = Layer.succeed( |
| 108 | ComposioToolkitsRepository, |
| 109 | makeToolkitsRepositoryStub({ |
| 110 | createTrigger: (_scope, slug, params) => |
| 111 | Effect.sync(() => { |
| 112 | calls.push({ slug, params }); |
| 113 | return { |
| 114 | trigger_id: 'trg_new', |
| 115 | type: 'webhook' as const, |
| 116 | fallback_reason: null, |
| 117 | polling_interval: null, |
| 118 | }; |
| 119 | }), |
| 120 | }) |
| 121 | ); |
| 122 | const projectContext = Layer.succeed( |
| 123 | ProjectContext, |
| 124 | ProjectContext.of({ resolve: Effect.succeed(Option.some(project)) }) |
| 125 | ); |
| 126 | |
| 127 | yield* cli(args).pipe(Effect.provide(Layer.merge(repository, projectContext))); |
| 128 | |
| 129 | expect(calls).toEqual([ |
| 130 | { |
| 131 | slug: 'GMAIL_NEW_GMAIL_MESSAGE', |
| 132 | params: { |
| 133 | connected_account_id: 'con_123', |
| 134 | trigger_config: { label: 'inbox' }, |
| 135 | user_id: expectedUserId, |
| 136 | }, |
| 137 | }, |
| 138 | ]); |
| 139 | const logs = (yield* MockConsole.getLines({ stripAnsi: true })) |
| 140 | .slice(previousLines) |
| 141 | .filter(line => line.includes('test user id')); |
| 142 | expect(logs).toEqual( |
| 143 | expectedLog === undefined ? [] : [expect.stringContaining(expectedLog)] |
| 144 | ); |
| 145 | }) |
| 146 | ); |
| 147 | } |
| 148 | ); |
| 149 | |
| 150 | layer(TestLive(dangerousDevConfig))( |
| 151 | '[Given] create with valid args [Then] creates trigger', |
| 152 | it => { |
| 153 | it.effect('creates trigger and prints id', () => |
| 154 | Effect.gen(function* () { |
| 155 | yield* cli([ |
| 156 | 'dev', |
| 157 | 'triggers', |
| 158 | 'create', |
| 159 | 'GMAIL_NEW_GMAIL_MESSAGE', |
| 160 | '--connected-account-id', |
| 161 | 'con_123', |
| 162 | ]); |
| 163 | const lines = yield* MockConsole.getLines({ stripAnsi: true }); |
| 164 | const output = lines.join('\n'); |
| 165 | |
| 166 | expect(output).toContain('Trigger created'); |
| 167 | expect(output).toContain('trg_gmail_new_gmail_message_con_123'); |
| 168 | expect(lines.at(-1)).toBe( |
| 169 | JSON.stringify( |
| 170 | { |
| 171 | trigger_id: 'trg_gmail_new_gmail_message_con_123', |
| 172 | type: 'webhook', |
| 173 | fallback_reason: null, |
| 174 | polling_interval: null, |
| 175 | }, |
| 176 | null, |
| 177 | 2 |
| 178 | ) |
| 179 | ); |
| 180 | }) |
| 181 | ); |
| 182 | } |
| 183 | ); |
| 184 | |
| 185 | layer(TestLive(dangerousDevConfig))( |
| 186 | '[Given] create with a JSON object config [Then] sends it as the trigger config', |
| 187 | it => { |
| 188 | it.effect('passes the parsed config and connected account to the API', () => |
| 189 | Effect.gen(function* () { |
| 190 | const calls: Array<{ slug: string; params?: TriggerInstanceUpsertParams }> = []; |
| 191 | const repository = Layer.succeed( |
| 192 | ComposioToolkitsRepository, |
| 193 | makeToolkitsRepositoryStub({ |
| 194 | createTrigger: (_scope, slug, params) => |
| 195 | Effect.sync(() => { |
| 196 | calls.push({ slug, params }); |
| 197 | return { |
| 198 | trigger_id: 'trg_new', |
| 199 | type: 'webhook' as const, |
| 200 | fallback_reason: null, |
| 201 | polling_interval: null, |
| 202 | }; |
| 203 | }), |
| 204 | }) |
| 205 | ); |
| 206 | yield* cli([ |
| 207 | 'dev', |
| 208 | 'triggers', |
| 209 | 'create', |
| 210 | 'GMAIL_NEW_GMAIL_MESSAGE', |
| 211 | '--connected-account-id', |
| 212 | 'con_123', |
| 213 | '--trigger-config', |
| 214 | '{"labelIds":"INBOX"}', |
| 215 | ]).pipe(Effect.provide(repository)); |
| 216 | |
| 217 | expect(calls).toEqual([ |
| 218 | { |
| 219 | slug: 'GMAIL_NEW_GMAIL_MESSAGE', |
| 220 | params: { |
| 221 | connected_account_id: 'con_123', |
| 222 | trigger_config: { labelIds: 'INBOX' }, |
| 223 | user_id: 'global-default', |
| 224 | }, |
| 225 | }, |
| 226 | ]); |
| 227 | }) |
| 228 | ); |
| 229 | } |
| 230 | ); |
| 231 | |
| 232 | layer(TestLive(dangerousDevConfig))( |
| 233 | '[Given] invalid input [Then] fails with a usage error before any request', |
| 234 | it => { |
| 235 | it.effect.each([ |
| 236 | { |
| 237 | name: 'no trigger name', |
| 238 | args: [], |
| 239 | option: '<trigger-name>', |
| 240 | message: /^Missing required argument: <trigger-name>\nTry specifying a trigger slug/, |
| 241 | }, |
| 242 | { |
| 243 | name: 'invalid JSON in --trigger-config', |
| 244 | args: ['GMAIL_NEW_GMAIL_MESSAGE', '--trigger-config', '{'], |
| 245 | option: '--trigger-config', |
| 246 | message: /^Invalid JSON in --trigger-config\. Please provide valid JSON\.\nExample:\n/, |
| 247 | }, |
| 248 | { |
| 249 | name: 'a JSON array in --trigger-config', |
| 250 | args: ['GMAIL_NEW_GMAIL_MESSAGE', '--trigger-config', '[1,2,3]'], |
| 251 | option: '--trigger-config', |
| 252 | message: /^--trigger-config must be a JSON object/, |
| 253 | }, |
| 254 | { |
| 255 | name: 'a JSON number in --trigger-config', |
| 256 | args: ['GMAIL_NEW_GMAIL_MESSAGE', '--trigger-config', '42'], |
| 257 | option: '--trigger-config', |
| 258 | message: /^--trigger-config must be a JSON object/, |
| 259 | }, |
| 260 | ])('[Given] $name [Then] raises a CommandInputError', ({ args, option, message }) => |
| 261 | Effect.gen(function* () { |
| 262 | // Every repository method dies, so reaching the API fails the test. |
| 263 | const repository = Layer.succeed( |
| 264 | ComposioToolkitsRepository, |
| 265 | makeToolkitsRepositoryStub({}) |
| 266 | ); |
| 267 | const failure = yield* cli(['dev', 'triggers', 'create', ...args]).pipe( |
| 268 | Effect.provide(repository), |
| 269 | Effect.flip |
| 270 | ); |
| 271 | |
| 272 | expect(failure).toMatchObject({ |
| 273 | _tag: 'commands/CommandInputError', |
| 274 | command: 'dev triggers create', |
| 275 | option, |
| 276 | message: expect.stringMatching(message), |
| 277 | }); |
| 278 | // The invocation prints the message on stderr, and nothing else. The suite shares one |
| 279 | // console, so this case's line is the last one. |
| 280 | assert(isUsageFailure(failure)); |
| 281 | expect((yield* MockConsole.getLines()).at(-1)).toBe(failure.message); |
| 282 | }) |
| 283 | ); |
| 284 | } |
| 285 | ); |
| 286 | |
| 287 | layer(TestLive(dangerousDevConfig))( |
| 288 | '[Given] the API rejects the create [Then] reports the failure and exits 1', |
| 289 | it => { |
| 290 | it.effect('prints the fallback message and the list hint', () => |
| 291 | Effect.gen(function* () { |
| 292 | const repository = Layer.succeed( |
| 293 | ComposioToolkitsRepository, |
| 294 | makeToolkitsRepositoryStub({ |
| 295 | createTrigger: () => Effect.fail(notFoundError('Trigger type not found')), |
| 296 | }) |
| 297 | ); |
| 298 | const exit = yield* Effect.exit( |
| 299 | cli(['dev', 'triggers', 'create', 'UNKNOWN_TRIGGER']).pipe(Effect.provide(repository)) |
| 300 | ); |
| 301 | const output = (yield* MockConsole.getLines({ stripAnsi: true })).join('\n'); |
| 302 | |
| 303 | expect(output).toContain('Failed to create trigger "UNKNOWN_TRIGGER".'); |
| 304 | expect(output).toContain('composio dev triggers list'); |
| 305 | expect(output).not.toContain('Trigger created'); |
| 306 | expect(exitCodeOf(exit)).toBe(1); |
| 307 | }) |
| 308 | ); |
| 309 | } |
| 310 | ); |
| 311 | }); |
| 312 | |
| 313 | // The request goes to the developer project the command resolved, never to the one the backend |
| 314 | // would pick for the key. |
| 315 | describe('CLI: composio dev triggers create acts on the developer project', () => { |
| 316 | layer(TestLive(dangerousDevConfig))(it => { |
| 317 | it.effect.each([ |
| 318 | { given: 'a directory set up for a project', args: [], scope: DIRECTORY_PROJECT_SCOPE }, |
| 319 | { |
| 320 | given: '--project-name', |
| 321 | args: ['--project-name', OTHER_DEVELOPER_PROJECT.name], |
| 322 | scope: OTHER_PROJECT_SCOPE, |
| 323 | }, |
| 324 | ])('[Given] $given [Then] creates the trigger in that project', ({ args, scope }) => |
| 325 | Effect.gen(function* () { |
| 326 | const { exitCode, calls } = yield* runRecordingRepositoryScopes([ |
| 327 | ...[ |
| 328 | 'dev', |
| 329 | 'triggers', |
| 330 | 'create', |
| 331 | 'GMAIL_NEW_GMAIL_MESSAGE', |
| 332 | '--connected-account-id', |
| 333 | 'con_123', |
| 334 | ], |
| 335 | ...args, |
| 336 | ]); |
| 337 | |
| 338 | expect(exitCode).toBe(0); |
| 339 | expect(calls).toEqual([{ method: 'createTrigger', scope: expect.objectContaining(scope) }]); |
| 340 | }) |
| 341 | ); |
| 342 | }); |
| 343 | |
| 344 | layer( |
| 345 | TestLive({ ...devConfigWithoutProject, accountData: defaultDeveloperProjectAccountData() }) |
| 346 | )(it => { |
| 347 | it.effect( |
| 348 | '[Given] a directory set up for no project [Then] acts on the default developer project', |
| 349 | () => |
| 350 | Effect.gen(function* () { |
| 351 | yield* loginWithoutDeveloperProject; |
| 352 | const { exitCode, calls } = yield* runRecordingRepositoryScopes([ |
| 353 | 'dev', |
| 354 | 'triggers', |
| 355 | 'create', |
| 356 | 'GMAIL_NEW_GMAIL_MESSAGE', |
| 357 | '--connected-account-id', |
| 358 | 'con_123', |
| 359 | ]); |
| 360 | |
| 361 | expect(exitCode).toBe(0); |
| 362 | expect(calls).toEqual([ |
| 363 | { method: 'createTrigger', scope: expect.objectContaining(DEFAULT_PROJECT_SCOPE) }, |
| 364 | ]); |
| 365 | }) |
| 366 | ); |
| 367 | }); |
| 368 | }); |