Files
bonaminandClaude Opus 5.5 c1af7770cf feat: ordering from the menu tablet at a table (platform-v2 step 6.6, D-30)
- tablet_orders (0034) + services/tablet_orders.py: venue rules (menu_tablet.ordering) with
  per-tablet overrides: kitchen or waiter confirms first, no open order open/refuse, limits
  (items per order, of one dish, time between orders)
- /api/menu-tablet: quote, order, order/{id}, bill (see + ask), requests accept/decline, ordering
- terminal: Add to the order (POS ProductDialog), cart, note, send, live status, the bill
- waiter: TabletRequests on the table page, tablet_order message theme with Open the table
- manager: Settings -> Menu tablets
- tests: test_tablet_orders.py, menu-tablet unit, menu_tablet.mjs e2e (the done-when)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 14:42:37 +02:00

152 lines
8.1 KiB
Python

"""The menu tablet (plan step 5.7a): the menu for a paired tablet, categories in the catalogue's
order with their products (a product in several categories shows in each), a sales point's live
prices, sold out crossed out or hidden, one category or all; menu changes reach the tablets."""
import pytest
from tests.test_live import err, owner_h, pair # noqa: F401 (fixtures)
from tests.test_media import jpeg, media_dir, up # noqa: F401 (fixture + helpers)
@pytest.fixture
def menu(db):
from models.product import Category, Product, ProductCategory
grill, drinks = Category(name="Grill", sort_order=0), Category(name="Drinks", sort_order=1)
db.add_all([grill, drinks])
db.flush()
pita = Category(name="Pita", sort_order=0, parent_id=grill.id)
db.add(pita)
db.flush()
def prod(name, cats, price, **kw):
p = Product(name=name, base_price=price, primary_category_id=cats[0].id, **kw)
db.add(p)
db.flush()
for i, c in enumerate(cats):
db.add(ProductCategory(product_id=p.id, category_id=c.id, sort_order=kw.get("sort_order", 0)))
return p
out = {"steak": prod("Steak", [grill], 12.0, description="Pork, 300 g", tags='["gluten-free"]', sort_order=1),
"souvlaki": prod("Souvlaki", [grill, pita], 3.5, sort_order=0),
"beer": prod("Beer", [drinks], 4.5),
"wine": prod("Wine", [drinks], 5.0, sold_at='["online_delivery"]'),
"old": prod("Old", [grill], 1.0, lifecycle_status="archived")}
db.commit()
out["cats"] = {"grill": grill.id, "drinks": drinks.id, "pita": pita.id}
return out
@pytest.fixture
def tablet(pair):
token, r = pair("menu_tablet", "Entrance")
assert r.status_code == 201, r.text
return {"h": {"X-Xenia-Device": token}, "id": r.json()["id"]}
def names(data):
return [(c["name"], [data["products"][str(i)]["name"] for i in c["products"]]) for c in data["categories"]]
def test_the_menu_on_a_tablet(client, owner_h, tablet, menu):
photo = up(client, owner_h, jpeg(800, 600)).json()
client.put(f"/api/products/{menu['steak'].id}", headers=owner_h, json={"media_id": photo["id"]})
data = client.get("/api/menu-tablet/menu", headers=tablet["h"]).json()
assert names(data) == [("Grill", ["Souvlaki", "Steak"]), ("Pita", ["Souvlaki"]), ("Drinks", ["Beer"])] # Wine: delivery only
assert [c["depth"] for c in data["categories"]] == [0, 1, 0]
steak = data["products"][str(menu["steak"].id)]
assert (steak["price"], steak["description"], steak["tags"], steak["image"]["card"]) == (12.0, "Pork, 300 g", ["gluten-free"], photo["urls"]["card"])
assert data["config"]["view"] == "expanded" and data["config"]["search"] is True
def test_the_tablets_settings_change_the_menu(client, owner_h, tablet, menu, db):
menu["beer"].is_available = False
db.commit()
data = client.get("/api/menu-tablet/menu", headers=tablet["h"]).json()
assert data["products"][str(menu["beer"].id)]["available"] is False
cfg = {"sold_out": "hide", "category": menu["cats"]["grill"], "sales_point": "none", "view": "full"}
assert client.patch(f"/api/devices/{tablet['id']}", headers=owner_h, json={"config": cfg}).status_code == 200
data = client.get("/api/menu-tablet/menu", headers=tablet["h"]).json()
assert names(data) == [("Grill", ["Souvlaki", "Steak"]), ("Pita", ["Souvlaki"])]
assert {p["price"] for p in data["products"].values()} == {None} and data["config"]["view"] == "full"
client.patch(f"/api/devices/{tablet['id']}", headers=owner_h, json={"config": {"sales_point": "online_delivery"}})
data = client.get("/api/menu-tablet/menu", headers=tablet["h"]).json()
assert names(data)[0][0] == "Grill" and None not in {p["price"] for p in data["products"].values()} # prices again, still only Grill
assert err(client.patch(f"/api/devices/{tablet['id']}", headers=owner_h, json={"config": {"view": "huge"}})) == "validation.device_config"
def test_menu_changes_reach_the_tablets(client, owner_h, tablet, menu, db, monkeypatch):
import routers.menu_tablet as mt
sent = []
monkeypatch.setattr("services.ws_bus.broadcast_sync", lambda kind, data, **kw: sent.append((kind, kw.get("device_ids"))))
mt.menu_changed(db)
assert sent == [("menu_tablet.changed", [tablet["id"]])]
client.put(f"/api/products/{menu['steak'].id}", headers=owner_h, json={"base_price": 13.5})
assert client.get("/api/menu-tablet/menu", headers=tablet["h"]).json()["products"][str(menu["steak"].id)]["price"] == 13.5
def test_only_menu_tablets(client, pair):
token, _ = pair("kds", "Kitchen")
assert err(client.get("/api/menu-tablet/menu", headers={"X-Xenia-Device": token})) == "device.out_of_scope"
assert err(client.get("/api/menu-tablet/menu")) == "device.not_paired"
# ── 5.7b: calling a waiter from a tablet at a table ─────────────────────────
@pytest.fixture
def table(db):
from models.table import Table
t = Table(number=5, is_active=True)
db.add(t)
db.commit()
return t
@pytest.fixture(autouse=True)
def _calls():
import routers.menu_tablet as mt
mt.reset_calls()
yield
mt.reset_calls()
def test_a_tablet_at_a_table_calls_a_waiter(client, owner_h, tablet, menu, table, make_user, db, monkeypatch):
"""Done when (5.7b): a tablet set to table 5 calls a waiter and the waiter gets the message."""
import routers.kds as kds
from models.user import WaiterZone
maria = make_user(role="waiter")
db.add(WaiterZone(waiter_id=maria.id, group_id=None))
db.commit()
got = []
monkeypatch.setattr(kds, "broadcast_sync", lambda kind, data, **kw: got.append((kind, data, kw)), raising=False)
assert client.get("/api/menu-tablet/menu", headers=tablet["h"]).json()["table"] is None
assert err(client.post("/api/menu-tablet/call", headers=tablet["h"])) == "menu_tablet.no_table"
client.patch(f"/api/devices/{tablet['id']}", headers=owner_h, json={"config": {"table": table.id}})
assert client.get("/api/menu-tablet/menu", headers=tablet["h"]).json()["table"] == {"id": table.id, "name": "5", "can_call": True, "can_order": False, "can_see_bill": True}
r = client.post("/api/menu-tablet/call", headers=tablet["h"])
assert r.status_code == 200 and r.json()["waiters"] >= 1 and r.json()["again_in"] == 60
from models.message import StaffMessage
msg = db.query(StaffMessage).order_by(StaffMessage.id.desc()).first()
assert msg.message_type == "table_call" and msg.body == "Table 5: the guests call a waiter." and str(maria.id) in msg.target_waiter_ids
assert str(table.id) in msg.table_ids
# once a minute at most
again = client.post("/api/menu-tablet/call", headers=tablet["h"])
assert again.status_code == 429 and err(again) == "menu_tablet.call_too_soon" and again.json()["error"]["params"]["seconds"] <= 60
# the option off: no calls, the menu says so
client.patch(f"/api/devices/{tablet['id']}", headers=owner_h, json={"config": {"table": table.id, "call_waiter": False}})
assert client.get("/api/menu-tablet/menu", headers=tablet["h"]).json()["table"]["can_call"] is False
assert err(client.post("/api/menu-tablet/call", headers=tablet["h"])) == "menu_tablet.no_table"
def test_the_tables_open_order_decides_who_hears_it(client, owner_h, tablet, table, make_user, db):
from models.order import Order, OrderWaiter
from models.message import StaffMessage
nikos, eleni = make_user(role="waiter"), make_user(role="waiter")
order = Order(table_id=table.id, opened_by=nikos.id, status="open", order_type="dine_in")
db.add(order)
db.flush()
db.add(OrderWaiter(order_id=order.id, waiter_id=nikos.id))
db.commit()
client.patch(f"/api/devices/{tablet['id']}", headers=owner_h, json={"config": {"table": table.id}})
assert client.post("/api/menu-tablet/call", headers=tablet["h"]).status_code == 200
msg = db.query(StaffMessage).order_by(StaffMessage.id.desc()).first()
assert msg.target_waiter_ids == f"[{nikos.id}]" and eleni.id
assert err(client.patch(f"/api/devices/{tablet['id']}", headers=owner_h, json={"config": {"table": 99999}})) == "validation.device_config"