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, afterEach } from 'vitest'; |
| 2 | import { checkForLatestVersionFromNPM } from '../../src/utils/version'; |
| 3 | |
| 4 | describe('checkForLatestVersionFromNPM', () => { |
| 5 | afterEach(() => { |
| 6 | vi.useRealTimers(); |
| 7 | vi.restoreAllMocks(); |
| 8 | vi.unstubAllGlobals(); |
| 9 | }); |
| 10 | |
| 11 | it('bounds the registry request and clears the timer once it responds', async () => { |
| 12 | vi.useFakeTimers(); |
| 13 | const fetchMock = vi.fn<typeof fetch>(async () => Response.json({ version: '0.0.1' })); |
| 14 | vi.stubGlobal('fetch', fetchMock); |
| 15 | |
| 16 | await checkForLatestVersionFromNPM('1.0.0'); |
| 17 | |
| 18 | expect(fetchMock).toHaveBeenCalledWith('https://registry.npmjs.org/@composio/core/latest', { |
| 19 | signal: expect.any(AbortSignal), |
| 20 | }); |
| 21 | // A timer left pending after the fetch resolved keeps a workerd request context |
| 22 | // open for the full timeout on every successful check. |
| 23 | expect(vi.getTimerCount()).toBe(0); |
| 24 | }); |
| 25 | |
| 26 | it('swallows a timeout from a registry request that never responds', async () => { |
| 27 | vi.useFakeTimers(); |
| 28 | const fetchMock = vi.fn<typeof fetch>( |
| 29 | (_input, init) => |
| 30 | new Promise<Response>((_resolve, reject) => { |
| 31 | init?.signal?.addEventListener('abort', () => reject(init.signal?.reason), { |
| 32 | once: true, |
| 33 | }); |
| 34 | }) |
| 35 | ); |
| 36 | vi.stubGlobal('fetch', fetchMock); |
| 37 | |
| 38 | const versionCheck = checkForLatestVersionFromNPM('1.0.0'); |
| 39 | await vi.advanceTimersByTimeAsync(2_000); |
| 40 | |
| 41 | await expect(versionCheck).resolves.toBeUndefined(); |
| 42 | }); |
| 43 | }); |