1
import { describe, expect, it } from '@effect/vitest';
2
import { afterEach, beforeEach, vi } from 'vitest';
3
-import * as BunFileSystem from '@effect/platform-bun/BunFileSystem';
4
-import * as BunPath from '@effect/platform-bun/BunPath';
3
import * as BunServices from '@effect/platform-bun/BunServices';
4
import { ConfigProvider, Effect, Layer } from 'effect';
5
import { existsSync, mkdirSync, mkdtempSync, rmSync, utimesSync, writeFileSync } from 'node:fs';
6
import { tmpdir } from 'node:os';
7
import { dirname, join } from 'node:path';
8
import { Writable } from 'node:stream';
9
+import { APP_VERSION } from 'src/constants';
10
import { extendConfigProvider } from 'src/services/config';
11
import { defaultNodeOs, NodeOs } from 'src/services/node-os';
12
import { NodeProcess } from 'src/services/node-process';
15
createPluginHint,
16
findRootCommandName,
17
resolvePluginHintConfig,
19
- showPluginAcquisitionHint,
18
type PluginHintConfig,
19
} from 'src/services/plugin-hint';
20
import { makeTerminalUI, TerminalUI } from 'src/services/terminal-ui';
21
import { terminalUITestImpl } from 'test/__utils__/services/terminal-ui-test';
24
-
25
-const tracked = vi.hoisted(() => ({
26
- events: [] as Array<{ readonly name: string; readonly properties?: Record<string, unknown> }>,
27
-}));
22
+import { trackedEvents } from 'test/__utils__/tracked-events';
23
24
vi.mock('src/analytics/dispatch', async importOriginal => {
30
- const actual = await importOriginal<typeof import('src/analytics/dispatch')>();
31
- const { Effect } = await import('effect');
25
+ const { recordTrackedEvent } = await import('test/__utils__/tracked-events');
26
return {
33
- ...actual,
34
- trackCliEventEffect: (
35
- event: { readonly name: string; readonly properties?: Record<string, unknown> } | null
36
- ) =>
37
- Effect.sync(() => {
38
- if (event) tracked.events.push(event);
39
- }),
27
+ ...(await importOriginal<typeof import('src/analytics/dispatch')>()),
28
+ trackCliEventEffect: recordTrackedEvent,
29
};
30
});
31
38
afterEach(() => {
39
vi.useRealTimers();
40
vi.unstubAllEnvs();
52
- tracked.events.length = 0;
41
+ trackedEvents.length = 0;
42
rmSync(tempDir, { recursive: true, force: true });
43
});
44
56
-const PlatformLayers = Layer.mergeAll(BunFileSystem.layer, BunPath.layer);
45
+const hintLayers = () =>
46
+ Layer.mergeAll(
47
+ BunServices.layer,
48
+ NodeProcess.Default,
49
+ Layer.succeed(NodeOs, defaultNodeOs({ homedir: tempDir })),
50
+ Layer.succeed(TerminalUI, terminalUITestImpl)
51
+ );
52
53
function makeConfig(overrides?: Partial<PluginHintConfig>): PluginHintConfig {
54
return {
123
ConfigProvider.ConfigProvider,
124
extendConfigProvider(ConfigProvider.fromEnv())
125
),
131
- Effect.provide(
132
- Layer.merge(PlatformLayers, Layer.succeed(NodeOs, defaultNodeOs({ homedir: tempDir })))
133
- )
126
+ Effect.provide(hintLayers())
127
);
128
});
129
143
ConfigProvider.ConfigProvider,
144
extendConfigProvider(ConfigProvider.fromEnv())
145
),
153
- Effect.provide(
154
- Layer.merge(PlatformLayers, Layer.succeed(NodeOs, defaultNodeOs({ homedir: tempDir })))
155
- )
146
+ Effect.provide(hintLayers())
147
);
148
});
149
165
ConfigProvider.ConfigProvider,
166
extendConfigProvider(ConfigProvider.fromEnv())
167
),
177
- Effect.provide(
178
- Layer.merge(PlatformLayers, Layer.succeed(NodeOs, defaultNodeOs({ homedir: tempDir })))
179
- )
168
+ Effect.provide(hintLayers())
169
);
170
});
171
});
203
"Tip: running under Claude Code without the Composio plugin — 'composio setup --yes' installs it.",
204
"Tip: running under Codex without the Composio plugin — 'composio setup --yes' installs it.",
205
]);
217
- }).pipe(Effect.provide(PlatformLayers))
206
+ }).pipe(Effect.provide(hintLayers()))
207
);
208
220
- it.effect('reports whether the hint was printed', () =>
209
+ it.effect('tracks CLI_PLUGIN_HINT_SHOWN once per printed hint', () =>
210
Effect.gen(function* () {
222
- const config = makeConfig();
211
+ const config = makeConfig({ invocationOrigin: 'installer', commandName: 'whoami' });
212
224
- const first = yield* createPluginHint(config).showPluginHint(makeTerminal(output));
225
- const second = yield* createPluginHint(config).showPluginHint(makeTerminal(output));
213
+ yield* createPluginHint(config).showPluginHint(makeTerminal(output));
214
+ yield* createPluginHint(config).showPluginHint(makeTerminal(output));
215
227
- expect(first).toBe(true);
228
- expect(second).toBe(false);
216
expect(output).toHaveLength(1);
230
- }).pipe(Effect.provide(PlatformLayers))
217
+ expect(trackedEvents).toEqual([
218
+ {
219
+ name: 'CLI_PLUGIN_HINT_SHOWN',
220
+ properties: expect.objectContaining({
221
+ source: 'cli',
222
+ invocation_origin: 'installer',
223
+ cli_version: APP_VERSION,
224
+ command_path: 'whoami',
225
+ agent_host: 'claude',
226
+ }),
227
+ },
228
+ ]);
229
+ }).pipe(Effect.provide(hintLayers()))
230
);
231
232
it.effect('stays silent when the plugin is installed', () =>
241
242
expect(output).toEqual([]);
243
expect(existsSync(claudeConfig.stateDirectory)).toBe(false);
245
- }).pipe(Effect.provide(PlatformLayers))
244
+ }).pipe(Effect.provide(hintLayers()))
245
);
246
247
it.effect('stays silent when plugin state is unreadable', () =>
252
yield* createPluginHint(config).showPluginHint(makeTerminal(output));
253
254
expect(output).toEqual([]);
256
- }).pipe(Effect.provide(PlatformLayers))
255
+ }).pipe(Effect.provide(hintLayers()))
256
);
257
258
it.effect('stays silent outside a host, in run children, and for setup', () =>
268
);
269
270
expect(output).toEqual([]);
271
+ expect(trackedEvents).toEqual([]);
272
expect(existsSync(configs[2]!.stateDirectory)).toBe(false);
273
- }).pipe(Effect.provide(PlatformLayers))
273
+ }).pipe(Effect.provide(hintLayers()))
274
);
275
276
it.effect('allows only one concurrent process to claim a host hint', () =>
286
287
expect(output).toHaveLength(1);
288
expect(existsSync(join(config.stateDirectory, 'claude.stamp'))).toBe(true);
289
- }).pipe(Effect.provide(PlatformLayers))
289
+ }).pipe(Effect.provide(hintLayers()))
290
);
291
292
it.effect('throttles each host independently', () =>
300
yield* createPluginHint(codexConfig).showPluginHint(makeTerminal(output));
301
302
expect(output).toHaveLength(2);
303
- }).pipe(Effect.provide(PlatformLayers))
303
+ }).pipe(Effect.provide(hintLayers()))
304
);
305
306
it.effect('retires a stale stamp and shows the hint again', () =>
316
yield* createPluginHint(config).showPluginHint(makeTerminal(output));
317
318
expect(output).toHaveLength(1);
319
- }).pipe(Effect.provide(PlatformLayers))
319
+ }).pipe(Effect.provide(hintLayers()))
320
);
321
322
it.effect('writes to stderr even when no stream is a TTY', () =>
338
yield* createPluginHint(config).showPluginHint(terminal);
339
340
expect(chunks.join('')).toContain('composio setup');
341
- }).pipe(Effect.provide(PlatformLayers))
342
- );
343
-});
344
-
345
-describe('showPluginAcquisitionHint', () => {
346
- const output: string[] = [];
347
- const runHint = (argv: ReadonlyArray<string>) =>
348
- showPluginAcquisitionHint(argv).pipe(
349
- Effect.provideService(
350
- ConfigProvider.ConfigProvider,
351
- extendConfigProvider(ConfigProvider.fromEnv())
352
- ),
353
- Effect.provide(
354
- Layer.mergeAll(
355
- BunServices.layer,
356
- NodeProcess.Default,
357
- Layer.succeed(NodeOs, defaultNodeOs({ homedir: tempDir })),
358
- Layer.succeed(
359
- TerminalUI,
360
- TerminalUI.of({
361
- ...terminalUITestImpl,
362
- error: line => Effect.sync(() => output.push(line)),
363
- })
364
- )
365
- )
366
- )
367
- );
368
-
369
- beforeEach(() => {
370
- output.length = 0;
371
- vi.stubEnv('COMPOSIO_CACHE_DIR', join(tempDir, 'cache'));
372
- vi.stubEnv('CLAUDECODE', '1');
373
- vi.stubEnv('COMPOSIO_CLI_INVOCATION_ORIGIN', 'installer');
374
- });
375
-
376
- it.effect('tracks CLI_PLUGIN_HINT_SHOWN once per printed hint', () =>
377
- Effect.gen(function* () {
378
- yield* runHint(['/bin/bun', '/cli/bin.ts', 'whoami']);
379
- yield* runHint(['/bin/bun', '/cli/bin.ts', 'whoami']);
380
-
381
- expect(output).toHaveLength(1);
382
- expect(tracked.events).toEqual([
383
- {
384
- name: 'CLI_PLUGIN_HINT_SHOWN',
385
- properties: expect.objectContaining({
386
- source: 'cli',
387
- invocation_origin: 'installer',
388
- cli_version: expect.any(String),
389
- command_path: 'whoami',
390
- agent_host: 'claude',
391
- }),
392
- },
393
- ]);
394
- })
395
- );
396
-
397
- it.effect('tracks nothing when the hint is suppressed', () =>
398
- Effect.gen(function* () {
399
- yield* runHint(['/bin/bun', '/cli/bin.ts', 'setup']);
400
-
401
- expect(output).toEqual([]);
402
- expect(tracked.events).toEqual([]);
403
- })
341
+ }).pipe(Effect.provide(hintLayers()))
342
);
343
});