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 | /** |
| 2 | * Process and streams: signals, a closed stdout pipe, and the three stream |
| 3 | * contracts (stdout is data only; decoration, notices, and hints stay off it). |
| 4 | */ |
| 5 | |
| 6 | import { afterAll, beforeAll, describe, expect, it } from 'bun:test'; |
| 7 | import { CONSUMER_PROJECT, CONSUMER_USER_ID, MEMBER } from '@e2e-tests/cli-fake-backend'; |
| 8 | import { startCliRig, type CliRig } from '@e2e-tests/cli-fake-backend/rig'; |
| 9 | import { z } from 'zod'; |
| 10 | import { |
| 11 | clean, |
| 12 | ExecutionResult, |
| 13 | RIG_TIMEOUT_MS, |
| 14 | runWithTerminal, |
| 15 | SEARCH_SLACK, |
| 16 | SearchResult, |
| 17 | stdoutJson, |
| 18 | TEST_TIMEOUT_MS, |
| 19 | writeDebugLog, |
| 20 | } from './support'; |
| 21 | |
| 22 | const Whoami = z.looseObject({ email: z.string() }); |
| 23 | const UPDATE_NOTICE = 'Update available'; |
| 24 | const PLUGIN_HINT = 'Tip: running under Claude Code'; |
| 25 | |
| 26 | describe('process: signals', () => { |
| 27 | let rig: CliRig; |
| 28 | |
| 29 | beforeAll(async () => { |
| 30 | rig = await startCliRig({ label: 'signals' }); |
| 31 | await rig.login(); |
| 32 | // Nobody finishes the authorization, so `link` is still waiting when the signal arrives. |
| 33 | rig.fake.world.linkCompletesAfterPolls = null; |
| 34 | }, RIG_TIMEOUT_MS); |
| 35 | |
| 36 | afterAll(async () => { |
| 37 | await writeDebugLog(rig, import.meta.dir, 'signals'); |
| 38 | await rig.stop(); |
| 39 | }); |
| 40 | |
| 41 | /** |
| 42 | * Sends the signal once `ready` is on stderr, so a slow start cannot make the |
| 43 | * signal arrive before the command is waiting. `settle` seconds after the |
| 44 | * command ended, stdout is measured, which catches a child left running. |
| 45 | */ |
| 46 | const interrupted = (command: string, signal: 'INT' | 'TERM', ready: string, settle = 0) => |
| 47 | [ |
| 48 | `${command} > /tmp/sig.out 2> /tmp/sig.err &`, |
| 49 | 'pid=$!', |
| 50 | `for i in $(seq 1 150); do grep -q '${ready}' /tmp/sig.err && break; sleep 0.1; done`, |
| 51 | `grep -q '${ready}' /tmp/sig.err || echo "never ready"`, |
| 52 | `kill -${signal} $pid`, |
| 53 | 'wait $pid', |
| 54 | 'echo "status=$?"', |
| 55 | `sleep ${settle}`, |
| 56 | 'echo "stdout-bytes=$(wc -c < /tmp/sig.out)"', |
| 57 | ].join('\n'); |
| 58 | |
| 59 | for (const { signal, status, toolkit } of [ |
| 60 | { signal: 'INT', status: 130, toolkit: 'slack' }, |
| 61 | { signal: 'TERM', status: 143, toolkit: 'github' }, |
| 62 | ] as const) { |
| 63 | it( |
| 64 | `SIG${signal} while \`link\` waits: exits ${status} with nothing on stdout`, |
| 65 | async () => { |
| 66 | const result = await rig.run(interrupted(`composio link ${toolkit}`, signal, '/api/v3/s/')); |
| 67 | expect(result.stdout).not.toContain('never ready'); |
| 68 | expect(result.stdout).toContain(`status=${status}`); |
| 69 | expect(result.stdout).toContain('stdout-bytes=0'); |
| 70 | // The link was requested once, and the interrupted wait did not request another. |
| 71 | expect( |
| 72 | result.requests.filter(entry => entry.route === 'toolRouter.session.link') |
| 73 | ).toHaveLength(1); |
| 74 | }, |
| 75 | TEST_TIMEOUT_MS |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | it( |
| 80 | 'SIGINT while `run` executes a script: exits 130 and the script stops', |
| 81 | async () => { |
| 82 | const result = await rig.run( |
| 83 | interrupted( |
| 84 | `composio run 'console.error("started"); await new Promise(resolve => setTimeout(resolve, 4000)); console.log("finished")'`, |
| 85 | 'INT', |
| 86 | 'started', |
| 87 | // Longer than the script's 4 seconds: a script left running would print by then. |
| 88 | 5 |
| 89 | ) |
| 90 | ); |
| 91 | expect(result.stdout).not.toContain('never ready'); |
| 92 | expect(result.stdout).toContain('status=130'); |
| 93 | expect(result.stdout).toContain('stdout-bytes=0'); |
| 94 | }, |
| 95 | TEST_TIMEOUT_MS |
| 96 | ); |
| 97 | |
| 98 | it( |
| 99 | '`link --no-browser` off a terminal prints the authorization URL on stderr before it waits', |
| 100 | async () => { |
| 101 | // Killed while it waits: whatever it printed, it printed before the authorization. |
| 102 | const result = await rig.run('composio link gmail --no-browser', { timeoutSeconds: 6 }); |
| 103 | const connection = rig.fake.world.connections.at(-1); |
| 104 | expect(result.exitCode).toBe(124); |
| 105 | expect(result.stderr).toContain(`${rig.fake.docker.http}/api/v3/s/${connection?.id}`); |
| 106 | expect(result.stderr).toContain('For agents: Show the URL above to the user'); |
| 107 | expect(result.stderr).toContain('--no-wait'); |
| 108 | // stdout is for the connected account's JSON, which only comes once it is active. |
| 109 | expect(result.stdout).toBe(''); |
| 110 | }, |
| 111 | TEST_TIMEOUT_MS |
| 112 | ); |
| 113 | |
| 114 | it( |
| 115 | '`link --no-wait` prints the authorization URL and returns at once', |
| 116 | async () => { |
| 117 | const result = await rig.run('composio link gmail --no-wait'); |
| 118 | expect(result.exitCode).toBe(0); |
| 119 | const connection = rig.fake.world.connections.at(-1); |
| 120 | expect( |
| 121 | stdoutJson(result, z.looseObject({ status: z.string(), redirect_url: z.string() })) |
| 122 | ).toMatchObject({ |
| 123 | status: 'pending', |
| 124 | redirect_url: `${rig.fake.docker.http}/api/v3/s/${connection?.id}`, |
| 125 | }); |
| 126 | expect(result.requests.map(entry => entry.route)).not.toContain('connectedAccounts.retrieve'); |
| 127 | }, |
| 128 | TEST_TIMEOUT_MS |
| 129 | ); |
| 130 | }); |
| 131 | |
| 132 | describe('streams', () => { |
| 133 | let rig: CliRig; |
| 134 | |
| 135 | beforeAll(async () => { |
| 136 | rig = await startCliRig({ label: 'streams' }); |
| 137 | await rig.login(); |
| 138 | rig.fake.world.connect(CONSUMER_PROJECT.label, CONSUMER_USER_ID, 'slack'); |
| 139 | }, RIG_TIMEOUT_MS); |
| 140 | |
| 141 | afterAll(async () => { |
| 142 | await writeDebugLog(rig, import.meta.dir, 'streams'); |
| 143 | await rig.stop(); |
| 144 | }); |
| 145 | |
| 146 | const seedUpdateAvailable = () => |
| 147 | rig.run( |
| 148 | `printf '{"lastChecked":"%s","latestVersion":"99.0.0"}' "$(date -u +%Y-%m-%dT%H:%M:%S.000Z)" > ~/.composio/update-check.json` |
| 149 | ); |
| 150 | |
| 151 | // `true` exits without reading, so the pipe is closed before the CLI writes; a |
| 152 | // reader like `head -1` would let a small result fit in the pipe's buffer. |
| 153 | const CLOSED_EARLY = `{ ${SEARCH_SLACK}; echo $? > /tmp/pipe.rc; } | true; cat /tmp/pipe.rc`; |
| 154 | |
| 155 | it( |
| 156 | 'a reader that closes stdout early does not fail the command', |
| 157 | async () => { |
| 158 | const result = await rig.run(CLOSED_EARLY); |
| 159 | expect(result.exitCode).toBe(0); |
| 160 | expect(result.stdout).toBe('0\n'); |
| 161 | expect(result.stderr).not.toContain('EPIPE'); |
| 162 | }, |
| 163 | TEST_TIMEOUT_MS |
| 164 | ); |
| 165 | |
| 166 | it( |
| 167 | 'stdin: `-d -` reads the arguments from a pipe', |
| 168 | async () => { |
| 169 | const result = await rig.run('composio execute SLACK_SEND_MESSAGE -d -', { |
| 170 | stdin: '{"channel":"general","text":"from stdin"}', |
| 171 | }); |
| 172 | expect(result.exitCode).toBe(0); |
| 173 | expect(stdoutJson(result, ExecutionResult).data).toMatchObject({ |
| 174 | message: { text: 'from stdin' }, |
| 175 | }); |
| 176 | }, |
| 177 | TEST_TIMEOUT_MS |
| 178 | ); |
| 179 | |
| 180 | it( |
| 181 | 'stdin: `-d -` with nothing on stdin fails without waiting for input', |
| 182 | async () => { |
| 183 | const result = await rig.run('composio execute SLACK_SEND_MESSAGE -d -', { |
| 184 | timeoutSeconds: 10, |
| 185 | }); |
| 186 | expect(result.exitCode).toBe(1); |
| 187 | expect(result.stdout).toBe(''); |
| 188 | expect(clean(result.stderr)).toContain('Invalid JSON input'); |
| 189 | expect(result.requests.map(entry => entry.route)).not.toContain('dashboard.execute'); |
| 190 | }, |
| 191 | TEST_TIMEOUT_MS |
| 192 | ); |
| 193 | |
| 194 | it( |
| 195 | 'no terminal at all: stdout is the JSON and stderr is empty', |
| 196 | async () => { |
| 197 | const result = await rig.run('composio whoami'); |
| 198 | expect(result.exitCode).toBe(0); |
| 199 | expect(stdoutJson(result, Whoami).email).toBe(MEMBER.email); |
| 200 | expect(result.stderr).toBe(''); |
| 201 | }, |
| 202 | TEST_TIMEOUT_MS |
| 203 | ); |
| 204 | |
| 205 | it( |
| 206 | 'stdout piped, stderr a terminal: the JSON goes to the pipe, decoration to the terminal', |
| 207 | async () => { |
| 208 | const result = await runWithTerminal(rig, 'composio whoami > /tmp/tty.out'); |
| 209 | expect(result.exitCode).toBe(0); |
| 210 | const piped = await rig.journey.readFile('/tmp/tty.out'); |
| 211 | expect(Whoami.parse(JSON.parse(piped)).email).toBe(MEMBER.email); |
| 212 | expect(result.terminal).toContain(MEMBER.email); |
| 213 | // The terminal shows the human rendering, not a second copy of the data. |
| 214 | expect(result.terminal).not.toContain('"account_type"'); |
| 215 | }, |
| 216 | TEST_TIMEOUT_MS |
| 217 | ); |
| 218 | |
| 219 | it( |
| 220 | 'stdout a terminal, stderr redirected: no data leaks onto the terminal', |
| 221 | async () => { |
| 222 | const result = await runWithTerminal(rig, 'composio whoami 2> /tmp/tty.err'); |
| 223 | expect(result.exitCode).toBe(0); |
| 224 | expect(result.terminal).not.toContain('"account_type"'); |
| 225 | expect(await rig.journey.readFile('/tmp/tty.err')).toBe(''); |
| 226 | }, |
| 227 | TEST_TIMEOUT_MS |
| 228 | ); |
| 229 | |
| 230 | it( |
| 231 | 'the update notice is never written to stdout', |
| 232 | async () => { |
| 233 | await seedUpdateAvailable(); |
| 234 | const piped = await rig.run('composio whoami'); |
| 235 | expect(stdoutJson(piped, Whoami).email).toBe(MEMBER.email); |
| 236 | expect(piped.stdout).not.toContain(UPDATE_NOTICE); |
| 237 | |
| 238 | const withTerminal = await runWithTerminal(rig, 'composio whoami > /tmp/notice.out'); |
| 239 | expect(withTerminal.terminal).toContain(`${UPDATE_NOTICE}:`); |
| 240 | expect(withTerminal.terminal).toContain('99.0.0'); |
| 241 | const data = await rig.journey.readFile('/tmp/notice.out'); |
| 242 | expect(Whoami.parse(JSON.parse(data)).email).toBe(MEMBER.email); |
| 243 | }, |
| 244 | TEST_TIMEOUT_MS |
| 245 | ); |
| 246 | |
| 247 | it( |
| 248 | 'the plugin hint under an agent host is written to stderr, never stdout', |
| 249 | async () => { |
| 250 | const result = await rig.run(`CLAUDECODE=1 ${SEARCH_SLACK}`); |
| 251 | expect(result.exitCode).toBe(0); |
| 252 | expect(stdoutJson(result, SearchResult).results).toHaveLength(1); |
| 253 | expect(result.stdout).not.toContain(PLUGIN_HINT); |
| 254 | expect(clean(result.stderr)).toContain(PLUGIN_HINT); |
| 255 | }, |
| 256 | TEST_TIMEOUT_MS |
| 257 | ); |
| 258 | |
| 259 | it( |
| 260 | 'today: `search --human` off a terminal prints nothing and exits 0', |
| 261 | async () => { |
| 262 | const result = await rig.run(`${SEARCH_SLACK} --human`); |
| 263 | expect(result.exitCode).toBe(0); |
| 264 | expect(result.stdout).toBe(''); |
| 265 | expect(result.stderr).toBe(''); |
| 266 | }, |
| 267 | TEST_TIMEOUT_MS |
| 268 | ); |
| 269 | |
| 270 | // CLI defect: `--human` renders through terminal decoration only, so off a terminal it prints nothing and exits 0. |
| 271 | it.failing( |
| 272 | '`search --human` off a terminal still prints the results', |
| 273 | async () => { |
| 274 | const result = await rig.run(`${SEARCH_SLACK} --human`); |
| 275 | expect(`${result.stdout}${result.stderr}`).toContain('SLACK_SEND_MESSAGE'); |
| 276 | }, |
| 277 | TEST_TIMEOUT_MS |
| 278 | ); |
| 279 | }); |