"""Scenes (owner 2026-11-21): named modes of the venue (party mode) that are on or off; price rules follow them ("a scene is on"), signage lines too (tests/test_signage_schedule.py).""" from datetime import timedelta from core.timeutil import utcnow from tests.test_live import err, owner_h, pair, terminal_login # noqa: F401 (fixtures) from tests.test_pos import menu, till # noqa: F401 (fixtures) from tests.test_pricing_setup import happy_hour def test_scenes_are_made_and_switched(client, owner_h, make_user, login): r = client.post("/api/scenes", headers=owner_h, json={"name": " Party mode ", "color": "#ff6699"}) assert r.status_code == 201 and r.json()["name"] == "Party mode" and r.json()["active"] is False sid = r.json()["id"] assert client.put(f"/api/scenes/{sid}/active", headers=owner_h, json={"active": True}).json()["active"] is True soon = (utcnow() + timedelta(hours=2)).isoformat() on = client.put(f"/api/scenes/{sid}/active", headers=owner_h, json={"active": True, "until": soon}).json() assert on["active"] is True and on["until"] past = (utcnow() - timedelta(minutes=1)).isoformat() assert err(client.put(f"/api/scenes/{sid}/active", headers=owner_h, json={"active": True, "until": past})) == "scene.until_past" assert err(client.put("/api/scenes/99999/active", headers=owner_h, json={"active": True})) == "scene.not_found" assert client.post("/api/scenes", headers=owner_h, json={"name": "x", "color": "pink"}).status_code == 422 # a waiter can't switch scenes h = login(make_user(role="waiter"), client="waiter") assert client.put(f"/api/scenes/{sid}/active", headers=h, json={"active": False}).status_code == 403 def test_a_scene_turns_itself_off(db, client, owner_h): from models.scene import Scene from services.scenes import active_ids s = Scene(name="Brunch", is_active=True, until=utcnow() - timedelta(seconds=1), sort_order=0) db.add(s) db.commit() assert s.id not in active_ids(db) assert client.get("/api/scenes", headers=owner_h).json()[0]["active"] is False def test_a_price_rule_follows_a_scene(client, owner_h, till, menu): line = {"product_id": menu["coffee"].id, "quantity": 2} sid = client.post("/api/scenes", headers=owner_h, json={"name": "Party mode"}).json()["id"] rule = happy_hour(targets=[{"target_type": "item", "target_ids": [menu["coffee"].id]}]) rule["conditions"] = [{"condition_type": "scene_active", "params": {"scene_id": sid}}] r = client.post("/api/pricing/modifiers", headers=owner_h, json=rule) assert r.status_code == 201, r.text assert client.post("/api/pos/quote", headers=till["h"], json={"lines": [line]}).json()["total"] == 6.0 client.put(f"/api/scenes/{sid}/active", headers=owner_h, json={"active": True}) assert client.post("/api/pos/quote", headers=till["h"], json={"lines": [line]}).json()["total"] == 4.8 client.put(f"/api/scenes/{sid}/active", headers=owner_h, json={"active": False}) assert client.post("/api/pos/quote", headers=till["h"], json={"lines": [line]}).json()["total"] == 6.0 bad = dict(rule, conditions=[{"condition_type": "scene_active", "params": {}}]) assert err(client.post("/api/pricing/modifiers", headers=owner_h, json=bad)) == "pricing.bad_condition"