import importlib.util import pathlib _p = pathlib.Path(__file__).resolve().parents[2] / "scripts" / "health_watch.py" _spec = importlib.util.spec_from_file_location("health_watch", _p) hw = importlib.util.module_from_spec(_spec) _spec.loader.exec_module(hw) _OK = {"status": "healthy", "schedulers": {"swing": {"alive": True, "stale": False}}, "db_worker": {"alive": True}, "dlq": {"manual": 0}} def test_healthy_has_no_problems(): assert hw.problems_v1(_OK) == [] def test_dead_scheduler_detected_even_if_http_200(): h = {**_OK, "status": "degraded", "schedulers": {"swing": {"alive": False, "stale": False}}} p = hw.problems_v1(h) assert any("swing" in x for x in p) def test_stale_and_dbworker_and_dlq(): h = {**_OK, "schedulers": {"dt": {"alive": True, "stale": True, "last_cycle_age": 900}}, "db_worker": {"alive": False}, "dlq": {"manual": 2}} assert len(hw.problems_v1(h)) == 3 def test_non_dict_is_problem(): assert hw.problems_v1("") != [] def test_alert_after_threshold_once_then_recover(): s, a = hw.next_state_v1({}, ["x"], 2) assert a is None s, a = hw.next_state_v1(s, ["x"], 2) assert a == "alert" s, a = hw.next_state_v1(s, ["x"], 2) assert a is None s, a = hw.next_state_v1(s, [], 2) assert a == "recover" and s == {"fails": 0, "alerted": False}