1
+import { describe, it, expect, beforeEach, afterEach } from '@effect/vitest';
2
+import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
3
+import { tmpdir } from 'node:os';
4
+import { dirname, join } from 'node:path';
5
+import { Writable } from 'node:stream';
6
+import { BunFileSystem, BunPath } from '@effect/platform-bun';
7
+import { Effect, Layer } from 'effect';
8
+import { makeTerminalUI, type TerminalUI } from 'src/services/terminal-ui';
9
+import {
10
+ createPluginHint,
11
+ detectPluginHost,
12
+ type PluginHintConfig,
13
+ type PluginHintState,
14
+} from 'src/services/plugin-hint';
15
+
16
+const PlatformLayers = Layer.mergeAll(BunFileSystem.layer, BunPath.layer);
17
+
18
+const DAY_MS = 24 * 60 * 60 * 1000;
19
+
20
+let tempDir: string;
21
+beforeEach(() => {
22
+ tempDir = mkdtempSync(join(tmpdir(), 'plugin-hint-test-'));
23
+});
24
+afterEach(() => {
25
+ rmSync(tempDir, { recursive: true, force: true });
26
+});
27
+
28
+function makeConfig(overrides?: Partial<PluginHintConfig>): PluginHintConfig {
29
+ return {
30
+ stateFile: join(tempDir, '.composio', 'plugin-hint.json'),
31
+ host: 'claude',
32
+ invocationOrigin: undefined,
33
+ claudeInstalledPluginsFile: join(tempDir, '.claude', 'plugins', 'installed_plugins.json'),
34
+ codexConfigFile: join(tempDir, '.codex', 'config.toml'),
35
+ hintIntervalMs: DAY_MS,
36
+ ...overrides,
37
+ };
38
+}
39
+
40
+const makeTerminal = (output: string[]): Pick<TerminalUI, 'error'> => ({
41
+ error: line => Effect.sync(() => output.push(line)),
42
+});
43
+
44
+function writeFileAt(file: string, contents: string): void {
45
+ mkdirSync(dirname(file), { recursive: true });
46
+ writeFileSync(file, contents);
47
+}
48
+
49
+function writeHintState(config: PluginHintConfig, state: PluginHintState): void {
50
+ writeFileAt(config.stateFile, JSON.stringify(state));
51
+}
52
+
53
+function writeClaudePlugins(config: PluginHintConfig, plugins: Record<string, unknown[]>): void {
54
+ writeFileAt(config.claudeInstalledPluginsFile, JSON.stringify({ version: 2, plugins }));
55
+}
56
+
57
+describe('detectPluginHost', () => {
58
+ it('detects Claude Code from CLAUDECODE', () => {
59
+ expect(
60
+ detectPluginHost({ claudeCode: '1', codexThreadId: undefined, codexSandbox: undefined })
61
+ ).toBe('claude');
62
+ });
63
+
64
+ it('detects Codex from CODEX_THREAD_ID', () => {
65
+ expect(
66
+ detectPluginHost({ claudeCode: undefined, codexThreadId: 'abc', codexSandbox: undefined })
67
+ ).toBe('codex');
68
+ });
69
+
70
+ it('detects Codex from CODEX_SANDBOX', () => {
71
+ expect(
72
+ detectPluginHost({
73
+ claudeCode: undefined,
74
+ codexThreadId: undefined,
75
+ codexSandbox: 'seatbelt',
76
+ })
77
+ ).toBe('codex');
78
+ });
79
+
80
+ it('prefers Claude Code when both markers are present', () => {
81
+ expect(
82
+ detectPluginHost({ claudeCode: '1', codexThreadId: 'abc', codexSandbox: undefined })
83
+ ).toBe('claude');
84
+ });
85
+
86
+ it('detects no host without markers', () => {
87
+ expect(
88
+ detectPluginHost({ claudeCode: undefined, codexThreadId: undefined, codexSandbox: undefined })
89
+ ).toBeUndefined();
90
+ });
91
+});
92
+
93
+describe('showPluginHint', () => {
94
+ let output: string[];
95
+
96
+ beforeEach(() => {
97
+ output = [];
98
+ });
99
+
100
+ it.effect('shows the hint when the Claude plugin state file is missing', () =>
101
+ Effect.gen(function* () {
102
+ const config = makeConfig();
103
+
104
+ yield* createPluginHint(config).showPluginHint(makeTerminal(output));
105
+
106
+ expect(output).toHaveLength(1);
107
+ expect(output[0]).toContain('Claude Code');
108
+ expect(output[0]).toContain('composio setup');
109
+ }).pipe(Effect.provide(PlatformLayers))
110
+ );
111
+
112
+ it.effect('shows the hint when the Claude plugin is not installed', () =>
113
+ Effect.gen(function* () {
114
+ const config = makeConfig();
115
+ writeClaudePlugins(config, { 'other@marketplace': [{ scope: 'user' }] });
116
+
117
+ yield* createPluginHint(config).showPluginHint(makeTerminal(output));
118
+
119
+ expect(output).toHaveLength(1);
120
+ }).pipe(Effect.provide(PlatformLayers))
121
+ );
122
+
123
+ it.effect('shows the hint when the Claude plugin entry has no installations', () =>
124
+ Effect.gen(function* () {
125
+ const config = makeConfig();
126
+ writeClaudePlugins(config, { 'composio@composio': [] });
127
+
128
+ yield* createPluginHint(config).showPluginHint(makeTerminal(output));
129
+
130
+ expect(output).toHaveLength(1);
131
+ }).pipe(Effect.provide(PlatformLayers))
132
+ );
133
+
134
+ it.effect('stays silent when the Claude plugin is installed', () =>
135
+ Effect.gen(function* () {
136
+ const config = makeConfig();
137
+ writeClaudePlugins(config, { 'composio@composio': [{ scope: 'user' }] });
138
+
139
+ yield* createPluginHint(config).showPluginHint(makeTerminal(output));
140
+
141
+ expect(output).toEqual([]);
142
+ expect(existsSync(config.stateFile)).toBe(false);
143
+ }).pipe(Effect.provide(PlatformLayers))
144
+ );
145
+
146
+ it.effect('stays silent when the Claude plugin state file is unreadable', () =>
147
+ Effect.gen(function* () {
148
+ const config = makeConfig();
149
+ writeFileAt(config.claudeInstalledPluginsFile, 'not json{');
150
+
151
+ yield* createPluginHint(config).showPluginHint(makeTerminal(output));
152
+
153
+ expect(output).toEqual([]);
154
+ }).pipe(Effect.provide(PlatformLayers))
155
+ );
156
+
157
+ it.effect('shows the hint when the Codex config has no plugin section', () =>
158
+ Effect.gen(function* () {
159
+ const config = makeConfig({ host: 'codex' });
160
+ writeFileAt(config.codexConfigFile, '[plugins."glen@glen"]\nenabled = true\n');
161
+
162
+ yield* createPluginHint(config).showPluginHint(makeTerminal(output));
163
+
164
+ expect(output).toHaveLength(1);
165
+ expect(output[0]).toContain('Codex');
166
+ }).pipe(Effect.provide(PlatformLayers))
167
+ );
168
+
169
+ it.effect('shows the hint when the Codex config file is missing', () =>
170
+ Effect.gen(function* () {
171
+ const config = makeConfig({ host: 'codex' });
172
+
173
+ yield* createPluginHint(config).showPluginHint(makeTerminal(output));
174
+
175
+ expect(output).toHaveLength(1);
176
+ }).pipe(Effect.provide(PlatformLayers))
177
+ );
178
+
179
+ it.effect('stays silent when the Codex config has the plugin section', () =>
180
+ Effect.gen(function* () {
181
+ const config = makeConfig({ host: 'codex' });
182
+ writeFileAt(config.codexConfigFile, '[plugins."composio@composio"]\nenabled = true\n');
183
+
184
+ yield* createPluginHint(config).showPluginHint(makeTerminal(output));
185
+
186
+ expect(output).toEqual([]);
187
+ }).pipe(Effect.provide(PlatformLayers))
188
+ );
189
+
190
+ it.effect('stays silent outside a plugin-capable host', () =>
191
+ Effect.gen(function* () {
192
+ const config = makeConfig({ host: undefined });
193
+
194
+ yield* createPluginHint(config).showPluginHint(makeTerminal(output));
195
+
196
+ expect(output).toEqual([]);
197
+ }).pipe(Effect.provide(PlatformLayers))
198
+ );
199
+
200
+ it.effect('stays silent when invoked from a composio run child', () =>
201
+ Effect.gen(function* () {
202
+ const config = makeConfig({ invocationOrigin: 'run' });
203
+
204
+ yield* createPluginHint(config).showPluginHint(makeTerminal(output));
205
+
206
+ expect(output).toEqual([]);
207
+ }).pipe(Effect.provide(PlatformLayers))
208
+ );
209
+
210
+ it.effect('stays silent when the hint was shown within the interval', () =>
211
+ Effect.gen(function* () {
212
+ const config = makeConfig();
213
+ writeHintState(config, { lastHintShown: new Date().toISOString() });
214
+
215
+ yield* createPluginHint(config).showPluginHint(makeTerminal(output));
216
+
217
+ expect(output).toEqual([]);
218
+ }).pipe(Effect.provide(PlatformLayers))
219
+ );
220
+
221
+ it.effect('shows the hint again after the interval elapses', () =>
222
+ Effect.gen(function* () {
223
+ const config = makeConfig();
224
+ writeHintState(config, { lastHintShown: '2000-01-01T00:00:00.000Z' });
225
+
226
+ yield* createPluginHint(config).showPluginHint(makeTerminal(output));
227
+
228
+ expect(output).toHaveLength(1);
229
+ }).pipe(Effect.provide(PlatformLayers))
230
+ );
231
+
232
+ it.effect('shows the hint when the throttle state file is unreadable', () =>
233
+ Effect.gen(function* () {
234
+ const config = makeConfig();
235
+ writeFileAt(config.stateFile, 'not json{');
236
+
237
+ yield* createPluginHint(config).showPluginHint(makeTerminal(output));
238
+
239
+ expect(output).toHaveLength(1);
240
+ }).pipe(Effect.provide(PlatformLayers))
241
+ );
242
+
243
+ it.effect('stamps the throttle state when the hint is shown', () =>
244
+ Effect.gen(function* () {
245
+ const config = makeConfig();
246
+
247
+ yield* createPluginHint(config).showPluginHint(makeTerminal(output));
248
+
249
+ expect(output).toHaveLength(1);
250
+ expect(existsSync(config.stateFile)).toBe(true);
251
+ output = [];
252
+ yield* createPluginHint(config).showPluginHint(makeTerminal(output));
253
+ expect(output).toEqual([]);
254
+ }).pipe(Effect.provide(PlatformLayers))
255
+ );
256
+
257
+ it.effect('writes to stderr even when no stream is a TTY', () =>
258
+ Effect.gen(function* () {
259
+ const config = makeConfig();
260
+ const chunks: string[] = [];
261
+ const capture = new Writable({
262
+ write(chunk, _encoding, callback) {
263
+ chunks.push(String(chunk));
264
+ callback();
265
+ },
266
+ });
267
+ const terminal = makeTerminalUI({
268
+ stdin: { isTTY: false },
269
+ stdout: Object.assign(new Writable({ write: (_c, _e, cb) => cb() }), { isTTY: false }),
270
+ stderr: Object.assign(capture, { isTTY: false }),
271
+ });
272
+
273
+ yield* createPluginHint(config).showPluginHint(terminal);
274
+
275
+ expect(chunks.join('')).toContain('composio setup');
276
+ }).pipe(Effect.provide(PlatformLayers))
277
+ );
278
+});