*** Settings ***
Documentation    End-to-end tests for phase reversion state machine
Resource         ${CURDIR}/common.resource
Suite Setup      Setup Test Environment
Suite Teardown   Cleanup Test Environment

*** Variables ***
${HELPER_SCRIPT}    robot/helper_phase_reversion.py

*** Test Cases ***
Manual Reversion From Execute To Strategize
    [Documentation]    Revert a plan from Execute to Strategize manually
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    manual_revert    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    reversion-manual-ok

Auto Reversion From Constrained Apply
    [Documentation]    Automatically revert when Apply is constrained and profile permits
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    auto_revert_apply    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    reversion-auto-apply-ok

Loop Guard Blocks Excessive Reversions
    [Documentation]    Verify the max 3 reversions guard
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    loop_guard    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    loop-guard-ok

Profile Gated Auto Reversion
    [Documentation]    Manual profile blocks auto-reversion
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    profile_gate    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    profile-gate-ok

Terminal Plan Cannot Be Reverted
    [Documentation]    Applied plan cannot be reverted
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    terminal_block    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    terminal-block-ok

Reversion Preserves Context
    [Documentation]    Arguments and project links survive reversion
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    context_preserved    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    context-preserved-ok

Decision Recording On Reversion
    [Documentation]    Each reversion is logged as a decision
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    decision_recording    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    decision-recording-ok

Can Revert To Validation
    [Documentation]    Validate allowed reversion paths
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    can_revert_to    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    can-revert-to-ok
