Files
xenia-pos-local/local_backend/tests/test_day_auto.py
T
bonaminandClaude Opus 5.5 096bd53dbd feat(backend,manager): the business day opens and closes by itself (opening hours, a time, or only an alert)
- services/day_auto.py: a minute loop (venue time, offline) opens the day when the opening
  hours start (or at business_day.auto_open_time) and closes it when they end (or at
  auto_close_time); 'alert' only tells the managers. Offsets before / after, one day across
  the break or one per span, spans past midnight belong to the day they start, a day closed
  by hand isn't reopened in the same span, a restart catches up the open (until the business
  date changes for 'time') and alerts a missed close instead of doing it late.
- The close runs only when no order of the day is unclosed (Q-10): otherwise a sticky alert
  and a new try every 15 minutes. A locked / expired license turns the open into an alert.
- Sticky notifications: notifications.resolved_at + core.notifications.resolve(); the
  alerts resolve when the day opens / closes (by hand or by itself).
- services/business_days.py: the one open / close path (the routes and the loop); days opened
  or closed by the system have no opener / closer and opened_auto / closed_auto (Alembic 0052).
- Manager: Settings -> Operation "Opening and closing the day"; the bell's action buttons
  ("Open now", "Go to the overview"); Reports -> Days marks days opened / closed by itself.

Plan platform-v2 6F.8.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 13:17:15 +02:00

168 lines
7.7 KiB
Python

"""The day opens and closes by itself (plan 6F.8): services/day_auto.tick with a fake clock.
Hours used: Monday 09:00-15:00 and 18:00-23:00; Tuesday 10:00-01:00 (past midnight).
2026-11-30 is a Monday; Athens is UTC+2 then."""
from datetime import datetime
import pytest
from services import day_auto
HOURS = {"days": [[{"from": "09:00", "to": "15:00"}, {"from": "18:00", "to": "23:00"}],
[{"from": "10:00", "to": "01:00"}], [], [], [], [], []], "note": None}
def at(local: str):
from core.timeutil import venue_local_to_utc
return venue_local_to_utc(datetime.fromisoformat(local))
@pytest.fixture
def boss(make_user, login):
return login(make_user(role="owner"))
@pytest.fixture
def venue(client, boss, monkeypatch):
import services.cloud_sync as cs
monkeypatch.setattr(cs, "apply_license", lambda *a, **k: None) # no cloud license here: keep it as it is
day_auto.reset_for_tests()
def set_(**values):
for k, v in values.items():
r = client.put(f"/api/settings/business_day.{k}", headers=boss, json={"value": v})
assert r.status_code == 200, r.text
assert client.put("/api/identity/hours", headers=boss, json=HOURS).status_code == 200
yield set_
day_auto.reset_for_tests()
def days(db):
from models.business_day import BusinessDay
db.expire_all()
return db.query(BusinessDay).order_by(BusinessDay.id).all()
def alerts(client, boss):
return [n["key"] for n in client.get("/api/platform/notifications", headers=boss).json()["items"]
if n["key"].startswith("business_day.")]
def test_off_by_default_does_nothing(db, venue):
assert day_auto.tick(db, now=at("2026-11-30T09:00")) == []
assert days(db) == []
def test_schedule_one_day_over_the_break(db, venue):
venue(auto_open="schedule", auto_close="schedule")
assert day_auto.tick(db, now=at("2026-11-30T08:59")) == []
assert day_auto.tick(db, now=at("2026-11-30T09:00")) == ["opened"]
d = days(db)[0]
assert d.status == "open" and d.opened_auto and d.opened_by_id is None
assert day_auto.tick(db, now=at("2026-11-30T16:00")) == [] # the break: one day
assert day_auto.tick(db, now=at("2026-11-30T23:00")) == ["closed"]
d = days(db)[0]
assert d.status == "closed" and d.closed_auto and d.closed_by_id is None
assert day_auto.tick(db, now=at("2026-11-30T23:05")) == [] # not reopened
assert len(days(db)) == 1
def test_break_closes_and_reopens_when_not_ignored(db, venue):
venue(auto_open="schedule", auto_close="schedule", auto_ignore_break="false")
day_auto.tick(db, now=at("2026-11-30T09:00"))
assert day_auto.tick(db, now=at("2026-11-30T15:00")) == ["closed"]
assert day_auto.tick(db, now=at("2026-11-30T17:59")) == []
assert day_auto.tick(db, now=at("2026-11-30T18:00")) == ["opened"]
assert [d.status for d in days(db)] == ["closed", "open"]
def test_a_day_closed_by_hand_is_not_reopened_in_the_same_span(db, venue, client, boss):
venue(auto_open="schedule")
day_auto.tick(db, now=at("2026-11-30T09:00"))
assert client.post("/api/business-day/close", headers=boss, json={}).status_code == 200
assert day_auto.tick(db, now=at("2026-11-30T12:00")) == []
assert day_auto.tick(db, now=at("2026-12-01T10:00")) == ["opened"] # the next day's span
def test_span_past_midnight_belongs_to_the_day_it_starts(db, venue):
venue(auto_open="schedule", auto_close="schedule")
assert day_auto.tick(db, now=at("2026-12-01T10:00")) == ["opened"] # Tuesday
assert day_auto.tick(db, now=at("2026-12-02T00:30")) == [] # still Tuesday's span
assert day_auto.tick(db, now=at("2026-12-02T01:00")) == ["closed"]
def test_offsets(db, venue):
venue(auto_open="schedule", auto_close="schedule", auto_open_offset_min="30", auto_close_offset_min="45")
assert day_auto.tick(db, now=at("2026-11-30T08:30")) == ["opened"]
assert day_auto.tick(db, now=at("2026-11-30T23:30")) == []
assert day_auto.tick(db, now=at("2026-11-30T23:45")) == ["closed"]
def test_time_overrides_the_hours(db, venue):
venue(auto_open="time", auto_open_time="07:00", auto_close="time", auto_close_time="22:00")
assert day_auto.tick(db, now=at("2026-12-03T06:59")) == [] # Thursday: no hours at all
assert day_auto.tick(db, now=at("2026-12-03T07:00")) == ["opened"]
assert day_auto.tick(db, now=at("2026-12-03T22:00")) == ["closed"]
assert day_auto.tick(db, now=at("2026-12-03T23:00")) == []
assert day_auto.tick(db, now=at("2026-12-04T06:30")) == [] # past the rollover: no catch-up
assert day_auto.tick(db, now=at("2026-12-04T07:00")) == ["opened"]
def test_alert_is_one_sticky_notification_that_resolves(db, venue, client, boss):
venue(auto_open="alert", auto_close="alert")
assert day_auto.tick(db, now=at("2026-11-30T09:00")) == ["alert:business_day.not_open"]
day_auto.tick(db, now=at("2026-11-30T09:05"))
assert alerts(client, boss) == ["business_day.not_open"] # one, not two
n = client.get("/api/platform/notifications", headers=boss).json()["items"][0]
assert n["params"]["action"] == "open_day" and "09:00" in n["message"] and n["expires_at"] is None
assert days(db) == [] # alert only
assert client.post("/api/business-day/open", headers=boss, json={}).status_code == 201
assert alerts(client, boss) == [] # resolved at once
day_auto.tick(db, now=at("2026-11-30T23:00"))
assert alerts(client, boss) == ["business_day.not_closed"]
assert client.post("/api/business-day/close", headers=boss, json={}).status_code == 200
assert alerts(client, boss) == []
def test_open_tables_block_the_close_and_it_retries(db, venue, client, boss, make_user):
from models.order import Order
venue(auto_open="schedule", auto_close="schedule")
day_auto.tick(db, now=at("2026-11-30T09:00"))
d = days(db)[0]
o = Order(opened_by=make_user(role="waiter").id, business_day_id=d.id, status="paid")
db.add(o)
db.commit()
assert day_auto.tick(db, now=at("2026-11-30T23:00")) == ["alert:business_day.tables_open", "waiting"]
assert alerts(client, boss) == ["business_day.tables_open"]
o.status = "closed"
db.commit()
assert day_auto.tick(db, now=at("2026-11-30T23:10")) == ["waiting"] # every 15 minutes
assert days(db)[0].status == "open"
assert day_auto.tick(db, now=at("2026-11-30T23:15")) == ["closed"]
assert alerts(client, boss) == []
def test_restart_catches_up_the_open_and_alerts_a_missed_close(db, venue, client, boss):
venue(auto_open="schedule", auto_close="schedule")
assert day_auto.tick(db, now=at("2026-11-30T11:00"), started_at=at("2026-11-30T11:00")) == ["opened"]
out = day_auto.tick(db, now=at("2026-11-30T23:40"), started_at=at("2026-11-30T23:30"))
assert out == ["alert:business_day.not_closed"] and days(db)[0].status == "open"
assert alerts(client, boss) == ["business_day.not_closed"]
def test_locked_license_alerts_instead_of_opening(db, venue, client, boss, monkeypatch):
from services.license_state import license_state
venue(auto_open="schedule")
monkeypatch.setitem(license_state, "locked", True)
assert day_auto.tick(db, now=at("2026-11-30T09:00")) == ["alert:business_day.open_blocked"]
assert days(db) == [] and alerts(client, boss) == ["business_day.open_blocked"]
def test_spans_of():
from datetime import date
mon = date(2026, 11, 30)
assert [(a.hour, b.hour) for a, b in day_auto.spans_of(HOURS["days"], mon, False)] == [(9, 15), (18, 23)]
assert [(a.hour, b.hour) for a, b in day_auto.spans_of(HOURS["days"], mon, True)] == [(9, 23)]
(a, b), = day_auto.spans_of(HOURS["days"], date(2026, 12, 1), True)
assert b.day == 2 and b.hour == 1