"""Floor plans and the live table map (plan step 6.8, D-31).""" import pytest from tests.test_live import err, owner_h # noqa: F401 (fixtures) @pytest.fixture def venue(db): from models.table import Table, TableGroup inside, terrace = TableGroup(name="Inside", sort_order=0), TableGroup(name="Terrace", sort_order=1) db.add_all([inside, terrace]) db.flush() t = {n: Table(number=n, group_id=inside.id if n < 10 else terrace.id, seat_count=4) for n in (1, 2, 5, 11)} db.add_all(t.values()) db.commit() return {"inside": inside, "terrace": terrace, **{f"t{n}": x for n, x in t.items()}} def plan(client, h, **extra): r = client.post("/api/floor-plans", headers=h, json={"name": "Ground floor", "width": 1000, "height": 600, **extra}) assert r.status_code == 201, r.text return r.json() def test_a_floor_plan_with_tables_walls_and_an_irregular_shape(client, owner_h, venue, monkeypatch): import services.ws_bus as bus sent = [] monkeypatch.setattr(bus, "broadcast_sync", lambda t, d, **kw: sent.append(t)) p = plan(client, owner_h, group_id=venue["inside"].id, grid_on=True, grid_size=50, outline=[[0, 0], [1000, 0], [1000, 300], [600, 600], [0, 600]]) assert p["outline"][3] == [600, 600] and p["grid_on"] is True and p["items"] == [] r = client.put(f"/api/floor-plans/{p['id']}/layout", headers=owner_h, json={ "tables": [{"id": venue["t1"].id, "x": 100, "y": 100, "width": 80, "height": 80, "shape": "round", "seats": 2}, {"id": venue["t2"].id, "x": 1500, "y": 200, "width": 160, "height": 70, "shape": "long", "rotation": -90}], "items": [{"kind": "bar", "x": 500, "y": 40, "width": 400, "height": 50}, {"kind": "label", "x": 800, "y": 500, "width": 120, "height": 30, "text": " Kitchen "}], }) assert r.status_code == 200, r.text assert [(i["kind"], i["text"]) for i in r.json()["items"]] == [("bar", None), ("label", "Kitchen")] data = client.get("/api/floor-plans", headers=owner_h).json() placed = {t["id"]: t for t in data["tables"]} assert placed[venue["t1"].id]["plan_id"] == p["id"] and placed[venue["t1"].id]["shape"] == "round" and placed[venue["t1"].id]["seats"] == 2 assert placed[venue["t2"].id]["x"] == 1000 and placed[venue["t2"].id]["rotation"] == 270 # kept inside, angle normalised assert placed[venue["t5"].id]["plan_id"] is None # saved again without table 2: it leaves the plan client.put(f"/api/floor-plans/{p['id']}/layout", headers=owner_h, json={"tables": [{"id": venue["t1"].id, "x": 1, "y": 1, "width": 80, "height": 80}]}) assert {t["id"]: t["plan_id"] for t in client.get("/api/floor-plans", headers=owner_h).json()["tables"]}[venue["t2"].id] is None assert "floor_plans.changed" in sent # bad shapes are refused assert err(client.put(f"/api/floor-plans/{p['id']}", headers=owner_h, json={"name": "x", "width": 1000, "height": 600, "outline": [[0, 0], [10, 10]]})) == "floor_plan.bad_outline" assert err(client.put(f"/api/floor-plans/{p['id']}", headers=owner_h, json={"name": "x", "width": 1000, "height": 600, "outline": [[0, 0], [2000, 0], [0, 10]]})) == "floor_plan.bad_outline" # deleting the plan keeps the tables assert client.delete(f"/api/floor-plans/{p['id']}", headers=owner_h).status_code == 204 assert all(t["plan_id"] is None for t in client.get("/api/floor-plans", headers=owner_h).json()["tables"]) def test_several_plans_per_zone_and_a_background(client, owner_h, venue, db): from models.media import Media m = Media(name="Top view", kind="image", sha256="a" * 64, content_type="image/webp", bytes=10) db.add(m) db.commit() a = plan(client, owner_h, group_id=venue["inside"].id, name="Ground floor") b = plan(client, owner_h, group_id=venue["inside"].id, name="Upstairs", media_id=m.id, background_opacity=0.4) assert b["background"]["full"].endswith("/full") and b["sort_order"] > a["sort_order"] assert err(client.post("/api/floor-plans", headers=owner_h, json={"name": "x", "group_id": 999})) == "floor_plan.bad_zone" def test_the_live_map_shows_each_table_state_to_the_right_people(client, owner_h, venue, db, make_user, login): from core.timeutil import utcnow from models.order import Order, OrderWaiter from models.user import WaiterZone p = plan(client, owner_h) client.put(f"/api/floor-plans/{p['id']}/layout", headers=owner_h, json={"tables": [ {"id": venue[k].id, "x": 100 * i, "y": 100, "width": 80, "height": 80} for i, k in enumerate(("t1", "t2", "t11"), 1)]}) maria = make_user(role="waiter", nickname="Maria") db.add(WaiterZone(waiter_id=maria.id, group_id=venue["inside"].id)) o = Order(table_id=venue["t1"].id, opened_by=maria.id, status="open", customer_count=3, opened_at=utcnow()) db.add(o) db.flush() db.add(OrderWaiter(order_id=o.id, waiter_id=maria.id)) db.commit() h = login(maria, client="waiter") data = client.get("/api/floor-plans/live", headers=h).json() by = {t["id"]: t for t in data["tables"]} assert venue["t11"].id not in by # the terrace isn't her zone t1 = by[venue["t1"].id] assert t1["state"] == "open" and t1["mine"] is True and t1["order"]["guests"] == 3 and t1["order"]["people"] == ["Maria"] assert t1["order"]["opened_at"].endswith("Z") and t1["seats"] == 4 assert by[venue["t2"].id]["state"] == "free" and by[venue["t2"].id]["order"] is None assert [x["id"] for x in data["plans"]] == [p["id"]] assert len(client.get("/api/floor-plans/live", headers=owner_h).json()["tables"]) == 4 # only the tables' managers set plans up assert client.post("/api/floor-plans", headers=h, json={"name": "x"}).status_code == 403