Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
import { afterAll, afterEach, beforeAll, expect, test } from 'vitest'
import { http, HttpResponse } from 'msw'
import { InvalidArgumentError, Sandbox } from '../../src'
import { TEST_API_KEY, apiUrl } from '../setup'
import { setupMockApi } from '../mockApi'
let lastCreateBody: Record<string, unknown> | undefined
let lastForkBody: Record<string, unknown> | undefined
let lastPauseBody: Record<string, unknown> | undefined
let lastSnapshotBody: Record<string, unknown> | undefined
let lastConnectBody: Record<string, unknown> | undefined
const server = setupMockApi(
http.post(apiUrl('/v2/sandboxes'), async ({ request }) => {
lastCreateBody = (await request.json()) as Record<string, unknown>
return HttpResponse.json({
sandboxID: 'test-sandbox-id',
templateID: 'base',
envdVersion: '0.2.4',
})
}),
http.post(apiUrl('/v2/sandboxes/:sandboxID/connect'), async ({ request }) => {
lastConnectBody = (await request.json()) as Record<string, unknown>
return HttpResponse.json({
sandboxID: 'test-sandbox-id',
templateID: 'base',
envdVersion: '0.2.4',
})
}),
http.post(apiUrl('/sandboxes/:sandboxID/fork'), async ({ request }) => {
lastForkBody = (await request.json()) as Record<string, unknown>
return HttpResponse.json([
{
sandbox: {
sandboxID: 'forked-sandbox-id',
templateID: 'base',
envdVersion: '0.2.4',
},
},
])
}),
http.post(apiUrl('/sandboxes/:sandboxID/pause'), async ({ request }) => {
lastPauseBody = (await request.json()) as Record<string, unknown>
return new HttpResponse(null, { status: 204 })
}),
http.post(apiUrl('/sandboxes/:sandboxID/snapshots'), async ({ request }) => {
lastSnapshotBody = (await request.json()) as Record<string, unknown>
return HttpResponse.json(
{ snapshotID: 'test-snapshot-id', names: [] },
{ status: 201 }
)
})
)
beforeAll(() => server.listen({ onUnhandledRequest: 'error' }))
afterAll(() => server.close())
afterEach(() => {
lastCreateBody = undefined
lastForkBody = undefined
lastPauseBody = undefined
lastSnapshotBody = undefined
lastConnectBody = undefined
server.resetHandlers()
})
test('Sandbox.create omits timeout and allow_internet_access when unset', async () => {
await Sandbox.create('base', { apiKey: TEST_API_KEY })
expect(lastCreateBody).toBeDefined()
expect(lastCreateBody).not.toHaveProperty('timeout')
expect(lastCreateBody).not.toHaveProperty('secure')
expect(lastCreateBody).not.toHaveProperty('allow_internet_access')
})
test('Sandbox.create ignores the deprecated secure option', async () => {
await Sandbox.create('base', { apiKey: TEST_API_KEY, secure: true })
expect(lastCreateBody).toBeDefined()
expect(lastCreateBody).not.toHaveProperty('secure')
})
test('Sandbox.create sends explicit timeout and allow_internet_access', async () => {
await Sandbox.create('base', {
apiKey: TEST_API_KEY,
timeoutMs: 60_000,
allowInternetAccess: false,
})
expect(lastCreateBody?.timeout).toBe(60)
expect(lastCreateBody?.allow_internet_access).toBe(false)
})
test('Sandbox.fork omits timeout and count when unset', async () => {
await Sandbox.fork('test-sandbox-id', { apiKey: TEST_API_KEY })
expect(lastForkBody).toBeDefined()
expect(lastForkBody).not.toHaveProperty('timeout')
expect(lastForkBody).not.toHaveProperty('count')
})
test('Sandbox.fork sends explicit timeout and count', async () => {
await Sandbox.fork('test-sandbox-id', {
apiKey: TEST_API_KEY,
timeoutMs: 60_000,
count: 2,
})
expect(lastForkBody?.timeout).toBe(60)
expect(lastForkBody?.count).toBe(2)
})
test('Sandbox.fork sends the maximum count', async () => {
await Sandbox.fork('test-sandbox-id', {
apiKey: TEST_API_KEY,
count: 20,
})
expect(lastForkBody?.count).toBe(20)
})
test.each([0, -1, 21, 101, 1.5])(
'Sandbox.fork rejects count %s',
async (count) => {
await expect(
Sandbox.fork('test-sandbox-id', { apiKey: TEST_API_KEY, count })
).rejects.toThrow(
new InvalidArgumentError('count must be an integer between 1 and 20')
)
expect(lastForkBody).toBeUndefined()
}
)
test('Sandbox.pause omits memory when mode is unset', async () => {
await Sandbox.pause('test-sandbox-id', { apiKey: TEST_API_KEY })
expect(lastPauseBody).toBeDefined()
expect(lastPauseBody).not.toHaveProperty('memory')
})
test.each([
['filesystem', false],
['full', true],
] as const)('Sandbox.pause maps mode %s to memory %s', async (mode, memory) => {
await Sandbox.pause('test-sandbox-id', { apiKey: TEST_API_KEY, mode })
expect(lastPauseBody?.memory).toBe(memory)
})
test('Sandbox.pause still sends the deprecated keepMemory', async () => {
await Sandbox.pause('test-sandbox-id', {
apiKey: TEST_API_KEY,
keepMemory: false,
})
expect(lastPauseBody?.memory).toBe(false)
})
test('Sandbox.pause rejects mode together with keepMemory', async () => {
await expect(
Sandbox.pause('test-sandbox-id', {
apiKey: TEST_API_KEY,
mode: 'full',
keepMemory: true,
})
).rejects.toThrowError(InvalidArgumentError)
expect(lastPauseBody).toBeUndefined()
})
test('Sandbox.pause rejects an unknown mode', async () => {
await expect(
Sandbox.pause('test-sandbox-id', {
apiKey: TEST_API_KEY,
mode: 'memory' as never,
})
).rejects.toThrow(
new InvalidArgumentError(
'mode must be one of: full, filesystem (got "memory").'
)
)
expect(lastPauseBody).toBeUndefined()
})
test('Sandbox.createSnapshot omits memory when mode is unset', async () => {
await Sandbox.createSnapshot('test-sandbox-id', { apiKey: TEST_API_KEY })
expect(lastSnapshotBody).toBeDefined()
expect(lastSnapshotBody).not.toHaveProperty('memory')
})
test.each([
['filesystem', false],
['full', true],
] as const)(
'Sandbox.createSnapshot maps mode %s to memory %s',
async (mode, memory) => {
await Sandbox.createSnapshot('test-sandbox-id', {
apiKey: TEST_API_KEY,
mode,
})
expect(lastSnapshotBody?.memory).toBe(memory)
}
)
test('Sandbox.connect omits timeout when unset', async () => {
await Sandbox.connect('test-sandbox-id', { apiKey: TEST_API_KEY })
expect(lastConnectBody).toBeDefined()
expect(lastConnectBody).not.toHaveProperty('timeout')
})
test('Sandbox.connect sends an explicit timeout', async () => {
await Sandbox.connect('test-sandbox-id', {
apiKey: TEST_API_KEY,
timeoutMs: 60_000,
})
expect(lastConnectBody?.timeout).toBe(60)
})