Files
social-app/backend/tests/unit/v1/agent/test_router_guards.py
T
qzl 1c02503d1d refactor: 简化 AgentScope 运行时模块与事件处理
- 移除冗余的 user_token 参数传递
- 重构 tool.result 事件使用 ToolAgentOutput 模型
- 重构 text.end 事件使用 WorkerAgentOutput 模型
- 简化 store 模块的 tool result 处理逻辑
- 更新 router/service 适配新事件结构
- 清理废弃的测试文件与设计文档
- 新增 AgentRuns 多模态存储设计文档
2026-03-13 17:27:18 +08:00

124 lines
3.7 KiB
Python

from __future__ import annotations
from types import SimpleNamespace
from typing import Any, cast
from uuid import uuid4
from ag_ui.core import RunAgentInput
from fastapi import HTTPException
import pytest
from core.auth.models import CurrentUser
from v1.agent import router as agent_router
def _resume_input_with_tool_message() -> RunAgentInput:
return RunAgentInput.model_validate(
{
"threadId": "00000000-0000-0000-0000-000000000001",
"runId": "run-resume-1",
"state": {},
"messages": [
{
"id": "tool-1",
"role": "tool",
"toolCallId": "call-1",
"content": '{"toolName":"navigate_to_route","result":{"ok":true}}',
}
],
"tools": [],
"context": [],
"forwardedProps": {},
}
)
@pytest.mark.asyncio
async def test_enqueue_resume_rejects_without_tool_contract() -> None:
request = RunAgentInput.model_validate(
{
"threadId": "00000000-0000-0000-0000-000000000001",
"runId": "run-resume-invalid",
"state": {},
"messages": [{"id": "u1", "role": "user", "content": "continue"}],
"tools": [],
"context": [],
"forwardedProps": {},
}
)
class _Service:
async def enqueue_resume(self, **kwargs): # noqa: ANN003
del kwargs
raise AssertionError("enqueue_resume should not be called")
with pytest.raises(HTTPException) as exc_info:
await agent_router.enqueue_resume(
thread_id="00000000-0000-0000-0000-000000000001",
request=request,
service=cast(Any, _Service()),
current_user=CurrentUser(id=uuid4(), email="user@example.com"),
)
assert exc_info.value.status_code == 422
@pytest.mark.asyncio
async def test_enqueue_resume_rejects_when_rate_limited(
monkeypatch: pytest.MonkeyPatch,
) -> None:
request = _resume_input_with_tool_message()
async def _deny_run(*, user_id: str) -> bool:
del user_id
return False
monkeypatch.setattr(agent_router, "_allow_run_request", _deny_run)
class _Service:
async def enqueue_resume(self, **kwargs): # noqa: ANN003
del kwargs
raise AssertionError("enqueue_resume should not be called")
with pytest.raises(HTTPException) as exc_info:
await agent_router.enqueue_resume(
thread_id="00000000-0000-0000-0000-000000000001",
request=request,
service=cast(Any, _Service()),
current_user=CurrentUser(id=uuid4(), email="user@example.com"),
)
assert exc_info.value.status_code == 429
@pytest.mark.asyncio
async def test_enqueue_resume_accepts_valid_tool_contract(
monkeypatch: pytest.MonkeyPatch,
) -> None:
request = _resume_input_with_tool_message()
async def _allow_run(*, user_id: str) -> bool:
del user_id
return True
monkeypatch.setattr(agent_router, "_allow_run_request", _allow_run)
class _Service:
async def enqueue_resume(self, **kwargs): # noqa: ANN003
return SimpleNamespace(
task_id="task-resume-1",
thread_id=kwargs["thread_id"],
run_id=kwargs["run_input"].run_id,
created=False,
)
result = await agent_router.enqueue_resume(
thread_id="00000000-0000-0000-0000-000000000001",
request=request,
service=cast(Any, _Service()),
current_user=CurrentUser(id=uuid4(), email="user@example.com"),
)
assert result.task_id == "task-resume-1"
assert result.run_id == "run-resume-1"