Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { BadRequestError, type RequestOptions } from '@composio/client/effect'; |
| 2 | import { describe, expect, it, layer } from '@effect/vitest'; |
| 3 | import { ConfigProvider, Effect, Option } from 'effect'; |
| 4 | import { afterEach, vi } from 'vitest'; |
| 5 | import { PassThrough } from 'node:stream'; |
| 6 | import type { |
| 7 | SessionCreateParams, |
| 8 | SessionProxyExecuteParams, |
| 9 | } from '@composio/client/resources/tool-router'; |
| 10 | import { ReportedProxyCommandError } from 'src/commands/proxy/proxy.errors'; |
| 11 | import { |
| 12 | normalizeProxyMethod, |
| 13 | parseProxyBody, |
| 14 | parseProxyHeader, |
| 15 | } from 'src/commands/proxy/proxy.handler'; |
| 16 | import * as consumerShortTermCache from 'src/services/consumer-short-term-cache'; |
| 17 | import * as toolPermissions from 'src/services/tool-permissions'; |
| 18 | import { cli, MockConsole, TestLive } from 'test/__utils__'; |
| 19 | import { redirectedTerminalUI } from 'test/__utils__/services/redirected-terminal-ui'; |
| 20 | |
| 21 | const testConfigProvider = ConfigProvider.fromEnv({ |
| 22 | env: { COMPOSIO_USER_API_KEY: 'test_api_key' }, |
| 23 | }); |
| 24 | |
| 25 | const noActiveConnection = { |
| 26 | code: 4302, |
| 27 | slug: 'ToolRouterV2_NoActiveConnection', |
| 28 | message: 'No active connection', |
| 29 | status: 400, |
| 30 | }; |
| 31 | |
| 32 | describe('CLI: composio proxy', () => { |
| 33 | afterEach(() => { |
| 34 | vi.restoreAllMocks(); |
| 35 | }); |
| 36 | |
| 37 | it('parses curl-style header values', () => { |
| 38 | expect(parseProxyHeader('content-type: application/json')).toEqual({ |
| 39 | name: 'content-type', |
| 40 | value: 'application/json', |
| 41 | }); |
| 42 | }); |
| 43 | |
| 44 | it('normalizes HTTP methods', () => { |
| 45 | expect(normalizeProxyMethod('post')).toBe('POST'); |
| 46 | }); |
| 47 | |
| 48 | it('parses JSON-like request bodies when possible', () => { |
| 49 | expect(parseProxyBody('{ foo: "bar" }')).toEqual({ foo: 'bar' }); |
| 50 | expect(parseProxyBody('plain text')).toBe('plain text'); |
| 51 | }); |
| 52 | |
| 53 | describe('[Given] stdin is a pipe', () => { |
| 54 | /** Runs `composio proxy` over `stdin` and returns the body it sent to proxy_execute. */ |
| 55 | const proxyBodyOver = (stdin: PassThrough, args: ReadonlyArray<string>) => |
| 56 | Effect.gen(function* () { |
| 57 | let sentParams: SessionProxyExecuteParams | undefined; |
| 58 | const live = TestLive({ |
| 59 | baseConfigProvider: testConfigProvider, |
| 60 | fixture: 'global-test-user-id', |
| 61 | stdin: { isTTY: false, data: '', stream: stdin }, |
| 62 | toolRouter: { |
| 63 | proxyExecute: (_sessionId: string, params: SessionProxyExecuteParams) => |
| 64 | Effect.sync(() => { |
| 65 | sentParams = params; |
| 66 | return { |
| 67 | result_type: 'completed' as const, |
| 68 | status: 200, |
| 69 | data: { ok: true }, |
| 70 | headers: {}, |
| 71 | }; |
| 72 | }), |
| 73 | }, |
| 74 | }); |
| 75 | const output = yield* Effect.gen(function* () { |
| 76 | yield* cli([ |
| 77 | 'proxy', |
| 78 | 'https://gmail.googleapis.com/gmail/v1/users/me/profile', |
| 79 | '--toolkit', |
| 80 | 'gmail', |
| 81 | ...args, |
| 82 | ]); |
| 83 | const lines = yield* MockConsole.getLines({ stripAnsi: true }); |
| 84 | return lines.join('\n'); |
| 85 | }).pipe(Effect.provide(live)); |
| 86 | expect(sentParams).toBeDefined(); |
| 87 | return { body: sentParams?.body, output }; |
| 88 | }); |
| 89 | |
| 90 | it.live( |
| 91 | '[When] it is held open and no -d is passed [Then] the request is sent without reading it', |
| 92 | () => |
| 93 | Effect.gen(function* () { |
| 94 | const heldOpen = new PassThrough(); |
| 95 | heldOpen.write('{"never":"read"}'); |
| 96 | |
| 97 | const { body, output } = yield* proxyBodyOver(heldOpen, []); |
| 98 | |
| 99 | expect(body).toBeUndefined(); |
| 100 | expect(heldOpen.readableLength).toBeGreaterThan(0); |
| 101 | expect(output).not.toContain('Sending the request without a body'); |
| 102 | }) |
| 103 | ); |
| 104 | |
| 105 | // A bodiless GET is the read agents run with a held-open pipe, so only the other methods warn. |
| 106 | it.live.each(['POST', 'PUT', 'PATCH', 'DELETE'])( |
| 107 | '[When] a %s is sent without -d [Then] it warns that piped input is not the body, without reading it', |
| 108 | method => |
| 109 | Effect.gen(function* () { |
| 110 | const piped = new PassThrough(); |
| 111 | piped.write('{"meant":"as body"}'); |
| 112 | |
| 113 | const { body, output } = yield* proxyBodyOver(piped, ['-X', method]); |
| 114 | |
| 115 | expect(body).toBeUndefined(); |
| 116 | expect(piped.readableLength).toBeGreaterThan(0); |
| 117 | expect(output).toContain( |
| 118 | 'Sending the request without a body: composio proxy reads stdin only with -d -. Pass -d - to send piped input as the body.' |
| 119 | ); |
| 120 | }) |
| 121 | ); |
| 122 | |
| 123 | it.live('[When] -d - is passed [Then] the body is read from stdin', () => |
| 124 | Effect.gen(function* () { |
| 125 | const piped = new PassThrough(); |
| 126 | setTimeout(() => piped.end('{"message":{"raw":"abc"}}'), 1500); |
| 127 | |
| 128 | const { body, output } = yield* proxyBodyOver(piped, ['-X', 'POST', '-d', '-']); |
| 129 | |
| 130 | expect(body).toEqual({ message: { raw: 'abc' } }); |
| 131 | expect(output).not.toContain('Sending the request without a body'); |
| 132 | }) |
| 133 | ); |
| 134 | |
| 135 | it.live('[When] -d @- is passed [Then] the body is read from stdin', () => |
| 136 | Effect.gen(function* () { |
| 137 | const piped = new PassThrough(); |
| 138 | piped.end('plain text'); |
| 139 | |
| 140 | const { body } = yield* proxyBodyOver(piped, ['-X', 'POST', '-d', '@-']); |
| 141 | |
| 142 | expect(body).toBe('plain text'); |
| 143 | }) |
| 144 | ); |
| 145 | }); |
| 146 | |
| 147 | layer(TestLive({ baseConfigProvider: testConfigProvider, fixture: 'global-test-user-id' }))( |
| 148 | '[Given] curl-like proxy flags [Then] it creates a scoped session and forwards proxy_execute', |
| 149 | it => { |
| 150 | it.effect('forwards proxy execute params and prints the response', () => |
| 151 | Effect.gen(function* () { |
| 152 | let createParams: SessionCreateParams | undefined; |
| 153 | let proxyOptions: RequestOptions | undefined; |
| 154 | let proxyParams: |
| 155 | | { |
| 156 | sessionId: string; |
| 157 | params: SessionProxyExecuteParams; |
| 158 | } |
| 159 | | undefined; |
| 160 | |
| 161 | const live = TestLive({ |
| 162 | baseConfigProvider: testConfigProvider, |
| 163 | fixture: 'global-test-user-id', |
| 164 | cliUserConfig: { experimentalFeatures: { multi_account: false } }, |
| 165 | connectedAccountsData: { |
| 166 | items: [ |
| 167 | { |
| 168 | id: 'con_gmail_work', |
| 169 | alias: 'work', |
| 170 | word_id: 'gmail-work', |
| 171 | status: 'ACTIVE', |
| 172 | status_reason: null, |
| 173 | is_disabled: false, |
| 174 | user_id: 'consumer-user-org_test', |
| 175 | toolkit: { slug: 'gmail' }, |
| 176 | auth_config: { |
| 177 | id: 'ac_gmail_oauth', |
| 178 | auth_scheme: 'OAUTH2', |
| 179 | is_composio_managed: true, |
| 180 | is_disabled: false, |
| 181 | }, |
| 182 | created_at: '2026-01-01T00:00:00.000Z', |
| 183 | updated_at: '2026-01-01T00:00:00.000Z', |
| 184 | test_request_endpoint: '', |
| 185 | }, |
| 186 | { |
| 187 | id: 'con_gmail_personal', |
| 188 | alias: 'personal', |
| 189 | word_id: 'gmail-personal', |
| 190 | status: 'ACTIVE', |
| 191 | status_reason: null, |
| 192 | is_disabled: false, |
| 193 | user_id: 'consumer-user-org_test', |
| 194 | toolkit: { slug: 'gmail' }, |
| 195 | auth_config: { |
| 196 | id: 'ac_gmail_oauth', |
| 197 | auth_scheme: 'OAUTH2', |
| 198 | is_composio_managed: true, |
| 199 | is_disabled: false, |
| 200 | }, |
| 201 | created_at: '2026-01-02T00:00:00.000Z', |
| 202 | updated_at: '2026-01-02T00:00:00.000Z', |
| 203 | test_request_endpoint: '', |
| 204 | }, |
| 205 | ], |
| 206 | }, |
| 207 | toolRouter: { |
| 208 | create: (params: SessionCreateParams) => |
| 209 | Effect.sync(() => { |
| 210 | createParams = params; |
| 211 | return { |
| 212 | session_id: 'trs_proxy_test', |
| 213 | config: { |
| 214 | user_id: params.user_id, |
| 215 | execute: {}, |
| 216 | search: {}, |
| 217 | preload: { tools: [] }, |
| 218 | instant: false, |
| 219 | }, |
| 220 | config_version: 1, |
| 221 | mcp: { type: 'http' as const, url: 'https://mcp.test.composio.dev' }, |
| 222 | tool_router_tools: [], |
| 223 | }; |
| 224 | }), |
| 225 | proxyExecute: ( |
| 226 | sessionId: string, |
| 227 | params: SessionProxyExecuteParams, |
| 228 | options?: RequestOptions |
| 229 | ) => |
| 230 | Effect.sync(() => { |
| 231 | proxyParams = { sessionId, params }; |
| 232 | proxyOptions = options; |
| 233 | return { |
| 234 | result_type: 'completed' as const, |
| 235 | status: 200, |
| 236 | data: { |
| 237 | ok: true, |
| 238 | endpoint: params.endpoint, |
| 239 | method: params.method, |
| 240 | }, |
| 241 | headers: { 'content-type': 'application/json' }, |
| 242 | }; |
| 243 | }), |
| 244 | }, |
| 245 | }); |
| 246 | |
| 247 | const output = yield* Effect.gen(function* () { |
| 248 | yield* cli([ |
| 249 | 'proxy', |
| 250 | 'https://gmail.googleapis.com/gmail/v1/users/me/drafts', |
| 251 | '--toolkit', |
| 252 | 'gmail', |
| 253 | '--account', |
| 254 | 'work', |
| 255 | '-X', |
| 256 | 'post', |
| 257 | '-H', |
| 258 | 'content-type: application/json', |
| 259 | '-d', |
| 260 | '{ "message": { "raw": "abc" } }', |
| 261 | ]); |
| 262 | const lines = yield* MockConsole.getLines({ stripAnsi: true }); |
| 263 | return lines.join('\n'); |
| 264 | }).pipe(Effect.provide(live)); |
| 265 | |
| 266 | expect(createParams).toEqual({ |
| 267 | user_id: 'consumer-user-org_test', |
| 268 | connected_accounts: { gmail: 'con_gmail_work' }, |
| 269 | manage_connections: { enable: false }, |
| 270 | toolkits: { enable: ['gmail'] }, |
| 271 | instant: { return_instant_charge: true }, |
| 272 | }); |
| 273 | expect(proxyParams).toEqual({ |
| 274 | sessionId: 'trs_proxy_test', |
| 275 | params: { |
| 276 | toolkit_slug: 'gmail', |
| 277 | endpoint: 'https://gmail.googleapis.com/gmail/v1/users/me/drafts', |
| 278 | method: 'POST', |
| 279 | body: { message: { raw: 'abc' } }, |
| 280 | parameters: [ |
| 281 | { |
| 282 | name: 'content-type', |
| 283 | type: 'header', |
| 284 | value: 'application/json', |
| 285 | }, |
| 286 | ], |
| 287 | }, |
| 288 | }); |
| 289 | // A proxied call does not ask for retries, so the client sends it |
| 290 | // once: a retry after the upstream API already acted would duplicate |
| 291 | // the side effect. |
| 292 | expect(proxyOptions).toBeUndefined(); |
| 293 | |
| 294 | expect(output).toContain('Status: 200'); |
| 295 | expect(output).toContain('"ok": true'); |
| 296 | }) |
| 297 | ); |
| 298 | } |
| 299 | ); |
| 300 | |
| 301 | layer(TestLive({ baseConfigProvider: testConfigProvider, fixture: 'global-test-user-id' }))( |
| 302 | '[Given] no --account and several connected accounts, none aliased default [Then] proxy fails instead of silently picking one', |
| 303 | it => { |
| 304 | it.effect('fails before creating a session', () => |
| 305 | Effect.gen(function* () { |
| 306 | let createCalled = false; |
| 307 | const live = TestLive({ |
| 308 | baseConfigProvider: testConfigProvider, |
| 309 | fixture: 'global-test-user-id', |
| 310 | cliUserConfig: { experimentalFeatures: { multi_account: false } }, |
| 311 | connectedAccountsData: { |
| 312 | items: [ |
| 313 | { |
| 314 | id: 'con_gmail_work', |
| 315 | alias: 'work', |
| 316 | word_id: 'gmail-work', |
| 317 | status: 'ACTIVE', |
| 318 | status_reason: null, |
| 319 | is_disabled: false, |
| 320 | user_id: 'consumer-user-org_test', |
| 321 | toolkit: { slug: 'gmail' }, |
| 322 | auth_config: { |
| 323 | id: 'ac_gmail_oauth', |
| 324 | auth_scheme: 'OAUTH2', |
| 325 | is_composio_managed: true, |
| 326 | is_disabled: false, |
| 327 | }, |
| 328 | created_at: '2026-01-01T00:00:00.000Z', |
| 329 | updated_at: '2026-01-01T00:00:00.000Z', |
| 330 | test_request_endpoint: '', |
| 331 | }, |
| 332 | { |
| 333 | id: 'con_gmail_personal', |
| 334 | alias: 'personal', |
| 335 | word_id: 'gmail-personal', |
| 336 | status: 'ACTIVE', |
| 337 | status_reason: null, |
| 338 | is_disabled: false, |
| 339 | user_id: 'consumer-user-org_test', |
| 340 | toolkit: { slug: 'gmail' }, |
| 341 | auth_config: { |
| 342 | id: 'ac_gmail_oauth', |
| 343 | auth_scheme: 'OAUTH2', |
| 344 | is_composio_managed: true, |
| 345 | is_disabled: false, |
| 346 | }, |
| 347 | created_at: '2026-01-02T00:00:00.000Z', |
| 348 | updated_at: '2026-01-02T00:00:00.000Z', |
| 349 | test_request_endpoint: '', |
| 350 | }, |
| 351 | ], |
| 352 | }, |
| 353 | toolRouter: { |
| 354 | create: (_params: SessionCreateParams) => |
| 355 | Effect.sync(() => { |
| 356 | createCalled = true; |
| 357 | return { |
| 358 | session_id: 'trs_proxy_test', |
| 359 | config: { |
| 360 | user_id: 'consumer-user-org_test', |
| 361 | execute: {}, |
| 362 | search: {}, |
| 363 | preload: { tools: [] }, |
| 364 | instant: false, |
| 365 | }, |
| 366 | config_version: 1, |
| 367 | mcp: { type: 'http' as const, url: 'https://mcp.test.composio.dev' }, |
| 368 | tool_router_tools: [], |
| 369 | }; |
| 370 | }), |
| 371 | }, |
| 372 | }); |
| 373 | |
| 374 | const failure = yield* Effect.gen(function* () { |
| 375 | return yield* cli([ |
| 376 | 'proxy', |
| 377 | 'https://gmail.googleapis.com/gmail/v1/users/me/drafts', |
| 378 | '--toolkit', |
| 379 | 'gmail', |
| 380 | '--skip-connection-check', |
| 381 | '-X', |
| 382 | 'post', |
| 383 | ]).pipe(Effect.flip); |
| 384 | }).pipe(Effect.provide(live)); |
| 385 | |
| 386 | expect(createCalled).toBe(false); |
| 387 | expect(String(failure)).toContain( |
| 388 | 'Multiple connected accounts exist for toolkit "gmail"' |
| 389 | ); |
| 390 | }) |
| 391 | ); |
| 392 | } |
| 393 | ); |
| 394 | |
| 395 | layer(TestLive({ baseConfigProvider: testConfigProvider, fixture: 'global-test-user-id' }))( |
| 396 | '[Given] cached missing toolkit [Then] proxy fails fast before session creation', |
| 397 | it => { |
| 398 | it.effect('uses the connected toolkit cache keyed by toolkit', () => |
| 399 | Effect.gen(function* () { |
| 400 | const refreshSpy = vi |
| 401 | .spyOn(consumerShortTermCache, 'refreshConsumerConnectedToolkitsCache') |
| 402 | .mockReturnValue(Effect.void); |
| 403 | const getFreshSpy = vi |
| 404 | .spyOn(consumerShortTermCache, 'getFreshConsumerConnectedToolkitsFromCache') |
| 405 | .mockReturnValue(Effect.succeed(Option.some(['slack']))); |
| 406 | |
| 407 | let createCalled = false; |
| 408 | const live = TestLive({ |
| 409 | baseConfigProvider: testConfigProvider, |
| 410 | fixture: 'global-test-user-id', |
| 411 | toolRouter: { |
| 412 | create: (_params: SessionCreateParams) => |
| 413 | Effect.sync(() => { |
| 414 | createCalled = true; |
| 415 | return { |
| 416 | session_id: 'trs_proxy_test', |
| 417 | config: { |
| 418 | user_id: 'global_test_user_id', |
| 419 | execute: {}, |
| 420 | search: {}, |
| 421 | preload: { tools: [] }, |
| 422 | instant: false, |
| 423 | }, |
| 424 | config_version: 1, |
| 425 | mcp: { type: 'http' as const, url: 'https://mcp.test.composio.dev' }, |
| 426 | tool_router_tools: [], |
| 427 | }; |
| 428 | }), |
| 429 | }, |
| 430 | }); |
| 431 | |
| 432 | const { failure, output } = yield* Effect.gen(function* () { |
| 433 | const failure = yield* cli([ |
| 434 | 'proxy', |
| 435 | 'https://gmail.googleapis.com/gmail/v1/users/me/profile', |
| 436 | '--toolkit', |
| 437 | 'gmail', |
| 438 | ]).pipe(Effect.flip); |
| 439 | const lines = yield* MockConsole.getLines({ stripAnsi: true }); |
| 440 | return { failure, output: lines.join('\n') }; |
| 441 | }).pipe(Effect.provide(live)); |
| 442 | |
| 443 | expect(createCalled).toBe(false); |
| 444 | expect(failure).toBeInstanceOf(ReportedProxyCommandError); |
| 445 | expect(failure).toMatchObject({ toolkit: 'gmail' }); |
| 446 | |
| 447 | expect(output).toContain('Toolkit "gmail" is not connected for this user'); |
| 448 | expect(output).toContain('composio link gmail'); |
| 449 | expect(refreshSpy).toHaveBeenCalled(); |
| 450 | expect(getFreshSpy).toHaveBeenCalled(); |
| 451 | }) |
| 452 | ); |
| 453 | } |
| 454 | ); |
| 455 | |
| 456 | layer(TestLive({ baseConfigProvider: testConfigProvider, fixture: 'global-test-user-id' }))( |
| 457 | '[Given] backend 4302 no-connection error [Then] proxy rewrites it to link guidance', |
| 458 | it => { |
| 459 | it.effect('translates proxy_execute connection errors like execute does', () => |
| 460 | Effect.gen(function* () { |
| 461 | const live = TestLive({ |
| 462 | baseConfigProvider: testConfigProvider, |
| 463 | fixture: 'global-test-user-id', |
| 464 | toolRouter: { |
| 465 | proxyExecute: () => |
| 466 | Effect.fail( |
| 467 | new BadRequestError({ |
| 468 | message: `400 ${JSON.stringify({ error: noActiveConnection })}`, |
| 469 | method: 'POST', |
| 470 | url: 'https://mock.invalid/api/v3.1/tool_router/session/{session_id}/proxy_execute', |
| 471 | status: 400, |
| 472 | requestId: undefined, |
| 473 | details: noActiveConnection, |
| 474 | body: { error: noActiveConnection }, |
| 475 | }) |
| 476 | ), |
| 477 | }, |
| 478 | }); |
| 479 | |
| 480 | const { failure, output } = yield* Effect.gen(function* () { |
| 481 | const failure = yield* cli([ |
| 482 | 'proxy', |
| 483 | 'https://gmail.googleapis.com/gmail/v1/users/me/profile', |
| 484 | '--toolkit', |
| 485 | 'gmail', |
| 486 | '--skip-connection-check', |
| 487 | ]).pipe(Effect.flip); |
| 488 | const lines = yield* MockConsole.getLines({ stripAnsi: true }); |
| 489 | return { failure, output: lines.join('\n') }; |
| 490 | }).pipe(Effect.provide(live)); |
| 491 | |
| 492 | expect(failure).toMatchObject({ |
| 493 | name: 'ComposioNoActiveConnectionError', |
| 494 | toolkit: 'gmail', |
| 495 | }); |
| 496 | |
| 497 | expect(output).toContain('No active connection found for toolkit "gmail"'); |
| 498 | expect(output).toContain('composio link gmail'); |
| 499 | }) |
| 500 | ); |
| 501 | } |
| 502 | ); |
| 503 | |
| 504 | layer(TestLive({ baseConfigProvider: testConfigProvider, fixture: 'global-test-user-id' }))( |
| 505 | '[Given] proxy_execute asks for user input [Then] proxy fails without a response', |
| 506 | it => { |
| 507 | it.effect('reports that the proxied call requires user input', () => |
| 508 | Effect.gen(function* () { |
| 509 | const live = TestLive({ |
| 510 | baseConfigProvider: testConfigProvider, |
| 511 | fixture: 'global-test-user-id', |
| 512 | toolRouter: { |
| 513 | proxyExecute: () => |
| 514 | Effect.succeed({ |
| 515 | result_type: 'input_required' as const, |
| 516 | input_requests: { |
| 517 | approval_1: { |
| 518 | type: 'elicitation' as const, |
| 519 | mode: 'form' as const, |
| 520 | message: 'Allow this request to Gmail?', |
| 521 | requested_schema: { type: 'object' }, |
| 522 | }, |
| 523 | }, |
| 524 | request_state: 'opaque-state-token', |
| 525 | }), |
| 526 | }, |
| 527 | }); |
| 528 | |
| 529 | const { failure, output } = yield* Effect.gen(function* () { |
| 530 | const failure = yield* cli([ |
| 531 | 'proxy', |
| 532 | 'https://gmail.googleapis.com/gmail/v1/users/me/profile', |
| 533 | '--toolkit', |
| 534 | 'gmail', |
| 535 | '--skip-connection-check', |
| 536 | ]).pipe(Effect.flip); |
| 537 | const lines = yield* MockConsole.getLines({ stripAnsi: true }); |
| 538 | return { failure, output: lines.join('\n') }; |
| 539 | }).pipe(Effect.provide(live)); |
| 540 | |
| 541 | expect(failure).toMatchObject({ _tag: 'commands/ProxyRequestError' }); |
| 542 | expect(output).toContain( |
| 543 | 'GET proxy call via \\"gmail\\" requires user input before it can run (1 input request) and was not executed.' |
| 544 | ); |
| 545 | expect(output).not.toContain('Status:'); |
| 546 | expect(output).not.toContain('opaque-state-token'); |
| 547 | }) |
| 548 | ); |
| 549 | } |
| 550 | ); |
| 551 | |
| 552 | layer(TestLive({ baseConfigProvider: testConfigProvider, fixture: 'global-test-user-id' }))( |
| 553 | '[Given] a session hook blocks the proxied request [Then] proxy fails with the hook message', |
| 554 | it => { |
| 555 | it.effect('reports the hook that blocked the request', () => |
| 556 | Effect.gen(function* () { |
| 557 | const live = TestLive({ |
| 558 | baseConfigProvider: testConfigProvider, |
| 559 | fixture: 'global-test-user-id', |
| 560 | toolRouter: { |
| 561 | proxyExecute: () => |
| 562 | Effect.succeed({ |
| 563 | result_type: 'blocked' as const, |
| 564 | reason: { type: 'request_blocked' as const, hook_id: 'no_external_email' }, |
| 565 | message: 'Blocked by session policy: No external email', |
| 566 | }), |
| 567 | }, |
| 568 | }); |
| 569 | |
| 570 | const { failure, output } = yield* Effect.gen(function* () { |
| 571 | const failure = yield* cli([ |
| 572 | 'proxy', |
| 573 | 'https://gmail.googleapis.com/gmail/v1/users/me/profile', |
| 574 | '--toolkit', |
| 575 | 'gmail', |
| 576 | '--skip-connection-check', |
| 577 | ]).pipe(Effect.flip); |
| 578 | const lines = yield* MockConsole.getLines({ stripAnsi: true }); |
| 579 | return { failure, output: lines.join('\n') }; |
| 580 | }).pipe(Effect.provide(live)); |
| 581 | |
| 582 | expect(failure).toMatchObject({ _tag: 'commands/ProxyRequestError' }); |
| 583 | expect(output).toContain( |
| 584 | 'GET proxy call via \\"gmail\\" was blocked by session hook \\"no_external_email\\": Blocked by session policy: No external email' |
| 585 | ); |
| 586 | expect(output).not.toContain('Status:'); |
| 587 | }) |
| 588 | ); |
| 589 | } |
| 590 | ); |
| 591 | |
| 592 | layer(TestLive({ baseConfigProvider: testConfigProvider, fixture: 'global-test-user-id' }))( |
| 593 | '[Given] an always_deny override on the default account [Then] proxy is denied before proxy_execute', |
| 594 | it => { |
| 595 | it.effect('gates on the account the session resolves when --account is omitted', () => |
| 596 | Effect.gen(function* () { |
| 597 | vi.spyOn(toolPermissions, 'getConsumerPermissionSnapshot').mockImplementation(params => |
| 598 | Effect.succeed({ |
| 599 | orgId: params.orgId, |
| 600 | projectId: params.projectId, |
| 601 | consumerUserId: params.consumerUserId, |
| 602 | enhancedControlsEnabled: true, |
| 603 | permissions: { |
| 604 | default: 'allow_all', |
| 605 | overrides: { '*:con_gmail_work': 'always_deny' }, |
| 606 | }, |
| 607 | connectedAccountIds: [], |
| 608 | fetchedAt: 1_767_225_600_000, |
| 609 | }) |
| 610 | ); |
| 611 | |
| 612 | let createParams: SessionCreateParams | undefined; |
| 613 | let proxyCalled = false; |
| 614 | const live = TestLive({ |
| 615 | baseConfigProvider: testConfigProvider, |
| 616 | fixture: 'global-test-user-id', |
| 617 | cliUserConfig: { experimentalFeatures: { multi_account: false } }, |
| 618 | connectedAccountsData: { |
| 619 | items: [ |
| 620 | { |
| 621 | id: 'con_gmail_work', |
| 622 | alias: 'work', |
| 623 | word_id: 'gmail-work', |
| 624 | status: 'ACTIVE', |
| 625 | status_reason: null, |
| 626 | is_disabled: false, |
| 627 | user_id: 'consumer-user-org_test', |
| 628 | toolkit: { slug: 'gmail' }, |
| 629 | auth_config: { |
| 630 | id: 'ac_gmail_oauth', |
| 631 | auth_scheme: 'OAUTH2', |
| 632 | is_composio_managed: true, |
| 633 | is_disabled: false, |
| 634 | }, |
| 635 | created_at: '2026-01-01T00:00:00.000Z', |
| 636 | updated_at: '2026-01-01T00:00:00.000Z', |
| 637 | test_request_endpoint: '', |
| 638 | }, |
| 639 | ], |
| 640 | }, |
| 641 | toolRouter: { |
| 642 | create: (params: SessionCreateParams) => |
| 643 | Effect.sync(() => { |
| 644 | createParams = params; |
| 645 | return { |
| 646 | session_id: 'trs_proxy_test', |
| 647 | config: { |
| 648 | user_id: params.user_id, |
| 649 | execute: {}, |
| 650 | search: {}, |
| 651 | preload: { tools: [] }, |
| 652 | instant: false, |
| 653 | }, |
| 654 | config_version: 1, |
| 655 | mcp: { type: 'http' as const, url: 'https://mcp.test.composio.dev' }, |
| 656 | tool_router_tools: [], |
| 657 | }; |
| 658 | }), |
| 659 | proxyExecute: () => |
| 660 | Effect.sync(() => { |
| 661 | proxyCalled = true; |
| 662 | return { |
| 663 | result_type: 'completed' as const, |
| 664 | status: 200, |
| 665 | data: { ok: true }, |
| 666 | headers: { 'content-type': 'application/json' }, |
| 667 | }; |
| 668 | }), |
| 669 | }, |
| 670 | }); |
| 671 | |
| 672 | const failure = yield* cli([ |
| 673 | 'proxy', |
| 674 | 'https://gmail.googleapis.com/gmail/v1/users/me/drafts', |
| 675 | '--toolkit', |
| 676 | 'gmail', |
| 677 | '-X', |
| 678 | 'post', |
| 679 | ]).pipe(Effect.flip, Effect.provide(live)); |
| 680 | |
| 681 | expect(createParams?.connected_accounts).toEqual({ gmail: 'con_gmail_work' }); |
| 682 | expect(failure).toBeInstanceOf(toolPermissions.ToolPermissionDeniedError); |
| 683 | expect(failure).toMatchObject({ deniedBy: 'permissions', toolSlug: 'gmail' }); |
| 684 | expect(proxyCalled).toBe(false); |
| 685 | }) |
| 686 | ); |
| 687 | } |
| 688 | ); |
| 689 | |
| 690 | // Formatting fixtures: standalone controls (BEL, DEL), CR/LF, a lone CR and a tab. |
| 691 | const remoteText = 'first line\r\nsecond\tcol\u0007 \u007f end\rover'; |
| 692 | |
| 693 | layer(TestLive({ baseConfigProvider: testConfigProvider, fixture: 'global-test-user-id' }))( |
| 694 | '[Given] a text response with controls, stdout redirected and stderr a terminal', |
| 695 | it => { |
| 696 | it.effect('[Then] the note escapes them and stdout gets the body as it is', () => |
| 697 | Effect.gen(function* () { |
| 698 | const terminal = redirectedTerminalUI({ decoratedStderr: true }); |
| 699 | const live = TestLive({ |
| 700 | baseConfigProvider: testConfigProvider, |
| 701 | fixture: 'global-test-user-id', |
| 702 | toolRouter: { |
| 703 | proxyExecute: () => |
| 704 | Effect.succeed({ |
| 705 | result_type: 'completed' as const, |
| 706 | status: 200, |
| 707 | data: remoteText, |
| 708 | headers: { 'content-type': 'text/plain' }, |
| 709 | }), |
| 710 | }, |
| 711 | }); |
| 712 | |
| 713 | yield* cli([ |
| 714 | 'proxy', |
| 715 | 'https://gmail.googleapis.com/gmail/v1/users/me/profile', |
| 716 | '--toolkit', |
| 717 | 'gmail', |
| 718 | '--skip-connection-check', |
| 719 | ]).pipe(Effect.provide(terminal.layer), Effect.provide(live)); |
| 720 | |
| 721 | expect(terminal.stdout()).toBe(`${remoteText}\n`); |
| 722 | const stderr = terminal.stderr(); |
| 723 | expect(stderr).toContain('Proxy Response'); |
| 724 | expect(stderr).toContain('first line'); |
| 725 | expect(stderr).toContain('second\tcol\\u0007 \\u007f end\\u000dover'); |
| 726 | expect(stderr).not.toMatch(/[\u0007\u007f]/u); |
| 727 | }) |
| 728 | ); |
| 729 | } |
| 730 | ); |
| 731 | |
| 732 | layer(TestLive({ baseConfigProvider: testConfigProvider, fixture: 'global-test-user-id' }))( |
| 733 | '[Given] a blocked request whose message has controls', |
| 734 | it => { |
| 735 | it.effect('[Then] stderr shows it escaped and the stdout JSON keeps the message', () => |
| 736 | Effect.gen(function* () { |
| 737 | const terminal = redirectedTerminalUI({ decoratedStderr: true }); |
| 738 | const live = TestLive({ |
| 739 | baseConfigProvider: testConfigProvider, |
| 740 | fixture: 'global-test-user-id', |
| 741 | toolRouter: { |
| 742 | proxyExecute: () => |
| 743 | Effect.succeed({ |
| 744 | result_type: 'blocked' as const, |
| 745 | reason: { type: 'request_blocked' as const, hook_id: 'policy' }, |
| 746 | message: remoteText, |
| 747 | }), |
| 748 | }, |
| 749 | }); |
| 750 | |
| 751 | const failure = yield* cli([ |
| 752 | 'proxy', |
| 753 | 'https://gmail.googleapis.com/gmail/v1/users/me/profile', |
| 754 | '--toolkit', |
| 755 | 'gmail', |
| 756 | '--skip-connection-check', |
| 757 | ]).pipe(Effect.provide(terminal.layer), Effect.flip, Effect.provide(live)); |
| 758 | |
| 759 | expect(failure).toMatchObject({ _tag: 'commands/ProxyRequestError' }); |
| 760 | const result = JSON.parse(terminal.stdout()) as { readonly error: string }; |
| 761 | expect(result.error).toBe( |
| 762 | `GET proxy call via "gmail" was blocked by session hook "policy": ${remoteText}` |
| 763 | ); |
| 764 | const stderr = terminal.stderr(); |
| 765 | expect(stderr).toContain( |
| 766 | 'blocked by session hook "policy": first line\\u000d\\u000asecond\\u0009col\\u0007 \\u007f end\\u000dover' |
| 767 | ); |
| 768 | expect(stderr).not.toMatch(/[\u0007\u007f]/u); |
| 769 | }) |
| 770 | ); |
| 771 | } |
| 772 | ); |
| 773 | }); |