1
+#!/usr/bin/env bun
2
+
3
+import { createServer, type IncomingMessage, type ServerResponse } from 'node:http';
4
+
5
+const GMAIL_TOOLKIT = {
6
+ name: 'Gmail',
7
+ slug: 'gmail',
8
+ auth_schemes: ['OAUTH2'],
9
+ composio_managed_auth_schemes: ['OAUTH2'],
10
+ is_local_toolkit: false,
11
+ meta: {
12
+ description: 'Gmail toolkit for deterministic CLI e2e tests.',
13
+ categories: [],
14
+ created_at: '2024-01-01T00:00:00.000Z',
15
+ updated_at: '2024-01-01T00:00:00.000Z',
16
+ available_versions: ['2024.01.01'],
17
+ tools_count: 40,
18
+ triggers_count: 2,
19
+ },
20
+ no_auth: false,
21
+} as const;
22
+
23
+const GMAIL_TOOLKIT_DETAILED = {
24
+ name: 'Gmail',
25
+ slug: 'gmail',
26
+ is_local_toolkit: false,
27
+ composio_managed_auth_schemes: ['OAUTH2'],
28
+ no_auth: false,
29
+ meta: {
30
+ description: 'Gmail toolkit for deterministic CLI e2e tests.',
31
+ categories: [],
32
+ created_at: '2024-01-01T00:00:00.000Z',
33
+ updated_at: '2024-01-01T00:00:00.000Z',
34
+ available_versions: ['2024.01.01'],
35
+ tools_count: 40,
36
+ triggers_count: 2,
37
+ },
38
+ auth_config_details: [
39
+ {
40
+ mode: 'OAUTH2',
41
+ name: 'OAuth 2.0',
42
+ fields: {
43
+ auth_config_creation: {
44
+ required: [],
45
+ optional: [],
46
+ },
47
+ connected_account_initiation: {
48
+ required: [],
49
+ optional: [],
50
+ },
51
+ },
52
+ },
53
+ ],
54
+} as const;
55
+
56
+export interface MockToolkitsServer {
57
+ readonly hostBaseUrl: string;
58
+ readonly dockerBaseUrl: string;
59
+ readonly requests: string[];
60
+ readonly close: () => Promise<void>;
61
+}
62
+
63
+const sendJson = (res: ServerResponse, statusCode: number, body: unknown): void => {
64
+ res.writeHead(statusCode, { 'Content-Type': 'application/json' });
65
+ res.end(JSON.stringify(body));
66
+};
67
+
68
+const matchingToolkits = (search: string | null): ReadonlyArray<typeof GMAIL_TOOLKIT> => {
69
+ if (search === null || search.length === 0) {
70
+ return [GMAIL_TOOLKIT];
71
+ }
72
+
73
+ const normalized = search.toLowerCase();
74
+ if (normalized === 'gmail' || normalized === 'gmai') {
75
+ return [GMAIL_TOOLKIT];
76
+ }
77
+
78
+ return [];
79
+};
80
+
81
+const parseLimit = (req: IncomingMessage): number => {
82
+ const url = new URL(req.url ?? '/', `http://${req.headers.host ?? '127.0.0.1'}`);
83
+ const parsed = Number.parseInt(url.searchParams.get('limit') ?? '', 10);
84
+ return Number.isFinite(parsed) && parsed > 0 ? parsed : 30;
85
+};
86
+
87
+export async function startMockToolkitsListServer(options?: {
88
+ host?: string;
89
+ port?: number;
90
+}): Promise<MockToolkitsServer> {
91
+ const host = options?.host ?? '0.0.0.0';
92
+ const port = options?.port ?? 0;
93
+ const requests: string[] = [];
94
+
95
+ const server = createServer((req, res) => {
96
+ const url = new URL(req.url ?? '/', `http://${req.headers.host ?? '127.0.0.1'}`);
97
+ requests.push(`${req.method ?? 'GET'} ${url.pathname}${url.search}`);
98
+
99
+ if ((req.method ?? 'GET') === 'GET' && url.pathname === '/api/v3/toolkits') {
100
+ const items = matchingToolkits(url.searchParams.get('search')).slice(0, parseLimit(req));
101
+ sendJson(res, 200, {
102
+ items,
103
+ total_items: items.length,
104
+ total_pages: 1,
105
+ current_page: 1,
106
+ next_cursor: null,
107
+ });
108
+ return;
109
+ }
110
+
111
+ if ((req.method ?? 'GET') === 'GET' && url.pathname === '/api/v3/toolkits/gmail') {
112
+ sendJson(res, 200, GMAIL_TOOLKIT_DETAILED);
113
+ return;
114
+ }
115
+
116
+ if ((req.method ?? 'GET') === 'GET' && url.pathname.startsWith('/api/v3/toolkits/')) {
117
+ sendJson(res, 404, {
118
+ error: `Toolkit not found: ${url.pathname.split('/').at(-1) ?? 'unknown'}`,
119
+ });
120
+ return;
121
+ }
122
+
123
+ if ((req.method ?? 'GET') === 'POST' && url.pathname === '/api/v3/cli/analytics') {
124
+ sendJson(res, 204, {});
125
+ return;
126
+ }
127
+
128
+ sendJson(res, 404, {
129
+ error: `Unhandled mock route: ${req.method ?? 'GET'} ${url.pathname}`,
130
+ });
131
+ });
132
+
133
+ await new Promise<void>((resolve, reject) => {
134
+ server.once('error', reject);
135
+ server.listen({ host, port }, () => {
136
+ server.off('error', reject);
137
+ resolve();
138
+ });
139
+ });
140
+
141
+ const address = server.address();
142
+ if (address === null || typeof address === 'string') {
143
+ throw new Error('Failed to bind mock toolkits list server to a TCP port');
144
+ }
145
+
146
+ const close = () =>
147
+ new Promise<void>((resolve, reject) => {
148
+ server.close(error => {
149
+ if (error) {
150
+ reject(error);
151
+ return;
152
+ }
153
+ resolve();
154
+ });
155
+ });
156
+
157
+ return {
158
+ hostBaseUrl: `http://127.0.0.1:${address.port}`,
159
+ dockerBaseUrl: `http://host.docker.internal:${address.port}`,
160
+ requests,
161
+ close,
162
+ };
163
+}
164
+
165
+const parsePortArg = (): number | undefined => {
166
+ const index = process.argv.indexOf('--port');
167
+ if (index === -1) return undefined;
168
+
169
+ const value = Number.parseInt(process.argv[index + 1] ?? '', 10);
170
+ return Number.isFinite(value) ? value : undefined;
171
+};
172
+
173
+if (import.meta.main) {
174
+ const server = await startMockToolkitsListServer({ port: parsePortArg() });
175
+
176
+ const shutdown = async () => {
177
+ await server.close();
178
+ process.exit(0);
179
+ };
180
+
181
+ process.on('SIGINT', () => {
182
+ void shutdown();
183
+ });
184
+ process.on('SIGTERM', () => {
185
+ void shutdown();
186
+ });
187
+
188
+ await new Promise(() => {});
189
+}