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, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | import { mockClient } from '../utils/mocks/client.mock'; |
| 3 | import { toolMocks } from '../utils/mocks/data.mock'; |
| 4 | import { Tools } from '../../src/models/Tools'; |
| 5 | import ComposioClient, { APIUserAbortError } from '@composio/client'; |
| 6 | import { MockProvider } from '../utils/mocks/provider.mock'; |
| 7 | import { ComposioRequestCancelledError } from '../../src/errors/SDKErrors'; |
| 8 | import type { SessionContext } from '../../src/types/customTool.types'; |
| 9 | import { ComposioToolExecutionError } from '../../src/errors/ToolErrors'; |
| 10 | |
| 11 | describe('Cancellation — error normalization', () => { |
| 12 | let tools: Tools<unknown, unknown, MockProvider>; |
| 13 | |
| 14 | beforeEach(() => { |
| 15 | vi.clearAllMocks(); |
| 16 | tools = new Tools(mockClient as unknown as ComposioClient, { provider: new MockProvider() }); |
| 17 | }); |
| 18 | |
| 19 | it('translates a real APIUserAbortError (which inherits name==="Error") into ComposioRequestCancelledError', async () => { |
| 20 | const realAbort = new APIUserAbortError(); |
| 21 | expect(realAbort.name).toBe('Error'); |
| 22 | |
| 23 | mockClient.tools.list.mockImplementationOnce(async () => { |
| 24 | throw realAbort; |
| 25 | }); |
| 26 | |
| 27 | await expect( |
| 28 | tools.getRawComposioTools({ search: 'send email' }, undefined, { |
| 29 | signal: AbortSignal.abort(), |
| 30 | }) |
| 31 | ).rejects.toBeInstanceOf(ComposioRequestCancelledError); |
| 32 | }); |
| 33 | |
| 34 | it('translates a generic AbortError into ComposioRequestCancelledError', async () => { |
| 35 | mockClient.tools.list.mockImplementationOnce(async () => { |
| 36 | const err = new Error('The operation was aborted'); |
| 37 | err.name = 'AbortError'; |
| 38 | throw err; |
| 39 | }); |
| 40 | |
| 41 | await expect( |
| 42 | tools.getRawComposioTools({ search: 'send email' }, undefined, { |
| 43 | signal: AbortSignal.abort(), |
| 44 | }) |
| 45 | ).rejects.toBeInstanceOf(ComposioRequestCancelledError); |
| 46 | }); |
| 47 | |
| 48 | it('walks error.cause to detect a wrapped abort', async () => { |
| 49 | mockClient.tools.list.mockImplementationOnce(async () => { |
| 50 | const inner = new APIUserAbortError(); |
| 51 | const outer = new Error('Request failed during retry', { cause: inner }); |
| 52 | outer.name = 'APIError'; |
| 53 | throw outer; |
| 54 | }); |
| 55 | |
| 56 | await expect( |
| 57 | tools.getRawComposioTools({ search: 'send email' }, undefined, { |
| 58 | signal: AbortSignal.abort(), |
| 59 | }) |
| 60 | ).rejects.toBeInstanceOf(ComposioRequestCancelledError); |
| 61 | }); |
| 62 | |
| 63 | it('does NOT relabel an unrelated failure as cancellation even if the signal aborts after it', async () => { |
| 64 | // TOCTOU guard: classification keys on the error being abort-shaped, not just |
| 65 | // on signal.aborted. A genuine failure (e.g. a 500) that happens to coincide |
| 66 | // with a late abort must surface as-is, never as a caller cancellation. |
| 67 | const controller = new AbortController(); |
| 68 | const serverError = new Error('Internal Server Error'); |
| 69 | mockClient.tools.list.mockImplementationOnce(async () => { |
| 70 | controller.abort(); |
| 71 | throw serverError; |
| 72 | }); |
| 73 | |
| 74 | await expect( |
| 75 | tools.getRawComposioTools({ search: 'send email' }, undefined, { |
| 76 | signal: controller.signal, |
| 77 | }) |
| 78 | ).rejects.toBe(serverError); |
| 79 | }); |
| 80 | |
| 81 | it('abort during execute surfaces as ComposioRequestCancelledError, not ComposioToolExecutionError', async () => { |
| 82 | const controller = new AbortController(); |
| 83 | vi.spyOn(tools, 'getRawComposioToolBySlug').mockResolvedValueOnce( |
| 84 | toolMocks.transformedTool as never |
| 85 | ); |
| 86 | mockClient.tools.execute.mockImplementationOnce(async () => { |
| 87 | controller.abort(); |
| 88 | const err = new Error('Request was aborted'); |
| 89 | err.name = 'APIUserAbortError'; |
| 90 | throw err; |
| 91 | }); |
| 92 | |
| 93 | try { |
| 94 | await tools.execute( |
| 95 | 'COMPOSIO_TOOL', |
| 96 | { userId: 'user_1', arguments: {}, dangerouslySkipVersionCheck: true }, |
| 97 | { signal: controller.signal } |
| 98 | ); |
| 99 | throw new Error('expected throw'); |
| 100 | } catch (err) { |
| 101 | expect(err).toBeInstanceOf(ComposioRequestCancelledError); |
| 102 | expect(err).not.toBeInstanceOf(ComposioToolExecutionError); |
| 103 | } |
| 104 | }); |
| 105 | |
| 106 | it('abort during schema retrieval surfaces as ComposioRequestCancelledError, not ComposioToolNotFoundError', async () => { |
| 107 | mockClient.tools.retrieve.mockReset(); |
| 108 | mockClient.tools.retrieve.mockImplementationOnce(async () => { |
| 109 | const err = new Error('Request was aborted'); |
| 110 | err.name = 'APIUserAbortError'; |
| 111 | throw err; |
| 112 | }); |
| 113 | |
| 114 | await expect( |
| 115 | tools.getRawComposioToolBySlug('GITHUB_GET_REPOS', undefined, { |
| 116 | signal: AbortSignal.abort(), |
| 117 | }) |
| 118 | ).rejects.toBeInstanceOf(ComposioRequestCancelledError); |
| 119 | }); |
| 120 | |
| 121 | it('ComposioRequestCancelledError carries a non-empty message', async () => { |
| 122 | mockClient.tools.list.mockReset(); |
| 123 | mockClient.tools.list.mockImplementationOnce(async () => { |
| 124 | const err = new Error('The operation was aborted'); |
| 125 | err.name = 'APIUserAbortError'; |
| 126 | throw err; |
| 127 | }); |
| 128 | |
| 129 | try { |
| 130 | await tools.getRawComposioTools({ search: 'send email' }, undefined, { |
| 131 | signal: AbortSignal.abort(), |
| 132 | }); |
| 133 | throw new Error('expected throw'); |
| 134 | } catch (err) { |
| 135 | expect(err).toBeInstanceOf(ComposioRequestCancelledError); |
| 136 | expect((err as Error).message).toMatch(/cancelled/i); |
| 137 | } |
| 138 | }); |
| 139 | }); |
| 140 | |
| 141 | describe('Cancellation — execute signal forwarding', () => { |
| 142 | let tools: Tools<unknown, unknown, MockProvider>; |
| 143 | |
| 144 | beforeEach(() => { |
| 145 | vi.clearAllMocks(); |
| 146 | tools = new Tools(mockClient as unknown as ComposioClient, { provider: new MockProvider() }); |
| 147 | }); |
| 148 | |
| 149 | it('forwards signal to client.tools.execute and schema lookup', async () => { |
| 150 | const controller = new AbortController(); |
| 151 | const requestOptions = { signal: controller.signal }; |
| 152 | |
| 153 | const getRawSpy = vi.spyOn(tools, 'getRawComposioToolBySlug'); |
| 154 | getRawSpy.mockResolvedValueOnce(toolMocks.transformedTool as never); |
| 155 | mockClient.tools.execute.mockResolvedValueOnce(toolMocks.rawToolExecuteResponse); |
| 156 | |
| 157 | await tools.execute( |
| 158 | 'COMPOSIO_TOOL', |
| 159 | { userId: 'user_1', arguments: {}, dangerouslySkipVersionCheck: true }, |
| 160 | requestOptions |
| 161 | ); |
| 162 | |
| 163 | expect(mockClient.tools.execute).toHaveBeenCalledWith('COMPOSIO_TOOL', expect.any(Object), { |
| 164 | ...requestOptions, |
| 165 | maxRetries: 0, |
| 166 | }); |
| 167 | expect(getRawSpy).toHaveBeenCalledWith('COMPOSIO_TOOL', expect.any(Object), requestOptions); |
| 168 | }); |
| 169 | |
| 170 | it('pre-aborted signal short-circuits before modifiers run', async () => { |
| 171 | vi.spyOn(tools, 'getRawComposioToolBySlug').mockResolvedValueOnce( |
| 172 | toolMocks.transformedTool as never |
| 173 | ); |
| 174 | |
| 175 | const controller = new AbortController(); |
| 176 | controller.abort(); |
| 177 | |
| 178 | await expect( |
| 179 | tools.execute( |
| 180 | 'COMPOSIO_TOOL', |
| 181 | { userId: 'user_1', arguments: {}, dangerouslySkipVersionCheck: true }, |
| 182 | { beforeExecute: vi.fn(p => p.params), signal: controller.signal } |
| 183 | ) |
| 184 | ).rejects.toBeInstanceOf(ComposioRequestCancelledError); |
| 185 | }); |
| 186 | |
| 187 | it('runs afterExecute to completion when the signal fires after the tool already executed', async () => { |
| 188 | const controller = new AbortController(); |
| 189 | |
| 190 | vi.spyOn(tools, 'getRawComposioToolBySlug').mockResolvedValueOnce( |
| 191 | toolMocks.transformedTool as never |
| 192 | ); |
| 193 | // The remote tool executes successfully, then the caller's signal fires |
| 194 | // (e.g. an AbortSignal.timeout) before post-processing runs. |
| 195 | mockClient.tools.execute.mockImplementationOnce(async () => { |
| 196 | controller.abort(); |
| 197 | return toolMocks.rawToolExecuteResponse; |
| 198 | }); |
| 199 | |
| 200 | const afterExecute = vi.fn(({ result }) => result); |
| 201 | |
| 202 | const result = await tools.execute( |
| 203 | 'COMPOSIO_TOOL', |
| 204 | { userId: 'user_1', arguments: {}, dangerouslySkipVersionCheck: true }, |
| 205 | { afterExecute, signal: controller.signal } |
| 206 | ); |
| 207 | |
| 208 | // The tool already committed (the side effect happened), so the abort lost |
| 209 | // the race: afterExecute MUST still run rather than being silently skipped. |
| 210 | expect(afterExecute).toHaveBeenCalledTimes(1); |
| 211 | expect(result).toBeDefined(); |
| 212 | }); |
| 213 | }); |
| 214 | |
| 215 | describe('Cancellation — custom-tool AbortError classification', () => { |
| 216 | it('AbortError from tool internal abort (signal NOT fired) is NOT misclassified as cancellation', async () => { |
| 217 | const { z } = await import('zod'); |
| 218 | const { createCustomTool } = await import('../../src/models/CustomTool'); |
| 219 | const { executeCustomTool } = await import('../../src/models/customToolExecution'); |
| 220 | const customTool = createCustomTool('OWN_ABORT_TOOL', { |
| 221 | name: 'Own abort', |
| 222 | description: 'test', |
| 223 | inputParams: z.object({}), |
| 224 | execute: async () => { |
| 225 | const err = new Error('Tool internal timeout'); |
| 226 | err.name = 'AbortError'; |
| 227 | throw err; |
| 228 | }, |
| 229 | }); |
| 230 | |
| 231 | const controller = new AbortController(); |
| 232 | const result = await executeCustomTool( |
| 233 | { handle: customTool, finalSlug: 'LOCAL_OWN_ABORT_TOOL' } as never, |
| 234 | {}, |
| 235 | createSessionContext(), |
| 236 | { signal: controller.signal } |
| 237 | ); |
| 238 | |
| 239 | expect(result.successful).toBe(false); |
| 240 | expect(result.error).toBe('Tool internal timeout'); |
| 241 | }); |
| 242 | |
| 243 | it('AbortError after caller abort IS classified as cancellation', async () => { |
| 244 | const { z } = await import('zod'); |
| 245 | const { createCustomTool } = await import('../../src/models/CustomTool'); |
| 246 | const { executeCustomTool } = await import('../../src/models/customToolExecution'); |
| 247 | const controller = new AbortController(); |
| 248 | |
| 249 | const customTool = createCustomTool('COOP_CANCEL_TOOL', { |
| 250 | name: 'Coop cancel', |
| 251 | description: 'test', |
| 252 | inputParams: z.object({}), |
| 253 | execute: async () => { |
| 254 | controller.abort(); |
| 255 | const err = new Error('The operation was aborted'); |
| 256 | err.name = 'AbortError'; |
| 257 | throw err; |
| 258 | }, |
| 259 | }); |
| 260 | |
| 261 | await expect( |
| 262 | executeCustomTool( |
| 263 | { handle: customTool, finalSlug: 'LOCAL_COOP_CANCEL_TOOL' } as never, |
| 264 | {}, |
| 265 | createSessionContext(), |
| 266 | { signal: controller.signal } |
| 267 | ) |
| 268 | ).rejects.toBeInstanceOf(ComposioRequestCancelledError); |
| 269 | }); |
| 270 | |
| 271 | it('pre-aborted signal short-circuits before custom-tool user code runs', async () => { |
| 272 | const { z } = await import('zod'); |
| 273 | const { createCustomTool } = await import('../../src/models/CustomTool'); |
| 274 | const { executeCustomTool } = await import('../../src/models/customToolExecution'); |
| 275 | const userExecute = vi.fn(); |
| 276 | const customTool = createCustomTool('CUSTOM_TOOL', { |
| 277 | name: 'Custom tool', |
| 278 | description: 'test', |
| 279 | inputParams: z.object({}), |
| 280 | execute: async () => { |
| 281 | await userExecute(); |
| 282 | return {}; |
| 283 | }, |
| 284 | }); |
| 285 | |
| 286 | const controller = new AbortController(); |
| 287 | controller.abort(); |
| 288 | |
| 289 | await expect( |
| 290 | executeCustomTool( |
| 291 | { handle: customTool, finalSlug: 'LOCAL_CUSTOM_TOOL' } as never, |
| 292 | {}, |
| 293 | createSessionContext(), |
| 294 | { signal: controller.signal } |
| 295 | ) |
| 296 | ).rejects.toBeInstanceOf(ComposioRequestCancelledError); |
| 297 | expect(userExecute).not.toHaveBeenCalled(); |
| 298 | }); |
| 299 | }); |
| 300 | |
| 301 | function createSessionContext(): SessionContext { |
| 302 | return { |
| 303 | userId: 'user_1', |
| 304 | execute: vi.fn(), |
| 305 | proxyExecute: vi.fn(), |
| 306 | }; |
| 307 | } |
| 308 | |
| 309 | describe('Cancellation — SessionContext signal forwarding', () => { |
| 310 | it('ctx.execute aborts are normalized so in-tool try/catch detects ComposioRequestCancelledError', async () => { |
| 311 | const { SessionContextImpl } = await import('../../src/models/SessionContext'); |
| 312 | const { executeCustomTool: execCustomTool } = |
| 313 | await import('../../src/models/customToolExecution'); |
| 314 | |
| 315 | const controller = new AbortController(); |
| 316 | const sessionExecuteSpy = vi.fn().mockImplementation(async () => { |
| 317 | controller.abort(); |
| 318 | throw new APIUserAbortError(); |
| 319 | }); |
| 320 | const fakeClient = { |
| 321 | toolRouter: { session: { execute: sessionExecuteSpy, proxyExecute: vi.fn() } }, |
| 322 | }; |
| 323 | const ctxInstance = new SessionContextImpl(fakeClient as never, 'u', 's'); |
| 324 | |
| 325 | let observedInToolError: unknown = undefined; |
| 326 | const { z } = await import('zod'); |
| 327 | const customToolEntry = { |
| 328 | handle: { |
| 329 | slug: 'INTOOL_CATCH', |
| 330 | name: 'in-tool catch', |
| 331 | inputParams: z.object({}), |
| 332 | execute: async (_input: unknown, ctx: SessionContext) => { |
| 333 | try { |
| 334 | await ctx.execute('SOMETHING', {}); |
| 335 | return { reached: true }; |
| 336 | } catch (err) { |
| 337 | observedInToolError = err; |
| 338 | throw err; |
| 339 | } |
| 340 | }, |
| 341 | }, |
| 342 | }; |
| 343 | |
| 344 | await expect( |
| 345 | execCustomTool(customToolEntry as never, {}, ctxInstance, { |
| 346 | signal: controller.signal, |
| 347 | }) |
| 348 | ).rejects.toBeInstanceOf(ComposioRequestCancelledError); |
| 349 | |
| 350 | expect(observedInToolError).toBeInstanceOf(ComposioRequestCancelledError); |
| 351 | }); |
| 352 | }); |
| 353 | |
| 354 | describe('Cancellation — composite operation forwarding', () => { |
| 355 | beforeEach(() => { |
| 356 | vi.clearAllMocks(); |
| 357 | }); |
| 358 | |
| 359 | it('toolkits.authorize forwards signal through every preflight call', async () => { |
| 360 | const { Toolkits } = await import('../../src/models/Toolkits'); |
| 361 | const toolkits = new Toolkits(mockClient as unknown as ComposioClient); |
| 362 | |
| 363 | const getToolkitBySlugSpy = vi |
| 364 | .spyOn( |
| 365 | toolkits as unknown as { getToolkitBySlug: (...args: unknown[]) => Promise<unknown> }, |
| 366 | 'getToolkitBySlug' |
| 367 | ) |
| 368 | .mockResolvedValue({ |
| 369 | slug: 'github', |
| 370 | name: 'GitHub', |
| 371 | authConfigDetails: [{ mode: 'OAUTH2', fields: {} }], |
| 372 | } as never); |
| 373 | const { AuthConfigs } = await import('../../src/models/AuthConfigs'); |
| 374 | const authConfigsListSpy = vi |
| 375 | .spyOn(AuthConfigs.prototype, 'list') |
| 376 | .mockImplementation(async (_query, reqOpts) => { |
| 377 | await mockClient.authConfigs.list({} as never, reqOpts); |
| 378 | return { items: [{ id: 'ac_existing' } as never], nextCursor: null, totalPages: 1 }; |
| 379 | }); |
| 380 | mockClient.authConfigs.list.mockResolvedValueOnce({ |
| 381 | items: [{ id: 'ac_existing' }], |
| 382 | totalPages: 1, |
| 383 | } as never); |
| 384 | mockClient.connectedAccounts.list.mockResolvedValueOnce({ |
| 385 | items: [], |
| 386 | totalPages: 1, |
| 387 | } as never); |
| 388 | mockClient.connectedAccounts.create = vi.fn().mockResolvedValue({ |
| 389 | id: 'ca_new', |
| 390 | connectionData: { val: { status: 'INITIATED', redirectUrl: null } }, |
| 391 | }); |
| 392 | |
| 393 | const requestOptions = { signal: new AbortController().signal }; |
| 394 | await toolkits.authorize('user_1', 'github', undefined, requestOptions); |
| 395 | |
| 396 | expect(getToolkitBySlugSpy).toHaveBeenCalledWith('github', requestOptions); |
| 397 | expect(authConfigsListSpy).toHaveBeenCalledWith({ toolkit: 'github' }, requestOptions); |
| 398 | expect(mockClient.connectedAccounts.list).toHaveBeenCalledWith( |
| 399 | expect.any(Object), |
| 400 | requestOptions |
| 401 | ); |
| 402 | expect(mockClient.connectedAccounts.create).toHaveBeenCalledWith( |
| 403 | expect.any(Object), |
| 404 | requestOptions |
| 405 | ); |
| 406 | }); |
| 407 | |
| 408 | it('connectedAccounts.initiate forwards signal to list preflight and create', async () => { |
| 409 | const { ConnectedAccounts } = await import('../../src/models/ConnectedAccounts'); |
| 410 | const connectedAccounts = new ConnectedAccounts(mockClient as unknown as ComposioClient); |
| 411 | |
| 412 | mockClient.connectedAccounts.list.mockResolvedValueOnce({ |
| 413 | items: [], |
| 414 | totalPages: 1, |
| 415 | } as never); |
| 416 | mockClient.connectedAccounts.create = vi.fn().mockResolvedValue({ |
| 417 | id: 'ca_new', |
| 418 | connectionData: { val: { status: 'INITIATED', redirectUrl: null } }, |
| 419 | }); |
| 420 | |
| 421 | const requestOptions = { signal: new AbortController().signal }; |
| 422 | await connectedAccounts.initiate('user_1', 'ac_1', { allowMultiple: true }, requestOptions); |
| 423 | |
| 424 | expect(mockClient.connectedAccounts.list).toHaveBeenCalledWith( |
| 425 | expect.any(Object), |
| 426 | requestOptions |
| 427 | ); |
| 428 | expect(mockClient.connectedAccounts.create).toHaveBeenCalledWith( |
| 429 | expect.any(Object), |
| 430 | requestOptions |
| 431 | ); |
| 432 | }); |
| 433 | |
| 434 | it('connectedAccounts.link forwards signal to list preflight and link.create', async () => { |
| 435 | const { ConnectedAccounts } = await import('../../src/models/ConnectedAccounts'); |
| 436 | const linkCreate = vi.fn().mockResolvedValue({ |
| 437 | connected_account_id: 'ca_new', |
| 438 | redirect_url: 'https://example.test/cb', |
| 439 | }); |
| 440 | const extendedClient = { ...mockClient, link: { create: linkCreate } }; |
| 441 | const connectedAccounts = new ConnectedAccounts(extendedClient as unknown as ComposioClient); |
| 442 | |
| 443 | mockClient.connectedAccounts.list.mockResolvedValueOnce({ |
| 444 | items: [], |
| 445 | totalPages: 1, |
| 446 | } as never); |
| 447 | |
| 448 | const requestOptions = { signal: new AbortController().signal }; |
| 449 | await connectedAccounts.link('user_1', 'ac_1', { allowMultiple: true }, requestOptions); |
| 450 | |
| 451 | expect(mockClient.connectedAccounts.list).toHaveBeenCalledWith( |
| 452 | expect.any(Object), |
| 453 | requestOptions |
| 454 | ); |
| 455 | expect(linkCreate).toHaveBeenCalledWith(expect.any(Object), requestOptions); |
| 456 | }); |
| 457 | }); |