from beaver.engine import AgentLoop, EngineLoader
from beaver.foundation.events import InboundMessage, MessageBus, OutboundMessage
from beaver.interfaces.channels import ChannelManager, MemoryChannelAdapter
from beaver.interfaces.gateway.main import run_gateway
from beaver.interfaces.web.app import create_app
from beaver.interfaces.web.schemas import WebChatRequest, WebChatResponse


def test_agent_loop_boots(tmp_path) -> None:
    loop = AgentLoop(loader=EngineLoader(workspace=tmp_path))
    loaded = loop.boot()
    assert "echo" in loaded.tools
    assert "memory" in loaded.tools
    assert "session_search" in loaded.tools


def test_web_app_factory() -> None:
    app = create_app()
    assert app.title == "Beaver Backend"


def test_gateway_entry_imports() -> None:
    assert callable(run_gateway)


def test_message_bus_imports() -> None:
    bus = MessageBus()
    assert isinstance(bus, MessageBus)
    assert InboundMessage(channel="test", content="hello").channel == "test"
    assert OutboundMessage(channel="test", content="ok", session_id=None, finish_reason="stop").content == "ok"


def test_channel_imports() -> None:
    bus = MessageBus()
    channel = MemoryChannelAdapter(bus)
    manager = ChannelManager(bus)
    manager.register(channel)
    assert manager.channels["memory"] is channel


def test_web_schema_imports() -> None:
    assert WebChatRequest(message="hello").message == "hello"
    assert WebChatResponse(
        session_id="s",
        run_id="r",
        output_text="ok",
        finish_reason="stop",
        tool_iterations=0,
    ).output_text == "ok"
