Просмотр исходного кода

rules: generalize same-day exclusion into its own discount rule

Pulls the same-day-booking exclusion out of short_notice (just added there)
into a standalone "same_day" transformer rule, mirroring min_price: it strips
any date in the current same-day window from the accumulated discount list,
regardless of which earlier rule produced it, so future generator rules don't
each need to reimplement this time-of-day logic. Wired into the this_month
pipeline just before min_price.

short_notice reverts to pure date-based logic (local_today(), no same-day
awareness of its own).
ZiB 14 часов назад
Родитель
Сommit
a3c7786317

+ 1 - 0
room_cal/config-all.json

@@ -28,6 +28,7 @@
           "short_notice",
           "fill_month",
           "no_single_nights",
+          "same_day",
           "min_price"
         ]
       },

+ 1 - 1
room_cal/config.json

@@ -38,7 +38,7 @@
       "feed_name": "Fire"
     },
     "pipelines": {
-      "this_month": {"type": "discount", "rules": ["short_notice", "fill_month", "no_single_nights", "min_price"]},
+      "this_month": {"type": "discount", "rules": ["short_notice", "fill_month", "no_single_nights", "same_day", "min_price"]},
       "future_block":  {"type": "closed",   "rules": ["future_block"]},
       "special_events": {"type": "price",    "rules": ["fire"]}
     }

+ 40 - 0
room_cal/rules/discount/same_day.py

@@ -0,0 +1,40 @@
+"""Strip same-day (and near-same-day) discounts from the accumulated discount list.
+
+Transformer rule: removes any date that falls in the current same-day window — see
+`_same_day_dates` — from the accumulated DiscountList, regardless of which earlier rule
+in the pipeline generated it. Centralizes the same-day exclusion so individual generator
+rules (short_notice, fill_month, etc.) don't each need to reimplement this time-of-day logic.
+"""
+from datetime import date, datetime, timedelta
+
+from room_cal._utils import get_config, local_now
+from room_cal.discount import DiscountList
+
+RULE_NAME = 'same_day'
+
+
+def _same_day_dates(now: datetime) -> set[date]:
+  """Dates too close to check-in to be eligible for any rule-based 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:
+  if not data:
+    return {}
+  same_day = _same_day_dates(local_now())
+  return {d: rec for d, rec in data.items() if d not in same_day}

+ 4 - 30
room_cal/rules/discount/short_notice.py

@@ -5,12 +5,11 @@ Discount tiers for each qualifying open date:
   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.
+Same-day exclusion lives in the separate `same_day` rule, not here — see that module.
 """
-from datetime import date, datetime, timedelta
+from datetime import date, timedelta
 
-from room_cal._utils import get_config, iter_dates, local_now
+from room_cal._utils import get_config, iter_dates, local_today
 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
@@ -18,31 +17,10 @@ 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
-  now = local_now()
-  today = now.date()
+  today = local_today()
   window_end = today + timedelta(days=max_days - 1)
 
   effective_start = max(start_date, today)
@@ -54,12 +32,8 @@ 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_

+ 103 - 0
tests/test_same_day_rule.py

@@ -0,0 +1,103 @@
+import pytest
+from datetime import date, datetime, timedelta
+from types import SimpleNamespace
+from zoneinfo import ZoneInfo
+
+from room_cal.discount import DiscountData, DiscountType
+from room_cal.rules.discount.same_day import RULE_NAME, _same_day_dates, run
+
+_TZ = ZoneInfo('America/Los_Angeles')
+
+
+def _stub_config(monkeypatch, *, checkin_hour: int = 14):
+  cfg = SimpleNamespace(global_=SimpleNamespace(property=SimpleNamespace(checkin_hour=checkin_hour)))
+  monkeypatch.setattr('room_cal.rules.discount.same_day.get_config', lambda: cfg)
+
+
+def _stub_now(monkeypatch, dt: datetime):
+  monkeypatch.setattr('room_cal.rules.discount.same_day.local_now', lambda: dt)
+
+
+def test_rule_registered():
+  assert RULE_NAME == 'same_day'
+
+
+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_empty_input_returns_empty(self, monkeypatch):
+    assert await run(date(2026, 1, 1), date(2026, 1, 1), {}) == {}
+
+  @pytest.mark.asyncio
+  async def test_strips_same_day_dates_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)
+    data = {
+      today: DiscountData(type=DiscountType.PERCENT, value=40),
+      today + timedelta(days=1): DiscountData(type=DiscountType.PERCENT, value=30),
+      today + timedelta(days=2): DiscountData(type=DiscountType.PERCENT, value=20),
+    }
+
+    result = await run(today, today + timedelta(days=2), data)
+
+    assert today not in result
+    assert today + timedelta(days=1) in result  # tomorrow is NOT same-day in the morning window
+    assert today + timedelta(days=2) in result
+
+  @pytest.mark.asyncio
+  async def test_strips_same_day_dates_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)
+    data = {
+      today: DiscountData(type=DiscountType.PERCENT, value=40),
+      today + timedelta(days=1): DiscountData(type=DiscountType.PERCENT, value=30),
+      today + timedelta(days=2): DiscountData(type=DiscountType.PERCENT, value=20),
+    }
+
+    result = await run(today, today + timedelta(days=2), data)
+
+    assert today not in result
+    assert today + timedelta(days=1) not in result
+    assert today + timedelta(days=2) in result
+    assert result[today + timedelta(days=2)].value == 20
+
+  @pytest.mark.asyncio
+  async def test_untouched_dates_pass_through_unchanged(self, monkeypatch):
+    today = date(2026, 7, 10)
+    now = datetime(2026, 7, 10, 10, 0, tzinfo=_TZ)
+    _stub_config(monkeypatch)
+    _stub_now(monkeypatch, now)
+    rec = DiscountData(type=DiscountType.FLAT, value=15)
+    data = {today + timedelta(days=5): rec}
+
+    result = await run(today, today + timedelta(days=5), data)
+
+    assert result[today + timedelta(days=5)] is rec

+ 50 - 76
tests/test_short_notice_rule.py

@@ -1,23 +1,17 @@
 import pytest
-from datetime import date, datetime, timedelta
+from datetime import date, timedelta
 from types import SimpleNamespace
-from zoneinfo import ZoneInfo
 
-from room_cal.rules.discount.short_notice import RULE_NAME, _same_day_dates, run
+from room_cal.rules.discount.short_notice import RULE_NAME, 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)),
-  )
+def _stub_config(monkeypatch, *, max_days: int = 14, ramp_days: int = 7):
+  cfg = SimpleNamespace(rules=SimpleNamespace(short_notice=SimpleNamespace(max_days=max_days, ramp_days=ramp_days)))
   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_today(monkeypatch, today: date):
+  monkeypatch.setattr('room_cal.rules.discount.short_notice.local_today', lambda: today)
 
 
 def _stub_open(monkeypatch, open_set: set[date]):
@@ -30,85 +24,65 @@ 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)}
+@pytest.mark.asyncio
+async def test_both_neighbors_open_gives_40_percent(monkeypatch):
+  today = date(2026, 7, 10)
+  _stub_config(monkeypatch, max_days=14, ramp_days=0)
+  _stub_today(monkeypatch, today)
+  d = today + timedelta(days=3)
+  _stub_open(monkeypatch, {d - timedelta(days=1), d, d + timedelta(days=1)})
 
-  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)}
+  result = await run(today, today + timedelta(days=5), {})
 
-  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)}
+  assert result[d].value == 40
 
-  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)}
+@pytest.mark.asyncio
+async def test_only_day_before_open_gives_30_percent(monkeypatch):
+  today = date(2026, 7, 10)
+  _stub_config(monkeypatch, max_days=14, ramp_days=0)
+  _stub_today(monkeypatch, today)
+  d = today + timedelta(days=3)
+  _stub_open(monkeypatch, {d - timedelta(days=1), d})
 
+  result = await run(today, today + timedelta(days=5), {})
 
-class TestRun:
+  assert result[d].value == 30
 
-  @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), {})
+@pytest.mark.asyncio
+async def test_only_day_after_open_gives_20_percent(monkeypatch):
+  today = date(2026, 7, 10)
+  _stub_config(monkeypatch, max_days=14, ramp_days=0)
+  _stub_today(monkeypatch, today)
+  d = today + timedelta(days=3)
+  _stub_open(monkeypatch, {d, d + timedelta(days=1)})
 
-    assert today not in result
-    assert today + timedelta(days=1) in result  # tomorrow is NOT same-day in the morning window
+  result = await run(today, today + timedelta(days=5), {})
 
-  @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)
+  assert result[d].value == 20
 
-    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_neither_neighbor_open_gives_no_discount(monkeypatch):
+  today = date(2026, 7, 10)
+  _stub_config(monkeypatch, max_days=14, ramp_days=0)
+  _stub_today(monkeypatch, today)
+  d = today + timedelta(days=3)
+  _stub_open(monkeypatch, {d})
 
-  @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), {})
 
-    result = await run(today, today + timedelta(days=5), {})
+  assert d not in result
 
-    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())
+@pytest.mark.asyncio
+async def test_empty_window_returns_empty(monkeypatch):
+  today = date(2026, 7, 10)
+  _stub_config(monkeypatch, max_days=1, ramp_days=0)
+  _stub_today(monkeypatch, today)
+  _stub_open(monkeypatch, set())
 
-    result = await run(today + timedelta(days=5), today + timedelta(days=5), {})
+  result = await run(today + timedelta(days=5), today + timedelta(days=5), {})
 
-    assert result == {}
+  assert result == {}