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 { expect, test } from 'bun:test'; |
| 2 | import { readFileSync } from 'node:fs'; |
| 3 | import { z } from 'zod'; |
| 4 | |
| 5 | // Exercise the published snippet itself without opening OAuth or making API calls. |
| 6 | const page = readFileSync( |
| 7 | new URL('../../content/examples/restrict-gmail-domain.mdx', import.meta.url), |
| 8 | 'utf8', |
| 9 | ); |
| 10 | const source = page.match(/```typescript title="restrict-gmail.ts"\n([\s\S]*?)\n```/)?.[1]; |
| 11 | if (!source) throw new Error('Missing runnable TypeScript example'); |
| 12 | const compiled = new Bun.Transpiler({ loader: 'ts' }).transformSync( |
| 13 | source.replace(/^import .*;\n/gm, ''), |
| 14 | ); |
| 15 | const runExample = new Function( |
| 16 | 'Composio', 'z', 'process', 'console', |
| 17 | `return (async () => { ${compiled} })();`, |
| 18 | ); |
| 19 | |
| 20 | const cases = [ |
| 21 | { name: 'allowed domain', data: { emailAddress: 'alice@example.com' }, approved: true }, |
| 22 | { name: 'mixed case', data: { emailAddress: 'alice@EXAMPLE.COM' }, approved: true }, |
| 23 | { name: 'personal Gmail', data: { emailAddress: 'alice@gmail.com' }, approved: false }, |
| 24 | { name: 'suffix match', data: { emailAddress: 'alice@notexample.com' }, approved: false }, |
| 25 | { name: 'subdomain', data: { emailAddress: 'alice@sub.example.com' }, approved: false }, |
| 26 | { name: 'extra suffix', data: { emailAddress: 'alice@example.com.evil.test' }, approved: false }, |
| 27 | { name: 'missing email', data: {}, approved: false }, |
| 28 | { name: 'null profile', data: null, approved: false }, |
| 29 | { name: 'non-string email', data: { emailAddress: 42 }, approved: false }, |
| 30 | { name: 'empty local part', data: { emailAddress: '@example.com' }, approved: false }, |
| 31 | { name: 'multiple at signs', data: { emailAddress: 'alice@other@example.com' }, approved: false }, |
| 32 | { name: 'display-name syntax', data: { emailAddress: 'Alice <alice@example.com>' }, approved: false }, |
| 33 | { name: 'whitespace', data: { emailAddress: 'alice @example.com' }, approved: false }, |
| 34 | ]; |
| 35 | |
| 36 | for (const scenario of cases) { |
| 37 | test(`Gmail domain example: ${scenario.name}`, async () => { |
| 38 | await checkExample(scenario.data, scenario.approved); |
| 39 | }); |
| 40 | } |
| 41 | |
| 42 | test('Gmail domain example rejects upstream errors and cleanup failures', async () => { |
| 43 | await checkExample({ emailAddress: 'alice@example.com' }, false, 'proxy'); |
| 44 | await checkExample({ emailAddress: 'alice@example.com' }, false, 'status'); |
| 45 | await checkExample({ emailAddress: 'alice@example.com' }, false, 'timeout'); |
| 46 | await checkExample({ emailAddress: 'alice@gmail.com' }, false, 'disable'); |
| 47 | }); |
| 48 | |
| 49 | async function checkExample( |
| 50 | data: unknown, |
| 51 | approved: boolean, |
| 52 | failure?: 'proxy' | 'status' | 'timeout' | 'disable', |
| 53 | ) { |
| 54 | const events: string[] = []; |
| 55 | const accountId = 'ca_new_connection'; |
| 56 | class Composio { |
| 57 | connectedAccounts = { |
| 58 | disable: async (id: string) => { |
| 59 | expect(id).toBe(accountId); |
| 60 | events.push('disable'); |
| 61 | if (failure === 'disable') throw new Error('disable failed'); |
| 62 | }, |
| 63 | }; |
| 64 | tools = { |
| 65 | proxyExecute: async (params: Record<string, unknown>) => { |
| 66 | expect(params).toEqual({ |
| 67 | endpoint: 'https://gmail.googleapis.com/gmail/v1/users/me/profile', |
| 68 | method: 'GET', |
| 69 | connectedAccountId: accountId, |
| 70 | }); |
| 71 | events.push('profile'); |
| 72 | if (failure === 'proxy') throw new Error('network failure'); |
| 73 | return { status: failure === 'status' ? 403 : 200, data }; |
| 74 | }, |
| 75 | }; |
| 76 | async create(userId: string, config: Record<string, unknown>) { |
| 77 | expect(userId).toBe('demo'); |
| 78 | expect(config.toolkits).toEqual(['gmail']); |
| 79 | expect(config.manageConnections).toBe(false); |
| 80 | expect(config.sandbox).toEqual({ enable: false }); |
| 81 | if (events.length === 0) { |
| 82 | events.push('onboarding'); |
| 83 | } else { |
| 84 | expect(approved).toBe(true); |
| 85 | expect(events).toEqual(['onboarding', 'wait', 'profile']); |
| 86 | expect(config.connectedAccounts).toEqual({ gmail: [accountId] }); |
| 87 | events.push('agent-session'); |
| 88 | } |
| 89 | return { |
| 90 | authorize: async (toolkit: string) => { |
| 91 | expect(toolkit).toBe('gmail'); |
| 92 | return { |
| 93 | id: accountId, |
| 94 | redirectUrl: 'https://example.test/connect', |
| 95 | waitForConnection: async (timeout: number) => { |
| 96 | expect(timeout).toBe(300_000); |
| 97 | events.push('wait'); |
| 98 | if (failure === 'timeout') throw new Error('timed out'); |
| 99 | return { id: accountId }; |
| 100 | }, |
| 101 | }; |
| 102 | }, |
| 103 | tools: async () => { |
| 104 | expect(events.at(-1)).toBe('agent-session'); |
| 105 | events.push('tools'); |
| 106 | return []; |
| 107 | }, |
| 108 | }; |
| 109 | } |
| 110 | } |
| 111 | const result = runExample(Composio, z, { env: { COMPOSIO_USER_ID: 'demo' } }, { log() {} }); |
| 112 | if (approved) { |
| 113 | await result; |
| 114 | expect(events).toEqual(['onboarding', 'wait', 'profile', 'agent-session', 'tools']); |
| 115 | } else { |
| 116 | await expect(result).rejects.toThrow(failure === 'disable' ? 'disable failed' : 'Connection disabled'); |
| 117 | expect(events.at(-1)).toBe('disable'); |
| 118 | expect(events).not.toContain('agent-session'); |
| 119 | expect(events).not.toContain('tools'); |
| 120 | } |
| 121 | } |