*** Settings ***
Documentation     Integration tests for sandbox infrastructure: protocol, NoSandbox,
...               factory, manager, and merge strategies.
...               Covers commit 548a09f stages B3.1-B3.8 sandbox infrastructure.
Resource          ${CURDIR}/common.resource
Suite Setup       Setup Test Environment
Suite Teardown    Cleanup Test Environment

*** Variables ***
${HELPER_SCRIPT}    robot/helper_sandbox_integration.py

*** Test Cases ***
Sandbox Status State Machine Transitions
    [Documentation]    Verify SandboxStatus enum validates all valid/invalid state transitions
    [Tags]    sandbox    protocol    transitions
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    status-transitions    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    sandbox-status-transitions-ok

NoSandbox Full Lifecycle
    [Documentation]    Verify NoSandbox create -> get_path -> commit -> cleanup with protocol compliance
    [Tags]    sandbox    nosandbox    lifecycle
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    no-sandbox-lifecycle    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    no-sandbox-lifecycle-ok

NoSandbox Rollback Always Raises
    [Documentation]    Verify NoSandbox.rollback raises SandboxRollbackError (changes are immediate)
    [Tags]    sandbox    nosandbox    rollback
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    no-sandbox-rollback    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    no-sandbox-rollback-ok

Factory Creates NoSandbox For None Strategy
    [Documentation]    Verify SandboxFactory creates NoSandbox for 'none' strategy and rejects unsupported
    [Tags]    sandbox    factory    create
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    factory-create-none    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    factory-create-none-ok

Factory Reports Supported Strategies Per Resource Type
    [Documentation]    Verify SandboxFactory.get_supported_strategies returns correct strategy lists
    [Tags]    sandbox    factory    strategies
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    factory-supported    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    factory-supported-ok

Manager Lifecycle With Get Or Create And Commit All
    [Documentation]    Verify SandboxManager lazy creation, deduplication, commit_all, and cleanup_all
    [Tags]    sandbox    manager    lifecycle
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    manager-lifecycle    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    manager-lifecycle-ok

Manager Handles Multiple Resources Per Plan
    [Documentation]    Verify SandboxManager tracks multiple sandboxes per plan correctly
    [Tags]    sandbox    manager    multiple
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    manager-multiple-resources    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    manager-multiple-resources-ok

Manager Atomic Commit Rolls Back On Failure
    [Documentation]    Verify SandboxManager.commit_all is atomic: on partial failure, already-committed sandboxes are rolled back
    [Tags]    sandbox    manager    atomic    commit
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    manager-atomic-commit    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    manager-atomic-commit-ok

Sequential Merge Strategy Returns Theirs
    [Documentation]    Verify SequentialMergeStrategy always picks theirs (last-write-wins)
    [Tags]    sandbox    merge    sequential
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    sequential-merge    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    sequential-merge-ok

JSON Merge Strategy Deep Merges Objects
    [Documentation]    Verify JsonMergeStrategy performs deep recursive merge on JSON objects
    [Tags]    sandbox    merge    json
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    json-merge    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    json-merge-ok

JSON Merge Strategy Concat Arrays Mode
    [Documentation]    Verify JsonMergeStrategy concatenates arrays in concat mode
    [Tags]    sandbox    merge    json    concat
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    json-merge-concat    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    json-merge-concat-ok
