*** Settings ***
Documentation    E2E test for Workflow Example 12: Large-Scale Hierarchical Feature
...    Implementation (supervised profile).
...
...    Expert-level scenario building a notification system across 4
...    projects with hierarchical plan decomposition, error recovery
...    via plan correct, and phased apply.
...
...    Zero mocking — real CLI, real LLM API keys.
...
...    ``Skip If No LLM Keys`` ensures graceful degradation in keyless
...    CI environments.
Resource    common_e2e.resource
Suite Setup    WF12 Suite Setup
Suite Teardown    E2E Suite Teardown
Force Tags       E2E

*** Keywords ***
WF12 Suite Setup
  [Documentation]    E2E Suite Setup plus workspace init and unique run suffix for name isolation.
  [Tags]    tdd_issue    tdd_issue_4188
  E2E Suite Setup
  # Initialise the database so plan/resource/project commands work in all tests.
  # Use --force because the workspace may already contain an initialised project.
  ${init}=    Run CleverAgents Command    init --force --yes
  Should Be Equal As Integers    ${init.rc}    0
  # Generate a unique suffix to avoid UNIQUE constraint collisions on
  # repeated E2E runs against the same database (parallel CI safety).
  ${suffix}=    Evaluate    __import__('uuid').uuid4().hex[:12]
  Set Suite Variable    ${RUN_SUFFIX}    ${suffix}
  # Derive run-unique resource/project/action names.
  Set Suite Variable    ${ACTION_NAME}    local/wf12-notifications-${suffix}
  Set Suite Variable    ${PROTOS_RES}    local/wf12-protos-res-${suffix}
  Set Suite Variable    ${API_RES}    local/wf12-api-res-${suffix}
  Set Suite Variable    ${WORKER_RES}    local/wf12-worker-res-${suffix}
  Set Suite Variable    ${FRONTEND_RES}    local/wf12-frontend-res-${suffix}
  Set Suite Variable    ${PROTOS_PROJ}    local/wf12-protos-${suffix}
  Set Suite Variable    ${API_PROJ}    local/wf12-api-${suffix}
  Set Suite Variable    ${WORKER_PROJ}    local/wf12-worker-${suffix}
  Set Suite Variable    ${FRONTEND_PROJ}    local/wf12-frontend-${suffix}

Create Project Repo
  [Documentation]    Create a temp git repo for a project component.
  ...    Returns the path to the created repository.
  [Arguments]    ${name}    ${content}
  ${repo}=    Create Temp Git Repo wf12-${name}-${RUN_SUFFIX}
  Create Directory    ${repo}${/}src
  Create File    ${repo}${/}src${/}main.py    ${content}
  ${git_add}=    Run Process    git    add    .    cwd=${repo}    timeout=60s    on_timeout=kill
  Should Be Equal As Integers    ${git_add.rc}    0    msg=git add failed for ${name}: ${git_add.stderr}
  ${git_commit}=    Run Process    git    commit    -m    Initial ${name}    cwd=${repo}    timeout=60s    on_timeout=kill
  Should Be Equal As Integers    ${git_commit.rc}    0    msg=git commit failed for ${name}: ${git_commit.stderr}
  RETURN    ${repo}

Register Project With Invariant
  [Documentation]    Register resource, create project with an invariant.
  ...    Note: Spec shows most projects with 2 invariants. This keyword
  ...    accepts 1 invariant per project as a simplification. The invariant
  ...    registration path is exercised identically regardless of count.
  ...    TODO: Add a second invariant per project once the full invariant
  ...    matrix is stable.
  [Arguments]    ${res_name}    ${proj_name}    ${repo_dir}    ${invariant_text}
  ${branch_result}=    Run Process    git    rev-parse    --abbrev-ref    HEAD    cwd=${repo_dir}    timeout=60s    on_timeout=kill
  Should Be Equal As Integers    ${branch_result.rc}    0    msg=git rev-parse failed: ${branch_result.stderr}
  ${branch}=    Strip String    ${branch_result.stdout}
  # Register resource
  ${r}=    Run CleverAgents Command
  ...    resource add git-checkout ${res_name}
  ...    --path ${repo_dir} --branch ${branch}
  ...    --format json
  Should Not Contain    ${r.stdout}${r.stderr} Traceback
  Should Not Contain    ${r.stdout}${r.stderr} INTERNAL
  Output Should Contain    ${r}    ${res_name}
  # Create project with invariant (spec requires per-project invariants)
  ${p}=    Run CleverAgents Command
  ...    project create ${proj_name}
  ...    --resource ${res_name}
  ...    --invariant ${invariant_text}
  ...    --format json
  Should Not Contain    ${p.stdout}${p.stderr} Traceback
  Should Not Contain    ${p.stdout}${p.stderr} INTERNAL
  Output Should Contain    ${p}    ${proj_name}

Verify Plan In List
  [Documentation]    Verify a plan appears in list output.
  [Arguments]    ${plan_id}
  ${list_result}=    Run CleverAgents Command    plan list --format json expected_rc=None timeout=120s
  Should Be Equal As Integers    ${list_result.rc}    0    msg=list failed (rc=${list_result.rc}): ${list_result.stderr}
  Output Should Contain    ${list_result}    ${plan_id}

Select Non Root Decision Id
  [Documentation]    Parse JSON tree output and select a non-root decision ID
  ...    suitable for correction. Uses a targeted regex to extract
  ...    only values from ``"decision_id"`` fields (avoid matching
  ...    plan_id / resource_id). Requires at least 2 decision IDs to
  ...    guarantee the returned ID is not the root.
  [Arguments]    ${tree_stdout}
  # Targeted regex — only match values from "decision_id" JSON fields
  # Crockford Base32 character class: excludes I, L, O, U
  ${all_ids}=    Get Regexp Matches ${tree_stdout} "decision_id"\\s*:\\s*"([0-9A-HJKMNP-TV-Z]{26})" 1
  # Guard: need at least 2 decision IDs (root + at least one child)
  ${id_count}=    Get Length ${all_ids}
  Should Be True    ${id_count} >= 2
  ...    Need ≥2 decision IDs to select non-root, found ${id_count}
  # Use the last ID found — in JSON serialization of the tree, leaf/child
  # decisions appear after the root prompt_definition decision.
  ${last_index}=    Evaluate    len($all_ids) - 1
  ${decision_id}=    Set Variable    ${all_ids}[${last_index}]
  # Defensive check: ensure selected ID differs from first (presumed root)
  # to guard against JSON serialization order assumptions.
  Should Not Be Equal    ${decision_id}    ${all_ids}[0]    msg=Selected non-root decision ID should differ from first ID (presumed root)
  RETURN    ${decision_id}

*** Test Cases ***
WF12 Large Scale Hierarchical Feature Implementation
  [Documentation]    Supervised-profile workflow: 4-project notification system
  ...    with hierarchical decomposition, user guidance via plan
  ...    correct (append mode), and dependency-ordered apply.
  ...
  ...    Note: ``plan prompt`` (spec Step 4 — supervised-profile
  ...    user intervention) is not yet implemented as a CLI command.
  ...    Once available, this test should add a ``plan prompt`` step
  ...    after tree inspection to provide user guidance and verify
  ...    the ``user_intervention`` decision is created.
  [Tags]    tdd_issue    tdd_issue_4188    tdd_expected_fail
  [Timeout]    35 minutes
  # TDD placeholder - implementation pending
  Fail    WF12 hierarchical decomposition not yet implemented (TDD placeholder)

















