@devries-cameron20 / composio Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 04:45 UTC

next
ts 900 lines 33.4 KB
Raw
1 import { afterEach, assert, describe, expect, it } from '@effect/vitest';
2 import { vi } from 'vitest';
3 import * as BunFileSystem from '@effect/platform-bun/BunFileSystem';
4 import * as BunPath from '@effect/platform-bun/BunPath';
5 import { ConfigProvider, Effect, Fiber, Layer } from 'effect';
6 import * as tempy from 'tempy';
7 import {
8 AmbiguousDeveloperProjectNameError,
9 createProjectApiKey,
10 findDeveloperProjectByName,
11 getLatestToolVersion,
12 getSessionInfoByUserApiKey,
13 listOrganizations,
14 listOrgProjects,
15 } from 'src/services/composio-account';
16 import {
17 ComposioClientSingleton,
18 ComposioToolkitsRepository,
19 type ComposioToolkitsRepositoryShape,
20 } from 'src/services/composio-clients';
21 import { InvalidToolkitsError } from 'src/services/toolkit-version-validation';
22 import { handleClientApiError } from 'src/effects/handle-http-error';
23 import { isClientApiError } from 'src/utils/api-error-extraction';
24 import { defaultNodeOs, NodeOs } from 'src/services/node-os';
25 import { TerminalUI } from 'src/services/terminal-ui';
26 import { makeOrgProject, makeSessionInfo } from 'test/__utils__/models/account';
27 import { makeToolkitFixture } from 'test/__utils__/models/toolkits';
28 import { terminalUITestImpl } from 'test/__utils__/services/terminal-ui-test';
29 import { exitCodeOf } from 'test/__utils__/exit-code';
30
31 // Exercise real user-context/config resolution without opening the OS credential store.
32 vi.mock('@composio/cli-keyring/effect', async importOriginal => {
33 const actual = await importOriginal<typeof import('@composio/cli-keyring/effect')>();
34 const { Effect, Layer } = await import('effect');
35 const { KeyringError } = await import('@composio/cli-keyring');
36 return {
37 ...actual,
38 KeyringLiveWithBackend: () =>
39 Layer.succeed(actual.KeyringService, {
40 getPassword: () => Effect.fail(new KeyringError({ kind: 'NoEntry' })),
41 getSecret: () => Effect.fail(new KeyringError({ kind: 'NoEntry' })),
42 setPassword: () => Effect.die(new Error('Unexpected credential write')),
43 setSecret: () => Effect.die(new Error('Unexpected credential write')),
44 deleteCredential: () => Effect.die(new Error('Unexpected credential deletion')),
45 isAvailable: Effect.succeed(true),
46 }),
47 };
48 });
49
50 const BASE_URL = 'https://backend.composio.dev';
51
52 // The developer project a project resource call names.
53 const PROJECT_SCOPE = { orgId: 'org_test', projectId: 'proj_test' };
54
55 const platformLayer = () =>
56 Layer.mergeAll(
57 BunFileSystem.layer,
58 BunPath.layer,
59 Layer.succeed(NodeOs, defaultNodeOs({ homedir: tempy.temporaryDirectory() })),
60 Layer.succeed(
61 ConfigProvider.ConfigProvider,
62
63 ConfigProvider.fromEnv({
64 env: { COMPOSIO_USER_API_KEY: 'uak_config', COMPOSIO_BASE_URL: BASE_URL },
65 })
66 )
67 );
68
69 const withSingleton = Layer.provide(ComposioClientSingleton.Default, platformLayer());
70 const withRepository = Layer.provide(ComposioToolkitsRepository.Default, platformLayer());
71
72 const jsonResponse = (body: unknown, status = 200, headers: Record<string, string> = {}) =>
73 new Response(JSON.stringify(body), {
74 status,
75 headers: { 'Content-Type': 'application/json', ...headers },
76 });
77
78 // Error responses opt out of the client's retry policy so a scenario sees
79 // exactly one request, unless the scenario is about retries.
80 const errorResponse = (status: number, body: unknown) =>
81 jsonResponse(body, status, { 'x-should-retry': 'false' });
82
83 const errorEnvelope = (fields: { message: string; code: number; suggested_fix?: string }) => ({
84 error: { slug: 'Test_Error', status: 0, ...fields },
85 });
86
87 const requestOf = (call: readonly unknown[] | undefined) => {
88 const [input, init] = call ?? [];
89 return {
90 url: new URL(String(input)),
91 headers: new Headers((init as RequestInit | undefined)?.headers),
92 };
93 };
94
95 // `toolkits.list` items as the API sends them: dates as strings.
96 const toolkitListItem = (slug: string) => ({
97 ...makeToolkitFixture(slug),
98 meta: {
99 ...makeToolkitFixture(slug).meta,
100 created_at: '2024-05-03T11:44:32.061Z',
101 updated_at: '2024-05-03T11:44:32.061Z',
102 },
103 });
104
105 describe('composio-clients', () => {
106 afterEach(() => {
107 vi.restoreAllMocks();
108 });
109
110 describe('account helpers', () => {
111 it.effect('[Given] orgId [Then] session info carries x-org-id without x-project-id', () =>
112 Effect.gen(function* () {
113 const fetchSpy = vi
114 .spyOn(globalThis, 'fetch')
115 .mockResolvedValue(jsonResponse(makeSessionInfo()));
116
117 yield* getSessionInfoByUserApiKey({ userApiKey: 'uak_test', orgId: 'ok_selected_org' });
118
119 const { url, headers } = requestOf(fetchSpy.mock.calls[0]);
120 expect(url.pathname).toBe('/api/v3.1/auth/session/info');
121 expect(headers.get('x-user-api-key')).toBe('uak_test');
122 expect(headers.get('x-org-id')).toBe('ok_selected_org');
123 expect(headers.has('x-project-id')).toBe(false);
124 expect(headers.has('x-api-key')).toBe(false);
125 }).pipe(Effect.provide(withSingleton))
126 );
127
128 it.effect('[Given] no orgId [Then] session info omits x-org-id', () =>
129 Effect.gen(function* () {
130 const fetchSpy = vi
131 .spyOn(globalThis, 'fetch')
132 .mockResolvedValue(jsonResponse(makeSessionInfo()));
133
134 yield* getSessionInfoByUserApiKey({ userApiKey: 'uak_test' });
135
136 const { headers } = requestOf(fetchSpy.mock.calls[0]);
137 expect(headers.get('x-user-api-key')).toBe('uak_test');
138 expect(headers.has('x-org-id')).toBe(false);
139 }).pipe(Effect.provide(withSingleton))
140 );
141
142 it.effect('[Given] a session without a project [Then] fails with HttpDecodingError', () =>
143 Effect.gen(function* () {
144 vi.spyOn(globalThis, 'fetch').mockResolvedValue(
145 jsonResponse({ ...makeSessionInfo(), project: null })
146 );
147
148 const error = yield* getSessionInfoByUserApiKey({ userApiKey: 'uak_test' }).pipe(
149 Effect.flip
150 );
151
152 expect(error._tag).toBe('services/HttpDecodingError');
153 }).pipe(Effect.provide(withSingleton))
154 );
155
156 it.effect('listOrganizations returns id and name for each organization', () =>
157 Effect.gen(function* () {
158 vi.spyOn(globalThis, 'fetch').mockResolvedValue(
159 jsonResponse({
160 organizations: [
161 { id: 'org_a', name: 'A', created_at: 'x', updated_at: 'x' },
162 { id: 'org_b', name: 'B', created_at: 'x', updated_at: 'x' },
163 ],
164 total_pages: 1,
165 current_page: 1,
166 total_items: 2,
167 })
168 );
169
170 const result = yield* listOrganizations({ apiKey: 'uak_test' });
171
172 expect(result).toEqual({
173 data: [
174 { id: 'org_a', name: 'A' },
175 { id: 'org_b', name: 'B' },
176 ],
177 total_items: 2,
178 });
179 }).pipe(Effect.provide(withSingleton))
180 );
181
182 it.effect('listOrganizations fails with DecodeError when an organization has no name', () =>
183 Effect.gen(function* () {
184 vi.spyOn(globalThis, 'fetch').mockResolvedValue(
185 jsonResponse(
186 {
187 organizations: [{ id: 'org_a', created_at: 'x', updated_at: 'x' }],
188 total_pages: 1,
189 current_page: 1,
190 total_items: 1,
191 },
192 200,
193 { 'x-request-id': 'req_orgs' }
194 )
195 );
196
197 const error = yield* listOrganizations({ apiKey: 'uak_test' }).pipe(Effect.flip);
198
199 expect(error).toMatchObject({
200 _tag: '@composio/client/DecodeError',
201 operation: 'org.list',
202 requestId: 'req_orgs',
203 });
204 }).pipe(Effect.provide(withSingleton))
205 );
206
207 it.effect('listOrgProjects asks for every org project, scoped to the org only', () =>
208 Effect.gen(function* () {
209 const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue(
210 jsonResponse({
211 data: [makeOrgProject({ id: 'p1', name: 'One' })],
212 total_pages: 1,
213 current_page: 1,
214 total_items: 1,
215 })
216 );
217
218 yield* listOrgProjects({ apiKey: 'uak_test', orgId: 'org_1' });
219
220 const { url, headers } = requestOf(fetchSpy.mock.calls[0]);
221 expect(url.pathname).toBe('/api/v3.1/org/project/list');
222 expect(url.searchParams.get('list_all_org_projects')).toBe('true');
223 expect(url.searchParams.get('limit')).toBe('50');
224 expect(headers.get('x-org-id')).toBe('org_1');
225 expect(headers.has('x-project-id')).toBe(false);
226 }).pipe(Effect.provide(withSingleton))
227 );
228
229 it.effect(
230 'listOrgProjects returns a project whose webhook version the client does not know',
231 () =>
232 Effect.gen(function* () {
233 const project = { ...makeOrgProject({ id: 'p1', name: 'One' }), webhook_version: 'V9' };
234 vi.spyOn(globalThis, 'fetch').mockResolvedValue(
235 jsonResponse({ data: [project], total_pages: 1, current_page: 1, total_items: 1 })
236 );
237
238 const result = yield* listOrgProjects({ apiKey: 'uak_test', orgId: 'org_1' });
239
240 expect(result.data).toEqual([project]);
241 }).pipe(Effect.provide(withSingleton))
242 );
243
244 it.effect('findDeveloperProjectByName rejects a case-insensitive name collision', () =>
245 Effect.gen(function* () {
246 vi.spyOn(globalThis, 'fetch').mockResolvedValue(
247 jsonResponse({
248 data: [
249 makeOrgProject({ id: 'p1', name: 'Shared' }),
250 makeOrgProject({ id: 'p2', name: 'shared' }),
251 ],
252 total_pages: 1,
253 current_page: 1,
254 total_items: 2,
255 })
256 );
257
258 const error = yield* findDeveloperProjectByName({
259 apiKey: 'uak_test',
260 orgId: 'org_1',
261 name: 'SHARED',
262 }).pipe(Effect.flip);
263
264 expect(error).toBeInstanceOf(AmbiguousDeveloperProjectNameError);
265 }).pipe(Effect.provide(withSingleton))
266 );
267
268 it.effect('createProjectApiKey returns the minted key from the response body', () =>
269 Effect.gen(function* () {
270 const fetchSpy = vi
271 .spyOn(globalThis, 'fetch')
272 .mockResolvedValue(jsonResponse({ data: { api_key: 'uak_minted' } }));
273
274 const key = yield* createProjectApiKey({
275 apiKey: 'uak_test',
276 orgId: 'org_1',
277 projectId: 'proj_1',
278 name: 'cli',
279 });
280
281 expect(key).toBe('uak_minted');
282 const { url, headers } = requestOf(fetchSpy.mock.calls[0]);
283 expect(url.pathname).toBe('/api/v3/org/project/proj_1/api_keys/create');
284 expect(headers.get('x-org-id')).toBe('org_1');
285 expect(headers.get('x-project-id')).toBe('proj_1');
286 }).pipe(Effect.provide(withSingleton))
287 );
288
289 it.effect('createProjectApiKey never retries a retryable failure', () =>
290 Effect.gen(function* () {
291 // A bare 500 is retryable by default; the create call must still send once.
292 const fetchSpy = vi
293 .spyOn(globalThis, 'fetch')
294 .mockResolvedValue(jsonResponse({ message: 'boom' }, 500));
295
296 const error = yield* createProjectApiKey({
297 apiKey: 'uak_test',
298 orgId: 'org_1',
299 projectId: 'proj_1',
300 name: 'cli',
301 }).pipe(Effect.flip);
302
303 expect(fetchSpy).toHaveBeenCalledOnce();
304 expect(error).toMatchObject({
305 _tag: '@composio/client/InternalServerError',
306 status: 500,
307 });
308 }).pipe(Effect.provide(withSingleton))
309 );
310
311 it.effect('a connection failure fails with APIConnectionError naming the transport', () =>
312 Effect.gen(function* () {
313 vi.spyOn(globalThis, 'fetch').mockRejectedValue(new TypeError('fetch failed'));
314
315 const error = yield* createProjectApiKey({
316 apiKey: 'uak_test',
317 orgId: 'org_1',
318 projectId: 'proj_1',
319 name: 'cli',
320 }).pipe(Effect.flip);
321
322 expect(error).toMatchObject({
323 _tag: '@composio/client/APIConnectionError',
324 description: 'fetch failed',
325 });
326 }).pipe(Effect.provide(withSingleton))
327 );
328
329 // Live: the client sleeps between attempts on Effect's clock.
330 it.live('retries a retryable failure and reports the eventual success', () =>
331 Effect.gen(function* () {
332 // No `x-should-retry: false` here: this scenario is about the retry
333 // the owned client performs and the old hand-rolled fetch did not.
334 const fetchSpy = vi
335 .spyOn(globalThis, 'fetch')
336 .mockResolvedValueOnce(jsonResponse({ message: 'flaky' }, 500, { 'retry-after-ms': '0' }))
337 .mockResolvedValueOnce(jsonResponse(makeSessionInfo({ orgName: 'Retried Org' })));
338
339 const info = yield* getSessionInfoByUserApiKey({ userApiKey: 'uak_test' });
340
341 expect(fetchSpy).toHaveBeenCalledTimes(2);
342 expect(info.project.org.name).toBe('Retried Org');
343 }).pipe(Effect.provide(withSingleton))
344 );
345
346 it.effect('getLatestToolVersion with a project key sends exactly one credential', () =>
347 Effect.gen(function* () {
348 const fetchSpy = vi
349 .spyOn(globalThis, 'fetch')
350 .mockResolvedValue(jsonResponse({ tool_slug: 'GMAIL_SEND_EMAIL', version: '1' }));
351
352 const result = yield* getLatestToolVersion({
353 apiKey: 'uak_test',
354 toolSlug: 'GMAIL_SEND_EMAIL',
355 projectApiKey: 'ak_project',
356 });
357
358 expect(result.version).toBe('1');
359 const { url, headers } = requestOf(fetchSpy.mock.calls[0]);
360 expect(url.pathname).toBe('/api/v3/tools/GMAIL_SEND_EMAIL/get_latest_version');
361 expect(headers.get('x-api-key')).toBe('ak_project');
362 expect(headers.has('x-user-api-key')).toBe(false);
363 }).pipe(Effect.provide(withSingleton))
364 );
365
366 it.effect.each([
367 {
368 known: 'the org and the project',
369 orgId: 'org_1',
370 projectId: 'proj_1',
371 projectApiKey: undefined,
372 credentials: { user: 'uak_test', project: null },
373 },
374 {
375 known: 'the org only',
376 orgId: 'org_1',
377 projectId: undefined,
378 projectApiKey: undefined,
379 credentials: { user: 'uak_test', project: null },
380 },
381 {
382 known: 'the project only',
383 orgId: undefined,
384 projectId: 'proj_1',
385 projectApiKey: undefined,
386 credentials: { user: 'uak_test', project: null },
387 },
388 {
389 known: 'the org and the project, with a project key',
390 orgId: 'org_1',
391 projectId: 'proj_1',
392 projectApiKey: 'ak_project',
393 credentials: { user: null, project: 'ak_project' },
394 },
395 ])('getLatestToolVersion sends whichever of $known is known', params =>
396 Effect.gen(function* () {
397 const fetchSpy = vi
398 .spyOn(globalThis, 'fetch')
399 .mockResolvedValue(jsonResponse({ tool_slug: 'GMAIL_SEND_EMAIL', version: '1' }));
400
401 yield* getLatestToolVersion({
402 apiKey: 'uak_test',
403 toolSlug: 'GMAIL_SEND_EMAIL',
404 orgId: params.orgId,
405 projectId: params.projectId,
406 projectApiKey: params.projectApiKey,
407 });
408
409 const { headers } = requestOf(fetchSpy.mock.calls[0]);
410 expect(headers.get('x-org-id')).toBe(params.orgId ?? null);
411 expect(headers.get('x-project-id')).toBe(params.projectId ?? null);
412 expect({
413 user: headers.get('x-user-api-key'),
414 project: headers.get('x-api-key'),
415 }).toEqual(params.credentials);
416 }).pipe(Effect.provide(withSingleton))
417 );
418 });
419
420 describe('repository request core', () => {
421 it.effect.each(['getToolkits', 'getTools', 'getTriggerTypes'] as const)(
422 '%s aborts an in-flight pagination request when interrupted',
423 method =>
424 Effect.gen(function* () {
425 const started = Promise.withResolvers<AbortSignal>();
426 const fetchSpy = vi
427 .spyOn(globalThis, 'fetch')
428 .mockResolvedValueOnce(
429 jsonResponse({ items: [], next_cursor: 'page_2', total_pages: 2 })
430 )
431 .mockImplementationOnce((_input, init) => {
432 const signal = init!.signal!;
433 started.resolve(signal);
434 return new Promise<Response>((_resolve, reject) => {
435 signal.addEventListener('abort', () => reject(signal.reason), { once: true });
436 });
437 });
438 const repository = yield* ComposioToolkitsRepository;
439 const fiber = yield* Effect.forkChild(repository[method]());
440 const requestSignal = yield* Effect.promise(() => started.promise);
441
442 yield* Fiber.interrupt(fiber);
443
444 expect(requestSignal.aborted).toBe(true);
445 expect(fetchSpy).toHaveBeenCalledTimes(2);
446 }).pipe(Effect.provide(withRepository))
447 );
448
449 it.effect('fetches every toolkit page, sorted by slug, and counts each request', () =>
450 Effect.gen(function* () {
451 const fetchSpy = vi
452 .spyOn(globalThis, 'fetch')
453 .mockResolvedValueOnce(
454 jsonResponse({
455 items: [toolkitListItem('slack'), toolkitListItem('gmail')],
456 next_cursor: 'page_2',
457 total_pages: 3,
458 current_page: 1,
459 total_items: 4,
460 })
461 )
462 .mockResolvedValueOnce(
463 jsonResponse({
464 items: [toolkitListItem('zoom')],
465 next_cursor: 'page_3',
466 total_pages: 3,
467 current_page: 2,
468 total_items: 4,
469 })
470 )
471 .mockResolvedValueOnce(
472 jsonResponse({
473 items: [toolkitListItem('asana')],
474 next_cursor: null,
475 total_pages: 3,
476 current_page: 3,
477 total_items: 4,
478 })
479 );
480 const repository = yield* ComposioToolkitsRepository;
481
482 const toolkits = yield* repository.getToolkits();
483
484 expect(toolkits.map(t => t.slug)).toEqual(['asana', 'gmail', 'slack', 'zoom']);
485 expect(fetchSpy).toHaveBeenCalledTimes(3);
486 const requests = fetchSpy.mock.calls.map(call => requestOf(call).url.searchParams);
487 expect(requests.map(query => query.get('limit'))).toEqual(['1000', '1000', '1000']);
488 expect(requests.map(query => query.get('cursor'))).toEqual([null, 'page_2', 'page_3']);
489 const metrics = yield* repository.getMetrics();
490 expect(metrics.requests).toBe(3);
491 expect(metrics.byteSize).toBeGreaterThan(0);
492 }).pipe(Effect.provide(withRepository))
493 );
494
495 it.effect('a cursor the server repeats fails the listing instead of looping', () =>
496 Effect.gen(function* () {
497 const fetchSpy = vi.spyOn(globalThis, 'fetch').mockImplementation(async () =>
498 jsonResponse({
499 items: [toolkitListItem('gmail')],
500 next_cursor: 'page_2',
501 total_pages: 2,
502 })
503 );
504 const repository = yield* ComposioToolkitsRepository;
505
506 const error = yield* repository.getToolkits().pipe(Effect.flip);
507
508 expect(error._tag).toBe('@composio/client/ConfigurationError');
509 expect(fetchSpy).toHaveBeenCalledTimes(2);
510 }).pipe(Effect.provide(withRepository))
511 );
512
513 it.effect('a list item that breaks the catalog contract fails with HttpDecodingError', () =>
514 Effect.gen(function* () {
515 const { meta: _meta, ...withoutMeta } = toolkitListItem('gmail');
516 vi.spyOn(globalThis, 'fetch').mockResolvedValue(
517 jsonResponse({ items: [withoutMeta], next_cursor: null, total_pages: 1 })
518 );
519 const repository = yield* ComposioToolkitsRepository;
520
521 const error = yield* repository.getToolkits().pipe(Effect.flip);
522
523 expect(error._tag).toBe('services/HttpDecodingError');
524 }).pipe(Effect.provide(withRepository))
525 );
526
527 it.effect('searchTools returns a page the API contract does not allow as it arrived', () =>
528 Effect.gen(function* () {
529 // No `total_items`, and an item with only the fields the listing prints.
530 const page = {
531 items: [{ slug: 'GMAIL_SEND_EMAIL', name: 'Send Email', description: 'Send an email' }],
532 next_cursor: null,
533 total_pages: 1,
534 current_page: 1,
535 };
536 vi.spyOn(globalThis, 'fetch').mockResolvedValue(jsonResponse(page));
537 const repository = yield* ComposioToolkitsRepository;
538
539 const result = yield* repository.searchTools({ search: 'send' });
540
541 expect(result).toEqual(page);
542 }).pipe(Effect.provide(withRepository))
543 );
544
545 it.effect('an API error envelope arrives as the status error with its details', () =>
546 Effect.gen(function* () {
547 vi.spyOn(globalThis, 'fetch').mockResolvedValue(
548 errorResponse(
549 404,
550 errorEnvelope({ message: 'Not found', code: 404, suggested_fix: 'Check the id' })
551 )
552 );
553 const repository = yield* ComposioToolkitsRepository;
554
555 const error = yield* repository
556 .getAuthConfig(PROJECT_SCOPE, 'ac_missing')
557 .pipe(Effect.flip);
558
559 expect(error).toMatchObject({
560 _tag: '@composio/client/NotFoundError',
561 status: 404,
562 details: { message: 'Not found', code: 404, suggested_fix: 'Check the id' },
563 });
564 }).pipe(Effect.provide(withRepository))
565 );
566
567 it.effect('a non-envelope error body keeps the status and leaves details undefined', () =>
568 Effect.gen(function* () {
569 vi.spyOn(globalThis, 'fetch').mockResolvedValue(
570 errorResponse(502, { message: 'Bad gateway' })
571 );
572 const repository = yield* ComposioToolkitsRepository;
573
574 const error = yield* repository.getAuthConfig(PROJECT_SCOPE, 'ac_1').pipe(Effect.flip);
575
576 expect(error).toMatchObject({ status: 502, details: undefined });
577 }).pipe(Effect.provide(withRepository))
578 );
579
580 it.effect('an envelope without suggested_fix renders without a suggested-fix line', () =>
581 Effect.gen(function* () {
582 vi.spyOn(globalThis, 'fetch').mockResolvedValue(
583 errorResponse(400, errorEnvelope({ message: 'Bad input', code: 1001 }))
584 );
585 const repository = yield* ComposioToolkitsRepository;
586 const error = yield* repository.getAuthConfig(PROJECT_SCOPE, 'ac_1').pipe(Effect.flip);
587 expect(error).toMatchObject({ details: { message: 'Bad input', code: 1001 } });
588 assert(isClientApiError(error));
589
590 const plain: string[] = [];
591 const errors: string[] = [];
592 const steps: string[] = [];
593 const ui = TerminalUI.of({
594 ...terminalUITestImpl,
595 error: message => Effect.sync(() => void plain.push(message)),
596 log: {
597 ...terminalUITestImpl.log,
598 error: message => Effect.sync(() => void errors.push(message)),
599 step: message => Effect.sync(() => void steps.push(message)),
600 },
601 });
602 const exit = yield* Effect.exit(
603 handleClientApiError(ui, {
604 fallbackMessage: 'fallback',
605 hint: 'hint',
606 })(error)
607 );
608
609 // On a non-TTY the failure is two plain stderr lines: the message, then the hint.
610 expect(plain).toEqual(['Bad input', 'hint']);
611 expect(errors).toEqual([]);
612 expect(steps).toEqual([]);
613 expect(exitCodeOf(exit)).toBe(1);
614 }).pipe(Effect.provide(withRepository))
615 );
616
617 it.effect('drops credential-bearing fields from auth configs and connected accounts', () =>
618 Effect.gen(function* () {
619 const authConfig = {
620 id: 'ac_1',
621 uuid: 'uuid_ac_1',
622 name: 'Gmail auth',
623 no_of_connections: 0,
624 status: 'ENABLED',
625 type: 'default',
626 toolkit: { slug: 'gmail', logo: '' },
627 auth_scheme: 'OAUTH2',
628 is_composio_managed: true,
629 credentials: { client_secret: 'secret_credentials' },
630 shared_credentials: { token: 'secret_shared' },
631 proxy_config: { url: 'https://proxy', api_key: 'secret_proxy' },
632 deprecated: { secret: 'secret_deprecated' },
633 };
634 const connectedAccount = {
635 id: 'con_1',
636 status: 'ACTIVE',
637 status_reason: null,
638 is_disabled: false,
639 user_id: 'default',
640 toolkit: { slug: 'gmail' },
641 auth_config: {
642 id: 'ac_1',
643 auth_scheme: 'OAUTH2',
644 is_composio_managed: true,
645 is_disabled: false,
646 },
647 created_at: '2026-01-01T00:00:00Z',
648 updated_at: '2026-01-01T00:00:00Z',
649 test_request_endpoint: '',
650 state: { val: { access_token: 'secret_state' } },
651 data: { access_token: 'secret_data' },
652 params: { refresh_token: 'secret_params' },
653 };
654 vi.spyOn(globalThis, 'fetch')
655 .mockResolvedValueOnce(jsonResponse(authConfig))
656 .mockResolvedValueOnce(jsonResponse(connectedAccount));
657 const repository = yield* ComposioToolkitsRepository;
658
659 const decodedAuthConfig = yield* repository.getAuthConfig(PROJECT_SCOPE, 'ac_1');
660 const account = yield* repository.getConnectedAccount(PROJECT_SCOPE, 'con_1');
661
662 expect(JSON.stringify(decodedAuthConfig)).not.toContain('secret');
663 expect(JSON.stringify(account)).not.toContain('secret');
664 expect(account.id).toBe('con_1');
665 }).pipe(Effect.provide(withRepository))
666 );
667
668 it.effect('listActiveTriggers defaults the page counts a response leaves out', () =>
669 Effect.gen(function* () {
670 vi.spyOn(globalThis, 'fetch').mockResolvedValue(
671 jsonResponse({ items: [{ id: 'ti_1' }, { id: 'ti_2' }] })
672 );
673 const repository = yield* ComposioToolkitsRepository;
674
675 const triggers = yield* repository.listActiveTriggers(PROJECT_SCOPE, {});
676
677 expect(triggers).toMatchObject({
678 total_items: 2,
679 total_pages: 1,
680 current_page: 1,
681 next_cursor: null,
682 });
683 expect(triggers.items.map(item => item.id)).toEqual(['ti_1', 'ti_2']);
684 }).pipe(Effect.provide(withRepository))
685 );
686
687 it.effect('createAuthConfig returns only the allowlisted fields of the response', () =>
688 Effect.gen(function* () {
689 vi.spyOn(globalThis, 'fetch').mockResolvedValue(
690 jsonResponse({
691 auth_config: {
692 id: 'ac_new',
693 auth_scheme: 'OAUTH2',
694 is_composio_managed: false,
695 credentials: { client_secret: 'secret_credentials' },
696 },
697 toolkit: { slug: 'gmail' },
698 })
699 );
700 const repository = yield* ComposioToolkitsRepository;
701
702 const created = yield* repository.createAuthConfig(PROJECT_SCOPE, {
703 toolkit: { slug: 'gmail' },
704 });
705
706 expect(created).toEqual({
707 auth_config: { id: 'ac_new', auth_scheme: 'OAUTH2', is_composio_managed: false },
708 toolkit: { slug: 'gmail' },
709 });
710 }).pipe(Effect.provide(withRepository))
711 );
712
713 it.effect('getToolkitsBySlugs turns a 404 into InvalidToolkitsError naming the slug', () =>
714 Effect.gen(function* () {
715 vi.spyOn(globalThis, 'fetch').mockResolvedValue(
716 errorResponse(404, errorEnvelope({ message: 'Toolkit not found', code: 404 }))
717 );
718 const repository = yield* ComposioToolkitsRepository;
719
720 const error = yield* repository.getToolkitsBySlugs(['ghost']).pipe(Effect.flip);
721
722 expect(error).toBeInstanceOf(InvalidToolkitsError);
723 expect((error as InvalidToolkitsError).invalidToolkits).toEqual(['ghost']);
724 }).pipe(Effect.provide(withRepository))
725 );
726
727 it.effect('getToolkitsBySlugs propagates a 500 as the client status error', () =>
728 Effect.gen(function* () {
729 vi.spyOn(globalThis, 'fetch').mockResolvedValue(
730 errorResponse(500, errorEnvelope({ message: 'Server error', code: 500 }))
731 );
732 const repository = yield* ComposioToolkitsRepository;
733
734 const error = yield* repository.getToolkitsBySlugs(['gmail']).pipe(Effect.flip);
735
736 expect(error).toMatchObject({
737 _tag: '@composio/client/InternalServerError',
738 status: 500,
739 });
740 }).pipe(Effect.provide(withRepository))
741 );
742
743 it.effect('validateToolkitVersions ignores an override outside the filter', () =>
744 Effect.gen(function* () {
745 const fetchSpy = vi.spyOn(globalThis, 'fetch');
746 const repository = yield* ComposioToolkitsRepository;
747
748 const result = yield* repository.validateToolkitVersions(
749 new Map([['slack', '20250101_00']]),
750 ['gmail']
751 );
752
753 expect(result.validatedOverrides.size).toBe(0);
754 expect(result.warnings).toEqual([
755 'Version override for "slack" will be ignored (toolkit not in --toolkits filter)',
756 ]);
757 expect(fetchSpy).not.toHaveBeenCalled();
758 }).pipe(Effect.provide(withRepository))
759 );
760 });
761 });
762
763 describe('ComposioToolkitsRepository toolkit lists', () => {
764 afterEach(() => {
765 vi.restoreAllMocks();
766 });
767
768 // The real repository over the real client, with no `ProjectContext`: the
769 // consumer-mode setup, where only an explicit scope names a project.
770 const repositoryLayer = () =>
771 Layer.provide(
772 ComposioToolkitsRepository.Default,
773 Layer.mergeAll(
774 BunFileSystem.layer,
775 BunPath.layer,
776 Layer.succeed(NodeOs, defaultNodeOs({ homedir: tempy.temporaryDirectory() })),
777 Layer.succeed(
778 ConfigProvider.ConfigProvider,
779
780 ConfigProvider.fromEnv({
781 env: {
782 COMPOSIO_USER_API_KEY: 'uak_test',
783 COMPOSIO_BASE_URL: 'https://backend.composio.dev',
784 },
785 })
786 )
787 )
788 );
789
790 // An empty last page: what is asserted is the request, not the decoding.
791 const spyOnToolkitsFetch = () =>
792 vi.spyOn(globalThis, 'fetch').mockImplementation(
793 async () =>
794 new Response(JSON.stringify({ items: [], total_pages: 1, next_cursor: null }), {
795 status: 200,
796 headers: { 'Content-Type': 'application/json' },
797 })
798 );
799
800 const requestOf = (fetchSpy: ReturnType<typeof spyOnToolkitsFetch>) => {
801 const [input, init] = fetchSpy.mock.calls[0]!;
802 return { url: new URL(String(input)), headers: new Headers(init?.headers) };
803 };
804
805 it.effect(
806 '[Given] a scope [Then] getProjectToolkits lists managed_by=project for that project',
807 () =>
808 Effect.gen(function* () {
809 const fetchSpy = spyOnToolkitsFetch();
810 const repository = yield* ComposioToolkitsRepository;
811
812 yield* repository.getProjectToolkits({ orgId: 'org_scoped', projectId: 'pr_scoped' });
813
814 expect(fetchSpy).toHaveBeenCalledOnce();
815 const { url, headers } = requestOf(fetchSpy);
816 expect(url.pathname).toBe('/api/v3.1/toolkits');
817 expect(url.searchParams.get('managed_by')).toBe('project');
818 expect(headers.get('x-org-id')).toBe('org_scoped');
819 expect(headers.get('x-project-id')).toBe('pr_scoped');
820 }).pipe(Effect.provide(repositoryLayer()))
821 );
822
823 it.effect('[Given] no scope [Then] getToolkits lists without managed_by or a project', () =>
824 Effect.gen(function* () {
825 const fetchSpy = spyOnToolkitsFetch();
826 const repository = yield* ComposioToolkitsRepository;
827
828 yield* repository.getToolkits();
829
830 expect(fetchSpy).toHaveBeenCalledOnce();
831 const { url, headers } = requestOf(fetchSpy);
832 expect(url.pathname).toBe('/api/v3.1/toolkits');
833 expect(url.searchParams.has('managed_by')).toBe(false);
834 expect(headers.has('x-org-id')).toBe(false);
835 expect(headers.has('x-project-id')).toBe(false);
836 }).pipe(Effect.provide(repositoryLayer()))
837 );
838
839 // Auth configs, connected accounts and trigger instances belong to a project, so every call
840 // names the one the command resolved instead of leaving it to the key's default.
841 const scope = { orgId: 'org_scoped', projectId: 'pr_scoped' };
842 it.effect.each<{
843 readonly method: string;
844 readonly call: (repository: ComposioToolkitsRepositoryShape) => Effect.Effect<unknown, unknown>;
845 }>([
846 {
847 method: 'listAuthConfigs',
848 call: (repository: ComposioToolkitsRepositoryShape) => repository.listAuthConfigs(scope, {}),
849 },
850 {
851 method: 'getAuthConfig',
852 call: (repository: ComposioToolkitsRepositoryShape) =>
853 repository.getAuthConfig(scope, 'ac_1'),
854 },
855 {
856 method: 'createAuthConfig',
857 call: (repository: ComposioToolkitsRepositoryShape) =>
858 repository.createAuthConfig(scope, { toolkit: { slug: 'gmail' } }),
859 },
860 {
861 method: 'getConnectedAccount',
862 call: (repository: ComposioToolkitsRepositoryShape) =>
863 repository.getConnectedAccount(scope, 'con_1'),
864 },
865 {
866 method: 'listActiveTriggers',
867 call: (repository: ComposioToolkitsRepositoryShape) =>
868 repository.listActiveTriggers(scope, {}),
869 },
870 {
871 method: 'createTrigger',
872 call: (repository: ComposioToolkitsRepositoryShape) =>
873 repository.createTrigger(scope, 'GMAIL_NEW_GMAIL_MESSAGE', {}),
874 },
875 {
876 method: 'enableTrigger',
877 call: (repository: ComposioToolkitsRepositoryShape) =>
878 repository.enableTrigger(scope, 'ti_1'),
879 },
880 {
881 method: 'disableTrigger',
882 call: (repository: ComposioToolkitsRepositoryShape) =>
883 repository.disableTrigger(scope, 'ti_1'),
884 },
885 ])('[Given] a scope [Then] $method sends its org and project', ({ call }) =>
886 Effect.gen(function* () {
887 const fetchSpy = spyOnToolkitsFetch();
888 const repository = yield* ComposioToolkitsRepository;
889
890 // What is asserted is the request; the empty answer may not decode.
891 yield* Effect.exit(call(repository));
892
893 expect(fetchSpy).toHaveBeenCalledOnce();
894 const { headers } = requestOf(fetchSpy);
895 expect(headers.get('x-user-api-key')).toBe('uak_test');
896 expect(headers.get('x-org-id')).toBe('org_scoped');
897 expect(headers.get('x-project-id')).toBe('pr_scoped');
898 }).pipe(Effect.provide(repositoryLayer()))
899 );
900 });