"""Digital signage, playlists and slides (plan step 5.4a): playlists for an orientation, picture / video / text / menu slides with their timing and windows, a screen's playlist resolved for playing, changes told only to the screens playing them, built-in pictures.""" import pytest from tests.test_live import err, owner_h, pair # noqa: F401 (fixtures) from tests.test_media import jpeg, media_dir, mp4, up # noqa: F401 (fixture + helpers) @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 screens(pair): out = {} for n in (1, 2): token, r = pair("signage", f"TV {n}") assert r.status_code == 201, r.text out[n] = {"h": {"X-Xenia-Device": token}, "id": r.json()["id"]} return out def playlist(client, h, name="Promos", **kw): r = client.post("/api/signage/playlists", headers=h, json={"name": name, **kw}) assert r.status_code == 201, r.text return r.json() def slide(client, h, pid, kind, **body): r = client.post(f"/api/signage/playlists/{pid}/slides", headers=h, json={"kind": kind, **body}) assert r.status_code == 201, r.text return r.json() def test_a_screen_plays_its_playlist_in_order(client, owner_h, screens, sent): """Done when (5.4a): picture, video and text slides reach a paired screen in order.""" pic, vid, bg = up(client, owner_h, jpeg(1920, 1080)).json(), up(client, owner_h, mp4(), name="v.mp4", ctype="video/mp4").json(), \ up(client, owner_h, jpeg(1920, 1080, (9, 9, 9))).json() p = playlist(client, owner_h, orientation="landscape", transition="fade") s1 = slide(client, owner_h, p["id"], "image", content={"media_id": pic["id"]}, duration_s=8) s2 = slide(client, owner_h, p["id"], "video", content={"media_id": vid["id"]}) s3 = slide(client, owner_h, p["id"], "text", transition="zoom", content={"title": "Happy hour", "body": "17:00-19:00", "background": {"kind": "media", "media_id": bg["id"], "dim": 40}}) assert s2["video_to_end"] is True and s1["video_to_end"] is False assert client.get(f"/api/signage/screen", headers=screens[1]["h"]).json()["playlist"] is None # nothing chosen yet r = client.put(f"/api/signage/screens/{screens[1]['id']}", headers=owner_h, json={"playlist_id": p["id"]}) assert r.status_code == 200 and r.json()["playlist_name"] == "Promos" and r.json()["orientation"] == "landscape" got = client.get("/api/signage/screen", headers=screens[1]["h"]).json() pl = got["playlist"] assert [s["id"] for s in pl["slides"]] == [s1["id"], s2["id"], s3["id"]] assert [s["transition"] for s in pl["slides"]] == ["fade", "fade", "zoom"] # the playlist's default assert pl["media"][str(pic["id"])]["url"] == pic["urls"]["full"] and pl["media"][str(vid["id"])]["url"] == vid["urls"]["original"] assert pl["media"][str(bg["id"])]["kind"] == "image" and pl["slides"][2]["content"]["background"]["dim"] == 40 assert got["screen"] == {"id": screens[1]["id"], "name": "TV 1", "orientation": "landscape", "rotation": 0} assert len(got["version"]) == 16 def test_changes_reach_only_the_screens_playing_them(client, owner_h, screens, sent): a, b = playlist(client, owner_h, "A"), playlist(client, owner_h, "B") x = slide(client, owner_h, a["id"], "text", content={"title": "x"}) y = slide(client, owner_h, a["id"], "text", content={"title": "y"}) client.put(f"/api/signage/screens/{screens[1]['id']}", headers=owner_h, json={"playlist_id": a["id"]}) client.put(f"/api/signage/screens/{screens[2]['id']}", headers=owner_h, json={"playlist_id": b["id"]}) before = client.get("/api/signage/screen", headers=screens[1]["h"]).json()["version"] sent.clear() r = client.put(f"/api/signage/playlists/{a['id']}/order", headers=owner_h, json={"slide_ids": [y["id"], x["id"]]}) assert [s["id"] for s in r.json()["slides"]] == [y["id"], x["id"]] assert sent == [("signage.changed", [screens[1]["id"]])] after = client.get("/api/signage/screen", headers=screens[1]["h"]) assert [s["content"]["title"] for s in after.json()["playlist"]["slides"]] == ["y", "x"] and after.json()["version"] != before sent.clear() client.put(f"/api/signage/slides/{x['id']}", headers=owner_h, json={"enabled": False}) assert sent == [("signage.changed", [screens[1]["id"]])] assert [s["id"] for s in client.get("/api/signage/screen", headers=screens[1]["h"]).json()["playlist"]["slides"]] == [y["id"]] assert err(client.put(f"/api/signage/playlists/{a['id']}/order", headers=owner_h, json={"slide_ids": [y["id"]]})) == "signage.order_mismatch" def test_slides_are_typed_and_checked(client, owner_h): pic, vid = up(client, owner_h, jpeg(800, 600)).json(), up(client, owner_h, mp4(), name="v.mp4", ctype="video/mp4").json() p = playlist(client, owner_h) url = f"/api/signage/playlists/{p['id']}/slides" assert err(client.post(url, headers=owner_h, json={"kind": "image", "content": {"media_id": vid["id"]}})) == "media.not_a_picture" assert err(client.post(url, headers=owner_h, json={"kind": "video", "content": {"media_id": pic["id"]}})) == "signage.not_a_video" assert err(client.post(url, headers=owner_h, json={"kind": "text", "content": {"background": {"kind": "media", "media_id": vid["id"]}}})) == "media.not_a_picture" assert err(client.post(url, headers=owner_h, json={"kind": "image", "content": {"media_id": 99999}})) == "media.not_found" r = client.post(url, headers=owner_h, json={"kind": "text", "content": {"text_colour": "red"}}) assert err(r) == "signage.content_invalid" and r.json()["error"]["params"]["field"] == "text_colour" assert err(client.post(url, headers=owner_h, json={"kind": "text", "content": {"sparkles": True}})) == "signage.content_invalid" assert client.post(url, headers=owner_h, json={"kind": "dance"}).status_code == 422 assert client.post(url, headers=owner_h, json={"kind": "text", "time_from": "25:00"}).status_code == 422 assert client.post(url, headers=owner_h, json={"kind": "text", "days": [7]}).status_code == 422 s = slide(client, owner_h, p["id"], "text", days=[4, 5, 5], time_from="12:00", time_to="15:00", date_to="2026-12-31", content={"title": "Lunch menu", "font": "serif", "text_size": 140}) assert s["days"] == [4, 5] and s["time_from"] == "12:00" and s["date_to"] == "2026-12-31" assert s["content"]["background"] == {"kind": "colour", "colour": "#111827", "media_id": None, "dim": 0, "fit": "cover"} r = client.put(f"/api/signage/slides/{s['id']}", headers=owner_h, json={"days": [], "time_from": None}) assert r.json()["days"] == [] and r.json()["time_from"] is None and r.json()["time_to"] == "15:00" menu = slide(client, owner_h, p["id"], "menu", content={"title": "Our menu"}) assert menu["content"]["title"] == "Our menu" def test_duplicates_positions_and_deleting(client, owner_h, screens, sent): p = playlist(client, owner_h) a = slide(client, owner_h, p["id"], "text", content={"title": "a"}) b = slide(client, owner_h, p["id"], "text", content={"title": "b"}) first = slide(client, owner_h, p["id"], "text", content={"title": "first"}, position=0) copy = client.post(f"/api/signage/slides/{a['id']}/duplicate", headers=owner_h).json() titles = lambda pid: [s["content"]["title"] for s in client.get(f"/api/signage/playlists/{pid}", headers=owner_h).json()["slides"]] # noqa: E731 assert titles(p["id"]) == ["first", "a", "a", "b"] and copy["id"] != a["id"] client.delete(f"/api/signage/slides/{first['id']}", headers=owner_h) got = client.get(f"/api/signage/playlists/{p['id']}", headers=owner_h).json()["slides"] assert [s["position"] for s in got] == [0, 1, 2] and [s["content"]["title"] for s in got] == ["a", "a", "b"] dup = client.post(f"/api/signage/playlists/{p['id']}/duplicate", headers=owner_h).json() assert dup["name"] == "Promos (2)" and titles(dup["id"]) == ["a", "a", "b"] client.put(f"/api/signage/screens/{screens[1]['id']}", headers=owner_h, json={"playlist_id": p["id"]}) sent.clear() assert client.delete(f"/api/signage/playlists/{p['id']}", headers=owner_h).status_code == 204 assert sent == [("signage.changed", [screens[1]["id"]])] assert client.get("/api/signage/screen", headers=screens[1]["h"]).json()["playlist"] is None assert b["id"] and err(client.get(f"/api/signage/playlists/{p['id']}", headers=owner_h)) == "signage.playlist_unknown" def test_a_screens_shape_and_the_playlists(client, owner_h, screens): tall = playlist(client, owner_h, "Tall menu", orientation="portrait") r = client.put(f"/api/signage/screens/{screens[1]['id']}", headers=owner_h, json={"playlist_id": tall["id"], "rotation": 90}) assert r.json()["orientation_mismatch"] is True and r.json()["rotation"] == 90 r = client.put(f"/api/signage/screens/{screens[1]['id']}", headers=owner_h, json={"orientation": "portrait"}) assert r.json()["orientation_mismatch"] is False and r.json()["playlist_id"] == tall["id"] assert client.get("/api/signage/screen", headers=screens[1]["h"]).json()["screen"]["orientation"] == "portrait" assert [s["name"] for s in client.get("/api/signage/playlists", headers=owner_h).json()[0]["screens"]] == ["TV 1"] assert err(client.put(f"/api/signage/screens/{screens[1]['id']}", headers=owner_h, json={"playlist_id": 99999})) == "validation.device_config" assert client.put(f"/api/signage/screens/{screens[1]['id']}", headers=owner_h, json={"rotation": 45}).status_code == 422 # the Devices page edits the same settings assert client.patch(f"/api/devices/{screens[2]['id']}", headers=owner_h, json={"config": {"playlist": tall["id"], "orientation": "portrait"}}).status_code == 200 assert [s["playlist_id"] for s in client.get("/api/signage/screens", headers=owner_h).json() if s["id"] == screens[2]["id"]] == [tall["id"]] def test_media_in_a_slide_cant_be_deleted(client, owner_h): pic = up(client, owner_h, jpeg(640, 360)).json() p = playlist(client, owner_h, "Window") s = slide(client, owner_h, p["id"], "text", name="Opening", content={"background": {"kind": "media", "media_id": pic["id"]}}) r = client.delete(f"/api/media/{pic['id']}", headers=owner_h) assert r.status_code == 409 and r.json()["error"]["params"]["used_by"] == [{"kind": "signage_slide", "id": s["id"], "name": "Window ยท Opening"}] def test_built_in_pictures(client, db, owner_h, tmp_path): from services import media as store folder = tmp_path / "builtin" folder.mkdir() (folder / "wood_table.jpg").write_bytes(jpeg(1200, 800, (120, 80, 40))) (folder / "README.md").write_text("not a picture") (folder / "broken.png").write_bytes(b"nope") assert store.import_builtin(db, str(folder)) == 1 assert store.import_builtin(db, str(folder)) == 0 # once lib = client.get("/api/media?collection=builtin", headers=owner_h).json() assert [(i["name"], i["builtin"]) for i in lib["items"]] == [("wood table", True)] assert client.get("/api/media?collection=own", headers=owner_h).json()["total"] == 0 bid = lib["items"][0]["id"] assert err(client.delete(f"/api/media/{bid}", headers=owner_h)) == "media.builtin" assert err(client.patch(f"/api/media/{bid}", headers=owner_h, json={"name": "mine"})) == "media.builtin" # the same picture uploaded by the venue is the built-in one again = up(client, owner_h, (folder / "wood_table.jpg").read_bytes()).json() assert again["existing"] is True and again["builtin"] is True def test_who_may(client, make_user, login, screens, pair): waiter = login(make_user(role="waiter")) assert client.get("/api/signage/playlists", headers=waiter).status_code == 403 token, _ = pair("kds", "Kitchen") assert err(client.get("/api/signage/screen", headers={"X-Xenia-Device": token})) == "device.out_of_scope" assert err(client.get("/api/signage/screen")) == "device.not_paired"