test_main.py 2.85 KB
"""Tests for skill registration during AgentHub initialization."""
import pytest
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="你是智能助手",
            description="协调调度各子Agent",
            is_steward=True,
        ),
    }


class TestSkillRegistration:
    def test_agent_hub_registers_skills_on_init(self):
        """AgentHub.__init__ should register skills to Toolkit via register_agent_skill."""
        from app.agent_core.agent_hub import AgentHub

        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 = {}
        mock_session_service = MagicMock()
        mock_im = MagicMock()

        with patch('agentscope.tool.Toolkit.register_agent_skill') as mock_register:
            with patch('agentscope.model.OpenAIChatModel.__init__', return_value=None):
                hub = AgentHub(
                    mock_loader, mock_session_service, mock_im,
                    preloaded_agents=_make_test_agents(),
                )
                assert mock_register.called

    def test_agent_hub_skills_loaded_from_toolkit(self):
        """Skills dict should come from load_skills_from_toolkit, not load_skills."""
        from app.agent_core.agent_hub import AgentHub

        expected_skills = {
            "buddy-sings": {
                "code": "buddy-sings",
                "name": "buddy-sings",
                "description": "Test desc",
                "instruction": "Test desc",
                "location": "skills/buddy-sings",
            }
        }

        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 = expected_skills
        mock_session_service = MagicMock()
        mock_im = MagicMock()

        with patch('agentscope.tool.Toolkit.register_agent_skill'):
            with patch('agentscope.model.OpenAIChatModel.__init__', return_value=None):
                hub = AgentHub(
                    mock_loader, mock_session_service, mock_im,
                    preloaded_agents=_make_test_agents(),
                )
                assert hub.skills == expected_skills
                assert "buddy-sings" in hub.skills