*** Settings ***
Documentation    E2E test for Workflow Example 18: Container with Remote Repo
...    Clone (trusted profile).
...
...    Exercises resource registration with container-instance and
...    --clone-into flag for remote repo clone, two-step project
...    creation and linking, action with trusted automation profile,
...    plan-level execution environment with fallback priority, and
...    full plan lifecycle including apply with container commit/push.
...
...    Zero mocking — real CLI, real LLM API keys.
Resource    common_e2e.resource
Suite Setup    WF18 Suite Setup
Suite Teardown    E2E Suite Teardown
Force Tags       E2E

*** Variables ***
${ACTION_PREFIX}    local/wf18-clone-action
${RESOURCE_PREFIX}    local/wf18-clone-res
${PROJECT_PREFIX}    local/wf18-clone-proj

*** Keywords ***
WF18 Suite Setup
  [Documentation]    E2E Suite Setup plus unique suffix generation and dynamic
  ...    actor selection for WF18 tests.
  [Tags]    tdd_issue tdd_issue_4188
  E2E Suite Setup
  # Initialise the workspace so CLI commands work.
  ${init}=    Run CleverAgents Command    init --force --yes
  Should Be Equal As Integers    ${init.rc}    0
  # Generate a unique suffix for resource/project names to avoid UNIQUE
  # constraint collisions on repeated E2E runs or parallel CI.
  ${suffix}=    Evaluate    __import__('uuid').uuid4().hex[:12]
  Set Suite Variable    ${RUN_SUFFIX}    ${suffix}
  # Pick an actor that matches the available API key.
  ${has_anthropic}=    Evaluate    bool(__import__('os').environ.get('ANTHROPIC_API_KEY', ''))
  IF    ${has_anthropic}
  ${actor}=    Set Variable    anthropic/claude-sonnet-4-20250514
  ELSE
  ${actor}=    Set Variable    openai/gpt-4o
  END
  Set Suite Variable    ${LLM_ACTOR}    ${actor}
  # Compute unique action name using the run suffix for parallel CI safety.
  ${action_name}=    Set Variable    ${ACTION_PREFIX}-${RUN_SUFFIX}
  Set Suite Variable    ${ACTION_NAME}    ${action_name}

Create Remote Clone Repo
  [Documentation]    Create a temp git repo simulating a project that would
  ...    be cloned from a remote repository into a container.
  ${repo}=    Create Temp Git Repo wf18-remote-clone-${RUN_SUFFIX}
  Create Directory    ${repo}${/}src
  Create Directory    ${repo}${/}deploy
  ${app_content}=    Catenate    SEPARATOR=\n
  ...    """Application for remote deployment via container clone."""
  ...    ${EMPTY}
  ...    ${EMPTY}
  ...    class DeploymentManager:
  ...    ${SPACE}${SPACE}${SPACE}${SPACE}"""Manages deployment lifecycle."""
  ...    ${EMPTY}
  ...    ${SPACE}${SPACE}${SPACE}${SPACE}def __init__(self, repo_url, branch="main"):
  ...    ${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}self.repo_url = repo_url
  ...    ${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}self.branch = branch
  ...    ${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}self.deployed = False
  ...    ${EMPTY}
  ...    ${SPACE}${SPACE}${SPACE}${SPACE}def deploy(self):
  ...    ${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}"""Execute deployment."""
  ...    ${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}self.deployed = True
  ...    ${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}return {"status": "deployed", "branch": self.branch}
  Create File    ${repo}${/}src${/}deploy_manager.py    ${app_content}
  ${deploy_config}=    Catenate    SEPARATOR=\n
  ...    # Deployment configuration
  ...    CONTAINER_IMAGE=python:3.12-slim
  ...    CLONE_DEPTH=1
  ...    DEPLOY_TIMEOUT=300
  ...    HEALTH_CHECK_INTERVAL=10
  Create File    ${repo}${/}deploy${/}config.env    ${deploy_config}
  # NOTE: This Dockerfile is fixture data only — never built by this test.
  # It simulates a project that would be cloned into a container.
  ${dockerfile}=    Catenate    SEPARATOR=\n
  ...    FROM python:3.12-slim
  ...    RUN apt-get update && apt-get install -y git
  ...    WORKDIR /workspace
  ...    ARG REPO_URL
  ...    RUN git clone --depth 1 \${REPO_URL} .
  ...    CMD ["python", "-m", "src.deploy_manager"]
  Create File    ${repo}${/}Dockerfile    ${dockerfile}
  Create File    ${repo}${/}src${/}__init__.py    \n
  ${git_add}=    Run Process    git    add    .    cwd=${repo}
  Should Be Equal As Integers    ${git_add.rc}    0    msg=Fixture git add failed: ${git_add.stderr}
  ${git_commit}=    Run Process    git    commit    -m    Initial remote-clone container project    cwd=${repo}
  Should Be Equal As Integers    ${git_commit.rc}    0    msg=Fixture git commit failed: ${git_commit.stderr}
  RETURN    ${repo}

*** Test Cases ***
WF18 Container With Remote Repo Clone Trusted Profile
  [Documentation]    Trusted-profile workflow: container-instance resource with
  ...    --clone-into for remote repo clone, two-step project setup,
  ...    plan-level execution environment with fallback priority, and
  ...    full plan lifecycle including apply with container commit/push.
  [Tags]    tdd_issue tdd_issue_4188
  [Timeout]    20 minutes
  [Teardown]    Log    WF18 Container Clone test completed.
  Skip If No LLM Keys













