@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 415 lines 13.8 KB
Raw
1 import { mkdirSync, writeFileSync } from 'node:fs';
2 import { join } from 'node:path';
3 import { describe, expect, layer } from '@effect/vitest';
4 import { Effect } from 'effect';
5 import { ChildProcess, ChildProcessSpawner } from 'effect/process';
6 import { afterEach, beforeEach, vi } from 'vitest';
7 import { CommandRunner } from 'src/services/command-runner';
8 import { NodeOs } from 'src/services/node-os';
9 import { SetupSkillInstaller } from 'src/services/setup-skill-installer';
10 import { getTerminalCapabilities, TerminalUI } from 'src/services/terminal-ui';
11 import { cli, TestLive } from 'test/__utils__';
12 import { terminalUITestImpl } from 'test/__utils__/services/terminal-ui-test';
13 import { eventsNamed, trackedEvents } from 'test/__utils__/tracked-events';
14
15 vi.mock('src/analytics/dispatch', async importOriginal => {
16 const { recordTrackedEvent } = await import('test/__utils__/tracked-events');
17 return {
18 ...(await importOriginal<typeof import('src/analytics/dispatch')>()),
19 trackCliEventEffect: recordTrackedEvent,
20 };
21 });
22
23 type AgentHost = 'claude' | 'codex';
24
25 interface FakeHostState {
26 available: boolean;
27 marketplace: boolean;
28 plugin: boolean;
29 }
30
31 const makeFakeHosts = (
32 initial: Partial<Record<AgentHost, Partial<FakeHostState>>>,
33 options: {
34 readonly failOn?: string;
35 readonly codexVersion?: string;
36 } = {}
37 ) => {
38 const state: Record<AgentHost, FakeHostState> = {
39 claude: { available: false, marketplace: false, plugin: false, ...initial.claude },
40 codex: { available: false, marketplace: false, plugin: false, ...initial.codex },
41 };
42
43 const marketplaceOutput = (host: AgentHost): string => {
44 if (!state[host].marketplace) return host === 'claude' ? '[]' : '{"marketplaces":[]}';
45 if (host === 'claude') {
46 return JSON.stringify([
47 {
48 name: 'composio',
49 source: {
50 source: 'github',
51 repo: 'https://github.com/ComposioHQ/composio-plugin-cc.git',
52 },
53 },
54 ]);
55 }
56 return JSON.stringify({
57 marketplaces: [
58 {
59 name: 'composio',
60 marketplaceSource: { sourceType: 'git', source: 'ComposioHQ/composio-plugin-openai' },
61 },
62 ],
63 });
64 };
65
66 const pluginOutput = (host: AgentHost): string => {
67 if (host === 'claude') {
68 if (!state.claude.plugin) return '[]';
69 return JSON.stringify([{ id: 'composio@composio', scope: 'user', enabled: true }]);
70 }
71 if (!state.codex.plugin) return '{"installed":[],"available":[]}';
72 return JSON.stringify({
73 installed: [{ pluginId: 'composio@composio', installed: true, enabled: true }],
74 available: [],
75 });
76 };
77
78 const runner = CommandRunner.of({
79 run: () => Effect.succeed(ChildProcessSpawner.ExitCode(0)),
80 capture: rawCommand => {
81 if (!ChildProcess.isStandardCommand(rawCommand)) {
82 throw new Error('Expected a standard command');
83 }
84 const parts = [rawCommand.command, ...rawCommand.args];
85 const host = parts[0] as AgentHost;
86 const command = parts.join(' ');
87 const respond = (result: { exitCode?: number; stdout?: string; stderr?: string }) =>
88 Effect.succeed({
89 exitCode: result.exitCode ?? 0,
90 stdout: result.stdout ?? '',
91 stderr: result.stderr ?? '',
92 });
93
94 if (parts[1] === '--version') {
95 if (!state[host].available) return respond({ exitCode: 127, stderr: 'not found' });
96 return respond({
97 stdout: host === 'claude' ? '2.1.0' : (options.codexVersion ?? 'codex-cli 0.144.1'),
98 });
99 }
100 if (command.endsWith('--help')) {
101 return respond({ stdout: `Usage: ${host} plugin\n\n --json` });
102 }
103 if (options.failOn && command.includes(options.failOn)) {
104 return respond({ exitCode: 2, stderr: 'native operation failed' });
105 }
106 if (command === `${host} plugin marketplace list --json`) {
107 return respond({ stdout: marketplaceOutput(host) });
108 }
109 if (command === `${host} plugin list --json`) {
110 return respond({ stdout: pluginOutput(host) });
111 }
112 if (command.includes('plugin marketplace add')) {
113 state[host].marketplace = true;
114 return respond({});
115 }
116 if (
117 command === 'claude plugin uninstall composio@composio --scope user --yes' ||
118 command === 'codex plugin remove composio@composio --json'
119 ) {
120 state[host].plugin = false;
121 return respond({});
122 }
123 if (
124 command.includes('plugin install') ||
125 command.includes('plugin enable') ||
126 command === 'codex plugin add composio@composio --json'
127 ) {
128 state[host].plugin = true;
129 return respond({});
130 }
131 return respond({});
132 },
133 });
134
135 return { runner, state };
136 };
137
138 const makeSkillInstaller = (initiallyReady = false) => {
139 let ready = initiallyReady;
140 return SetupSkillInstaller.of({
141 isClaudeSkillReady: Effect.sync(() => ready),
142 hasManagedClaudeSkill: Effect.sync(() => ready),
143 ensureClaudeSkill: Effect.sync(() => {
144 const changed = !ready;
145 ready = true;
146 return changed;
147 }),
148 removeClaudeSkill: Effect.sync(() => {
149 const changed = ready;
150 ready = false;
151 return changed;
152 }),
153 });
154 };
155
156 // The cancellation path requires an interactive terminal to reach the confirm prompt.
157 const decliningUI = TerminalUI.of({
158 ...terminalUITestImpl,
159 capabilities: Effect.succeed(
160 getTerminalCapabilities({
161 stdin: { isTTY: true },
162 stdout: { isTTY: false },
163 stderr: { isTTY: true },
164 })
165 ),
166 confirm: () => Effect.succeed(false),
167 });
168
169 describe('CLI: composio setup telemetry', () => {
170 beforeEach(() => {
171 trackedEvents.length = 0;
172 });
173
174 afterEach(() => {
175 vi.unstubAllEnvs();
176 });
177
178 const hostSignals = makeFakeHosts({ claude: { available: true } });
179 layer(
180 TestLive({
181 commandRunner: hostSignals.runner,
182 setupSkillInstaller: makeSkillInstaller(),
183 })
184 )('undetected host presence signals', it => {
185 it.effect('reports the config dir and known binary paths only for the undetected host', () =>
186 Effect.gen(function* () {
187 vi.stubEnv('CODEX_HOME', '');
188 const os = yield* NodeOs;
189 mkdirSync(join(os.homedir, '.codex'), { recursive: true });
190 mkdirSync(join(os.homedir, '.local', 'bin'), { recursive: true });
191 writeFileSync(join(os.homedir, '.local', 'bin', 'codex'), '');
192
193 yield* cli(['setup', '--target', 'auto', '--yes']);
194
195 const detected = eventsNamed('CLI_SETUP_HOST_DETECTED');
196 const codex = detected.find(event => event.properties?.agent_host === 'codex');
197 const claude = detected.find(event => event.properties?.agent_host === 'claude');
198 expect(codex?.properties).toMatchObject({
199 available: false,
200 host_config_dir_present: true,
201 host_binary_in_known_paths: true,
202 });
203 expect(claude?.properties).toMatchObject({ available: true });
204 expect(claude?.properties?.host_config_dir_present).toBeUndefined();
205 expect(claude?.properties?.host_binary_in_known_paths).toBeUndefined();
206 })
207 );
208 });
209
210 const freshClaude = makeFakeHosts({ claude: { available: true } });
211 layer(
212 TestLive({
213 commandRunner: freshClaude.runner,
214 setupSkillInstaller: makeSkillInstaller(),
215 })
216 )('fresh Claude install', it => {
217 it.effect('tracks per-host detection and a verified plugin install', () =>
218 Effect.gen(function* () {
219 yield* cli(['setup', '--target', 'auto', '--yes']);
220
221 const detectedEvents = eventsNamed('CLI_SETUP_HOST_DETECTED');
222 expect(detectedEvents).toHaveLength(2);
223 expect(detectedEvents.map(event => event.properties)).toEqual(
224 expect.arrayContaining([
225 expect.objectContaining({
226 agent_host: 'claude',
227 available: true,
228 supported: true,
229 host_version: '2.1.0',
230 unsupported_reason_code: undefined,
231 operation: 'setup',
232 requested_target: 'auto',
233 source: 'cli',
234 }),
235 expect.objectContaining({
236 agent_host: 'codex',
237 available: false,
238 supported: false,
239 unsupported_reason_code: undefined,
240 }),
241 ])
242 );
243
244 expect(eventsNamed('CLI_PLUGIN_SETUP_SUCCEEDED')).toEqual([
245 expect.objectContaining({
246 properties: expect.objectContaining({
247 agent_host: 'claude',
248 operation: 'setup',
249 action: 'installed',
250 }),
251 }),
252 ]);
253 expect(eventsNamed('CLI_PLUGIN_SETUP_FAILED')).toHaveLength(0);
254 })
255 );
256 });
257
258 const legacyCodex = makeFakeHosts(
259 { codex: { available: true } },
260 { codexVersion: 'codex-cli 0.137.0' }
261 );
262 layer(TestLive({ commandRunner: legacyCodex.runner }))('unsupported Codex only', it => {
263 it.effect('tracks the normalized reason code and the installer skip', () =>
264 Effect.gen(function* () {
265 yield* cli(['setup', '--target', 'auto', '--yes', '--if-present']);
266
267 expect(eventsNamed('CLI_SETUP_HOST_DETECTED')).toEqual(
268 expect.arrayContaining([
269 expect.objectContaining({
270 properties: expect.objectContaining({
271 agent_host: 'codex',
272 available: true,
273 supported: false,
274 unsupported_reason_code: 'codex_too_old',
275 }),
276 }),
277 ])
278 );
279 expect(eventsNamed('CLI_SETUP_SKIPPED')).toEqual([
280 expect.objectContaining({
281 properties: expect.objectContaining({
282 operation: 'setup',
283 requested_target: 'auto',
284 reason: 'no_host_detected',
285 }),
286 }),
287 ]);
288 })
289 );
290 });
291
292 const noHosts = makeFakeHosts({});
293 layer(TestLive({ commandRunner: noHosts.runner }))('no detected host', it => {
294 it.effect('tracks the installer skip when nothing is detected', () =>
295 Effect.gen(function* () {
296 yield* cli(['setup', '--target', 'auto', '--yes', '--if-present']);
297
298 expect(eventsNamed('CLI_SETUP_SKIPPED')).toEqual([
299 expect.objectContaining({
300 properties: expect.objectContaining({ reason: 'no_host_detected' }),
301 }),
302 ]);
303 expect(eventsNamed('CLI_SETUP_CANCELLED')).toHaveLength(0);
304 })
305 );
306
307 it.effect('reports absent config dirs for every undetected host', () =>
308 Effect.gen(function* () {
309 vi.stubEnv('CLAUDE_CONFIG_DIR', '');
310 vi.stubEnv('CODEX_HOME', '');
311
312 yield* cli(['setup', '--target', 'auto', '--yes', '--if-present']);
313
314 const detected = eventsNamed('CLI_SETUP_HOST_DETECTED');
315 const absentHost = expect.objectContaining({
316 available: false,
317 host_config_dir_present: false,
318 host_binary_in_known_paths: expect.any(Boolean),
319 });
320 expect(detected.map(event => event.properties)).toEqual([absentHost, absentHost]);
321 })
322 );
323 });
324
325 const declinedSetup = makeFakeHosts({ claude: { available: true } });
326 layer(
327 TestLive({
328 commandRunner: declinedSetup.runner,
329 setupSkillInstaller: makeSkillInstaller(),
330 terminalUI: decliningUI,
331 })
332 )('declined interactive setup', it => {
333 it.effect('tracks user cancellation without mutating the host', () =>
334 Effect.gen(function* () {
335 yield* cli(['setup', '--target', 'claude']);
336
337 expect(eventsNamed('CLI_SETUP_CANCELLED')).toEqual([
338 expect.objectContaining({
339 properties: expect.objectContaining({
340 operation: 'setup',
341 requested_target: 'claude',
342 reason: 'user_declined',
343 }),
344 }),
345 ]);
346 expect(declinedSetup.state.claude.plugin).toBe(false);
347 expect(eventsNamed('CLI_PLUGIN_SETUP_SUCCEEDED')).toHaveLength(0);
348 })
349 );
350 });
351
352 const declinedUninstall = makeFakeHosts({
353 claude: { available: true, marketplace: true, plugin: true },
354 });
355 layer(
356 TestLive({
357 commandRunner: declinedUninstall.runner,
358 setupSkillInstaller: makeSkillInstaller(true),
359 terminalUI: decliningUI,
360 })
361 )('declined interactive uninstall', it => {
362 it.effect('tracks user cancellation for the uninstall operation', () =>
363 Effect.gen(function* () {
364 yield* cli(['setup', '--uninstall', '--target', 'claude']);
365
366 expect(eventsNamed('CLI_SETUP_CANCELLED')).toEqual([
367 expect.objectContaining({
368 properties: expect.objectContaining({
369 operation: 'uninstall',
370 requested_target: 'claude',
371 reason: 'user_declined',
372 }),
373 }),
374 ]);
375 expect(declinedUninstall.state.claude.plugin).toBe(true);
376 })
377 );
378 });
379
380 const failedInstall = makeFakeHosts(
381 { claude: { available: true } },
382 { failOn: 'plugin install' }
383 );
384 layer(
385 TestLive({
386 commandRunner: failedInstall.runner,
387 setupSkillInstaller: makeSkillInstaller(),
388 })
389 )('native install failure', it => {
390 it.effect('tracks the per-host failure with its phase', () =>
391 Effect.gen(function* () {
392 const failure = yield* cli(['setup', '--target', 'claude', '--yes']).pipe(Effect.flip);
393 expect(failure).toMatchObject({
394 _tag: 'services/SetupCommandError',
395 operation: 'setup',
396 cause: { _tag: 'services/SetupProcessError', target: 'claude' },
397 });
398
399 const failureEvents = eventsNamed('CLI_PLUGIN_SETUP_FAILED');
400 expect(failureEvents).toEqual([
401 expect.objectContaining({
402 properties: expect.objectContaining({
403 agent_host: 'claude',
404 operation: 'setup',
405 phase: 'install',
406 error_name: 'services/SetupProcessError',
407 }),
408 }),
409 ]);
410 expect(failureEvents[0]?.properties).not.toHaveProperty('error_message');
411 expect(eventsNamed('CLI_PLUGIN_SETUP_SUCCEEDED')).toHaveLength(0);
412 })
413 );
414 });
415 });