*** Settings ***
Documentation     End-to-end tests for cross-plan correction cascading:
...               cancel, cancel+rollback, rejection, and atomic cascade.
Resource          ${CURDIR}/common.resource
Suite Setup       Setup Test Environment
Suite Teardown    Cleanup Test Environment

*** Variables ***
${HELPER}    robot/helper_cross_plan_correction.py

*** Test Cases ***
Cancel Not-Started Child Plan
    [Documentation]    Cascade cancels a not-yet-started child plan
    [Tags]    phase2    correction    cross-plan    cancel
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    cancel-not-started    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    cancel-not-started-ok

Cancel And Rollback In-Progress Child Plan
    [Documentation]    Cascade cancels and rolls back an in-progress child plan
    [Tags]    phase2    correction    cross-plan    rollback
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    cancel-rollback-in-progress    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    cancel-rollback-in-progress-ok

Cancel And Rollback Completed Unapplied Child Plan
    [Documentation]    Cascade cancels and rolls back a completed-unapplied child plan
    [Tags]    phase2    correction    cross-plan    rollback
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    cancel-rollback-completed    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    cancel-rollback-completed-ok

Reject Applied Child Plan
    [Documentation]    Cascade rejects correction when child plan is already applied
    [Tags]    phase2    correction    cross-plan    rejection
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    reject-applied    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    reject-applied-ok

Mixed States Rejection
    [Documentation]    Reject correction when any child plan is applied, even if others are cancellable
    [Tags]    phase2    correction    cross-plan    rejection    mixed
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    mixed-states-reject    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    mixed-states-reject-ok

Atomic Cascade Failure Rollback
    [Documentation]    Failure during cascade rolls back all completed actions
    [Tags]    phase2    correction    cross-plan    atomic
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    atomic-failure    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    atomic-failure-ok

Execute Correction With Cascade No Children
    [Documentation]    Correction with cascade and no child plans succeeds normally
    [Tags]    phase2    correction    cross-plan    integration
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    cascade-no-children    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    cascade-no-children-ok

Execute Correction With Cascade Rejection
    [Documentation]    Correction with cascade rejects when child plan is applied
    [Tags]    phase2    correction    cross-plan    integration    rejection
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    cascade-rejection    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    cascade-rejection-ok
