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 * as BunFileSystem from '@effect/platform-bun/BunFileSystem'; |
| 2 | import * as BunPath from '@effect/platform-bun/BunPath'; |
| 3 | import { describe, expect, it } from '@effect/vitest'; |
| 4 | import { ConfigProvider, Effect, Layer, Option } from 'effect'; |
| 5 | import * as FileSystem from 'effect/FileSystem'; |
| 6 | import path from 'node:path'; |
| 7 | import { Writable } from 'node:stream'; |
| 8 | import * as tempy from 'tempy'; |
| 9 | import { ShellSetupAbortError } from 'src/commands/install/install.errors'; |
| 10 | import { |
| 11 | BIN_DIR_NOT_ABSOLUTE_MESSAGE, |
| 12 | BIN_DIR_UNSAFE_MESSAGE, |
| 13 | } from 'src/commands/install/install.format'; |
| 14 | import { installShellIntegration, runInstall } from 'src/commands/install/install.handler'; |
| 15 | import { CliUserConfig } from 'src/models/cli-user-config'; |
| 16 | import { ComposioCliUserConfig } from 'src/services/cli-user-config'; |
| 17 | import { defaultNodeOs, NodeOs } from 'src/services/node-os'; |
| 18 | import { NodeProcess } from 'src/services/node-process'; |
| 19 | import { getTerminalCapabilities, makeTerminalUI, TerminalUI } from 'src/services/terminal-ui'; |
| 20 | import { terminalUITestImpl } from 'test/__utils__/services/terminal-ui-test'; |
| 21 | |
| 22 | const BIN_DIR = '/opt/composio/bin'; |
| 23 | |
| 24 | const cliUserConfig = ComposioCliUserConfig.of({ |
| 25 | data: { |
| 26 | channel: 'stable', |
| 27 | developerModeEnabled: false, |
| 28 | developerDangerousCommandsEnabled: false, |
| 29 | experimentalFeatures: {}, |
| 30 | artifactDirectory: undefined, |
| 31 | security: 'auto', |
| 32 | }, |
| 33 | raw: CliUserConfig.make({ |
| 34 | developer: { enabled: false, destructiveActions: false }, |
| 35 | experimentalFeatures: {}, |
| 36 | artifactDirectory: Option.none(), |
| 37 | security: 'auto', |
| 38 | }), |
| 39 | channel: 'stable', |
| 40 | isDevModeEnabled: () => false, |
| 41 | areDeveloperDangerousCommandsEnabled: () => false, |
| 42 | isExperimentalFeatureEnabled: () => false, |
| 43 | update: () => Effect.void, |
| 44 | }); |
| 45 | |
| 46 | const makeSink = () => { |
| 47 | const chunks: string[] = []; |
| 48 | const sink = new Writable({ |
| 49 | write(chunk, _encoding, callback) { |
| 50 | chunks.push(String(chunk)); |
| 51 | callback(); |
| 52 | }, |
| 53 | }); |
| 54 | return Object.assign(sink, { isTTY: false, chunks }); |
| 55 | }; |
| 56 | |
| 57 | /** |
| 58 | * What the install handler needs and nothing more: the real filesystem over a fresh home |
| 59 | * directory, the running executable at `execPath`, the given environment, and a terminal |
| 60 | * whose stderr is a pipe, so every report line is written plain and can be compared exactly. |
| 61 | * Pass `ui` to swap the terminal out — e.g. for a recording UI that pins the decorated |
| 62 | * (`canDecorate=true`) report channel. |
| 63 | */ |
| 64 | const installTest = ( |
| 65 | params: { |
| 66 | readonly env?: Record<string, string>; |
| 67 | readonly execPath?: string; |
| 68 | readonly ui?: Layer.Layer<TerminalUI>; |
| 69 | } = {} |
| 70 | ) => { |
| 71 | const home = tempy.temporaryDirectory({ prefix: 'install-handler' }); |
| 72 | const stderr = makeSink(); |
| 73 | const uiLayer = |
| 74 | params.ui ?? |
| 75 | Layer.succeed( |
| 76 | TerminalUI, |
| 77 | makeTerminalUI({ stdin: { isTTY: false }, stdout: makeSink(), stderr }) |
| 78 | ); |
| 79 | const layer = Layer.mergeAll( |
| 80 | BunFileSystem.layer, |
| 81 | BunPath.layer, |
| 82 | Layer.succeed(NodeOs, defaultNodeOs({ homedir: home })), |
| 83 | Layer.succeed( |
| 84 | NodeProcess, |
| 85 | NodeProcess.of({ |
| 86 | cwd: home, |
| 87 | execPath: params.execPath ?? `${BIN_DIR}/composio`, |
| 88 | platform: 'linux', |
| 89 | arch: 'x64', |
| 90 | }) |
| 91 | ), |
| 92 | uiLayer, |
| 93 | Layer.succeed(ComposioCliUserConfig, cliUserConfig), |
| 94 | ConfigProvider.layer(ConfigProvider.fromEnv({ env: { PATH: '/usr/bin:/bin', ...params.env } })) |
| 95 | ); |
| 96 | return { |
| 97 | home, |
| 98 | layer, |
| 99 | /** Every stderr line written so far. */ |
| 100 | stderrLines: () => stderr.chunks.join('').split('\n').slice(0, -1), |
| 101 | }; |
| 102 | }; |
| 103 | |
| 104 | const readHomeFile = (home: string, file: string) => |
| 105 | Effect.gen(function* () { |
| 106 | const fs = yield* FileSystem.FileSystem; |
| 107 | return yield* fs.readFileString(path.join(home, file)); |
| 108 | }); |
| 109 | |
| 110 | /** |
| 111 | * A stderr-TTY terminal (`canDecorate=true`, the interactive default) that records where |
| 112 | * `makeInstallReporter`'s decorated arm sends each report: status lines to `log.step`, |
| 113 | * per-file confirmations to `log.success`, aborts to `log.error`, boxed hints to `note`, |
| 114 | * and the plain `error` fallback never touched. |
| 115 | */ |
| 116 | const decoratedInstallUI = () => { |
| 117 | const calls = { |
| 118 | step: [] as string[], |
| 119 | success: [] as string[], |
| 120 | warn: [] as string[], |
| 121 | error: [] as string[], |
| 122 | note: [] as [message: string, title: string | undefined][], |
| 123 | plainError: [] as string[], |
| 124 | }; |
| 125 | const ui = TerminalUI.of({ |
| 126 | ...terminalUITestImpl, |
| 127 | capabilities: Effect.succeed( |
| 128 | getTerminalCapabilities({ |
| 129 | stdin: { isTTY: false }, |
| 130 | stdout: { isTTY: false }, |
| 131 | stderr: { isTTY: true }, |
| 132 | }) |
| 133 | ), |
| 134 | error: data => Effect.sync(() => calls.plainError.push(data)), |
| 135 | log: { |
| 136 | ...terminalUITestImpl.log, |
| 137 | success: message => Effect.sync(() => calls.success.push(message)), |
| 138 | warn: message => Effect.sync(() => calls.warn.push(message)), |
| 139 | error: message => Effect.sync(() => calls.error.push(message)), |
| 140 | step: message => Effect.sync(() => calls.step.push(message)), |
| 141 | }, |
| 142 | note: (message, title) => Effect.sync(() => calls.note.push([message, title])), |
| 143 | }); |
| 144 | return { calls, layer: Layer.succeed(TerminalUI, ui) }; |
| 145 | }; |
| 146 | |
| 147 | describe('installShellIntegration', () => { |
| 148 | it.effect('zsh: writes ~/.zshrc and prints the restart hint', () => { |
| 149 | const { home, layer, stderrLines } = installTest({ env: { SHELL: '/bin/zsh' } }); |
| 150 | return Effect.gen(function* () { |
| 151 | yield* installShellIntegration({ completions: false }); |
| 152 | |
| 153 | expect(stderrLines()).toEqual([ |
| 154 | 'Detected shell: zsh', |
| 155 | `PATH: will add ${BIN_DIR} to $PATH`, |
| 156 | 'Completions: skipped for zsh', |
| 157 | 'Updated ~/.zshrc', |
| 158 | 'Restart your shell to apply changes:', |
| 159 | 'source ~/.zshrc', |
| 160 | ]); |
| 161 | expect(yield* readHomeFile(home, '.zshrc')).toBe( |
| 162 | `\n# Composio CLI\nexport PATH="${BIN_DIR}:$PATH"\n` |
| 163 | ); |
| 164 | }).pipe(Effect.provide(layer)); |
| 165 | }); |
| 166 | |
| 167 | it.effect('zsh: a second run reports the current block and writes nothing', () => { |
| 168 | const { layer, stderrLines } = installTest({ env: { SHELL: '/bin/zsh' } }); |
| 169 | return Effect.gen(function* () { |
| 170 | yield* installShellIntegration({ completions: false }); |
| 171 | const firstRun = stderrLines().length; |
| 172 | yield* installShellIntegration({ completions: false }); |
| 173 | |
| 174 | expect(stderrLines().slice(firstRun)).toEqual([ |
| 175 | 'Detected shell: zsh', |
| 176 | 'PATH: already configured', |
| 177 | 'Completions: skipped for zsh', |
| 178 | 'Shell integration already configured — nothing to do.', |
| 179 | ]); |
| 180 | }).pipe(Effect.provide(layer)); |
| 181 | }); |
| 182 | |
| 183 | it.effect( |
| 184 | 'bash with an existing ~/.profile: the created ~/.bash_profile sources it and is reported first', |
| 185 | () => { |
| 186 | const { home, layer, stderrLines } = installTest({ env: { SHELL: '/bin/bash' } }); |
| 187 | return Effect.gen(function* () { |
| 188 | const fs = yield* FileSystem.FileSystem; |
| 189 | yield* fs.writeFileString(path.join(home, '.profile'), 'export FOO=1\n'); |
| 190 | |
| 191 | yield* installShellIntegration({ completions: false }); |
| 192 | |
| 193 | expect(stderrLines()).toEqual([ |
| 194 | 'Detected shell: bash', |
| 195 | `PATH: will add ${BIN_DIR} to $PATH`, |
| 196 | 'Completions: skipped by default (pass --completions to enable)', |
| 197 | 'Updated ~/.bash_profile', |
| 198 | 'Updated ~/.bashrc', |
| 199 | 'Restart your shell to apply changes:', |
| 200 | 'source ~/.bashrc', |
| 201 | '~/.bash_profile was also updated — that entry applies to new login shells too.', |
| 202 | ]); |
| 203 | expect(yield* readHomeFile(home, '.bash_profile')).toMatch( |
| 204 | /^\n# Created by the Composio CLI installer\.\n[\s\S]*\nfi\n\n# Composio CLI\n/ |
| 205 | ); |
| 206 | expect(yield* readHomeFile(home, '.profile')).toBe('export FOO=1\n'); |
| 207 | }).pipe(Effect.provide(layer)); |
| 208 | } |
| 209 | ); |
| 210 | |
| 211 | it.effect('fish with completions: PATH and completions land in their own files', () => { |
| 212 | const { home, layer, stderrLines } = installTest({ env: { SHELL: '/usr/bin/fish' } }); |
| 213 | return Effect.gen(function* () { |
| 214 | yield* installShellIntegration({ completions: true }); |
| 215 | |
| 216 | expect(stderrLines()).toEqual([ |
| 217 | 'Detected shell: fish', |
| 218 | `PATH: will add ${BIN_DIR} to $PATH`, |
| 219 | 'Completions: will install fish completions to ~/.config/fish/completions/composio.fish', |
| 220 | 'Updated ~/.config/fish/config.fish', |
| 221 | 'Updated ~/.config/fish/completions/composio.fish', |
| 222 | 'Restart your shell to apply changes:', |
| 223 | 'exec fish', |
| 224 | ]); |
| 225 | expect(yield* readHomeFile(home, '.config/fish/completions/composio.fish')).toMatch( |
| 226 | /^\n# Composio CLI completions\n/ |
| 227 | ); |
| 228 | }).pipe(Effect.provide(layer)); |
| 229 | }); |
| 230 | |
| 231 | it.effect('a stale managed block is replaced in place', () => { |
| 232 | const { home, layer, stderrLines } = installTest({ |
| 233 | env: { SHELL: '/bin/zsh', COMPOSIO_BIN_DIR: '/custom/bin' }, |
| 234 | }); |
| 235 | return Effect.gen(function* () { |
| 236 | const fs = yield* FileSystem.FileSystem; |
| 237 | yield* fs.writeFileString( |
| 238 | path.join(home, '.zshrc'), |
| 239 | '# mine\n# Composio CLI\nexport PATH="/old/bin:$PATH"\n' |
| 240 | ); |
| 241 | |
| 242 | yield* installShellIntegration({ completions: false }); |
| 243 | |
| 244 | expect(stderrLines().slice(0, 4)).toEqual([ |
| 245 | 'Detected shell: zsh', |
| 246 | 'PATH: will update the managed block to /custom/bin', |
| 247 | 'Completions: skipped for zsh', |
| 248 | 'Updated ~/.zshrc', |
| 249 | ]); |
| 250 | expect(yield* readHomeFile(home, '.zshrc')).toBe( |
| 251 | '# mine\n\n# Composio CLI\nexport PATH="/custom/bin:$PATH"\n' |
| 252 | ); |
| 253 | }).pipe(Effect.provide(layer)); |
| 254 | }); |
| 255 | |
| 256 | it.effect('the installer origin keeps the status lines and drops the restart hint', () => { |
| 257 | const { layer, stderrLines } = installTest({ |
| 258 | env: { SHELL: '/bin/zsh', COMPOSIO_CLI_INVOCATION_ORIGIN: 'installer' }, |
| 259 | }); |
| 260 | return Effect.gen(function* () { |
| 261 | yield* installShellIntegration({ completions: false }); |
| 262 | |
| 263 | expect(stderrLines()).toEqual([ |
| 264 | 'Detected shell: zsh', |
| 265 | `PATH: will add ${BIN_DIR} to $PATH`, |
| 266 | 'Completions: skipped for zsh', |
| 267 | 'Updated ~/.zshrc', |
| 268 | ]); |
| 269 | }).pipe(Effect.provide(layer)); |
| 270 | }); |
| 271 | |
| 272 | it.effect('no detectable shell prints the line to add by hand', () => { |
| 273 | const { layer, stderrLines } = installTest({ env: { SHELL: '/bin/sh' } }); |
| 274 | return Effect.gen(function* () { |
| 275 | yield* installShellIntegration({ completions: false }); |
| 276 | |
| 277 | expect(stderrLines()).toEqual([ |
| 278 | 'warning: Could not detect your shell. Manually add the following to your shell config:', |
| 279 | 'PATH setup:', |
| 280 | `export PATH="${BIN_DIR}:$PATH"`, |
| 281 | ]); |
| 282 | }).pipe(Effect.provide(layer)); |
| 283 | }); |
| 284 | |
| 285 | it.effect('no detectable shell with the bin dir on $PATH has nothing to update', () => { |
| 286 | const { layer, stderrLines } = installTest({ env: { PATH: `/usr/bin:${BIN_DIR}` } }); |
| 287 | return Effect.gen(function* () { |
| 288 | yield* installShellIntegration({ completions: false }); |
| 289 | |
| 290 | expect(stderrLines()).toEqual([ |
| 291 | `PATH: ${BIN_DIR} is already on $PATH in this process, and no shell config file could be identified — nothing to update.`, |
| 292 | ]); |
| 293 | }).pipe(Effect.provide(layer)); |
| 294 | }); |
| 295 | |
| 296 | it.effect.each([ |
| 297 | ['a relative bin dir', './bin', BIN_DIR_NOT_ABSOLUTE_MESSAGE], |
| 298 | ['a bin dir with a PATH delimiter', '/a:/b', BIN_DIR_UNSAFE_MESSAGE], |
| 299 | ['a bin dir that expands in double quotes', '/tmp/x$(evil)', BIN_DIR_UNSAFE_MESSAGE], |
| 300 | ])('%s aborts with ShellSetupAbortError before touching any file', ([, binDir, message]) => { |
| 301 | const { home, layer, stderrLines } = installTest({ |
| 302 | env: { SHELL: '/bin/zsh', COMPOSIO_BIN_DIR: binDir }, |
| 303 | }); |
| 304 | return Effect.gen(function* () { |
| 305 | const error = yield* Effect.flip(installShellIntegration({ completions: false })); |
| 306 | |
| 307 | expect(error).toEqual(new ShellSetupAbortError({ message })); |
| 308 | expect(stderrLines()).toEqual([`error: ${message}`]); |
| 309 | const fs = yield* FileSystem.FileSystem; |
| 310 | expect(yield* fs.exists(path.join(home, '.zshrc'))).toBe(false); |
| 311 | }).pipe(Effect.provide(layer)); |
| 312 | }); |
| 313 | }); |
| 314 | |
| 315 | describe('runInstall', () => { |
| 316 | it.effect('--shell overrides $SHELL and --no-completions wins over --completions', () => { |
| 317 | const { home, layer, stderrLines } = installTest({ env: { SHELL: '/bin/zsh' } }); |
| 318 | return Effect.gen(function* () { |
| 319 | yield* runInstall({ completions: true, noCompletions: true, shell: Option.some('fish') }); |
| 320 | |
| 321 | expect(stderrLines().slice(0, 3)).toEqual([ |
| 322 | 'Detected shell: fish', |
| 323 | `PATH: will add ${BIN_DIR} to $PATH`, |
| 324 | 'Completions: skipped by default (pass --completions to enable)', |
| 325 | ]); |
| 326 | const fs = yield* FileSystem.FileSystem; |
| 327 | expect(yield* fs.exists(path.join(home, '.zshrc'))).toBe(false); |
| 328 | }).pipe(Effect.provide(layer)); |
| 329 | }); |
| 330 | |
| 331 | it.effect('--completions without --no-completions installs them', () => { |
| 332 | const { layer, stderrLines } = installTest({ env: { SHELL: '/bin/bash' } }); |
| 333 | return Effect.gen(function* () { |
| 334 | yield* runInstall({ completions: true, noCompletions: false, shell: Option.none() }); |
| 335 | |
| 336 | expect(stderrLines()[2]).toBe('Completions: will install shell completions'); |
| 337 | }).pipe(Effect.provide(layer)); |
| 338 | }); |
| 339 | }); |
| 340 | |
| 341 | describe('decorated reporter (stderr TTY)', () => { |
| 342 | it.effect( |
| 343 | 'zsh: reports route to the log/note channels with the same content and no plain prefixes', |
| 344 | () => { |
| 345 | const { calls, layer: ui } = decoratedInstallUI(); |
| 346 | const { home, layer, stderrLines } = installTest({ env: { SHELL: '/bin/zsh' }, ui }); |
| 347 | return Effect.gen(function* () { |
| 348 | yield* installShellIntegration({ completions: false }); |
| 349 | |
| 350 | expect(calls.step).toEqual([ |
| 351 | 'Detected shell: zsh', |
| 352 | `PATH: will add ${BIN_DIR} to $PATH`, |
| 353 | 'Completions: skipped for zsh', |
| 354 | ]); |
| 355 | expect(calls.success).toEqual(['Updated ~/.zshrc']); |
| 356 | expect(calls.note).toEqual([['source ~/.zshrc', 'Restart your shell to apply changes']]); |
| 357 | expect(calls.warn).toEqual([]); |
| 358 | expect(calls.error).toEqual([]); |
| 359 | expect(calls.plainError).toEqual([]); |
| 360 | expect(stderrLines()).toEqual([]); |
| 361 | expect(yield* readHomeFile(home, '.zshrc')).toBe( |
| 362 | `\n# Composio CLI\nexport PATH="${BIN_DIR}:$PATH"\n` |
| 363 | ); |
| 364 | }).pipe(Effect.provide(layer)); |
| 365 | } |
| 366 | ); |
| 367 | |
| 368 | it.effect('abort: the failure line routes to log.error without the error: prefix', () => { |
| 369 | const { calls, layer: ui } = decoratedInstallUI(); |
| 370 | const { home, layer } = installTest({ |
| 371 | env: { SHELL: '/bin/zsh', COMPOSIO_BIN_DIR: '/tmp/x$(evil)' }, |
| 372 | ui, |
| 373 | }); |
| 374 | return Effect.gen(function* () { |
| 375 | const error = yield* Effect.flip(installShellIntegration({ completions: false })); |
| 376 | |
| 377 | expect(error).toEqual(new ShellSetupAbortError({ message: BIN_DIR_UNSAFE_MESSAGE })); |
| 378 | expect(calls.error).toEqual([BIN_DIR_UNSAFE_MESSAGE]); |
| 379 | expect(calls.step).toEqual([]); |
| 380 | expect(calls.success).toEqual([]); |
| 381 | expect(calls.warn).toEqual([]); |
| 382 | expect(calls.note).toEqual([]); |
| 383 | expect(calls.plainError).toEqual([]); |
| 384 | const fs = yield* FileSystem.FileSystem; |
| 385 | expect(yield* fs.exists(path.join(home, '.zshrc'))).toBe(false); |
| 386 | }).pipe(Effect.provide(layer)); |
| 387 | }); |
| 388 | }); |