"""
测试 tool_adapter.py —— ToolSchema → OpenAI/Anthropic 原生格式转换
"""
import json
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent.parent))

from aacode.utils.tool_adapter import to_openai_tools, to_anthropic_tools
from aacode.utils.tool_registry import ToolSchema, ToolParameter


class TestToolAdapter:
    """OpenAI/Anthropic 格式转换"""

    def test_openai_basic(self):
        schema = ToolSchema(
            name="test_tool",
            description="A test tool",
            parameters=[
                ToolParameter(name="arg1", type=str, required=True, description="arg1 desc"),
                ToolParameter(name="arg2", type=int, required=False, default=10, description="arg2 desc"),
            ],
        )
        tools = to_openai_tools({"test_tool": schema})
        assert len(tools) == 1
        func = tools[0]["function"]
        assert func["name"] == "test_tool"
        assert func["description"] == "A test tool"
        assert func["parameters"]["type"] == "object"
        assert "arg1" in func["parameters"]["required"]
        assert "arg2" not in func["parameters"]["required"]
        assert func["parameters"]["properties"]["arg1"]["type"] == "string"
        assert func["parameters"]["properties"]["arg2"]["type"] == "integer"
        assert func["parameters"]["properties"]["arg2"]["default"] == 10

    def test_openai_multiple_types(self):
        schema = ToolSchema(
            name="multi_type",
            description="test",
            parameters=[
                ToolParameter(name="a", type=str, required=True, description=""),
                ToolParameter(name="b", type=int, required=False, description=""),
                ToolParameter(name="c", type=float, required=False, description=""),
                ToolParameter(name="d", type=bool, required=False, description=""),
                ToolParameter(name="e", type=list, required=False, description=""),
                ToolParameter(name="f", type=dict, required=False, description=""),
            ],
        )
        tools = to_openai_tools({"multi_type": schema})
        props = tools[0]["function"]["parameters"]["properties"]
        assert props["a"]["type"] == "string"
        assert props["b"]["type"] == "integer"
        assert props["c"]["type"] == "number"
        assert props["d"]["type"] == "boolean"
        assert props["e"]["type"] == "array"
        assert props["f"]["type"] == "object"
        assert len(tools[0]["function"]["parameters"]["required"]) == 1

    def test_anthropic_basic(self):
        schema = ToolSchema(
            name="test_tool",
            description="A test tool",
            parameters=[
                ToolParameter(name="arg1", type=str, required=True, description="arg1 desc"),
            ],
        )
        tools = to_anthropic_tools({"test_tool": schema})
        assert len(tools) == 1
        assert tools[0]["name"] == "test_tool"
        assert tools[0]["description"] == "A test tool"
        assert tools[0]["input_schema"]["type"] == "object"
        assert "arg1" in tools[0]["input_schema"]["properties"]
        assert "arg1" in tools[0]["input_schema"]["required"]

    def test_multiple_tools(self):
        schemas = {
            "tool_a": ToolSchema(name="tool_a", description="A", parameters=[]),
            "tool_b": ToolSchema(name="tool_b", description="B", parameters=[]),
        }
        openai = to_openai_tools(schemas)
        assert len(openai) == 2
        names = [t["function"]["name"] for t in openai]
        assert "tool_a" in names
        assert "tool_b" in names

    def test_empty_schemas(self):
        tools = to_openai_tools({})
        assert tools == []
        tools = to_anthropic_tools({})
        assert tools == []
