mirror of
https://github.com/yashshah9/agentbox.git
synced 2026-11-20 20:33:18 +00:00
Context: - Agent workflows need files written in one run to exist in the next without a gVisor backend. - Tar the sandbox workspace (minus the entry script) and restore it when snapshot_id is sent. Test Plan: - docker compose run --rm test - tests/test_api.py::test_snapshot_restore_keeps_workspace_file - tests/test_api.py::test_unknown_snapshot_returns_400 Additional Notes: - Snapshots live under AGENTBOX_SNAPSHOT_DIR (default /tmp/agentbox-snapshots). Isolation is still subprocess.
79 lines
2.0 KiB
Python
79 lines
2.0 KiB
Python
"""Tests for agentbox API."""
|
|
|
|
import shutil
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
from agentbox.api.app import app
|
|
|
|
client = TestClient(app)
|
|
|
|
|
|
def test_health() -> None:
|
|
resp = client.get("/health")
|
|
assert resp.status_code == 200
|
|
assert resp.json()["status"] == "ok"
|
|
|
|
|
|
def test_run_python() -> None:
|
|
resp = client.post("/v1/run", json={"code": "print('hello agentbox')"})
|
|
assert resp.status_code == 200
|
|
body = resp.json()
|
|
assert "hello agentbox" in body["stdout"]
|
|
assert body["exit_code"] == 0
|
|
|
|
|
|
def test_run_rejects_unknown_language() -> None:
|
|
resp = client.post("/v1/run", json={"code": "print(1)", "language": "ruby"})
|
|
assert resp.status_code == 400
|
|
|
|
|
|
def test_run_timeout_limit() -> None:
|
|
resp = client.post(
|
|
"/v1/run",
|
|
json={
|
|
"code": "import time; time.sleep(5)",
|
|
"limits": {"timeout_seconds": 1},
|
|
},
|
|
)
|
|
assert resp.status_code == 408
|
|
|
|
|
|
@pytest.mark.skipif(shutil.which("node") is None, reason="node not installed")
|
|
def test_run_javascript() -> None:
|
|
resp = client.post(
|
|
"/v1/run",
|
|
json={"code": "console.log('hello node')", "language": "javascript"},
|
|
)
|
|
assert resp.status_code == 200
|
|
assert "hello node" in resp.json()["stdout"]
|
|
assert resp.json()["exit_code"] == 0
|
|
|
|
|
|
def test_snapshot_restore_keeps_workspace_file() -> None:
|
|
first = client.post(
|
|
"/v1/run",
|
|
json={"code": "open('memo.txt','w').write('kept')", "snapshot": True},
|
|
)
|
|
assert first.status_code == 200
|
|
snapshot_id = first.json()["snapshot_id"]
|
|
assert snapshot_id
|
|
second = client.post(
|
|
"/v1/run",
|
|
json={
|
|
"code": "print(open('memo.txt').read())",
|
|
"snapshot_id": snapshot_id,
|
|
},
|
|
)
|
|
assert second.status_code == 200
|
|
assert "kept" in second.json()["stdout"]
|
|
|
|
|
|
def test_unknown_snapshot_returns_400() -> None:
|
|
resp = client.post(
|
|
"/v1/run",
|
|
json={"code": "print(1)", "snapshot_id": "does-not-exist"},
|
|
)
|
|
assert resp.status_code == 400
|