Feature: Execution environment routing
  As the tool execution engine
  I need to route tool execution to host or container environments
  With a priority chain: tool > plan > project > default (host)

  # ── ExecutionEnvironment enum ───────────────────────────────────────

  Scenario: ExecutionEnvironment enum has HOST value
    Given I import ExecutionEnvironment
    Then the HOST value should be "host"

  Scenario: ExecutionEnvironment enum has CONTAINER value
    Given I import ExecutionEnvironment
    Then the CONTAINER value should be "container"

  Scenario: ExecutionEnvironment is a StrEnum
    Given I import ExecutionEnvironment
    Then ExecutionEnvironment should be a StrEnum subclass

  # ── Plan model field ────────────────────────────────────────────────

  Scenario: Plan model has execution_environment field defaulting to None
    Given I create a plan with default fields
    Then the plan execution_environment should be None

  Scenario: Plan model accepts execution_environment host
    Given I create a plan with execution_environment "host"
    Then the plan execution_environment should be "host"

  Scenario: Plan model accepts execution_environment container
    Given I create a plan with execution_environment "container"
    Then the plan execution_environment should be "container"

  # ── ContextConfig model field ───────────────────────────────────────

  Scenario: ContextConfig has execution_environment field defaulting to None
    Given I create a ContextConfig with default fields
    Then the context config execution_environment should be None

  Scenario: ContextConfig accepts execution_environment host
    Given I create a ContextConfig with execution_environment "host"
    Then the context config execution_environment should be "host"

  Scenario: ContextConfig accepts execution_environment container
    Given I create a ContextConfig with execution_environment "container"
    Then the context config execution_environment should be "container"

  # ── Resolver: default resolution ────────────────────────────────────

  Scenario: Resolver defaults to host when nothing is set
    Given I create an ExecutionEnvironmentResolver
    When I resolve with no overrides
    Then the resolved environment should be "host"

  Scenario: Resolver uses explicit default
    Given I create an ExecutionEnvironmentResolver
    When I resolve with default "container"
    Then the resolved environment should be "container"

  # ── Resolver: priority chain ────────────────────────────────────────

  Scenario: Project-level overrides default
    Given I create an ExecutionEnvironmentResolver
    When I resolve with project_env "container"
    Then the resolved environment should be "container"

  Scenario: Plan-level overrides project-level
    Given I create an ExecutionEnvironmentResolver
    When I resolve with plan_env "host" and project_env "container"
    Then the resolved environment should be "host"

  Scenario: Tool-level overrides plan-level
    Given I create an ExecutionEnvironmentResolver
    When I resolve with tool_env "container" and plan_env "host"
    Then the resolved environment should be "container"

  Scenario: Full priority chain tool wins
    Given I create an ExecutionEnvironmentResolver
    When I resolve the full chain with tool_env "container" and plan_env "host" and project_env "host"
    Then the resolved environment should be "container"

  # ── Resolver: validation ────────────────────────────────────────────

  Scenario: Resolver raises ValueError on invalid environment
    Given I create an ExecutionEnvironmentResolver
    When I resolve with tool_env "invalid_env"
    Then exec-env a ValueError should be raised

  # ── Container availability ──────────────────────────────────────────

  Scenario: validate_container_available succeeds with container resource
    Given I create an ExecutionEnvironmentResolver
    When I validate container available with types "devcontainer-instance,git-checkout"
    Then validation should succeed

  Scenario: validate_container_available raises when no container resource
    Given I create an ExecutionEnvironmentResolver
    When I validate container available with types "git-checkout,fs-directory"
    Then a ContainerUnavailableError should be raised

  Scenario: resolve_and_validate passes for host
    Given I create an ExecutionEnvironmentResolver
    When I resolve_and_validate with project_env "host" and types "git-checkout"
    Then the resolved environment should be "host"

  Scenario: resolve_and_validate raises for container without resource
    Given I create an ExecutionEnvironmentResolver
    When I resolve_and_validate with plan_env "container" and types "git-checkout"
    Then a ContainerUnavailableError should be raised

  Scenario: resolve_and_validate passes for container with resource
    Given I create an ExecutionEnvironmentResolver
    When I resolve_and_validate with plan_env "container" and types "devcontainer-instance"
    Then the resolved environment should be "container"

  # ── ToolRunner integration ──────────────────────────────────────────

  Scenario: ToolRunner routes to host by default
    Given I create a ToolRunner with a mock registry
    When I execute a tool with no environment overrides
    Then the tool should execute successfully on host

  Scenario: ToolRunner returns error for container without resource
    Given I create a ToolRunner with a mock registry
    When I execute a tool with plan_env "container" and no container resources
    Then the tool result should have an error about container unavailable

  # ── Container resource types ────────────────────────────────────────

  Scenario: CONTAINER_RESOURCE_TYPES includes expected types
    Given I import CONTAINER_RESOURCE_TYPES
    Then the container types should contain "container-instance"
    And the container types should contain "devcontainer-instance"
    And the container types should not contain "devcontainer-file"
    And the container types should not contain "git-checkout"

  # ── ContainerUnavailableError ───────────────────────────────────────

  Scenario: ContainerUnavailableError includes project name
    When I create a ContainerUnavailableError for project "my-project"
    Then exec-env the error message should contain "my-project"
    And exec-env the error message should contain "Container resource unavailable"

  Scenario: ContainerUnavailableError without project name
    When I create a ContainerUnavailableError without project name
    Then exec-env the error message should contain "Container resource unavailable"
    And exec-env the error message should not contain "for project"
