@clone-into
Feature: container-instance --clone-into argument
  As a CleverAgents user
  I want to clone a git repository into a running container
  So that I can work with the repository inside the container environment

  # ── URL validation ─────────────────────────────────────────

  Scenario Outline: validate_clone_into_url accepts valid git URLs
    When I validate clone-into URL "<url>"
    Then the clone-into URL should be valid

    Examples:
      | url                                          |
      | https://github.com/org/repo.git              |
      | http://internal.example.com/repo.git         |
      | git@github.com:org/repo.git                  |
      | ssh://git@bitbucket.org/org/repo.git         |
      | git://github.com/org/repo.git                |
      | /local/path/to/repo                          |
      | ./relative/repo                              |

  Scenario Outline: validate_clone_into_url rejects invalid URLs
    When I validate clone-into URL "<url>"
    Then the clone-into URL should be invalid

    Examples:
      | url                  |
      | not-a-url            |
      | ftp://example.com    |

  # ── clone_repo_into_container argument validation ──────────

  Scenario: clone_repo_into_container rejects empty container_id
    When I call clone_repo_into_container with empty container_id
    Then it should raise ValueError mentioning "container_id"

  Scenario: clone_repo_into_container rejects empty repo_url
    When I call clone_repo_into_container with empty repo_url
    Then it should raise ValueError mentioning "repo_url"

  # ── Successful clone ────────────────────────────────────────

  Scenario: clone_repo_into_container succeeds when docker exec returns 0
    Given a mock docker exec that succeeds for clone
    When I clone "https://github.com/org/repo.git" into container "abc123def456"
    Then the clone should succeed
    And the clone target should be "/workspace"

  Scenario: clone_repo_into_container uses custom target directory
    Given a mock docker exec that succeeds for clone
    When I clone repo "https://github.com/org/repo.git" into container "abc123def456" with target "/custom/dir"
    Then the clone should succeed
    And the clone target should be "/custom/dir"

  # ── Failure handling ────────────────────────────────────────

  Scenario: clone_repo_into_container raises CloneIntoError when docker exec fails
    Given a mock docker exec that fails for clone with stderr "fatal: repository not found"
    When I clone "https://github.com/org/missing.git" into container "abc123def456"
    Then the clone should raise CloneIntoError
    And the CloneIntoError should mention "repository not found"

  # ── CloneIntoError attributes ──────────────────────────────

  Scenario: CloneIntoError stores repo_url, container_id, and stderr
    Given a CloneIntoError with repo "https://example.com/repo.git" container "ctr-001" stderr "auth failed"
    Then the CloneIntoError repo_url should be "https://example.com/repo.git"
    And the CloneIntoError container_id should be "ctr-001"
    And the CloneIntoError stderr should be "auth failed"

  # ── container-instance resource type has --clone-into arg ──

  Scenario: container-instance resource type has clone-into CLI argument
    When I look up the "container-instance" resource type spec
    Then the spec should have a CLI argument named "clone-into"
    And the clone-into argument should be of type "string"
    And the clone-into argument should not be required

  # ── validate_clone_into_url edge cases ─────────────────────

  Scenario: validate_clone_into_url rejects whitespace-only string
    When I validate clone-into URL with whitespace only
    Then the clone-into URL should be invalid

  # -- End-to-end handler wiring --

  Scenario: DevcontainerHandler.resolve() calls clone_repo_into_container when clone_into property is set
    Given a devcontainer-instance resource with clone_into "https://github.com/org/repo.git" and tracker container_id "abc123def456"
    And clone_repo_into_container is mocked to succeed
    When DevcontainerHandler.resolve() is called for the resource
    Then clone_repo_into_container should have been called with container_id "abc123def456" and url "https://github.com/org/repo.git"
