Skip to content

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

2.6 KB
Raw Blame
import type { Ctx } from '../ctx'
import { UpstreamError } from '../errors'
import { SupervisorLink } from './supervisor-link'

describe('supervisor deployment responses', () => {
  afterEach(() => jest.useRealTimers())
  const busy = new UpstreamError('supervisor', 409, 'busy', '{"error":"deploy_in_progress"}')
  const link = (get = jest.fn()) =>
    new SupervisorLink({ timing: { activateMs: 20, pollMs: 10 }, store: { get } } as unknown as Ctx)

  test('retries a busy supervisor within the activation budget', async () => {
    jest.useFakeTimers()
    const request = jest.fn().mockRejectedValueOnce(busy).mockResolvedValue(undefined)
    const success = link().call('start', 3, request)
    await jest.advanceTimersByTimeAsync(10)
    await success
    expect(request).toHaveBeenCalledTimes(2)
    const stillBusy = jest.fn().mockRejectedValue(busy)
    const failure = expect(link().call('start', 3, stillBusy)).rejects.toMatchObject({
      step: 'start',
      message: expect.stringContaining('stayed busy'),
    })
    await jest.advanceTimersByTimeAsync(20)
    await failure
    expect(stillBusy).toHaveBeenCalledTimes(3)
  })

  test.each([
    ['not_built', 'is not built'],
    ['not_found_release', 'is no longer on the Box'],
  ])('reports %s against the requested release', async (code, reason) => {
    const upstream = new UpstreamError('supervisor', 404, code, JSON.stringify({ error: code }))
    await expect(
      link().call('start', 7, async () => {
        throw upstream
      }),
    ).rejects.toMatchObject({
      step: 'start',
      message: expect.stringContaining('release #7 ' + reason),
      cause: upstream,
    })
  })

  test('retains unknown upstream errors and stops waiting for a release which never reported live', async () => {
    const upstream = new UpstreamError('supervisor', 502, 'connection closed', 'bad gateway')
    await expect(
      link().call('build', 3, async () => {
        throw upstream
      }),
    ).rejects.toBe(upstream)
    jest.useFakeTimers()
    const get = jest.fn().mockResolvedValue({ status: { phase: 'starting' } })
    const waiting = expect(link(get).waitLive('main', 3)).rejects.toMatchObject({
      step: 'start',
      message: expect.stringContaining('did not report live/failed for release 3'),
    })
    await jest.advanceTimersByTimeAsync(20)
    await waiting
    get.mockResolvedValue({ status: { phase: 'failed', log_tail: ['no healthy service'] } })
    await expect(link(get).waitLive('main', 3)).rejects.toMatchObject({
      step: 'start',
      logTail: ['no healthy service'],
      message: 'services did not become healthy',
    })
  })
})