*** Settings ***
Documentation     Integration smoke test for project create persistence (bug #589).
...               Verifies that ``agents project create`` commits to the database
...               so that ``agents project list`` shows previously created projects.
Resource          ${CURDIR}/common.resource
Library           Process
Library           OperatingSystem
Library           String
Suite Setup       Setup Test Environment
Suite Teardown    Cleanup Test Environment

*** Test Cases ***
Project Create Then List Shows Created Project
    [Documentation]    After creating a project, listing projects should include it.
    ${tmpdir}=    Evaluate    __import__('tempfile').mkdtemp(prefix='persist_589_')
    ${init}=    Run Process    ${PYTHON}    -m    cleveragents    init    persist-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    project    create    local/smoke-proj
    ...    timeout=120s    on_timeout=kill    cwd=${tmpdir}
    Should Be Equal As Integers    ${create.rc}    0
    ...    msg=project create should exit 0 but got ${create.rc}. stderr: ${create.stderr}
    ${list}=    Run Process    ${PYTHON}    -m    cleveragents    project    list
    ...    timeout=120s    on_timeout=kill    cwd=${tmpdir}
    Should Be Equal As Integers    ${list.rc}    0
    ...    msg=project list should exit 0 but got ${list.rc}. stderr: ${list.stderr}
    Should Contain    ${list.stdout}    local/smoke-proj
    ...    msg=project list output should contain 'local/smoke-proj' but got: ${list.stdout}
    [Teardown]    Remove Directory    ${tmpdir}    recursive=True

Multiple Created Projects Appear In List
    [Documentation]    Creating two projects should result in both appearing in list.
    ${tmpdir}=    Evaluate    __import__('tempfile').mkdtemp(prefix='persist_589_multi_')
    ${init}=    Run Process    ${PYTHON}    -m    cleveragents    init    persist-multi
    ...    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_first}=    Run Process    ${PYTHON}    -m    cleveragents    project    create    local/first
    ...    timeout=120s    on_timeout=kill    cwd=${tmpdir}
    Should Be Equal As Integers    ${create_first.rc}    0
    ...    msg=project create local/first should exit 0 but got ${create_first.rc}. stderr: ${create_first.stderr}
    ${create_second}=    Run Process    ${PYTHON}    -m    cleveragents    project    create    local/second
    ...    timeout=120s    on_timeout=kill    cwd=${tmpdir}
    Should Be Equal As Integers    ${create_second.rc}    0
    ...    msg=project create local/second should exit 0 but got ${create_second.rc}. stderr: ${create_second.stderr}
    ${list}=    Run Process    ${PYTHON}    -m    cleveragents    project    list
    ...    timeout=120s    on_timeout=kill    cwd=${tmpdir}
    Should Be Equal As Integers    ${list.rc}    0
    ...    msg=project list should exit 0 but got ${list.rc}. stderr: ${list.stderr}
    Should Contain    ${list.stdout}    local/first
    ...    msg=project list should contain 'local/first' but got: ${list.stdout}
    Should Contain    ${list.stdout}    local/second
    ...    msg=project list should contain 'local/second' but got: ${list.stdout}
    [Teardown]    Remove Directory    ${tmpdir}    recursive=True
