*** Settings ***
Documentation     Integration tests for --load-context CLI feature
...               SEC1: Uses simple_echo_config.yaml which requires code blocks.
Library           Process
Library           OperatingSystem
Library           String
Library           DateTime
Library           Collections
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}/test_load_contexts
${UNIQUE_ID}                ${EMPTY}
${TEMP}                     ${EMPTY}

*** Test Cases ***
Test Load Context Transiently With Run Command
    [Documentation]    Verify --load-context loads context without persisting
    ${context_file} =    Create Sample Context JSON File
    ${result} =    Run Process    ${PYTHON}    -m    cleveragents    actor    run
    ...    -c    ${SIMPLE_CONFIG}
    ...    --load-context    ${context_file}
    ...    --unsafe
    ...    --allow-rxpy-in-run-mode
    ...    test-actor    "test message"

    Should Be Equal As Integers    ${result.rc}    0
    # Verify no context directory was created (transient)
    ${home} =    Get Environment Variable    HOME
    Directory Should Not Exist    ${home}/.cleveragents/context/_temp_*

Test Load Context Into Named Context
    [Documentation]    Verify --load-context with --context imports and persists
    ${context_name} =    Set Variable    load_named_${UNIQUE_ID}
    ${context_file} =    Create Sample Context JSON File

    # Load context into named context
    ${result} =    Run Process    ${PYTHON}    -m    cleveragents    actor    run
    ...    -c    ${SIMPLE_CONFIG}
    ...    --load-context    ${context_file}
    ...    --context    ${context_name}
    ...    --context-dir    ${CONTEXT_DIR}
    ...    --unsafe
    ...    --allow-rxpy-in-run-mode
    ...    test-actor    "test message"

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

    # Verify context was created and persisted
    Directory Should Exist    ${CONTEXT_DIR}/${context_name}
    File Should Exist    ${CONTEXT_DIR}/${context_name}/messages.json
    File Should Exist    ${CONTEXT_DIR}/${context_name}/global_context.json

    # Verify global context was loaded
    ${global_ctx} =    Get File    ${CONTEXT_DIR}/${context_name}/global_context.json
    Should Contain    ${global_ctx}    test_key
    Should Contain    ${global_ctx}    test_value

Test Load Context Replaces Existing Named Context
    [Documentation]    Verify --load-context overwrites existing context
    ${context_name} =    Set Variable    replace_${UNIQUE_ID}

    # Create initial context with different data
    ${result1} =    Run Process    ${PYTHON}    -m    cleveragents    actor    run
    ...    -c    ${SIMPLE_CONFIG}
    ...    --context    ${context_name}
    ...    --context-dir    ${CONTEXT_DIR}
    ...    --unsafe
    ...    --allow-rxpy-in-run-mode
    ...    test-actor    "original message"

    Should Be Equal As Integers    ${result1.rc}    0

    # Create new context file with different data
    ${new_context_file} =    Create Context JSON File With Different Data

    # Load new context, replacing the old one
    ${result2} =    Run Process    ${PYTHON}    -m    cleveragents    actor    run
    ...    -c    ${SIMPLE_CONFIG}
    ...    --load-context    ${new_context_file}
    ...    --context    ${context_name}
    ...    --context-dir    ${CONTEXT_DIR}
    ...    --unsafe
    ...    --allow-rxpy-in-run-mode
    ...    test-actor    "new message"

    Should Be Equal As Integers    ${result2.rc}    0

    # Verify context was replaced with new data
    ${global_ctx} =    Get File    ${CONTEXT_DIR}/${context_name}/global_context.json
    Should Contain    ${global_ctx}    replaced_key
    Should Contain    ${global_ctx}    replaced_value

Test Load Context From Export Format
    [Documentation]    Verify loading context from exported file works correctly
    ${context_name} =    Set Variable    export_test_${UNIQUE_ID}
    ${export_file} =    Set Variable    ${TEMP}/exported_context.json

    # Create a context with some data
    ${result1} =    Run Process    ${PYTHON}    -m    cleveragents    actor    run
    ...    -c    ${SIMPLE_CONFIG}
    ...    --context    ${context_name}
    ...    --context-dir    ${CONTEXT_DIR}
    ...    --unsafe
    ...    --allow-rxpy-in-run-mode
    ...    test-actor    "test data for export"

    Should Be Equal As Integers    ${result1.rc}    0

    # Export the context
    ${result2} =    Run Process    ${PYTHON}    -m    cleveragents    context    export
    ...    ${context_name}
    ...    ${export_file}
    ...    --context-dir    ${CONTEXT_DIR}

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

    # Delete the original context
    ${result3} =    Run Process    ${PYTHON}    -m    cleveragents    context    delete
    ...    ${context_name}
    ...    --context-dir    ${CONTEXT_DIR}
    ...    --yes

    Should Be Equal As Integers    ${result3.rc}    0

    # Load the exported file into a new context
    ${new_context_name} =    Set Variable    import_test_${UNIQUE_ID}
    ${result4} =    Run Process    ${PYTHON}    -m    cleveragents    actor    run
    ...    -c    ${SIMPLE_CONFIG}
    ...    --load-context    ${export_file}
    ...    --context    ${new_context_name}
    ...    --context-dir    ${CONTEXT_DIR}
    ...    --unsafe
    ...    --allow-rxpy-in-run-mode
    ...    test-actor    "verify import"

    Should Be Equal As Integers    ${result4.rc}    0

    # Verify the imported context has the original data
    Directory Should Exist    ${CONTEXT_DIR}/${new_context_name}
    ${messages} =    Get File    ${CONTEXT_DIR}/${new_context_name}/messages.json
    Should Contain    ${messages}    test data for export

Test Load Context With Non-Existent File
    [Documentation]    Verify appropriate error when context file doesn't exist
    ${result} =    Run Process    ${PYTHON}    -m    cleveragents    actor    run
    ...    -c    ${SIMPLE_CONFIG}
    ...    --load-context    /nonexistent/file.json
    ...    --unsafe
    ...    --allow-rxpy-in-run-mode
    ...    test-actor    "test"

    Should Not Be Equal As Integers    ${result.rc}    0
    Should Contain Any    ${result.stderr}    does not exist    No such file    not found

Test Load Context With Invalid JSON
    [Documentation]    Verify appropriate error when JSON is malformed
    ${bad_json_file} =    Set Variable    ${TEMP}/bad_context.json
    Create File    ${bad_json_file}    { invalid json content

    ${result} =    Run Process    ${PYTHON}    -m    cleveragents    actor    run
    ...    -c    ${SIMPLE_CONFIG}
    ...    --load-context    ${bad_json_file}
    ...    --unsafe
    ...    --allow-rxpy-in-run-mode
    ...    test-actor    "test"

    Should Not Be Equal As Integers    ${result.rc}    0

Test Load Context Help Text
    [Documentation]    Verify --load-context appears in help text
    ${result} =    Run Process    ${PYTHON}    -m    cleveragents    actor    run    --help    stderr=STDOUT    env:NO_COLOR=1    env:TERM=dumb
    Should Be Equal As Integers    ${result.rc}    0
    # Debug: show repr of bytes around 'load' to reveal any ANSI escape codes
    ${idx} =    Evaluate    $result.stdout.find('load')
    ${snippet} =    Evaluate    repr($result.stdout[max(0,$idx-30):$idx+40]) if $idx >= 0 else 'NOT FOUND; tail=' + repr($result.stdout[-200:])
    Log    DEBUG stdout repr around 'load': ${snippet}    console=True
    Should Contain    ${result.stdout}    --load-context

Test Load Context Interactive Help Text
    [Documentation]    Verify --load-context appears in actor run help text
    ${result} =    Run Process    ${PYTHON}    -m    cleveragents    actor    run    --help    stderr=STDOUT    env:NO_COLOR=1    env:TERM=dumb
    Should Be Equal As Integers    ${result.rc}    0
    # Debug: show repr of bytes around 'load' to reveal any ANSI escape codes
    ${idx} =    Evaluate    $result.stdout.find('load')
    ${snippet} =    Evaluate    repr($result.stdout[max(0,$idx-30):$idx+40]) if $idx >= 0 else 'NOT FOUND; tail=' + repr($result.stdout[-200:])
    Log    DEBUG stdout repr around 'load': ${snippet}    console=True
    Should Contain    ${result.stdout}    --load-context


Test Load Context With All Components
    [Documentation]    Verify all context components (messages, state, metadata, global_context) are loaded
    ${context_name} =    Set Variable    full_load_${UNIQUE_ID}
    ${context_file} =    Create Full Context JSON File

    ${result} =    Run Process    ${PYTHON}    -m    cleveragents    actor    run
    ...    -c    ${SIMPLE_CONFIG}
    ...    --load-context    ${context_file}
    ...    --context    ${context_name}
    ...    --context-dir    ${CONTEXT_DIR}
    ...    --unsafe
    ...    --allow-rxpy-in-run-mode
    ...    test-actor    "verify all components"

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

    # Verify all components were loaded
    File Should Exist    ${CONTEXT_DIR}/${context_name}/messages.json
    File Should Exist    ${CONTEXT_DIR}/${context_name}/metadata.json
    File Should Exist    ${CONTEXT_DIR}/${context_name}/state.json
    File Should Exist    ${CONTEXT_DIR}/${context_name}/global_context.json

    # Verify content of each component
    ${messages} =    Get File    ${CONTEXT_DIR}/${context_name}/messages.json
    Should Contain    ${messages}    initial message

    ${state} =    Get File    ${CONTEXT_DIR}/${context_name}/state.json
    Should Contain    ${state}    test_state_key

    ${global_ctx} =    Get File    ${CONTEXT_DIR}/${context_name}/global_context.json
    Should Contain    ${global_ctx}    session_id

*** 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}/ca_loadctx_${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

Create Sample Context JSON File
    [Documentation]    Create a simple context JSON file for testing
    ${context_file} =    Set Variable    ${TEMP}/sample_context.json
    ${content} =    Catenate    SEPARATOR=\n
    ...    {
    ...      "context_name": "sample",
    ...      "messages": [
    ...        {
    ...          "role": "user",
    ...          "content": "Hello",
    ...          "timestamp": "2025-01-01T00:00:00",
    ...          "metadata": {}
    ...        }
    ...      ],
    ...      "metadata": {
    ...        "created_at": "2025-01-01T00:00:00"
    ...      },
    ...      "state": {},
    ...      "global_context": {
    ...        "test_key": "test_value"
    ...      }
    ...    }
    Create File    ${context_file}    ${content}
    RETURN    ${context_file}

Create Context JSON File With Different Data
    [Documentation]    Create a context JSON file with different data for replacement test
    ${context_file} =    Set Variable    ${TEMP}/different_context.json
    ${content} =    Catenate    SEPARATOR=\n
    ...    {
    ...      "context_name": "different",
    ...      "messages": [],
    ...      "metadata": {},
    ...      "state": {},
    ...      "global_context": {
    ...        "replaced_key": "replaced_value"
    ...      }
    ...    }
    Create File    ${context_file}    ${content}
    RETURN    ${context_file}

Create Full Context JSON File
    [Documentation]    Create a context JSON file with all components
    ${context_file} =    Set Variable    ${TEMP}/full_context.json
    ${content} =    Catenate    SEPARATOR=\n
    ...    {
    ...      "context_name": "full",
    ...      "messages": [
    ...        {
    ...          "role": "user",
    ...          "content": "initial message",
    ...          "timestamp": "2025-01-01T00:00:00",
    ...          "metadata": {}
    ...        }
    ...      ],
    ...      "metadata": {
    ...        "created_at": "2025-01-01T00:00:00",
    ...        "message_count": 1
    ...      },
    ...      "state": {
    ...        "test_state_key": "test_state_value"
    ...      },
    ...      "global_context": {
    ...        "session_id": "12345",
    ...        "user_preferences": {"theme": "dark"}
    ...      }
    ...    }
    Create File    ${context_file}    ${content}
    RETURN    ${context_file}
