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 ComposioClient from '@composio/client'; |
| 3 | import { Webhooks, WebhookSubscriptions, WebhookEndpoints } from '../../src/models/Webhooks'; |
| 4 | import { telemetry } from '../../src/telemetry/Telemetry'; |
| 5 | import { ValidationError } from '../../src/errors/ValidationErrors'; |
| 6 | |
| 7 | vi.mock('../../src/telemetry/Telemetry', () => ({ |
| 8 | telemetry: { |
| 9 | instrument: vi.fn(), |
| 10 | }, |
| 11 | })); |
| 12 | |
| 13 | const createMockClient = () => ({ |
| 14 | baseURL: 'https://api.composio.dev', |
| 15 | apiKey: 'test-api-key', |
| 16 | webhookSubscriptions: { |
| 17 | list: vi.fn(), |
| 18 | retrieve: vi.fn(), |
| 19 | create: vi.fn(), |
| 20 | update: vi.fn(), |
| 21 | delete: vi.fn(), |
| 22 | rotateSecret: vi.fn(), |
| 23 | listEventTypes: vi.fn(), |
| 24 | }, |
| 25 | webhookEndpoints: { |
| 26 | list: vi.fn(), |
| 27 | retrieve: vi.fn(), |
| 28 | create: vi.fn(), |
| 29 | replace: vi.fn(), |
| 30 | update: vi.fn(), |
| 31 | }, |
| 32 | }); |
| 33 | |
| 34 | const webhookUrl = 'https://example.com/webhooks/composio'; |
| 35 | const rawSubscription = { |
| 36 | id: 'sub_123', |
| 37 | webhook_url: webhookUrl, |
| 38 | version: 'V3', |
| 39 | enabled_events: ['composio.trigger.message'], |
| 40 | secret: 'whsec_123', |
| 41 | created_at: '2026-01-01T00:00:00Z', |
| 42 | updated_at: '2026-01-01T00:00:00Z', |
| 43 | }; |
| 44 | const transformedSubscription = { |
| 45 | id: 'sub_123', |
| 46 | webhookUrl, |
| 47 | version: 'V3', |
| 48 | enabledEvents: ['composio.trigger.message'], |
| 49 | secret: 'whsec_123', |
| 50 | createdAt: '2026-01-01T00:00:00Z', |
| 51 | updatedAt: '2026-01-01T00:00:00Z', |
| 52 | }; |
| 53 | |
| 54 | const rawEndpoint = { |
| 55 | id: 'we_abc123', |
| 56 | toolkit_slug: 'slack', |
| 57 | client_id: 'client_1', |
| 58 | webhook_url: 'https://backend.composio.dev/webhook/slack/we_abc123', |
| 59 | data: { signing_secret: '***' }, |
| 60 | created_at: '2026-01-01T00:00:00Z', |
| 61 | }; |
| 62 | const transformedEndpoint = { |
| 63 | id: 'we_abc123', |
| 64 | toolkitSlug: 'slack', |
| 65 | clientId: 'client_1', |
| 66 | webhookUrl: 'https://backend.composio.dev/webhook/slack/we_abc123', |
| 67 | data: { signing_secret: '***' }, |
| 68 | createdAt: '2026-01-01T00:00:00Z', |
| 69 | }; |
| 70 | |
| 71 | describe('Webhooks', () => { |
| 72 | let webhooks: Webhooks; |
| 73 | let mockClient: ReturnType<typeof createMockClient>; |
| 74 | |
| 75 | beforeEach(() => { |
| 76 | vi.clearAllMocks(); |
| 77 | mockClient = createMockClient(); |
| 78 | webhooks = new Webhooks(mockClient as unknown as ComposioClient); |
| 79 | }); |
| 80 | |
| 81 | describe('constructor', () => { |
| 82 | it('mounts the subscriptions and endpoints sub-namespaces', () => { |
| 83 | expect(webhooks.subscriptions).toBeInstanceOf(WebhookSubscriptions); |
| 84 | expect(webhooks.endpoints).toBeInstanceOf(WebhookEndpoints); |
| 85 | }); |
| 86 | |
| 87 | it('instruments every class with telemetry', () => { |
| 88 | expect(telemetry.instrument).toHaveBeenCalledWith(webhooks, 'Webhooks'); |
| 89 | expect(telemetry.instrument).toHaveBeenCalledWith( |
| 90 | webhooks.subscriptions, |
| 91 | 'WebhookSubscriptions' |
| 92 | ); |
| 93 | expect(telemetry.instrument).toHaveBeenCalledWith(webhooks.endpoints, 'WebhookEndpoints'); |
| 94 | }); |
| 95 | }); |
| 96 | |
| 97 | describe('subscriptions', () => { |
| 98 | describe('list', () => { |
| 99 | it('lists subscriptions and transforms the response', async () => { |
| 100 | mockClient.webhookSubscriptions.list.mockResolvedValue({ |
| 101 | items: [rawSubscription], |
| 102 | next_cursor: null, |
| 103 | total_pages: 1, |
| 104 | current_page: 1, |
| 105 | total_items: 1, |
| 106 | }); |
| 107 | |
| 108 | const result = await webhooks.subscriptions.list(); |
| 109 | |
| 110 | expect(mockClient.webhookSubscriptions.list).toHaveBeenCalledWith( |
| 111 | { limit: undefined, cursor: undefined }, |
| 112 | undefined |
| 113 | ); |
| 114 | expect(result).toEqual({ |
| 115 | items: [transformedSubscription], |
| 116 | nextCursor: null, |
| 117 | totalPages: 1, |
| 118 | currentPage: 1, |
| 119 | totalItems: 1, |
| 120 | }); |
| 121 | }); |
| 122 | |
| 123 | it('forwards pagination params and request options', async () => { |
| 124 | mockClient.webhookSubscriptions.list.mockResolvedValue({ |
| 125 | items: [], |
| 126 | next_cursor: 'next', |
| 127 | total_pages: 2, |
| 128 | current_page: 1, |
| 129 | total_items: 3, |
| 130 | }); |
| 131 | const signal = new AbortController().signal; |
| 132 | |
| 133 | const result = await webhooks.subscriptions.list({ limit: 2, cursor: 'abc' }, { signal }); |
| 134 | |
| 135 | expect(mockClient.webhookSubscriptions.list).toHaveBeenCalledWith( |
| 136 | { limit: 2, cursor: 'abc' }, |
| 137 | { signal } |
| 138 | ); |
| 139 | expect(result.nextCursor).toBe('next'); |
| 140 | }); |
| 141 | |
| 142 | it('throws a ValidationError for invalid params', async () => { |
| 143 | await expect( |
| 144 | webhooks.subscriptions.list({ limit: 'ten' } as unknown as { limit: number }) |
| 145 | ).rejects.toThrow(ValidationError); |
| 146 | expect(mockClient.webhookSubscriptions.list).not.toHaveBeenCalled(); |
| 147 | }); |
| 148 | }); |
| 149 | |
| 150 | describe('get', () => { |
| 151 | it('retrieves a subscription by id', async () => { |
| 152 | mockClient.webhookSubscriptions.retrieve.mockResolvedValue(rawSubscription); |
| 153 | |
| 154 | const result = await webhooks.subscriptions.get('sub_123'); |
| 155 | |
| 156 | expect(mockClient.webhookSubscriptions.retrieve).toHaveBeenCalledWith('sub_123', undefined); |
| 157 | expect(result).toEqual(transformedSubscription); |
| 158 | }); |
| 159 | }); |
| 160 | |
| 161 | describe('set', () => { |
| 162 | it('creates a subscription when none exists', async () => { |
| 163 | mockClient.webhookSubscriptions.list.mockResolvedValue({ items: [] }); |
| 164 | mockClient.webhookSubscriptions.create.mockResolvedValue(rawSubscription); |
| 165 | |
| 166 | const result = await webhooks.subscriptions.set({ webhookUrl }); |
| 167 | |
| 168 | expect(mockClient.webhookSubscriptions.list).toHaveBeenCalledWith({ limit: 1 }, undefined); |
| 169 | expect(mockClient.webhookSubscriptions.create).toHaveBeenCalledWith( |
| 170 | { |
| 171 | webhook_url: webhookUrl, |
| 172 | enabled_events: ['composio.trigger.message'], |
| 173 | version: 'V3', |
| 174 | }, |
| 175 | undefined |
| 176 | ); |
| 177 | expect(mockClient.webhookSubscriptions.update).not.toHaveBeenCalled(); |
| 178 | expect(result).toEqual(transformedSubscription); |
| 179 | }); |
| 180 | |
| 181 | it('updates the existing subscription when one exists', async () => { |
| 182 | mockClient.webhookSubscriptions.list.mockResolvedValue({ items: [{ id: 'sub_123' }] }); |
| 183 | mockClient.webhookSubscriptions.update.mockResolvedValue(rawSubscription); |
| 184 | |
| 185 | await webhooks.subscriptions.set({ |
| 186 | webhookUrl, |
| 187 | enabledEvents: ['composio.trigger.message', 'composio.connected_account.expired'], |
| 188 | version: 'V2', |
| 189 | }); |
| 190 | |
| 191 | expect(mockClient.webhookSubscriptions.update).toHaveBeenCalledWith( |
| 192 | 'sub_123', |
| 193 | { |
| 194 | webhook_url: webhookUrl, |
| 195 | enabled_events: ['composio.trigger.message', 'composio.connected_account.expired'], |
| 196 | version: 'V2', |
| 197 | }, |
| 198 | undefined |
| 199 | ); |
| 200 | expect(mockClient.webhookSubscriptions.create).not.toHaveBeenCalled(); |
| 201 | }); |
| 202 | |
| 203 | it('forwards request options to both calls', async () => { |
| 204 | mockClient.webhookSubscriptions.list.mockResolvedValue({ items: [] }); |
| 205 | mockClient.webhookSubscriptions.create.mockResolvedValue(rawSubscription); |
| 206 | const signal = new AbortController().signal; |
| 207 | |
| 208 | await webhooks.subscriptions.set({ webhookUrl }, { signal }); |
| 209 | |
| 210 | expect(mockClient.webhookSubscriptions.list).toHaveBeenCalledWith({ limit: 1 }, { signal }); |
| 211 | expect(mockClient.webhookSubscriptions.create).toHaveBeenCalledWith(expect.any(Object), { |
| 212 | signal, |
| 213 | }); |
| 214 | }); |
| 215 | |
| 216 | it('throws a ValidationError for invalid params before any request', async () => { |
| 217 | await expect(webhooks.subscriptions.set({ webhookUrl, enabledEvents: [] })).rejects.toThrow( |
| 218 | ValidationError |
| 219 | ); |
| 220 | expect(mockClient.webhookSubscriptions.list).not.toHaveBeenCalled(); |
| 221 | }); |
| 222 | |
| 223 | it('throws a ValidationError when the list probe has no items', async () => { |
| 224 | mockClient.webhookSubscriptions.list.mockResolvedValue({ total_items: 0 }); |
| 225 | |
| 226 | await expect(webhooks.subscriptions.set({ webhookUrl })).rejects.toThrow(ValidationError); |
| 227 | expect(mockClient.webhookSubscriptions.create).not.toHaveBeenCalled(); |
| 228 | expect(mockClient.webhookSubscriptions.update).not.toHaveBeenCalled(); |
| 229 | }); |
| 230 | |
| 231 | it('throws a ValidationError when the list probe is malformed', async () => { |
| 232 | mockClient.webhookSubscriptions.list.mockResolvedValue({ items: [{ id: 42 }] }); |
| 233 | |
| 234 | await expect(webhooks.subscriptions.set({ webhookUrl })).rejects.toThrow(ValidationError); |
| 235 | expect(mockClient.webhookSubscriptions.create).not.toHaveBeenCalled(); |
| 236 | expect(mockClient.webhookSubscriptions.update).not.toHaveBeenCalled(); |
| 237 | }); |
| 238 | }); |
| 239 | |
| 240 | describe('update', () => { |
| 241 | it('updates only the provided fields', async () => { |
| 242 | mockClient.webhookSubscriptions.update.mockResolvedValue(rawSubscription); |
| 243 | |
| 244 | const result = await webhooks.subscriptions.update('sub_123', { |
| 245 | enabledEvents: ['composio.trigger.message'], |
| 246 | }); |
| 247 | |
| 248 | expect(mockClient.webhookSubscriptions.update).toHaveBeenCalledWith( |
| 249 | 'sub_123', |
| 250 | { |
| 251 | webhook_url: undefined, |
| 252 | enabled_events: ['composio.trigger.message'], |
| 253 | version: undefined, |
| 254 | }, |
| 255 | undefined |
| 256 | ); |
| 257 | expect(result).toEqual(transformedSubscription); |
| 258 | }); |
| 259 | |
| 260 | it('throws a ValidationError for invalid params', async () => { |
| 261 | await expect( |
| 262 | webhooks.subscriptions.update('sub_123', { version: 'V9' as 'V3' }) |
| 263 | ).rejects.toThrow(ValidationError); |
| 264 | expect(mockClient.webhookSubscriptions.update).not.toHaveBeenCalled(); |
| 265 | }); |
| 266 | }); |
| 267 | |
| 268 | describe('delete', () => { |
| 269 | it('deletes a subscription', async () => { |
| 270 | mockClient.webhookSubscriptions.delete.mockResolvedValue({ success: true }); |
| 271 | |
| 272 | const result = await webhooks.subscriptions.delete('sub_123'); |
| 273 | |
| 274 | expect(mockClient.webhookSubscriptions.delete).toHaveBeenCalledWith('sub_123', undefined); |
| 275 | expect(result).toEqual({ success: true }); |
| 276 | }); |
| 277 | }); |
| 278 | |
| 279 | describe('rotateSecret', () => { |
| 280 | it('rotates the signing secret', async () => { |
| 281 | mockClient.webhookSubscriptions.rotateSecret.mockResolvedValue({ |
| 282 | id: 'sub_123', |
| 283 | secret: 'whsec_new', |
| 284 | }); |
| 285 | |
| 286 | const result = await webhooks.subscriptions.rotateSecret('sub_123'); |
| 287 | |
| 288 | expect(mockClient.webhookSubscriptions.rotateSecret).toHaveBeenCalledWith( |
| 289 | 'sub_123', |
| 290 | undefined |
| 291 | ); |
| 292 | expect(result).toEqual({ id: 'sub_123', secret: 'whsec_new' }); |
| 293 | }); |
| 294 | }); |
| 295 | |
| 296 | describe('listEventTypes', () => { |
| 297 | it('lists event types in camelCase', async () => { |
| 298 | mockClient.webhookSubscriptions.listEventTypes.mockResolvedValue({ |
| 299 | items: [ |
| 300 | { |
| 301 | event_type: 'composio.trigger.message', |
| 302 | description: 'Trigger fired', |
| 303 | supported_versions: ['V2', 'V3'], |
| 304 | }, |
| 305 | ], |
| 306 | }); |
| 307 | |
| 308 | const result = await webhooks.subscriptions.listEventTypes(); |
| 309 | |
| 310 | expect(mockClient.webhookSubscriptions.listEventTypes).toHaveBeenCalledWith(undefined); |
| 311 | expect(result).toEqual({ |
| 312 | items: [ |
| 313 | { |
| 314 | eventType: 'composio.trigger.message', |
| 315 | description: 'Trigger fired', |
| 316 | supportedVersions: ['V2', 'V3'], |
| 317 | }, |
| 318 | ], |
| 319 | }); |
| 320 | }); |
| 321 | }); |
| 322 | }); |
| 323 | |
| 324 | describe('endpoints', () => { |
| 325 | describe('list', () => { |
| 326 | it('lists endpoints without a filter', async () => { |
| 327 | mockClient.webhookEndpoints.list.mockResolvedValue({ |
| 328 | items: [{ id: 'we_abc123', toolkit_slug: 'slack', client_id: null }], |
| 329 | }); |
| 330 | |
| 331 | const result = await webhooks.endpoints.list(); |
| 332 | |
| 333 | expect(mockClient.webhookEndpoints.list).toHaveBeenCalledWith( |
| 334 | { toolkit_slug: undefined }, |
| 335 | undefined |
| 336 | ); |
| 337 | expect(result).toEqual({ |
| 338 | items: [{ id: 'we_abc123', toolkitSlug: 'slack', clientId: null }], |
| 339 | }); |
| 340 | }); |
| 341 | |
| 342 | it('maps the toolkitSlug filter to toolkit_slug', async () => { |
| 343 | mockClient.webhookEndpoints.list.mockResolvedValue({ items: [] }); |
| 344 | |
| 345 | await webhooks.endpoints.list({ toolkitSlug: 'slack' }); |
| 346 | |
| 347 | expect(mockClient.webhookEndpoints.list).toHaveBeenCalledWith( |
| 348 | { toolkit_slug: 'slack' }, |
| 349 | undefined |
| 350 | ); |
| 351 | }); |
| 352 | }); |
| 353 | |
| 354 | describe('get', () => { |
| 355 | it('retrieves an endpoint by nano id', async () => { |
| 356 | mockClient.webhookEndpoints.retrieve.mockResolvedValue(rawEndpoint); |
| 357 | |
| 358 | const result = await webhooks.endpoints.get('we_abc123'); |
| 359 | |
| 360 | expect(mockClient.webhookEndpoints.retrieve).toHaveBeenCalledWith('we_abc123', undefined); |
| 361 | expect(result).toEqual(transformedEndpoint); |
| 362 | }); |
| 363 | }); |
| 364 | |
| 365 | describe('create', () => { |
| 366 | it('creates an endpoint for a toolkit and OAuth app', async () => { |
| 367 | mockClient.webhookEndpoints.create.mockResolvedValue(rawEndpoint); |
| 368 | |
| 369 | const result = await webhooks.endpoints.create({ |
| 370 | toolkitSlug: 'slack', |
| 371 | clientId: 'client_1', |
| 372 | }); |
| 373 | |
| 374 | expect(mockClient.webhookEndpoints.create).toHaveBeenCalledWith( |
| 375 | { toolkit_slug: 'slack', client_id: 'client_1' }, |
| 376 | undefined |
| 377 | ); |
| 378 | expect(result).toEqual(transformedEndpoint); |
| 379 | }); |
| 380 | |
| 381 | it('throws a ValidationError when required fields are missing', async () => { |
| 382 | await expect( |
| 383 | webhooks.endpoints.create({ toolkitSlug: 'slack' } as { |
| 384 | toolkitSlug: string; |
| 385 | clientId: string; |
| 386 | }) |
| 387 | ).rejects.toThrow(ValidationError); |
| 388 | expect(mockClient.webhookEndpoints.create).not.toHaveBeenCalled(); |
| 389 | }); |
| 390 | }); |
| 391 | |
| 392 | describe('replace', () => { |
| 393 | it('replaces the setup fields', async () => { |
| 394 | mockClient.webhookEndpoints.replace.mockResolvedValue(rawEndpoint); |
| 395 | |
| 396 | const result = await webhooks.endpoints.replace('we_abc123', { |
| 397 | data: { signing_secret: 'secret' }, |
| 398 | }); |
| 399 | |
| 400 | expect(mockClient.webhookEndpoints.replace).toHaveBeenCalledWith( |
| 401 | 'we_abc123', |
| 402 | { data: { signing_secret: 'secret' } }, |
| 403 | undefined |
| 404 | ); |
| 405 | expect(result).toEqual(transformedEndpoint); |
| 406 | }); |
| 407 | |
| 408 | it('throws a ValidationError for non-string values', async () => { |
| 409 | await expect( |
| 410 | webhooks.endpoints.replace('we_abc123', { |
| 411 | data: { enabled: true } as unknown as Record<string, string>, |
| 412 | }) |
| 413 | ).rejects.toThrow(ValidationError); |
| 414 | expect(mockClient.webhookEndpoints.replace).not.toHaveBeenCalled(); |
| 415 | }); |
| 416 | }); |
| 417 | |
| 418 | describe('update', () => { |
| 419 | it('merges the setup fields', async () => { |
| 420 | mockClient.webhookEndpoints.update.mockResolvedValue(rawEndpoint); |
| 421 | |
| 422 | const result = await webhooks.endpoints.update('we_abc123', { |
| 423 | data: { signing_secret: 'rotated' }, |
| 424 | }); |
| 425 | |
| 426 | expect(mockClient.webhookEndpoints.update).toHaveBeenCalledWith( |
| 427 | 'we_abc123', |
| 428 | { data: { signing_secret: 'rotated' } }, |
| 429 | undefined |
| 430 | ); |
| 431 | expect(result).toEqual(transformedEndpoint); |
| 432 | }); |
| 433 | }); |
| 434 | }); |
| 435 | }); |