test_toolkit.py 3.7 KB
"""AgentScope Toolkit behavior tests — verify group & schema management."""
import pytest
from agentscope.tool import Toolkit


class TestToolkitRegister:
    def test_register_function_to_basic_group(self):
        """Register to implicit basic group — always returns schema."""
        toolkit = Toolkit()

        async def get_weather(city: str) -> str:
            """Query city weather."""
            return f"{city}: sunny 25°C"

        toolkit.register_tool_function(get_weather, group_name="basic")

        schemas = toolkit.get_json_schemas()
        names = [s["function"]["name"] for s in schemas]
        assert "get_weather" in names

    def test_register_to_uncreated_group_raises(self):
        """AgentScope: non-basic groups must be created first or ValueError."""
        toolkit = Toolkit()

        async def secret_tool(x: str) -> str:
            """Secret"""
            return x

        with pytest.raises(ValueError, match="not found"):
            toolkit.register_tool_function(secret_tool, group_name="admin")

    def test_register_with_created_group(self):
        """create_tool_group then register; default active=False, schemas hidden."""
        toolkit = Toolkit()
        toolkit.create_tool_group("admin", "Admin tools", active=False)

        async def admin_tool(x: str) -> str:
            """Admin"""
            return x

        toolkit.register_tool_function(admin_tool, group_name="admin")

        assert "admin" in toolkit.groups
        assert toolkit.groups["admin"].active is False

        schemas = toolkit.get_json_schemas()
        assert len(schemas) == 0  # admin group inactive, no schemas exposed


class TestToolkitGroups:
    def test_basic_group_not_in_groups_dict(self):
        """basic is implicit, always active, and not in self.groups."""
        toolkit = Toolkit()
        assert "basic" not in toolkit.groups

    def test_update_tool_groups_activation(self):
        """update_tool_groups activates/deactivates tool groups."""
        toolkit = Toolkit()
        toolkit.create_tool_group("vision", "Vision tools", active=False)

        async def tool_a(x: str) -> str:
            """A"""
            return x

        async def tool_b(y: str) -> str:
            """B"""
            return y

        toolkit.register_tool_function(tool_a, group_name="basic")
        toolkit.register_tool_function(tool_b, group_name="vision")

        assert len(toolkit.get_json_schemas()) == 1  # only basic

        # Activate vision group
        toolkit.update_tool_groups(["vision"], active=True)
        schemas = toolkit.get_json_schemas()
        assert len(schemas) == 2
        names = [s["function"]["name"] for s in schemas]
        assert "tool_a" in names
        assert "tool_b" in names

    def test_basic_group_always_active(self):
        """basic group cannot be deactivated via update_tool_groups."""
        toolkit = Toolkit()

        async def t(x: str) -> str:
            """T"""
            return x

        toolkit.register_tool_function(t, group_name="basic")
        assert len(toolkit.get_json_schemas()) == 1

        toolkit.update_tool_groups(["basic"], active=False)
        assert len(toolkit.get_json_schemas()) == 1  # unchanged


class TestToolkitSchemaFormat:
    def test_schema_openai_compatible(self):
        toolkit = Toolkit()

        async def search(query: str, limit: int = 10) -> str:
            """Search knowledge base."""
            return f"results for {query}"

        toolkit.register_tool_function(search, group_name="basic")

        schemas = toolkit.get_json_schemas()
        s = schemas[0]
        assert s["type"] == "function"
        assert s["function"]["name"] == "search"
        assert "query" in s["function"]["parameters"]["properties"]