forked from cleveragents/cleveragents-core
82 lines
2.7 KiB
Python
82 lines
2.7 KiB
Python
import asyncio
|
|
import unittest.mock
|
|
from unittest.mock import patch
|
|
|
|
from behave import given
|
|
from behave import then
|
|
from behave import when
|
|
|
|
from cleveragents.agents.base import Agent
|
|
from cleveragents.core.application import CleverAgentsApp
|
|
from cleveragents.routing.router import Router
|
|
from cleveragents.templates.renderer import TemplateRenderer
|
|
|
|
|
|
class MockAgent(Agent):
|
|
async def process(self, message, context=None):
|
|
return f"Response to: {message}"
|
|
|
|
def get_capabilities(self):
|
|
return ["mock"]
|
|
|
|
|
|
@given("I have a router for the session")
|
|
def step_impl(context):
|
|
context.template_renderer = TemplateRenderer()
|
|
agent = MockAgent("test_agent", {}, context.template_renderer)
|
|
context.router = Router(
|
|
"test_router", {"test_agent": agent}, context.template_renderer
|
|
)
|
|
|
|
|
|
@when("I start an interactive session via the app")
|
|
def step_impl(context):
|
|
"""
|
|
This test verifies that CleverAgentsApp correctly initializes an InteractiveSession
|
|
with the new multi-router parameters.
|
|
"""
|
|
with patch(
|
|
"cleveragents.core.application.InteractiveSession"
|
|
) as MockSession, patch(
|
|
"cleveragents.core.application.CleverAgentsApp._initialize_agents_and_routers"
|
|
) as mock_init_routers:
|
|
mock_session_instance = MockSession.return_value
|
|
mock_session_instance.run = unittest.mock.AsyncMock()
|
|
|
|
# We test the app's ability to start a session
|
|
app = CleverAgentsApp()
|
|
|
|
# Manually set up the app state as if a config was loaded
|
|
app.routers = {"test_router": context.router}
|
|
app.default_router_name = "test_router"
|
|
|
|
# Run the method that creates and runs the session
|
|
asyncio.run(app.start_interactive_session())
|
|
|
|
# Store the mock for assertions in the 'then' step
|
|
context.MockSession = MockSession
|
|
mock_init_routers.assert_called_once()
|
|
|
|
|
|
@then("the session should be initialized correctly for multi-router support")
|
|
def step_impl(context):
|
|
"""
|
|
Verifies that InteractiveSession was instantiated with the correct
|
|
multi-router arguments and that its run method was called.
|
|
"""
|
|
# Verify InteractiveSession was instantiated correctly
|
|
context.MockSession.assert_called_once()
|
|
call_args, call_kwargs = context.MockSession.call_args
|
|
|
|
# Check that the constructor received the dictionary of routers
|
|
assert "routers" in call_kwargs
|
|
assert "test_router" in call_kwargs["routers"]
|
|
assert call_kwargs["routers"]["test_router"] == context.router
|
|
|
|
# Check that the initial route name was passed
|
|
assert "initial_route_name" in call_kwargs
|
|
assert call_kwargs["initial_route_name"] == "test_router"
|
|
|
|
# Verify the session's run method was called
|
|
context.MockSession.return_value.run.assert_called_once()
|