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