*** Settings ***
Documentation    Integration tests for Event System Domain Event Taxonomy
...              Validates EventType completeness, DomainEvent model fields,
...              ReactiveEventBus emit/subscribe/stream/audit_log, and
...              LoggingEventBus conformance.
Resource         ${CURDIR}/common.resource
Suite Setup      Setup Test Environment
Suite Teardown   Cleanup Test Environment

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

*** Test Cases ***
EventType Enum Has At Least 43 Members
    [Documentation]    Verify the EventType enum defines at least 43 event types
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    event_type_count
    ...    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    event-type-count-ok

EventType Covers All Required Domains
    [Documentation]    Verify EventType has values for all required domains
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    event_type_domains
    ...    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    event-type-domains-ok

EventType Values Follow Dot Convention
    [Documentation]    Every value matches <domain>.<action>
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    event_type_dot_convention
    ...    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    event-type-dot-convention-ok

DomainEvent Has All Required Fields
    [Documentation]    Verify DomainEvent includes all spec-required fields
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    domain_event_fields
    ...    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    domain-event-fields-ok

DomainEvent JSON Round Trip
    [Documentation]    Serialize and deserialize a fully populated DomainEvent
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    domain_event_json_roundtrip
    ...    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    domain-event-json-roundtrip-ok

DomainEvent Is Immutable
    [Documentation]    Frozen model rejects field mutation
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    domain_event_immutable
    ...    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    domain-event-immutable-ok

ReactiveEventBus Emit Subscribe Audit
    [Documentation]    Emit dispatches to handlers and persists to audit_log
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    reactive_emit_subscribe_audit
    ...    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    reactive-emit-subscribe-audit-ok

ReactiveEventBus Continues Dispatching After Handler Error
    [Documentation]    A failing handler does not block subsequent handlers
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    reactive_handler_exception_isolation
    ...    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    reactive-handler-exception-isolation-ok

ReactiveEventBus Continues After Stream Observer Error
    [Documentation]    A failing stream observer does not block handlers or audit
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    reactive_stream_exception_isolation
    ...    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    reactive-stream-exception-isolation-ok

ReactiveEventBus Stream Observable
    [Documentation]    stream property exposes rx.Observable that receives events
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    reactive_stream_observable
    ...    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    reactive-stream-observable-ok

ReactiveEventBus Stream Is Read Only
    [Documentation]    stream should not expose Subject mutation methods
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    reactive_stream_read_only
    ...    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    reactive-stream-read-only-ok

ReactiveEventBus Audit Log Order
    [Documentation]    audit_log preserves emission order
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    audit_log_order
    ...    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    audit-log-order-ok

ReactiveEventBus Audit Log Defensive Copy
    [Documentation]    audit_log returns a copy — external mutation is safe
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    audit_log_defensive_copy
    ...    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    audit-log-defensive-copy-ok

ReactiveEventBus Audit Log Retention Cap
    [Documentation]    configured cap keeps only newest events and supports clearing
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    audit_log_retention_cap
    ...    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    audit-log-retention-cap-ok

ReactiveEventBus Rejects Invalid Audit Log Retention Cap
    [Documentation]    max_audit_log_size must be positive when provided
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    audit_log_invalid_retention_cap
    ...    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    audit-log-invalid-retention-cap-ok

Log Correlation Via Shared Correlation ID
    [Documentation]    Events sharing a correlation_id are traceable in audit_log
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    log_correlation
    ...    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    log-correlation-ok

Protocol Conformance For Both Bus Implementations
    [Documentation]    Both ReactiveEventBus and LoggingEventBus satisfy EventBus
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    protocol_conformance
    ...    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    protocol-conformance-ok

LoggingEventBus Emit Subscribe
    [Documentation]    LoggingEventBus dispatches to subscribed handlers
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    logging_emit_subscribe
    ...    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    logging-emit-subscribe-ok

LoggingEventBus Continues Dispatching After Handler Error
    [Documentation]    LoggingEventBus isolates handler exceptions and continues
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    logging_handler_exception_isolation
    ...    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    logging-handler-exception-isolation-ok

ReactiveEventBus Type Validation
    [Documentation]    Emit/subscribe reject invalid argument types
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    type_validation
    ...    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    type-validation-ok
