The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.
import type { Ctx } from '../ctx'
import type { EnvRow, LogLine } from '../db'
import { UpstreamError } from '../errors'
import { Resources } from '../resources'
describe('Box sleep, wake and observation', () => {
afterEach(() => {
jest.restoreAllMocks()
jest.useRealTimers()
})
function runtime() {
const env = {
id: 'main',
active_box_id: 'box',
box_url: 'https://box.example.test',
control_secret: 'control',
services: JSON.stringify([{ name: 'web', state: 'starting' }]),
} as EnvRow
let status = 'running'
const getBox = jest.fn(async (): Promise<{ id: string; status: string } | null> => ({ id: 'box', status }))
const startBox = jest.fn(async () => {
status = 'running'
})
const stopBox = jest.fn(async () => {
status = 'stopped'
})
const updateEnv = jest.fn(async (_id, fields: Partial<EnvRow>) => {
Object.assign(env, fields)
})
const appendLogs = jest.fn(async (_id: string, lines: LogLine[]) => lines)
const services = [{ name: 'web', state: 'running' }]
const health = jest.fn(async () => ({ services, box: { memory_used_bytes: 100 } }))
const ctx = {
boxlite: { getBox, startBox, stopBox },
supervisor: { health },
store: { get: async () => env, appendLogs, updateEnv },
bus: { publish: jest.fn() },
} as unknown as Ctx
return { env, resources: new Resources(ctx), getBox, startBox, stopBox, health, appendLogs, services }
}
test('reports cached services while asleep and coalesces concurrent wakes before refreshing health', async () => {
jest.useFakeTimers({ now: 0 })
const h = runtime()
expect(await h.resources.liveServices(h.env)).toMatchObject({
box: 'running',
fresh: true,
services: h.services,
usage: { memory_used_bytes: 100 },
})
expect(await h.resources.boxStatus('box')).toBe('running')
expect(h.getBox).toHaveBeenCalledTimes(1)
await h.resources.sleep('box')
expect(await h.resources.liveServices(h.env)).toEqual({
box: 'stopped',
services: h.services,
fresh: false,
usage: null,
})
expect(h.health).toHaveBeenCalledTimes(1)
await Promise.all([h.resources.wake('box'), h.resources.wake('box')])
expect(h.startBox).toHaveBeenCalledTimes(1)
expect(h.resources.recentlyWoken('box')).toBe(true)
expect(await h.resources.boxStatus('box')).toBe('running')
expect(h.getBox).toHaveBeenCalledTimes(2)
jest.advanceTimersByTime(180_000)
expect(h.resources.recentlyWoken('box')).toBe(false)
h.health.mockRejectedValueOnce(new Error('not ready'))
expect(await h.resources.liveServices(h.env)).toEqual({
box: 'running',
services: h.services,
fresh: false,
usage: null,
})
})
test('retains the failed wake and its system log until another request can retry', async () => {
jest.useFakeTimers({ now: 0 })
jest.spyOn(console, 'error').mockImplementation(() => undefined)
const h = runtime()
h.startBox.mockRejectedValueOnce(new Error('at capacity'))
await h.resources.wake('box')
expect(h.resources.wakeFailure('box')).toBe('at capacity')
expect(h.appendLogs).toHaveBeenCalledWith(h.env.id, [
expect.objectContaining({ source: 'system', level: 'error', line: expect.stringContaining('at capacity') }),
])
jest.advanceTimersByTime(29_999)
await h.resources.wake('box')
expect(h.startBox).toHaveBeenCalledTimes(1)
jest.advanceTimersByTime(1)
expect(h.resources.wakeFailure('box')).toBeNull()
await h.resources.wake('box')
expect(h.startBox).toHaveBeenCalledTimes(2)
expect(h.appendLogs).toHaveBeenCalledTimes(1)
})
test('serves the last known services when the platform loses the Box or health times out', async () => {
jest.useFakeTimers({ now: 0 })
const h = runtime()
const cached = [{ name: 'web', state: 'starting' }]
h.getBox.mockResolvedValueOnce(null)
expect(await h.resources.liveServices(h.env)).toEqual({
box: 'missing',
services: cached,
fresh: false,
usage: null,
})
jest.advanceTimersByTime(5_000)
h.getBox.mockRejectedValueOnce(new Error('runner unavailable'))
expect(await h.resources.liveServices(h.env)).toMatchObject({ box: 'unknown', services: cached, fresh: false })
expect(h.health).not.toHaveBeenCalled()
h.health.mockImplementationOnce(() => new Promise(() => undefined))
const lookup = h.resources.liveServices(h.env, 15)
await jest.advanceTimersByTimeAsync(15)
expect(await lookup).toEqual({ box: 'running', services: cached, fresh: false, usage: null })
expect(h.startBox).not.toHaveBeenCalled()
})
test('an already stopped Box can sleep again and other stop failures reach the caller', async () => {
const h = runtime()
h.stopBox.mockRejectedValueOnce(new UpstreamError('box', 400, 'Box is not started'))
await h.resources.sleep('box')
expect(await h.resources.boxStatus('box')).toBe('stopped')
h.stopBox.mockRejectedValueOnce(new UpstreamError('box', 503, 'unavailable'))
await expect(h.resources.sleep('box')).rejects.toMatchObject({ status: 503 })
})
})