Feature: Plan Generation Graph Uncovered Lines Coverage
  As a developer
  I want comprehensive test coverage for edge cases in PlanGenerationGraph
  So that I can ensure all code paths are exercised

  Background:
    Given the langgraph plan generation module is importable

  # Testing line 90: else branch when LLM is provided
  Scenario: PlanGenerationGraph can be instantiated with custom LLM
    Given I have a mock custom LLM instance
    When I create a langgraph PlanGenerationGraph with the custom LLM
    Then the langgraph graph should use the custom LLM
    And the langgraph graph should not use FakeListLLM

  # Testing lines 250-254: Exception handling in analyze_requirements
  Scenario: Analyze requirements handles LLM exceptions gracefully
    Given I have a langgraph PlanGenerationGraph instance with failing LLM
    And I have a langgraph PlanGenerationState with prompt and contexts
    When I execute the langgraph analyze_requirements node with the failing state
    Then the uncovered analyzed_requirements should be empty
    And the langgraph error should contain "Requirements analysis failed"

  # Testing lines 297-305: Create operation with different prompt keywords
  Scenario: Generate plan creates test file when prompt contains test
    Given I have a langgraph PlanGenerationGraph instance
    And I have a langgraph state with prompt "create test cases for validation"
    When I execute the langgraph generate_plan node with test prompt
    Then the langgraph generated file path should be "test_generated.py"
    And the langgraph operation type should be CREATE

  # Testing lines 298-301: Create operation for error handling file
  Scenario: Generate plan creates error handler when prompt mentions error
    Given I have a langgraph PlanGenerationGraph instance
    And I have a langgraph state with prompt "add error handling"
    When I execute the langgraph generate_plan node with error prompt
    Then the langgraph generated file path should be "error_handler.py"
    And the langgraph operation type should be CREATE

  # Testing lines 298-301: Create operation for exception handling file
  Scenario: Generate plan creates error handler when prompt mentions exception
    Given I have a langgraph PlanGenerationGraph instance
    And I have a langgraph state with prompt "implement exception handling"
    When I execute the langgraph generate_plan node with exception prompt
    Then the langgraph generated file path should be "error_handler.py"
    And the langgraph operation type should be CREATE

  # Testing line 303: Default file path for create operation
  Scenario: Generate plan creates default file when prompt has no keywords
    Given I have a langgraph PlanGenerationGraph instance
    And I have a langgraph state with prompt "add new feature"
    When I execute the langgraph generate_plan node with generic prompt
    Then the langgraph generated file path should be "generated.py"
    And the langgraph operation type should be CREATE

  # Testing lines 329-333: Exception handling in generate_plan
  Scenario: Generate plan handles LLM exceptions gracefully
    Given I have a langgraph PlanGenerationGraph instance with failing LLM
    And I have a langgraph state with valid requirements
    When I execute the langgraph generate_plan node with failing LLM
    Then the langgraph generated_changes should be empty
    And the langgraph error should contain "Code generation failed"

  # Testing lines 380-386: Exception handling in validate
  Scenario: Validate node handles LLM exceptions gracefully
    Given I have a langgraph PlanGenerationGraph instance with failing LLM
    And I have a langgraph state with generated changes
    When I execute the langgraph validate node with failing LLM
    Then the langgraph validation status should be "FAIL"
    And the langgraph validation message should contain "Validation failed"

  # Testing lines 483-498: Async invoke method
  Scenario: Workflow ainvoke method returns complete state asynchronously
    Given I have a langgraph PlanGenerationGraph instance
    And I have langgraph workflow inputs with project plan and contexts
    When I invoke the langgraph workflow asynchronously
    Then the langgraph async workflow result should contain all expected fields
    And the langgraph async result should have generated_changes
    And the langgraph async result should have validation_result

  # Testing line 403: Retry count increment in should_retry
  Scenario: Should retry increments retry count correctly
    Given I have a langgraph PlanGenerationGraph instance with max_retries 3
    And I have a langgraph state with retry_count 1
    When I check uncovered langgraph should_retry with FAIL validation and retry_count 1
    Then the uncovered langgraph state retry_count should be incremented to 2
    And the langgraph retry decision should be "retry"

  # Testing full workflow with retry logic
  Scenario: Workflow retries on validation failure up to max attempts
    Given I have a langgraph PlanGenerationGraph instance with max_retries 2
    And I have langgraph workflow inputs that will fail validation initially
    When I invoke the langgraph workflow with retry scenario
    Then the langgraph workflow should have performed at least one retry

  # Testing validation with short content that fails length check
  Scenario: Validate node fails when generated code is too short
    Given I have a langgraph PlanGenerationGraph instance with strict validation
    And I have a langgraph state with minimal generated changes
    When I execute the langgraph validate node with short content
    Then the langgraph validation might fail based on content and LLM response
    And the langgraph validation result should have a status field

  # Testing stream method with multiple events
  Scenario: Workflow stream yields events from all nodes
    Given I have a langgraph PlanGenerationGraph instance
    And I have langgraph workflow inputs with project plan and contexts
    When I stream the langgraph workflow execution
    Then the langgraph stream should yield events from load_context
    And the langgraph stream should yield events from analyze_requirements
    And the langgraph stream should yield events from generate_plan
    And the langgraph stream should yield events from validate

  # Testing format_context_summary edge cases
  Scenario: Format context summary handles contexts with no content
    Given I have a langgraph PlanGenerationGraph instance
    When I format the langgraph context summary with empty content contexts
    Then the langgraph summary should handle None content gracefully
    And the langgraph summary should include file paths

  # Testing modify operation path
  Scenario: Generate plan modifies existing file when contexts provided
    Given I have a langgraph PlanGenerationGraph instance
    And I have a langgraph state with existing context file
    When I execute the langgraph generate_plan node for modification
    Then the langgraph operation type should be MODIFY
    And the langgraph generated file path should match context path
    And the langgraph change should have original_content from context

  # Testing lines 260-262: chain passthrough when no retry support
  Scenario: Chain without retry support is returned unchanged
    Given I have a langgraph PlanGenerationGraph instance
    When I wrap a langgraph chain without retry support
    Then the langgraph chain wrapper should return the original chain

  # Testing lines 413-431: explicit file path handling
  Scenario: Generate plan honors explicit existing file path
    Given I have a langgraph PlanGenerationGraph instance
    And I have a langgraph state with explicit path to an existing file
    When I execute the langgraph generate_plan node with explicit path
    Then the langgraph generated file path should match the explicit file
    And the langgraph operation type should be MODIFY
    And the langgraph change should include original file content

  # Testing lines 420-425: explicit directory path handling
  Scenario: Generate plan writes to generated file under explicit directory
    Given I have a langgraph PlanGenerationGraph instance
    And I have a langgraph state with explicit directory path
    When I execute the langgraph generate_plan node with explicit path
    Then the langgraph generated file path should be under the explicit directory
    And the langgraph operation type should be CREATE

  # Testing lines 432-435: unsuffixed explicit path handling
  Scenario: Generate plan appends generated file name to unsuffixed path
    Given I have a langgraph PlanGenerationGraph instance
    And I have a langgraph state with explicit path without a suffix
    When I execute the langgraph generate_plan node with explicit path
    Then the langgraph generated file path should append generated.py
    And the langgraph operation type should be CREATE

  # Testing lines 486-493: matching explicit path to context
  Scenario: Generate plan selects matching context for explicit path hint
    Given I have a langgraph PlanGenerationGraph instance
    And I have a langgraph state with contexts and an explicit path hint
    When I execute the langgraph generate_plan node with explicit path
    Then the langgraph generated change should use the matching context path
    And the langgraph operation type should be MODIFY
    And the langgraph change should have original_content from context
