*** Settings ***
Documentation     Integration tests for correction subtree isolation.
...               Verifies that decision corrections recompute only the
...               affected subtree while excluding root and sibling nodes.
Resource          ${CURDIR}/common.resource
Suite Setup       Setup Test Environment
Suite Teardown    Cleanup Test Environment

*** Variables ***
${HELPER}    robot/helper_correction_subtree_isolation.py

*** Test Cases ***
Leaf Node Isolation
    [Documentation]    Target a leaf — only the leaf is affected
    [Tags]    phase2    correction    subtree    isolation
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    leaf-isolation    cwd=${WORKSPACE}    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    leaf-isolation-ok

Middle Node Isolation
    [Documentation]    Target a middle node — middle and descendants affected
    [Tags]    phase2    correction    subtree    isolation
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    middle-node-isolation    cwd=${WORKSPACE}    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    middle-node-isolation-ok

Root Targets All Decisions
    [Documentation]    Target root — all decisions affected, none excluded
    [Tags]    phase2    correction    subtree    isolation
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    root-targets-all    cwd=${WORKSPACE}    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    root-targets-all-ok

Rollback Tier Computation
    [Documentation]    Verify tier depth at depth 0, 1, 2, and 3
    [Tags]    phase2    correction    subtree    tier
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    rollback-tier-computation    cwd=${WORKSPACE}    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    rollback-tier-computation-ok

Enhanced Dry Run Report
    [Documentation]    Dry-run report includes excluded decisions and tier
    [Tags]    phase2    correction    subtree    dryrun
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    dry-run-report-enhanced    cwd=${WORKSPACE}    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    dry-run-report-enhanced-ok

Dry Run Tier Zero Warning
    [Documentation]    Root targeting triggers tier-0 warning
    [Tags]    phase2    correction    subtree    dryrun    tier
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    dry-run-tier-zero-warning    cwd=${WORKSPACE}    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    dry-run-tier-zero-warning-ok

Subtree Isolation Validation
    [Documentation]    Isolation validation passes for non-root targets
    [Tags]    phase2    correction    subtree    validation
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    subtree-isolation-validation    cwd=${WORKSPACE}    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    subtree-isolation-validation-ok

Edge Case Empty Tree
    [Documentation]    Edge cases: empty tree, unknown node, root parent
    [Tags]    phase2    correction    subtree    edge
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    edge-case-empty-tree    cwd=${WORKSPACE}    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    edge-case-empty-tree-ok

Influence DAG Traversal
    [Documentation]    Influence edges pull additional decisions into affected set
    [Tags]    phase2    correction    subtree    influence
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    influence-dag-traversal    cwd=${WORKSPACE}    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    influence-dag-traversal-ok

Append Mode Correction
    [Documentation]    Append mode spawns a child plan without impact analysis
    [Tags]    phase2    correction    subtree    append
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    append-mode-correction    cwd=${WORKSPACE}    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    append-mode-correction-ok

Dry Run Enforcement
    [Documentation]    Dry-run corrections cannot be executed
    [Tags]    phase2    correction    subtree    dryrun    enforcement
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    dry-run-enforcement    cwd=${WORKSPACE}    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    dry-run-enforcement-ok

Execute Revert Non Dry Run
    [Documentation]    Revert correction executes end-to-end with correct results
    [Tags]    phase2    correction    subtree    revert
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    execute-revert-non-dry-run    cwd=${WORKSPACE}    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    execute-revert-non-dry-run-ok

Status Guard Enforcement
    [Documentation]    Applied and cancelled corrections cannot be re-executed
    [Tags]    phase2    correction    subtree    guard
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    status-guard-enforcement    cwd=${WORKSPACE}    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    status-guard-enforcement-ok

Mode Mismatch Enforcement
    [Documentation]    execute_revert on append and execute_append on revert both raise
    [Tags]    phase2    correction    subtree    guard
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    mode-mismatch-enforcement    cwd=${WORKSPACE}    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    mode-mismatch-enforcement-ok

Diamond DAG Topology
    [Documentation]    Convergent (diamond) DAG topology does not cause duplicate visits
    [Tags]    phase2    correction    subtree    influence    diamond
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    diamond-dag-topology    cwd=${WORKSPACE}    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    diamond-dag-topology-ok

Dry Run Exception Recovery
    [Documentation]    Dry-run report restores status even when analyze_impact fails
    [Tags]    phase2    correction    subtree    dryrun    recovery
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    dry-run-exception-recovery    cwd=${WORKSPACE}    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    dry-run-exception-recovery-ok

Execute Revert With Influence Edges
    [Documentation]    Revert correction works correctly with influence DAG edges
    [Tags]    phase2    correction    subtree    revert    influence
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    execute-revert-with-influence    cwd=${WORKSPACE}    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    execute-revert-with-influence-ok

DAG Only Nodes In Excluded Set
    [Documentation]    Nodes appearing only in the influence DAG are in the excluded set
    [Tags]    phase2    correction    subtree    influence    excluded
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    dag-only-nodes-excluded    cwd=${WORKSPACE}    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    dag-only-nodes-excluded-ok
