|
|
@@ -0,0 +1,305 @@
|
|
|
+# -*- coding: utf-8 -*-
|
|
|
+import unittest
|
|
|
+import json
|
|
|
+from unittest.mock import patch, AsyncMock, MagicMock
|
|
|
+from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
+
|
|
|
+from app.schemas.agent import AgentChatRequest
|
|
|
+from app.models.user import VasUser
|
|
|
+from app.services.agent.tools.registry import ToolRegistry, agent_tool, tool_registry
|
|
|
+from app.services.agent.memory import AgentMemory
|
|
|
+from app.services.agent.engine import AgentEngine
|
|
|
+from app.services.agent.tools.builtin.order_tools import query_user_orders, cancel_user_order
|
|
|
+from app.services.agent.tools.builtin.product_tools import search_products
|
|
|
+
|
|
|
+
|
|
|
+class TestAgentToolsAndRegistry(unittest.IsolatedAsyncioTestCase):
|
|
|
+ """测试工具注册、OpenAPI Schema 自动生成与上下文自动注入"""
|
|
|
+
|
|
|
+ def test_tool_schema_generation(self):
|
|
|
+ registry = ToolRegistry()
|
|
|
+
|
|
|
+ @registry.register(name="mock_search", description="Search items")
|
|
|
+ def mock_search(keyword: str, limit: int = 10, *, db: AsyncSession, current_user: VasUser):
|
|
|
+ return {"keyword": keyword, "limit": limit}
|
|
|
+
|
|
|
+ tool = registry.get_tool("mock_search")
|
|
|
+ self.assertIsNotNone(tool)
|
|
|
+ schema = tool.openai_schema
|
|
|
+
|
|
|
+ self.assertEqual(schema["type"], "function")
|
|
|
+ fn = schema["function"]
|
|
|
+ self.assertEqual(fn["name"], "mock_search")
|
|
|
+ self.assertEqual(fn["description"], "Search items")
|
|
|
+
|
|
|
+ # 验证保留参数 db, current_user 没有暴露给 LLM
|
|
|
+ props = fn["parameters"]["properties"]
|
|
|
+ self.assertIn("keyword", props)
|
|
|
+ self.assertIn("limit", props)
|
|
|
+ self.assertNotIn("db", props)
|
|
|
+ self.assertNotIn("current_user", props)
|
|
|
+ self.assertEqual(props["keyword"]["type"], "string")
|
|
|
+ self.assertEqual(props["limit"]["type"], "integer")
|
|
|
+
|
|
|
+ # keyword 无默认值应为必填,limit 有默认值不应在 required 中
|
|
|
+ self.assertIn("keyword", fn["parameters"]["required"])
|
|
|
+ self.assertNotIn("limit", fn["parameters"]["required"])
|
|
|
+
|
|
|
+ async def test_tool_execution_with_context_injection(self):
|
|
|
+ registry = ToolRegistry()
|
|
|
+
|
|
|
+ @registry.register(name="echo_tool", description="Echo tool")
|
|
|
+ async def echo_tool(msg: str, *, db: AsyncSession, current_user: VasUser):
|
|
|
+ return {
|
|
|
+ "echo": msg,
|
|
|
+ "user_id": current_user.id,
|
|
|
+ "db_injected": db is not None,
|
|
|
+ }
|
|
|
+
|
|
|
+ mock_user = VasUser(id="usr_123", role="user")
|
|
|
+ mock_db = MagicMock(spec=AsyncSession)
|
|
|
+
|
|
|
+ result = await registry.execute(
|
|
|
+ "echo_tool",
|
|
|
+ arguments={"msg": "hello agent"},
|
|
|
+ context={"db": mock_db, "current_user": mock_user},
|
|
|
+ )
|
|
|
+
|
|
|
+ self.assertEqual(result["echo"], "hello agent")
|
|
|
+ self.assertEqual(result["user_id"], "usr_123")
|
|
|
+ self.assertTrue(result["db_injected"])
|
|
|
+
|
|
|
+
|
|
|
+class TestAgentMemory(unittest.IsolatedAsyncioTestCase):
|
|
|
+ """测试 Redis 会话记忆"""
|
|
|
+
|
|
|
+ @patch("app.services.agent.memory.get_redis_client")
|
|
|
+ async def test_memory_append_and_get(self, mock_get_redis):
|
|
|
+ mock_redis = AsyncMock()
|
|
|
+ storage = {}
|
|
|
+
|
|
|
+ async def fake_get(key):
|
|
|
+ return storage.get(key)
|
|
|
+
|
|
|
+ async def fake_setex(key, ttl, value):
|
|
|
+ storage[key] = value
|
|
|
+
|
|
|
+ async def fake_delete(key):
|
|
|
+ storage.pop(key, None)
|
|
|
+
|
|
|
+ mock_redis.get = fake_get
|
|
|
+ mock_redis.setex = fake_setex
|
|
|
+ mock_redis.delete = fake_delete
|
|
|
+ mock_get_redis.return_value = mock_redis
|
|
|
+
|
|
|
+ session_id = "test_sess_001"
|
|
|
+ # 初始为空
|
|
|
+ history = await AgentMemory.get_history(session_id)
|
|
|
+ self.assertEqual(history, [])
|
|
|
+
|
|
|
+ # 追加新消息
|
|
|
+ await AgentMemory.append_messages(
|
|
|
+ session_id,
|
|
|
+ [
|
|
|
+ {"role": "user", "content": "我的订单状态是什么?"},
|
|
|
+ {"role": "assistant", "content": "您有1笔待支付订单。"},
|
|
|
+ ],
|
|
|
+ )
|
|
|
+
|
|
|
+ history2 = await AgentMemory.get_history(session_id)
|
|
|
+ self.assertEqual(len(history2), 2)
|
|
|
+ self.assertEqual(history2[0]["content"], "我的订单状态是什么?")
|
|
|
+
|
|
|
+ # 清除 session
|
|
|
+ await AgentMemory.clear_session(session_id)
|
|
|
+ history3 = await AgentMemory.get_history(session_id)
|
|
|
+ self.assertEqual(history3, [])
|
|
|
+
|
|
|
+
|
|
|
+class TestAgentEngine(unittest.IsolatedAsyncioTestCase):
|
|
|
+ """测试 Agent 编排引擎:多步工具调用与流式事件生成"""
|
|
|
+
|
|
|
+ @patch("app.services.agent.memory.AgentMemory.append_messages", new_callable=AsyncMock)
|
|
|
+ @patch("app.services.agent.memory.AgentMemory.get_history", new_callable=AsyncMock)
|
|
|
+ @patch("app.services.llm_service.LlmService.chat_completion_with_tools_async")
|
|
|
+ async def test_agent_sync_tool_call_loop(self, mock_chat, mock_get_history, mock_append):
|
|
|
+ mock_get_history.return_value = []
|
|
|
+ mock_user = VasUser(id="user_test_99", role="user")
|
|
|
+ mock_db = MagicMock(spec=AsyncSession)
|
|
|
+
|
|
|
+ # 模拟 LLM 第一步发起工具调用,第二步汇总回答
|
|
|
+ mock_chat.side_effect = [
|
|
|
+ {
|
|
|
+ "role": "assistant",
|
|
|
+ "content": "",
|
|
|
+ "tool_calls": [
|
|
|
+ {
|
|
|
+ "id": "call_abc123",
|
|
|
+ "type": "function",
|
|
|
+ "function": {
|
|
|
+ "name": "query_user_orders",
|
|
|
+ "arguments": json.dumps({"limit": 5}),
|
|
|
+ },
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "role": "assistant",
|
|
|
+ "content": "您当前有0笔订单。",
|
|
|
+ "tool_calls": None,
|
|
|
+ },
|
|
|
+ ]
|
|
|
+
|
|
|
+ # 模拟 db.execute 返回空订单列表
|
|
|
+ mock_result = MagicMock()
|
|
|
+ mock_result.scalars().all.return_value = []
|
|
|
+ mock_db.execute = AsyncMock(return_value=mock_result)
|
|
|
+
|
|
|
+ request = AgentChatRequest(message="查一下我的最新订单", session_id="test_sess_run")
|
|
|
+ response = await AgentEngine.run_sync(
|
|
|
+ request=request,
|
|
|
+ current_user=mock_user,
|
|
|
+ db=mock_db,
|
|
|
+ )
|
|
|
+
|
|
|
+ self.assertEqual(response.answer, "您当前有0笔订单。")
|
|
|
+ self.assertEqual(len(response.steps), 1)
|
|
|
+ self.assertEqual(response.steps[0].tool, "query_user_orders")
|
|
|
+ self.assertEqual(response.steps[0].arguments, {"limit": 5})
|
|
|
+ self.assertFalse(response.steps[0].is_error)
|
|
|
+
|
|
|
+ @patch("app.services.agent.memory.AgentMemory.append_messages", new_callable=AsyncMock)
|
|
|
+ @patch("app.services.agent.memory.AgentMemory.get_history", new_callable=AsyncMock)
|
|
|
+ @patch("app.services.llm_service.LlmService.chat_completion_with_tools_async")
|
|
|
+ async def test_agent_stream_events(self, mock_chat, mock_get_history, mock_append):
|
|
|
+ mock_get_history.return_value = []
|
|
|
+ mock_user = VasUser(id="user_test_99", role="user")
|
|
|
+ mock_db = MagicMock(spec=AsyncSession)
|
|
|
+
|
|
|
+ # 模拟无工具调用直接回答
|
|
|
+ mock_chat.return_value = {
|
|
|
+ "role": "assistant",
|
|
|
+ "content": "你好,我是你的系统业务助手!",
|
|
|
+ "tool_calls": None,
|
|
|
+ }
|
|
|
+
|
|
|
+ request = AgentChatRequest(message="你好", stream=True)
|
|
|
+ events = []
|
|
|
+ async for chunk in AgentEngine.run_stream(
|
|
|
+ request=request,
|
|
|
+ current_user=mock_user,
|
|
|
+ db=mock_db,
|
|
|
+ ):
|
|
|
+ events.append(chunk)
|
|
|
+
|
|
|
+ combined = "".join(events)
|
|
|
+ self.assertIn("event: think", combined)
|
|
|
+ self.assertIn("event: message", combined)
|
|
|
+ self.assertIn("event: finish", combined)
|
|
|
+ self.assertIn("你好,我是你的系统业务助手!", combined)
|
|
|
+
|
|
|
+
|
|
|
+class TestAgentAPIEndpoints(unittest.TestCase):
|
|
|
+ """测试 FastAPI 接口层挂载与鉴权"""
|
|
|
+
|
|
|
+ def setUp(self):
|
|
|
+ from fastapi.testclient import TestClient
|
|
|
+ from app.main import app
|
|
|
+ from app.core.auth import get_current_user
|
|
|
+
|
|
|
+ self.mock_user = VasUser(id="api_test_user", role="user", email="test@example.com")
|
|
|
+
|
|
|
+ async def override_current_user():
|
|
|
+ return self.mock_user
|
|
|
+
|
|
|
+ app.dependency_overrides[get_current_user] = override_current_user
|
|
|
+ self.client = TestClient(app)
|
|
|
+
|
|
|
+ def tearDown(self):
|
|
|
+ from app.main import app
|
|
|
+ app.dependency_overrides.clear()
|
|
|
+
|
|
|
+ def test_list_tools_endpoint(self):
|
|
|
+ response = self.client.get("/api/agent/tools")
|
|
|
+ self.assertEqual(response.status_code, 200)
|
|
|
+ data = response.json()
|
|
|
+ self.assertEqual(data["code"], 0)
|
|
|
+ tool_names = [t["name"] for t in data["data"]]
|
|
|
+ self.assertIn("query_user_orders", tool_names)
|
|
|
+ self.assertIn("search_products", tool_names)
|
|
|
+ self.assertIn("list_node_configurations", tool_names)
|
|
|
+ self.assertIn("toggle_node_configuration", tool_names)
|
|
|
+ self.assertIn("rollback_node_configuration", tool_names)
|
|
|
+ self.assertIn("list_plugins", tool_names)
|
|
|
+ self.assertIn("get_proxy_pool_stats", tool_names)
|
|
|
+
|
|
|
+ @patch("app.services.agent.engine.AgentEngine.run_sync")
|
|
|
+ def test_agent_chat_sync_endpoint(self, mock_run_sync):
|
|
|
+ from app.schemas.agent import AgentChatResponse
|
|
|
+
|
|
|
+ mock_run_sync.return_value = AgentChatResponse(
|
|
|
+ session_id="sess_123",
|
|
|
+ answer="为您查到以下商品",
|
|
|
+ steps=[],
|
|
|
+ steps_count=0,
|
|
|
+ )
|
|
|
+
|
|
|
+ response = self.client.post(
|
|
|
+ "/api/agent/chat",
|
|
|
+ json={"message": "帮我看看有什么商品", "stream": False},
|
|
|
+ )
|
|
|
+ self.assertEqual(response.status_code, 200)
|
|
|
+ data = response.json()
|
|
|
+ self.assertEqual(data["code"], 0)
|
|
|
+ self.assertEqual(data["data"]["answer"], "为您查到以下商品")
|
|
|
+
|
|
|
+
|
|
|
+class TestOpsToolsPermissions(unittest.IsolatedAsyncioTestCase):
|
|
|
+ """测试运维管理工具的管理员角色鉴权拦截"""
|
|
|
+
|
|
|
+ async def test_non_admin_blocked_from_node_configs(self):
|
|
|
+ from app.services.agent.tools.builtin.node_config_tools import list_node_configurations, toggle_node_configuration
|
|
|
+
|
|
|
+ normal_user = VasUser(id="u1", role="user")
|
|
|
+ mock_db = MagicMock(spec=AsyncSession)
|
|
|
+
|
|
|
+ res1 = await list_node_configurations(db=mock_db, current_user=normal_user)
|
|
|
+ self.assertIn("error", res1)
|
|
|
+ self.assertIn("权限不足", res1["error"])
|
|
|
+
|
|
|
+ res2 = await toggle_node_configuration(entry_type="booker", entry_id=1, enable=True, db=mock_db, current_user=normal_user)
|
|
|
+ self.assertIn("error", res2)
|
|
|
+ self.assertIn("权限不足", res2["error"])
|
|
|
+
|
|
|
+ async def test_non_admin_blocked_from_plugins_and_proxies(self):
|
|
|
+ from app.services.agent.tools.builtin.plugin_tools import list_plugins
|
|
|
+ from app.services.agent.tools.builtin.proxy_pool_tools import get_proxy_pool_stats
|
|
|
+
|
|
|
+ normal_user = VasUser(id="u1", role="user")
|
|
|
+ mock_db = MagicMock(spec=AsyncSession)
|
|
|
+
|
|
|
+ p_res = await list_plugins(db=mock_db, current_user=normal_user)
|
|
|
+ self.assertIn("error", p_res)
|
|
|
+ self.assertIn("权限不足", p_res["error"])
|
|
|
+
|
|
|
+ prx_res = await get_proxy_pool_stats(db=mock_db, current_user=normal_user)
|
|
|
+ self.assertIn("error", prx_res)
|
|
|
+ self.assertIn("权限不足", prx_res["error"])
|
|
|
+
|
|
|
+ @patch("app.services.node_configuration_service.NodeConfigurationService.list_entries")
|
|
|
+ async def test_admin_allowed_for_node_configs(self, mock_list_entries):
|
|
|
+ from app.services.agent.tools.builtin.node_config_tools import list_node_configurations
|
|
|
+
|
|
|
+ admin_user = VasUser(id="admin_1", role="admin")
|
|
|
+ mock_db = MagicMock(spec=AsyncSession)
|
|
|
+ mock_list_entries.return_value = ([], 0)
|
|
|
+
|
|
|
+ res = await list_node_configurations(db=mock_db, current_user=admin_user)
|
|
|
+ self.assertNotIn("error", res)
|
|
|
+ self.assertEqual(res["total"], 0)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ unittest.main()
|
|
|
+
|