From 04e03692556ed336f8b8d07d2d565c724fe84772 Mon Sep 17 00:00:00 2001 From: "2569718930@qq.com" <2569718930@qq.com> Date: Sun, 17 May 2026 15:06:27 +0800 Subject: [PATCH] feat: implement Telegram push notification utility with state management and market filtering --- src/utils/telegram_push.py | 4 ++-- tests/test_telegram_hashtags.py | 42 +++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/utils/telegram_push.py b/src/utils/telegram_push.py index 261d567c..6ebe2107 100644 --- a/src/utils/telegram_push.py +++ b/src/utils/telegram_push.py @@ -1052,7 +1052,7 @@ def _run_high_freq_airport_cycle( deb_pred: Optional[float] = None try: from web.app import _analyze - city_weather = _analyze(city) + city_weather = _analyze(city, force_refresh=True) deb_raw = (city_weather.get("deb") or {}).get("prediction") if deb_raw is not None: deb_pred = float(deb_raw) @@ -1112,7 +1112,7 @@ def _run_high_freq_airport_cycle( and now_ts - last_city_ts > 540): time.sleep(4) try: - city_weather = _analyze(city) + city_weather = _analyze(city, force_refresh=True) deb_raw2 = (city_weather.get("deb") or {}).get("prediction") if deb_raw2 is not None: deb_pred = float(deb_raw2) diff --git a/tests/test_telegram_hashtags.py b/tests/test_telegram_hashtags.py index 33885416..791f1528 100644 --- a/tests/test_telegram_hashtags.py +++ b/tests/test_telegram_hashtags.py @@ -5,6 +5,7 @@ from src.utils.telegram_push import ( MARKET_MONITOR_INTERVAL_SEC, _build_airport_status_message, _build_market_monitor_message, + _run_high_freq_airport_cycle, ) @@ -116,3 +117,44 @@ def test_shenzhen_is_removed_from_telegram_push_city_lists(): assert "shenzhen" not in MARKET_MONITOR_CITIES assert "shenzhen" not in HIGH_FREQ_AIRPORT_CITIES assert "shenzhen" not in HIGH_FREQ_AIRPORT_ICAO + + +def test_high_freq_airport_push_forces_analysis_refresh(monkeypatch): + import src.utils.telegram_push as telegram_push + import web.app as web_app + + calls = [] + + def fake_analyze(city, force_refresh=False, **_kwargs): + calls.append((city, force_refresh)) + return { + "local_time": "12:00", + "current": {"temp": 31.0}, + "deb": {"prediction": 29.0}, + "airport_current": {"max_so_far": 30.0, "max_temp_time": "11:50", "obs_time": "12:00"}, + "mgm_nearby": [ + {"icao": "ZSQD", "temp": 31.0, "obs_time": "2026-05-17T04:00:00Z"}, + ], + } + + class Bot: + def __init__(self): + self.messages = [] + + def send_message(self, chat_id, message): + self.messages.append((chat_id, message)) + + bot = Bot() + monkeypatch.setattr(telegram_push, "HIGH_FREQ_AIRPORT_CITIES", {"qingdao"}) + monkeypatch.setattr(web_app, "_analyze", fake_analyze) + + sent = _run_high_freq_airport_cycle( + bot=bot, + config={}, + chat_ids=["chat-1"], + state={"last_by_city": {}}, + ) + + assert sent is True + assert calls == [("qingdao", True)] + assert bot.messages