Files
agentbox/tests/test_api.py
T
Yash Shah f0045a3a5f fix(api): snapshot files only and surface HTTP errors in the SDK
Context:
- tar.add on directories recursively re-included main.py/main.js that snapshot restore is supposed to skip.
- AgentboxClient.raise_for_status turned expected 408 timeouts into opaque httpx errors.
- Missing runtimes returned HTTP 200 with exit 127 instead of 400.

Test Plan:
- docker compose run --rm test
- tests/test_api.py::test_snapshot_restore_keeps_workspace_file
- tests/test_api.py::test_run_timeout_limit

Additional Notes:
- health CLI now prints the package version, matching the other five CLIs.
- TestClient is used as a context manager to avoid the Starlette lifespan warning path.
2026-08-19 11:57:34 +05:30

84 lines
2.3 KiB
Python

"""Tests for agentbox API."""
import shutil
from collections.abc import Iterator
import pytest
from fastapi.testclient import TestClient
from agentbox.api.app import app
@pytest.fixture
def client() -> Iterator[TestClient]:
with TestClient(app) as test_client:
yield test_client
def test_health(client: TestClient) -> None:
resp = client.get("/health")
assert resp.status_code == 200
assert resp.json()["status"] == "ok"
def test_run_python(client: TestClient) -> 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(client: TestClient) -> None:
resp = client.post("/v1/run", json={"code": "print(1)", "language": "ruby"})
assert resp.status_code == 400
def test_run_timeout_limit(client: TestClient) -> 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(client: TestClient) -> 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(client: TestClient) -> 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(client: TestClient) -> None:
resp = client.post(
"/v1/run",
json={"code": "print(1)", "snapshot_id": "does-not-exist"},
)
assert resp.status_code == 400