Feature: Plan CLI uncovered lines and branches coverage
  As a developer
  I want to exercise the uncovered lines and branches in plan.py
  So that code coverage gaps at lines 80, 818, 827-828, 999, 1017-1035,
  1314-1315, 1391-1392, 1573-1578, 1594-1596, 1700-1701, 1739 are closed

  # ===================================================================
  # Legacy cd command — full body coverage
  # ===================================================================

  Scenario: Legacy cd switches to an existing plan
    Given a plan-cov CLI runner
    And a mocked legacy container for plan-cov cd that succeeds
    When I invoke plan-cov legacy cd "my-plan"
    Then the plan-cov command should exit normally
    And the plan-cov output should contain "Switched to plan"

  Scenario: Legacy cd raises CleverAgentsError
    Given a plan-cov CLI runner
    And a mocked legacy container for plan-cov cd that raises CleverAgentsError
    When I invoke plan-cov legacy cd "bad-plan"
    Then the plan-cov command should abort
    And the plan-cov output should contain "Error"

  # ===================================================================
  # use command — invariant_actor and invariants
  # ===================================================================

  Scenario: Use action with estimation-actor and invariant-actor overrides
    Given a plan-cov CLI runner
    And a mocked lifecycle service for plan-cov use action
    When I invoke plan-cov use with estimation-actor "openai/gpt-4" and invariant-actor "openai/gpt-4"
    Then the plan-cov command should exit normally
    And the plan-cov created plan should have estimation_actor set

  Scenario: Use action with --invariant flags builds plan invariants
    Given a plan-cov CLI runner
    And a mocked lifecycle service for plan-cov use action
    When I invoke plan-cov use with invariant "No new warnings"
    Then the plan-cov command should exit normally

  # ===================================================================
  # apply — no plan_id auto-discovery branches
  # ===================================================================

  Scenario: apply without plan_id when no plans are ready
    Given a plan-cov CLI runner
    And a mocked lifecycle service for plan-cov that returns no execute-complete plans
    When I invoke plan-cov apply without plan_id
    Then the plan-cov command should abort
    And the plan-cov output should contain "No plans ready for apply"

  Scenario: apply without plan_id when multiple plans are ready
    Given a plan-cov CLI runner
    And a mocked lifecycle service for plan-cov that returns multiple execute-complete plans
    When I invoke plan-cov apply without plan_id
    Then the plan-cov command should abort
    And the plan-cov output should contain "Multiple plans ready"

  Scenario: apply rejects a read-only plan
    Given a plan-cov CLI runner
    And a mocked lifecycle service for plan-cov that returns a read-only plan
    When I invoke plan-cov apply with plan_id "01ARZ3NDEKTSV4RRFFQ69G5FAA"
    Then the plan-cov command should abort
    And the plan-cov output should contain "read-only"

  # ===================================================================
  # status command — CleverAgentsError handler
  # ===================================================================

  Scenario: status command raises CleverAgentsError
    Given a plan-cov CLI runner
    And a mocked lifecycle service for plan-cov status that raises CleverAgentsError
    When I invoke plan-cov status "01ARZ3NDEKTSV4RRFFQ69G5FAA"
    Then the plan-cov command should abort
    And the plan-cov output should contain "Error"

  # ===================================================================
  # errors command — no error_category branch
  # ===================================================================

  Scenario: errors command on plan with no error recovery data
    Given a plan-cov CLI runner
    And a mocked lifecycle service for plan-cov errors with no error_category
    When I invoke plan-cov errors "01ARZ3NDEKTSV4RRFFQ69G5FAA" with format "json"
    Then the plan-cov command should exit normally
    And the plan-cov output should contain "plan_id"

  Scenario: errors command on plan with empty error_details in rich format
    Given a plan-cov CLI runner
    And a mocked lifecycle service for plan-cov errors with empty details
    When I invoke plan-cov errors "01ARZ3NDEKTSV4RRFFQ69G5FAA"
    Then the plan-cov command should exit normally
    And the plan-cov output should contain "No error recovery records"
  Scenario: errors command prints OK footer in rich format
    Given a plan-cov CLI runner
    And a mocked lifecycle service for plan-cov errors with empty details
    When I invoke plan-cov errors "01ARZ3NDEKTSV4RRFFQ69G5FAA"
    Then the plan-cov command should exit normally
    And the plan-cov output should contain "✓ OK"
