*** Settings ***
Documentation    Integration tests for ACMS Context Tier runtime logic (issue #821)
...              Verifies auto-promotion, staleness enforcement, budget eviction,
...              and tier transition event emission.
Resource         ${CURDIR}/common.resource
Suite Setup      Setup Test Environment
Suite Teardown   Cleanup Test Environment

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

*** Test Cases ***
Auto Promotion On Repeated Access
    [Documentation]    Accessing a cold-tier fragment 5 times auto-promotes it
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    auto-promote    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    context-tier-runtime-auto-promote-ok

Staleness Enforcement Demotes Stale Fragments
    [Documentation]    enforce_staleness() demotes hot fragments older than TTL
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    staleness    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    context-tier-runtime-staleness-ok

Budget Enforcement Evicts On Overflow
    [Documentation]    Storing beyond hot-tier token budget evicts LRU fragment
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    budget-evict    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    context-tier-runtime-budget-evict-ok

Tier Transition Event Emission
    [Documentation]    Promotion/demotion/eviction emits DomainEvent via EventBus
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    event-emission    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    context-tier-runtime-event-emission-ok
