Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 430 lines 14.8 KB
Raw
1 import { afterEach, describe, expect, mock, spyOn, test } from 'bun:test';
2 import { readFile } from 'fs/promises';
3 import { join } from 'path';
4 import { z } from 'zod';
5 import { fetchToolkitFromProduction } from '../../lib/toolkit-api';
6
7 const originalApiKey = process.env.COMPOSIO_API_KEY;
8 const originalApiBase = process.env.COMPOSIO_API_BASE;
9 const originalFallback = process.env.COMPOSIO_TOOLKIT_LIVE_FALLBACK;
10 const originalFetch = globalThis.fetch;
11
12 const productionPayload = {
13 slug: 'GitHub',
14 name: 'GitHub',
15 type: 'native',
16 enabled: true,
17 composio_managed_auth_schemes: ['oauth2', 'API_KEY'],
18 auth_config_details: [
19 {
20 mode: 'OAuth2',
21 name: 'github_oauth',
22 fields: {
23 auth_config_creation: {
24 required: [
25 {
26 name: 'client_id',
27 displayName: 'Client id',
28 type: 'string',
29 description: 'Client id of the app',
30 },
31 ],
32 optional: [],
33 },
34 connected_account_initiation: { required: [], optional: [] },
35 },
36 },
37 { mode: 'api_key', name: 'API key', fields: {} },
38 ],
39 meta: {
40 logo: 'https://logos.composio.dev/api/github',
41 description: 'GitHub description',
42 categories: [{ name: 'developer tools' }],
43 tools_count: 893,
44 triggers_count: 20,
45 version: '20260815_00',
46 },
47 };
48
49 function useApiKey() {
50 process.env.COMPOSIO_API_KEY = 'test-key';
51 delete process.env.COMPOSIO_TOOLKIT_LIVE_FALLBACK;
52 }
53
54 afterEach(() => {
55 globalThis.fetch = originalFetch;
56 mock.restore();
57
58 if (originalApiKey === undefined) delete process.env.COMPOSIO_API_KEY;
59 else process.env.COMPOSIO_API_KEY = originalApiKey;
60 if (originalApiBase === undefined) delete process.env.COMPOSIO_API_BASE;
61 else process.env.COMPOSIO_API_BASE = originalApiBase;
62 if (originalFallback === undefined) delete process.env.COMPOSIO_TOOLKIT_LIVE_FALLBACK;
63 else process.env.COMPOSIO_TOOLKIT_LIVE_FALLBACK = originalFallback;
64 });
65
66 describe('fetchToolkitFromProduction', () => {
67 test('preserves Instant support from the production toolkit response', async () => {
68 useApiKey();
69 globalThis.fetch = Object.assign(
70 mock(async () => Response.json({ ...productionPayload, instant: { supported: true } })),
71 { preconnect: originalFetch.preconnect }
72 );
73 const toolkit = await fetchToolkitFromProduction('instant-example');
74 expect(toolkit?.instant).toEqual({ supported: true });
75 });
76 test('maps the single-toolkit response and normalizes auth modes', async () => {
77 useApiKey();
78 const fetcher = mock(async () => Response.json(productionPayload));
79 globalThis.fetch = fetcher;
80
81 const toolkit = await fetchToolkitFromProduction('GitHub');
82
83 expect(toolkit).toEqual({
84 slug: 'github',
85 name: 'GitHub',
86 logo: 'https://logos.composio.dev/api/github',
87 description: 'GitHub description',
88 category: 'developer tools',
89 authSchemes: ['OAUTH2', 'API_KEY'],
90 composioManagedAuthSchemes: ['OAUTH2', 'API_KEY'],
91 toolCount: 893,
92 triggerCount: 20,
93 version: '20260815_00',
94 tools: [],
95 triggers: [],
96 authConfigDetails: [
97 {
98 mode: 'OAUTH2',
99 name: 'github_oauth',
100 fields: {
101 auth_config_creation: {
102 required: [
103 {
104 name: 'client_id',
105 displayName: 'Client id',
106 type: 'string',
107 description: 'Client id of the app',
108 required: true,
109 default: null,
110 },
111 ],
112 optional: [],
113 },
114 connected_account_initiation: { required: [], optional: [] },
115 },
116 },
117 {
118 mode: 'API_KEY',
119 name: 'API key',
120 fields: {
121 auth_config_creation: { required: [], optional: [] },
122 connected_account_initiation: { required: [], optional: [] },
123 },
124 },
125 ],
126 });
127 expect(fetcher).toHaveBeenCalledTimes(1);
128 expect(fetcher.mock.calls[0]?.[0]).toBe('https://backend.composio.dev/api/v3.1/toolkits/github');
129 const init = fetcher.mock.calls[0]?.[1] as (RequestInit & { next?: unknown }) | undefined;
130 expect(new Headers(init?.headers).get('x-api-key')).toBe('test-key');
131 expect(init?.next).toEqual({ revalidate: 3600 });
132 expect(init?.signal).toBeInstanceOf(AbortSignal);
133 });
134
135 test('matches the committed snapshot key set and metadata defaults', async () => {
136 useApiKey();
137 const snapshotSchema = z.array(
138 z.object({
139 slug: z.string(),
140 name: z.string(),
141 logo: z.string().nullable(),
142 description: z.string(),
143 category: z.string().nullable(),
144 authSchemes: z.array(z.string()),
145 composioManagedAuthSchemes: z.array(z.string()).optional(),
146 toolCount: z.number(),
147 triggerCount: z.number(),
148 version: z.string().nullable(),
149 tools: z.array(z.unknown()),
150 triggers: z.array(z.unknown()),
151 authConfigDetails: z.array(z.unknown()).optional(),
152 })
153 );
154 const snapshots = snapshotSchema.parse(
155 JSON.parse(await readFile(join(import.meta.dir, '../../public/data/toolkits.json'), 'utf8'))
156 );
157 const snapshot = snapshots.find(toolkit => toolkit.slug === 'github');
158 expect(snapshot).toBeDefined();
159 if (!snapshot) throw new Error('Expected committed GitHub toolkit snapshot');
160
161 globalThis.fetch = mock(async () =>
162 Response.json({
163 slug: snapshot.slug,
164 name: snapshot.name,
165 type: 'native',
166 enabled: true,
167 composio_managed_auth_schemes: snapshot.composioManagedAuthSchemes,
168 auth_config_details: snapshot.authConfigDetails,
169 meta: {
170 logo: snapshot.logo,
171 description: snapshot.description,
172 categories: snapshot.category ? [{ name: snapshot.category }] : [],
173 tools_count: snapshot.toolCount,
174 triggers_count: snapshot.triggerCount,
175 version: snapshot.version,
176 },
177 })
178 );
179
180 const toolkit = await fetchToolkitFromProduction(snapshot.slug);
181 expect(toolkit).not.toBeNull();
182 expect(Object.keys(toolkit ?? {}).sort()).toEqual(
183 Object.keys({ ...snapshot, tools: [], triggers: [] }).sort()
184 );
185 expect(toolkit?.authSchemes).toEqual(snapshot.authSchemes);
186 expect(toolkit?.composioManagedAuthSchemes).toEqual(snapshot.composioManagedAuthSchemes);
187 expect(toolkit?.version).toBe(snapshot.version);
188 expect(toolkit?.tools).toEqual([]);
189 expect(toolkit?.triggers).toEqual([]);
190 });
191
192 test('degrades missing metadata and malformed auth details without throwing', async () => {
193 useApiKey();
194 globalThis.fetch = mock(async () =>
195 Response.json({
196 slug: 'LENIENT',
197 name: 'Lenient',
198 type: 'native',
199 enabled: true,
200 auth_config_details: [
201 null,
202 'junk',
203 { name: 'missing mode' },
204 {
205 mode: 'oauth2',
206 fields: {
207 auth_config_creation: { required: [null], optional: [] },
208 },
209 },
210 ],
211 })
212 );
213
214 const toolkit = await fetchToolkitFromProduction('lenient');
215 expect(toolkit).toMatchObject({
216 slug: 'lenient',
217 name: 'Lenient',
218 logo: null,
219 description: '',
220 category: null,
221 authSchemes: ['OAUTH2'],
222 toolCount: 0,
223 triggerCount: 0,
224 version: null,
225 tools: [],
226 triggers: [],
227 });
228 expect(toolkit?.authConfigDetails).toHaveLength(1);
229 expect(toolkit?.authConfigDetails?.[0]?.fields.auth_config_creation.required[0]).toEqual({
230 name: '',
231 displayName: '',
232 type: 'string',
233 description: '',
234 required: true,
235 default: null,
236 });
237 });
238
239 test.each(['../etc', 'has space', 'A'.repeat(80), ''])(
240 'rejects invalid slug %p before fetch',
241 async slug => {
242 useApiKey();
243 const fetcher = mock(async () => Response.json(productionPayload));
244 globalThis.fetch = fetcher;
245
246 expect(await fetchToolkitFromProduction(slug)).toBeNull();
247 expect(fetcher).not.toHaveBeenCalled();
248 }
249 );
250
251 test('accepts a leading underscore used by production toolkit slugs', async () => {
252 useApiKey();
253 const fetcher = mock(async () =>
254 Response.json({ ...productionPayload, slug: '_1password', name: '1Password' })
255 );
256 globalThis.fetch = fetcher;
257
258 expect(await fetchToolkitFromProduction('_1password')).toMatchObject({
259 slug: '_1password',
260 name: '1Password',
261 });
262 expect(fetcher).toHaveBeenCalledTimes(1);
263 });
264
265 test('rejects excluded public toolkit slugs before fetch', async () => {
266 useApiKey();
267 const fetcher = mock(async () => Response.json(productionPayload));
268 globalThis.fetch = fetcher;
269
270 expect(await fetchToolkitFromProduction('TEST_APP')).toBeNull();
271 expect(fetcher).not.toHaveBeenCalled();
272 });
273
274 test.each([
275 ['custom toolkit', { ...productionPayload, slug: 'custom-only', type: 'custom' }],
276 ['disabled toolkit', { ...productionPayload, slug: 'disabled-only', enabled: false }],
277 [
278 'toolkit without public flags',
279 { slug: 'missing-public-flags', name: 'Missing public flags' },
280 ],
281 ])('rejects a %s returned by the public endpoint', async (_label, payload) => {
282 useApiKey();
283 const fetcher = mock(async () => Response.json(payload));
284 globalThis.fetch = fetcher;
285 spyOn(console, 'warn').mockImplementation(() => {});
286
287 expect(await fetchToolkitFromProduction(payload.slug)).toBeNull();
288 expect(fetcher).toHaveBeenCalledTimes(1);
289 });
290
291 test('bounds the production request to 15 seconds', async () => {
292 useApiKey();
293 const controller = new AbortController();
294 const timeout = spyOn(AbortSignal, 'timeout').mockReturnValue(controller.signal);
295 spyOn(console, 'warn').mockImplementation(() => {});
296 let sent: AbortSignal | null | undefined;
297 globalThis.fetch = mock(async (_input: RequestInfo | URL, init?: RequestInit) => {
298 sent = init?.signal;
299 controller.abort();
300 return Response.json(productionPayload);
301 });
302
303 expect(await fetchToolkitFromProduction('timeout-bound')).toBeNull();
304 expect(timeout).toHaveBeenCalledWith(15_000);
305 expect(sent?.aborted).toBe(true);
306 });
307
308 test('requires an API key and warns without fetching', async () => {
309 delete process.env.COMPOSIO_API_KEY;
310 delete process.env.COMPOSIO_TOOLKIT_LIVE_FALLBACK;
311 const fetcher = mock(async () => Response.json(productionPayload));
312 globalThis.fetch = fetcher;
313 const warning = spyOn(console, 'warn').mockImplementation(() => {});
314
315 expect(await fetchToolkitFromProduction('missing-key')).toBeNull();
316 expect(fetcher).not.toHaveBeenCalled();
317 expect(warning).toHaveBeenCalledTimes(1);
318 });
319
320 test('reads the kill switch on every call', async () => {
321 useApiKey();
322 const fetcher = mock(async () => Response.json({ ...productionPayload, slug: 'toggle' }));
323 globalThis.fetch = fetcher;
324 process.env.COMPOSIO_TOOLKIT_LIVE_FALLBACK = '0';
325
326 expect(await fetchToolkitFromProduction('toggle')).toBeNull();
327 delete process.env.COMPOSIO_TOOLKIT_LIVE_FALLBACK;
328 expect(await fetchToolkitFromProduction('toggle')).not.toBeNull();
329 expect(fetcher).toHaveBeenCalledTimes(1);
330 });
331
332 test('ignores COMPOSIO_API_BASE when building the production URL', async () => {
333 useApiKey();
334 process.env.COMPOSIO_API_BASE = 'https://staging-backend.composio.dev/api/v3';
335 const fetcher = mock(async () => Response.json(productionPayload));
336 globalThis.fetch = fetcher;
337
338 await fetchToolkitFromProduction('github');
339 expect(fetcher.mock.calls[0]?.[0]).toBe('https://backend.composio.dev/api/v3.1/toolkits/github');
340 });
341
342 test('negative-caches a 404 for 60 seconds, then retries', async () => {
343 useApiKey();
344 const fetcher = mock(async () => new Response(null, { status: 404 }));
345 globalThis.fetch = fetcher;
346 const warning = spyOn(console, 'warn').mockImplementation(() => {});
347 const now = spyOn(Date, 'now').mockReturnValue(1_000);
348
349 expect(await fetchToolkitFromProduction('cache-miss')).toBeNull();
350 expect(await fetchToolkitFromProduction('cache-miss')).toBeNull();
351 expect(fetcher).toHaveBeenCalledTimes(1);
352
353 now.mockReturnValue(61_001);
354 expect(await fetchToolkitFromProduction('cache-miss')).toBeNull();
355 expect(fetcher).toHaveBeenCalledTimes(2);
356 expect(warning).toHaveBeenCalledTimes(2);
357 });
358
359 test('coalesces concurrent identical live misses into one request', async () => {
360 useApiKey();
361 let release: (() => void) | undefined;
362 const fetcher = mock(async () => {
363 await new Promise<void>(resolve => {
364 release = resolve;
365 });
366 return new Response(null, { status: 404 });
367 });
368 globalThis.fetch = fetcher;
369 spyOn(console, 'warn').mockImplementation(() => {});
370
371 const lookups = Array.from({ length: 20 }, () =>
372 fetchToolkitFromProduction('concurrent-miss')
373 );
374 expect(fetcher).toHaveBeenCalledTimes(1);
375
376 release?.();
377 expect(await Promise.all(lookups)).toEqual(Array.from({ length: 20 }, () => null));
378 expect(fetcher).toHaveBeenCalledTimes(1);
379 });
380
381 test('bounds concurrent lookup bookkeeping', async () => {
382 useApiKey();
383 let release: (() => void) | undefined;
384 const gate = new Promise<void>(resolve => {
385 release = resolve;
386 });
387 const fetcher = mock(async () => {
388 await gate;
389 return new Response(null, { status: 404 });
390 });
391 globalThis.fetch = fetcher;
392 spyOn(console, 'warn').mockImplementation(() => {});
393
394 const lookups = Array.from({ length: 1_025 }, (_, index) =>
395 fetchToolkitFromProduction(`bounded-flight-${index}`)
396 );
397 const evictedLookup = fetchToolkitFromProduction('bounded-flight-0');
398 expect(fetcher).toHaveBeenCalledTimes(1_026);
399
400 release?.();
401 await Promise.all([...lookups, evictedLookup]);
402 });
403
404 test('bounds negative-cache entries for unique missing slugs', async () => {
405 useApiKey();
406 const fetcher = mock(async () => new Response(null, { status: 404 }));
407 globalThis.fetch = fetcher;
408 spyOn(console, 'warn').mockImplementation(() => {});
409
410 for (let index = 0; index <= 1_024; index += 1) {
411 expect(await fetchToolkitFromProduction(`bounded-miss-${index}`)).toBeNull();
412 }
413 expect(fetcher).toHaveBeenCalledTimes(1_025);
414
415 expect(await fetchToolkitFromProduction('bounded-miss-0')).toBeNull();
416 expect(fetcher).toHaveBeenCalledTimes(1_026);
417 });
418
419 test.each([
420 ['500 response', async () => new Response(null, { status: 500 })],
421 ['network error', async () => Promise.reject(new Error('offline'))],
422 ['invalid JSON', async () => new Response('{', { status: 200 })],
423 ])('returns null for %s without throwing', async (_label, responseFactory) => {
424 useApiKey();
425 globalThis.fetch = mock(responseFactory);
426 spyOn(console, 'warn').mockImplementation(() => {});
427
428 expect(await fetchToolkitFromProduction(`failure-${_label.replaceAll(' ', '-')}`)).toBeNull();
429 });
430 });