1
+import { describe, expect, it, layer } from '@effect/vitest';
2
+import { ConfigProvider, Effect } from 'effect';
3
+import { buildToolLogShorthandSearchParams } from 'src/commands/logs/tools/logs.tools.handler';
4
+import { cli, MockConsole, TestLive } from 'test/__utils__';
5
+import { stubClientResources } from 'test/__utils__/services/stub-composio-client';
6
+
7
+const testLayer = TestLive({
8
+ baseConfigProvider: ConfigProvider.fromEnv({ env: { COMPOSIO_USER_API_KEY: 'test_api_key' } }),
9
+ fixture: 'global-test-user-id',
10
+});
11
+
12
+const log = {
13
+ id: 'log_1',
14
+ actionKey: 'GMAIL_SEND_EMAIL',
15
+ appKey: 'gmail',
16
+ app: { icon: 'https://logos.composio.dev/gmail', name: 'Gmail' },
17
+ connectedAccountId: 'ca_gmail_1',
18
+ entityId: 'user_1',
19
+ status: 'success',
20
+ executionTime: 412,
21
+ minimalResponse: '{"id":"msg_1"}',
22
+ createdAt: 1767225600000,
23
+};
24
+
25
+describe('buildToolLogShorthandSearchParams', () => {
26
+ it('[Given] no filters [Then] returns empty search params', () => {
27
+ expect(buildToolLogShorthandSearchParams({})).toEqual([]);
28
+ });
29
+
30
+ it('[Given] all supported filters [Then] maps each one to the expected backend key', () => {
31
+ const output = buildToolLogShorthandSearchParams({
32
+ tool: 'GMAIL_SEND_EMAIL',
33
+ toolkit: 'gmail',
34
+ connectedAccountId: 'ca_123',
35
+ authConfigId: 'ac_123',
36
+ status: 'success',
37
+ userId: 'user_123',
38
+ logId: 'log_123',
39
+ toolRouterSessionId: 'trs_123',
40
+ sessionId: 'sess_123',
41
+ });
42
+
43
+ expect(output).toEqual([
44
+ { field: 'action_key', operation: '==', value: 'GMAIL_SEND_EMAIL' },
45
+ { field: 'toolkit_key', operation: '==', value: 'gmail' },
46
+ { field: 'connected_account_id', operation: '==', value: 'ca_123' },
47
+ { field: 'auth_config_id', operation: '==', value: 'ac_123' },
48
+ { field: 'execution_status', operation: '==', value: 'success' },
49
+ { field: 'user_id', operation: '==', value: 'user_123' },
50
+ { field: 'log_id', operation: '==', value: 'log_123' },
51
+ { field: 'tool_router_session_id', operation: '==', value: 'trs_123' },
52
+ { field: 'session_id', operation: '==', value: 'sess_123' },
53
+ ]);
54
+ });
55
+
56
+ it('[Given] comma-separated filter values [Then] expands each value into separate search params', () => {
57
+ const output = buildToolLogShorthandSearchParams({
58
+ tool: 'GMAIL_SEND_EMAIL,SLACK_SEND_MESSAGE',
59
+ toolkit: 'gmail,slack',
60
+ status: 'success,failed',
61
+ logId: 'log_1,log_2',
62
+ });
63
+
64
+ expect(output).toEqual([
65
+ { field: 'action_key', operation: '==', value: 'GMAIL_SEND_EMAIL' },
66
+ { field: 'action_key', operation: '==', value: 'SLACK_SEND_MESSAGE' },
67
+ { field: 'toolkit_key', operation: '==', value: 'gmail' },
68
+ { field: 'toolkit_key', operation: '==', value: 'slack' },
69
+ { field: 'execution_status', operation: '==', value: 'success' },
70
+ { field: 'execution_status', operation: '==', value: 'failed' },
71
+ { field: 'log_id', operation: '==', value: 'log_1' },
72
+ { field: 'log_id', operation: '==', value: 'log_2' },
73
+ ]);
74
+ });
75
+});
76
+
77
+describe('CLI: composio dev logs tools', () => {
78
+ layer(testLayer)('[Given] no filters and no logs', it => {
79
+ it.effect('[Then] it warns and prints the empty page as the API returned it', () =>
80
+ Effect.gen(function* () {
81
+ const page = { data: [], nextCursor: null };
82
+ const stub = stubClientResources(['logs'], () => Response.json(page));
83
+ yield* cli(['dev', 'logs', 'tools']).pipe(Effect.provide(stub.layer));
84
+ const stderr = yield* MockConsole.getLines({ stream: 'stderr', stripAnsi: true });
85
+ const stdout = yield* MockConsole.getLines({ stream: 'stdout' });
86
+
87
+ expect(stderr.join('\n')).toContain('No tool logs found for the given filters.');
88
+ expect(stdout).toEqual([JSON.stringify(page, null, 2)]);
89
+ expect(stub.requests).toMatchObject([
90
+ { method: 'POST', body: { cursor: null, limit: 30, case_sensitive: false } },
91
+ ]);
92
+ expect(stub.requests[0]?.body).not.toHaveProperty('search_params');
93
+ })
94
+ );
95
+ });
96
+
97
+ layer(testLayer)('[Given] every filter flag', it => {
98
+ it.effect(
99
+ '[Then] each is sent as search params in a fixed order and the limit is clamped',
100
+ () =>
101
+ Effect.gen(function* () {
102
+ const stub = stubClientResources(['logs'], () => Response.json({ data: [] }));
103
+ yield* cli([
104
+ 'dev',
105
+ 'logs',
106
+ 'tools',
107
+ '--session-id',
108
+ 'sess_1',
109
+ '--tool-router-session-id',
110
+ 'trs_1',
111
+ '--log-id',
112
+ 'log_a,log_b',
113
+ '--user-id',
114
+ 'user_1',
115
+ '--status',
116
+ 'failed',
117
+ '--auth-config-id',
118
+ 'ac_1',
119
+ '--connected-account-id',
120
+ 'ca_1',
121
+ '--toolkit',
122
+ 'gmail,slack',
123
+ '--tool',
124
+ 'GMAIL_SEND_EMAIL',
125
+ '--cursor',
126
+ '7',
127
+ '--from',
128
+ '1',
129
+ '--to',
130
+ '2',
131
+ '--limit',
132
+ '5000',
133
+ '--case-sensitive',
134
+ ]).pipe(Effect.provide(stub.layer));
135
+
136
+ expect(stub.requests).toEqual([
137
+ {
138
+ method: 'POST',
139
+ path: '/api/v3.1/internal/action_execution/logs',
140
+ body: {
141
+ from: 1,
142
+ to: 2,
143
+ cursor: 7,
144
+ limit: 1000,
145
+ case_sensitive: true,
146
+ search_params: [
147
+ { field: 'action_key', operation: '==', value: 'GMAIL_SEND_EMAIL' },
148
+ { field: 'toolkit_key', operation: '==', value: 'gmail' },
149
+ { field: 'toolkit_key', operation: '==', value: 'slack' },
150
+ { field: 'connected_account_id', operation: '==', value: 'ca_1' },
151
+ { field: 'auth_config_id', operation: '==', value: 'ac_1' },
152
+ { field: 'execution_status', operation: '==', value: 'failed' },
153
+ { field: 'user_id', operation: '==', value: 'user_1' },
154
+ { field: 'log_id', operation: '==', value: 'log_a' },
155
+ { field: 'log_id', operation: '==', value: 'log_b' },
156
+ { field: 'tool_router_session_id', operation: '==', value: 'trs_1' },
157
+ { field: 'session_id', operation: '==', value: 'sess_1' },
158
+ ],
159
+ },
160
+ },
161
+ ]);
162
+ })
163
+ );
164
+ });
165
+
166
+ layer(testLayer)('[Given] a page with a next cursor', it => {
167
+ it.effect('[Then] it lists the logs, hints at the first one, and prints the cursor', () =>
168
+ Effect.gen(function* () {
169
+ const page = { data: [log], nextCursor: 42 };
170
+ const stub = stubClientResources(['logs'], () => Response.json(page));
171
+ yield* cli(['dev', 'logs', 'tools']).pipe(Effect.provide(stub.layer));
172
+ const stderr = yield* MockConsole.getLines({ stream: 'stderr', stripAnsi: true });
173
+ const stdout = yield* MockConsole.getLines({ stream: 'stdout' });
174
+
175
+ expect(stderr.join('\n')).toContain('Listing 1 tool log\n');
176
+ expect(stderr).toContain(
177
+ 'To view full details for a log:\n> composio dev logs tools "log_1"'
178
+ );
179
+ expect(stderr).toContain('Next cursor: 42');
180
+ expect(stdout).toEqual([JSON.stringify(page, null, 2)]);
181
+ })
182
+ );
183
+ });
184
+
185
+ layer(testLayer)('[Given] a log id argument', it => {
186
+ it.effect('[Then] it fetches that log alone and prints it as the API returned it', () =>
187
+ Effect.gen(function* () {
188
+ const toolLog = { ...log, payloadReceived: { to: 'a@b.c' }, response: { id: 'msg_1' } };
189
+ const stub = stubClientResources(['logs'], () => Response.json(toolLog));
190
+ yield* cli(['dev', 'logs', 'tools', 'log_1']).pipe(Effect.provide(stub.layer));
191
+ const stderr = yield* MockConsole.getLines({ stream: 'stderr', stripAnsi: true });
192
+ const stdout = yield* MockConsole.getLines({ stream: 'stdout' });
193
+
194
+ expect(stub.requests).toEqual([
195
+ { method: 'GET', path: '/api/v3.1/internal/action_execution/log/log_1' },
196
+ ]);
197
+ expect(stderr.join('\n')).toContain('Payload:\n{\n "to": "a@b.c"\n}');
198
+ expect(stdout).toEqual([JSON.stringify(toolLog, null, 2)]);
199
+ })
200
+ );
201
+ });
202
+
203
+ layer(testLayer)('[Given] an empty log id argument', it => {
204
+ it.effect('[Then] it lists logs instead of fetching one', () =>
205
+ Effect.gen(function* () {
206
+ const stub = stubClientResources(['logs'], () => Response.json({ data: [] }));
207
+ yield* cli(['dev', 'logs', 'tools', '']).pipe(Effect.provide(stub.layer));
208
+
209
+ expect(stub.requests).toMatchObject([{ method: 'POST' }]);
210
+ })
211
+ );
212
+ });
213
+
214
+ layer(testLayer)('[Given] the API rejects the request for one log', it => {
215
+ it.effect('[Then] the failure names the log id', () =>
216
+ Effect.gen(function* () {
217
+ const stub = stubClientResources(['logs'], () =>
218
+ Response.json(
219
+ { error: { message: 'Not found', code: 1404, slug: 'Not_Found', status: 404 } },
220
+ { status: 404 }
221
+ )
222
+ );
223
+ const failure = yield* cli(['dev', 'logs', 'tools', 'log_missing']).pipe(
224
+ Effect.provide(stub.layer),
225
+ Effect.flip
226
+ );
227
+
228
+ expect(failure).toMatchObject({
229
+ _tag: 'commands/ToolLogsRequestError',
230
+ message: 'Failed to fetch tool log "log_missing".',
231
+ });
232
+ })
233
+ );
234
+ });
235
+
236
+ layer(TestLive())('[Given] no API key', it => {
237
+ it.effect('[Then] it fails with NotLoggedInError', () =>
238
+ Effect.gen(function* () {
239
+ const error = yield* cli(['dev', 'logs', 'tools']).pipe(Effect.flip);
240
+ expect(error).toMatchObject({ _tag: 'commands/NotLoggedInError' });
241
+ })
242
+ );
243
+ });
244
+});