Buckets:

download
raw
4.86 kB
"""
Tests for the rule engine module.
Tests cover:
- Filter matching logic (_matches_filter)
- Threshold condition evaluation (_evaluate_threshold_condition)
- Cooldown enforcement
- Engine health reporting
"""
import pytest
from datetime import datetime, timezone, timedelta
from a2a_server.rule_engine import ProactiveRuleEngine
class TestMatchesFilter:
def setup_method(self):
self.engine = ProactiveRuleEngine()
def test_no_filter_matches_all(self):
assert self.engine._matches_filter(None, {'any': 'data'})
def test_empty_filter_matches_all(self):
assert self.engine._matches_filter({}, {'any': 'data'})
def test_exact_match(self):
assert self.engine._matches_filter(
{'status': 'failed'},
{'status': 'failed', 'code': 500},
)
def test_exact_mismatch(self):
assert not self.engine._matches_filter(
{'status': 'failed'},
{'status': 'ok'},
)
def test_list_filter_in(self):
assert self.engine._matches_filter(
{'status': ['failed', 'degraded']},
{'status': 'degraded'},
)
def test_list_filter_not_in(self):
assert not self.engine._matches_filter(
{'status': ['failed', 'degraded']},
{'status': 'healthy'},
)
def test_multiple_conditions_and_logic(self):
assert self.engine._matches_filter(
{'status': 'failed', 'region': 'us-east'},
{'status': 'failed', 'region': 'us-east', 'extra': True},
)
def test_multiple_conditions_partial_match(self):
assert not self.engine._matches_filter(
{'status': 'failed', 'region': 'us-east'},
{'status': 'failed', 'region': 'eu-west'},
)
def test_missing_key_fails(self):
assert not self.engine._matches_filter(
{'status': 'failed'},
{'code': 500},
)
def test_non_dict_event_data_fails(self):
assert not self.engine._matches_filter(
{'status': 'failed'},
'just a string',
)
class TestThresholdCondition:
def setup_method(self):
self.engine = ProactiveRuleEngine()
def test_status_equals(self):
assert self.engine._evaluate_threshold_condition(
"status == 'failed'", 'failed', {}
)
def test_status_equals_mismatch(self):
assert not self.engine._evaluate_threshold_condition(
"status == 'failed'", 'healthy', {}
)
def test_status_not_equals(self):
assert self.engine._evaluate_threshold_condition(
"status != 'healthy'", 'degraded', {}
)
def test_status_not_equals_mismatch(self):
assert not self.engine._evaluate_threshold_condition(
"status != 'healthy'", 'healthy', {}
)
def test_empty_condition_triggers_on_non_healthy(self):
assert self.engine._evaluate_threshold_condition('', 'failed', {})
assert not self.engine._evaluate_threshold_condition('', 'healthy', {})
def test_double_quoted_value(self):
assert self.engine._evaluate_threshold_condition(
'status == "failed"', 'failed', {}
)
class TestEngineHealth:
def test_initial_health(self):
engine = ProactiveRuleEngine()
health = engine.get_health()
assert health['running'] is False
assert 'check_interval_seconds' in health
def test_stats_initially_none(self):
engine = ProactiveRuleEngine()
assert engine.get_stats() is None
class TestCooldownLogic:
"""Test that cooldown is respected before triggering."""
def setup_method(self):
self.engine = ProactiveRuleEngine()
def test_cooldown_not_elapsed(self):
"""A rule triggered 30s ago with 300s cooldown should be skipped."""
# This is tested implicitly through _try_trigger_rule,
# but we verify the core logic here
last_triggered = datetime.now(timezone.utc) - timedelta(seconds=30)
cooldown = 300
now = datetime.now(timezone.utc)
elapsed = (now - last_triggered).total_seconds()
assert elapsed < cooldown # Should be skipped
def test_cooldown_elapsed(self):
"""A rule triggered 600s ago with 300s cooldown should be allowed."""
last_triggered = datetime.now(timezone.utc) - timedelta(seconds=600)
cooldown = 300
now = datetime.now(timezone.utc)
elapsed = (now - last_triggered).total_seconds()
assert elapsed >= cooldown # Should be allowed
def test_no_previous_trigger_always_allowed(self):
"""A rule never triggered should always be allowed."""
# last_triggered_at is None — should be allowed
last_triggered = None
assert last_triggered is None # No cooldown check needed

Xet Storage Details

Size:
4.86 kB
·
Xet hash:
cd9aab137b252aa4385944046d4dfee0d6f736717dfec92dc148d772e0a77709

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.