Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 555 lines 17.9 KB
Raw
1 import { describe, it, expect, vi, beforeEach, afterEach, beforeAll, afterAll } from 'vitest';
2 import { BatchProcessor } from '../../src/telemetry/BatchProcessor';
3 import { TelemetryTransport } from '../../src/telemetry/Telemetry';
4 import { TelemetryService } from '../../src/services/telemetry/TelemetryService';
5 import type { TelemetryPayload } from '../../src/services/telemetry/TelemetryService.types';
6 import { TelemetryMetadata } from '../../src/types/telemetry.types';
7
8 const flushMicrotasks = async () => {
9 await Promise.resolve();
10 await Promise.resolve();
11 };
12
13 // Helper to create a mock telemetry payload
14 const createPayload = (overrides: Partial<TelemetryPayload> = {}): TelemetryPayload => ({
15 functionName: 'TestClass.testMethod',
16 durationMs: 123,
17 timestamp: Date.now() / 1000,
18 props: { foo: 'bar' },
19 source: {
20 host: 'test-host',
21 service: 'sdk',
22 language: 'typescript',
23 version: '1.0.0',
24 platform: 'node',
25 },
26 metadata: { provider: 'test' },
27 ...overrides,
28 });
29
30 describe('BatchProcessor', () => {
31 let callback: ReturnType<typeof vi.fn>;
32 let processor: BatchProcessor;
33
34 beforeEach(() => {
35 callback = vi.fn().mockResolvedValue(undefined);
36 processor = new BatchProcessor(50, 3, callback); // small batch size and time for test
37 });
38
39 afterEach(() => {
40 vi.useRealTimers();
41 });
42
43 it('should batch items and defer callback when batch size is reached', async () => {
44 const payloads = [createPayload(), createPayload(), createPayload()];
45 payloads.forEach(p => processor.pushItem(p));
46 expect(callback).not.toHaveBeenCalled();
47
48 await flushMicrotasks();
49
50 expect(callback).toHaveBeenCalledOnce();
51 expect(callback).toHaveBeenCalledWith(payloads);
52 });
53
54 it('should call callback after timeout if batch size not reached', async () => {
55 vi.useFakeTimers();
56 const payloads = [createPayload(), createPayload()];
57 payloads.forEach(p => processor.pushItem(p));
58 expect(callback).not.toHaveBeenCalled();
59 vi.advanceTimersByTime(60);
60 await flushMicrotasks();
61
62 expect(callback).toHaveBeenCalledOnce();
63 expect(callback).toHaveBeenCalledWith(payloads);
64 });
65
66 it('should reset timer and batch after processing', () => {
67 const payloads = [createPayload(), createPayload(), createPayload()];
68 payloads.forEach(p => processor.pushItem(p));
69 // After batch, timer should be null and batch empty
70 // @ts-expect-error: private access for test
71 expect(processor.timer).toBeNull();
72 // @ts-expect-error: private access for test
73 expect(processor.batch).toEqual([]);
74 });
75
76 describe('async callback handling', () => {
77 it('should handle async callbacks properly', async () => {
78 let resolveCallback: () => void;
79 const asyncCallback = vi.fn().mockImplementation(() => {
80 return new Promise<void>(resolve => {
81 resolveCallback = resolve;
82 });
83 });
84 const asyncProcessor = new BatchProcessor(50, 2, asyncCallback);
85
86 const payloads = [createPayload(), createPayload()];
87 payloads.forEach(p => asyncProcessor.pushItem(p));
88
89 await flushMicrotasks();
90
91 expect(asyncCallback).toHaveBeenCalledOnce();
92 expect(asyncCallback).toHaveBeenCalledWith(payloads);
93
94 // Resolve the async callback
95 resolveCallback!();
96 await asyncProcessor.flush();
97 });
98
99 it('should track pending batches for async callbacks', async () => {
100 let resolveCallback: () => void;
101 const asyncCallback = vi.fn().mockImplementation(() => {
102 return new Promise<void>(resolve => {
103 resolveCallback = resolve;
104 });
105 });
106 const asyncProcessor = new BatchProcessor(50, 2, asyncCallback);
107
108 asyncProcessor.pushItem(createPayload());
109 asyncProcessor.pushItem(createPayload());
110
111 // @ts-expect-error: private access for test
112 expect(asyncProcessor.pendingBatches.size).toBe(1);
113
114 await flushMicrotasks();
115 resolveCallback!();
116 await asyncProcessor.flush();
117
118 // @ts-expect-error: private access for test
119 expect(asyncProcessor.pendingBatches.size).toBe(0);
120 });
121
122 it('should silently handle errors in async callbacks', async () => {
123 const asyncCallback = vi.fn().mockRejectedValue(new Error('Network error'));
124 const asyncProcessor = new BatchProcessor(50, 2, asyncCallback);
125
126 asyncProcessor.pushItem(createPayload());
127 asyncProcessor.pushItem(createPayload());
128
129 // Should not throw
130 await expect(asyncProcessor.flush()).resolves.toBeUndefined();
131 });
132
133 it('should clear batch before async callback completes', async () => {
134 let resolveCallback: () => void;
135 const asyncCallback = vi.fn().mockImplementation(() => {
136 return new Promise<void>(resolve => {
137 resolveCallback = resolve;
138 });
139 });
140 const asyncProcessor = new BatchProcessor(50, 2, asyncCallback);
141
142 asyncProcessor.pushItem(createPayload());
143 asyncProcessor.pushItem(createPayload());
144
145 // Batch should be cleared immediately, not after callback completes
146 // @ts-expect-error: private access for test
147 expect(asyncProcessor.batch).toEqual([]);
148
149 await flushMicrotasks();
150 resolveCallback!();
151 await asyncProcessor.flush();
152 });
153 });
154
155 describe('flush method', () => {
156 it('should process remaining items when flush is called', async () => {
157 const asyncCallback = vi.fn().mockResolvedValue(undefined);
158 const asyncProcessor = new BatchProcessor(50, 10, asyncCallback); // large batch size
159
160 asyncProcessor.pushItem(createPayload());
161 asyncProcessor.pushItem(createPayload());
162
163 expect(asyncCallback).not.toHaveBeenCalled();
164
165 await asyncProcessor.flush();
166
167 expect(asyncCallback).toHaveBeenCalledOnce();
168 });
169
170 it('should wait for pending async callback to complete', async () => {
171 let callbackCompleted = false;
172 let resolveCallback: () => void;
173 const asyncCallback = vi.fn().mockImplementation(() => {
174 return new Promise<void>(resolve => {
175 resolveCallback = resolve;
176 }).then(() => {
177 callbackCompleted = true;
178 });
179 });
180 const asyncProcessor = new BatchProcessor(50, 2, asyncCallback);
181
182 asyncProcessor.pushItem(createPayload());
183 asyncProcessor.pushItem(createPayload());
184
185 expect(callbackCompleted).toBe(false);
186
187 const flushPromise = asyncProcessor.flush();
188 expect(callbackCompleted).toBe(false);
189
190 await flushMicrotasks();
191 resolveCallback!();
192 await flushPromise;
193
194 expect(callbackCompleted).toBe(true);
195 });
196
197 it('should be safe to call flush multiple times', async () => {
198 const asyncCallback = vi.fn().mockResolvedValue(undefined);
199 const asyncProcessor = new BatchProcessor(50, 10, asyncCallback);
200
201 asyncProcessor.pushItem(createPayload());
202
203 await asyncProcessor.flush();
204 await asyncProcessor.flush();
205 await asyncProcessor.flush();
206
207 expect(asyncCallback).toHaveBeenCalledOnce();
208 });
209
210 it('should handle flush with no pending items', async () => {
211 const asyncCallback = vi.fn().mockResolvedValue(undefined);
212 const asyncProcessor = new BatchProcessor(50, 10, asyncCallback);
213
214 await expect(asyncProcessor.flush()).resolves.toBeUndefined();
215 expect(asyncCallback).not.toHaveBeenCalled();
216 });
217
218 it('should await all concurrent batches when flush is called', async () => {
219 const completionOrder: number[] = [];
220 let resolveFirst: () => void;
221 let resolveSecond: () => void;
222
223 const asyncCallback = vi
224 .fn()
225 .mockImplementationOnce(() => {
226 return new Promise<void>(resolve => {
227 resolveFirst = resolve;
228 }).then(() => {
229 completionOrder.push(1);
230 });
231 })
232 .mockImplementationOnce(() => {
233 return new Promise<void>(resolve => {
234 resolveSecond = resolve;
235 }).then(() => {
236 completionOrder.push(2);
237 });
238 });
239
240 const asyncProcessor = new BatchProcessor(50, 2, asyncCallback);
241
242 // Trigger first batch
243 asyncProcessor.pushItem(createPayload());
244 asyncProcessor.pushItem(createPayload());
245
246 // Trigger second batch while first is still processing
247 asyncProcessor.pushItem(createPayload());
248 asyncProcessor.pushItem(createPayload());
249
250 await flushMicrotasks();
251
252 expect(asyncCallback).toHaveBeenCalledTimes(2);
253
254 // @ts-expect-error: private access for test
255 expect(asyncProcessor.pendingBatches.size).toBe(2);
256
257 // Start flush, which should wait for both batches
258 const flushPromise = asyncProcessor.flush();
259
260 // Resolve second batch first (out of order)
261 resolveSecond!();
262 // Small delay to let promises settle
263 await new Promise(r => setTimeout(r, 10));
264
265 // @ts-expect-error: private access for test
266 expect(asyncProcessor.pendingBatches.size).toBe(1);
267
268 // Resolve first batch
269 resolveFirst!();
270
271 // Now flush should complete
272 await flushPromise;
273
274 // Both batches should have completed
275 expect(completionOrder).toEqual([2, 1]);
276 // @ts-expect-error: private access for test
277 expect(asyncProcessor.pendingBatches.size).toBe(0);
278 });
279
280 it('should not lose earlier batches when new batches are started', async () => {
281 const batchesProcessed: number[][] = [];
282 let resolvers: (() => void)[] = [];
283
284 const asyncCallback = vi.fn().mockImplementation((batch: unknown[]) => {
285 return new Promise<void>(resolve => {
286 resolvers.push(resolve);
287 }).then(() => {
288 batchesProcessed.push(batch.map((_, i) => i));
289 });
290 });
291
292 const asyncProcessor = new BatchProcessor(50, 1, asyncCallback); // batch size 1 for easier testing
293
294 // Push 3 items, each triggers a batch immediately
295 asyncProcessor.pushItem(createPayload({ functionName: 'batch1' }));
296 asyncProcessor.pushItem(createPayload({ functionName: 'batch2' }));
297 asyncProcessor.pushItem(createPayload({ functionName: 'batch3' }));
298
299 await flushMicrotasks();
300
301 expect(asyncCallback).toHaveBeenCalledTimes(3);
302
303 // @ts-expect-error: private access for test
304 expect(asyncProcessor.pendingBatches.size).toBe(3);
305
306 // Resolve all in order
307 resolvers.forEach(r => r());
308 await asyncProcessor.flush();
309
310 // All 3 batches should have been processed
311 expect(batchesProcessed.length).toBe(3);
312 // @ts-expect-error: private access for test
313 expect(asyncProcessor.pendingBatches.size).toBe(0);
314 });
315 });
316 });
317
318 describe('TelemetryTransport', () => {
319 let transport: TelemetryTransport;
320 let sendMetricSpy: unknown;
321 let sendErrorLogSpy: unknown;
322 const metadata: TelemetryMetadata = {
323 apiKey: 'test-key',
324 baseUrl: 'http://localhost',
325 version: '1.0.0',
326 isAgentic: false,
327 host: 'test-host',
328 provider: 'test',
329 };
330
331 beforeEach(async () => {
332 sendMetricSpy = vi.spyOn(TelemetryService, 'sendMetric').mockResolvedValue({} as unknown);
333 sendErrorLogSpy = vi.spyOn(TelemetryService, 'sendErrorLog').mockResolvedValue({} as unknown);
334 transport = new TelemetryTransport();
335 transport.setup(metadata);
336 await transport.flush();
337 });
338
339 afterEach(() => {
340 vi.restoreAllMocks();
341 vi.unstubAllEnvs();
342 });
343
344 it('should send SDK_INITIALIZED metric on setup', () => {
345 expect(sendMetricSpy).toHaveBeenCalled();
346 const call = sendMetricSpy.mock.calls[0][0][0];
347 expect(call.functionName).toBeDefined();
348 expect(call.metadata?.provider).toBe('test');
349 });
350
351 it('should batch metrics and send via TelemetryService', async () => {
352 const payload = createPayload();
353 await transport.sendMetric([payload]);
354 expect(sendMetricSpy).toHaveBeenCalledWith([payload]);
355 });
356
357 it('should send error telemetry immediately', async () => {
358 const payload = createPayload({ error: { name: 'Error', message: 'fail', stack: 'stack' } });
359 await transport.sendErrorTelemetry(payload);
360 expect(sendErrorLogSpy).toHaveBeenCalledWith(payload);
361 });
362
363 it('should instrument async methods and batch telemetry', async () => {
364 class TestClass {
365 async foo(a: number) {
366 return a * 2;
367 }
368 }
369 const instance = new TestClass();
370 const instrumented = transport.instrument(instance);
371 await instrumented.foo(21);
372 // The batchProcessor will push the payload, but we can check sendMetric is eventually called
373 await transport.sendMetric([]); // flush
374 expect(sendMetricSpy).toHaveBeenCalled();
375 });
376
377 it('should instrument async methods defined on plain objects', async () => {
378 vi.stubEnv('NODE_ENV', 'development');
379 const instance = { double: async (a: number) => a * 2 };
380
381 transport.instrument(instance, 'PlainObject');
382
383 await expect(instance.double(21)).resolves.toBe(42);
384 await transport.flush();
385 const functionNames = sendMetricSpy.mock.calls.flatMap(([payloads]) =>
386 payloads.map((payload: TelemetryPayload) => payload.functionName)
387 );
388 expect(functionNames).toContain('PlainObject.double');
389 });
390
391 it('should send error telemetry if instrumented method throws', async () => {
392 class TestClass {
393 async fail() {
394 throw new Error('fail!');
395 }
396 }
397 const instance = new TestClass();
398 const instrumented = transport.instrument(instance);
399 await expect(instrumented.fail()).rejects.toThrow('fail!');
400 await transport.flush();
401 expect(sendErrorLogSpy).toHaveBeenCalled();
402 });
403
404 it('should not wait for deferred error telemetry before rejecting instrumented errors', async () => {
405 let resolveTelemetry: (() => void) | undefined;
406 sendErrorLogSpy.mockImplementationOnce(
407 () =>
408 new Promise(resolve => {
409 resolveTelemetry = () => resolve({} as unknown);
410 })
411 );
412
413 class TestClass {
414 async fail() {
415 throw new Error('fail!');
416 }
417 }
418 const instance = new TestClass();
419 const instrumented = transport.instrument(instance);
420
421 await expect(
422 Promise.race([
423 instrumented.fail().then(
424 () => 'resolved',
425 () => 'rejected'
426 ),
427 new Promise(resolve => setTimeout(() => resolve('timeout'), 20)),
428 ])
429 ).resolves.toBe('rejected');
430
431 await flushMicrotasks();
432 expect(sendErrorLogSpy).toHaveBeenCalled();
433
434 resolveTelemetry?.();
435 await transport.flush();
436 });
437
438 it('should flush pending telemetry when flush is called', async () => {
439 class TestClass {
440 async foo(a: number) {
441 return a * 2;
442 }
443 }
444 const instance = new TestClass();
445 const instrumented = transport.instrument(instance);
446
447 // Call the method to generate telemetry
448 await instrumented.foo(21);
449
450 // Flush should complete without error
451 await expect(transport.flush()).resolves.toBeUndefined();
452 });
453 });
454
455 describe('TelemetryService network bounding', () => {
456 afterEach(() => {
457 vi.useRealTimers();
458 vi.restoreAllMocks();
459 vi.unstubAllGlobals();
460 });
461
462 it('bounds sendMetric with an abort signal and clears the timer once it responds', async () => {
463 vi.useFakeTimers();
464 const fetchMock = vi.fn<typeof fetch>(async () => Response.json({ ok: true }));
465 vi.stubGlobal('fetch', fetchMock);
466
467 await TelemetryService.sendMetric([createPayload()]);
468
469 expect(fetchMock).toHaveBeenCalledWith(
470 'https://telemetry.composio.dev/v1/metrics/invocations',
471 expect.objectContaining({ method: 'POST', signal: expect.any(AbortSignal) })
472 );
473 // A timer left pending after the fetch resolved keeps a workerd request context
474 // open for the full timeout on every successful send.
475 expect(vi.getTimerCount()).toBe(0);
476 });
477
478 it('clears the timer when serializing the payload fails', async () => {
479 vi.useFakeTimers();
480 const fetchMock = vi.fn<typeof fetch>();
481 vi.stubGlobal('fetch', fetchMock);
482
483 const payload = createPayload({ props: { value: BigInt(1) } });
484
485 await expect(TelemetryService.sendMetric([payload])).resolves.toBeUndefined();
486 expect(fetchMock).not.toHaveBeenCalled();
487 expect(vi.getTimerCount()).toBe(0);
488 });
489
490 it('swallows a sendMetric timeout when the endpoint never responds', async () => {
491 vi.useFakeTimers();
492 const fetchMock = vi.fn<typeof fetch>(
493 (_input, init) =>
494 new Promise<Response>((_resolve, reject) => {
495 init?.signal?.addEventListener('abort', () => reject(init.signal?.reason), {
496 once: true,
497 });
498 })
499 );
500 vi.stubGlobal('fetch', fetchMock);
501
502 const send = TelemetryService.sendMetric([createPayload()]);
503 await vi.advanceTimersByTimeAsync(3_000);
504
505 await expect(send).resolves.toBeUndefined();
506 });
507
508 it('bounds sendErrorLog with an abort signal and clears the timer once it responds', async () => {
509 vi.useFakeTimers();
510 const fetchMock = vi.fn<typeof fetch>(async () => Response.json({ ok: true }));
511 vi.stubGlobal('fetch', fetchMock);
512
513 await TelemetryService.sendErrorLog(createPayload());
514
515 expect(fetchMock).toHaveBeenCalledWith(
516 'https://telemetry.composio.dev/v1/errors',
517 expect.objectContaining({ method: 'POST', signal: expect.any(AbortSignal) })
518 );
519 expect(vi.getTimerCount()).toBe(0);
520 });
521
522 it('swallows a sendErrorLog timeout when the endpoint never responds', async () => {
523 vi.useFakeTimers();
524 const fetchMock = vi.fn<typeof fetch>(
525 (_input, init) =>
526 new Promise<Response>((_resolve, reject) => {
527 init?.signal?.addEventListener('abort', () => reject(init.signal?.reason), {
528 once: true,
529 });
530 })
531 );
532 vi.stubGlobal('fetch', fetchMock);
533
534 const send = TelemetryService.sendErrorLog(createPayload());
535 await vi.advanceTimersByTimeAsync(3_000);
536
537 await expect(send).resolves.toBeUndefined();
538 });
539 });
540
541 beforeAll(() => {
542 // Save the original NODE_ENV
543 process.env._ORIGINAL_NODE_ENV = process.env.NODE_ENV;
544 process.env.NODE_ENV = 'development';
545 });
546
547 afterAll(() => {
548 // Restore the original NODE_ENV
549 if (process.env._ORIGINAL_NODE_ENV !== undefined) {
550 process.env.NODE_ENV = process.env._ORIGINAL_NODE_ENV;
551 delete process.env._ORIGINAL_NODE_ENV;
552 } else {
553 delete process.env.NODE_ENV;
554 }
555 });