Files
cleveragents-core/features/steps/plan_lifecycle_cli_steps.py
T
hurui200320 414abb1396
CI / benchmark-publish (push) Waiting to run
CI / lint (push) Successful in 19s
CI / build (push) Successful in 20s
CI / quality (push) Successful in 3m49s
CI / typecheck (push) Successful in 3m58s
CI / benchmark-regression (push) Waiting to run
CI / security (push) Successful in 4m7s
CI / integration_tests (push) Successful in 6m48s
CI / unit_tests (push) Successful in 7m30s
CI / docker (push) Successful in 1m12s
CI / e2e_tests (push) Successful in 10m1s
CI / coverage (push) Successful in 11m48s
CI / status-check (push) Successful in 1s
fix(cli): add missing --yes flag to plan apply command (#1127)
## Summary

Adds the `--yes`/`-y` flag to the `lifecycle-apply` CLI command as required by the specification (`agents plan apply [--yes|-y] <PLAN_ID>`). Without `--yes`, a confirmation prompt now displays before proceeding with the destructive Apply phase. With `--yes`, the apply proceeds immediately without prompting.

Closes #932

## Changes

### Source Code
- **`src/cleveragents/cli/commands/plan.py`**: Added `yes: Annotated[bool, typer.Option("--yes", "-y", help="Skip confirmation prompt")] = False` parameter to `lifecycle_apply_plan`. Added `typer.confirm()` prompt before the apply operation, consistent with the pattern used by `rollback_plan`, `correct_plan`, and other destructive commands.
  - Confirmation prompt text matches spec exactly: `"Apply changes for plan {plan_id}?"` producing `Apply changes for plan <ID>? [y/N]:`.
  - Fixed redundant plan ID display when `pre_plan` is `None` — now shows `"Apply changes for plan X?"` instead of `"Apply plan X (X)?"`.
  - Added `except ValueError` handler consistent with sibling commands `lifecycle_execute_plan` and `_lifecycle_apply_with_id`.
  - Added `except Exception` catch-all handler with `isinstance(e, (typer.Abort, typer.Exit))` re-raise guard, consistent with `lifecycle_execute_plan`.
  - Moved `PlanPhase` and `ProcessingState` imports to module level per CONTRIBUTING.md §Import Guidelines.

### TDD Tag Removal (Bug Fix Workflow)
- **`features/tdd_plan_apply_yes_flag.feature`**: Removed `@tdd_expected_fail` tag (leaving `@tdd_bug` and `@tdd_bug_932` as permanent regression guards).
- **`robot/tdd_plan_apply_yes_flag.robot`**: Removed `tdd_expected_fail` tag (leaving `tdd_bug` and `tdd_bug_932`).

### Test Updates
Updated all existing `lifecycle-apply` invocations across 17 test/benchmark files to pass `--yes`, since the new confirmation prompt would otherwise abort in non-interactive test environments:
- 9 Behave step definition files
- 3 Robot Framework helper scripts
- 2 Robot Framework e2e acceptance tests
- 3 ASV benchmark files (4 invocations: `cli_robot_flow_bench.py` ×2, `m1_sourcecode_smoke_bench.py` ×1, `plan_cli_smoke_bench.py` ×1)

### Confirmation Prompt Tests (New + Strengthened)
- **`features/tdd_plan_apply_yes_flag.feature`**: 5 scenarios total:
  - `lifecycle-apply recognises the --yes long flag` — verifies flag acceptance, prompt suppression, exit code 0, and `apply_plan` was called
  - `lifecycle-apply recognises the -y short flag` — same as above for short flag
  - `lifecycle-apply without --yes prompts for confirmation and user declines` — verifies `"Apply cancelled."` message, `exit_code == 0`, and `apply_plan` was NOT called
  - `lifecycle-apply without --yes prompts for confirmation and user accepts` — verifies prompt appears, `exit_code == 0`, and `apply_plan` was called
  - `lifecycle-apply catches unexpected exceptions cleanly` — verifies `"Unexpected error"` output, no traceback leak, non-zero exit code (exercises the `except Exception` catch-all)
- **`features/steps/tdd_plan_apply_yes_flag_steps.py`**: Refactored step definitions:
  - `_make_mock_plan` uses `PlanPhase` and `ProcessingState` enum types instead of raw strings
  - `_make_mock_plan` uses `datetime.now(tz=UTC)` instead of timezone-naive `datetime.now()`
  - Unified prompt suppression step handles both `--yes` and `-y` via parameterised step pattern
  - Added `When` step for unexpected error scenario with `RuntimeError` side_effect
  - Added `Then` step for non-zero exit code assertion
- **Feature/Robot documentation**: Updated stale descriptions that said "implementation does not accept --yes" to reflect the flag is now implemented.

### Documentation
- **`docs/reference/plan_cli.md`**: Updated `lifecycle-apply` section with:
  - `### Synopsis` heading with code block
  - `### Options` table listing `--yes/-y` and `--format/-f` flags
  - `### Arguments` table listing `PLAN_ID`
  - Matches the style used by other command sections in the same file

## Review Fixes (Cycle 3 — Luis's review)

| ID | Severity | Issue | Resolution |
|----|----------|-------|------------|
| M1 | Medium | `typer.Abort()` on user decline produces exit code 1 and redundant "Aborted." | Changed to `raise typer.Exit(0)` — consistent with `correct_decision` and legacy `apply` |
| M2 | Medium | Missing exit code assertion on decline scenario | Added `And the lifecycle-apply exit code should be 0` to the decline scenario |
| M3 | Medium | Spec compliance: "summary of pending changes" not implemented | Deferred — spec example shows summary *after* confirmation, not before; implementation matches spec. Ticket-vs-spec ambiguity noted. |
| L1 | Low | Missing `except Exception` catch-all handler | Added catch-all matching `lifecycle_execute_plan` pattern; re-raises `typer.Abort`/`typer.Exit` |
| L2 | Low | Documentation description not updated | Expanded description in `plan_cli.md` to explain confirmation prompt and `--yes` |
| L3 | Low | Dead code `is not None` guards | Removed both guards — `get_plan()` raises `NotFoundError`, never returns `None` |
| I1 | Info | Duplicate `PlanPhase` import | Hoisted import to top of `try` block, eliminating duplicate at old line 2087 |
| I2 | Info | `typer.confirm` without explicit `default=False` | Added `default=False` for consistency with sibling commands |
| L4 | Low | No test for `--yes` after positional arg | Not addressed — Typer/Click handles both orderings; low risk |
| L5 | Low | No test for auto-select + interactive prompt | Not addressed — separate concern outside ticket scope |
| I3 | Info | Robot helper only tests flag recognition | By design — noted as informational |

## Review Fixes (Cycle 4 — Self-QA)

| ID | Severity | Issue | Resolution |
|----|----------|-------|------------|
| Major-1 | Major | No test for `except Exception` catch-all handler | Added new scenario `"lifecycle-apply catches unexpected exceptions cleanly"` with `RuntimeError` side_effect; asserts `"Unexpected error"` output, no traceback, non-zero exit |
| Minor-2 | Minor | Missing `ValueError` handler inconsistent with siblings | Added `except ValueError as e:` with `"[red]Execution Error:[/red]"` before catch-all, matching `lifecycle_execute_plan` and `_lifecycle_apply_with_id` |
| Minor-3 | Minor | Flag scenarios don't verify `apply_plan` called | Added `And the lifecycle-apply should have called apply` to both `--yes` and `-y` scenarios |
| Minor-4 | Minor | Stale docstring in Robot helper references `tdd_expected_fail` inversion | Updated to reflect bug is fixed and tests serve as regression guards |
| Minor-5 | Minor | `plan_cli.md` lacks Options table for `lifecycle-apply` | Added Synopsis, Options, and Arguments sections matching sibling command style |
| Nit-6 | Nit | Duplicated step defs for `--yes` vs `-y` prompt suppression | Unified into single parameterised step `"the lifecycle-apply {flag} output should not contain the confirmation prompt"` |
| Nit-7 | Nit | `datetime.now()` timezone-naive | Changed to `datetime.now(tz=UTC)` |
| Nit-8 | Nit | `_make_mock_plan` params use `str` instead of enum types | Changed to `PlanPhase` and `ProcessingState` enum types |

## Review Fixes (Cycle 5 — Jeff's approval note)

| ID | Severity | Issue | Resolution |
|----|----------|-------|------------|
| Import-1 | Minor | `PlanPhase`/`ProcessingState` imports inside function body instead of module level | Moved to module-level import per CONTRIBUTING.md §Import Guidelines |

## Known Limitations / Deferred Items

- **M3: Ticket AC mentions "summary of pending changes"** but the spec example only shows `"Apply changes for plan <ID>? [y/N]: y"` without a change summary. The implementation shows plan ID only (matching the spec), not a change summary. This is a ticket-vs-spec ambiguity; recommend discussing with ticket author.
- **Legacy `apply` command** accepts `--yes` but does not pass it to `_lifecycle_apply_with_id()`. This is a pre-existing issue outside the scope of this ticket.
- **`pre_plan is None` branch** has no explicit test. Pre-existing architectural issue; no action taken.

## Quality Gates

| Gate | Result |
|------|--------|
| `nox -s lint` |  passed |
| `nox -s typecheck` |  passed (0 errors) |
| `nox -s unit_tests` |  passed (471 features, 12,424 scenarios, 0 failures) |
| `nox -s integration_tests` |  passed (1,727 tests, 0 failures) |
| `nox -s e2e_tests` |  passed (41 tests, 0 failures) |
| `nox -s coverage_report` |  passed (≥97% coverage) |

Reviewed-on: #1127
Reviewed-by: Jeffrey Phillips Freeman <jeffrey.freeman@cleverthis.com>
Co-authored-by: Rui Hu <rui.hu@cleverthis.com>
Co-committed-by: Rui Hu <rui.hu@cleverthis.com>
2026-03-26 07:50:09 +00:00

576 lines
20 KiB
Python

"""Step definitions for plan lifecycle CLI coverage."""
from __future__ import annotations
import importlib
from types import SimpleNamespace
from unittest.mock import MagicMock, patch
from behave import given, then, when
from typer.testing import CliRunner
from cleveragents.application.services.plan_lifecycle_service import (
ActionNotAvailableError,
InvalidPhaseTransitionError,
PlanLifecycleService,
PlanNotReadyError,
)
from cleveragents.cli.commands import plan as plan_module
from cleveragents.cli.commands.plan import app as plan_app
from cleveragents.core.exceptions import (
CleverAgentsError,
NotFoundError,
PlanError,
ValidationError,
)
from cleveragents.domain.models.core.action import ActionState
from cleveragents.domain.models.core.plan import (
NamespacedName,
Plan,
PlanIdentity,
PlanPhase,
ProcessingState,
ProjectLink,
)
_ULIDS = [
"01ARZ3NDEKTSV4RRFFQ69G5FAV",
"01ARZ3NDEKTSV4RRFFQ69G5FAW",
"01ARZ3NDEKTSV4RRFFQ69G5FAX",
"01ARZ3NDEKTSV4RRFFQ69G5FAY",
"01ARZ3NDEKTSV4RRFFQ69G5FAZ",
"01ARZ3NDEKTSV4RRFFQ69G5FB0",
"01ARZ3NDEKTSV4RRFFQ69G5FB1",
"01ARZ3NDEKTSV4RRFFQ69G5FB2",
"01ARZ3NDEKTSV4RRFFQ69G5FB3",
"01ARZ3NDEKTSV4RRFFQ69G5FB4",
"01ARZ3NDEKTSV4RRFFQ69G5FB5",
"01ARZ3NDEKTSV4RRFFQ69G5FB6",
"01ARZ3NDEKTSV4RRFFQ69G5FB7",
"01ARZ3NDEKTSV4RRFFQ69G5FB8",
"01ARZ3NDEKTSV4RRFFQ69G5FB9",
"01ARZ3NDEKTSV4RRFFQ69G5FBA",
"01ARZ3NDEKTSV4RRFFQ69G5FBB",
"01ARZ3NDEKTSV4RRFFQ69G5FBC",
"01ARZ3NDEKTSV4RRFFQ69G5FBD",
"01ARZ3NDEKTSV4RRFFQ69G5FBE",
]
def _make_plan(
*,
plan_id: str,
name: str,
description: str = "Lifecycle plan",
phase: PlanPhase = PlanPhase.STRATEGIZE,
processing_state: ProcessingState = ProcessingState.COMPLETE,
project_links: list[ProjectLink] | None = None,
error_message: str | None = None,
) -> Plan:
return Plan(
identity=PlanIdentity(plan_id=plan_id),
namespaced_name=NamespacedName.parse(name),
action_name="local/test-action",
description=description,
definition_of_done=None,
phase=phase,
processing_state=processing_state,
project_links=project_links or [],
error_message=error_message,
reusable=True,
read_only=False,
)
def _output(context) -> str:
return getattr(context.result, "output", "") if hasattr(context, "result") else ""
@given("a plan lifecycle CLI runner")
def step_plan_lifecycle_cli_runner(context) -> None:
context.runner = CliRunner()
@given("a mocked lifecycle service for plan commands")
def step_mocked_plan_lifecycle_service(context) -> None:
context.lifecycle_service = MagicMock()
patcher = patch(
"cleveragents.cli.commands.plan._get_lifecycle_service",
return_value=context.lifecycle_service,
)
patcher.start()
if not hasattr(context, "_cleanup_handlers"):
context._cleanup_handlers = []
context._cleanup_handlers.append(patcher.stop)
# Widen the Rich console so table columns do not wrap during tests
console_patcher = patch.object(plan_module.console, "width", 200)
console_patcher.start()
context._cleanup_handlers.append(console_patcher.stop)
@when("I call the plan lifecycle service helper")
def step_call_lifecycle_service_helper(context) -> None:
settings = MagicMock()
service = PlanLifecycleService(settings=settings)
container = SimpleNamespace(plan_lifecycle_service=MagicMock(return_value=service))
reloaded_plan_module = importlib.reload(plan_module)
with patch(
"cleveragents.application.container.get_container", return_value=container
):
context.helper_settings = settings
context.helper_service = reloaded_plan_module._get_lifecycle_service()
@then("the lifecycle service helper should return a plan lifecycle service")
def step_lifecycle_service_helper_returns(context) -> None:
svc = context.helper_service
assert isinstance(svc, PlanLifecycleService), (
f"Expected PlanLifecycleService, got {type(svc).__name__} "
f"(module={type(svc).__module__})"
)
assert svc.settings is context.helper_settings
@when("I run plan lifecycle use with parsed arguments")
def step_plan_use_with_parsed_arguments(context) -> None:
action = SimpleNamespace(namespaced_name="local/code-coverage")
context.lifecycle_service.get_action.side_effect = NotFoundError(
resource_type="action",
resource_id="missing",
)
context.lifecycle_service.get_action_by_name.return_value = action
plan = _make_plan(
plan_id=_ULIDS[1],
name="local/coverage-plan",
description="x" * 240,
error_message="Strategy failed",
project_links=[
ProjectLink(project_name="proj-1"),
ProjectLink(project_name="proj-2"),
],
)
context.lifecycle_service.use_action.return_value = plan
context.result = context.runner.invoke(
plan_app,
[
"use",
"local/code-coverage",
"--project",
"proj-1",
"--project",
"proj-2",
"--arg",
"count=3",
"--arg",
"ratio=4.2",
"--arg",
"enabled=true",
"--arg",
"label=alpha",
],
)
@when('I run plan lifecycle use with invalid argument "{arg_value}"')
def step_plan_use_with_invalid_argument(context, arg_value: str) -> None:
action = SimpleNamespace(namespaced_name="local/code-coverage")
context.lifecycle_service.get_action.return_value = action
context.result = context.runner.invoke(
plan_app,
[
"use",
"local/code-coverage",
"--project",
"proj-1",
"--arg",
arg_value,
],
)
@when('I run plan lifecycle use causing "{error_type}"')
def step_plan_use_error(context, error_type: str) -> None:
action = SimpleNamespace(namespaced_name="local/code-coverage")
context.lifecycle_service.get_action.return_value = action
if error_type == "action not available":
context.lifecycle_service.use_action.side_effect = ActionNotAvailableError(
str(action.namespaced_name),
ActionState.ARCHIVED,
)
elif error_type == "validation error":
context.lifecycle_service.use_action.side_effect = ValidationError(
"Invalid action"
)
elif error_type == "general error":
context.lifecycle_service.use_action.side_effect = CleverAgentsError(
"Use failed"
)
else:
raise ValueError(f"Unhandled error type: {error_type}")
context.result = context.runner.invoke(
plan_app,
[
"use",
"local/code-coverage",
"--project",
"proj-1",
],
)
@when("I run plan execute without a plan id with {count:d} complete plans")
def step_plan_execute_without_plan_id(context, count: int) -> None:
plans = [
_make_plan(
plan_id=_ULIDS[idx],
name=f"local/execute-plan-{idx}",
phase=PlanPhase.STRATEGIZE,
processing_state=ProcessingState.COMPLETE,
)
for idx in range(count)
]
context.lifecycle_service.list_plans.return_value = plans
if count == 1:
context.lifecycle_service.get_plan.return_value = plans[0]
context.lifecycle_service.execute_plan.return_value = plans[0]
context.execute_plans = plans
# Mock the plan executor for successful execute paths
mock_executor = MagicMock()
executor_patcher = patch(
"cleveragents.cli.commands.plan._get_plan_executor",
return_value=mock_executor,
)
executor_patcher.start()
if not hasattr(context, "_cleanup_handlers"):
context._cleanup_handlers = []
context._cleanup_handlers.append(executor_patcher.stop)
context.result = context.runner.invoke(plan_app, ["execute"])
@when('I run plan execute for plan id "{plan_id}" causing "{error_type}"')
def step_plan_execute_error(context, plan_id: str, error_type: str) -> None:
# Provide a real plan so phase checks pass before the error is raised
error_plan = _make_plan(
plan_id=plan_id,
name="local/error-plan",
phase=PlanPhase.STRATEGIZE,
processing_state=ProcessingState.COMPLETE,
)
context.lifecycle_service.get_plan.return_value = error_plan
if error_type == "invalid transition":
context.lifecycle_service.execute_plan.side_effect = (
InvalidPhaseTransitionError(
PlanPhase.STRATEGIZE,
PlanPhase.EXECUTE,
)
)
elif error_type == "plan not ready":
context.lifecycle_service.execute_plan.side_effect = PlanNotReadyError(
plan_id,
PlanPhase.STRATEGIZE,
ProcessingState.QUEUED,
)
elif error_type == "general error":
context.lifecycle_service.execute_plan.side_effect = CleverAgentsError(
"Execute failed"
)
else:
raise ValueError(f"Unhandled error type: {error_type}")
context.result = context.runner.invoke(plan_app, ["execute", plan_id])
@when("I run plan lifecycle apply without a plan id with {count:d} complete plans")
def step_plan_lifecycle_apply_without_plan_id(context, count: int) -> None:
plans = [
_make_plan(
plan_id=_ULIDS[10 + idx],
name=f"local/apply-plan-{idx}",
phase=PlanPhase.EXECUTE,
processing_state=ProcessingState.COMPLETE,
)
for idx in range(count)
]
context.lifecycle_service.list_plans.return_value = plans
if count == 1:
context.lifecycle_service.apply_plan.return_value = plans[0]
context.apply_plans = plans
context.result = context.runner.invoke(plan_app, ["lifecycle-apply", "--yes"])
@when('I run plan lifecycle apply for plan id "{plan_id}" causing "{error_type}"')
def step_plan_lifecycle_apply_error(context, plan_id: str, error_type: str) -> None:
if error_type == "invalid transition":
context.lifecycle_service.apply_plan.side_effect = InvalidPhaseTransitionError(
PlanPhase.EXECUTE,
PlanPhase.APPLY,
)
elif error_type == "plan not ready":
context.lifecycle_service.apply_plan.side_effect = PlanNotReadyError(
plan_id,
PlanPhase.EXECUTE,
ProcessingState.QUEUED,
)
elif error_type == "general error":
context.lifecycle_service.apply_plan.side_effect = CleverAgentsError(
"Apply failed"
)
else:
raise ValueError(f"Unhandled error type: {error_type}")
context.result = context.runner.invoke(
plan_app, ["lifecycle-apply", "--yes", plan_id]
)
@when("I run plan status without a plan id and {count:d} plans exist")
def step_plan_status_without_plan_id(context, count: int) -> None:
if count == 0:
context.lifecycle_service.list_plans.return_value = []
else:
plans = [
_make_plan(
plan_id=_ULIDS[5 + idx],
name=f"local/status-plan-{idx}",
phase=PlanPhase.EXECUTE if idx % 2 == 0 else PlanPhase.APPLY,
processing_state=(
ProcessingState.COMPLETE if idx % 2 == 0 else ProcessingState.QUEUED
),
)
for idx in range(count)
]
context.lifecycle_service.list_plans.return_value = plans
context.status_plans = plans
context.result = context.runner.invoke(plan_app, ["status"])
@when('I run plan status for legacy plan id "{plan_id}"')
def step_plan_status_for_legacy_plan(context, plan_id: str) -> None:
context.lifecycle_service.get_plan.return_value = "legacy-plan"
context.result = context.runner.invoke(plan_app, ["status", plan_id])
@when("I run plan status causing a general error")
def step_plan_status_error(context) -> None:
context.lifecycle_service.list_plans.side_effect = CleverAgentsError(
"Status failed"
)
context.result = context.runner.invoke(plan_app, ["status"])
@when('I run plan lifecycle list with invalid phase "{phase}"')
def step_plan_lifecycle_list_invalid_phase(context, phase: str) -> None:
context.result = context.runner.invoke(
plan_app,
[
"lifecycle-list",
"--phase",
phase,
],
)
@when("I run plan lifecycle list with no plans")
def step_plan_lifecycle_list_no_plans(context) -> None:
context.lifecycle_service.list_plans.return_value = []
context.result = context.runner.invoke(plan_app, ["lifecycle-list"])
@when('I run plan lifecycle list with plans and phase "{phase}"')
def step_plan_lifecycle_list_with_plans(context, phase: str) -> None:
plans = [
_make_plan(
plan_id=_ULIDS[12],
name="local/short-projects",
phase=PlanPhase.EXECUTE,
processing_state=ProcessingState.PROCESSING,
project_links=[ProjectLink(project_name="proj-1")],
),
_make_plan(
plan_id=_ULIDS[13],
name="local/multi-projects",
phase=PlanPhase.APPLY,
processing_state=ProcessingState.QUEUED,
project_links=[
ProjectLink(project_name="proj-1"),
ProjectLink(project_name="proj-2"),
ProjectLink(project_name="proj-3"),
],
),
]
context.lifecycle_service.list_plans.return_value = plans
context.result = context.runner.invoke(
plan_app,
[
"lifecycle-list",
"--phase",
phase,
"--project",
"proj-1",
],
)
@when("I run plan lifecycle list causing a general error")
def step_plan_lifecycle_list_error(context) -> None:
context.lifecycle_service.list_plans.side_effect = CleverAgentsError("List failed")
context.result = context.runner.invoke(plan_app, ["lifecycle-list"])
@when('I run plan cancel for plan id "{plan_id}" with reason "{reason}"')
def step_plan_cancel_with_reason(context, plan_id: str, reason: str) -> None:
plan = _make_plan(
plan_id=plan_id,
name="local/cancel-plan",
phase=PlanPhase.APPLY,
processing_state=ProcessingState.PROCESSING,
)
context.lifecycle_service.cancel_plan.return_value = plan
context.result = context.runner.invoke(
plan_app,
[
"cancel",
plan_id,
"--reason",
reason,
],
)
@when('I run plan cancel for plan id "{plan_id}" without reason')
def step_plan_cancel_without_reason(context, plan_id: str) -> None:
plan = _make_plan(
plan_id=plan_id,
name="local/cancel-plan",
phase=PlanPhase.APPLY,
processing_state=ProcessingState.PROCESSING,
)
context.lifecycle_service.cancel_plan.return_value = plan
context.result = context.runner.invoke(plan_app, ["cancel", plan_id])
@when('I run plan cancel for plan id "{plan_id}" causing "{error_type}"')
def step_plan_cancel_error(context, plan_id: str, error_type: str) -> None:
if error_type == "plan error":
context.lifecycle_service.cancel_plan.side_effect = PlanError("Cannot cancel")
elif error_type == "general error":
context.lifecycle_service.cancel_plan.side_effect = CleverAgentsError(
"Cancel failed"
)
else:
raise ValueError(f"Unhandled error type: {error_type}")
context.result = context.runner.invoke(plan_app, ["cancel", plan_id])
@then("the plan lifecycle command should succeed")
def step_plan_lifecycle_command_succeeds(context) -> None:
assert context.result.exit_code == 0, f"Output: {_output(context)}"
@then("the plan lifecycle command should abort")
def step_plan_lifecycle_command_aborts(context) -> None:
assert context.result.exit_code != 0, f"Output: {_output(context)}"
@then('the plan lifecycle output should contain "{text}"')
def step_plan_lifecycle_output_contains(context, text: str) -> None:
output = _output(context)
assert text in output, f"Expected '{text}' in output: {output}"
@then('the plan lifecycle output should not contain "{text}"')
def step_plan_lifecycle_output_not_contains(context, text: str) -> None:
output = _output(context)
assert text not in output, f"Did not expect '{text}' in output: {output}"
@then("the plan lifecycle use should pass parsed arguments")
def step_plan_lifecycle_use_parsed_arguments(context) -> None:
context.lifecycle_service.get_action_by_name.assert_called_once()
kwargs = context.lifecycle_service.use_action.call_args.kwargs
links = kwargs["project_links"]
assert [link.project_name for link in links] == ["proj-1", "proj-2"]
assert kwargs["arguments"] == {
"count": 3,
"ratio": 4.2,
"enabled": True,
"label": "alpha",
}
@then("the execute command should run the single ready plan")
def step_execute_command_runs_single_plan(context) -> None:
assert len(context.execute_plans) == 1
plan_id = context.execute_plans[0].identity.plan_id
context.lifecycle_service.execute_plan.assert_called_once_with(plan_id)
@then("the lifecycle apply command should run the single ready plan")
def step_lifecycle_apply_runs_single_plan(context) -> None:
assert len(context.apply_plans) == 1
plan_id = context.apply_plans[0].identity.plan_id
context.lifecycle_service.apply_plan.assert_called_once_with(plan_id)
# ------------------------------------------------------------------
# Regression: lifecycle-service sharing between CLI handler & executor
# ------------------------------------------------------------------
@when("I run plan execute verifying lifecycle service sharing")
def step_plan_execute_verify_sharing(context) -> None:
"""Run ``plan execute`` and capture the ``_get_plan_executor`` call.
After Background has already mocked ``_get_lifecycle_service`` to
return ``context.lifecycle_service``, we patch ``_get_plan_executor``
so we can later assert it was called with the **same** service
instance.
"""
plan = _make_plan(
plan_id=_ULIDS[0],
name="local/shared-svc-plan",
phase=PlanPhase.STRATEGIZE,
processing_state=ProcessingState.COMPLETE,
)
context.lifecycle_service.list_plans.return_value = [plan]
context.lifecycle_service.get_plan.return_value = plan
context.lifecycle_service.execute_plan.return_value = plan
mock_executor = MagicMock()
executor_patcher = patch(
"cleveragents.cli.commands.plan._get_plan_executor",
return_value=mock_executor,
)
mock_get_executor = executor_patcher.start()
if not hasattr(context, "_cleanup_handlers"):
context._cleanup_handlers = []
context._cleanup_handlers.append(executor_patcher.stop)
context._mock_get_plan_executor = mock_get_executor
context.result = context.runner.invoke(plan_app, ["execute"])
@then("the plan executor should receive the same lifecycle service instance")
def step_executor_shares_lifecycle_service(context) -> None:
"""Assert ``_get_plan_executor`` was called with the shared service."""
mock_fn = context._mock_get_plan_executor
mock_fn.assert_called_once()
call_kwargs = mock_fn.call_args.kwargs
assert "lifecycle_service" in call_kwargs, (
"_get_plan_executor was not called with a lifecycle_service kwarg; "
f"got kwargs: {call_kwargs}"
)
assert call_kwargs["lifecycle_service"] is context.lifecycle_service, (
"Expected _get_plan_executor to receive the same lifecycle service "
"instance that _get_lifecycle_service() returned, but it received "
"a different object."
)