| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- import pytest
- from datetime import date, datetime, timedelta
- from types import SimpleNamespace
- from zoneinfo import ZoneInfo
- from room_cal.rules.discount.short_notice import RULE_NAME, _same_day_dates, run
- _TZ = ZoneInfo('America/Los_Angeles')
- def _stub_config(monkeypatch, *, max_days: int = 14, ramp_days: int = 7, checkin_hour: int = 14):
- cfg = SimpleNamespace(
- rules=SimpleNamespace(short_notice=SimpleNamespace(max_days=max_days, ramp_days=ramp_days)),
- global_=SimpleNamespace(property=SimpleNamespace(checkin_hour=checkin_hour)),
- )
- monkeypatch.setattr('room_cal.rules.discount.short_notice.get_config', lambda: cfg)
- def _stub_now(monkeypatch, dt: datetime):
- monkeypatch.setattr('room_cal.rules.discount.short_notice.local_now', lambda: dt)
- def _stub_open(monkeypatch, open_set: set[date]):
- async def _open_dates(start_date, end_date):
- return open_set
- monkeypatch.setattr('room_cal.rules.discount.short_notice.open_dates', _open_dates)
- def test_rule_registered():
- assert RULE_NAME == 'short_notice'
- class TestSameDayDates:
- def test_morning_before_checkin_is_today_only(self):
- now = datetime(2026, 7, 10, 10, 0, tzinfo=_TZ) # 10 AM, checkin_hour=14 -> rollover=2
- assert _same_day_dates(now) == {date(2026, 7, 10)}
- def test_right_at_rollover_is_today_only(self):
- now = datetime(2026, 7, 10, 2, 0, tzinfo=_TZ)
- assert _same_day_dates(now) == {date(2026, 7, 10)}
- def test_afternoon_after_checkin_is_today_and_tomorrow(self):
- now = datetime(2026, 7, 10, 16, 0, tzinfo=_TZ) # 4 PM
- assert _same_day_dates(now) == {date(2026, 7, 10), date(2026, 7, 11)}
- def test_right_at_checkin_hour_is_today_and_tomorrow(self):
- now = datetime(2026, 7, 10, 14, 0, tzinfo=_TZ)
- assert _same_day_dates(now) == {date(2026, 7, 10), date(2026, 7, 11)}
- def test_after_midnight_before_rollover_is_today_and_tomorrow(self):
- # 1 AM on the 10th: still the prior evening's "today + tomorrow" window
- now = datetime(2026, 7, 10, 1, 0, tzinfo=_TZ)
- assert _same_day_dates(now) == {date(2026, 7, 10), date(2026, 7, 11)}
- class TestRun:
- @pytest.mark.asyncio
- async def test_same_day_dates_never_discounted_morning(self, monkeypatch):
- today = date(2026, 7, 10)
- now = datetime(2026, 7, 10, 10, 0, tzinfo=_TZ) # before checkin_hour -> same_day = {today}
- _stub_config(monkeypatch)
- _stub_now(monkeypatch, now)
- # today, tomorrow, and the day after are all open with open neighbors on both sides
- open_set = {today + timedelta(days=i) for i in range(-1, 5)}
- _stub_open(monkeypatch, open_set)
- result = await run(today, today + timedelta(days=3), {})
- assert today not in result
- assert today + timedelta(days=1) in result # tomorrow is NOT same-day in the morning window
- @pytest.mark.asyncio
- async def test_same_day_dates_never_discounted_afternoon(self, monkeypatch):
- today = date(2026, 7, 10)
- now = datetime(2026, 7, 10, 16, 0, tzinfo=_TZ) # after checkin_hour -> same_day = {today, +1}
- _stub_config(monkeypatch)
- _stub_now(monkeypatch, now)
- open_set = {today + timedelta(days=i) for i in range(-1, 5)}
- _stub_open(monkeypatch, open_set)
- result = await run(today, today + timedelta(days=3), {})
- assert today not in result
- assert today + timedelta(days=1) not in result
- assert today + timedelta(days=2) in result
- @pytest.mark.asyncio
- async def test_normal_tiers_unaffected_outside_same_day(self, monkeypatch):
- today = date(2026, 7, 10)
- now = datetime(2026, 7, 10, 10, 0, tzinfo=_TZ)
- _stub_config(monkeypatch, max_days=14, ramp_days=0)
- _stub_now(monkeypatch, now)
- d = today + timedelta(days=3)
- # d has both neighbors open -> 40%; well outside the same-day window
- open_set = {d - timedelta(days=1), d, d + timedelta(days=1)}
- _stub_open(monkeypatch, open_set)
- result = await run(today, today + timedelta(days=5), {})
- assert result[d].value == 40
- @pytest.mark.asyncio
- async def test_empty_window_returns_empty(self, monkeypatch):
- today = date(2026, 7, 10)
- now = datetime(2026, 7, 10, 10, 0, tzinfo=_TZ)
- _stub_config(monkeypatch, max_days=1, ramp_days=0)
- _stub_now(monkeypatch, now)
- _stub_open(monkeypatch, set())
- result = await run(today + timedelta(days=5), today + timedelta(days=5), {})
- assert result == {}
|