Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { describe, expect, it } from '@effect/vitest'; |
| 2 | import { ConfigProvider, Effect } from 'effect'; |
| 3 | import { HttpClient, HttpClientResponse } from 'effect/http'; |
| 4 | import { checkApolloContract, renderReport } from '../../scripts/apollo-contract'; |
| 5 | |
| 6 | const config = { |
| 7 | COMPOSIO_BASE_URL: 'https://staging-backend.composio.dev', |
| 8 | COMPOSIO_API_KEY: 'test-project-key', |
| 9 | COMPOSIO_USER_API_KEY: 'test-user-key', |
| 10 | }; |
| 11 | |
| 12 | describe('Apollo contract check', () => { |
| 13 | it.effect.each([401, 403, 429])('stops after HTTP %s without retrying', status => |
| 14 | Effect.gen(function* () { |
| 15 | const requests: string[] = []; |
| 16 | const client = HttpClient.make(request => { |
| 17 | requests.push(request.url); |
| 18 | return Effect.succeed( |
| 19 | HttpClientResponse.fromWeb( |
| 20 | request, |
| 21 | new Response(JSON.stringify({ error: { message: 'rejected' } }), { status }) |
| 22 | ) |
| 23 | ); |
| 24 | }); |
| 25 | const result = yield* checkApolloContract.pipe( |
| 26 | Effect.provideService(HttpClient.HttpClient, client), |
| 27 | Effect.provideService(ConfigProvider.ConfigProvider, ConfigProvider.fromEnvRecord(config)) |
| 28 | ); |
| 29 | expect(requests).toHaveLength(1); |
| 30 | expect(result.results).toHaveLength(10); |
| 31 | expect(result.results[0]?.outcome).toMatchObject({ |
| 32 | status: 'fail', |
| 33 | detail: expect.stringContaining(`HTTP ${status}:`), |
| 34 | }); |
| 35 | expect(result.results[0]?.outcome.detail).toContain('rejected'); |
| 36 | expect(result.results.slice(1).every(result => result.outcome.status === 'not run')).toBe( |
| 37 | true |
| 38 | ); |
| 39 | }) |
| 40 | ); |
| 41 | |
| 42 | it.effect('reports schema paths and continues using the correct credentials', () => |
| 43 | Effect.gen(function* () { |
| 44 | const requests: Array<{ url: string; headers: Readonly<Record<string, string>> }> = []; |
| 45 | const client = HttpClient.make(request => { |
| 46 | requests.push({ url: request.url, headers: request.headers }); |
| 47 | return Effect.succeed(HttpClientResponse.fromWeb(request, Response.json({}))); |
| 48 | }); |
| 49 | const result = yield* checkApolloContract.pipe( |
| 50 | Effect.provideService(HttpClient.HttpClient, client), |
| 51 | Effect.provideService(ConfigProvider.ConfigProvider, ConfigProvider.fromEnvRecord(config)) |
| 52 | ); |
| 53 | expect(requests).toHaveLength(10); |
| 54 | expect(result.results.every(result => result.outcome.status === 'fail')).toBe(true); |
| 55 | expect(result.results[0]?.outcome.detail).toContain('schema mismatch: items:'); |
| 56 | expect( |
| 57 | requests |
| 58 | .slice(0, 8) |
| 59 | .every(request => request.headers['x-api-key'] === config.COMPOSIO_API_KEY) |
| 60 | ).toBe(true); |
| 61 | expect( |
| 62 | requests |
| 63 | .slice(8) |
| 64 | .every(request => request.headers['x-user-api-key'] === config.COMPOSIO_USER_API_KEY) |
| 65 | ).toBe(true); |
| 66 | }) |
| 67 | ); |
| 68 | |
| 69 | it.effect('reports an empty organization list as a failed project check', () => |
| 70 | Effect.gen(function* () { |
| 71 | const client = HttpClient.make(request => |
| 72 | Effect.succeed( |
| 73 | HttpClientResponse.fromWeb( |
| 74 | request, |
| 75 | Response.json( |
| 76 | request.url.includes('/org/list') |
| 77 | ? { organizations: [], total_pages: 1, current_page: 1, total_items: 0 } |
| 78 | : {} |
| 79 | ) |
| 80 | ) |
| 81 | ) |
| 82 | ); |
| 83 | const result = yield* checkApolloContract.pipe( |
| 84 | Effect.provideService(HttpClient.HttpClient, client), |
| 85 | Effect.provideService(ConfigProvider.ConfigProvider, ConfigProvider.fromEnvRecord(config)) |
| 86 | ); |
| 87 | expect(result.results[8]?.outcome.status).toBe('pass'); |
| 88 | expect(result.results[9]?.outcome).toMatchObject({ |
| 89 | status: 'fail', |
| 90 | detail: 'the user key belongs to no organization', |
| 91 | }); |
| 92 | expect(renderReport(result)).toContain('1/10 cases passed'); |
| 93 | }) |
| 94 | ); |
| 95 | |
| 96 | it.effect('keeps hostile error text inside its report cell', () => |
| 97 | Effect.gen(function* () { |
| 98 | const message = |
| 99 | 'boom\r| forged | pass |\n<img src=x> [click](https://evil.test) `code` \\| **bold** &'; |
| 100 | const client = HttpClient.make(request => |
| 101 | Effect.succeed( |
| 102 | HttpClientResponse.fromWeb( |
| 103 | request, |
| 104 | Response.json({ error: { message } }, { status: 401 }) |
| 105 | ) |
| 106 | ) |
| 107 | ); |
| 108 | const result = yield* checkApolloContract.pipe( |
| 109 | Effect.provideService(HttpClient.HttpClient, client), |
| 110 | Effect.provideService(ConfigProvider.ConfigProvider, ConfigProvider.fromEnvRecord(config)) |
| 111 | ); |
| 112 | const report = renderReport(result); |
| 113 | expect(report).not.toContain('\r'); |
| 114 | expect(report).not.toContain('<img'); |
| 115 | expect(report).not.toContain('[click]'); |
| 116 | expect(report).not.toContain('`code`'); |
| 117 | expect(report).not.toContain('**bold**'); |
| 118 | const rows = report.split('\n').filter(line => line.startsWith('|')); |
| 119 | expect(rows).toHaveLength(12); |
| 120 | expect(rows.every(row => row.split('|').length === 5)).toBe(true); |
| 121 | expect(report).toContain('| forged | pass |'); |
| 122 | expect(report).toContain('&amp;'); |
| 123 | }) |
| 124 | ); |
| 125 | |
| 126 | it.effect.each([ |
| 127 | { ...config, COMPOSIO_BASE_URL: 'https://untrusted.example' }, |
| 128 | { ...config, COMPOSIO_API_KEY: '' }, |
| 129 | { ...config, COMPOSIO_USER_API_KEY: undefined }, |
| 130 | ])('rejects invalid configuration before sending credentials', env => |
| 131 | Effect.gen(function* () { |
| 132 | let requests = 0; |
| 133 | const client = HttpClient.make(request => { |
| 134 | requests++; |
| 135 | return Effect.succeed(HttpClientResponse.fromWeb(request, Response.json({}))); |
| 136 | }); |
| 137 | const error = yield* checkApolloContract.pipe( |
| 138 | Effect.provideService(HttpClient.HttpClient, client), |
| 139 | Effect.provideService(ConfigProvider.ConfigProvider, ConfigProvider.fromEnvRecord(env)), |
| 140 | Effect.flip |
| 141 | ); |
| 142 | expect(error).toBeDefined(); |
| 143 | expect(requests).toBe(0); |
| 144 | }) |
| 145 | ); |
| 146 | }); |