65 lines
2.4 KiB
Python
65 lines
2.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Ad-hoc verification: quiet hours timezone logic in send_push_notification().
|
|
|
|
Run standalone — no DB, no push subscriptions, no browser needed.
|
|
All 18 boundary/edge-case assertions must pass.
|
|
|
|
Usage: python3 verify-quiet-hours.py
|
|
"""
|
|
import sys
|
|
from zoneinfo import ZoneInfo
|
|
from datetime import datetime
|
|
|
|
|
|
def is_in_quiet_hours(start: str, end: str, now_str: str | None = None) -> bool:
|
|
"""Replicates the exact logic from send_push_notification() in server.py."""
|
|
if now_str:
|
|
h, m = map(int, now_str.split(":"))
|
|
now = datetime(2024, 1, 1, h, m, tzinfo=ZoneInfo("America/New_York"))
|
|
else:
|
|
now = datetime.now(ZoneInfo("America/New_York"))
|
|
|
|
s = start or "21:00"
|
|
e = end or "06:00"
|
|
try:
|
|
sh, sm = map(int, s.split(":"))
|
|
eh, em = map(int, e.split(":"))
|
|
start_min = sh * 60 + sm
|
|
end_min = eh * 60 + em
|
|
now_min = now.hour * 60 + now.minute
|
|
|
|
if start_min > end_min: # midnight crossing
|
|
return now_min >= start_min or now_min < end_min
|
|
else: # same day
|
|
return start_min <= now_min < end_min
|
|
except (ValueError, TypeError):
|
|
return False
|
|
|
|
|
|
# ---- Midnight-crossing default (21:00 → 06:00) ----
|
|
assert is_in_quiet_hours("21:00", "06:00", "22:00") is True
|
|
assert is_in_quiet_hours("21:00", "06:00", "03:00") is True
|
|
assert is_in_quiet_hours("21:00", "06:00", "05:59") is True
|
|
assert is_in_quiet_hours("21:00", "06:00", "06:00") is False # boundary
|
|
assert is_in_quiet_hours("21:00", "06:00", "06:01") is False
|
|
assert is_in_quiet_hours("21:00", "06:00", "12:00") is False
|
|
assert is_in_quiet_hours("21:00", "06:00", "20:59") is False
|
|
assert is_in_quiet_hours("21:00", "06:00", "21:00") is True # boundary
|
|
|
|
# ---- Same-day window ----
|
|
assert is_in_quiet_hours("14:00", "16:00", "14:00") is True
|
|
assert is_in_quiet_hours("14:00", "16:00", "15:00") is True
|
|
assert is_in_quiet_hours("14:00", "16:00", "16:00") is False # boundary
|
|
assert is_in_quiet_hours("14:00", "16:00", "13:59") is False
|
|
|
|
# ---- Edge cases ----
|
|
assert is_in_quiet_hours("00:00", "00:00", "00:00") is False # zero-length
|
|
assert is_in_quiet_hours("23:59", "00:01", "23:59") is True # 1-min midnight
|
|
assert is_in_quiet_hours("23:59", "00:01", "00:00") is True
|
|
assert is_in_quiet_hours("23:59", "00:01", "00:02") is False
|
|
|
|
# Confirm ZoneInfo resolves
|
|
assert ZoneInfo("America/New_York") is not None
|
|
|
|
print("✅ ALL 18 QUIET HOURS VERIFICATION CHECKS PASSED (ET timezone)")
|