*** Settings ***
Documentation    Integration tests for Fix-then-Revalidate orchestration loop
Library          Process
Library          OperatingSystem
Resource         ${CURDIR}/common.resource
Suite Setup      Setup Test Environment
Suite Teardown   Cleanup Test Environment

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

*** Test Cases ***
Fix Then Revalidate Models Are Valid
    [Documentation]    Verify FixAttemptRecord and FixThenRevalidateResult Pydantic models
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    models    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    models-ok

Fix Succeeds On First Attempt
    [Documentation]    Fix loop resolves validation on first fix attempt
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    fix_first    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    fix-first-ok

Fix Succeeds After Multiple Attempts
    [Documentation]    Fix loop resolves validation after multiple fix attempts
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    fix_multiple    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    fix-multiple-ok

Terminal Failure When All Attempts Exhausted
    [Documentation]    Plan reaches terminal failure when retries exhausted
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    terminal_failure    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    terminal-failure-ok

Escalation To Strategy Revision
    [Documentation]    Loop escalates to strategy revision when auto_strategy_revision enabled
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    escalation    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    escalation-ok

Informational Validations Skipped
    [Documentation]    Informational validations do not trigger fix loop
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    informational_skipped    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    informational-skipped-ok

Retry Counting Per Validation Per Plan
    [Documentation]    Verify retry counting and reset per validation per plan
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    retry_counting    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    retry-counting-ok

Unknown Command Returns Error
    [Documentation]    Running with unknown command exits with code 1
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    nonexistent_cmd    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    1
    Should Contain    ${result.stderr}    Unknown command
