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 * 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 { Effect, Layer, Option } from 'effect'; |
| 5 | import * as FileSystem from 'effect/FileSystem'; |
| 6 | import * as Path from 'effect/Path'; |
| 7 | import path from 'node:path'; |
| 8 | import * as tempy from 'tempy'; |
| 9 | import { |
| 10 | buildShellConfig, |
| 11 | detectShellFromEnv, |
| 12 | isUnsafePath, |
| 13 | pathBlockForShell, |
| 14 | pathFilesForShell, |
| 15 | renderWithHome, |
| 16 | resolveBashLoginFile, |
| 17 | type Shell, |
| 18 | } from 'src/services/shell-config'; |
| 19 | |
| 20 | const HOME = '/home/user'; |
| 21 | const PlatformLive = Layer.mergeAll(BunFileSystem.layer, BunPath.layer); |
| 22 | |
| 23 | describe('detectShellFromEnv', () => { |
| 24 | it.effect.each([ |
| 25 | ['/bin/zsh', Option.some('zsh')], |
| 26 | ['/usr/local/bin/bash', Option.some('bash')], |
| 27 | ['/opt/homebrew/bin/fish', Option.some('fish')], |
| 28 | ['zsh', Option.some('zsh')], |
| 29 | ['/bin/sh', Option.none()], |
| 30 | ['/usr/bin/nu', Option.none()], |
| 31 | ['', Option.none()], |
| 32 | ] as const)('$SHELL=%j detects %s', ([shellEnv, expected]) => |
| 33 | Effect.gen(function* () { |
| 34 | const p = yield* Path.Path; |
| 35 | expect(detectShellFromEnv(p, shellEnv)).toEqual(expected); |
| 36 | }).pipe(Effect.provide(PlatformLive)) |
| 37 | ); |
| 38 | }); |
| 39 | |
| 40 | describe('isUnsafePath', () => { |
| 41 | it.each([ |
| 42 | ['/home/user/.local/bin', false], |
| 43 | ["/Users/o'brien/.local/bin", false], |
| 44 | ['/opt/a;b|c&(d)', false], |
| 45 | ['/tmp/x$(evil)', true], |
| 46 | ['/tmp/`evil`', true], |
| 47 | ['/tmp/"quoted"', true], |
| 48 | ['/tmp/back\\slash', true], |
| 49 | ['/tmp/a:/b', true], |
| 50 | ['/tmp/new\nline', true], |
| 51 | ['/tmp/carriage\rreturn', true], |
| 52 | ])('%j is unsafe: %s', (dir, expected) => { |
| 53 | expect(isUnsafePath(dir)).toBe(expected); |
| 54 | }); |
| 55 | }); |
| 56 | |
| 57 | describe('renderWithHome', () => { |
| 58 | it.each([ |
| 59 | [HOME, '$HOME'], |
| 60 | [`${HOME}/.local/bin`, '$HOME/.local/bin'], |
| 61 | [`${HOME}2/bin`, `${HOME}2/bin`], |
| 62 | ['/usr/local/bin', '/usr/local/bin'], |
| 63 | ])('%j renders as %j', (dir, expected) => { |
| 64 | expect(renderWithHome(dir, HOME)).toBe(expected); |
| 65 | }); |
| 66 | }); |
| 67 | |
| 68 | describe('pathBlockForShell', () => { |
| 69 | it.each<[Shell, string]>([ |
| 70 | ['zsh', '# Composio CLI\nexport PATH="$HOME/.local/bin:$PATH"'], |
| 71 | ['bash', '# Composio CLI\nexport PATH="$HOME/.local/bin:$PATH"'], |
| 72 | ['fish', '# Composio CLI\nset --export PATH "$HOME/.local/bin" $PATH'], |
| 73 | ])('%s writes the managed marker and its PATH assignment', (shell, expected) => { |
| 74 | expect(pathBlockForShell(shell, `${HOME}/.local/bin`, HOME)).toBe(expected); |
| 75 | }); |
| 76 | }); |
| 77 | |
| 78 | describe('pathFilesForShell and buildShellConfig', () => { |
| 79 | it.effect('zsh configures ~/.zshrc and puts completions in the same file', () => |
| 80 | Effect.gen(function* () { |
| 81 | const p = yield* Path.Path; |
| 82 | const pathFiles = pathFilesForShell(p, 'zsh', HOME, undefined); |
| 83 | expect(pathFiles).toEqual([`${HOME}/.zshrc`]); |
| 84 | |
| 85 | const config = buildShellConfig(p, 'zsh', pathFiles, '/opt/bin', Option.none(), HOME); |
| 86 | expect(config.completionFile).toBe(`${HOME}/.zshrc`); |
| 87 | expect(config.completionBlock).toEqual(Option.none()); |
| 88 | }).pipe(Effect.provide(PlatformLive)) |
| 89 | ); |
| 90 | |
| 91 | it.effect('bash configures ~/.bashrc and its login file', () => |
| 92 | Effect.gen(function* () { |
| 93 | const p = yield* Path.Path; |
| 94 | expect(pathFilesForShell(p, 'bash', HOME, `${HOME}/.bash_login`)).toEqual([ |
| 95 | `${HOME}/.bashrc`, |
| 96 | `${HOME}/.bash_login`, |
| 97 | ]); |
| 98 | }).pipe(Effect.provide(PlatformLive)) |
| 99 | ); |
| 100 | |
| 101 | it.effect('fish keeps completions in their own file under a marker', () => |
| 102 | Effect.gen(function* () { |
| 103 | const p = yield* Path.Path; |
| 104 | const pathFiles = pathFilesForShell(p, 'fish', HOME, undefined); |
| 105 | expect(pathFiles).toEqual([`${HOME}/.config/fish/config.fish`]); |
| 106 | |
| 107 | const config = buildShellConfig( |
| 108 | p, |
| 109 | 'fish', |
| 110 | pathFiles, |
| 111 | '/opt/bin', |
| 112 | Option.some('complete -c composio'), |
| 113 | HOME |
| 114 | ); |
| 115 | expect(config.completionFile).toBe(`${HOME}/.config/fish/completions/composio.fish`); |
| 116 | expect(config.completionBlock).toEqual( |
| 117 | Option.some('# Composio CLI completions\ncomplete -c composio') |
| 118 | ); |
| 119 | }).pipe(Effect.provide(PlatformLive)) |
| 120 | ); |
| 121 | }); |
| 122 | |
| 123 | describe('resolveBashLoginFile', () => { |
| 124 | const resolveIn = (existing: ReadonlyArray<string>) => |
| 125 | Effect.gen(function* () { |
| 126 | const fs = yield* FileSystem.FileSystem; |
| 127 | const p = yield* Path.Path; |
| 128 | const home = tempy.temporaryDirectory({ prefix: 'shell-config' }); |
| 129 | yield* Effect.forEach(existing, file => fs.writeFileString(path.join(home, file), '')); |
| 130 | const resolved = yield* resolveBashLoginFile(p, home, fs); |
| 131 | return { ...resolved, file: path.relative(home, resolved.file) }; |
| 132 | }).pipe(Effect.provide(PlatformLive)); |
| 133 | |
| 134 | it.effect.each([ |
| 135 | [[], { file: '.bash_profile', seedsProfilePassthrough: false }], |
| 136 | [['.profile'], { file: '.bash_profile', seedsProfilePassthrough: true }], |
| 137 | [['.bash_login'], { file: '.bash_login', seedsProfilePassthrough: false }], |
| 138 | [['.bash_login', '.profile'], { file: '.bash_login', seedsProfilePassthrough: false }], |
| 139 | [['.bash_profile', '.bash_login'], { file: '.bash_profile', seedsProfilePassthrough: false }], |
| 140 | [['.bash_profile', '.profile'], { file: '.bash_profile', seedsProfilePassthrough: false }], |
| 141 | ] as const)('with %j present resolves %j', ([existing, expected]) => |
| 142 | Effect.gen(function* () { |
| 143 | expect(yield* resolveIn(existing)).toEqual(expected); |
| 144 | }) |
| 145 | ); |
| 146 | }); |