test_short_notice_rule.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import pytest
  2. from datetime import date, datetime, timedelta
  3. from types import SimpleNamespace
  4. from zoneinfo import ZoneInfo
  5. from room_cal.rules.discount.short_notice import RULE_NAME, _same_day_dates, run
  6. _TZ = ZoneInfo('America/Los_Angeles')
  7. def _stub_config(monkeypatch, *, max_days: int = 14, ramp_days: int = 7, checkin_hour: int = 14):
  8. cfg = SimpleNamespace(
  9. rules=SimpleNamespace(short_notice=SimpleNamespace(max_days=max_days, ramp_days=ramp_days)),
  10. global_=SimpleNamespace(property=SimpleNamespace(checkin_hour=checkin_hour)),
  11. )
  12. monkeypatch.setattr('room_cal.rules.discount.short_notice.get_config', lambda: cfg)
  13. def _stub_now(monkeypatch, dt: datetime):
  14. monkeypatch.setattr('room_cal.rules.discount.short_notice.local_now', lambda: dt)
  15. def _stub_open(monkeypatch, open_set: set[date]):
  16. async def _open_dates(start_date, end_date):
  17. return open_set
  18. monkeypatch.setattr('room_cal.rules.discount.short_notice.open_dates', _open_dates)
  19. def test_rule_registered():
  20. assert RULE_NAME == 'short_notice'
  21. class TestSameDayDates:
  22. def test_morning_before_checkin_is_today_only(self):
  23. now = datetime(2026, 7, 10, 10, 0, tzinfo=_TZ) # 10 AM, checkin_hour=14 -> rollover=2
  24. assert _same_day_dates(now) == {date(2026, 7, 10)}
  25. def test_right_at_rollover_is_today_only(self):
  26. now = datetime(2026, 7, 10, 2, 0, tzinfo=_TZ)
  27. assert _same_day_dates(now) == {date(2026, 7, 10)}
  28. def test_afternoon_after_checkin_is_today_and_tomorrow(self):
  29. now = datetime(2026, 7, 10, 16, 0, tzinfo=_TZ) # 4 PM
  30. assert _same_day_dates(now) == {date(2026, 7, 10), date(2026, 7, 11)}
  31. def test_right_at_checkin_hour_is_today_and_tomorrow(self):
  32. now = datetime(2026, 7, 10, 14, 0, tzinfo=_TZ)
  33. assert _same_day_dates(now) == {date(2026, 7, 10), date(2026, 7, 11)}
  34. def test_after_midnight_before_rollover_is_today_and_tomorrow(self):
  35. # 1 AM on the 10th: still the prior evening's "today + tomorrow" window
  36. now = datetime(2026, 7, 10, 1, 0, tzinfo=_TZ)
  37. assert _same_day_dates(now) == {date(2026, 7, 10), date(2026, 7, 11)}
  38. class TestRun:
  39. @pytest.mark.asyncio
  40. async def test_same_day_dates_never_discounted_morning(self, monkeypatch):
  41. today = date(2026, 7, 10)
  42. now = datetime(2026, 7, 10, 10, 0, tzinfo=_TZ) # before checkin_hour -> same_day = {today}
  43. _stub_config(monkeypatch)
  44. _stub_now(monkeypatch, now)
  45. # today, tomorrow, and the day after are all open with open neighbors on both sides
  46. open_set = {today + timedelta(days=i) for i in range(-1, 5)}
  47. _stub_open(monkeypatch, open_set)
  48. result = await run(today, today + timedelta(days=3), {})
  49. assert today not in result
  50. assert today + timedelta(days=1) in result # tomorrow is NOT same-day in the morning window
  51. @pytest.mark.asyncio
  52. async def test_same_day_dates_never_discounted_afternoon(self, monkeypatch):
  53. today = date(2026, 7, 10)
  54. now = datetime(2026, 7, 10, 16, 0, tzinfo=_TZ) # after checkin_hour -> same_day = {today, +1}
  55. _stub_config(monkeypatch)
  56. _stub_now(monkeypatch, now)
  57. open_set = {today + timedelta(days=i) for i in range(-1, 5)}
  58. _stub_open(monkeypatch, open_set)
  59. result = await run(today, today + timedelta(days=3), {})
  60. assert today not in result
  61. assert today + timedelta(days=1) not in result
  62. assert today + timedelta(days=2) in result
  63. @pytest.mark.asyncio
  64. async def test_normal_tiers_unaffected_outside_same_day(self, monkeypatch):
  65. today = date(2026, 7, 10)
  66. now = datetime(2026, 7, 10, 10, 0, tzinfo=_TZ)
  67. _stub_config(monkeypatch, max_days=14, ramp_days=0)
  68. _stub_now(monkeypatch, now)
  69. d = today + timedelta(days=3)
  70. # d has both neighbors open -> 40%; well outside the same-day window
  71. open_set = {d - timedelta(days=1), d, d + timedelta(days=1)}
  72. _stub_open(monkeypatch, open_set)
  73. result = await run(today, today + timedelta(days=5), {})
  74. assert result[d].value == 40
  75. @pytest.mark.asyncio
  76. async def test_empty_window_returns_empty(self, monkeypatch):
  77. today = date(2026, 7, 10)
  78. now = datetime(2026, 7, 10, 10, 0, tzinfo=_TZ)
  79. _stub_config(monkeypatch, max_days=1, ramp_days=0)
  80. _stub_now(monkeypatch, now)
  81. _stub_open(monkeypatch, set())
  82. result = await run(today + timedelta(days=5), today + timedelta(days=5), {})
  83. assert result == {}