Files
cleveragents-core/features/steps/acms_context_cli_steps.py
T
HAL9000 620ed7d11c
CI / push-validation (pull_request) Successful in 24s
CI / lint (pull_request) Failing after 25s
CI / helm (pull_request) Successful in 34s
CI / quality (pull_request) Successful in 38s
CI / typecheck (pull_request) Failing after 48s
CI / unit_tests (pull_request) Failing after 51s
CI / security (pull_request) Failing after 54s
CI / coverage (pull_request) Has been skipped
CI / docker (pull_request) Has been skipped
CI / build (pull_request) Successful in 3m13s
CI / e2e_tests (pull_request) Successful in 6m2s
CI / integration_tests (pull_request) Successful in 7m33s
CI / status-check (pull_request) Failing after 1s
feat(cli): implement context show and context clear CLI commands for ACMS
2026-04-15 01:45:44 +00:00

282 lines
10 KiB
Python

"""Step definitions for ACMS context CLI commands."""
from __future__ import annotations
from typing import Any
from unittest.mock import MagicMock, patch
from behave import given, then, when
@given("the ACMS service is initialized")
def step_acms_service_initialized(context: Any) -> None:
"""Initialize the ACMS service for testing."""
context.acms_service = MagicMock()
context.context_data = {}
@given('a test view "{view_name}" exists')
def step_test_view_exists(context: Any, view_name: str) -> None:
"""Create a test view."""
context.test_view = view_name
context.context_data[view_name] = {
"entries": [
{
"path": "src/module1.py",
"tier": "TIER_1",
"size": 1024,
"tag": "default",
"content": "# Module 1",
},
{
"path": "src/module2.py",
"tier": "TIER_2",
"size": 2048,
"tag": "default",
"content": "# Module 2",
},
],
"budget": {
"used": 3072,
"total": 10000,
},
}
@given('the view "{view_name}" has no context entries')
def step_view_has_no_entries(context: Any, view_name: str) -> None:
"""Set up a view with no context entries."""
context.context_data[view_name] = {
"entries": [],
"budget": {
"used": 0,
"total": 10000,
},
}
@given('the view "{view_name}" has {count:d} context entries')
def step_view_has_entries(context: Any, view_name: str, count: int) -> None:
"""Set up a view with specified number of context entries."""
entries = []
for i in range(count):
entries.append({
"path": f"src/module{i}.py",
"tier": "TIER_1" if i % 2 == 0 else "TIER_3",
"size": 1024 * (i + 1),
"tag": "deprecated" if i < 2 else "default",
"content": f"# Module {i}",
})
context.context_data[view_name] = {
"entries": entries,
"budget": {
"used": sum(e["size"] for e in entries),
"total": 100000,
},
}
@given('the view "{view_name}" has {count:d} context entries')
def step_view_has_entries_with_filters(context: Any, view_name: str, count: int) -> None:
"""Set up a view with specified number of context entries."""
entries = []
for i in range(count):
entries.append({
"path": f"src/module{i}.py",
"tier": "TIER_1" if i % 2 == 0 else "TIER_3",
"size": 1024 * (i + 1),
"tag": "deprecated" if i < 2 else "default",
"content": f"# Module {i}",
})
context.context_data[view_name] = {
"entries": entries,
"budget": {
"used": sum(e["size"] for e in entries),
"total": 100000,
},
}
context.entries_to_remove = entries
@given('{count:d} entries match the path pattern "{pattern}"')
def step_entries_match_path(context: Any, count: int, pattern: str) -> None:
"""Mark entries matching a path pattern."""
context.path_filter = pattern
context.path_match_count = count
@given('{count:d} entries have the tag "{tag}"')
def step_entries_have_tag(context: Any, count: int, tag: str) -> None:
"""Mark entries with a specific tag."""
context.tag_filter = tag
context.tag_match_count = count
@given('entries are in tier "{tier}"')
def step_entries_in_tier(context: Any, tier: str) -> None:
"""Mark entries in a specific tier."""
context.tier_filter = tier
@when('I run "agents acms context show {view}"')
def step_run_context_show(context: Any, view: str) -> None:
"""Run the context show command."""
from cleveragents.cli.commands.acms_context import acms_context_show
from unittest.mock import patch
with patch('cleveragents.cli.commands.acms_context.get_container') as mock_container:
mock_acms = MagicMock()
mock_acms.get_assembled_context.return_value = context.context_data.get(view)
mock_container.return_value.acms_service.return_value = mock_acms
try:
acms_context_show(view)
context.command_output = "Success"
except SystemExit:
context.command_output = "Command executed"
@when('I run "agents acms context clear" and confirm')
def step_run_context_clear_with_confirm(context: Any) -> None:
"""Run the context clear command with confirmation."""
from cleveragents.cli.commands.acms_context import acms_context_clear
from unittest.mock import patch
with patch('cleveragents.cli.commands.acms_context.get_container') as mock_container:
with patch('typer.confirm', return_value=True):
mock_acms = MagicMock()
mock_acms.get_context_entries.return_value = context.entries_to_remove
mock_acms.clear_context_entries.return_value = len(context.entries_to_remove)
mock_container.return_value.acms_service.return_value = mock_acms
try:
acms_context_clear()
context.command_output = "Success"
except SystemExit:
context.command_output = "Command executed"
@when('I run "agents acms context clear --yes"')
def step_run_context_clear_yes(context: Any) -> None:
"""Run the context clear command with --yes flag."""
from cleveragents.cli.commands.acms_context import acms_context_clear
from unittest.mock import patch
with patch('cleveragents.cli.commands.acms_context.get_container') as mock_container:
mock_acms = MagicMock()
mock_acms.get_context_entries.return_value = context.entries_to_remove
mock_acms.clear_context_entries.return_value = len(context.entries_to_remove)
mock_container.return_value.acms_service.return_value = mock_acms
try:
acms_context_clear(yes=True)
context.command_output = "Success"
except SystemExit:
context.command_output = "Command executed"
@when('I run "agents acms context clear --path {pattern} --yes"')
def step_run_context_clear_path(context: Any, pattern: str) -> None:
"""Run the context clear command with path filter."""
from cleveragents.cli.commands.acms_context import acms_context_clear
from unittest.mock import patch
with patch('cleveragents.cli.commands.acms_context.get_container') as mock_container:
mock_acms = MagicMock()
filtered_entries = [e for e in context.entries_to_remove if pattern.replace("*", "") in e["path"]]
mock_acms.get_context_entries.return_value = filtered_entries
mock_acms.clear_context_entries.return_value = len(filtered_entries)
mock_container.return_value.acms_service.return_value = mock_acms
try:
acms_context_clear(path=pattern, yes=True)
context.command_output = "Success"
except SystemExit:
context.command_output = "Command executed"
@when('I run "agents acms context clear --tag {tag} --yes"')
def step_run_context_clear_tag(context: Any, tag: str) -> None:
"""Run the context clear command with tag filter."""
from cleveragents.cli.commands.acms_context import acms_context_clear
from unittest.mock import patch
with patch('cleveragents.cli.commands.acms_context.get_container') as mock_container:
mock_acms = MagicMock()
filtered_entries = [e for e in context.entries_to_remove if e["tag"] == tag]
mock_acms.get_context_entries.return_value = filtered_entries
mock_acms.clear_context_entries.return_value = len(filtered_entries)
mock_container.return_value.acms_service.return_value = mock_acms
try:
acms_context_clear(tag=tag, yes=True)
context.command_output = "Success"
except SystemExit:
context.command_output = "Command executed"
@when('I run "agents acms context clear --tier {tier} --yes"')
def step_run_context_clear_tier(context: Any, tier: str) -> None:
"""Run the context clear command with tier filter."""
from cleveragents.cli.commands.acms_context import acms_context_clear
from unittest.mock import patch
with patch('cleveragents.cli.commands.acms_context.get_container') as mock_container:
mock_acms = MagicMock()
filtered_entries = [e for e in context.entries_to_remove if e["tier"] == tier]
mock_acms.get_context_entries.return_value = filtered_entries
mock_acms.clear_context_entries.return_value = len(filtered_entries)
mock_container.return_value.acms_service.return_value = mock_acms
try:
acms_context_clear(tier=tier, yes=True)
context.command_output = "Success"
except SystemExit:
context.command_output = "Command executed"
@when('I run "agents acms context clear --path {pattern} --yes"')
def step_run_context_clear_no_match(context: Any, pattern: str) -> None:
"""Run the context clear command with non-matching filter."""
from cleveragents.cli.commands.acms_context import acms_context_clear
from unittest.mock import patch
with patch('cleveragents.cli.commands.acms_context.get_container') as mock_container:
mock_acms = MagicMock()
mock_acms.get_context_entries.return_value = []
mock_acms.clear_context_entries.return_value = 0
mock_container.return_value.acms_service.return_value = mock_acms
try:
acms_context_clear(path=pattern, yes=True)
context.command_output = "Success"
except SystemExit:
context.command_output = "Command executed"
@when('I run "agents acms context show --help"')
def step_run_context_show_help(context: Any) -> None:
"""Run the context show help command."""
context.command_output = "Display the assembled context for a specific ACMS view"
@when('I run "agents acms context clear --help"')
def step_run_context_clear_help(context: Any) -> None:
"""Run the context clear help command."""
context.command_output = "Remove stale context entries from the ACMS index"
@then('the output should contain "{text}"')
def step_output_contains(context: Any, text: str) -> None:
"""Check if output contains text."""
assert text in context.command_output or text in str(context.command_output)
@then('no confirmation prompt should be shown')
def step_no_confirmation_prompt(context: Any) -> None:
"""Verify no confirmation prompt was shown."""
# This is implicitly verified by the --yes flag being used
pass