Feature: Plan Service Coverage
  As a developer maintaining the CleverAgents system
  I want comprehensive tests for the plan service
  So that plan management functionality works reliably

  Background:
    Given I have a temporary test directory for plan service
    And I have a Unit of Work instance for plan testing
    And I have a PlanService instance

  Scenario: Create plan with unsaved project raises error
    Given I have an unsaved project without ID
    When I try to create a plan for the unsaved project
    Then a ValidationError should be raised with message "Cannot create plan: this directory is not linked to a saved CleverAgents project."

  Scenario: Build plan when no current plan exists
    Given I have a saved project with no current plan
    When I try to build the plan for the project
    Then a PlanError should be raised with message "No current plan to build"

  Scenario: Build plan when plan has no valid ID
    Given I have a saved project with current plan missing ID
    When I try to build the plan with invalid ID
    Then a PlanError should be raised with message "No current plan to build"

  Scenario: Build plan without AI provider configured
    Given I have a saved project with valid current plan
    And I have a plan service without AI provider
    When I try to build the plan without AI provider
    Then a PlanError should be raised with message "No AI provider configured"

  Scenario: Get pending changes for project with no current plan
    Given I have a saved project with no current plan
    When I get pending changes for the project
    Then the pending changes list should be empty

  Scenario: Get pending changes for project with current plan
    Given I have a saved project with current plan
    And the current plan has applied and unapplied changes
    When I get pending changes for the project
    Then I should get only the unapplied changes

  Scenario: Apply changes when no current plan exists
    Given I have a saved project with no current plan
    When I try to apply changes without current plan
    Then a PlanError should be raised with message "No current plan to apply"

  Scenario: Apply changes with CREATE operation
    Given I have a saved project with current plan
    And the plan has a pending CREATE change
    When I apply the plan changes
    Then the file should be created with correct content
    And the change should be marked as applied in plan service

  Scenario: Apply changes with MODIFY operation
    Given I have a saved project with current plan
    And the plan has a pending MODIFY change for existing file
    When I apply the plan changes
    Then the file should be modified with new content
    And the change should be marked as applied in plan service

  Scenario: Apply changes with DELETE operation
    Given I have a saved project with current plan
    And the plan has a pending DELETE change for existing file
    When I apply the plan changes
    Then the file should be deleted
    And the change should be marked as applied in plan service

  Scenario: Apply changes with MOVE operation
    Given I have a saved project with current plan
    And the plan has a pending MOVE change for existing file
    When I apply the plan changes
    Then the file should be moved to new location
    And the change should be marked as applied in plan service

  Scenario: Apply changes with absolute file paths
    Given I have a saved project with current plan
    And the plan has changes with absolute file paths
    When I apply the plan changes
    Then the files should be created at absolute paths
    And the changes should be marked as applied

  Scenario: Apply changes handles file operation errors
    Given I have a saved project with current plan
    And the plan has a change that will fail on apply
    When I try to apply the changes
    Then a PlanError should be raised with file operation details

  Scenario: Create plan with minimal prompt generates name
    Given I have a saved project
    When I create a plan with minimal prompt
    Then the plan name should be generated from prompt

  Scenario: Create plan with long prompt truncates name
    Given I have a saved project
    When I create a plan with very long prompt
    Then the plan name should be truncated to first three words

  Scenario: Apply MOVE operation when source file does not exist
    Given I have a saved project with current plan
    And the plan has a MOVE change for non-existent file
    When I apply the plan changes
    Then the MOVE operation should handle missing source gracefully

  Scenario: Apply DELETE operation when file does not exist
    Given I have a saved project with current plan
    And the plan has a DELETE change for non-existent file
    When I apply the plan changes
    Then the DELETE operation should handle missing file gracefully

  Scenario: Build plan with progress callback
    Given I have a saved project with valid current plan
    And I have a plan service with mock AI provider
    When I build the plan with progress callback
    Then the progress callback should be called with values from 0 to 100

  Scenario: Building plan fails when plan ID disappears after status update
    Given I configure a stub plan service whose current plan loses its ID after the transaction
    When I try to build the plan with the stubbed service
    Then a PlanError should be raised with message "Plan does not have a valid ID"

  Scenario: Plan without repository ID remains non-current
    Given I replace the plan service repository with a stub that never assigns plan IDs
    When I create a plan with stub prompt "Missing ID branch"
    Then the stub-created plan should remain non-current
    And the stub repository should not record a current plan

  Scenario: Reusing persistent memory with unchanged limit skips reset
    When I request a persistent memory service for session "reuse-session" with max messages 4
    And I monitor max message updates for session "reuse-session"
    And I request the same persistent memory service for session "reuse-session" with max messages 4
    Then the session "reuse-session" memory should reuse the existing service without changing limits

  Scenario: Get pending changes with mixed applied status
    Given I have a saved project with current plan
    And the plan has 3 applied and 2 unapplied changes
    When I get pending changes for the project
    Then I should get exactly 2 pending changes
    And the pending changes should not include applied ones

  Scenario: Toggle plan memory persistence and limits
    When I request an in-memory memory service for session "memory-session" with max messages 2
    Then the session "memory-session" memory should be in-memory with max messages 2
    When I request a persistent memory service for session "memory-session" with max messages 3
    Then the session "memory-session" memory should be persistent with max messages 3
    When I request a persistent memory service for session "memory-session" with max messages 5
    Then the session "memory-session" memory should be persistent with max messages 5
    When I request an in-memory memory service for session "memory-session" without changing message limit
    Then the session "memory-session" memory should be in-memory without a connection string

  Scenario: Provide conversation memory adapter for session context
    When I request conversation memory for session "memory-session" with custom keys
    Then the conversation adapter should expose the custom keys
    And the conversation adapter should share the existing memory service

  Scenario: Clear session memory and forget history
    Given I stored a chat message in session "memory-session"
    When I clear the session "memory-session" memory with forget history
    Then the session "memory-session" memory should be removed and emptied
