Files
cleveragents-core/features/steps/coverage_extras_steps.py
T
freemo 4ad51561fc perf(tests): optimize medium-slow BDD features (10-100s tier)
Cap time.sleep and asyncio.sleep globally at 10ms in before_all to
eliminate retry/backoff waits (tenacity wait_fixed, retry_auto_debug
exponential backoff). Save originals as time._original_sleep and
asyncio._original_sleep for tests that need real wall-clock delays
(CircuitBreaker recovery, debounce timers, validation timeouts).

Enhance template-DB patch: add "db." prefix to _SCENARIO_DB_PREFIXES
and check db_path.stat().st_size > 0 so 0-byte auto-created SQLite
files receive the template copy instead of falling through to real
migrations.

Replace subprocess.run CLI invocations with typer.testing.CliRunner
in module_coverage, main_coverage_complete, and coverage_extras step
files, eliminating ~6s Python cold-start overhead per call.

Switch plan_persistence and action_persistence to in-memory SQLite by
default; only cross-restart scenarios use file-based via an explicit
Given step.

Replace MigrationRunner.run_migrations with Base.metadata.create_all
in plan_service_steps for freshly created in-memory engines.

Removed leftover debug comment in environment.py.

Total tier runtime: 565s -> 21s (96% reduction), 20 features all
under 5s behave-internal time.

ISSUES CLOSED: #480
2026-03-02 02:01:27 +00:00

159 lines
5.0 KiB
Python

"""Additional step definitions to increase test coverage."""
from unittest.mock import patch
from behave import then, when
from cleveragents.cli.main import main
from cleveragents.core.exceptions import CleverAgentsError, RateLimitError
@when("I test CleverAgentsError with details")
def step_test_cleveragents_error_details(context):
"""Test CleverAgentsError with details."""
with patch("cleveragents.cli.main.app") as mock_app:
error = CleverAgentsError(
"Test error", details={"key1": "value1", "key2": "value2"}
)
mock_app.side_effect = error
context.exit_code = main(["info"])
@then("it should handle the error with details")
def step_check_error_details_handled(context):
"""Check error details were handled."""
assert context.exit_code == 1
@when("I call main with args that trigger line 166")
def step_main_with_program_name(context):
"""Test main with program name in args."""
# This tests line 166 where we strip 'cleveragents' from args
context.exit_code = main(["cleveragents", "--version"])
@then("it should strip the program name")
def step_check_program_stripped(context):
"""Check program name was stripped."""
assert context.exit_code == 0
@when("I test platform import error recovery")
def step_test_platform_import_error(context):
"""Test platform module import error handling."""
from cleveragents import platform
# First, test the success case
with patch("sys.modules", {"cleveragents.cli": None}):
try:
# This will trigger the import error path
platform.ensure_cli_importable()
context.import_succeeded = False
except ImportError:
context.import_succeeded = True
@then("platform should handle import errors gracefully")
def step_check_platform_import_handled(context):
"""Check platform import error was handled."""
# Since the module is already imported, we can't truly test the error path
# But we can verify the function works
from cleveragents import platform
module = platform.ensure_cli_importable()
assert module is not None
@when("I run the module directly as __main__")
def step_run_as_main(context):
"""Test running the module as __main__ via in-process CliRunner."""
from typer.testing import CliRunner
from cleveragents.cli.main import app as main_app
runner = CliRunner()
result = runner.invoke(main_app, ["--version"])
context.main_exit_code = result.exit_code
context.main_output = result.stdout or ""
@then("the __main__ module should execute correctly")
def step_check_main_executed(context):
"""Check __main__ executed correctly."""
assert context.main_exit_code == 0
assert "1.0.0" in context.main_output
@when("I test all empty module __init__ files")
def step_test_empty_inits(context):
"""Test importing all empty __init__ modules for coverage."""
modules_to_test = [
"cleveragents.application",
"cleveragents.application.services",
"cleveragents.application.workflows",
"cleveragents.cli.commands",
"cleveragents.domain",
"cleveragents.domain.contexts",
"cleveragents.domain.models",
"cleveragents.domain.plans",
"cleveragents.domain.repositories",
"cleveragents.infrastructure",
"cleveragents.providers",
"cleveragents.runtime",
"cleveragents.shared",
]
context.import_errors = []
for module_name in modules_to_test:
try:
__import__(module_name)
except Exception as e:
context.import_errors.append((module_name, str(e)))
@then("all modules should import successfully")
def step_check_modules_imported(context):
"""Check all modules imported."""
assert len(context.import_errors) == 0, f"Import errors: {context.import_errors}"
@when("I test RateLimitError exception")
def step_test_rate_limit_error(context):
"""Test RateLimitError creation and attributes."""
# Test with retry_after
error1 = RateLimitError("Rate limit hit", retry_after=60)
assert error1.retry_after == 60
assert str(error1) == "Rate limit hit"
# Test without retry_after
error2 = RateLimitError("Another rate limit")
assert error2.retry_after is None
context.rate_limit_tested = True
@then("RateLimitError should work correctly")
def step_check_rate_limit(context):
"""Check RateLimitError was tested."""
assert context.rate_limit_tested
@when("I test the __main__ module if clause")
def step_test_main_if_clause(context):
"""Test __main__ module's if __name__ == '__main__' clause in-process."""
from typer.testing import CliRunner
from cleveragents.cli.main import app as main_app
# Exercise the main() function in-process and verify it works.
runner = CliRunner()
result = runner.invoke(main_app, ["--version"])
context.direct_run_exit = result.exit_code
@then("the if __name__ clause should execute")
def step_check_if_name_clause(context):
"""Check if __name__ clause executed."""
assert context.direct_run_exit == 0