*** Settings ***
Documentation    Integration tests for conditional log replay in the parallel
...              behave runner (``scripts/run_behave_parallel.py``).
...
...              Verifies that passing chunks suppress output while failed,
...              errored, or crashed chunks surface diagnostics.
Resource         ${CURDIR}/common.resource
Suite Setup      Setup Test Environment
Suite Teardown   Cleanup Test Environment

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

*** Test Cases ***
Passing Chunk Output Is Suppressed
    [Documentation]    A fully passing worker chunk must not trigger log replay.
    [Tags]    testing    parallel    log_filtering
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    passing-chunk-suppressed
    ...    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    passing-chunk-suppressed-ok

Failed Chunk Output Is Replayed
    [Documentation]    A worker chunk with scenario failures must trigger log replay.
    [Tags]    testing    parallel    log_filtering
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    failed-chunk-replayed
    ...    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    failed-chunk-replayed-ok

Crashed Chunk Is Detected
    [Documentation]    A worker chunk that crashes (empty summary) must be detected.
    [Tags]    testing    parallel    log_filtering
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    crashed-chunk-detected
    ...    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    crashed-chunk-detected-ok

Worker Crash Returns Traceback And Empty Summary
    [Documentation]    A worker that raises an unhandled exception must return a
    ...              traceback in stderr and an empty summary so the parent can
    ...              detect the crash.
    [Tags]    testing    parallel    log_filtering
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    worker-crash-handling
    ...    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    worker-crash-handling-ok

Mixed Results Only Replay Failed Chunks
    [Documentation]    When aggregating results from multiple workers, only
    ...              failed chunks should have their output replayed.
    [Tags]    testing    parallel    log_filtering
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    mixed-results-filtering
    ...    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    mixed-results-filtering-ok

Summary Merge Semantics Are Preserved
    [Documentation]    The summary merge logic must correctly combine counts
    ...              from multiple worker chunks regardless of filtering.
    [Tags]    testing    parallel    log_filtering
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    summary-merge-preserved
    ...    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    summary-merge-preserved-ok
