*** Settings ***
Documentation    Integration tests for ASGI server lifecycle
...
...              Tests verify that the ASGI application, health endpoint,
...              agent card, A2A dispatch, and ServerLifecycle construction
...              all work end-to-end through real imports and instantiation.
Resource         ${CURDIR}/common.resource
Suite Setup      Setup Test Environment
Suite Teardown   Cleanup Test Environment

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

*** Test Cases ***
Create ASGI Application
    [Documentation]    create_asgi_app returns a FastAPI instance
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    create-app    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    create-app-ok

Health Endpoint Returns Healthy
    [Documentation]    GET /health returns 200 with status healthy
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    health-endpoint    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    health-endpoint-ok

Agent Card Discovery Endpoint
    [Documentation]    GET /.well-known/agent.json returns valid agent card
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    agent-card    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    agent-card-ok

A2A JSON-RPC Dispatch
    [Documentation]    POST /a2a dispatches health check operation via facade
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    a2a-dispatch    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    a2a-dispatch-ok

ServerLifecycle Construction
    [Documentation]    ServerLifecycle construction sets host, port, and initial state
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    lifecycle-construction    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    lifecycle-construction-ok

ServerLifecycle Graceful Shutdown
    [Documentation]    request_shutdown sets the should_exit flag
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    lifecycle-shutdown    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    lifecycle-shutdown-ok
