From 4e2f31607aebd7ea0739a6db22619a12697481e7 Mon Sep 17 00:00:00 2001 From: gonzaloriestra <14979109+gonzaloriestra@users.noreply.github.com> Date: Sun, 30 Aug 2026 00:25:19 +0000 Subject: [PATCH] [Tests] Add unit tests for OpenTelemetry throttle utility --- .../vendor/otel-js/utils/throttle.test.ts | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 packages/cli-kit/src/public/node/vendor/otel-js/utils/throttle.test.ts diff --git a/packages/cli-kit/src/public/node/vendor/otel-js/utils/throttle.test.ts b/packages/cli-kit/src/public/node/vendor/otel-js/utils/throttle.test.ts new file mode 100644 index 00000000000..a77cf63c673 --- /dev/null +++ b/packages/cli-kit/src/public/node/vendor/otel-js/utils/throttle.test.ts @@ -0,0 +1,100 @@ +import {throttle} from './throttle.js' +import {describe, expect, test, vi} from 'vitest' + +describe('throttle', () => { + test('executes immediately on leading edge by default', () => { + vi.useFakeTimers() + try { + const func = vi.fn((...args: unknown[]) => (args[0] as number) * 2) + const throttled = throttle(func, 100) + + const result = throttled(5) + expect(func).toHaveBeenCalledTimes(1) + expect(func).toHaveBeenCalledWith(5) + expect(result).toBe(10) + } finally { + vi.useRealTimers() + } + }) + + test('throttles calls within wait period and executes latest call on trailing edge', () => { + vi.useFakeTimers() + try { + const func = vi.fn((...args: unknown[]) => (args[0] as number) * 2) + const throttled = throttle(func, 100) + + throttled(1) + expect(func).toHaveBeenCalledTimes(1) + expect(func).toHaveBeenCalledWith(1) + + throttled(2) + throttled(3) + expect(func).toHaveBeenCalledTimes(1) + + vi.advanceTimersByTime(100) + expect(func).toHaveBeenCalledTimes(2) + expect(func).toHaveBeenCalledWith(3) + } finally { + vi.useRealTimers() + } + }) + + test('respects leading: false option by suppressing immediate execution', () => { + vi.useFakeTimers() + try { + const func = vi.fn((...args: unknown[]) => (args[0] as number) * 2) + const throttled = throttle(func, 100, {leading: false}) + + throttled(1) + expect(func).not.toHaveBeenCalled() + + vi.advanceTimersByTime(100) + expect(func).toHaveBeenCalledTimes(1) + expect(func).toHaveBeenCalledWith(1) + } finally { + vi.useRealTimers() + } + }) + + test('respects trailing: false option by disabling trailing edge execution', () => { + vi.useFakeTimers() + try { + const func = vi.fn((...args: unknown[]) => (args[0] as number) * 2) + const throttled = throttle(func, 100, {trailing: false}) + + throttled(1) + expect(func).toHaveBeenCalledTimes(1) + expect(func).toHaveBeenCalledWith(1) + + throttled(2) + vi.advanceTimersByTime(100) + expect(func).toHaveBeenCalledTimes(1) + } finally { + vi.useRealTimers() + } + }) + + test('swallows rejected promise errors on trailing edge execution to prevent unhandled rejections', async () => { + vi.useFakeTimers() + try { + const func = vi.fn((...args: unknown[]) => Promise.reject(new Error(`Failed ${args[0] as number}`))) + const throttled = throttle(func, 100) + + // Leading call - catch its rejection + const p1 = throttled(1) + await expect(p1).rejects.toThrow('Failed 1') + + // Trailing call + throttled(2) + + // Advance timers to trigger trailing execution + vi.advanceTimersByTime(100) + + // Expect function called for trailing execution without unhandled promise rejection + expect(func).toHaveBeenCalledTimes(2) + expect(func).toHaveBeenCalledWith(2) + } finally { + vi.useRealTimers() + } + }) +})