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

*** Test Cases ***
Session Create After Init Should Not Error
    [Documentation]    After agents init, session create --format plain should
    ...                exit 0 and produce a new session rather than a DI
    ...                AttributeError.
    [Tags]    tdd_issue    tdd_issue_570
    ${tmpdir}=    Evaluate    __import__('tempfile').mkdtemp(prefix='sce_570_')
    ${init}=    Run Process    ${PYTHON}    -m    cleveragents    init    sce-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}
    ${create}=    Run Process    ${PYTHON}    -m    cleveragents    session    create    --format    plain
    ...    timeout=120s    on_timeout=kill    cwd=${tmpdir}
    Should Be Equal As Integers    ${create.rc}    0
    ...    msg=session create should exit 0 but got ${create.rc}. stderr: ${create.stderr}
    Should Not Contain    ${create.stderr}    AttributeError
    ...    msg=session create should not raise AttributeError: ${create.stderr}
    [Teardown]    Remove Directory    ${tmpdir}    recursive=True

Session Create Then List Shows Created Session
    [Documentation]    After creating a session, listing should show it.
    [Tags]    tdd_issue    tdd_issue_570
    ${tmpdir}=    Evaluate    __import__('tempfile').mkdtemp(prefix='sce_570_list_')
    ${init}=    Run Process    ${PYTHON}    -m    cleveragents    init    sce-list
    ...    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}
    ${create}=    Run Process    ${PYTHON}    -m    cleveragents    session    create    --format    plain
    ...    timeout=120s    on_timeout=kill    cwd=${tmpdir}
    Should Be Equal As Integers    ${create.rc}    0
    ...    msg=session create should exit 0 but got ${create.rc}. stderr: ${create.stderr}
    Should Not Contain    ${create.stderr}    AttributeError
    ...    msg=session create should not raise AttributeError: ${create.stderr}
    ${list}=    Run Process    ${PYTHON}    -m    cleveragents    session    list    --format    plain
    ...    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 Contain    ${list.stdout}    total:
    ...    msg=Expected 'total:' in plain output: ${list.stdout}
    Should Not Contain    ${list.stdout}    total: 0
    ...    msg=Expected at least one session in list output: ${list.stdout}
    [Teardown]    Remove Directory    ${tmpdir}    recursive=True
