@tdd_issue @tdd_issue_2592 @tdd_bug @tdd_bug_2592 @mock_only @tdd_issue_4178
Feature: TDD Issue #2592 — ToolRunner.execute() 6-level execution environment precedence chain
  As the tool execution engine
  I want ToolRunner.execute() to apply the correct 6-level precedence chain
  So that override/fallback priority semantics are honoured at tool execution time

  # ── Unit tests: 6-level precedence chain via ToolRunner.execute() ──

  @tdd_issue @tdd_issue_4290
  Scenario: Plan override wins over devcontainer (Level 1 beats Level 3)
    Given a tool runner with a devcontainer-instance linked resource
    When I execute the tool with plan_env "host" and plan_priority "override"
    Then the resolved execution environment should be "host"

  @tdd_issue @tdd_issue_4290
  Scenario: Project override wins over devcontainer (Level 2 beats Level 3)
    Given a tool runner with a devcontainer-instance linked resource
    When I execute the tool with project_env "host" and project_priority "override"
    Then the resolved execution environment should be "host"

  @tdd_issue @tdd_issue_4290
  Scenario: Devcontainer wins over plan fallback (Level 3 beats Level 4)
    Given a tool runner with a devcontainer-instance linked resource
    When I execute the tool with plan_env "host" and plan_priority "fallback"
    Then the resolved execution environment should be "container"

  @tdd_issue @tdd_issue_4290
  Scenario: Devcontainer wins over project fallback (Level 3 beats Level 5)
    Given a tool runner with a devcontainer-instance linked resource
    When I execute the tool with project_env "host" and project_priority "fallback"
    Then the resolved execution environment should be "container"

  @tdd_issue @tdd_issue_4290
  Scenario: Plan fallback used when no devcontainer (Level 4)
    Given a tool runner with no linked resources
    When I execute the tool with plan_env "container" and plan_priority "fallback"
    Then the resolved execution environment should be "container"

  @tdd_issue @tdd_issue_4290
  Scenario: Project fallback used when no devcontainer and no plan env (Level 5)
    Given a tool runner with no linked resources
    When I execute the tool with project_env "container" and project_priority "fallback"
    Then the resolved execution environment should be "container"

  @tdd_issue @tdd_issue_4290
  Scenario: Host default when nothing configured (Level 6)
    Given a tool runner with no linked resources
    When I execute the tool with no environment configuration
    Then the resolved execution environment should be "host"

  @tdd_issue @tdd_issue_4290
  Scenario: Plan override beats project override (Level 1 beats Level 2)
    Given a tool runner with no linked resources
    When I execute the tool with plan_env "host" plan_priority "override" project_env "container" project_priority "override"
    Then the resolved execution environment should be "host"

  # ── Integration tests: override vs fallback scenarios ──

  @tdd_issue @tdd_issue_4290
  Scenario: Override bypasses devcontainer auto-detection
    Given a tool runner with a devcontainer-instance linked resource
    When I execute the tool with plan_env "host" and plan_priority "override"
    Then the resolved execution environment should be "host"
    And the devcontainer was not used

  @tdd_issue @tdd_issue_4290
  Scenario: Fallback defers to devcontainer when present
    Given a tool runner with a devcontainer-instance linked resource
    When I execute the tool with plan_env "host" and plan_priority "fallback"
    Then the resolved execution environment should be "container"
    And the devcontainer was used

  @tdd_issue @tdd_issue_4290
  Scenario: Fallback uses configured env when no devcontainer present
    Given a tool runner with no linked resources
    When I execute the tool with plan_env "host" and plan_priority "fallback"
    Then the resolved execution environment should be "host"

  @tdd_issue @tdd_issue_4290
  Scenario: Default priority is fallback — devcontainer wins over unconfigured plan env
    Given a tool runner with a devcontainer-instance linked resource
    When I execute the tool with plan_env "host" and no explicit priority
    Then the resolved execution environment should be "container"
