The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.
import { HttpBoxliteClient, type CreateBoxRequest } from './boxlite'
const base = 'https://boxes.example.test/api/v1'
const client = new HttpBoxliteClient(base, 'test-platform-key')
let fetcher: jest.SpiedFunction<typeof fetch>
beforeEach(() => {
fetcher = jest.spyOn(globalThis, 'fetch').mockRejectedValue(new Error('unexpected request'))
})
afterEach(() => jest.restoreAllMocks())
it('sends Box creation and lifecycle requests using the encoded identifier and platform credential', async () => {
const request: CreateBoxRequest = {
name: 'orders',
image: 'node:24',
cpus: 1,
memory_mib: 1024,
disk_size_gb: 8,
env: { NODE_ENV: 'production' },
cmd: ['node', 'app.js'],
auto_stop: 300,
auto_resume: true,
network: { inbound: { mode: 'enabled' } },
}
fetcher.mockResolvedValueOnce(Response.json({ box_id: 'box 1', name: 'orders', status: 'starting' }))
expect(await client.createBox(request)).toEqual({ id: 'box 1', name: 'orders', status: 'starting' })
fetcher.mockResolvedValueOnce(Response.json({ id: 'box 1', name: 'orders', status: 'running' }))
expect((await client.getBox('box 1'))?.status).toBe('running')
for (let n = 0; n < 3; n++) fetcher.mockResolvedValueOnce(new Response(null, { status: 204 }))
await client.stopBox('box 1')
await client.startBox('box 1')
await client.deleteBox('box 1')
expect(fetcher.mock.calls.map(([u, opts]) => [String(u).slice(base.length), opts?.method, opts?.body])).toEqual([
['/boxes', 'POST', JSON.stringify(request)],
['/boxes/box%201', 'GET', undefined],
['/boxes/box%201/stop', 'POST', ''],
['/boxes/box%201/start', 'POST', ''],
['/boxes/box%201', 'DELETE', undefined],
])
expect(fetcher.mock.calls[0][1]?.headers).toMatchObject({
authorization: 'Bearer test-platform-key',
'content-type': 'application/json',
})
})
it('treats missing Boxes as already deleted and propagates unrelated lookup and deletion errors', async () => {
fetcher.mockResolvedValueOnce(new Response(null, { status: 404 }))
expect(await client.getBox('absent')).toBeNull()
fetcher.mockResolvedValueOnce(new Response(null, { status: 404 }))
await expect(client.deleteBox('absent')).resolves.toBeUndefined()
fetcher.mockResolvedValueOnce(Response.json({ message: 'access denied' }, { status: 403 }))
await expect(client.getBox('existing')).rejects.toMatchObject({
status: 403,
message: expect.stringContaining('access denied'),
})
fetcher.mockResolvedValueOnce(new Response('unavailable', { status: 503 }))
await expect(client.deleteBox('existing')).rejects.toMatchObject({
status: 503,
message: expect.stringContaining('unavailable'),
})
})
it('accepts both tunnel response formats and reports a successful response with no tunnel', async () => {
for (const response of [{ uri: 'https://app.example.test///' }, { url: 'https://app.example.test' }]) {
fetcher.mockResolvedValueOnce(Response.json(response))
await expect(client.tunnel('box 1', 8080)).resolves.toBe('https://app.example.test')
}
expect(fetcher.mock.calls[0][0]).toBe(base + '/boxes/box%201/network/tunnel?port=8080')
fetcher.mockResolvedValueOnce(Response.json({}))
await expect(client.tunnel('box 1', 8080)).rejects.toMatchObject({
service: 'boxlite',
status: 200,
message: expect.stringContaining('no uri'),
})
})
it('fails locally without a platform key and reports transport failures without URL query values', async () => {
await expect(new HttpBoxliteClient(base, '').getBox('id')).rejects.toMatchObject({ code: 'unavailable' })
expect(fetcher).not.toHaveBeenCalled()
fetcher.mockRejectedValueOnce(new Error('offline'))
await expect(client.tunnel('id', 8080)).rejects.toMatchObject({
service: 'boxlite',
status: 0,
message: 'boxlite: POST https://boxes.example.test/api/v1/boxes/id/network/tunnel failed: offline',
})
})