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>
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
"""Signage schedules (plan step 5.4d): which playlist plays when (days, hours that may pass
|
||||
midnight, dates, priority, a fallback); a screen on a schedule gets every playlist in it so it
|
||||
switches by itself, and the server tells it when the pick changes."""
|
||||
from datetime import datetime
|
||||
"""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
|
||||
@@ -27,18 +29,37 @@ def test_which_entries_match():
|
||||
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}]
|
||||
assert pick(entries, 99, MON_10) == 99 # nothing → the fallback
|
||||
{"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, None, MON_10) is None # no fallback → blank
|
||||
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
|
||||
@@ -64,31 +85,59 @@ 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"]})
|
||||
r = client.post("/api/signage/schedules", headers=owner_h, json={
|
||||
"name": "The week", "fallback_playlist_id": normal["id"],
|
||||
"entries": [{"playlist_id": lunch["id"], "time_from": "12:00", "time_to": "15:00"},
|
||||
{"playlist_id": weekend["id"], "days": [5, 6], "priority": 1}]})
|
||||
assert r.status_code == 201, r.text
|
||||
token, dev = pair("signage", "TV")
|
||||
client.put(f"/api/signage/screens/{dev.json()['id']}", headers=owner_h, json={"schedule_id": r.json()["id"]})
|
||||
return {"h": {"X-Xenia-Device": token}, "id": dev.json()["id"], "schedule": r.json(),
|
||||
_, 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_schedule_and_every_playlist(client, owner_h, setup, at):
|
||||
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"
|
||||
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"
|
||||
s = client.get("/api/signage/schedules", headers=owner_h).json()[0]
|
||||
assert s["playing_now"]["name"] == "Weekend" and [x["name"] for x in s["screens"]] == ["TV"]
|
||||
screen = client.get("/api/signage/screens", headers=owner_h).json()[0]
|
||||
assert screen["schedule_name"] == "The week" and screen["orientation_mismatch"] is False
|
||||
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):
|
||||
@@ -100,7 +149,7 @@ def test_the_server_tells_a_screen_when_its_playlist_switches(client, db, setup,
|
||||
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
|
||||
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
|
||||
@@ -108,31 +157,31 @@ def test_the_server_tells_a_screen_when_its_playlist_switches(client, db, setup,
|
||||
|
||||
|
||||
def test_changes_and_deletes(client, owner_h, setup, sent, at):
|
||||
at(MON_10)
|
||||
sid = setup["schedule"]["id"]
|
||||
at(MON_13)
|
||||
lines = client.get("/api/signage/schedules", headers=owner_h).json()
|
||||
sent.clear()
|
||||
client.put(f"/api/signage/schedules/{sid}", headers=owner_h, json={"name": "The week", "fallback_playlist_id": setup["lunch"], "entries": []})
|
||||
assert sent == [("signage.changed", [setup["id"]])]
|
||||
assert client.get("/api/signage/screen", headers=setup["h"]).json()["playlist"]["name"] == "Lunch"
|
||||
# a slide changed in a playlist of the schedule reaches the screen
|
||||
lunch = client.get(f"/api/signage/playlists/{setup['lunch']}", headers=owner_h).json()
|
||||
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/{lunch['slides'][0]['id']}", headers=owner_h, json={"duration_s": 20})
|
||||
assert sent == [("signage.changed", [setup["id"]])]
|
||||
# deleting a playlist takes it out of the schedule
|
||||
client.delete(f"/api/signage/playlists/{setup['lunch']}", headers=owner_h)
|
||||
assert client.get("/api/signage/schedules", headers=owner_h).json()[0]["fallback_playlist_id"] is None
|
||||
assert client.get("/api/signage/screen", headers=setup["h"]).json()["playlist"] is None
|
||||
# deleting the schedule: the screen goes back to its playlist (none here)
|
||||
assert client.delete(f"/api/signage/schedules/{sid}", headers=owner_h).status_code == 204
|
||||
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"] is None
|
||||
assert got["schedule"] is None and got["playlist"]["name"] == "Normal"
|
||||
|
||||
|
||||
def test_schedules_are_checked(client, owner_h, setup):
|
||||
body = {"name": "Bad", "entries": [{"playlist_id": 99999}]}
|
||||
assert err(client.post("/api/signage/schedules", headers=owner_h, json=body)) == "signage.playlist_unknown"
|
||||
assert client.post("/api/signage/schedules", headers=owner_h, json={"name": "x", "entries": [{"playlist_id": setup["lunch"], "days": [9]}]}).status_code == 422
|
||||
assert client.post("/api/signage/schedules", headers=owner_h, json={"name": "x", "entries": [{"playlist_id": setup["lunch"], "time_to": "24:00"}]}).status_code == 422
|
||||
assert err(client.put(f"/api/signage/screens/{setup['id']}", headers=owner_h, json={"schedule_id": 99999})) == "validation.device_config"
|
||||
assert err(client.get("/api/signage/schedules/99999", headers=owner_h)) in ("http.404", "http.405")
|
||||
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"
|
||||
|
||||
Reference in New Issue
Block a user