Feature: Context Service
  As a developer
  I want to manage context files
  So that the AI has the right information

  Scenario: Add single file to context
    Given I have a context service
    And I have a test file "example.py"
    When I add file "example.py" to context
    Then the file should be added to service context
    And the context should contain 1 file

  Scenario: Add directory to context recursively
    Given I have a context service
    And I have a directory with multiple files
    When I add the directory to context recursively
    Then all files should be added to context

  Scenario: Remove file from context
    Given I have a context service
    And I have added "test.py" to the context
    When I remove "test.py" from context
    Then the service context should be empty

  Scenario: Clear all context
    Given I have a context service
    And I have added multiple files to context
    When I clear the context
    Then the service context should be empty

  Scenario: List context files
    Given I have a context service
    And I have added "file1.py" to the context
    And I have added "file2.py" to the context
    When I list the context
    Then I should see 2 files in the list
    And the list should contain "file1.py"
    And the list should contain "file2.py"

  Scenario: Reject context additions without an active plan
    Given I have a context service without a plan
    And I have a test file "orphan.py"
    When I try to add "orphan.py" to context
    Then a plan error should be raised for missing plan

  Scenario: Report duplicates when the same file is added twice
    Given I have a context service
    And I have added "dup.py" to the context
    When I add file "dup.py" to context again
    Then the file should be reported as already in context
    And the context should contain 1 file

  Scenario: Respect non-recursive directory loading
    Given I have a context service
    And I have a directory with nested ignored files
    When I add the directory to context without recursion
    Then 1 top-level file should be added from the directory
    And the nested file "nested.py" should not be added to context
    And the ignored file "ignored.pyc" should not be added to context

  Scenario: Enforce per-file size guardrails
    Given I have a context service
    And the context service max file size is set to 1 bytes
    And I have a test file "large.txt" of size 10 bytes
    When I add existing file "large.txt" to context
    Then no files should be added by the last operation

  Scenario: Enforce total context size guardrails
    Given I have a context service
    And the context service max context size is set to 15 bytes
    And I have a test file "first.txt" of size 10 bytes
    And I have a test file "second.txt" of size 10 bytes
    When I add existing file "first.txt" to context
    And I add existing file "second.txt" to context
    Then only "first.txt" should remain in the context

  Scenario: Skip unreadable files while adding context
    Given I have a context service
    And file reads will fail during addition
    And I have a test file "broken.py"
    When I add existing file "broken.py" to context
    Then no files should be added by the last operation
    And the context should contain 0 files

  Scenario: Remove context entries by directory path
    Given I have a context service
    And I have a directory with multiple files
    When I add the directory to context recursively
    And I remove directory "subdir" from context
    Then the service context should be empty

  Scenario: Resolve current project when listing files
    Given I have a context service
    And I have added "listed.py" to the context
    And the container project service returns the current test project
    When I list context files without providing a project
    Then I should receive 1 context file path
    And the list of file paths should include "listed.py"

  Scenario: Return empty results when container lookup fails
    Given I have a context service
    And the container lookup for projects fails
    When I list context files without providing a project
    Then I should receive 0 context file paths

  Scenario: Ignore configured patterns when scanning directories
    Given I have a context service
    And I have a directory with ignored context files
    When I add the directory to context recursively
    And I list the context
    Then the context should contain 1 file
    And the list should contain "keep.txt"
    And the list should not contain "hidden.txt"
    And the list should not contain "ignored.pyc"

  Scenario: Skip unsupported filesystem entries when adding context
    Given I have a context service
    And I have a special filesystem entry "context.pipe"
    When I add special entry "context.pipe" to context
    Then no files should be added by the last operation
    And the context should contain 0 files

  Scenario: Report duplicates when reloading directories recursively
    Given I have a context service
    And I have a directory with multiple files
    When I add the directory to context recursively
    And I add the directory to context recursively again
    Then the last addition should report 2 duplicates

  Scenario: Report duplicates when reloading directories without recursion
    Given I have a context service
    And I have a directory with multiple files
    When I add the directory to context without recursion
    And I add the directory to context without recursion again
    Then the last addition should report 2 duplicates

  Scenario: Skip additions when the current plan is unsaved
    Given I have a context service
    And I have a test file "unsaved.txt"
    When I add "unsaved.txt" with an unsaved plan
    Then no files should be added by the last operation

  Scenario: Ignore dot directories during recursive scans
    Given I have a context service
    And I have a directory with nested ignored files
    When I add the directory to context recursively
    And I list the context
    Then I should see 2 files in the list
    And the context listings should not contain "config"

  Scenario: Removing context without an active plan returns zero
    Given I have a context service without a plan
    When I remove "ghost.py" from context
    Then the removed count should be 0

  Scenario: Clearing context without an active plan returns zero
    Given I have a context service without a plan
    When I clear the context and record the removed count
    Then the recorded removal count should be 0

  Scenario: Listing context without a project identifier returns empty
    Given I have a context service
    And the test project loses its identifier
    When I list the context
    Then I should see 0 files in the list

  Scenario: Listing context without an active plan returns empty
    Given I have a context service without a plan
    When I list the context
    Then I should see 0 files in the list

  Scenario: Show and fetch stored context content
    Given I have a context service
    And I have added "content.py" to the context
    When I show context content
    Then the shown context content should include "content.py"
    And fetching context content for "content.py" should return stored content
    And fetching context content for "missing.py" should return nothing

  Scenario: List context files with an explicit project
    Given I have a context service
    And I have added "listed.py" to the context
    When I list context files for the current project
    Then I should receive 1 context file path
    And the list of file paths should include "listed.py"

  Scenario: List context files when the container returns no project
    Given I have a context service
    And the container project service returns no project
    When I list context files without providing a project
    Then I should receive 0 context file paths
