108 lines
3.3 KiB
Python
108 lines
3.3 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from types import SimpleNamespace
|
||
|
|
from uuid import uuid4
|
||
|
|
|
||
|
|
from fastapi.testclient import TestClient
|
||
|
|
|
||
|
|
from app import app
|
||
|
|
from core.auth.models import CurrentUser
|
||
|
|
from v1.agent.dependencies import get_agent_service
|
||
|
|
from v1.users.dependencies import get_current_user
|
||
|
|
|
||
|
|
|
||
|
|
class _FakeAgentService:
|
||
|
|
def __init__(self) -> None:
|
||
|
|
self._stream_called = False
|
||
|
|
|
||
|
|
async def enqueue_run(
|
||
|
|
self, *, session_id: str | None, prompt: str, current_user: CurrentUser
|
||
|
|
):
|
||
|
|
del prompt, current_user
|
||
|
|
resolved_session = session_id or "auto-created-session"
|
||
|
|
return SimpleNamespace(
|
||
|
|
task_id="task-run-1",
|
||
|
|
session_id=resolved_session,
|
||
|
|
created=session_id is None,
|
||
|
|
)
|
||
|
|
|
||
|
|
async def enqueue_resume(
|
||
|
|
self,
|
||
|
|
*,
|
||
|
|
session_id: str,
|
||
|
|
tool_call_id: str,
|
||
|
|
current_user: CurrentUser,
|
||
|
|
):
|
||
|
|
del tool_call_id, current_user
|
||
|
|
return SimpleNamespace(
|
||
|
|
task_id="task-resume-1", session_id=session_id, created=False
|
||
|
|
)
|
||
|
|
|
||
|
|
async def stream_events(
|
||
|
|
self,
|
||
|
|
*,
|
||
|
|
session_id: str,
|
||
|
|
last_event_id: str | None,
|
||
|
|
current_user: CurrentUser,
|
||
|
|
) -> list[dict[str, object]]:
|
||
|
|
del session_id, current_user
|
||
|
|
if self._stream_called:
|
||
|
|
return []
|
||
|
|
self._stream_called = True
|
||
|
|
return [
|
||
|
|
{"id": "2-0", "event": {"type": "RUN_STARTED"}, "cursor": last_event_id}
|
||
|
|
]
|
||
|
|
|
||
|
|
|
||
|
|
def test_run_requires_auth_and_returns_202_task_id() -> None:
|
||
|
|
app.dependency_overrides[get_agent_service] = lambda: _FakeAgentService()
|
||
|
|
client = TestClient(app)
|
||
|
|
|
||
|
|
try:
|
||
|
|
unauthorized = client.post(
|
||
|
|
"/api/v1/agent/runs",
|
||
|
|
json={"session_id": "session-1", "prompt": "hello"},
|
||
|
|
)
|
||
|
|
assert unauthorized.status_code == 401
|
||
|
|
|
||
|
|
app.dependency_overrides[get_current_user] = lambda: CurrentUser(
|
||
|
|
id=uuid4(), email="user@example.com"
|
||
|
|
)
|
||
|
|
authorized = client.post(
|
||
|
|
"/api/v1/agent/runs",
|
||
|
|
json={"session_id": "session-1", "prompt": "hello"},
|
||
|
|
)
|
||
|
|
assert authorized.status_code == 202
|
||
|
|
assert authorized.json()["task_id"] == "task-run-1"
|
||
|
|
assert authorized.json()["created"] is False
|
||
|
|
|
||
|
|
first_chat = client.post(
|
||
|
|
"/api/v1/agent/runs",
|
||
|
|
json={"prompt": "hello"},
|
||
|
|
)
|
||
|
|
assert first_chat.status_code == 202
|
||
|
|
assert first_chat.json()["session_id"] == "auto-created-session"
|
||
|
|
assert first_chat.json()["created"] is True
|
||
|
|
finally:
|
||
|
|
app.dependency_overrides = {}
|
||
|
|
|
||
|
|
|
||
|
|
def test_stream_reads_from_last_event_id() -> None:
|
||
|
|
app.dependency_overrides[get_agent_service] = lambda: _FakeAgentService()
|
||
|
|
app.dependency_overrides[get_current_user] = lambda: CurrentUser(
|
||
|
|
id=uuid4(), email="user@example.com"
|
||
|
|
)
|
||
|
|
client = TestClient(app)
|
||
|
|
|
||
|
|
try:
|
||
|
|
response = client.get(
|
||
|
|
"/api/v1/agent/runs/session-1/events?idle_limit=1",
|
||
|
|
headers={"Last-Event-ID": "1-0"},
|
||
|
|
)
|
||
|
|
assert response.status_code == 200
|
||
|
|
assert response.headers["content-type"].startswith("text/event-stream")
|
||
|
|
assert "id: 2-0" in response.text
|
||
|
|
assert "event: RUN_STARTED" in response.text
|
||
|
|
finally:
|
||
|
|
app.dependency_overrides = {}
|