*** Settings ***
Documentation     Actor-first context management tests for CleverAgents CLI
Library           Process
Library           OperatingSystem
Library           String
Library           DateTime
Resource          ${CURDIR}/common.resource
Suite Setup       Setup Test Environment
Suite Teardown    Cleanup Test Environment

*** Variables ***
${TEST_PROJECT_DIR}     ${TEMPDIR}/test_project_${EMPTY}
${UNIQUE_ID}            ${EMPTY}

*** Test Cases ***
Test Context Commands With Actor
    [Documentation]    Verify context commands work with actor-first approach
    [Tags]    smoke    actor    context

    # Initialize project first
    Create Directory    ${TEST_PROJECT_DIR}
    ${result} =    Run Process    ${PYTHON}    -m    cleveragents    init    test-project
    ...    cwd=${TEST_PROJECT_DIR}    timeout=120s
    Should Be Equal As Integers    ${result.rc}    0

    # Create test files
    Create Directory    ${TEST_PROJECT_DIR}/src
    Create File    ${TEST_PROJECT_DIR}/src/main.py    print("Hello World")

    # Load context with actor (simulating with environment variable)
    ${result} =    Run Process    ${PYTHON}    -m    cleveragents    context-load    src/
    ...    cwd=${TEST_PROJECT_DIR}    env:CLEVERAGENTS_TESTING_USE_MOCK_AI=true    env:CLEVERAGENTS_DEFAULT_ACTOR=openai/gpt-4    timeout=120s
    Should Be Equal As Integers    ${result.rc}    0

    # List contexts
    ${result} =    Run Process    ${PYTHON}    -m    cleveragents    context    list
    ...    cwd=${TEST_PROJECT_DIR}    timeout=30s
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    main.py

*** Keywords ***
Setup Test Environment
    [Documentation]    Create test environment
    # First run the common setup
    common.Setup Test Environment

    ${timestamp} =    Get Current Date    result_format=%Y%m%d%H%M%S
    ${random} =    Generate Random String    6    [NUMBERS]
    Set Suite Variable    ${UNIQUE_ID}    ${timestamp}_${random}

    # Create temp directory
    ${temp} =    Evaluate    tempfile.mkdtemp()    modules=tempfile
    Set Suite Variable    ${TEMP}    ${temp}
    Create Directory    ${TEMP}

    # Update TEST_PROJECT_DIR with unique ID
    Set Suite Variable    ${TEST_PROJECT_DIR}    ${TEMP}/test_project_${UNIQUE_ID}

    Log    Test environment created with ID: ${UNIQUE_ID}

Cleanup Test Environment
    [Documentation]    Clean up test environment
    Run Keyword If    '${TEMP}' != '${EMPTY}'    Remove Directory    ${TEMP}    recursive=True
    Remove Environment Variable    CLEVERAGENTS_TESTING_USE_MOCK_AI
    Remove Environment Variable    CLEVERAGENTS_DEFAULT_ACTOR
