"""
E2E tests for Agent Lifecycle operations.
These tests verify pause/resume and delete/resume flows.
"""
import pytest
from httpx import AsyncClient, ASGITransport
from src.main import create_app
AUTH_HEADERS = {"Authorization": "Bearer test-token"}
@pytest.mark.asyncio
async def test_pause_resume_flow():
"""Test pause and resume agent lifecycle operations."""
app = create_app()
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
response = await client.post(
"/agents",
json={
"name": "test-agent",
"sandbox_type": "local_process",
"adapter_type": "openclaw",
"idle_timeout_seconds": 3600,
},
headers=AUTH_HEADERS,
)
assert response.status_code == 201
agent_id = response.json()["id"]
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
response = await client.post(
f"/agents/{agent_id}/pause",
headers=AUTH_HEADERS,
)
assert response.status_code == 200
assert response.json()["status"] == "paused"
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
response = await client.post(
f"/agents/{agent_id}/resume",
headers=AUTH_HEADERS,
)
assert response.status_code == 200
assert response.json()["status"] == "running"
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
await client.delete(
f"/agents/{agent_id}",
headers=AUTH_HEADERS,
)
@pytest.mark.asyncio
async def test_delete_resume_flow():
"""Test resume from deleted agent - agent should be recreated."""
app = create_app()
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
response = await client.post(
"/agents",
json={
"name": "test-agent",
"sandbox_type": "local_process",
"adapter_type": "openclaw",
"idle_timeout_seconds": 3600,
},
headers=AUTH_HEADERS,
)
assert response.status_code == 201
agent_id = response.json()["id"]
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
response = await client.delete(
f"/agents/{agent_id}",
headers=AUTH_HEADERS,
)
assert response.status_code == 204
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
response = await client.post(
f"/agents/{agent_id}/resume",
headers=AUTH_HEADERS,
)
assert response.status_code == 200
assert response.json()["status"] == "running"