*** Settings ***
Documentation     Integration smoke test for session list DI error (bug #554).
...               Regression tests verifying that the DI container fix for
...               session list works correctly.  Bug #554 is now fixed.
Resource          ${CURDIR}/common.resource
Library           Process
Library           OperatingSystem
Suite Setup       Setup Test Environment
Suite Teardown    Cleanup Test Environment

*** Comments ***
# NOTE: ``Should Contain`` and ``Should Not Contain`` are case-sensitive
# by default in Robot Framework.  The assertions below rely on the exact
# output format produced by the CLI:
#   - "AttributeError" (Python exception class name, title-case)
# If the production code changes its error message casing, update these
# assertions accordingly.

*** Test Cases ***
Session List After Init Should Not Error
    [Documentation]    After agents init, session list should exit 0 and show
    ...                "No sessions found" rather than a DI AttributeError.
    [Tags]    tdd_issue    tdd_issue_554
    ${tmpdir}=    Evaluate    __import__('tempfile').mkdtemp(prefix='sle_554_')
    ${init}=    Run Process    ${PYTHON}    -m    cleveragents    init    sle-test
    ...    timeout=120s    on_timeout=kill    cwd=${tmpdir}
    Should Be Equal As Integers    ${init.rc}    0
    ...    msg=agents init should exit 0 but got ${init.rc}. stderr: ${init.stderr}
    ${list}=    Run Process    ${PYTHON}    -m    cleveragents    session    list
    ...    timeout=120s    on_timeout=kill    cwd=${tmpdir}
    Should Be Equal As Integers    ${list.rc}    0
    ...    msg=session list should exit 0 but got ${list.rc}. stderr: ${list.stderr}
    Should Not Contain    ${list.stderr}    AttributeError
    ...    msg=session list should not raise AttributeError in stderr: ${list.stderr}
    Should Not Contain    ${list.stdout}    AttributeError
    ...    msg=session list should not raise AttributeError in stdout: ${list.stdout}
    [Teardown]    Remove Directory    ${tmpdir}    recursive=True

Session List JSON Format Does Not Error
    [Documentation]    session list --format json should exit 0 without raising
    ...                a DI AttributeError.
    [Tags]    tdd_issue    tdd_issue_554
    ${tmpdir}=    Evaluate    __import__('tempfile').mkdtemp(prefix='sle_554_json_')
    ${init}=    Run Process    ${PYTHON}    -m    cleveragents    init    sle-json
    ...    timeout=120s    on_timeout=kill    cwd=${tmpdir}
    Should Be Equal As Integers    ${init.rc}    0
    ...    msg=agents init should exit 0 but got ${init.rc}. stderr: ${init.stderr}
    ${list}=    Run Process    ${PYTHON}    -m    cleveragents    session    list    --format    json
    ...    timeout=120s    on_timeout=kill    cwd=${tmpdir}
    Should Be Equal As Integers    ${list.rc}    0
    ...    msg=session list --format json should exit 0 but got ${list.rc}. stderr: ${list.stderr}
    Should Not Contain    ${list.stderr}    AttributeError
    ...    msg=session list --format json should not raise AttributeError in stderr: ${list.stderr}
    Should Not Contain    ${list.stdout}    AttributeError
    ...    msg=session list --format json should not raise AttributeError in stdout: ${list.stdout}
    [Teardown]    Remove Directory    ${tmpdir}    recursive=True
