Files
xenia-pos-local/local_backend/tests/test_signage_schedule.py
bonaminandClaude Opus 5.5 60e6a16217 feat(signage,scenes): schedule lines with screens and smart days, screens' defaults, horizontal timeline, scenes
- Scenes (Alembic 0043, models/scene.py, /api/scenes, core.scenes.manage): named
  modes on or off (optionally until a time); price rules / deals get the
  condition "a scene is on"; signage lines can play only during a scene;
  turning one on tells every screen and pushes the online menu.
- Signage schedules are lines (signage_schedule_items): playlist, screens
  ([] = every screen), priority, days, smart days (1st / last day, first / last
  week, Greek public holidays or not: core/calendar_days.py, mirrored in
  @xenia/signage), hours, dates, scene. No line → the screen's default
  playlist. 0043 turns the old named schedules into lines for their screens.
- Screens: default playlist, TV shape, turn; what plays now and why.
- Manager: tabs Screens · Playlists · Schedules · Slides (placeholder) ·
  Message and weather; schedule lines with playlist / screens / priority /
  delete on one line; the playlist editor's horizontal timeline of fixed
  cards, the slide's basics beside its preview (one height), then its content;
  Management → Scenes; the scene condition in pricing.
- Tests: backend (lines, smart days, scenes, prices), player logic, e2e.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 19:22:47 +02:00

188 lines
12 KiB
Python

"""Signage schedules (plan step 5.4d; lines with their screens, smart days and scenes, owner
2026-11-21): which playlist plays when (days, hours that may pass midnight, dates, smart days,
priority, a scene); no line → the screen's default playlist. A screen with lines gets every
playlist they name so it switches by itself, and the server tells it when the pick changes."""
from datetime import date, datetime
import pytest
from core.calendar_days import is_holiday, month_days_ok, orthodox_easter
from services.signage_schedule import matches, pick
from tests.test_live import err, owner_h, pair # noqa: F401 (fixtures)
from tests.test_signage import playlist, slide
MON_10 = datetime(2026, 11, 16, 10, 0) # a Monday
MON_13 = datetime(2026, 11, 16, 13, 0)
SAT_13 = datetime(2026, 11, 21, 13, 0)
SAT_23 = datetime(2026, 11, 21, 23, 30)
SUN_01 = datetime(2026, 11, 22, 1, 0)
MON_01 = datetime(2026, 11, 16, 1, 0)
def test_which_entries_match():
lunch = {"playlist_id": 1, "time_from": "12:00", "time_to": "15:00"}
assert matches(lunch, MON_13) and not matches(lunch, MON_10)
night = {"playlist_id": 2, "days": [5], "time_from": "22:00", "time_to": "02:00"} # Saturday night
assert matches(night, SAT_23) and matches(night, SUN_01) # after midnight: Saturday's
assert not matches(night, MON_01)
xmas = {"playlist_id": 3, "date_from": "2026-12-20", "date_to": "2026-12-27"}
assert matches(xmas, datetime(2026, 12, 24, 20, 0)) and not matches(xmas, datetime(2026, 12, 28, 9, 0))
assert matches({"playlist_id": 4}, MON_10) # always
assert matches({"playlist_id": 5, "time_from": "18:00"}, datetime(2026, 11, 16, 23, 59)) # until the end of the day
party = {"playlist_id": 6, "scene_id": 7}
assert not matches(party, MON_10) and matches(party, MON_10, {7}) # only while the scene is on
def test_smart_days():
"""The same dates as packages/signage/tests/logic.test.mjs."""
assert [orthodox_easter(y) for y in (2025, 2026, 2027)] == [date(2025, 4, 20), date(2026, 4, 12), date(2027, 5, 2)]
assert is_holiday(date(2026, 3, 25)) and is_holiday(date(2026, 2, 23)) # Independence Day; Clean Monday 2026
assert is_holiday(date(2026, 4, 10)) and is_holiday(date(2026, 4, 13)) # Good Friday, Easter Monday
assert is_holiday(date(2026, 6, 1)) and not is_holiday(date(2026, 6, 2)) # Whit Monday
assert month_days_ok(["first"], date(2026, 11, 1)) and not month_days_ok(["first"], date(2026, 11, 2))
assert month_days_ok(["last"], date(2026, 2, 28)) and not month_days_ok(["last"], date(2026, 2, 27))
assert month_days_ok(["first_week"], date(2026, 11, 7)) and not month_days_ok(["first_week"], date(2026, 11, 8))
assert month_days_ok(["last_week"], date(2026, 11, 24)) and not month_days_ok(["last_week"], date(2026, 11, 23))
assert month_days_ok(["first", "last"], date(2026, 11, 30)) # any one is enough
assert not month_days_ok(["not_holiday"], date(2026, 12, 25)) and month_days_ok(["not_holiday"], date(2026, 12, 24))
assert matches({"playlist_id": 1, "month_days": ["last"], "time_from": "18:00"}, datetime(2026, 11, 30, 19, 0))
def test_the_winner():
entries = [{"playlist_id": 10, "time_from": "12:00", "time_to": "15:00"},
{"playlist_id": 20, "days": [5, 6]},
{"playlist_id": 30, "days": [5, 6], "time_from": "12:00", "time_to": "15:00"},
{"playlist_id": 40, "date_from": "2026-11-21", "date_to": "2026-11-21", "priority": 5},
{"playlist_id": 50, "scene_id": 1, "priority": 9}]
assert pick(entries, 99, MON_10) == 99 # nothing → the default
assert pick(entries, 99, MON_13) == 10
assert pick(entries, 99, datetime(2026, 11, 22, 13, 0)) == 30 # Sunday lunch: days + hours beat hours alone
assert pick(entries, 99, SAT_13) == 40 # the higher priority wins
assert pick(entries, 99, SAT_13, {1}) == 50 # party mode on: its line, the highest priority
assert pick(entries, None, MON_10) is None # no default → blank
@pytest.fixture
def sent(monkeypatch):
import routers.signage as sg
got = []
monkeypatch.setattr(sg, "broadcast_sync", lambda kind, data, **kw: got.append((kind, kw.get("device_ids"))))
return got
@pytest.fixture
def at(monkeypatch):
"""Freeze the venue clock: at(datetime) sets it."""
import core.timeutil as tu
from core.timeutil import venue_tz
state = {"now": MON_10}
monkeypatch.setattr(tu, "venue_now", lambda: state["now"].replace(tzinfo=venue_tz()))
return lambda dt: state.update(now=dt)
@pytest.fixture
def setup(client, owner_h, pair):
lunch, weekend, normal = (playlist(client, owner_h, n) for n in ("Lunch", "Weekend", "Normal"))
for p in (lunch, weekend, normal):
slide(client, owner_h, p["id"], "text", content={"title": p["name"]})
token, dev = pair("signage", "TV")
_, other = pair("signage", "Bar TV")
tv, bar = dev.json()["id"], other.json()["id"]
client.put(f"/api/signage/screens/{tv}", headers=owner_h, json={"playlist_id": normal["id"]})
r = client.put("/api/signage/schedules", headers=owner_h, json={"lines": [
{"playlist_id": lunch["id"], "screen_ids": [tv], "time_from": "12:00", "time_to": "15:00"},
{"playlist_id": weekend["id"], "days": [5, 6], "priority": 1}]}) # no screens: every screen
assert r.status_code == 200, r.text
return {"h": {"X-Xenia-Device": token}, "id": tv, "bar": bar, "lines": r.json(),
"lunch": lunch["id"], "weekend": weekend["id"], "normal": normal["id"]}
def test_a_screen_gets_its_lines_and_every_playlist(client, owner_h, setup, at):
at(MON_10)
got = client.get("/api/signage/screen", headers=setup["h"]).json()
assert got["playlist"]["name"] == "Normal" # its default
assert sorted(got["playlists"]) == sorted(str(i) for i in (setup["lunch"], setup["weekend"], setup["normal"]))
assert got["schedule"]["timezone"] == "Europe/Athens" and len(got["schedule"]["entries"]) == 2
assert got["schedule"]["fallback_playlist_id"] == setup["normal"]
at(MON_13)
assert client.get("/api/signage/screen", headers=setup["h"]).json()["playlist"]["name"] == "Lunch"
at(SAT_13)
assert client.get("/api/signage/screen", headers=setup["h"]).json()["playlist"]["name"] == "Weekend"
lines = client.get("/api/signage/schedules", headers=owner_h).json()
assert sorted(x["name"] for x in lines[1]["playing_on"]) == ["Bar TV", "TV"] # the weekend line is for every screen
screens = {s["name"]: s for s in client.get("/api/signage/screens", headers=owner_h).json()}
assert screens["TV"]["playlist_name"] == "Normal" and screens["TV"]["now"] == {"id": setup["weekend"], "name": "Weekend", "scheduled": True}
assert screens["TV"]["orientation_mismatch"] is False
def test_scenes_switch_screens_and_prices(client, db, owner_h, setup, at, sent):
"""A scene (party mode): its schedule line plays while it is on; a price rule follows it too."""
at(MON_10)
party = playlist(client, owner_h, "Party")
slide(client, owner_h, party["id"], "text", content={"title": "Party"})
scene = client.post("/api/scenes", headers=owner_h, json={"name": "Party mode", "color": "#ff6699"}).json()
lines = client.get("/api/signage/schedules", headers=owner_h).json()
body = [{k: x[k] for k in ("id", "playlist_id", "screen_ids", "priority", "days", "time_from", "time_to")} for x in lines]
body.append({"playlist_id": party["id"], "scene_id": scene["id"], "priority": 50})
assert client.put("/api/signage/schedules", headers=owner_h, json={"lines": body}).status_code == 200
assert client.get("/api/signage/screen", headers=setup["h"]).json()["playlist"]["name"] == "Normal"
sent.clear()
r = client.put(f"/api/scenes/{scene['id']}/active", headers=owner_h, json={"active": True})
assert r.status_code == 200 and r.json()["active"] is True
assert ("signage.changed", sorted([setup["id"], setup["bar"]])) in sent
got = client.get("/api/signage/screen", headers=setup["h"]).json()
assert got["playlist"]["name"] == "Party" and got["schedule"]["scenes"] == [scene["id"]]
client.put(f"/api/scenes/{scene['id']}/active", headers=owner_h, json={"active": False})
assert client.get("/api/signage/screen", headers=setup["h"]).json()["playlist"]["name"] == "Normal"
# deleting the scene turns its line off (it would otherwise play always)
assert client.delete(f"/api/scenes/{scene['id']}", headers=owner_h).status_code == 204
left = [x for x in client.get("/api/signage/schedules", headers=owner_h).json() if x["playlist_id"] == party["id"]]
assert left[0]["enabled"] is False and left[0]["scene_id"] is None
def test_the_server_tells_a_screen_when_its_playlist_switches(client, db, setup, at):
"""Done when (5.4d): the schedule switches the screen's playlist at the set hour."""
from services import signage_schedule as ss
ss.reset()
at(datetime(2026, 11, 16, 11, 59))
assert ss.due_changes(db) == [] # first look: remembered
at(datetime(2026, 11, 16, 11, 59, 40))
assert ss.due_changes(db) == []
at(datetime(2026, 11, 16, 12, 0))
assert ss.due_changes(db) == [setup["id"]] # lunch starts (only on the TV)
assert ss.due_changes(db) == []
at(datetime(2026, 11, 16, 15, 0))
assert ss.due_changes(db) == [setup["id"]] # and ends
ss.reset()
def test_changes_and_deletes(client, owner_h, setup, sent, at):
at(MON_13)
lines = client.get("/api/signage/schedules", headers=owner_h).json()
sent.clear()
assert client.put("/api/signage/schedules", headers=owner_h, json={"lines": [{"id": lines[1]["id"], "playlist_id": setup["weekend"], "days": [5, 6]}]}).status_code == 200
assert sorted(sent[0][1]) == sorted([setup["id"], setup["bar"]])
assert client.get("/api/signage/screen", headers=setup["h"]).json()["playlist"]["name"] == "Normal" # the lunch line is gone
# a slide changed in a playlist of its lines reaches the screen
wk = client.get(f"/api/signage/playlists/{setup['weekend']}", headers=owner_h).json()
sent.clear()
client.put(f"/api/signage/slides/{wk['slides'][0]['id']}", headers=owner_h, json={"duration_s": 20})
assert sorted(sent[0][1]) == sorted([setup["id"], setup["bar"]])
# deleting a playlist takes its lines with it
client.delete(f"/api/signage/playlists/{setup['weekend']}", headers=owner_h)
assert client.get("/api/signage/schedules", headers=owner_h).json() == []
got = client.get("/api/signage/screen", headers=setup["h"]).json()
assert got["schedule"] is None and got["playlist"]["name"] == "Normal"
def test_schedules_are_checked(client, owner_h, setup):
put = lambda line: client.put("/api/signage/schedules", headers=owner_h, json={"lines": [line]}) # noqa: E731
assert err(put({"playlist_id": 99999})) == "signage.playlist_unknown"
assert err(put({"playlist_id": setup["lunch"], "screen_ids": [99999]})) == "signage.screen_unknown"
assert err(put({"playlist_id": setup["lunch"], "scene_id": 99999})) == "signage.scene_unknown"
assert put({"playlist_id": setup["lunch"], "days": [9]}).status_code == 422
assert put({"playlist_id": setup["lunch"], "month_days": ["fullmoon"]}).status_code == 422
assert put({"playlist_id": setup["lunch"], "time_to": "24:00"}).status_code == 422
assert put({"playlist_id": setup["lunch"], "month_days": ["last", "first"]}).json()[0]["month_days"] == ["first", "last"]
assert err(client.put(f"/api/signage/screens/{setup['id']}", headers=owner_h, json={"playlist_id": 99999})) == "validation.device_config"