Feature: Actor context clear, remove, export, and import commands
  As a CleverAgents user
  I want to manage actor contexts via the CLI
  So that I can remove, export, and import conversation contexts

  Background:
    Given a temporary context directory for actor context tests

  # ── context clear ─────────────────────────────────────────

  Scenario: Clear a named actor context
    Given an actor context named "docs" exists with messages
    When I run actor context clear "docs" with --yes
    Then the actor context clear command should succeed
    And the context "docs" should exist
    And the context "docs" should be empty

  Scenario: Clear all actor contexts
    Given an actor context named "docs" exists with messages
    And an actor context named "notes" exists with messages
    When I run actor context clear --all with --yes
    Then the actor context clear command should succeed
    And all actor contexts should be empty

  Scenario: Clear non-existent context fails
    When I run actor context clear "ghost" with --yes
    Then the actor context clear command should fail with exit code 1

  Scenario: Clear requires NAME or --all
    When I run actor context clear without name or all
    Then the actor context clear command should fail with exit code 1

  Scenario: Clear rejects NAME with --all
    When I run actor context clear "docs" with --all
    Then the actor context clear command should fail with exit code 1

  Scenario: Clear outputs JSON format
    Given an actor context named "docs" exists with messages
    When I run actor context clear "docs" with --yes and format "json"
    Then the actor context clear command should succeed
    And the output should contain valid JSON with key "context_cleared"
    And the output JSON key "context_cleared" should contain keys "items, storage"
    And the output should contain valid JSON with key "retention"
    And the output JSON key "retention" should contain keys "context, files"

  # ── context remove ─────────────────────────────────────────

  Scenario: Remove a named actor context
    Given an actor context named "docs" exists
    When I run actor context remove "docs" with --yes
    Then the actor context remove command should succeed
    And the context "docs" should no longer exist

  Scenario: Remove all actor contexts
    Given an actor context named "docs" exists
    And an actor context named "notes" exists
    When I run actor context remove --all with --yes
    Then the actor context remove command should succeed
    And no actor contexts should remain

  Scenario: Remove non-existent context fails
    When I run actor context remove "nonexistent" with --yes
    Then the actor context remove command should fail with exit code 1

  Scenario: Remove requires NAME or --all
    When I run actor context remove without name or all
    Then the actor context remove command should fail with exit code 1

  Scenario: Remove rejects NAME with --all
    When I run actor context remove "docs" with --all
    Then the actor context remove command should fail with exit code 1

  Scenario: Remove outputs JSON format
    Given an actor context named "docs" exists
    When I run actor context remove "docs" with --yes and format "json"
    Then the actor context remove command should succeed
    And the output should contain valid JSON with key "context_removed"

  # ── context export ─────────────────────────────────────────

  Scenario: Export a named actor context to JSON
    Given an actor context named "docs" exists with messages
    When I run actor context export "docs" to a JSON file
    Then the actor context export command should succeed
    And the exported file should exist and contain valid JSON
    And the exported JSON should contain key "messages"

  Scenario: Export a named actor context to YAML
    Given an actor context named "docs" exists with messages
    When I run actor context export "docs" to a YAML file
    Then the actor context export command should succeed
    And the exported YAML file should exist and be valid

  Scenario: Export non-existent context fails
    When I run actor context export "nonexistent" to a JSON file
    Then the actor context export command should fail with exit code 1

  Scenario: Export outputs JSON format metadata
    Given an actor context named "docs" exists with messages
    When I run actor context export "docs" to a JSON file with format "json"
    Then the actor context export command should succeed
    And the output should contain valid JSON with key "context_export"

  # ── context import ─────────────────────────────────────────

  Scenario: Import a context from a JSON file
    Given a valid context JSON file named "imported-ctx.json"
    When I run actor context import from that file as "imported-ctx"
    Then the actor context import command should succeed
    And the context "imported-ctx" should exist

  Scenario: Import infers name from file metadata
    Given a valid context JSON file with context_name "auto-named"
    When I run actor context import from that file without a name
    Then the actor context import command should succeed
    And the context "auto-named" should exist

  Scenario: Import refuses to overwrite without --update
    Given an actor context named "existing" exists
    And a valid context JSON file named "existing.json"
    When I run actor context import from that file as "existing" without update
    Then the actor context import command should fail with exit code 1

  Scenario: Import with --update replaces existing context
    Given an actor context named "existing" exists
    And a valid context JSON file named "existing.json"
    When I run actor context import from that file as "existing" with --update
    Then the actor context import command should succeed
    And the context "existing" should exist

  Scenario: Export then import round-trip preserves data
    Given an actor context named "roundtrip" exists with messages
    When I export the context "roundtrip" to a JSON file
    And I remove the context "roundtrip"
    And I import the context from that JSON file as "roundtrip"
    Then the context "roundtrip" should exist
    And the imported context should have the same messages as the original
