*** Settings ***
Documentation    M1 source-code plan lifecycle E2E smoke tests via CLI
Resource         ${CURDIR}/common.resource
Suite Setup      Setup Test Environment
Suite Teardown   Cleanup Test Environment

*** Variables ***
${HELPER}    ${CURDIR}/helper_m1_sourcecode_smoke.py

*** Test Cases ***
M1 Action Create From Config
    [Documentation]    Create an action from M1 fixture YAML config
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    action-create    cwd=${WORKSPACE}    timeout=120s    on_timeout=kill
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m1-action-create-ok

M1 Plan Use Creates Strategize Plan
    [Documentation]    Use an action to create a plan in strategize phase
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    plan-use    cwd=${WORKSPACE}    timeout=120s    on_timeout=kill
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m1-plan-use-ok

M1 Plan Use With Project Link
    [Documentation]    Use action with a project argument to validate project linking
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    plan-use-project    cwd=${WORKSPACE}    timeout=120s    on_timeout=kill
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m1-plan-use-project-ok

M1 Plan Execute Transitions Phase
    [Documentation]    Execute a plan and verify phase transitions
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    plan-execute    cwd=${WORKSPACE}    timeout=120s    on_timeout=kill
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m1-plan-execute-ok

M1 Plan Diff Shows Changeset
    [Documentation]    Run plan diff to show changeset
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    plan-diff    cwd=${WORKSPACE}    timeout=120s    on_timeout=kill
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m1-plan-diff-ok

M1 Plan Apply Reaches Terminal
    [Documentation]    Apply a plan and verify terminal state
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    plan-apply    cwd=${WORKSPACE}    timeout=120s    on_timeout=kill
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m1-plan-apply-ok

M1 Full Lifecycle Action To Apply
    [Documentation]    End-to-end: action create -> plan use -> execute -> apply
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    full-lifecycle    cwd=${WORKSPACE}    timeout=120s    on_timeout=kill
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m1-full-lifecycle-ok

M1 Plan Use With Plain Format
    [Documentation]    Verify plan use --format plain stabilises assertions
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    plan-use-plain    cwd=${WORKSPACE}    timeout=120s    on_timeout=kill
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m1-plan-use-plain-ok
