Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 1,311 lines 44.6 KB
Raw
1 import { describe, expect, layer } from '@effect/vitest';
2 import { Effect, Exit, Fiber, Latch, Redacted } from 'effect';
3 import { TestClock } from 'effect/testing';
4 import { ChildProcess, ChildProcessSpawner } from 'effect/process';
5 import { afterEach, vi } from 'vitest';
6 import { SkillInstallError } from 'src/effects/install-skill';
7 import { CommandRunner } from 'src/services/command-runner';
8 import { SetupProcessError } from 'src/services/setup';
9 import { SetupCommandError } from 'src/services/setup-command-error';
10 import { SetupSkillInstaller } from 'src/services/setup-skill-installer';
11 import { cli, MockConsole, TestLive } from 'test/__utils__';
12
13 type AgentHost = 'claude' | 'codex';
14 type MarketplaceState = 'missing' | 'canonical' | 'conflict';
15 type PluginState = 'missing' | 'disabled' | 'enabled';
16
17 interface FakeHostState {
18 available: boolean;
19 marketplace: MarketplaceState;
20 plugin: PluginState;
21 pluginScope: 'user' | 'project';
22 }
23
24 interface HostOutputFormatter {
25 readonly version: string;
26 readonly marketplace: (state: MarketplaceState, source: string) => unknown;
27 readonly plugin: (installed: boolean, enabled: boolean, scope: 'user' | 'project') => unknown;
28 }
29
30 const MARKETPLACE_SOURCES: Readonly<Record<AgentHost, string>> = {
31 claude: 'https://github.com/ComposioHQ/composio-plugin-cc.git',
32 codex: 'ComposioHQ/composio-plugin-openai',
33 };
34
35 const HOST_OUTPUT_FORMATTERS: Readonly<Record<AgentHost, HostOutputFormatter>> = {
36 claude: {
37 version: '2.1.0',
38 marketplace: (state, source) => {
39 if (state === 'missing') return [];
40 return [{ name: 'composio', source: { source: 'github', repo: source } }];
41 },
42 plugin: (installed, enabled, scope) => {
43 if (!installed) return [];
44 return [{ id: 'composio@composio', scope, enabled }];
45 },
46 },
47 codex: {
48 version: 'codex-cli 0.144.1',
49 marketplace: (state, source) => {
50 if (state === 'missing') return { marketplaces: [] };
51 return {
52 marketplaces: [
53 {
54 name: 'composio',
55 marketplaceSource: { sourceType: 'git', source },
56 },
57 ],
58 };
59 },
60 plugin: (installed, enabled) => {
61 if (!installed) return { installed: [], available: [] };
62 return {
63 installed: [{ pluginId: 'composio@composio', installed, enabled }],
64 available: [],
65 };
66 },
67 },
68 };
69
70 const commandParts = (command: ChildProcess.Command): ReadonlyArray<string> => {
71 if (!ChildProcess.isStandardCommand(command)) {
72 throw new Error('Expected a standard command');
73 }
74 return [
75 command.command,
76 ...command.args.map(arg => (Redacted.isRedacted(arg) ? Redacted.value(arg) : arg)),
77 ];
78 };
79
80 const makeRunner = (
81 respond: (parts: ReadonlyArray<string>) => {
82 readonly exitCode?: number;
83 readonly stdout?: string;
84 readonly stderr?: string;
85 }
86 ) =>
87 CommandRunner.of({
88 run: () => Effect.succeed(ChildProcessSpawner.ExitCode(0)),
89 capture: command => {
90 const result = respond(commandParts(command));
91 return Effect.succeed({
92 exitCode: result.exitCode ?? 0,
93 stdout: result.stdout ?? '',
94 stderr: result.stderr ?? '',
95 });
96 },
97 });
98
99 const makeSkillInstaller = (initiallyReady = false, failInstall = false) => {
100 let ready = initiallyReady;
101 const ensureClaudeSkill = () => {
102 if (failInstall) {
103 return Effect.fail(
104 new SkillInstallError({
105 message: 'skill download failed',
106 phase: 'download',
107 })
108 );
109 }
110 return Effect.sync(() => {
111 const changed = !ready;
112 ready = true;
113 return changed;
114 });
115 };
116 return SetupSkillInstaller.of({
117 isClaudeSkillReady: Effect.sync(() => ready),
118 hasManagedClaudeSkill: Effect.sync(() => ready),
119 ensureClaudeSkill: ensureClaudeSkill(),
120 removeClaudeSkill: Effect.sync(() => {
121 const changed = ready;
122 ready = false;
123 return changed;
124 }),
125 });
126 };
127
128 const defaultHostState = (): FakeHostState => ({
129 available: false,
130 marketplace: 'missing',
131 plugin: 'missing',
132 pluginScope: 'user',
133 });
134
135 const makeFakeHosts = (
136 initial: Partial<Record<AgentHost, Partial<FakeHostState>>>,
137 options: {
138 readonly noOp?: boolean;
139 readonly failOn?: string;
140 readonly malformedOn?: string;
141 readonly claudeJsonInspection?: boolean;
142 readonly codexJsonInspection?: boolean;
143 readonly codexVersion?: string;
144 readonly marketplaceSource?: Partial<Record<AgentHost, string>>;
145 } = {}
146 ) => {
147 const state: Record<AgentHost, FakeHostState> = {
148 claude: { ...defaultHostState(), ...initial.claude },
149 codex: { ...defaultHostState(), ...initial.codex },
150 };
151 const commands: string[][] = [];
152
153 const marketplaceOutput = (host: AgentHost): string => {
154 const current = state[host].marketplace;
155 let source = 'someone-else/not-composio';
156 if (current === 'canonical') {
157 source = options.marketplaceSource?.[host] ?? MARKETPLACE_SOURCES[host];
158 }
159 return JSON.stringify(HOST_OUTPUT_FORMATTERS[host].marketplace(current, source));
160 };
161
162 const pluginOutput = (host: AgentHost): string => {
163 const current = state[host].plugin;
164 const installed = current !== 'missing';
165 const enabled = current === 'enabled';
166 return JSON.stringify(
167 HOST_OUTPUT_FORMATTERS[host].plugin(installed, enabled, state[host].pluginScope)
168 );
169 };
170
171 const runner = makeRunner(parts => {
172 commands.push([...parts]);
173 const host = parts[0] as AgentHost;
174 const command = parts.join(' ');
175 if (parts[1] === '--version') {
176 if (!state[host].available) return { exitCode: 127, stderr: 'not found' };
177 return {
178 stdout:
179 host === 'codex' && options.codexVersion
180 ? options.codexVersion
181 : HOST_OUTPUT_FORMATTERS[host].version,
182 };
183 }
184 if (
185 command === `${host} plugin marketplace list --help` ||
186 command === `${host} plugin list --help`
187 ) {
188 const supportsJson =
189 host === 'claude'
190 ? options.claudeJsonInspection !== false
191 : options.codexJsonInspection !== false;
192 return {
193 stdout: supportsJson
194 ? `Usage: ${command.replace(' --help', ' [OPTIONS]')}\n\n --json`
195 : `Usage: ${command.replace(' --help', ' [OPTIONS]')}`,
196 };
197 }
198 if (options.failOn && command.includes(options.failOn)) {
199 return { exitCode: 2, stderr: 'native operation failed' };
200 }
201 if (options.malformedOn && command.includes(options.malformedOn)) {
202 return { stdout: '{not-json' };
203 }
204 if (command === `${host} plugin marketplace list --json`) {
205 return { stdout: marketplaceOutput(host) };
206 }
207 if (command === `${host} plugin list --json`) {
208 return { stdout: pluginOutput(host) };
209 }
210 if (!options.noOp && command.includes('plugin marketplace add')) {
211 state[host].marketplace = 'canonical';
212 }
213 if (
214 !options.noOp &&
215 (command === 'claude plugin uninstall composio@composio --scope user --yes' ||
216 command === 'codex plugin remove composio@composio --json')
217 ) {
218 state[host].plugin = 'missing';
219 return {};
220 }
221 if (
222 !options.noOp &&
223 (command.includes('plugin install') ||
224 command.includes('plugin enable') ||
225 command === 'codex plugin add composio@composio --json')
226 ) {
227 state[host].plugin = 'enabled';
228 state[host].pluginScope = 'user';
229 }
230 return {};
231 });
232
233 return { commands, runner, state };
234 };
235
236 describe('CLI: composio setup', () => {
237 afterEach(() => {
238 vi.restoreAllMocks();
239 vi.unstubAllEnvs();
240 });
241
242 const freshClaude = makeFakeHosts({ claude: { available: true } });
243 layer(
244 TestLive({
245 fixture: 'user-config-example',
246 commandRunner: freshClaude.runner,
247 setupSkillInstaller: makeSkillInstaller(),
248 })
249 )('fresh Claude install', it => {
250 it.effect('uses exact native commands and verifies readiness', () =>
251 Effect.gen(function* () {
252 yield* cli(['setup', '--target', 'claude', '--yes']);
253
254 expect(freshClaude.commands).toContainEqual([
255 'claude',
256 'plugin',
257 'marketplace',
258 'add',
259 'https://github.com/ComposioHQ/composio-plugin-cc.git',
260 '--scope',
261 'user',
262 ]);
263 expect(freshClaude.commands).toContainEqual([
264 'claude',
265 'plugin',
266 'install',
267 'composio@composio',
268 '--scope',
269 'user',
270 ]);
271 expect(freshClaude.state.claude).toMatchObject({
272 marketplace: 'canonical',
273 plugin: 'enabled',
274 });
275
276 const output = (yield* MockConsole.getLines()).join('\n');
277 expect(output).toContain('Claude Code detected.');
278 expect(output).toContain(
279 'Successfully installed and enabled the Composio plugin for Claude Code.'
280 );
281 })
282 );
283 });
284
285 const existingCodex = makeFakeHosts({
286 codex: { available: true, marketplace: 'canonical', plugin: 'enabled' },
287 });
288 layer(TestLive({ commandRunner: existingCodex.runner }))('existing Codex install', it => {
289 it.effect('is idempotent', () =>
290 Effect.gen(function* () {
291 yield* cli(['setup', '--target', 'codex', '--yes']);
292
293 expect(existingCodex.commands.some(parts => parts.includes('add'))).toBe(false);
294 })
295 );
296 });
297
298 const existingClaudeWithStaleSkill = makeFakeHosts({
299 claude: { available: true, marketplace: 'canonical', plugin: 'enabled' },
300 });
301 let staleSkillRepaired = false;
302 let staleSkillReady = false;
303 const staleSkillInstaller = SetupSkillInstaller.of({
304 isClaudeSkillReady: Effect.sync(() => staleSkillReady),
305 hasManagedClaudeSkill: Effect.succeed(true),
306 ensureClaudeSkill: Effect.sync(() => {
307 staleSkillRepaired = true;
308 staleSkillReady = true;
309 return true;
310 }),
311 removeClaudeSkill: Effect.sync(() => {
312 const changed = staleSkillReady;
313 staleSkillReady = false;
314 return changed;
315 }),
316 });
317 layer(
318 TestLive({
319 commandRunner: existingClaudeWithStaleSkill.runner,
320 setupSkillInstaller: staleSkillInstaller,
321 })
322 )('existing Claude plugin with stale skill', it => {
323 it.effect('requires approval, then repairs the skill without extra output', () =>
324 Effect.gen(function* () {
325 const withoutApproval = yield* Effect.exit(cli(['setup', '--target', 'claude']));
326 expect(Exit.isFailure(withoutApproval)).toBe(true);
327 expect(staleSkillRepaired).toBe(false);
328
329 yield* cli(['setup', '--target', 'claude', '--yes']);
330
331 expect(staleSkillRepaired).toBe(true);
332 const output = (yield* MockConsole.getLines()).join('\n');
333 expect(output).toContain(
334 'The Composio plugin for Claude Code is already installed and enabled.'
335 );
336 expect(output).not.toContain('skill');
337 })
338 );
339 });
340
341 describe.each([
342 {
343 host: 'claude',
344 repairCommand: ['claude', 'plugin', 'enable', 'composio@composio', '--scope', 'user'],
345 },
346 {
347 host: 'codex',
348 repairCommand: ['codex', 'plugin', 'add', 'composio@composio', '--json'],
349 },
350 ] as const)('disabled $host plugin', ({ host, repairCommand }) => {
351 const disabled = makeFakeHosts({
352 [host]: { available: true, marketplace: 'canonical', plugin: 'disabled' },
353 });
354 layer(
355 TestLive({
356 commandRunner: disabled.runner,
357 setupSkillInstaller: makeSkillInstaller(true),
358 })
359 )(it => {
360 it.effect('repairs the plugin through the native path', () =>
361 Effect.gen(function* () {
362 yield* cli(['setup', '--target', host, '--yes']);
363
364 expect(disabled.state[host].plugin).toBe('enabled');
365 expect(disabled.commands).toContainEqual(repairCommand);
366 })
367 );
368 });
369 });
370
371 const freshCodex = makeFakeHosts({ codex: { available: true } });
372 layer(TestLive({ commandRunner: freshCodex.runner }))('fresh Codex install', it => {
373 it.effect('uses the public OpenAI marketplace and exact native commands', () =>
374 Effect.gen(function* () {
375 yield* cli(['setup', '--target', 'codex', '--yes']);
376
377 expect(freshCodex.commands).toContainEqual([
378 'codex',
379 'plugin',
380 'marketplace',
381 'add',
382 'https://github.com/ComposioHQ/composio-plugin-openai.git',
383 '--json',
384 ]);
385 expect(freshCodex.commands).toContainEqual([
386 'codex',
387 'plugin',
388 'add',
389 'composio@composio',
390 '--json',
391 ]);
392 })
393 );
394 });
395
396 const autoClaude = makeFakeHosts({ claude: { available: true } });
397 layer(
398 TestLive({
399 commandRunner: autoClaude.runner,
400 setupSkillInstaller: makeSkillInstaller(),
401 })
402 )('automatic setup with one host', it => {
403 it.effect('installs only the detected host', () =>
404 Effect.gen(function* () {
405 yield* cli(['setup', '--target', 'auto', '--yes']);
406
407 expect(autoClaude.commands.some(parts => parts[0] === 'claude')).toBe(true);
408 expect(
409 autoClaude.commands.some(parts => parts[0] === 'codex' && parts[1] !== '--version')
410 ).toBe(false);
411
412 const output = (yield* MockConsole.getLines()).join('\n');
413 expect(output).toContain('Claude Code detected.');
414 expect(output).toContain('Codex not detected.');
415 })
416 );
417 });
418
419 const autoCodex = makeFakeHosts({ codex: { available: true } });
420 layer(TestLive({ commandRunner: autoCodex.runner }))(
421 'automatic setup with only supported Codex',
422 it => {
423 it.effect('installs Codex when Claude is not detected', () =>
424 Effect.gen(function* () {
425 yield* cli(['setup', '--target', 'auto', '--yes']);
426
427 expect(autoCodex.state.codex).toMatchObject({
428 marketplace: 'canonical',
429 plugin: 'enabled',
430 });
431 const output = (yield* MockConsole.getLines()).join('\n');
432 expect(output).toContain('Codex detected.');
433 expect(output).toContain('Claude Code not detected.');
434 })
435 );
436 }
437 );
438
439 const autoBoth = makeFakeHosts({
440 claude: { available: true, marketplace: 'canonical', plugin: 'enabled' },
441 codex: { available: true, marketplace: 'canonical', plugin: 'enabled' },
442 });
443 layer(
444 TestLive({
445 commandRunner: autoBoth.runner,
446 setupSkillInstaller: makeSkillInstaller(true),
447 })
448 )('automatic setup with both hosts', it => {
449 it.effect('selects both detected hosts and reports only plugin status', () =>
450 Effect.gen(function* () {
451 yield* cli(['setup', '--target', 'auto']);
452
453 expect(autoBoth.commands.some(parts => parts[0] === 'claude')).toBe(true);
454 expect(autoBoth.commands.some(parts => parts[0] === 'codex')).toBe(true);
455
456 const output = (yield* MockConsole.getLines()).join('\n');
457 expect(output).toContain('Claude Code and Codex detected.');
458 expect(output).toContain(
459 'The Composio plugin for Claude Code is already installed and enabled.'
460 );
461 expect(output).toContain('The Composio plugin for Codex is already installed and enabled.');
462 expect(output).not.toContain('composio-cli skill');
463 })
464 );
465 });
466
467 const autoClaudeWithLegacyCodex = makeFakeHosts(
468 {
469 claude: { available: true },
470 codex: { available: true },
471 },
472 { codexJsonInspection: false, codexVersion: 'codex-cli 0.137.0' }
473 );
474 layer(
475 TestLive({
476 commandRunner: autoClaudeWithLegacyCodex.runner,
477 setupSkillInstaller: makeSkillInstaller(),
478 })
479 )('automatic setup with Claude and unsupported Codex', it => {
480 it.effect('installs Claude and reports that Codex was skipped', () =>
481 Effect.gen(function* () {
482 yield* cli(['setup', '--target', 'auto', '--yes']);
483
484 expect(autoClaudeWithLegacyCodex.state.claude).toMatchObject({
485 marketplace: 'canonical',
486 plugin: 'enabled',
487 });
488 expect(autoClaudeWithLegacyCodex.state.codex).toMatchObject({
489 marketplace: 'missing',
490 plugin: 'missing',
491 });
492 expect(
493 autoClaudeWithLegacyCodex.commands.some(
494 parts => parts[0] === 'codex' && parts.includes('--json')
495 )
496 ).toBe(false);
497
498 const output = (yield* MockConsole.getLines()).join('\n');
499 expect(output).toContain('Claude Code and Codex detected.');
500 expect(output).toContain('Codex plugin setup skipped.');
501 expect(output).toContain('requires Codex 0.139.0 or newer');
502 expect(output).toContain(
503 'Successfully installed and enabled the Composio plugin for Claude Code.'
504 );
505 })
506 );
507 });
508
509 const autoCodexWithLegacyClaude = makeFakeHosts(
510 {
511 claude: { available: true },
512 codex: { available: true },
513 },
514 { claudeJsonInspection: false }
515 );
516 layer(TestLive({ commandRunner: autoCodexWithLegacyClaude.runner }))(
517 'automatic setup with unsupported Claude and Codex',
518 it => {
519 it.effect('installs Codex and reports that Claude was skipped', () =>
520 Effect.gen(function* () {
521 yield* cli(['setup', '--target', 'auto', '--yes']);
522
523 expect(autoCodexWithLegacyClaude.state.claude).toMatchObject({
524 marketplace: 'missing',
525 plugin: 'missing',
526 });
527 expect(autoCodexWithLegacyClaude.state.codex).toMatchObject({
528 marketplace: 'canonical',
529 plugin: 'enabled',
530 });
531 expect(
532 autoCodexWithLegacyClaude.commands.some(
533 parts => parts[0] === 'claude' && parts.includes('--json')
534 )
535 ).toBe(false);
536
537 const output = (yield* MockConsole.getLines()).join('\n');
538 expect(output).toContain('Claude Code and Codex detected.');
539 expect(output).toContain('Claude Code plugin setup skipped.');
540 expect(output).toContain('Run `claude update`');
541 expect(output).toContain(
542 'Successfully installed and enabled the Composio plugin for Codex.'
543 );
544 })
545 );
546 }
547 );
548
549 const allTargets = makeFakeHosts({
550 claude: { available: true },
551 codex: { available: true },
552 });
553 layer(
554 TestLive({
555 commandRunner: allTargets.runner,
556 setupSkillInstaller: makeSkillInstaller(),
557 })
558 )('explicit all-host setup', it => {
559 it.effect('configures both supported hosts', () =>
560 Effect.gen(function* () {
561 yield* cli(['setup', '--target', 'all', '--yes']);
562
563 expect(allTargets.state.claude).toMatchObject({
564 marketplace: 'canonical',
565 plugin: 'enabled',
566 });
567 expect(allTargets.state.codex).toMatchObject({
568 marketplace: 'canonical',
569 plugin: 'enabled',
570 });
571 })
572 );
573 });
574
575 const partialAllTargets = makeFakeHosts({ claude: { available: true } });
576 layer(TestLive({ commandRunner: partialAllTargets.runner }))(
577 'explicit all-host setup with a missing host',
578 it => {
579 it.effect('fails before mutating the detected host', () =>
580 Effect.gen(function* () {
581 const exit = yield* Effect.exit(cli(['setup', '--target', 'all', '--yes']));
582
583 expect(Exit.isFailure(exit)).toBe(true);
584 expect(partialAllTargets.state.claude).toMatchObject({
585 marketplace: 'missing',
586 plugin: 'missing',
587 });
588 expect(
589 partialAllTargets.commands.some(parts =>
590 ['add', 'install', 'enable'].some(operation => parts.includes(operation))
591 )
592 ).toBe(false);
593 })
594 );
595 }
596 );
597
598 const allWithLegacyCodex = makeFakeHosts(
599 {
600 claude: { available: true },
601 codex: { available: true },
602 },
603 { codexJsonInspection: false, codexVersion: 'codex-cli 0.137.0' }
604 );
605 layer(
606 TestLive({
607 commandRunner: allWithLegacyCodex.runner,
608 setupSkillInstaller: makeSkillInstaller(),
609 })
610 )('explicit all-host setup with unsupported Codex', it => {
611 it.effect('fails before mutating Claude', () =>
612 Effect.gen(function* () {
613 const exit = yield* Effect.exit(cli(['setup', '--target', 'all', '--yes']));
614
615 expect(Exit.isFailure(exit)).toBe(true);
616 expect(allWithLegacyCodex.state.claude).toMatchObject({
617 marketplace: 'missing',
618 plugin: 'missing',
619 });
620 })
621 );
622 });
623
624 const uninstallBoth = makeFakeHosts({
625 claude: { available: true, marketplace: 'canonical', plugin: 'enabled' },
626 codex: { available: true, marketplace: 'canonical', plugin: 'enabled' },
627 });
628 layer(
629 TestLive({
630 commandRunner: uninstallBoth.runner,
631 setupSkillInstaller: makeSkillInstaller(true),
632 })
633 )('uninstall from both hosts', it => {
634 it.effect('uses native removal commands and is idempotent', () =>
635 Effect.gen(function* () {
636 yield* cli(['setup', '--uninstall', '--target', 'all', '--yes']);
637 yield* cli(['setup', '--uninstall', '--target', 'all', '--yes']);
638
639 expect(uninstallBoth.commands).toContainEqual([
640 'claude',
641 'plugin',
642 'uninstall',
643 'composio@composio',
644 '--scope',
645 'user',
646 '--yes',
647 ]);
648 expect(uninstallBoth.commands).toContainEqual([
649 'codex',
650 'plugin',
651 'remove',
652 'composio@composio',
653 '--json',
654 ]);
655 expect(
656 uninstallBoth.commands.filter(
657 parts =>
658 parts.join(' ') === 'claude plugin uninstall composio@composio --scope user --yes'
659 )
660 ).toHaveLength(1);
661 expect(
662 uninstallBoth.commands.filter(
663 parts => parts.join(' ') === 'codex plugin remove composio@composio --json'
664 )
665 ).toHaveLength(1);
666 expect(uninstallBoth.state.claude.plugin).toBe('missing');
667 expect(uninstallBoth.state.codex.plugin).toBe('missing');
668
669 const output = (yield* MockConsole.getLines()).join('\n');
670 expect(output).toContain('Successfully uninstalled the Composio plugin for Claude Code.');
671 expect(output).toContain('Successfully uninstalled the Composio plugin for Codex.');
672 expect(output).not.toContain('skill');
673 })
674 );
675 });
676
677 const uninstallMissing = makeFakeHosts({
678 claude: { available: true, marketplace: 'canonical', plugin: 'missing' },
679 codex: { available: true, marketplace: 'canonical', plugin: 'missing' },
680 });
681 layer(TestLive({ commandRunner: uninstallMissing.runner }))(
682 'uninstall when plugins are absent',
683 it => {
684 it.effect('reports the idempotent state without requiring approval', () =>
685 Effect.gen(function* () {
686 yield* cli(['setup', '--uninstall', '--target', 'all']);
687
688 const output = (yield* MockConsole.getLines()).join('\n');
689 expect(output).toContain('The Composio plugin for Claude Code is not installed.');
690 expect(output).toContain('The Composio plugin for Codex is not installed.');
691 })
692 );
693 }
694 );
695
696 const orphanedClaudeSkill = makeFakeHosts({
697 claude: { available: true, marketplace: 'canonical', plugin: 'missing' },
698 });
699 let managedClaudeSkill = true;
700 layer(
701 TestLive({
702 commandRunner: orphanedClaudeSkill.runner,
703 setupSkillInstaller: SetupSkillInstaller.of({
704 isClaudeSkillReady: Effect.sync(() => managedClaudeSkill),
705 hasManagedClaudeSkill: Effect.sync(() => managedClaudeSkill),
706 ensureClaudeSkill: Effect.succeed(false),
707 removeClaudeSkill: Effect.sync(() => {
708 const changed = managedClaudeSkill;
709 managedClaudeSkill = false;
710 return changed;
711 }),
712 }),
713 })
714 )('uninstall with an orphaned managed Claude skill', it => {
715 it.effect('requires approval before removing the managed skill', () =>
716 Effect.gen(function* () {
717 const withoutApproval = yield* Effect.exit(
718 cli(['setup', '--uninstall', '--target', 'claude'])
719 );
720 expect(Exit.isFailure(withoutApproval)).toBe(true);
721 expect(managedClaudeSkill).toBe(true);
722
723 yield* cli(['setup', '--uninstall', '--target', 'claude', '--yes']);
724 expect(managedClaudeSkill).toBe(false);
725 })
726 );
727 });
728
729 const uninstallWithUnmanagedClaudeSkill = makeFakeHosts({
730 claude: { available: true, marketplace: 'canonical', plugin: 'enabled' },
731 });
732 layer(
733 TestLive({
734 commandRunner: uninstallWithUnmanagedClaudeSkill.runner,
735 setupSkillInstaller: SetupSkillInstaller.of({
736 isClaudeSkillReady: Effect.succeed(true),
737 hasManagedClaudeSkill: Effect.succeed(false),
738 ensureClaudeSkill: Effect.succeed(false),
739 removeClaudeSkill: Effect.succeed(false),
740 }),
741 })
742 )('uninstall with an unmanaged Claude skill', it => {
743 it.effect('preserves the skill without treating plugin removal as a failure', () =>
744 Effect.gen(function* () {
745 yield* cli(['setup', '--uninstall', '--target', 'claude', '--yes']);
746
747 expect(uninstallWithUnmanagedClaudeSkill.state.claude.plugin).toBe('missing');
748 const output = (yield* MockConsole.getLines()).join('\n');
749 expect(output).toContain('Successfully uninstalled the Composio plugin for Claude Code.');
750 })
751 );
752 });
753
754 const nonInteractiveUninstall = makeFakeHosts({
755 claude: { available: true, marketplace: 'canonical', plugin: 'enabled' },
756 });
757 layer(
758 TestLive({
759 commandRunner: nonInteractiveUninstall.runner,
760 setupSkillInstaller: makeSkillInstaller(true),
761 })
762 )('non-interactive uninstall approval', it => {
763 it.effect('requires --yes before removing an installed plugin', () =>
764 Effect.gen(function* () {
765 const exit = yield* Effect.exit(cli(['setup', '--uninstall', '--target', 'claude']));
766
767 expect(Exit.isFailure(exit)).toBe(true);
768 expect(nonInteractiveUninstall.commands.some(parts => parts.includes('uninstall'))).toBe(
769 false
770 );
771 })
772 );
773 });
774
775 const conflictingUninstall = makeFakeHosts({
776 claude: { available: true, marketplace: 'conflict', plugin: 'enabled' },
777 });
778 layer(
779 TestLive({
780 commandRunner: conflictingUninstall.runner,
781 setupSkillInstaller: makeSkillInstaller(true),
782 })
783 )('uninstall with a conflicting marketplace', it => {
784 it.effect('removes the installed plugin without replacing the marketplace', () =>
785 Effect.gen(function* () {
786 yield* cli(['setup', '--uninstall', '--target', 'claude', '--yes']);
787
788 expect(conflictingUninstall.state.claude.plugin).toBe('missing');
789 expect(conflictingUninstall.state.claude.marketplace).toBe('conflict');
790 })
791 );
792 });
793
794 const failedUninstall = makeFakeHosts(
795 { claude: { available: true, marketplace: 'canonical', plugin: 'enabled' } },
796 { failOn: 'plugin uninstall' }
797 );
798 layer(
799 TestLive({
800 commandRunner: failedUninstall.runner,
801 setupSkillInstaller: makeSkillInstaller(true),
802 })
803 )('native uninstall failure', it => {
804 it.effect('fails without reporting the plugin as removed', () =>
805 Effect.gen(function* () {
806 const exit = yield* Effect.exit(
807 cli(['setup', '--uninstall', '--target', 'claude', '--yes'])
808 );
809
810 expect(Exit.isFailure(exit)).toBe(true);
811 expect(failedUninstall.state.claude.plugin).toBe('enabled');
812 const output = (yield* MockConsole.getLines()).join('\n');
813 expect(output).not.toContain('Successfully uninstalled');
814 })
815 );
816 });
817
818 const projectScopedClaude = makeFakeHosts({
819 claude: {
820 available: true,
821 marketplace: 'canonical',
822 plugin: 'enabled',
823 pluginScope: 'project',
824 },
825 });
826 layer(
827 TestLive({
828 commandRunner: projectScopedClaude.runner,
829 setupSkillInstaller: makeSkillInstaller(true),
830 })
831 )('project-scoped Claude plugin', it => {
832 it.effect('installs the required user-scoped plugin', () =>
833 Effect.gen(function* () {
834 yield* cli(['setup', '--target', 'claude', '--yes']);
835
836 expect(projectScopedClaude.commands).toContainEqual([
837 'claude',
838 'plugin',
839 'install',
840 'composio@composio',
841 '--scope',
842 'user',
843 ]);
844 expect(projectScopedClaude.state.claude.pluginScope).toBe('user');
845 })
846 );
847 });
848
849 const missingMarketplaceWithPlugin = makeFakeHosts({
850 claude: { available: true, marketplace: 'missing', plugin: 'enabled' },
851 });
852 layer(
853 TestLive({
854 commandRunner: missingMarketplaceWithPlugin.runner,
855 setupSkillInstaller: makeSkillInstaller(true),
856 })
857 )('plugin from a removed marketplace', it => {
858 it.effect('reinstalls after restoring the canonical marketplace', () =>
859 Effect.gen(function* () {
860 yield* cli(['setup', '--target', 'claude', '--yes']);
861
862 expect(missingMarketplaceWithPlugin.commands).toContainEqual([
863 'claude',
864 'plugin',
865 'install',
866 'composio@composio',
867 '--scope',
868 'user',
869 ]);
870 })
871 );
872 });
873
874 const unavailable = makeFakeHosts({});
875 layer(TestLive({ commandRunner: unavailable.runner }))('no supported host', it => {
876 it.effect('fails automatic setup', () =>
877 Effect.gen(function* () {
878 const exit = yield* Effect.exit(cli(['setup', '--target', 'auto', '--yes']));
879 expect(Exit.isFailure(exit)).toBe(true);
880 })
881 );
882
883 it.effect('can skip automatic setup for installer integration', () =>
884 Effect.gen(function* () {
885 const exit = yield* Effect.exit(
886 cli(['setup', '--target', 'auto', '--yes', '--if-present'])
887 );
888 expect(Exit.isSuccess(exit)).toBe(true);
889
890 const output = (yield* MockConsole.getLines()).join('\n');
891 expect(output).toContain('Claude Code and Codex not detected.');
892 })
893 );
894
895 it.effect('does not skip an explicitly requested missing host', () =>
896 Effect.gen(function* () {
897 const failure = yield* cli(['setup', '--target', 'claude', '--yes', '--if-present']).pipe(
898 Effect.flip
899 );
900 expect(failure).toMatchObject({
901 message: expect.stringContaining('claude is not installed or not available on PATH'),
902 });
903 })
904 );
905 });
906
907 const conflict = makeFakeHosts({
908 claude: { available: true, marketplace: 'conflict' },
909 });
910 layer(TestLive({ commandRunner: conflict.runner }))('marketplace source conflict', it => {
911 it.effect('fails safely instead of replacing the marketplace', () =>
912 Effect.gen(function* () {
913 const exit = yield* Effect.exit(cli(['setup', '--target', 'claude', '--yes']));
914 expect(Exit.isFailure(exit)).toBe(true);
915 expect(conflict.state.claude.marketplace).toBe('conflict');
916 })
917 );
918 });
919
920 describe.each([
921 'http://github.com/ComposioHQ/composio-plugin-cc.git',
922 'https://token@github.com/ComposioHQ/composio-plugin-cc.git',
923 'https://github.com/ComposioHQ/composio-plugin-cc.git?token=secret',
924 ])('unsafe marketplace source: %s', unsafeSource => {
925 const unsafeMarketplace = makeFakeHosts(
926 { claude: { available: true, marketplace: 'canonical' } },
927 { marketplaceSource: { claude: unsafeSource } }
928 );
929 layer(TestLive({ commandRunner: unsafeMarketplace.runner }))(it => {
930 it.effect('rejects a non-canonical GitHub URL', () =>
931 Effect.gen(function* () {
932 const exit = yield* Effect.exit(cli(['setup', '--target', 'claude', '--yes']));
933
934 expect(Exit.isFailure(exit)).toBe(true);
935 expect(
936 unsafeMarketplace.commands.some(parts =>
937 ['add', 'install', 'enable'].some(operation => parts.includes(operation))
938 )
939 ).toBe(false);
940 })
941 );
942 });
943 });
944
945 const failedInspection = makeFakeHosts(
946 { claude: { available: true } },
947 { failOn: 'plugin list --json' }
948 );
949 layer(TestLive({ commandRunner: failedInspection.runner }))('failed native inspection', it => {
950 it.effect('fails before running setup mutations', () =>
951 Effect.gen(function* () {
952 const exit = yield* Effect.exit(cli(['setup', '--target', 'claude', '--yes']));
953
954 expect(Exit.isFailure(exit)).toBe(true);
955 expect(
956 failedInspection.commands.some(parts =>
957 ['add', 'install', 'enable'].some(operation => parts.includes(operation))
958 )
959 ).toBe(false);
960 })
961 );
962 });
963
964 const malformedInspection = makeFakeHosts(
965 { codex: { available: true } },
966 { malformedOn: 'plugin marketplace list --json' }
967 );
968
969 const legacyCodex = makeFakeHosts(
970 { codex: { available: true } },
971 { codexJsonInspection: false, codexVersion: 'codex-cli 0.137.0' }
972 );
973
974 const legacyClaude = makeFakeHosts(
975 { claude: { available: true } },
976 { claudeJsonInspection: false }
977 );
978 layer(TestLive({ commandRunner: legacyClaude.runner }))(
979 'Claude without JSON plugin inspection',
980 it => {
981 it.effect('fails automatic setup with actionable update instructions', () =>
982 Effect.gen(function* () {
983 const failure = yield* cli(['setup', '--target', 'auto', '--yes']).pipe(Effect.flip);
984
985 expect(failure).toMatchObject({
986 message: expect.stringContaining('requires JSON plugin inspection'),
987 });
988 expect(failure).toMatchObject({ message: expect.stringContaining('claude update') });
989 expect(legacyClaude.commands.some(parts => parts.includes('--json'))).toBe(false);
990 expect(
991 legacyClaude.commands.some(parts =>
992 ['add', 'install', 'enable', 'uninstall'].some(operation => parts.includes(operation))
993 )
994 ).toBe(false);
995 })
996 );
997 }
998 );
999 layer(TestLive({ commandRunner: legacyCodex.runner }))(
1000 'Codex without JSON marketplace inspection',
1001 it => {
1002 it.effect('fails automatic setup when no supported host remains', () =>
1003 Effect.gen(function* () {
1004 const failure = yield* cli(['setup', '--target', 'auto', '--yes']).pipe(Effect.flip);
1005
1006 expect(failure).toMatchObject({
1007 message: expect.stringContaining('requires Codex 0.139.0 or newer'),
1008 });
1009 })
1010 );
1011
1012 it.effect('skips cleanly for installer integration when no supported host remains', () =>
1013 Effect.gen(function* () {
1014 const exit = yield* Effect.exit(
1015 cli(['setup', '--target', 'auto', '--yes', '--if-present'])
1016 );
1017
1018 expect(Exit.isSuccess(exit)).toBe(true);
1019 const output = (yield* MockConsole.getLines()).join('\n');
1020 expect(output).toContain('Codex plugin setup skipped.');
1021 expect(output).toContain('No supported agent host detected; plugin setup skipped.');
1022 expect(legacyCodex.commands.some(parts => parts.includes('--json'))).toBe(false);
1023 })
1024 );
1025
1026 it.effect('fails with an actionable upgrade message before running setup mutations', () =>
1027 Effect.gen(function* () {
1028 const failure = yield* cli(['setup', '--target', 'codex', '--yes']).pipe(Effect.flip);
1029
1030 expect(failure).toMatchObject({
1031 message: expect.stringContaining('requires Codex 0.139.0 or newer'),
1032 });
1033 expect(failure).toMatchObject({ message: expect.stringContaining('codex update') });
1034 expect(failure).not.toMatchObject({
1035 message: expect.stringContaining("unexpected argument '--json'"),
1036 });
1037 expect(legacyCodex.commands.some(parts => parts.includes('--json'))).toBe(false);
1038 expect(
1039 legacyCodex.commands.some(parts =>
1040 ['add', 'install', 'enable', 'remove'].some(operation => parts.includes(operation))
1041 )
1042 ).toBe(false);
1043 })
1044 );
1045 }
1046 );
1047 layer(TestLive({ commandRunner: malformedInspection.runner }))(
1048 'malformed native inspection',
1049 it => {
1050 it.effect('fails before running setup mutations', () =>
1051 Effect.gen(function* () {
1052 const exit = yield* Effect.exit(cli(['setup', '--target', 'codex', '--yes']));
1053
1054 expect(Exit.isFailure(exit)).toBe(true);
1055 expect(malformedInspection.commands.some(parts => parts.includes('add'))).toBe(false);
1056 })
1057 );
1058 }
1059 );
1060
1061 const preflightConflict = makeFakeHosts({
1062 claude: { available: true },
1063 codex: { available: true, marketplace: 'conflict' },
1064 });
1065 layer(
1066 TestLive({
1067 commandRunner: preflightConflict.runner,
1068 setupSkillInstaller: makeSkillInstaller(),
1069 })
1070 )('multi-host preflight', it => {
1071 it.effect('does not mutate an earlier host when a later host conflicts', () =>
1072 Effect.gen(function* () {
1073 const exit = yield* Effect.exit(cli(['setup', '--target', 'all', '--yes']));
1074
1075 expect(Exit.isFailure(exit)).toBe(true);
1076 expect(preflightConflict.state.claude).toMatchObject({
1077 marketplace: 'missing',
1078 plugin: 'missing',
1079 });
1080 })
1081 );
1082 });
1083
1084 const laterMutationFailure = makeFakeHosts(
1085 {
1086 claude: { available: true },
1087 codex: { available: true },
1088 },
1089 { failOn: 'codex plugin add composio@composio --json' }
1090 );
1091 layer(
1092 TestLive({
1093 commandRunner: laterMutationFailure.runner,
1094 setupSkillInstaller: makeSkillInstaller(),
1095 })
1096 )('multi-host mutation failure', it => {
1097 it.effect('reports hosts completed before a later target fails', () =>
1098 Effect.gen(function* () {
1099 const failure = yield* cli(['setup', '--target', 'all', '--yes']).pipe(Effect.flip);
1100
1101 expect(failure).toMatchObject({
1102 message: expect.stringContaining(
1103 'Setup completed for claude before a later target failed'
1104 ),
1105 });
1106 expect(laterMutationFailure.state.claude).toMatchObject({
1107 marketplace: 'canonical',
1108 plugin: 'enabled',
1109 });
1110 })
1111 );
1112 });
1113
1114 const nonInteractive = makeFakeHosts({ claude: { available: true } });
1115 layer(TestLive({ commandRunner: nonInteractive.runner }))('non-interactive approval', it => {
1116 it.effect('inspects first but requires --yes before mutating hosts', () =>
1117 Effect.gen(function* () {
1118 const exit = yield* Effect.exit(cli(['setup', '--target', 'claude']));
1119
1120 expect(Exit.isFailure(exit)).toBe(true);
1121 expect(nonInteractive.commands).toContainEqual(['claude', '--version']);
1122 expect(nonInteractive.commands).toContainEqual(['claude', 'plugin', 'list', '--json']);
1123 expect(
1124 nonInteractive.commands.some(parts =>
1125 ['add', 'install', 'enable'].some(operation => parts.includes(operation))
1126 )
1127 ).toBe(false);
1128 })
1129 );
1130 });
1131
1132 // Opens once setup reaches the host command. The command path does real
1133 // file I/O first (config, analytics state), so a single yield is not enough
1134 // to guarantee the 2-minute timeout has been armed on the TestClock before
1135 // the clock is advanced; advancing too early leaves the sleep pending
1136 // forever and the test hits vitest's own timeout instead.
1137 const hostCommandReached = Latch.makeUnsafe(false);
1138 const hangingRunner = CommandRunner.of({
1139 run: () => Effect.succeed(ChildProcessSpawner.ExitCode(0)),
1140 capture: () => Effect.andThen(hostCommandReached.open, Effect.never),
1141 });
1142 layer(TestLive({ commandRunner: hangingRunner }))('hung native host', it => {
1143 it.effect('times out instead of blocking setup forever', () =>
1144 Effect.gen(function* () {
1145 const fiber = yield* cli(['setup', '--target', 'claude', '--yes']).pipe(
1146 Effect.flip,
1147 Effect.forkChild
1148 );
1149 yield* hostCommandReached.await;
1150 yield* Effect.yieldNow;
1151 yield* TestClock.adjust('2 minutes');
1152 const failure = yield* Fiber.join(fiber);
1153
1154 expect(failure).toMatchObject({
1155 message: expect.stringContaining(
1156 'The `claude --version` command timed out after 2 minutes'
1157 ),
1158 });
1159 expect(failure).not.toMatchObject({
1160 message: expect.stringContaining('not installed or not available on PATH'),
1161 });
1162 })
1163 );
1164 });
1165
1166 const noOp = makeFakeHosts({ claude: { available: true } }, { noOp: true });
1167 layer(TestLive({ commandRunner: noOp.runner, setupSkillInstaller: makeSkillInstaller() }))(
1168 'post-install verification',
1169 it => {
1170 it.effect('fails when successful commands do not change native state', () =>
1171 Effect.gen(function* () {
1172 const exit = yield* Effect.exit(cli(['setup', '--target', 'claude', '--yes']));
1173 expect(Exit.isFailure(exit)).toBe(true);
1174 })
1175 );
1176 }
1177 );
1178
1179 const failedInstall = makeFakeHosts(
1180 { claude: { available: true } },
1181 { failOn: 'plugin install' }
1182 );
1183 layer(TestLive({ commandRunner: failedInstall.runner }))('native install failure', it => {
1184 it.effect('preserves the structured setup process failure as the command cause', () =>
1185 Effect.gen(function* () {
1186 const failure = yield* cli(['setup', '--target', 'claude', '--yes']).pipe(Effect.flip);
1187
1188 expect(failure).toBeInstanceOf(SetupCommandError);
1189 expect(failure).toMatchObject({ operation: 'setup', cause: expect.any(SetupProcessError) });
1190 expect(failure).toMatchObject({ cause: { target: 'claude', stage: 'mutate' } });
1191 })
1192 );
1193 });
1194
1195 const skillFailure = makeFakeHosts({
1196 claude: { available: true, marketplace: 'canonical', plugin: 'enabled' },
1197 });
1198 layer(
1199 TestLive({
1200 commandRunner: skillFailure.runner,
1201 setupSkillInstaller: makeSkillInstaller(false, true),
1202 })
1203 )('skill install failure', it => {
1204 it.effect('fails instead of reporting readiness', () =>
1205 Effect.gen(function* () {
1206 const exit = yield* Effect.exit(cli(['setup', '--target', 'claude', '--yes']));
1207 expect(Exit.isFailure(exit)).toBe(true);
1208 })
1209 );
1210 });
1211 });
1212
1213 const REASON_CODE_FIXTURES = {
1214 'only Claude Code installed': () => makeFakeHosts({ claude: { available: true } }),
1215 'no host installed': () => makeFakeHosts({}),
1216 'unsupported Codex only': () =>
1217 makeFakeHosts({ codex: { available: true } }, { codexVersion: 'codex-cli 0.137.0' }),
1218 'marketplace conflict': () =>
1219 makeFakeHosts({ claude: { available: true, marketplace: 'conflict' } }),
1220 'native install failure': () =>
1221 makeFakeHosts({ claude: { available: true } }, { failOn: 'plugin install' }),
1222 } as const;
1223
1224 const REASON_CODE_CASES: ReadonlyArray<{
1225 readonly hosts: keyof typeof REASON_CODE_FIXTURES;
1226 readonly cases: ReadonlyArray<{ readonly args: string; readonly expected: string }>;
1227 }> = [
1228 {
1229 hosts: 'only Claude Code installed',
1230 cases: [
1231 { args: '--target all --yes', expected: 'all_requires_both_hosts' },
1232 { args: '--target codex --yes', expected: 'target_not_installed' },
1233 { args: '--target claude', expected: 'non_interactive_requires_yes' },
1234 ],
1235 },
1236 { hosts: 'no host installed', cases: [{ args: '--yes', expected: 'no_host_detected' }] },
1237 {
1238 hosts: 'unsupported Codex only',
1239 cases: [
1240 { args: '--target codex --yes', expected: 'unsupported_host' },
1241 { args: '--yes', expected: 'unsupported_host' },
1242 ],
1243 },
1244 {
1245 hosts: 'marketplace conflict',
1246 cases: [{ args: '--target claude --yes', expected: 'marketplace_conflict' }],
1247 },
1248 {
1249 hosts: 'native install failure',
1250 cases: [{ args: '--target claude --yes', expected: 'unknown' }],
1251 },
1252 ];
1253
1254 describe('CLI: composio setup failure reason codes', () => {
1255 describe.each(REASON_CODE_CASES)('$hosts', ({ hosts, cases }) => {
1256 layer(TestLive({ commandRunner: REASON_CODE_FIXTURES[hosts]().runner }))(it => {
1257 it.effect.each(cases)('codes `setup $args` as $expected', ({ args, expected }) =>
1258 Effect.gen(function* () {
1259 const failure = yield* cli(['setup', ...args.split(' ')]).pipe(Effect.flip);
1260
1261 expect(failure).toBeInstanceOf(SetupCommandError);
1262 expect(failure).toMatchObject({ reasonCode: expected });
1263 })
1264 );
1265 });
1266 });
1267 });
1268
1269 describe('CLI: composio setup rerun hints stay non-interactive safe', () => {
1270 layer(TestLive({ commandRunner: REASON_CODE_FIXTURES['only Claude Code installed']().runner }))(
1271 'targeted host missing',
1272 it => {
1273 it.effect('target_not_installed rerun carries --yes', () =>
1274 Effect.gen(function* () {
1275 const failure = yield* cli(['setup', '--target', 'codex', '--yes']).pipe(Effect.flip);
1276 expect(failure).toMatchObject({
1277 message: expect.stringContaining('rerun `composio setup --yes --target codex`'),
1278 });
1279 })
1280 );
1281 }
1282 );
1283
1284 layer(TestLive({ commandRunner: REASON_CODE_FIXTURES['no host installed']().runner }))(
1285 'no host detected',
1286 it => {
1287 it.effect('no_host_detected rerun carries --yes', () =>
1288 Effect.gen(function* () {
1289 const failure = yield* cli(['setup', '--yes']).pipe(Effect.flip);
1290 expect(failure).toMatchObject({
1291 message: expect.stringContaining('rerun `composio setup --yes`'),
1292 });
1293 })
1294 );
1295 }
1296 );
1297
1298 layer(TestLive({ commandRunner: REASON_CODE_FIXTURES['marketplace conflict']().runner }))(
1299 'marketplace conflict',
1300 it => {
1301 it.effect('marketplace_conflict rerun carries --yes', () =>
1302 Effect.gen(function* () {
1303 const failure = yield* cli(['setup', '--target', 'claude', '--yes']).pipe(Effect.flip);
1304 expect(failure).toMatchObject({
1305 message: expect.stringContaining('rerun `composio setup --yes --target claude`'),
1306 });
1307 })
1308 );
1309 }
1310 );
1311 });