*** Settings ***
Documentation    Integration tests for TUI normal text input → A2A facade → LLM dispatch.
...
...              Verifies that:
...              - _create_tui_session() creates a real database-backed session
...              - _build_tui_facade() produces a wired A2aLocalFacade
...              - CleverAgentsTuiApp dispatches normal text to the facade
...                and renders the assistant response
...
...              Uses a FakeListLLM stub — no real API keys required.
Resource         ${CURDIR}/common.resource
Suite Setup      Setup Test Environment With Database Isolation
Suite Teardown   Cleanup Test Environment

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

*** Test Cases ***
TUI Create Session Returns Real Session ID
    [Documentation]    Verify _create_tui_session() creates a DB-backed session.
    [Tags]    tui_llm_dispatch
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    create-session
    ...    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    tui-create-session-ok

TUI Build Facade Returns Wired Facade
    [Documentation]    Verify _build_tui_facade() returns a non-None A2aLocalFacade.
    [Tags]    tui_llm_dispatch
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    build-facade
    ...    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    tui-build-facade-ok

TUI Normal Text Dispatches To Facade And Renders Response
    [Documentation]    Verify CleverAgentsTuiApp dispatches normal text to
    ...                the facade and updates the conversation widget with the
    ...                assistant response from a FakeListLLM.
    [Tags]    tui_llm_dispatch
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    dispatch-message
    ...    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    tui-dispatch-message-ok

TUI No Actor Error Renders Friendly Message
    [Documentation]    Verify that when no actor is configured,
    ...                the TUI shows a friendly error rather than crashing.
    [Tags]    tui_llm_dispatch
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    no-actor-error
    ...    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    tui-no-actor-error-ok

TUI DatabaseError Renders Friendly Message
    [Documentation]    Verify _run_llm_dispatch returns a user-readable error
    ...                string when a DatabaseError is raised during dispatch.
    [Tags]    tui_llm_dispatch
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    database-error
    ...    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    tui-database-error-ok

TUI SessionNotFoundError Renders Friendly Message
    [Documentation]    Verify _run_llm_dispatch returns a user-readable error
    ...                string when the session is missing at dispatch time.
    [Tags]    tui_llm_dispatch
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    session-not-found
    ...    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    tui-session-not-found-ok
