"""Tests for registry execution of native tools."""

import pytest
import tempfile
from pathlib import Path
from nanobot.agent.tools.registry import ToolRegistry
from nanobot.agent.tools.anthropic import BashTool20250124, EditTool20250728
from nanobot.agent.tools.anthropic.base import ToolResult, CLIResult


@pytest.mark.asyncio
async def test_registry_executes_bash_tool():
    """Test registry can execute BashTool20250124 and returns ToolResult."""
    registry = ToolRegistry()
    registry.register(BashTool20250124())

    result = await registry.execute("bash", {"command": "echo 'test'"})

    assert isinstance(result, ToolResult)
    assert result.output is not None
    assert "test" in result.output
    assert result.error is None


@pytest.mark.asyncio
async def test_registry_executes_edit_tool():
    """Test registry can execute EditTool20250728 and returns CLIResult."""
    registry = ToolRegistry()
    registry.register(EditTool20250728())

    with tempfile.TemporaryDirectory() as tmpdir:
        test_file = str(Path(tmpdir) / "test.txt")

        result = await registry.execute("str_replace_based_edit_tool", {
            "command": "create",
            "path": test_file,
            "file_text": "Hello, world!"
        })

        assert isinstance(result, CLIResult)
        assert "created" in result.output.lower() or "success" in result.output.lower()
        assert Path(test_file).exists()
        assert Path(test_file).read_text() == "Hello, world!"


@pytest.mark.asyncio
async def test_registry_mixed_tools():
    """Test registry can execute both native and function tools in same registry."""
    registry = ToolRegistry()

    # Register native tool
    registry.register(BashTool20250124())

    # Execute native tool
    result = await registry.execute("bash", {"command": "echo 'native'"})
    assert isinstance(result, ToolResult)
    assert "native" in result.output
