Commit 1005a7a4 by sunqi02 Committed by 胡悦

test: update tests for DB-driven agent config

- test_main.py: use preloaded_agents, new AgentConfig structure
- test_agent_hub.py: replace all 'main'/'meeting' with STEWARD_KEY/MEETING_KEY
- test_read_file_tool.py: use mock config_loader, add preloaded_agents
- PostReplyHook tests: remove account_mapping, add bot_id_mapping to context
- Routing tests: assert response_agent_code == STEWARD_KEY
- Collaboration tests: use dynamic agent keys
1 parent 68ac224a
...@@ -2,6 +2,18 @@ ...@@ -2,6 +2,18 @@
import pytest import pytest
from unittest.mock import patch, MagicMock from unittest.mock import patch, MagicMock
from app.config.settings import AgentConfig
def _make_test_agents():
"""Create test agent configs for AgentHub init."""
return {
"steward_key": AgentConfig(
name="管家 Agent", code="11",
prompt="你是智能助手", is_steward=True,
),
}
class TestSkillRegistration: class TestSkillRegistration:
def test_agent_hub_registers_skills_on_init(self): def test_agent_hub_registers_skills_on_init(self):
...@@ -9,20 +21,21 @@ class TestSkillRegistration: ...@@ -9,20 +21,21 @@ class TestSkillRegistration:
from app.service.agent_hub import AgentHub from app.service.agent_hub import AgentHub
mock_loader = MagicMock() mock_loader = MagicMock()
mock_loader.load_agents.return_value = {"main": MagicMock(skills=[])}
mock_model_config = MagicMock() mock_model_config = MagicMock()
mock_model_config.model_name = "test-model" mock_model_config.model_name = "test-model"
mock_model_config.api_key = "test-key" mock_model_config.api_key = "test-key"
mock_model_config.api_base = "http://test" mock_model_config.api_base = "http://test"
mock_loader.load_models.return_value = {"main": mock_model_config} mock_loader.load_models.return_value = {"main": mock_model_config}
mock_loader.load_account_mappings.return_value = ({}, {})
mock_loader.load_skills_from_toolkit.return_value = {} mock_loader.load_skills_from_toolkit.return_value = {}
mock_session_service = MagicMock() mock_session_service = MagicMock()
mock_im = MagicMock() mock_im = MagicMock()
with patch('agentscope.tool.Toolkit.register_agent_skill') as mock_register: with patch('agentscope.tool.Toolkit.register_agent_skill') as mock_register:
with patch('agentscope.model.OpenAIChatModel.__init__', return_value=None): with patch('agentscope.model.OpenAIChatModel.__init__', return_value=None):
hub = AgentHub(mock_loader, mock_session_service, mock_im) hub = AgentHub(
mock_loader, mock_session_service, mock_im,
preloaded_agents=_make_test_agents(),
)
assert mock_register.called assert mock_register.called
def test_agent_hub_skills_loaded_from_toolkit(self): def test_agent_hub_skills_loaded_from_toolkit(self):
...@@ -40,19 +53,20 @@ class TestSkillRegistration: ...@@ -40,19 +53,20 @@ class TestSkillRegistration:
} }
mock_loader = MagicMock() mock_loader = MagicMock()
mock_loader.load_agents.return_value = {"main": MagicMock(skills=[])}
mock_model_config = MagicMock() mock_model_config = MagicMock()
mock_model_config.model_name = "test-model" mock_model_config.model_name = "test-model"
mock_model_config.api_key = "test-key" mock_model_config.api_key = "test-key"
mock_model_config.api_base = "http://test" mock_model_config.api_base = "http://test"
mock_loader.load_models.return_value = {"main": mock_model_config} mock_loader.load_models.return_value = {"main": mock_model_config}
mock_loader.load_account_mappings.return_value = ({}, {})
mock_loader.load_skills_from_toolkit.return_value = expected_skills mock_loader.load_skills_from_toolkit.return_value = expected_skills
mock_session_service = MagicMock() mock_session_service = MagicMock()
mock_im = MagicMock() mock_im = MagicMock()
with patch('agentscope.tool.Toolkit.register_agent_skill'): with patch('agentscope.tool.Toolkit.register_agent_skill'):
with patch('agentscope.model.OpenAIChatModel.__init__', return_value=None): with patch('agentscope.model.OpenAIChatModel.__init__', return_value=None):
hub = AgentHub(mock_loader, mock_session_service, mock_im) hub = AgentHub(
mock_loader, mock_session_service, mock_im,
preloaded_agents=_make_test_agents(),
)
assert hub.skills == expected_skills assert hub.skills == expected_skills
assert "buddy-sings" in hub.skills assert "buddy-sings" in hub.skills
...@@ -5,7 +5,6 @@ import shutil ...@@ -5,7 +5,6 @@ import shutil
from pathlib import Path from pathlib import Path
from unittest.mock import MagicMock, AsyncMock from unittest.mock import MagicMock, AsyncMock
from app.config.loader import ConfigLoader
from app.service.active_session_service import ActiveSessionService from app.service.active_session_service import ActiveSessionService
from app.service.agent_hub import AgentHub from app.service.agent_hub import AgentHub
from agentscope.message._message_block import TextBlock from agentscope.message._message_block import TextBlock
...@@ -39,23 +38,39 @@ def sandbox_root(tmp_path): ...@@ -39,23 +38,39 @@ def sandbox_root(tmp_path):
@pytest.fixture @pytest.fixture
def hub_with_sandbox(sandbox_root): def hub_with_sandbox(sandbox_root):
"""Create an AgentHub whose read_file tool is sandboxed to tmp_path.""" """Create an AgentHub whose read_file tool is sandboxed to tmp_path."""
from app.config.settings import AgentConfig
test_agents = {
"test_steward": AgentConfig(
name="测试管家", code="1",
prompt="测试prompt", is_steward=True,
),
}
mock_loader = MagicMock()
mock_model_config = MagicMock()
mock_model_config.model_name = "test-model"
mock_model_config.api_key = "test-key"
mock_model_config.api_base = "http://test"
mock_loader.load_models.return_value = {"main": mock_model_config}
mock_loader.load_skills_from_toolkit.return_value = {}
session_service = ActiveSessionService() session_service = ActiveSessionService()
im_service = MagicMock() im_service = MagicMock()
im_service.send_message = AsyncMock(return_value={"code": 200}) im_service.send_message = AsyncMock(return_value={"code": 200})
hub = AgentHub( hub = AgentHub(
config_loader=ConfigLoader(), config_loader=mock_loader,
session_service=session_service, session_service=session_service,
im_service=im_service, im_service=im_service,
preloaded_agents=test_agents,
) )
# Override the read_file tool with one rooted at sandbox_root # Override the read_file tool with one rooted at sandbox_root
# by creating a new closure with the test root
from agentscope.tool import Toolkit from agentscope.tool import Toolkit
read_file_func = _make_read_file_with_root(sandbox_root) read_file_func = _make_read_file_with_root(sandbox_root)
# Replace in master toolkit and all agent toolkits
hub.toolkit.register_tool_function( hub.toolkit.register_tool_function(
read_file_func, read_file_func,
func_name="read_file", func_name="read_file",
...@@ -226,13 +241,31 @@ class TestReadFileToolRegistration: ...@@ -226,13 +241,31 @@ class TestReadFileToolRegistration:
def test_read_file_in_toolkit_schemas(self): def test_read_file_in_toolkit_schemas(self):
"""read_file 出现在 toolkit JSON schema 中""" """read_file 出现在 toolkit JSON schema 中"""
from app.config.settings import AgentConfig
test_agents = {
"test_steward": AgentConfig(
name="测试管家", code="1",
prompt="测试prompt", is_steward=True,
),
}
mock_loader = MagicMock()
mock_model_config = MagicMock()
mock_model_config.model_name = "test-model"
mock_model_config.api_key = "test-key"
mock_model_config.api_base = "http://test"
mock_loader.load_models.return_value = {"main": mock_model_config}
mock_loader.load_skills_from_toolkit.return_value = {}
im_service = MagicMock() im_service = MagicMock()
im_service.send_message = AsyncMock(return_value={"code": 200}) im_service.send_message = AsyncMock(return_value={"code": 200})
hub = AgentHub( hub = AgentHub(
config_loader=ConfigLoader(), config_loader=mock_loader,
session_service=ActiveSessionService(), session_service=ActiveSessionService(),
im_service=im_service, im_service=im_service,
preloaded_agents=test_agents,
) )
schemas = hub.toolkit.get_json_schemas() schemas = hub.toolkit.get_json_schemas()
...@@ -245,13 +278,31 @@ class TestReadFileToolRegistration: ...@@ -245,13 +278,31 @@ class TestReadFileToolRegistration:
def test_read_file_in_all_agent_toolkits(self): def test_read_file_in_all_agent_toolkits(self):
"""read_file 注册到所有 agent 的专属 toolkit""" """read_file 注册到所有 agent 的专属 toolkit"""
from app.config.settings import AgentConfig
test_agents = {
"test_steward": AgentConfig(
name="测试管家", code="1",
prompt="测试prompt", is_steward=True,
),
}
mock_loader = MagicMock()
mock_model_config = MagicMock()
mock_model_config.model_name = "test-model"
mock_model_config.api_key = "test-key"
mock_model_config.api_base = "http://test"
mock_loader.load_models.return_value = {"main": mock_model_config}
mock_loader.load_skills_from_toolkit.return_value = {}
im_service = MagicMock() im_service = MagicMock()
im_service.send_message = AsyncMock(return_value={"code": 200}) im_service.send_message = AsyncMock(return_value={"code": 200})
hub = AgentHub( hub = AgentHub(
config_loader=ConfigLoader(), config_loader=mock_loader,
session_service=ActiveSessionService(), session_service=ActiveSessionService(),
im_service=im_service, im_service=im_service,
preloaded_agents=test_agents,
) )
for agent_code, agent_tk in hub.agent_toolkits.items(): for agent_code, agent_tk in hub.agent_toolkits.items():
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!