Skip to content

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

2.0 KB
Raw Blame
import type { Ctx } from '../ctx'
import type { EnvRow } from '../db'
import { BoxProvisioner } from './boxes'
import type { BoxWaker } from './wake'

test('a lost worker lease stops supervisor polling after waking the Box', async () => {
  const lease = new AbortController()
  const env = { id: 'env1', active_box_id: 'box1', box_url: 'https://box.example.test' } as EnvRow
  const health = jest.fn()
  const ctx = {
    store: { envById: async () => env },
    boxlite: {
      getBox: async () => ({ id: 'box1', status: 'stopped' }),
      startBox: async () => lease.abort(new Error('worker lease lost')),
    },
    supervisor: { health },
    timing: { supervisorReadyMs: 100, pollMs: 1 },
  } as unknown as Ctx
  const waker = { noteWake: () => undefined } as unknown as BoxWaker
  await expect(new BoxProvisioner(ctx, waker).ensureRunning(env.id, () => undefined, lease.signal)).rejects.toThrow(
    'worker lease lost',
  )
  expect(health).not.toHaveBeenCalled()
})

test('in-place restarts report missing Boxes and incompatible supervisor versions directly', async () => {
  const env = { id: 'env1', active_box_id: null, box_url: null } as EnvRow
  const getBox = jest.fn()
  const health = jest.fn().mockResolvedValue({ version: 1 })
  const ctx = {
    store: { envById: async () => env },
    boxlite: { getBox },
    supervisor: { health },
    timing: { supervisorReadyMs: 1000 },
  } as unknown as Ctx
  const provisioner = new BoxProvisioner(ctx, { noteWake: jest.fn() } as unknown as BoxWaker)
  await expect(provisioner.ensureRunning(env.id, jest.fn())).rejects.toThrow('has no Box yet')
  Object.assign(env, { active_box_id: 'box', box_url: 'https://box.example.test' })
  getBox.mockResolvedValueOnce(null).mockResolvedValueOnce({ id: 'box', status: 'running' })
  await expect(provisioner.ensureRunning(env.id, jest.fn())).rejects.toThrow('no longer exists')
  await expect(provisioner.ensureRunning(env.id, jest.fn())).rejects.toThrow('runs supervisor protocol 1')
  expect(health).toHaveBeenCalledTimes(1)
})