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
| 1 | /** |
| 2 | * Keeps the fake backend honest against `@composio/client`. |
| 3 | * |
| 4 | * Every route named after a client operation is called once per sample below, |
| 5 | * and its answer is decoded with the response schema the client itself ships |
| 6 | * for that operation. A fake response the real client would reject fails here, |
| 7 | * before any CLI suite builds on it. |
| 8 | */ |
| 9 | |
| 10 | import { afterAll, beforeAll, describe, expect, it } from 'bun:test'; |
| 11 | import { schemaLoaders } from '@composio/client/effect/schemas'; |
| 12 | import * as Schema from 'effect/Schema'; |
| 13 | import { |
| 14 | CONSUMER_PROJECT, |
| 15 | CONSUMER_USER_ID, |
| 16 | DEVELOPER_PROJECT, |
| 17 | GLOBAL_TEST_USER_ID, |
| 18 | ORG, |
| 19 | ROUTES, |
| 20 | startFakeComposio, |
| 21 | USER_API_KEY, |
| 22 | type FakeComposio, |
| 23 | type World, |
| 24 | } from './src/index'; |
| 25 | |
| 26 | type OperationName = keyof typeof schemaLoaders; |
| 27 | |
| 28 | const isOperationName = (name: string): name is OperationName => name in schemaLoaders; |
| 29 | |
| 30 | /** Decodes like the client does when a call asks for validation; throws on the first mismatch. */ |
| 31 | const decodeWith = (schema: Schema.Codec<unknown, unknown, never, never>, json: unknown): unknown => |
| 32 | Schema.decodeUnknownSync(schema)(json); |
| 33 | |
| 34 | interface Sample { |
| 35 | /** The operation whose route the request must reach. */ |
| 36 | readonly operation: OperationName; |
| 37 | /** What makes this sample different from another one of the same operation. */ |
| 38 | readonly variant: string; |
| 39 | readonly method: 'GET' | 'POST'; |
| 40 | /** May read ids from state the `prepare` step created. */ |
| 41 | readonly path: (world: World) => string; |
| 42 | readonly body?: unknown; |
| 43 | readonly project?: 'consumer' | 'developer'; |
| 44 | readonly expectStatus?: number; |
| 45 | /** Seeds what the request needs and is undone by the next sample's fresh world. */ |
| 46 | readonly prepare?: (world: World) => void; |
| 47 | } |
| 48 | |
| 49 | const SLACK_ARGUMENTS = { channel: 'general', text: 'hello' }; |
| 50 | |
| 51 | const withConsumerSession = (world: World) => { |
| 52 | world.createToolRouterSession(CONSUMER_PROJECT.label, CONSUMER_USER_ID); |
| 53 | }; |
| 54 | |
| 55 | const withConnectedConsumerSession = (world: World) => { |
| 56 | withConsumerSession(world); |
| 57 | world.connect(CONSUMER_PROJECT.label, CONSUMER_USER_ID, 'slack'); |
| 58 | }; |
| 59 | |
| 60 | const SESSION = '/api/v3.1/tool_router/session/trs_e2e0001'; |
| 61 | |
| 62 | const SAMPLES: ReadonlyArray<Sample> = [ |
| 63 | { |
| 64 | operation: 'auth.session.retrieveInfo', |
| 65 | variant: 'user key', |
| 66 | method: 'GET', |
| 67 | path: () => '/api/v3.1/auth/session/info', |
| 68 | }, |
| 69 | { operation: 'org.list', variant: 'two orgs', method: 'GET', path: () => '/api/v3.1/org/list' }, |
| 70 | { |
| 71 | operation: 'org.project.list', |
| 72 | variant: 'developer projects', |
| 73 | method: 'GET', |
| 74 | path: () => '/api/v3.1/org/project/list', |
| 75 | }, |
| 76 | { |
| 77 | operation: '_internal.org.consumer.project.resolve', |
| 78 | variant: 'consumer project', |
| 79 | method: 'POST', |
| 80 | path: () => '/api/v3.1/org/consumer/project/resolve', |
| 81 | }, |
| 82 | { |
| 83 | operation: '_internal.org.consumer.listConnectedToolkits', |
| 84 | variant: 'one connected toolkit', |
| 85 | method: 'GET', |
| 86 | path: () => `/api/v3.1/org/consumer/connected_toolkits?user_id=${CONSUMER_USER_ID}`, |
| 87 | prepare: world => void world.connect(CONSUMER_PROJECT.label, CONSUMER_USER_ID, 'slack'), |
| 88 | }, |
| 89 | { |
| 90 | operation: '_internal.org.consumer.retrieveConfig', |
| 91 | variant: 'defaults', |
| 92 | method: 'GET', |
| 93 | path: () => '/api/v3.1/org/consumer/config', |
| 94 | }, |
| 95 | { |
| 96 | operation: '_internal.consumer.permissions.resolve', |
| 97 | variant: 'allow all', |
| 98 | method: 'POST', |
| 99 | path: () => '/api/v3.1/consumer/permissions/resolve', |
| 100 | body: {}, |
| 101 | }, |
| 102 | { |
| 103 | operation: '_internal.cli.createSession', |
| 104 | variant: 'pending', |
| 105 | method: 'POST', |
| 106 | path: () => '/api/v3.1/cli/create-session', |
| 107 | body: { scope: 'user' }, |
| 108 | }, |
| 109 | { |
| 110 | operation: '_internal.cli.getSession', |
| 111 | variant: 'pending', |
| 112 | method: 'GET', |
| 113 | path: world => `/api/v3.1/cli/get-session?id=${world.loginSessions[0]?.id}`, |
| 114 | prepare: world => void world.createLoginSession(), |
| 115 | }, |
| 116 | { |
| 117 | operation: '_internal.cli.getSession', |
| 118 | variant: 'linked', |
| 119 | method: 'GET', |
| 120 | path: world => `/api/v3.1/cli/get-session?id=${world.loginSessions[0]?.id}`, |
| 121 | prepare: world => { |
| 122 | world.createLoginSession(); |
| 123 | world.linkLoginSession(); |
| 124 | }, |
| 125 | }, |
| 126 | { |
| 127 | operation: 'toolkits.list', |
| 128 | variant: 'all toolkits', |
| 129 | method: 'GET', |
| 130 | path: () => '/api/v3.1/toolkits', |
| 131 | }, |
| 132 | { |
| 133 | operation: 'toolkits.retrieve', |
| 134 | variant: 'oauth toolkit', |
| 135 | method: 'GET', |
| 136 | path: () => '/api/v3.1/toolkits/slack', |
| 137 | }, |
| 138 | { |
| 139 | operation: 'toolkits.retrieve', |
| 140 | variant: 'no-auth toolkit', |
| 141 | method: 'GET', |
| 142 | path: () => '/api/v3.1/toolkits/hackernews', |
| 143 | }, |
| 144 | { operation: 'tools.list', variant: 'all tools', method: 'GET', path: () => '/api/v3.1/tools' }, |
| 145 | { |
| 146 | operation: 'tools.retrieve', |
| 147 | variant: 'one tool', |
| 148 | method: 'GET', |
| 149 | path: () => '/api/v3.1/tools/SLACK_SEND_MESSAGE', |
| 150 | }, |
| 151 | { |
| 152 | operation: 'toolRouter.session.create', |
| 153 | variant: 'consumer user', |
| 154 | method: 'POST', |
| 155 | path: () => '/api/v3.1/tool_router/session', |
| 156 | body: { user_id: CONSUMER_USER_ID }, |
| 157 | project: 'consumer', |
| 158 | expectStatus: 201, |
| 159 | }, |
| 160 | { |
| 161 | operation: 'toolRouter.session.search', |
| 162 | variant: 'not connected', |
| 163 | method: 'POST', |
| 164 | path: () => `${SESSION}/search`, |
| 165 | body: { queries: [{ use_case: 'send a slack message' }] }, |
| 166 | project: 'consumer', |
| 167 | prepare: withConsumerSession, |
| 168 | }, |
| 169 | { |
| 170 | operation: 'toolRouter.session.search', |
| 171 | variant: 'connected', |
| 172 | method: 'POST', |
| 173 | path: () => `${SESSION}/search`, |
| 174 | body: { queries: [{ use_case: 'send a slack message' }] }, |
| 175 | project: 'consumer', |
| 176 | prepare: withConnectedConsumerSession, |
| 177 | }, |
| 178 | { |
| 179 | operation: 'toolRouter.session.execute', |
| 180 | variant: 'completed', |
| 181 | method: 'POST', |
| 182 | path: () => `${SESSION}/execute`, |
| 183 | body: { tool_slug: 'SLACK_SEND_MESSAGE', arguments: SLACK_ARGUMENTS }, |
| 184 | project: 'consumer', |
| 185 | prepare: withConnectedConsumerSession, |
| 186 | }, |
| 187 | { |
| 188 | operation: 'toolRouter.session.executeMeta', |
| 189 | variant: 'completed', |
| 190 | method: 'POST', |
| 191 | path: () => `${SESSION}/execute_meta`, |
| 192 | body: { slug: 'COMPOSIO_SEARCH_TOOLS', arguments: {} }, |
| 193 | project: 'consumer', |
| 194 | prepare: withConsumerSession, |
| 195 | }, |
| 196 | { |
| 197 | operation: 'toolRouter.session.link', |
| 198 | variant: 'new connection', |
| 199 | method: 'POST', |
| 200 | path: () => `${SESSION}/link`, |
| 201 | body: { toolkit: 'slack' }, |
| 202 | project: 'consumer', |
| 203 | expectStatus: 201, |
| 204 | prepare: withConsumerSession, |
| 205 | }, |
| 206 | { |
| 207 | operation: 'toolRouter.session.toolkits', |
| 208 | variant: 'one connected', |
| 209 | method: 'GET', |
| 210 | path: () => `${SESSION}/toolkits`, |
| 211 | project: 'consumer', |
| 212 | prepare: withConnectedConsumerSession, |
| 213 | }, |
| 214 | { |
| 215 | operation: 'authConfigs.list', |
| 216 | variant: 'managed configs', |
| 217 | method: 'GET', |
| 218 | path: () => '/api/v3.1/auth_configs', |
| 219 | project: 'developer', |
| 220 | }, |
| 221 | { |
| 222 | operation: 'authConfigs.retrieve', |
| 223 | variant: 'managed config', |
| 224 | method: 'GET', |
| 225 | path: world => `/api/v3.1/auth_configs/${world.authConfigs[0]?.id}`, |
| 226 | project: 'developer', |
| 227 | }, |
| 228 | { |
| 229 | operation: 'connectedAccounts.list', |
| 230 | variant: 'active and pending', |
| 231 | method: 'GET', |
| 232 | path: () => '/api/v3.1/connected_accounts', |
| 233 | project: 'developer', |
| 234 | prepare: world => { |
| 235 | world.connect(DEVELOPER_PROJECT.label, GLOBAL_TEST_USER_ID, 'slack'); |
| 236 | world.linkCompletesAfterPolls = null; |
| 237 | world.beginConnection({ |
| 238 | projectLabel: DEVELOPER_PROJECT.label, |
| 239 | userId: GLOBAL_TEST_USER_ID, |
| 240 | toolkit: 'github', |
| 241 | }); |
| 242 | }, |
| 243 | }, |
| 244 | { |
| 245 | operation: 'connectedAccounts.retrieve', |
| 246 | variant: 'active', |
| 247 | method: 'GET', |
| 248 | path: world => `/api/v3.1/connected_accounts/${world.connections[0]?.id}`, |
| 249 | project: 'developer', |
| 250 | prepare: world => void world.connect(DEVELOPER_PROJECT.label, GLOBAL_TEST_USER_ID, 'slack'), |
| 251 | }, |
| 252 | { |
| 253 | operation: 'link.create', |
| 254 | variant: 'new connection', |
| 255 | method: 'POST', |
| 256 | path: () => '/api/v3.1/connected_accounts/link', |
| 257 | body: { auth_config_id: 'ac_e2e0001', user_id: GLOBAL_TEST_USER_ID }, |
| 258 | project: 'developer', |
| 259 | expectStatus: 201, |
| 260 | }, |
| 261 | { |
| 262 | operation: 'tools.execute', |
| 263 | variant: 'successful', |
| 264 | method: 'POST', |
| 265 | path: () => '/api/v3.1/tools/execute/SLACK_SEND_MESSAGE', |
| 266 | body: { user_id: GLOBAL_TEST_USER_ID, arguments: SLACK_ARGUMENTS }, |
| 267 | project: 'developer', |
| 268 | prepare: world => void world.connect(DEVELOPER_PROJECT.label, GLOBAL_TEST_USER_ID, 'slack'), |
| 269 | }, |
| 270 | ]; |
| 271 | |
| 272 | const PROJECT_IDS = { |
| 273 | consumer: CONSUMER_PROJECT.nanoId, |
| 274 | developer: DEVELOPER_PROJECT.nanoId, |
| 275 | } as const; |
| 276 | |
| 277 | describe('fake backend contract', () => { |
| 278 | let fake: FakeComposio; |
| 279 | |
| 280 | const call = async (sample: Sample): Promise<Response> => { |
| 281 | // A fresh world per sample: ids in sample paths assume nothing ran before. |
| 282 | await fake?.close(); |
| 283 | fake = await startFakeComposio(); |
| 284 | sample.prepare?.(fake.world); |
| 285 | return fetch(`${fake.host.http}${sample.path(fake.world)}`, { |
| 286 | method: sample.method, |
| 287 | headers: { |
| 288 | 'content-type': 'application/json', |
| 289 | 'x-user-api-key': USER_API_KEY, |
| 290 | 'x-org-id': ORG.id, |
| 291 | ...(sample.project ? { 'x-project-id': PROJECT_IDS[sample.project] } : {}), |
| 292 | }, |
| 293 | body: sample.body === undefined ? undefined : JSON.stringify(sample.body), |
| 294 | }); |
| 295 | }; |
| 296 | |
| 297 | beforeAll(async () => { |
| 298 | fake = await startFakeComposio(); |
| 299 | }); |
| 300 | |
| 301 | afterAll(async () => { |
| 302 | await fake.close(); |
| 303 | }); |
| 304 | |
| 305 | for (const sample of SAMPLES) { |
| 306 | it(`${sample.operation} (${sample.variant}) decodes with the client's schema`, async () => { |
| 307 | const response = await call(sample); |
| 308 | const json: unknown = await response.json(); |
| 309 | expect({ status: response.status, json }).toMatchObject({ |
| 310 | status: sample.expectStatus ?? 200, |
| 311 | }); |
| 312 | expect(fake.journal.entries().at(-1)?.route).toBe(sample.operation); |
| 313 | |
| 314 | const schema = await schemaLoaders[sample.operation](); |
| 315 | // Throws with the path of the first field the client would reject. |
| 316 | decodeWith(schema, json); |
| 317 | }); |
| 318 | } |
| 319 | |
| 320 | it('has a sample for every route that claims a client operation', () => { |
| 321 | const sampled = new Set<string>(SAMPLES.map(sample => sample.operation)); |
| 322 | const unsampled = ROUTES.filter(route => route.typed && !sampled.has(route.name)).map( |
| 323 | route => route.name |
| 324 | ); |
| 325 | expect(unsampled).toEqual([]); |
| 326 | }); |
| 327 | |
| 328 | it('names only real client operations on typed routes', () => { |
| 329 | const unknown = ROUTES.filter(route => route.typed && !isOperationName(route.name)).map( |
| 330 | route => route.name |
| 331 | ); |
| 332 | expect(unknown).toEqual([]); |
| 333 | }); |
| 334 | |
| 335 | it('rejects a response that drifts from the client schema', async () => { |
| 336 | // Guards the guard: a decoder that accepts anything would make this file vacuous. |
| 337 | const schema = await schemaLoaders['toolRouter.session.link'](); |
| 338 | expect(() => decodeWith(schema, { link_token: 'lt_1' })).toThrow(); |
| 339 | }); |
| 340 | }); |