mirror of
https://github.com/Dasemu/skipredictor.git
synced 2026-11-26 15:26:15 +00:00
189 lines
5.3 KiB
Python
189 lines
5.3 KiB
Python
"""Unit tests for bot-side prediction selection rules."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
import types
|
|
from datetime import datetime
|
|
from zoneinfo import ZoneInfo
|
|
|
|
telegram_module = types.ModuleType("telegram")
|
|
|
|
|
|
class _Update:
|
|
ALL_TYPES = object()
|
|
|
|
|
|
telegram_module.Update = _Update
|
|
|
|
constants_module = types.ModuleType("telegram.constants")
|
|
|
|
|
|
class _ParseMode:
|
|
HTML = "HTML"
|
|
|
|
|
|
constants_module.ParseMode = _ParseMode
|
|
|
|
ext_module = types.ModuleType("telegram.ext")
|
|
|
|
|
|
class _ApplicationBuilder:
|
|
def token(self, token: str):
|
|
return self
|
|
|
|
def build(self):
|
|
return types.SimpleNamespace(
|
|
add_handler=lambda *args, **kwargs: None,
|
|
run_polling=lambda *args, **kwargs: None,
|
|
)
|
|
|
|
|
|
class _Application:
|
|
@staticmethod
|
|
def builder():
|
|
return _ApplicationBuilder()
|
|
|
|
|
|
class _CommandHandler:
|
|
def __init__(self, *args, **kwargs) -> None:
|
|
self.args = args
|
|
self.kwargs = kwargs
|
|
|
|
|
|
class _ContextTypes:
|
|
DEFAULT_TYPE = object
|
|
|
|
|
|
ext_module.Application = _Application
|
|
ext_module.CommandHandler = _CommandHandler
|
|
ext_module.ContextTypes = _ContextTypes
|
|
|
|
sys.modules.setdefault("telegram", telegram_module)
|
|
sys.modules.setdefault("telegram.constants", constants_module)
|
|
sys.modules.setdefault("telegram.ext", ext_module)
|
|
|
|
from bot import telegram_bot
|
|
|
|
|
|
class FakeStore:
|
|
"""In-memory stand-in for SQLiteStore used in selection tests."""
|
|
|
|
def __init__(self, by_date: dict[str, dict], latest: dict | None = None) -> None:
|
|
self.by_date = by_date
|
|
self.latest = latest
|
|
self.db_path = "/tmp/fake.db"
|
|
self.requested_dates: list[str] = []
|
|
|
|
def load_predictions_for_date(self, *, prediction_date: str, run_kind: str):
|
|
self.requested_dates.append(prediction_date)
|
|
return self.by_date.get(prediction_date)
|
|
|
|
def load_latest_predictions(self, *, run_kind: str):
|
|
return self.latest
|
|
|
|
|
|
def test_load_predictions_prefers_today_before_ten():
|
|
"""Before 10:00 local time the bot should prefer today's run."""
|
|
today_payload = {"prediction_date": "2026-03-13", "stations": {}}
|
|
tomorrow_payload = {"prediction_date": "2026-03-14", "stations": {}}
|
|
store = FakeStore(
|
|
{
|
|
"2026-03-13": today_payload,
|
|
"2026-03-14": tomorrow_payload,
|
|
}
|
|
)
|
|
|
|
loaded = telegram_bot.load_predictions(
|
|
now=datetime(2026, 3, 13, 9, 30, tzinfo=ZoneInfo("Europe/Madrid")),
|
|
store=store,
|
|
)
|
|
|
|
assert loaded == today_payload
|
|
assert store.requested_dates == ["2026-03-13"]
|
|
|
|
|
|
def test_load_predictions_prefers_tomorrow_after_ten():
|
|
"""From 10:00 onwards the bot should prefer tomorrow's run."""
|
|
today_payload = {"prediction_date": "2026-03-13", "stations": {}}
|
|
tomorrow_payload = {"prediction_date": "2026-03-14", "stations": {}}
|
|
store = FakeStore(
|
|
{
|
|
"2026-03-13": today_payload,
|
|
"2026-03-14": tomorrow_payload,
|
|
}
|
|
)
|
|
|
|
loaded = telegram_bot.load_predictions(
|
|
now=datetime(2026, 3, 13, 10, 0, tzinfo=ZoneInfo("Europe/Madrid")),
|
|
store=store,
|
|
)
|
|
|
|
assert loaded == tomorrow_payload
|
|
assert store.requested_dates == ["2026-03-14"]
|
|
|
|
|
|
def test_load_predictions_falls_back_when_preferred_dates_are_missing():
|
|
"""If the preferred dates are absent, fall back to the latest published run."""
|
|
fallback_payload = {"prediction_date": "2026-03-16", "stations": {}}
|
|
store = FakeStore({}, latest=fallback_payload)
|
|
|
|
loaded = telegram_bot.load_predictions(
|
|
now=datetime(2026, 3, 13, 8, 0, tzinfo=ZoneInfo("Europe/Madrid")),
|
|
store=store,
|
|
)
|
|
|
|
assert loaded == fallback_payload
|
|
assert store.requested_dates == ["2026-03-13", "2026-03-14"]
|
|
|
|
|
|
def test_prediction_dates_window_returns_consecutive_dates():
|
|
"""Window helpers must return consecutive dates from the requested offset."""
|
|
dates = telegram_bot.prediction_dates_window(
|
|
start_offset=1,
|
|
length=3,
|
|
now=datetime(2026, 3, 13, 8, 0, tzinfo=ZoneInfo("Europe/Madrid")),
|
|
)
|
|
|
|
assert dates == ["2026-03-14", "2026-03-15", "2026-03-16"]
|
|
|
|
|
|
def test_format_daily_predictions_includes_all_three_stations():
|
|
"""Daily overview messages must include the three configured stations."""
|
|
payload = {
|
|
"prediction_date": "2026-03-14",
|
|
"stations": {
|
|
"pajares": {
|
|
"open_probability": 0.85,
|
|
"km_open": 15.2,
|
|
"crowd_level": "medium",
|
|
"weather_summary": "Nevadas débiles",
|
|
"rule_overrides": [],
|
|
"prediction_source": "hybrid",
|
|
},
|
|
"sanisidro": {
|
|
"open_probability": 0.55,
|
|
"km_open": 12.8,
|
|
"crowd_level": "high",
|
|
"weather_summary": "Viento moderado",
|
|
"rule_overrides": [],
|
|
"prediction_source": "ml",
|
|
},
|
|
"fuentes": {
|
|
"open_probability": 0.35,
|
|
"km_open": 2.0,
|
|
"crowd_level": "low",
|
|
"weather_summary": "Lluvia a media mañana",
|
|
"rule_overrides": ["Cota de nieve alta."],
|
|
"prediction_source": "rules",
|
|
},
|
|
},
|
|
}
|
|
|
|
text = telegram_bot.format_daily_predictions(payload)
|
|
|
|
assert "Valgrande-Pajares" in text
|
|
assert "San Isidro" in text
|
|
assert "Fuentes de Invierno" in text
|
|
assert "2026-03-14" in text
|