*** Settings ***
Documentation     Integration test for Scientific Paper Writer context management
...               SEC1: Uses simple_echo_config.yaml which requires code blocks.
Library           Process
Library           OperatingSystem
Library           String
Library           DateTime
Resource    ${CURDIR}/v2_paths.resource
Force Tags        code_blocks
Suite Setup       Setup Test Environment
Suite Teardown    Cleanup Test Environment

*** Variables ***
${SIMPLE_CONFIG}            ${V2_CONFIG_DIR}/simple_echo_config.yaml
${CONTEXT_DIR}              ${TEMPDIR}/sciwri_contexts
${UNIQUE_ID}                ${EMPTY}
${TEMP}                     ${EMPTY}

*** Test Cases ***
Test Context Export Import Commands
    [Documentation]    Test exporting and importing context
    ${source_ctx} =    Set Variable    export_${UNIQUE_ID}
    ${import_ctx} =    Set Variable    import_${UNIQUE_ID}
    ${export_file} =    Set Variable    ${TEMP}/export.json

    # Create source context with simple config
    Run Process    ${PYTHON}    -m    cleveragents    actor    run
    ...    -c    ${SIMPLE_CONFIG}
    ...    --context    ${source_ctx}
    ...    --context-dir    ${CONTEXT_DIR}
    ...    --unsafe
    ...    --allow-rxpy-in-run-mode
    ...    test-actor    Hello world
    ...    timeout=120s    on_timeout=kill

    # Export it
    ${result} =    Run Process    ${PYTHON}    -m    cleveragents    context    export
    ...    ${source_ctx}    ${export_file}
    ...    --context-dir    ${CONTEXT_DIR}

    Should Be Equal As Integers    ${result.rc}    0
    File Should Exist    ${export_file}

    # Import to new context
    ${result} =    Run Process    ${PYTHON}    -m    cleveragents    context    import
    ...    ${import_ctx}    ${export_file}
    ...    --context-dir    ${CONTEXT_DIR}

    Should Be Equal As Integers    ${result.rc}    0
    Directory Should Exist    ${CONTEXT_DIR}/${import_ctx}

Test Context List Command
    [Documentation]    Test listing contexts
    ${ctx1} =    Set Variable    list1_${UNIQUE_ID}
    ${ctx2} =    Set Variable    list2_${UNIQUE_ID}

    # Create two contexts with simple echo config
    Run Process    ${PYTHON}    -m    cleveragents    actor    run
    ...    -c    ${SIMPLE_CONFIG}
    ...    --context    ${ctx1}
    ...    --context-dir    ${CONTEXT_DIR}
    ...    --unsafe
    ...    --allow-rxpy-in-run-mode
    ...    test-actor    First
    ...    timeout=120s    on_timeout=kill

    Run Process    ${PYTHON}    -m    cleveragents    actor    run
    ...    -c    ${SIMPLE_CONFIG}
    ...    --context    ${ctx2}
    ...    --context-dir    ${CONTEXT_DIR}
    ...    --unsafe
    ...    --allow-rxpy-in-run-mode
    ...    test-actor    Second
    ...    timeout=120s    on_timeout=kill

    # List them
    ${result} =    Run Process    ${PYTHON}    -m    cleveragents    context    list
    ...    --context-dir    ${CONTEXT_DIR}

    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    ${ctx1}
    Should Contain    ${result.stdout}    ${ctx2}

*** Keywords ***
Setup Test Environment
    ${timestamp} =    Get Current Date    result_format=%Y%m%d_%H%M%S
    ${random} =    Evaluate    random.randint(1000, 9999)    modules=random
    Set Suite Variable    ${UNIQUE_ID}    ${timestamp}_${random}
    Set Suite Variable    ${TEMP}    ${TEMPDIR}/sciwri_test_${UNIQUE_ID}
    Set Suite Variable    ${CONTEXT_DIR}    ${TEMP}/contexts
    Create Directory    ${TEMP}
    Create Directory    ${CONTEXT_DIR}

Cleanup Test Environment
    Run Keyword And Ignore Error    Remove Directory    ${TEMP}    recursive=True
