from __future__ import annotations

from v1.agent.tool_dispatcher import (
    BackendExecutionResult,
    InterruptResult,
    ToolDispatcher,
    dispatch_tool_call,
)


class TestToolDispatcher:
    def test_frontend_tool_returns_interrupt(self):
        tool = {
            "name": "ui.navigate_to",
            "execution_target": "frontend",
            "args": {"path": "/home"},
        }
        result = dispatch_tool_call(tool)
        assert isinstance(result, InterruptResult)
        assert result.interrupt_type == "tool_execution"
        assert result.tool_name == "ui.navigate_to"

    def test_backend_tool_executes_directly(self):
        tool = {
            "name": "srv.get_user_info",
            "execution_target": "backend",
            "args": {"user_id": "u1"},
            "requires_approval": False,
        }
        result = dispatch_tool_call(tool)
        assert isinstance(result, BackendExecutionResult)
        assert result.tool_name == "srv.get_user_info"

    def test_backend_tool_with_approval_returns_interrupt(self):
        tool = {
            "name": "srv.transfer_funds",
            "execution_target": "backend",
            "args": {"to": "u2", "amount": 100},
            "requires_approval": True,
        }
        result = dispatch_tool_call(tool)
        assert isinstance(result, InterruptResult)
        assert result.interrupt_type == "approval_required"
        assert result.tool_name == "srv.transfer_funds"

    def test_dispatcher_class_can_dispatch(self):
        dispatcher = ToolDispatcher()
        tool = {
            "name": "ui.show_dialog",
            "execution_target": "frontend",
            "args": {"message": "Hello"},
        }
        result = dispatcher.dispatch(tool)
        assert isinstance(result, InterruptResult)
