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, 03:37 UTC
| 1 | import { describe, expect, mock, test } from 'bun:test'; |
| 2 | import { |
| 3 | createComposioClient, |
| 4 | listToolkitTools, |
| 5 | listToolkitTriggers, |
| 6 | TOOL_PAGE_SIZE, |
| 7 | TRIGGER_PAGE_LIMIT, |
| 8 | } from '../../lib/composio-api'; |
| 9 | |
| 10 | type Init = RequestInit & { next?: { revalidate?: number } }; |
| 11 | |
| 12 | function recordingClient(respond: (url: URL) => Response) { |
| 13 | const fetcher = mock(async (input: RequestInfo | URL, _init?: Init) => respond(new URL(String(input)))); |
| 14 | const client = createComposioClient('test-key', { revalidate: 3600, maxRetries: 0, fetch: fetcher }); |
| 15 | return { client, fetcher }; |
| 16 | } |
| 17 | |
| 18 | const page = (items: unknown[], next_cursor: string | null) => |
| 19 | Response.json({ items, next_cursor, total_pages: 2, current_page: next_cursor ? 1 : 2, total_items: 3 }); |
| 20 | |
| 21 | describe('listToolkitTools', () => { |
| 22 | test('follows next_cursor at TOOL_PAGE_SIZE, one cached request per page', async () => { |
| 23 | const { client, fetcher } = recordingClient(url => |
| 24 | url.searchParams.get('cursor') === 'c2' ? page([{ slug: 'C' }], null) : page([{ slug: 'A' }, { slug: 'B' }], 'c2') |
| 25 | ); |
| 26 | |
| 27 | expect(await listToolkitTools(client, 'tableau')).toEqual([{ slug: 'A' }, { slug: 'B' }, { slug: 'C' }]); |
| 28 | |
| 29 | const urls = fetcher.mock.calls.map(([input]) => new URL(String(input))); |
| 30 | expect(urls.map(url => url.pathname)).toEqual(['/api/v3.1/tools', '/api/v3.1/tools']); |
| 31 | expect(urls.map(url => url.searchParams.get('toolkit_slug'))).toEqual(['TABLEAU', 'TABLEAU']); |
| 32 | expect(urls.map(url => url.searchParams.get('limit'))).toEqual([String(TOOL_PAGE_SIZE), String(TOOL_PAGE_SIZE)]); |
| 33 | expect(urls.map(url => url.searchParams.get('cursor'))).toEqual([null, 'c2']); |
| 34 | expect(fetcher.mock.calls.map(([, init]) => new Headers(init?.headers).get('x-api-key'))).toEqual(['test-key', 'test-key']); |
| 35 | expect(fetcher.mock.calls.map(([, init]) => init?.next)).toEqual([{ revalidate: 3600 }, { revalidate: 3600 }]); |
| 36 | }); |
| 37 | }); |
| 38 | |
| 39 | describe('listToolkitTriggers', () => { |
| 40 | test('requests the latest trigger types of one toolkit', async () => { |
| 41 | const { client, fetcher } = recordingClient(() => page([{ slug: 'GITHUB_PUSH' }], null)); |
| 42 | |
| 43 | expect(await listToolkitTriggers(client, 'github')).toEqual([{ slug: 'GITHUB_PUSH' }]); |
| 44 | const url = new URL(String(fetcher.mock.calls[0]?.[0])); |
| 45 | expect(url.pathname).toBe('/api/v3.1/triggers_types'); |
| 46 | expect(url.searchParams.get('toolkit_slugs')).toBe('GITHUB'); |
| 47 | expect(url.searchParams.get('toolkit_versions')).toBe('latest'); |
| 48 | expect(url.searchParams.get('limit')).toBe(String(TRIGGER_PAGE_LIMIT)); |
| 49 | }); |
| 50 | }); |