Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

1.5 KB
Raw Blame
import { requestJson, retry } from './http'

it.each([
  ['fixed', [0, 100, 200, 300, 400]],
  ['linear', [0, 100, 300, 600, 950]],
  ['exponential', [0, 100, 300, 650, 1000]],
] as const)('retries with bounded %s delays and observes the attempt count', async (backoff, times) => {
  jest.useFakeTimers({ now: 0 })
  try {
    const attempts: number[] = []
    const failure = new Error('provider unavailable')
    const onRetry = jest.fn()
    const result = retry(
      async () => {
        attempts.push(Date.now())
        if (attempts.length < 5) throw failure
        return 'ready'
      },
      { attempts: 5, delayMs: 100, maxDelayMs: 350, backoff, retryable: (e) => e === failure, onRetry },
    )
    await jest.runAllTimersAsync()
    expect(await result).toBe('ready')
    expect(attempts).toEqual(times)
    expect(onRetry.mock.calls).toEqual([1, 2, 3, 4].map((attempt) => [failure, attempt]))
  } finally {
    jest.useRealTimers()
  }
})

it('keeps a caller cancellation signal and returns text from a non-JSON success response', async () => {
  const fetcher = jest.spyOn(globalThis, 'fetch').mockResolvedValue(new Response('ready'))
  const abort = new AbortController()
  try {
    await expect(
      requestJson('worker', { method: 'GET', url: 'https://worker.example.test/status', signal: abort.signal }),
    ).resolves.toEqual({ body: 'ready', status: 200 })
    expect(fetcher.mock.calls[0][1]?.signal).toBe(abort.signal)
  } finally {
    fetcher.mockRestore()
  }
})