Procházet zdrojové kódy

short_notice: never discount same-day check-in dates

Check-in time (checkin_hour, default 2 PM) is the real "same day" boundary,
not midnight: a booking made after checkin_hour for "today" is really asking
for an already-closed same-day slot, same as asking for tomorrow. So the
excluded set is time-of-day dependent — {today, tomorrow} from checkin_hour
until 2 AM the next day, {today} from 2 AM until checkin_hour (2 AM derived
as checkin_hour - 12).

Adds local_now() alongside local_today() in _utils.py since this is the first
rule that needs time-of-day, not just date.
ZiB před 12 hodinami
rodič
revize
084cfd3043

+ 6 - 0
room_cal/_utils.py

@@ -500,6 +500,12 @@ def local_today() -> date:
   return datetime.now(tz).date()
 
 
+def local_now() -> datetime:
+  """Return the current time-aware datetime in the configured property timezone."""
+  tz = ZoneInfo(get_config().global_.property.timezone)
+  return datetime.now(tz)
+
+
 def services() -> list[str]:
   """Return the keys of all successfully loaded services."""
   from room_cal.service_api.client import ServiceAPI

+ 31 - 3
room_cal/rules/discount/short_notice.py

@@ -4,10 +4,13 @@ Discount tiers for each qualifying open date:
   40% — both the day before and the day after are open
   30% — day before is open (day after is not)
   20% — day after is open (day before is not)
+
+Same-day dates never qualify (see `_same_day_dates`) — a guest booking that close to
+check-in isn't the "fill a vacant gap" case this rule exists for.
 """
-from datetime import date, timedelta
+from datetime import date, datetime, timedelta
 
-from room_cal._utils import get_config, iter_dates, local_today
+from room_cal._utils import get_config, iter_dates, local_now
 from room_cal.discount import DiscountData, DiscountList, DiscountType
 from room_cal.rules._utils import open_dates
 from room_cal.rules.discount._utils import apply_ramp
@@ -15,10 +18,31 @@ from room_cal.rules.discount._utils import apply_ramp
 RULE_NAME = 'short_notice'
 
 
+def _same_day_dates(now: datetime) -> set[date]:
+  """Dates too close to check-in to be eligible for the short-notice discount.
+
+  The property's check-in time (`checkin_hour`, default 2 PM) is the day boundary that
+  matters here, not midnight — a booking made at 3 PM for "today" is really asking for a
+  same-day check-in that's effectively already closed, same as if they'd asked for
+  "tomorrow". So the excluded set is time-of-day dependent:
+    - from `checkin_hour` until 2 AM the next calendar day: {today, tomorrow}
+    - from 2 AM until `checkin_hour`: {today}
+  The 2 AM cutoff is `checkin_hour` minus 12 hours; this assumes an afternoon check-in
+  (`checkin_hour >= 12`) so that rollover point falls earlier the same calendar day.
+  """
+  checkin_hour = get_config().global_.property.checkin_hour
+  rollover_hour = checkin_hour - 12
+  today = now.date()
+  if rollover_hour <= now.hour < checkin_hour:
+    return {today}
+  return {today, today + timedelta(days=1)}
+
+
 async def run(start_date: date, end_date: date, data: DiscountList) -> DiscountList:
   rules = get_config().rules.short_notice
   max_days = rules.max_days
-  today = local_today()
+  now = local_now()
+  today = now.date()
   window_end = today + timedelta(days=max_days - 1)
 
   effective_start = max(start_date, today)
@@ -30,8 +54,12 @@ async def run(start_date: date, end_date: date, data: DiscountList) -> DiscountL
   fetch_end = effective_end + timedelta(days=1)
   open_ = await open_dates(fetch_start, fetch_end)
 
+  same_day = _same_day_dates(now)
+
   result: DiscountList = {}
   for d in iter_dates(effective_start, effective_end):
+    if d in same_day:
+      continue
     if d not in open_:
       continue
     before_open = (d - timedelta(days=1)) in open_

+ 114 - 0
tests/test_short_notice_rule.py

@@ -0,0 +1,114 @@
+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 == {}