Files
temp/tests/features/steps/configuration_steps.py

153 lines
4.6 KiB
Python

import os
import tempfile
from pathlib import Path
import yaml
from behave import given
from behave import then
from behave import when
from cleveragents.core.config import ConfigurationError
from cleveragents.core.config import ConfigurationManager
@given("a valid configuration file")
def step_impl(context):
# Use the existing test fixture
context.config_file = Path("tests/fixtures/test_config.yaml")
assert (
context.config_file.exists()
), f"Test config file not found at {context.config_file}"
# Create a configuration manager
context.config_manager = ConfigurationManager()
@when("I load the configuration")
def step_impl(context):
context.config_manager.load_files([context.config_file])
@then("the configuration should be properly parsed")
def step_impl(context):
config = context.config_manager.to_dict()
assert "agents" in config
assert "test_agent" in config["agents"]
assert config["agents"]["test_agent"]["type"] == "llm"
assert "routes" in config
assert "main_router" in config["routes"]
@then("no validation errors should occur")
def step_impl(context):
try:
context.config_manager.validate()
context.validation_successful = True
except Exception as e:
context.validation_successful = False
context.validation_error = str(e)
assert (
context.validation_successful
), f"Validation failed with error: {getattr(context, 'validation_error', 'Unknown error')}"
@given("multiple configuration files")
def step_impl(context):
# Create a temporary directory for our test config files
context.temp_dir = tempfile.TemporaryDirectory()
# First config file
context.config_file1 = Path(context.temp_dir.name) / "config1.yaml"
config_data1 = {
"cleveragents": {"default_router": "agent1_route"},
"agents": {"agent1": {"type": "llm", "config": {"model": "base-model"}}},
"routes": {"agent1_route": [{"source": "input", "destination": "agent1"}]},
"common_setting": "original",
}
# Second config file (with overrides)
context.config_file2 = Path(context.temp_dir.name) / "config2.yaml"
config_data2 = {
"agents": {
"agent2": {"type": "chain", "config": {"steps": ["step1", "step2"]}}
},
"routes": {"agent2_route": [{"source": "input", "destination": "agent2"}]},
"common_setting": "overridden",
}
with open(context.config_file1, "w") as f:
yaml.dump(config_data1, f)
with open(context.config_file2, "w") as f:
yaml.dump(config_data2, f)
context.config_manager = ConfigurationManager()
@when("I load all configuration files")
def step_impl(context):
context.config_manager.load_files([context.config_file1, context.config_file2])
@then("the configurations should be merged correctly")
def step_impl(context):
config = context.config_manager.to_dict()
# Check that both agents exist in the merged config
assert "agents" in config
assert "agent1" in config["agents"]
assert "agent2" in config["agents"]
# Check specific values from each config
assert config["agents"]["agent1"]["type"] == "llm"
assert config["agents"]["agent2"]["type"] == "chain"
@then("later files should override earlier ones")
def step_impl(context):
config = context.config_manager.to_dict()
# Check that the common setting was overridden
assert config["common_setting"] == "overridden"
@given("a configuration with missing required fields")
def step_impl(context):
# Create a temporary file with invalid configuration
context.temp_dir = tempfile.TemporaryDirectory()
context.invalid_config_file = Path(context.temp_dir.name) / "invalid_config.yaml"
# Invalid configuration: 'routes' is present, but 'cleveragents.default_router' is missing.
config_data = {
"agents": {
"test_agent": {
"type": "llm",
"config": {"provider": "openai"},
}
},
"routes": {"main": [{"source": "input", "destination": "test_agent"}]},
}
with open(context.invalid_config_file, "w") as f:
yaml.dump(config_data, f)
context.config_manager = ConfigurationManager()
context.config_manager.load_files([context.invalid_config_file])
@when("I attempt to validate the configuration")
def step_impl(context):
try:
context.config_manager.validate()
context.validation_error = None
except Exception as e:
context.validation_error = e
@then("a configuration error should be raised")
def step_impl(context):
assert (
context.validation_error is not None
), "Expected a configuration error but none was raised"