forked from HAL9000/cleveragents-core
144 lines
4.6 KiB
Python
144 lines
4.6 KiB
Python
import asyncio
|
|
import tempfile
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
import yaml
|
|
from behave import given
|
|
from behave import then
|
|
from behave import when
|
|
|
|
from cleveragents.core.application import CleverAgentsApp
|
|
from cleveragents.core.exceptions import CleverAgentsException
|
|
|
|
|
|
@given("I create a CleverAgentsApp without configuration")
|
|
def step_impl(context):
|
|
context.app = CleverAgentsApp()
|
|
|
|
|
|
@then("the application should be initialized with default values")
|
|
def step_impl(context):
|
|
assert context.app.config_manager is not None
|
|
assert not context.app.routers
|
|
|
|
|
|
@given("I create a CleverAgentsApp with test configuration")
|
|
def step_impl(context):
|
|
context.temp_dir = tempfile.TemporaryDirectory()
|
|
config_path = Path(context.temp_dir.name) / "test_config.yaml"
|
|
config = {
|
|
"cleveragents": {"default_router": "main_flow"},
|
|
"agents": {
|
|
"test_agent": {
|
|
"type": "llm",
|
|
"config": {
|
|
"provider": "openai",
|
|
"model": "test-model",
|
|
"api_key": "mock-api-key",
|
|
},
|
|
}
|
|
},
|
|
"routes": {
|
|
"main_flow": [
|
|
{"source": "input", "destination": "test_agent"},
|
|
{"source": "test_agent", "destination": "output"},
|
|
]
|
|
},
|
|
}
|
|
with open(config_path, "w") as f:
|
|
yaml.dump(config, f)
|
|
context.config_files = [config_path]
|
|
context.app = CleverAgentsApp(context.config_files)
|
|
|
|
|
|
@when('I run the application in single-shot mode with prompt "{prompt}"')
|
|
def step_impl(context, prompt):
|
|
async def mock_run_single_shot(prompt, route_name=None):
|
|
return f"Response to: {prompt}"
|
|
|
|
with patch.object(context.app, "run_single_shot", mock_run_single_shot):
|
|
context.response = asyncio.run(context.app.run_single_shot(prompt))
|
|
|
|
|
|
@then("I should receive a response from the application")
|
|
def step_impl(context):
|
|
assert context.response is not None
|
|
assert "Response to:" in context.response
|
|
|
|
|
|
@when("I start an interactive session with the application")
|
|
def step_impl(context):
|
|
async def mock_start_session(history_file=None, route_name=None):
|
|
context.session_started = True
|
|
return None
|
|
|
|
with patch.object(context.app, "start_interactive_session", mock_start_session):
|
|
asyncio.run(context.app.start_interactive_session())
|
|
|
|
|
|
@then("the interactive session should be started")
|
|
def step_impl(context):
|
|
assert hasattr(context, "session_started") and context.session_started is True
|
|
|
|
|
|
@when("I try to run the application without loading configuration")
|
|
def step_impl(context):
|
|
# Create an app without config files
|
|
app = CleverAgentsApp()
|
|
try:
|
|
asyncio.run(app.run_single_shot("Hello"))
|
|
context.exception = None
|
|
except Exception as e:
|
|
context.exception = e
|
|
|
|
|
|
@then("an appropriate application error should be raised")
|
|
def step_impl(context):
|
|
assert context.exception is not None
|
|
assert isinstance(context.exception, CleverAgentsException)
|
|
# The lazy initializer will raise an error about the agent factory
|
|
assert "agent type" in str(context.exception).lower()
|
|
|
|
|
|
@when("I try to load invalid configuration")
|
|
def step_impl(context):
|
|
context.temp_dir = tempfile.TemporaryDirectory()
|
|
config_path = Path(context.temp_dir.name) / "invalid_config.yaml"
|
|
# Invalid config: missing 'agents' section
|
|
config = {"routes": {"main": []}}
|
|
with open(config_path, "w") as f:
|
|
yaml.dump(config, f)
|
|
try:
|
|
CleverAgentsApp([config_path])
|
|
context.error = None
|
|
except Exception as e:
|
|
context.error = e
|
|
|
|
|
|
@then("a configuration error should be raised for the application")
|
|
def step_impl(context):
|
|
assert context.error is not None
|
|
assert "configuration" in str(context.error).lower()
|
|
|
|
|
|
@when("I load configuration from test files")
|
|
def step_impl(context):
|
|
context.temp_dir = tempfile.TemporaryDirectory()
|
|
config_path = Path(context.temp_dir.name) / "test_config.yaml"
|
|
config = {
|
|
"cleveragents": {"default_router": "main_flow"},
|
|
"agents": {"test_agent": {"type": "llm", "config": {}}},
|
|
"routes": {"main_flow": [{"source": "input", "destination": "test_agent"}]},
|
|
}
|
|
with open(config_path, "w") as f:
|
|
yaml.dump(config, f)
|
|
context.config_files = [config_path]
|
|
context.app.load_configuration(context.config_files)
|
|
|
|
|
|
@then("the configuration should be loaded successfully")
|
|
def step_impl(context):
|
|
assert context.app.agent_factory is not None
|
|
assert context.app.config_manager.get("agents.test_agent") is not None
|