Files
cleveragents-core/tests/features/steps/interactive_commands_steps.py
T

247 lines
8.1 KiB
Python

import ast
import shlex
from unittest.mock import MagicMock
from unittest.mock import Mock
from behave import given
from behave import then
from behave import when
from cleveragents.interactive import commands
from cleveragents.interactive.commands import parse_command
# --- Parsing Steps ---
@given("I have a command parser")
def step_impl(context):
pass
@when('I parse the command "{command_str}"')
def step_impl(context, command_str):
context.parsed_command = parse_command(command_str)
@then('the command should be identified as "{expected_command}"')
def step_impl(context, expected_command):
assert (
context.parsed_command["command"] == expected_command
), f"Expected command '{expected_command}', got '{context.parsed_command['command']}'"
@then("the args should be empty")
def step_impl(context):
assert (
len(context.parsed_command["args"]) == 0
), f"Expected empty args, got {context.parsed_command['args']}"
@then('the args should contain "{arg1}" and "{arg2}"')
def step_impl(context, arg1, arg2):
args = context.parsed_command["args"]
assert arg1 in args, f"Expected '{arg1}' in args, got {args}"
assert arg2 in args, f"Expected '{arg2}' in args, got {args}"
@then("the arguments should be {expected_args}")
def step_impl(context, expected_args):
expected_list = ast.literal_eval(expected_args)
assert context.parsed_command["args"] == expected_list, (
f"Expected {expected_list}, " f"got {context.parsed_command['args']}"
)
# --- Executor Setup Steps ---
@given("I have a command executor")
def step_impl(context):
context.executor = commands.execute_command
context.context = {"session": None}
@given("I have a command executor without a session")
def step_impl(context):
context.executor = commands.execute_command
context.context = {"session": None}
@given("I have a command executor with a mock session")
def step_impl(context):
context.mock_session = Mock()
context.mock_session.routers = {"main": Mock(), "secondary": Mock()}
context.mock_session.active_route_name = "main"
context.context = {"session": context.mock_session}
context.executor = commands.execute_command
@given("I have a command executor with a complete mock session")
def step_impl(context):
context.executor = commands.execute_command
context.mock_session = MagicMock()
context.context = {"session": context.mock_session}
@given(
'the session has routes "{route1}", "{route2}" and active route "{active_route}"'
)
def step_impl(context, route1, route2, active_route):
context.mock_session.routers = {route1: Mock(), route2: Mock()}
context.mock_session.active_route_name = active_route
# --- Execution Steps ---
@when('I execute the command "{command}" with arguments "{arguments}"')
def step_impl(context, command, arguments):
args = shlex.split(arguments)
command_dict = {"command": command, "args": args}
context.result = context.executor(command_dict, context.context)
@when('I execute the command "{command}" with no arguments')
def step_impl(context, command):
command_dict = {"command": command, "args": []}
context.result = context.executor(command_dict, context.context)
@when('I execute the "{command}" command')
def step_impl(context, command):
command_dict = {"command": command, "args": []}
context.result = context.executor(command_dict, context.context)
@when('I execute the "{command}" command with args "{args_str}"')
def step_impl(context, command, args_str):
args = shlex.split(args_str)
command_dict = {"command": command, "args": args}
context.result = context.executor(command_dict, context.context)
@when('I execute the "{command}" command without args')
def step_impl(context, command):
command_dict = {"command": command, "args": []}
context.result = context.executor(command_dict, context.context)
@when('I execute the route command with arg "{route_name}"')
def step_impl(context, route_name):
command_dict = {"command": "route", "args": [route_name]}
context.result = context.executor(command_dict, context.context)
@when("I execute the route command without args")
def step_impl(context):
command_dict = {"command": "route", "args": []}
context.result = context.executor(command_dict, context.context)
# --- Assertion Steps ---
@then("the result should be successful")
def step_impl(context):
assert (
context.result["success"] is True
), f"Command was not successful: {context.result['message']}"
@then("the result should be unsuccessful")
def step_impl(context):
assert context.result["success"] is False, "Command was unexpectedly successful"
@then('the message should be "{expected_message}"')
def step_impl(context, expected_message):
assert context.result["message"] == expected_message, (
f"Expected '{expected_message}', " f"got '{context.result['message']}'"
)
@then('the result message should be "{expected_message}"')
def step_impl(context, expected_message):
assert context.result["message"] == expected_message
@then("I should receive help text containing available commands")
def step_impl(context):
assert (
"Available Commands:" in context.result["message"]
), "Help text doesn't contain 'Available Commands:'"
assert (
"/help" in context.result["message"]
), "Help text doesn't mention the help command"
assert (
"/exit" in context.result["message"]
), "Help text doesn't mention the exit command"
@then("I should receive a message about exiting")
def step_impl(context):
assert (
"Exiting" in context.result["message"]
), f"Expected message about exiting, got '{context.result['message']}'"
@then("the continue flag should be false")
def step_impl(context):
assert context.result["continue"] is False, "Expected continue flag to be False"
@then("I should receive a message about showing configuration")
def step_impl(context):
assert (
"Showing configuration" in context.result["message"]
), f"Expected message about showing configuration, got '{context.result['message']}'"
@then("I should receive a message about setting configuration")
def step_impl(context):
assert (
"Setting configuration" in context.result["message"]
), f"Expected message about setting configuration, got '{context.result['message']}'"
@then("I should receive a message about listing agents")
def step_impl(context):
assert (
"Listing available agents" in context.result["message"]
), f"Expected message about listing agents, got '{context.result['message']}'"
@then("I should receive a message about showing agent information")
def step_impl(context):
assert (
"Showing information for agent" in context.result["message"]
), f"Expected message about showing agent information, got '{context.result['message']}'"
@then("I should receive a message about switching agents")
def step_impl(context):
assert (
"Switching to agent" in context.result["message"]
), f"Expected message about switching agents, got '{context.result['message']}'"
@then('the active route should be switched to "{route_name}"')
def step_impl(context, route_name):
assert "Switched to route" in context.result["message"]
assert context.mock_session.active_route_name == route_name
@then("I should receive info about the current and available routes")
def step_impl(context):
assert "Current route: 'main'" in context.result["message"]
assert "Available: main, secondary" in context.result["message"]
@then("I should receive an error message about usage")
def step_impl(context):
assert not context.result["success"], "Expected command to fail but it succeeded"
assert (
"Usage:" in context.result["message"]
), f"Expected usage information, got '{context.result['message']}'"
@then("I should receive an error message about unknown command")
def step_impl(context):
assert not context.result["success"], "Expected command to fail but it succeeded"
assert (
"Unknown command" in context.result["message"]
), f"Expected unknown command message, got '{context.result['message']}'"