*** Settings ***
Documentation    Integration test for Workflow Example 7: CI/CD integration,
...              automated PR review and fix (ci profile).
...
...              Validates the CI/CD automation workflow covering: ci-profile
...              configuration, idempotent resource and project registration,
...              validation registration and attachment, plan lifecycle with
...              ci automation profile and phase-by-phase completion through
...              all phases until terminal applied state, and JSON output
...              structure verification.
Library          Process
Resource         ${CURDIR}/common.resource
Suite Setup      Setup Test Environment With Database Isolation
Suite Teardown   Cleanup Test Environment

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

*** Test Cases ***
WF07 CI Profile Config Set And Get
    [Documentation]    Set and verify ci automation profile, json format, and log level via ConfigService.
    [Tags]    cicd    integration    workflow7
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    config-ci-profile    cwd=${WORKSPACE}    timeout=30s    on_timeout=kill
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    wf07-config-ci-profile-ok

WF07 Idempotent Resource Registration
    [Documentation]    Register a git-checkout resource twice and verify only one exists.
    [Tags]    cicd    integration    workflow7
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    resource-idempotent    cwd=${WORKSPACE}    timeout=30s    on_timeout=kill
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    wf07-resource-idempotent-ok

WF07 Idempotent Project Registration
    [Documentation]    Create a project twice and verify it exists exactly once.
    [Tags]    cicd    integration    workflow7
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    project-idempotent    cwd=${WORKSPACE}    timeout=30s    on_timeout=kill
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    wf07-project-idempotent-ok

WF07 Validation Registration And Attachment
    [Documentation]    Register three validation tools, attach to a resource, and verify pipeline execution.
    [Tags]    cicd    integration    workflow7
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    validation-attach    cwd=${WORKSPACE}    timeout=30s    on_timeout=kill
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    wf07-validation-attach-ok

WF07 CI Plan Lifecycle
    [Documentation]    Create an action with arguments and invariants, run plan lifecycle with
    ...                phase-by-phase completion through all phases, then verify applied and cancelled terminal states.
    [Tags]    cicd    integration    workflow7
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    ci-plan-lifecycle    cwd=${WORKSPACE}    timeout=30s    on_timeout=kill
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    wf07-ci-plan-lifecycle-ok

WF07 JSON Output Parsing
    [Documentation]    Run commands with JSON output and verify structure including plan_id,
    ...                phase, state, and action fields.
    [Tags]    cicd    integration    workflow7
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    json-output    cwd=${WORKSPACE}    timeout=30s    on_timeout=kill
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    wf07-json-output-ok
