1
+import { describe, expect, it } from '@effect/vitest';
2
+import { Console, Effect, Layer, Option } from 'effect';
3
+import { runToolsList, type ToolsListParams } from 'src/commands/tools/list/tools.list.handler';
4
+import { noToolsFoundMessage } from 'src/commands/tools/list/tools.list.format';
5
+import type { Tools } from 'src/models/tools';
6
+import {
7
+ ComposioToolkitsRepository,
8
+ type ComposioToolkitsRepositoryShape,
9
+} from 'src/services/composio-clients';
10
+import { TerminalUI } from 'src/services/terminal-ui';
11
+import { MockConsole } from 'test/__utils__';
12
+import { resolveToolkitsTestData, toolkitCatalogMethods } from 'test/__utils__/layers/toolkits';
13
+import { terminalUITestImpl } from 'test/__utils__/services/terminal-ui-test';
14
+import { makeToolkitsRepositoryStub } from 'test/__utils__/services/toolkits-repository-stub';
15
+
16
+const tool = (slug: string, name: string, tags: ReadonlyArray<string> = []): Tools[number] => ({
17
+ slug,
18
+ name,
19
+ description: `${name} description`,
20
+ tags: [...tags],
21
+ available_versions: ['20260101_00'],
22
+ input_parameters: { type: 'object', properties: {} },
23
+ output_parameters: { type: 'object', properties: {} },
24
+});
25
+
26
+const TOOLS: Tools = [
27
+ tool('GMAIL_SEND_EMAIL', 'Send Email', ['email']),
28
+ tool('GMAIL_CREATE_DRAFT', 'Create Draft', ['email']),
29
+ tool('GMAIL_FETCH_EMAILS', 'Fetch Emails'),
30
+];
31
+
32
+type SearchToolsParams = Parameters<ComposioToolkitsRepositoryShape['searchTools']>[0];
33
+
34
+/**
35
+ * What `runToolsList` reads: the tool catalog, whose `searchTools` requests land in
36
+ * `requests`, and a headless terminal whose stdout data lands in `outputs`.
37
+ */
38
+const toolsListTest = (tools: Tools = TOOLS) => {
39
+ const outputs: Array<string> = [];
40
+ const requests: Array<SearchToolsParams> = [];
41
+ const catalog = toolkitCatalogMethods(resolveToolkitsTestData({ tools }));
42
+ const layer = Layer.mergeAll(
43
+ Layer.effect(Console.Console, MockConsole.make),
44
+ Layer.succeed(
45
+ TerminalUI,
46
+ TerminalUI.of({
47
+ ...terminalUITestImpl,
48
+ output: data =>
49
+ Effect.sync(() => {
50
+ outputs.push(data);
51
+ }),
52
+ })
53
+ ),
54
+ Layer.succeed(
55
+ ComposioToolkitsRepository,
56
+ makeToolkitsRepositoryStub({
57
+ searchTools: params =>
58
+ Effect.suspend(() => {
59
+ requests.push(params);
60
+ return catalog.searchTools(params);
61
+ }),
62
+ })
63
+ )
64
+ );
65
+ return { layer, outputs, requests };
66
+};
67
+
68
+const params = (overrides: Partial<ToolsListParams> = {}): ToolsListParams => ({
69
+ toolkit: 'gmail',
70
+ query: Option.none(),
71
+ tags: Option.none(),
72
+ limit: 30,
73
+ ...overrides,
74
+});
75
+
76
+describe('runToolsList', () => {
77
+ it.effect('[Given] filters [Then] passes them to the catalog search as given', () => {
78
+ const { layer, requests } = toolsListTest();
79
+ return Effect.gen(function* () {
80
+ yield* runToolsList(
81
+ params({ query: Option.some('send'), tags: Option.some('email,important') })
82
+ );
83
+
84
+ expect(requests).toEqual([
85
+ { search: 'send', toolkit_slug: 'gmail', tags: 'email,important', limit: 30 },
86
+ ]);
87
+ }).pipe(Effect.provide(layer));
88
+ });
89
+
90
+ it.effect.each([
91
+ { limit: 0, expected: 1 },
92
+ { limit: -3, expected: 1 },
93
+ { limit: 1001, expected: 1000 },
94
+ ])('[Given] limit $limit [Then] requests $expected tools', ({ limit, expected }) => {
95
+ const { layer, requests } = toolsListTest();
96
+ return Effect.gen(function* () {
97
+ yield* runToolsList(params({ limit }));
98
+
99
+ expect(requests.map(request => request.limit)).toEqual([expected]);
100
+ }).pipe(Effect.provide(layer));
101
+ });
102
+
103
+ it.effect('[Given] one page of tools [Then] lists them and prints them as JSON', () => {
104
+ const { layer, outputs } = toolsListTest();
105
+ return Effect.gen(function* () {
106
+ yield* runToolsList(params());
107
+ const lines = yield* MockConsole.getLines({ stripAnsi: true });
108
+
109
+ expect(lines[0]).toBe('Fetching tools...');
110
+ expect(lines[1]).toMatch(/^Listing 3 tools\n\n/);
111
+ expect(lines[2]).toBe('To inspect a tool:\n> composio tools info "GMAIL_SEND_EMAIL"');
112
+ expect(outputs.map(output => JSON.parse(output))).toEqual([
113
+ [
114
+ {
115
+ slug: 'GMAIL_SEND_EMAIL',
116
+ name: 'Send Email',
117
+ description: 'Send Email description',
118
+ tags: ['email'],
119
+ },
120
+ {
121
+ slug: 'GMAIL_CREATE_DRAFT',
122
+ name: 'Create Draft',
123
+ description: 'Create Draft description',
124
+ tags: ['email'],
125
+ },
126
+ {
127
+ slug: 'GMAIL_FETCH_EMAILS',
128
+ name: 'Fetch Emails',
129
+ description: 'Fetch Emails description',
130
+ tags: [],
131
+ },
132
+ ],
133
+ ]);
134
+ }).pipe(Effect.provide(layer));
135
+ });
136
+
137
+ it.effect('[Given] more tools than the limit [Then] names the first of several pages', () => {
138
+ const { layer, outputs } = toolsListTest();
139
+ return Effect.gen(function* () {
140
+ yield* runToolsList(params({ limit: 2 }));
141
+ const lines = yield* MockConsole.getLines({ stripAnsi: true });
142
+
143
+ expect(lines[1]).toMatch(/^Listing 2 tools \(page 1 of 2\)\n\n/);
144
+ expect(JSON.parse(outputs[0] ?? '[]')).toHaveLength(2);
145
+ }).pipe(Effect.provide(layer));
146
+ });
147
+
148
+ it.effect.each(['gmail', 'custom_grain'])(
149
+ '[Given] no tools in toolkit %s [Then] warns and prints nothing on stdout',
150
+ toolkit => {
151
+ const { layer, outputs } = toolsListTest([]);
152
+ return Effect.gen(function* () {
153
+ yield* runToolsList(params({ toolkit }));
154
+
155
+ expect(outputs).toEqual([]);
156
+ expect(yield* MockConsole.getLines({ stream: 'stderr' })).toEqual([
157
+ noToolsFoundMessage(toolkit),
158
+ ]);
159
+ }).pipe(Effect.provide(layer));
160
+ }
161
+ );
162
+});