Files
temp/tests/features/steps/error_verbosity_steps.py
T

142 lines
4.8 KiB
Python

"""Step definitions for error verbosity feature tests."""
import tempfile
from pathlib import Path
from unittest.mock import patch
from behave import given, then, when
from cleveragents.core.application import ReactiveCleverAgentsApp
@given("a test configuration file with a tool agent")
def step_create_test_config(context):
"""Create a temporary config file for testing."""
context.temp_dir = Path(tempfile.mkdtemp())
context.config_file = context.temp_dir / "test_config.yaml"
context.config_file.write_text(
"""
agents:
test_agent:
type: tool
config:
tools:
- name: test_tool
code: |
result = "test result"
routes:
main:
type: stream
stream_type: cold
operators:
- type: map
params:
agent: test_agent
publications:
- __output__
merges:
- sources: [__input__]
target: main
"""
)
@given("the application is initialized with verbose set to {verbose_value}")
def step_initialize_app_with_verbose(context, verbose_value):
"""Initialize the ReactiveCleverAgentsApp with specified verbose flag."""
verbose_bool = verbose_value == "True"
context.app = ReactiveCleverAgentsApp([context.config_file], verbose=verbose_bool, unsafe=True)
@when("a tool execution fails with a detailed error")
def step_tool_execution_fails(context):
"""Execute a tool that fails with a detailed error message."""
error_msg = "Detailed error: name 'details' is not defined"
with patch.object(
context.app.agents["test_agent"],
"process_message",
side_effect=Exception(error_msg),
):
context.result = context.app._execute_single_tool("test_tool", {})
context.error_msg = error_msg
@when("executing a nonexistent tool")
def step_execute_nonexistent_tool(context):
"""Execute a tool that doesn't exist."""
context.tool_name = "nonexistent_tool"
context.result = context.app._execute_single_tool(context.tool_name, {})
@then("the error message should not contain specific error details")
def step_error_should_not_contain_details(context):
"""Verify error message doesn't contain specific details."""
assert "name 'details' is not defined" not in context.result
assert "Detailed error:" not in context.result
@then("the error message should contain the specific error details")
def step_error_should_contain_details(context):
"""Verify error message contains specific details."""
assert "name 'details' is not defined" in context.result or context.error_msg in context.result
@then('the error message should contain "{text}"')
def step_error_should_contain_text(context, text):
"""Verify error message contains specific text."""
assert text in context.result, f"Expected '{text}' in result: {context.result}"
@then("the error message should suggest using verbose flag")
def step_error_should_suggest_verbose(context):
"""Verify error message suggests using the verbose flag."""
assert (
"Use -v flag for details" in context.result or "check logs" in context.result
), f"Expected verbose flag suggestion in result: {context.result}"
@then("the error message should not contain the tool name")
def step_error_should_not_contain_tool_name(context):
"""Verify error message doesn't contain the tool name."""
assert context.tool_name not in context.result
@then("the error message should contain the tool name")
def step_error_should_contain_tool_name(context):
"""Verify error message contains the tool name."""
assert context.tool_name in context.result
@then("the application verbose flag should be {expected_value}")
def step_verify_verbose_flag(context, expected_value):
"""Verify the verbose flag is set correctly."""
expected_bool = expected_value == "True"
assert context.app.verbose == expected_bool
@then("the error verbosity test should verify logging")
def step_error_verbosity_test_logging(context):
"""Verify the error was logged for error verbosity test."""
# Mock the logger and verify it was called
error_msg = "Test error for logging"
with patch.object(context.app.logger, "error") as mock_logger:
with patch.object(
context.app.agents["test_agent"],
"process_message",
side_effect=Exception(error_msg),
):
context.app._execute_single_tool("test_tool", {})
# Verify error was logged
mock_logger.assert_called_once()
context.log_call_args = str(mock_logger.call_args)
@then('the error verbosity log should contain "{text}"')
def step_log_should_contain_for_verbosity(context, text):
"""Verify the log contains specific text for error verbosity test."""
assert text in context.log_call_args, f"Expected '{text}' in log: {context.log_call_args}"