1
-import { describe, expect, it, layer } from '@effect/vitest';
1
+import { describe, expect, layer } from '@effect/vitest';
2
import { ConfigProvider, Effect } from 'effect';
3
-import { buildToolLogShorthandSearchParams } from 'src/commands/logs/tools/logs.tools.handler';
3
+import { TestClock } from 'effect/testing';
4
import { cli, MockConsole, TestLive } from 'test/__utils__';
5
import { stubClientResources } from 'test/__utils__/services/stub-composio-client';
6
+import { log } from './logs.tools.fixtures';
7
8
const testLayer = TestLive({
9
baseConfigProvider: ConfigProvider.fromEnv({ env: { COMPOSIO_USER_API_KEY: 'test_api_key' } }),
10
fixture: 'global-test-user-id',
11
});
12
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
-
13
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(['_internal'], () => 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 => {
14
+ layer(testLayer)(it => {
15
it.effect(
99
- '[Then] each is sent as search params in a fixed order and the limit is clamped',
16
+ '[Given] every supported filter [Then] searches the public API with at most 100 results',
17
() =>
18
Effect.gen(function* () {
102
- const stub = stubClientResources(['_internal'], () => Response.json({ data: [] }));
19
+ const stub = stubClientResources(['logs'], () =>
20
+ Response.json({ logs: [], next_cursor: null })
21
+ );
22
yield* cli([
23
'dev',
24
'logs',
25
'tools',
107
- '--session-id',
108
- 'sess_1',
26
'--tool-router-session-id',
27
'trs_1',
28
'--log-id',
112
- 'log_a,log_b',
29
+ 'log_a',
30
'--user-id',
31
'user_1',
32
'--status',
36
'--connected-account-id',
37
'ca_1',
38
'--toolkit',
122
- 'gmail,slack',
39
+ 'gmail',
40
'--tool',
41
'GMAIL_SEND_EMAIL',
42
'--cursor',
47
'2',
48
'--limit',
49
'5000',
133
- '--case-sensitive',
50
]).pipe(Effect.provide(stub.layer));
135
-
51
expect(stub.requests).toEqual([
52
{
53
method: 'POST',
139
- path: '/api/v3.1/internal/action_execution/logs',
54
+ path: '/api/v3.1/logs/tool_execution',
55
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' },
56
+ cursor: '7',
57
+ limit: 100,
58
+ time_range: { from: 1, to: 2 },
59
+ filters: [
60
+ { field: 'tool_slug', operator: '==', value: 'GMAIL_SEND_EMAIL' },
61
+ { field: 'toolkit_slug', operator: '==', value: 'gmail' },
62
+ { field: 'connected_account_id', operator: '==', value: 'ca_1' },
63
+ { field: 'auth_config_id', operator: '==', value: 'ac_1' },
64
+ { field: 'status', operator: '==', value: 'failed' },
65
+ { field: 'user_id', operator: '==', value: 'user_1' },
66
+ { field: 'log_id', operator: '==', value: 'log_a' },
67
+ { field: 'session_id', operator: '==', value: 'trs_1' },
68
],
69
},
70
},
71
]);
72
})
73
);
164
- });
74
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', () =>
75
+ it.effect.each([
76
+ { args: ['--limit', '0'], expected: { limit: 1 } },
77
+ { args: ['--from', '10'], expected: { time_range: { from: 10, to: 1000 } } },
78
+ { args: ['--to', '20'], expected: { time_range: { from: 0, to: 20 } } },
79
+ ])('[Given] $args [Then] builds a bounded public request', ({ args, expected }) =>
80
Effect.gen(function* () {
169
- const page = { data: [log], nextCursor: 42 };
170
- const stub = stubClientResources(['_internal'], () => 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)]);
81
+ yield* TestClock.setTime(1000);
82
+ const stub = stubClientResources(['logs'], () => Response.json({ logs: [] }));
83
+ yield* cli(['dev', 'logs', 'tools', ...args]).pipe(Effect.provide(stub.layer));
84
+ expect(stub.requests[0]?.body).toMatchObject(expected);
85
})
86
);
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(['_internal'], () => 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' });
87
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
- })
88
+ it.effect.each([
89
+ { args: ['dev', 'logs', 'tools', '--user-id', 'user_1'] },
90
+ { args: ['dev', '--user-id', 'user_1', 'logs', 'tools'] },
91
+ ])(
92
+ '[Given] a shared user filter at either position [Then] includes the user in the public filters',
93
+ ({ args }) =>
94
+ Effect.gen(function* () {
95
+ const stub = stubClientResources(['logs'], () => Response.json({ logs: [] }));
96
+ yield* cli(args).pipe(Effect.provide(stub.layer));
97
+ expect(stub.requests[0]?.body).toMatchObject({
98
+ filters: [{ field: 'user_id', operator: '==', value: 'user_1' }],
99
+ });
100
+ })
101
);
201
- });
102
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(['_internal'], () => Response.json({ data: [] }));
207
- yield* cli(['dev', 'logs', 'tools', '']).pipe(Effect.provide(stub.layer));
103
+ it.effect.each([
104
+ { args: ['--case-sensitive'], option: '--case-sensitive' },
105
+ { args: ['--session-id', 'cli_session'], option: '--session-id' },
106
+ { args: ['--status', 'unknown'], option: '--status' },
107
+ { args: ['--status', 'success,failed'], option: '--status' },
108
+ { args: ['--toolkit', 'gmail,slack'], option: '--toolkit' },
109
+ { args: ['--log-id', 'log_a,log_b'], option: '--log-id' },
110
+ { args: ['--user-id', 'user_1,user_2'], option: '--user-id' },
111
+ ])(
112
+ '[Given] $args [Then] rejects unsupported filtering before requesting logs',
113
+ ({ args, option }) =>
114
+ Effect.gen(function* () {
115
+ const stub = stubClientResources(['logs'], () => Response.json({ logs: [] }));
116
+ const failure = yield* cli(['dev', 'logs', 'tools', ...args]).pipe(
117
+ Effect.provide(stub.layer),
118
+ Effect.flip
119
+ );
120
+ expect(failure).toMatchObject({ _tag: 'commands/CommandInputError', option });
121
+ expect(stub.requests).toEqual([]);
122
+ })
123
+ );
124
209
- expect(stub.requests).toMatchObject([{ method: 'POST' }]);
210
- })
125
+ it.effect(
126
+ '[Given] an empty public page [Then] warns on stderr and returns the page on stdout',
127
+ () =>
128
+ Effect.gen(function* () {
129
+ const stdoutBefore = (yield* MockConsole.getLines({ stream: 'stdout' })).length;
130
+ const page = { logs: [], next_cursor: null };
131
+ const stub = stubClientResources(['logs'], () => Response.json(page));
132
+ yield* cli(['dev', 'logs', 'tools']).pipe(Effect.provide(stub.layer));
133
+ expect(
134
+ (yield* MockConsole.getLines({ stream: 'stderr', stripAnsi: true })).join('\n')
135
+ ).toContain('No tool logs found for the given filters.');
136
+ expect((yield* MockConsole.getLines({ stream: 'stdout' })).slice(stdoutBefore)).toEqual([
137
+ JSON.stringify(page, null, 2),
138
+ ]);
139
+ expect(stub.requests).toEqual([
140
+ {
141
+ method: 'POST',
142
+ path: '/api/v3.1/logs/tool_execution',
143
+ body: { cursor: null, limit: 30 },
144
+ },
145
+ ]);
146
+ })
147
);
212
- });
148
214
- layer(testLayer)('[Given] the API rejects the request for one log', it => {
215
- it.effect('[Then] the failure names the log id', () =>
149
+ it.effect('[Given] a page cursor [Then] displays it and sends it back unchanged', () =>
150
Effect.gen(function* () {
217
- const stub = stubClientResources(['_internal'], () =>
218
- Response.json(
219
- { error: { message: 'Not found', code: 1404, slug: 'Not_Found', status: 404 } },
220
- { status: 404 }
221
- )
151
+ const stdoutBefore = (yield* MockConsole.getLines({ stream: 'stdout' })).length;
152
+ const page = { logs: [log], next_cursor: '1767225600000' };
153
+ const stub = stubClientResources(['logs'], () => Response.json(page));
154
+ yield* cli(['dev', 'logs', 'tools']).pipe(Effect.provide(stub.layer));
155
+ const stderr = yield* MockConsole.getLines({ stream: 'stderr', stripAnsi: true });
156
+ expect(stderr).toContain('Next cursor: 1767225600000');
157
+ expect(stderr).toContain(
158
+ 'To view full details for a log:\n> composio dev logs tools "log_send_1"'
159
);
223
- const failure = yield* cli(['dev', 'logs', 'tools', 'log_missing']).pipe(
224
- Effect.provide(stub.layer),
225
- Effect.flip
160
+ expect((yield* MockConsole.getLines({ stream: 'stdout' })).slice(stdoutBefore)).toEqual([
161
+ JSON.stringify(page, null, 2),
162
+ ]);
163
+ yield* cli(['dev', 'logs', 'tools', '--cursor', page.next_cursor]).pipe(
164
+ Effect.provide(stub.layer)
165
);
227
-
228
- expect(failure).toMatchObject({
229
- _tag: 'commands/ToolLogsRequestError',
230
- message: 'Failed to fetch tool log "log_missing".',
231
- });
166
+ expect(stub.requests[1]?.body).toMatchObject({ cursor: page.next_cursor });
167
})
168
);
169
});
170
236
- layer(TestLive())('[Given] no API key', it => {
237
- it.effect('[Then] it fails with NotLoggedInError', () =>
171
+ layer(TestLive())(it => {
172
+ it.effect('[Given] no API key [Then] fails with NotLoggedInError', () =>
173
Effect.gen(function* () {
239
- const error = yield* cli(['dev', 'logs', 'tools']).pipe(Effect.flip);
240
- expect(error).toMatchObject({ _tag: 'commands/NotLoggedInError' });
174
+ expect(yield* cli(['dev', 'logs', 'tools']).pipe(Effect.flip)).toMatchObject({
175
+ _tag: 'commands/NotLoggedInError',
176
+ });
177
})
178
);
179
});