*** Settings ***
Documentation     Integration smoke test for project show after create (bug #590).
...               Verifies that ``agents project show`` finds a project
...               immediately after ``agents project create`` commits it.
Resource          ${CURDIR}/common.resource
Library           Process
Library           OperatingSystem
Library           String
Suite Setup       Setup Test Environment
Suite Teardown    Cleanup Test Environment

*** Test Cases ***
Project Show Displays Created Project
    [Documentation]    After creating a project, show should display its details.
    ${tmpdir}=    Evaluate    __import__('tempfile').mkdtemp(prefix='show_590_')
    ${init}=    Run Process    ${PYTHON}    -m    cleveragents    init    show-test
    ...    timeout=120s    on_timeout=kill    cwd=${tmpdir}
    Should Be Equal As Integers    ${init.rc}    0
    ...    msg=agents init failed: ${init.stderr}
    ${create}=    Run Process    ${PYTHON}    -m    cleveragents    project    create    local/show-proj
    ...    timeout=120s    on_timeout=kill    cwd=${tmpdir}
    Should Be Equal As Integers    ${create.rc}    0
    ...    msg=project create failed: ${create.stderr}
    ${result}=    Run Process    ${PYTHON}    -m    cleveragents    project    show    --format    plain    local/show-proj
    ...    timeout=120s    on_timeout=kill    cwd=${tmpdir}
    Should Be Equal As Integers    ${result.rc}    0
    ...    msg=project show should exit 0 but got ${result.rc}. stderr: ${result.stderr}
    Should Contain    ${result.stdout}    local/show-proj
    ...    msg=project show output should contain project name but got: ${result.stdout}
    [Teardown]    Remove Directory    ${tmpdir}    recursive=True

Project Show With Description Displays Description
    [Documentation]    Show should display the project description after creation.
    ${tmpdir}=    Evaluate    __import__('tempfile').mkdtemp(prefix='show_590_desc_')
    ${init}=    Run Process    ${PYTHON}    -m    cleveragents    init    show-desc-test
    ...    timeout=120s    on_timeout=kill    cwd=${tmpdir}
    Should Be Equal As Integers    ${init.rc}    0
    ...    msg=agents init failed: ${init.stderr}
    ${create}=    Run Process    ${PYTHON}    -m    cleveragents    project    create    local/desc-proj
    ...    -d    A test project
    ...    timeout=120s    on_timeout=kill    cwd=${tmpdir}
    Should Be Equal As Integers    ${create.rc}    0
    ...    msg=project create failed: ${create.stderr}
    ${result}=    Run Process    ${PYTHON}    -m    cleveragents    project    show    --format    plain    local/desc-proj
    ...    timeout=120s    on_timeout=kill    cwd=${tmpdir}
    Should Be Equal As Integers    ${result.rc}    0
    ...    msg=project show should exit 0 but got ${result.rc}. stderr: ${result.stderr}
    Should Contain    ${result.stdout}    A test project
    ...    msg=project show should display description but got: ${result.stdout}
    [Teardown]    Remove Directory    ${tmpdir}    recursive=True

Project Show Returns Error For Nonexistent Project
    [Documentation]    Showing a project that was never created should fail.
    ${tmpdir}=    Evaluate    __import__('tempfile').mkdtemp(prefix='show_590_missing_')
    ${init}=    Run Process    ${PYTHON}    -m    cleveragents    init    show-missing-test
    ...    timeout=120s    on_timeout=kill    cwd=${tmpdir}
    Should Be Equal As Integers    ${init.rc}    0
    ...    msg=agents init failed: ${init.stderr}
    ${result}=    Run Process    ${PYTHON}    -m    cleveragents    project    show    --format    plain    local/nonexistent
    ...    timeout=120s    on_timeout=kill    cwd=${tmpdir}
    Should Not Be Equal As Integers    ${result.rc}    0
    ...    msg=project show for nonexistent project should fail but exited 0
    ${combined}=    Set Variable    ${result.stdout}${result.stderr}
    ${lower}=    Convert To Lower Case    ${combined}
    Should Contain    ${lower}    not found
    ...    msg=project show for nonexistent project should mention "not found" but got: ${combined}
    [Teardown]    Remove Directory    ${tmpdir}    recursive=True
