sandbox-agent/examples/docker/tests/docker.test.ts
Nathan Flurry 35840facdd SDK: Add ensureServer() for automatic server recovery
Add ensureServer() to SandboxProvider interface to handle cases where the
sandbox-agent server stops or goes to sleep. The SDK now calls this method
after 3 consecutive health-check failures, allowing providers to restart the
server if needed. Most built-in providers (E2B, Daytona, Vercel, Modal,
ComputeSDK) implement this. Docker and Cloudflare manage server lifecycle
differently, and Local uses managed child processes.

Also update docs for quickstart, architecture, multiplayer, and session
persistence; mark persist-* packages as deprecated; and add ensureServer
implementations to all applicable providers.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
2026-03-15 20:29:12 -07:00

53 lines
1.6 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { startDockerSandbox } from "@sandbox-agent/example-shared/docker";
/**
* Docker integration test.
*
* Set SANDBOX_AGENT_DOCKER_IMAGE to the image tag to test (e.g. a locally-built
* full image). The test starts a container from that image, waits for
* sandbox-agent to become healthy, and validates the /v1/health endpoint.
*/
const image = process.env.SANDBOX_AGENT_DOCKER_IMAGE;
const shouldRun = Boolean(image);
const timeoutMs = Number.parseInt(process.env.SANDBOX_TEST_TIMEOUT_MS || "", 10) || 300_000;
const testFn = shouldRun ? it : it.skip;
describe("docker example", () => {
testFn(
"starts sandbox-agent and responds to /v1/health",
async () => {
const { baseUrl, cleanup } = await startDockerSandbox({
port: 2468,
image: image!,
});
try {
// Wait for health check
let healthy = false;
for (let i = 0; i < 60; i++) {
try {
const res = await fetch(`${baseUrl}/v1/health`);
if (res.ok) {
const data = await res.json();
if (data.status === "ok") {
healthy = true;
break;
}
}
} catch {}
await new Promise((r) => setTimeout(r, 1000));
}
expect(healthy).toBe(true);
const response = await fetch(`${baseUrl}/v1/health`);
expect(response.ok).toBe(true);
const data = await response.json();
expect(data.status).toBe("ok");
} finally {
await cleanup();
}
},
timeoutMs,
);
});