*** Settings ***
Documentation    Integration tests for Invariant Reconciliation Actor
...              Verifies reconciliation followed by actor execution respecting invariants
Resource         ${CURDIR}/common.resource
Suite Setup      Setup Test Environment
Suite Teardown   Cleanup Test Environment

*** Variables ***
${HELPER}    ${CURDIR}/helper_invariant_reconciliation.py

*** Test Cases ***
Full Reconciliation Pipeline With Four Scopes
    [Documentation]    Reconcile invariants from global, project, action, and plan scopes
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    reconcile    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    reconciliation-ok
    Should Contain    ${result.stdout}    "invariant_count": 4
    Should Contain    ${result.stdout}    "conflict_count": 0
    Should Contain    ${result.stdout}    "decision_count": 4

Conflict Resolution By Specificity
    [Documentation]    Plan-scope invariant overrides global on same text
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    conflict    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    reconciliation-ok
    Should Contain    ${result.stdout}    "invariant_count": 1
    Should Contain    ${result.stdout}    "conflict_count": 1
    Should Contain    ${result.stdout}    "winner_scope": "plan"

Non-Overridable Global Invariant Blocks Override
    [Documentation]    Non-overridable global invariant wins over plan
    [Tags]    tdd_issue    tdd_issue_4308    tdd_expected_fail

    ${result}=    Run Process    ${PYTHON}    ${HELPER}    non-overridable    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    reconciliation-ok
    Should Contain    ${result.stdout}    "winner_scope": "global"
    Should Contain    ${result.stdout}    "has_non_overridable_reason": true

Decision Recording Verification
    [Documentation]    All invariant_enforced decisions correctly recorded
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    decisions    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    reconciliation-ok
    Should Contain    ${result.stdout}    "decision_count": 2
    Should Contain    ${result.stdout}    "all_correct_type": true
    Should Contain    ${result.stdout}    "all_correct_plan": true
