test_main.py 2.59 KB
"""Tests for skill registration during AgentHub initialization."""
import pytest
from unittest.mock import patch, MagicMock


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

        mock_loader = MagicMock()
        mock_loader.load_agents.return_value = {"main": MagicMock(skills=[])}
        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_account_mappings.return_value = ({}, {})
        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)
                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.service.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_loader.load_agents.return_value = {"main": MagicMock(skills=[])}
        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_account_mappings.return_value = ({}, {})
        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)
                assert hub.skills == expected_skills
                assert "buddy-sings" in hub.skills