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, test } from 'bun:test'; |
| 2 | import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'; |
| 3 | import { tmpdir } from 'node:os'; |
| 4 | import { join, resolve } from 'node:path'; |
| 5 | import { renderToStaticMarkup } from 'react-dom/server'; |
| 6 | |
| 7 | import { GET } from '../../app/llms.mdx/[[...slug]]/route'; |
| 8 | import { MetaToolDetail } from '../../components/meta-tools/meta-tool-detail'; |
| 9 | import { getMetaToolBySlug } from '../../lib/meta-tools-data'; |
| 10 | |
| 11 | describe('proxy execute meta-tool reference', () => { |
| 12 | const exportUrl = 'https://backend.composio.dev/meta-tools.json'; |
| 13 | const tool = { |
| 14 | slug: 'COMPOSIO_PROXY_EXECUTE', |
| 15 | name: 'proxy_execute', |
| 16 | displayName: 'Proxy Execute', |
| 17 | description: 'Read from the snapshot', |
| 18 | tags: [], |
| 19 | toolkit: null, |
| 20 | inputParameters: { type: 'object', properties: { body: { description: 'Any JSON' } } }, |
| 21 | responseSchema: { type: 'object', properties: {} }, |
| 22 | }; |
| 23 | |
| 24 | test.each([ |
| 25 | ['checked-in snapshot', ''], |
| 26 | ['read-only export', exportUrl], |
| 27 | ['invalid export', `${exportUrl}?empty`], |
| 28 | ])('generation handles the %s', async (_name, source) => { |
| 29 | const directory = await mkdtemp(join(tmpdir(), 'composio-meta-tools-')); |
| 30 | const transport = join(directory, 'transport.ts'); |
| 31 | const snapshotPath = join(directory, 'public/data/meta-tools.json'); |
| 32 | const snapshot = JSON.stringify([tool]); |
| 33 | const exportedTool = { ...tool, description: 'Read from the export' }; |
| 34 | const invalidExport = source.endsWith('?empty'); |
| 35 | |
| 36 | try { |
| 37 | await mkdir(join(directory, 'public/data'), { recursive: true }); |
| 38 | await writeFile(snapshotPath, snapshot); |
| 39 | await writeFile(transport, ` |
| 40 | globalThis.fetch = async (input, init) => { |
| 41 | const request = new Request(input, init); |
| 42 | const source = process.env.COMPOSIO_META_TOOLS_SCHEMA_URL; |
| 43 | if (!source || request.url !== source || request.method !== 'GET' || |
| 44 | request.headers.has('x-api-key') || request.headers.has('authorization')) { |
| 45 | throw new Error('Unexpected request from the meta-tool generator'); |
| 46 | } |
| 47 | return Response.json(${JSON.stringify(invalidExport ? [] : [exportedTool])}); |
| 48 | }; |
| 49 | `); |
| 50 | const child = Bun.spawn([ |
| 51 | process.execPath, '--preload', transport, resolve('scripts/generate-meta-tools.ts'), |
| 52 | ], { |
| 53 | cwd: directory, |
| 54 | env: { |
| 55 | ...process.env, |
| 56 | COMPOSIO_API_KEY: source ? 'test-key' : '', |
| 57 | COMPOSIO_META_TOOLS_SCHEMA_URL: source, |
| 58 | }, |
| 59 | stdout: 'pipe', |
| 60 | stderr: 'pipe', |
| 61 | }); |
| 62 | const [exitCode, stdout, stderr] = await Promise.all([ |
| 63 | child.exited, new Response(child.stdout).text(), new Response(child.stderr).text(), |
| 64 | ]); |
| 65 | if (invalidExport) { |
| 66 | expect(exitCode).toBe(1); |
| 67 | expect(stderr).toContain('ZodError'); |
| 68 | expect(await readFile(snapshotPath, 'utf8')).toBe(snapshot); |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | expect(exitCode, `${stdout}\n${stderr}`).toBe(0); |
| 73 | expect(await readFile(join(directory, 'content/toolkits/meta-tools/index.mdx'), 'utf8')) |
| 74 | .toContain('/toolkits/meta-tools/proxy_execute'); |
| 75 | expect(await readFile(join(directory, 'content/toolkits/meta-tools/proxy_execute.mdx'), 'utf8')) |
| 76 | .toContain('<MetaToolDetailServer slug="COMPOSIO_PROXY_EXECUTE" />'); |
| 77 | const saved = await readFile(snapshotPath, 'utf8'); |
| 78 | if (source) { |
| 79 | expect(JSON.parse(saved)).toEqual([exportedTool]); |
| 80 | } else { |
| 81 | expect(saved).toBe(snapshot); |
| 82 | } |
| 83 | } finally { |
| 84 | await rm(directory, { recursive: true, force: true }); |
| 85 | } |
| 86 | }); |
| 87 | |
| 88 | test('HTML labels unrestricted request bodies as any', async () => { |
| 89 | const tool = await getMetaToolBySlug('COMPOSIO_PROXY_EXECUTE'); |
| 90 | expect(tool).not.toBeNull(); |
| 91 | const html = renderToStaticMarkup(<MetaToolDetail tool={tool!} />); |
| 92 | expect(html).toMatch(/>body<\/code><span[^>]*>any<\/span>/); |
| 93 | expect(html).toMatch(/>parameters<\/code><span[^>]*>array<object><\/span>/); |
| 94 | expect(html).toContain('HEAD'); |
| 95 | }); |
| 96 | |
| 97 | test('Markdown includes usage semantics and unrestricted nested response bodies', async () => { |
| 98 | const response = await GET( |
| 99 | new Request('https://docs.composio.dev/toolkits/meta-tools/proxy_execute.md'), |
| 100 | { params: Promise.resolve({ slug: ['toolkits', 'meta-tools', 'proxy_execute'] }) }, |
| 101 | ); |
| 102 | expect(response.status).toBe(200); |
| 103 | const markdown = await response.text(); |
| 104 | expect(markdown).toContain('`body` (any)'); |
| 105 | expect(markdown).toContain('`data` (any)'); |
| 106 | expect(markdown).toContain('`parameters` (array<object>)'); |
| 107 | expect(markdown).not.toContain('(undefined)'); |
| 108 | expect(markdown).toContain('`successful: false`'); |
| 109 | expect(markdown).toContain('non-2xx'); |
| 110 | expect(markdown).toContain('`type` instead of `in`'); |
| 111 | }); |
| 112 | }); |