Files
cleveragents-core/features/steps/coverage_boost_steps.py
freemo 0d4a47f3c0 fix(config): align Settings and alembic/env.py database_url defaults to spec-required ~/.cleveragents/cleveragents.db
- Fix Settings.database_url default from relative 'sqlite:///cleveragents.db'
  to absolute 'sqlite:////home/<user>/.cleveragents/cleveragents.db' using
  Path.home() / '.cleveragents' / 'cleveragents.db'
- Fix test_database_url default similarly to use ~/.cleveragents/cleveragents_test.db
- Fix alembic/env.py fallback default from Path.cwd() / '.cleveragents' / 'db.sqlite'
  to Path.home() / '.cleveragents' / 'cleveragents.db' (matching Settings)
- Both Settings and alembic/env.py now resolve to the same absolute path
- CLEVERAGENTS_DATABASE_URL env var override still works correctly in both
- Update coverage_boost_steps.py assertions to match new correct defaults
- Add 3 new Behave scenarios covering correct database_url default and env var override

ISSUES CLOSED: #2871
2026-04-05 10:17:24 +00:00

153 lines
5.0 KiB
Python

"""Step definitions for coverage boost tests."""
import os
from pathlib import Path
from behave import then, when
from cleveragents.config.settings import Settings
from cleveragents.platform import ensure_cli_importable
@when("I create a Settings instance")
def step_create_settings(context):
"""Create a Settings instance."""
# Clear any existing singleton
if hasattr(Settings, "_instance"):
Settings._instance = None
# Remove database URL env vars so we test the actual pydantic defaults,
# not the per-scenario temp paths injected by environment.py.
for key in ("CLEVERAGENTS_DATABASE_URL", "CLEVERAGENTS_TEST_DATABASE_URL"):
os.environ.pop(key, None)
context.settings = Settings()
@then("it should have default values")
def step_check_default_values(context):
"""Check Settings has default values."""
assert context.settings.server_host == "0.0.0.0"
assert context.settings.server_port == 8080
expected_db_url = f"sqlite:///{Path.home() / '.cleveragents' / 'cleveragents.db'}"
assert context.settings.database_url == expected_db_url
assert context.settings.debug_log_level == "INFO"
assert context.settings.storage_base_path.name == "data"
@when("I check if Settings is in production mode")
def step_check_production_mode(context):
"""Check production mode."""
settings = Settings()
context.is_production = settings.is_production
@then("it should return the correct production status")
def step_verify_production_status(context):
"""Verify production status."""
# Default should be production (debug_enabled=False, server_reload=False)
assert context.is_production
@when("I get the database URL from Settings")
def step_get_database_url(context):
"""Get database URL."""
# Clear singleton and database URL env vars so we test the actual
# pydantic defaults, not per-scenario temp paths from environment.py.
if hasattr(Settings, "_instance"):
Settings._instance = None
for key in ("CLEVERAGENTS_DATABASE_URL", "CLEVERAGENTS_TEST_DATABASE_URL"):
os.environ.pop(key, None)
settings = Settings()
context.db_url = settings.get_database_url()
context.test_db_url = settings.get_database_url(test=True)
@then("it should return the configured database URL")
def step_check_database_url(context):
"""Check database URL."""
expected_db_url = f"sqlite:///{Path.home() / '.cleveragents' / 'cleveragents.db'}"
expected_test_db_url = (
f"sqlite:///{Path.home() / '.cleveragents' / 'cleveragents_test.db'}"
)
assert context.db_url == expected_db_url
assert context.test_db_url == expected_test_db_url
@when("I check if any provider is configured in Settings")
def step_check_provider_configured(context):
"""Check if provider is configured."""
import shutil
import tempfile
from pathlib import Path
# Clear any existing settings
from cleveragents.config import settings as settings_module
from cleveragents.config.settings import Settings
settings_module._settings = None
# Store current env vars
stored_vars = {}
for key in [
"OPENAI_API_KEY",
"ANTHROPIC_API_KEY",
"GOOGLE_API_KEY",
"AZURE_API_KEY",
"OPENROUTER_API_KEY",
"GEMINI_API_KEY",
"HF_TOKEN",
]:
stored_vars[key] = os.environ.get(key)
os.environ.pop(key, None)
# Move .env file temporarily if it exists
env_file = Path(".env")
temp_env = None
if env_file.exists():
temp_env = Path(tempfile.mktemp(suffix=".env"))
shutil.move(str(env_file), str(temp_env))
try:
# Test with no providers
settings1 = Settings()
context.no_provider = settings1.has_provider_configured()
# Test with a provider
os.environ["OPENAI_API_KEY"] = "test-key"
# Force new settings instance
settings_module._settings = None
settings2 = Settings()
context.with_provider = settings2.has_provider_configured()
finally:
# Restore .env file
if temp_env and temp_env.exists():
shutil.move(str(temp_env), str(env_file))
# Clean up and restore env vars
os.environ.pop("OPENAI_API_KEY", None)
for key, val in stored_vars.items():
if val:
os.environ[key] = val
settings_module._settings = None
@then("it should return the provider status")
def step_verify_provider_status(context):
"""Verify provider status."""
assert not context.no_provider
assert context.with_provider
@when("I import and use the platform module")
def step_use_platform_module(context):
"""Use platform module."""
context.cli_module = ensure_cli_importable()
@then("ensure_cli_importable should work")
def step_check_cli_importable(context):
"""Check ensure_cli_importable worked."""
assert context.cli_module is not None
assert context.cli_module.__name__ == "cleveragents.cli"
assert hasattr(context.cli_module, "main")
assert hasattr(context.cli_module, "app")