*** Settings ***
Documentation     Integration tests for plan lifecycle model validation:
...               ERRORED terminality, per-phase state constraints, and
...               rejection of invalid phase/state combinations.
Resource          ${CURDIR}/common.resource
Suite Setup       Setup Test Environment
Suite Teardown    Cleanup Test Environment

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

*** Test Cases ***
ERRORED State Is Terminal
    [Documentation]    Verify that a plan with ERRORED processing state is terminal
    [Tags]    lifecycle    plan    model    terminal
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    errored-is-terminal    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    errored-is-terminal-ok

APPLIED Only Valid In APPLY Phase
    [Documentation]    Verify APPLIED state is rejected outside the APPLY phase
    [Tags]    lifecycle    plan    model    validation
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    applied-only-apply    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    applied-only-apply-ok

CONSTRAINED Only Valid In APPLY Phase
    [Documentation]    Verify CONSTRAINED state is rejected outside the APPLY phase
    [Tags]    lifecycle    plan    model    validation
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    constrained-only-apply    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    constrained-only-apply-ok

COMPLETE Only Valid In STRATEGIZE Or EXECUTE
    [Documentation]    Verify COMPLETE state is rejected in APPLY and ACTION phases
    [Tags]    lifecycle    plan    model    validation
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    complete-strategize-execute    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    complete-strategize-execute-ok

QUEUED Valid In Any Phase
    [Documentation]    Verify QUEUED state is accepted in all phases
    [Tags]    lifecycle    plan    model    validation
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    queued-any-phase    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    queued-any-phase-ok

PROCESSING Valid In Any Phase
    [Documentation]    Verify PROCESSING state is accepted in all phases
    [Tags]    lifecycle    plan    model    validation
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    processing-any-phase    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    processing-any-phase-ok

Assignment Order Safety
    [Documentation]    Verify state-first assignment order avoids validator errors
    [Tags]    lifecycle    plan    model    validation    regression
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    assignment-order-safety    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    assignment-order-safety-ok

Deserialization Coercion
    [Documentation]    Verify to_domain coerces legacy invalid phase/state combos
    [Tags]    lifecycle    plan    model    validation    deserialization
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    deserialization-coercion    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    deserialization-coercion-ok

Deserialization Coercion All Branches
    [Documentation]    Verify all three coercion branches in to_domain
    [Tags]    lifecycle    plan    model    validation    deserialization
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    deserialization-coercion-all-branches    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    deserialization-coercion-all-branches-ok
