Files
cleveragents-core/features/steps/settings_steps.py
T

493 lines
17 KiB
Python

"""Step definitions for settings feature tests."""
import os
from pathlib import Path
from behave import given, then, when
from cleveragents.config.settings import Settings
@given('the environment variable "{key}" is set to "{value}"')
def step_set_env_var(context, key, value):
"""Set an environment variable."""
os.environ[key] = value
if not hasattr(context, "env_vars_to_clean"):
context.env_vars_to_clean = []
context.env_vars_to_clean.append(key)
@given("no environment variables are set")
def step_clear_env_vars(context):
"""Clear all CleverAgents-related environment variables."""
keys_to_remove = [
"CLEVERAGENTS_LOG_LEVEL",
"CLEVERAGENTS_LOG_DIR",
"CLEVERAGENTS_DATA_DIR",
"CLEVERAGENTS_ENV",
"DATABASE_URL",
"OPENAI_API_KEY",
"ANTHROPIC_API_KEY",
"GEMINI_API_KEY",
"DEEPSEEK_API_KEY",
"COHERE_API_KEY",
"PERPLEXITY_API_KEY",
"GROQ_API_KEY",
"TOGETHER_API_KEY",
]
for key in keys_to_remove:
os.environ.pop(key, None)
@when("I load the settings")
def step_load_settings(context):
"""Load settings from environment."""
# Clear the singleton to force reload
Settings._instance = None
context.settings = Settings()
@when("I load the settings with defaults")
def step_load_settings_defaults(context):
"""Load settings with defaults."""
# Clear the singleton to force reload
Settings._instance = None
context.settings = Settings()
@then('the log level should be "{expected}"')
def step_check_log_level(context, expected):
"""Check the log level."""
assert context.settings.log_level == expected
@then('the log directory should be "{expected}"')
def step_check_log_dir(context, expected):
"""Check the log directory."""
assert str(context.settings.log_dir) == expected
@then('the log directory should contain "{substring}"')
def step_check_log_dir_contains(context, substring):
"""Check the log directory contains a substring."""
assert substring in str(context.settings.log_dir)
@then('the data directory should be "{expected}"')
def step_check_data_dir(context, expected):
"""Check the data directory."""
assert str(context.settings.data_dir) == expected
@then('the data directory should contain "{substring}"')
def step_check_data_dir_contains(context, substring):
"""Check the data directory contains a substring."""
assert substring in str(context.settings.data_dir)
@then('the environment should be "{expected}"')
def step_check_environment(context, expected):
"""Check the environment."""
assert context.settings.env == expected
@when('I set the environment alias to "{value}"')
def step_set_environment_alias(context, value):
"""Update the environment alias property."""
assert hasattr(context, "settings"), (
"Settings must be loaded before updating the environment alias."
)
context.settings.environment = value
@then('the environment alias should be "{expected}"')
def step_check_environment_alias(context, expected):
"""Validate the environment alias getter."""
assert context.settings.environment == expected
@then('the database URL should be "{expected}"')
def step_check_database_url(context, expected):
"""Check the database URL."""
assert context.settings.database_url == expected
@then("the database URL should be a SQLite database")
def step_check_sqlite_database(context):
"""Check that the database URL is SQLite."""
assert context.settings.database_url.startswith("sqlite://")
@then("Anthropic should be configured")
def step_check_anthropic_configured(context):
"""Check that Anthropic is configured."""
assert context.settings.anthropic_api_key is not None
@then("no providers should be configured")
def step_check_no_providers(context):
"""Check that no providers are configured."""
assert not context.settings.has_provider_configured()
@given('the data directory is "{directory}"')
def step_set_data_directory(context, directory):
"""Set the data directory for testing."""
context.test_data_dir = directory
@given('the storage base path is "{directory}"')
def step_set_storage_base_path(context, directory):
"""Record the storage base directory used for tests."""
context.storage_base_dir = directory
@when('I compute the storage path for "{storage_type}"')
def step_compute_storage_path(context, storage_type):
"""Compute the storage path from the configured base."""
base_dir = getattr(context, "storage_base_dir", None)
assert base_dir is not None, "Storage base path must be set before computing it."
settings = Settings.model_construct(storage_base_path=Path(base_dir))
context.storage_path = settings.get_storage_base_path(storage_type)
@when("I compute the default storage path")
def step_compute_default_storage_path(context):
"""Compute the storage path when no type is supplied."""
base_dir = getattr(context, "storage_base_dir", None)
assert base_dir is not None, "Storage base path must be set before computing it."
settings = Settings.model_construct(storage_base_path=Path(base_dir))
context.storage_path = settings.get_storage_base_path()
@when('I get the storage base path for "{storage_type}"')
def step_get_storage_path(context, storage_type):
"""Get the storage base path."""
settings = Settings(data_dir=Path(context.test_data_dir))
context.storage_path = settings.get_storage_base_path(storage_type)
@then('the storage path should be "{expected}"')
def step_check_storage_path(context, expected):
"""Check the storage path."""
assert str(context.storage_path) == expected
@given('the vector store path override is "{path_value}"')
def step_set_vector_store_override(context, path_value):
"""Record the vector store path override for later resolution."""
context.vector_store_override = path_value
@when("I resolve the vector store path")
def step_resolve_vector_store_path(context):
"""Resolve the configured vector store path to an absolute location."""
override = getattr(context, "vector_store_override", None)
assert override is not None, "Vector store path override must be provided first."
Settings._instance = None
settings = Settings.model_construct(vector_store_path=Path(override))
context.resolved_vector_store_path = settings.resolve_vector_store_path()
@then(
"the resolved vector store path should equal the override under the current working directory"
)
def step_verify_resolved_vector_store_path(context):
"""Ensure the vector store path is resolved relative to the current working directory."""
override = getattr(context, "vector_store_override", None)
assert override is not None, "Vector store path override must be provided first."
expected = (Path.cwd() / Path(override)).resolve()
assert context.resolved_vector_store_path == expected
@given('the environment is set to "{env}"')
def step_set_environment(context, env):
"""Set the environment."""
os.environ["CLEVERAGENTS_ENV"] = env
if not hasattr(context, "env_vars_to_clean"):
context.env_vars_to_clean = []
context.env_vars_to_clean.append("CLEVERAGENTS_ENV")
@when("I check if running in production")
def step_check_is_production(context):
"""Check if running in production."""
Settings._instance = None
settings = Settings()
context.is_production = settings.is_production()
@when(
'I evaluate production mode with env "{env_value}", debug "{debug_flag}", and reload "{reload_flag}"'
)
def step_evaluate_production_mode_flags(context, env_value, debug_flag, reload_flag):
"""Evaluate production helpers for the supplied flags."""
Settings._instance = None
settings = Settings.model_construct(
env=env_value,
debug_enabled=debug_flag.lower() == "true",
server_reload=reload_flag.lower() == "true",
)
context.is_production = settings.is_production()
context.is_production_mode = settings.is_production_mode
@then("is_production should be {expected}")
def step_verify_is_production(context, expected):
"""Verify the is_production value."""
expected_bool = expected == "True"
assert context.is_production == expected_bool
@then("is_production_mode should be {expected}")
def step_verify_is_production_mode(context, expected):
"""Verify the is_production_mode alias."""
expected_bool = expected == "True"
assert context.is_production_mode == expected_bool
@given('the {provider} API key is set to "{key}"')
def step_set_provider_key(context, provider, key):
"""Set a provider API key."""
env_key = f"{provider.upper().replace(' ', '_')}_API_KEY"
os.environ[env_key] = key
if not hasattr(context, "env_vars_to_clean"):
context.env_vars_to_clean = []
context.env_vars_to_clean.append(env_key)
@when('I instantiate settings with explicit openai API key "{value}"')
def step_instantiate_settings_with_openai(context, value):
"""Instantiate settings with a constructor-specified OpenAI key."""
Settings._instance = None
context.settings = Settings.model_construct(openai_api_key=value)
@when('I check if "{provider}" provider is configured')
def step_check_provider_configured(context, provider):
"""Check if a provider is configured."""
Settings._instance = None
settings = Settings()
context.provider_configured = settings.has_provider_configured(
provider.lower().replace(" ", "")
)
@when('I check if "{provider}" provider is configured using a constructed instance')
def step_check_provider_configured_constructed(context, provider):
"""Check provider configuration using a lightweight constructed settings instance."""
Settings._instance = None
settings = Settings.model_construct()
context.provider_configured = settings.has_provider_configured(
provider.lower().replace(" ", "")
)
@when('I clear the explicit "{provider}" key and recheck configuration')
def step_clear_explicit_key_and_recheck(context, provider):
"""Clear the in-memory provider key and re-evaluate configuration."""
Settings._instance = None
settings = Settings()
attr = provider.lower().replace(" ", "_") + "_api_key"
setattr(settings, attr, None)
context.provider_configured = settings.has_provider_configured(provider)
@then('has_provider_configured should be {expected} for "{provider}"')
def step_verify_provider_configured(context, expected, provider):
"""Verify provider configuration status."""
expected_bool = expected == "True"
assert context.provider_configured == expected_bool
@then("has_provider_configured should be {expected}")
def step_verify_any_provider_configured(context, expected):
"""Verify any provider configuration status."""
expected_bool = expected == "True"
assert context.provider_configured == expected_bool
@given("no API keys are set")
def step_clear_api_keys(context):
"""Clear all API keys."""
api_keys = [
"OPENAI_API_KEY",
"ANTHROPIC_API_KEY",
"GEMINI_API_KEY",
"DEEPSEEK_API_KEY",
"COHERE_API_KEY",
"PERPLEXITY_API_KEY",
"GROQ_API_KEY",
"TOGETHER_API_KEY",
]
for key in api_keys:
os.environ.pop(key, None)
@given('a temporary provider "{provider}" uses env "{env_var}"')
def step_define_temporary_provider(context, provider, env_var):
"""Temporarily register a provider mapping for provider lookup tests."""
normalized = provider.lower().replace(" ", "_")
if not normalized.endswith("_api_key"):
normalized = f"{normalized}_api_key"
original_map = Settings._PROVIDER_ENV_MAP
original_override = Settings._apply_external_env_overrides
Settings._PROVIDER_ENV_MAP = dict(Settings._PROVIDER_ENV_MAP)
Settings._PROVIDER_ENV_MAP[normalized] = (env_var,)
Settings._apply_external_env_overrides = lambda self: None
if not hasattr(context, "_cleanup_handlers"):
context._cleanup_handlers = []
def cleanup():
Settings._PROVIDER_ENV_MAP = original_map
Settings._apply_external_env_overrides = original_override
context._cleanup_handlers.append(cleanup)
@when("I check if any provider is configured")
def step_check_any_provider(context):
"""Check if any provider is configured."""
Settings._instance = None
settings = Settings()
context.provider_configured = settings.has_provider_configured()
@given('the database URL is set to "{url}"')
def step_set_database_url(context, url):
"""Set the database URL."""
os.environ["DATABASE_URL"] = url
if not hasattr(context, "env_vars_to_clean"):
context.env_vars_to_clean = []
context.env_vars_to_clean.append("DATABASE_URL")
@given("no database URL is set")
def step_clear_database_url(context):
"""Clear the database URL."""
os.environ.pop("DATABASE_URL", None)
@when("I get the database URL")
def step_get_database_url(context):
"""Get the database URL."""
Settings._instance = None
if hasattr(context, "test_data_dir"):
settings = Settings(data_dir=Path(context.test_data_dir))
else:
settings = Settings()
context.database_url = settings.get_database_url()
@when('I derive the test database URL from "{url}"')
def step_derive_test_database_url(context, url):
"""Derive the fallback test database URL when none is provided."""
Settings._instance = None
settings = Settings.model_construct(database_url=url, test_database_url=None)
context.derived_database_url = settings.get_database_url(test=True)
@then('the derived test database URL should be "{expected}"')
def step_check_derived_test_database_url(context, expected):
"""Verify the derived test database URL."""
assert context.derived_database_url == expected
@when("I get the settings instance")
def step_get_settings(context):
"""Get the settings instance."""
context.settings1 = Settings.get_settings()
@when("I get the settings instance again")
def step_get_settings_again(context):
"""Get the settings instance again."""
context.settings2 = Settings.get_settings()
@then("both instances should be the same object")
def step_verify_singleton(context):
"""Verify both instances are the same."""
assert context.settings1 is context.settings2
@then('the {provider} API key should be "{expected}"')
def step_check_provider_api_key(context, provider, expected):
"""Check a provider API key value."""
provider_key = provider.lower().replace(" ", "_") + "_api_key"
assert getattr(context.settings, provider_key) == expected
@when("I build the LangSmith config")
def step_build_langsmith_config(context):
"""Build the LangSmith configuration payload."""
Settings._instance = None
settings = Settings()
context.langsmith_config = settings.build_langsmith_config()
@then("the LangSmith config should be absent")
def step_assert_langsmith_config_absent(context):
"""Ensure LangSmith config is not generated when disabled."""
assert context.langsmith_config is None
@given("LangSmith is enabled without credentials")
def step_enable_langsmith_without_credentials(context):
"""Create a settings object with LangSmith enabled but missing key/project."""
Settings._instance = None
context.settings = Settings.model_construct(
langsmith_enabled=True,
langsmith_api_key=None,
langsmith_project=None,
)
@when("I validate the LangSmith configuration")
def step_validate_langsmith_configuration(context):
"""Run validation against the current LangSmith settings."""
assert hasattr(context, "settings"), "Settings must be initialized first."
context.langsmith_validation = context.settings.validate_langsmith_configuration()
@then("LangSmith validation should fail with {error_count:d} errors")
def step_assert_langsmith_validation_errors(context, error_count):
"""Assert the validation result and number of reported errors."""
enabled, errors = context.langsmith_validation
assert enabled is False
assert len(errors) == error_count
@given(
'LangSmith is configured with API key "{api_key}", project "{project}", and endpoint "{endpoint}"'
)
def step_configure_langsmith_with_endpoint(context, api_key, project, endpoint):
"""Construct LangSmith settings with explicit endpoint details."""
Settings._instance = None
context.settings = Settings.model_construct(
langsmith_enabled=True,
langsmith_api_key=api_key,
langsmith_project=project,
langsmith_endpoint=endpoint,
)
@when("I check if LangSmith is enabled")
def step_check_langsmith_enabled(context):
"""Evaluate whether LangSmith is enabled for the current settings."""
assert hasattr(context, "settings"), "Settings must be initialized first."
context.langsmith_enabled_flag = context.settings.is_langsmith_enabled
@then('the "{env_var}" environment variable should equal "{expected}"')
def step_verify_environment_variable(context, env_var, expected):
"""Verify that an environment variable has the expected value."""
assert os.environ.get(env_var) == expected
if not hasattr(context, "env_vars_to_clean"):
context.env_vars_to_clean = []
if env_var not in context.env_vars_to_clean:
context.env_vars_to_clean.append(env_var)