Port of the manager's KdsPage.jsx into terminal/src/apps/kds, split into pure logic (layout packing, age, buckets, rounds, totals, status cycle, settings; unit-tested) and components (card, item, action bars, dialogs, item totals, rails, settings panel). - Settings are the device's config; the settings panel saves them (managers only), zoom changes the size for the session. - Orders reload on live events and every reconnect; polling only while the live connection is down (DeviceProvider exposes liveUp; useLiveEvents helper). - "Tap to start" unlocks sound; venue clock via useFormat; English-first texts. - Undo of an item recomputes the order status at once; undo/redo clear the selection. - Tests: terminal unit +15, terminal.mjs +12 KDS checks (no polling, live order + chime, item/undo/prepare/decline, settings permission, change made elsewhere), backend regression for an item back to pending. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
156 lines
8.8 KiB
Python
156 lines
8.8 KiB
Python
"""Kitchen screens: device settings and the KDS's own scope (plan step 3.2a).
|
|
|
|
A paired KDS device sees its prep zone only, gets what it needs through /api/kds (context,
|
|
retry-print) and never needs a route outside its scope; a manager may change the screen's
|
|
settings from the screen itself (PATCH /api/devices/me/config).
|
|
"""
|
|
import pytest
|
|
|
|
from core.modules import device_config
|
|
from tests.test_live import err, pair, owner_h, terminal_login # noqa: F401 (fixtures)
|
|
|
|
|
|
@pytest.fixture
|
|
def kitchen(db, make_user):
|
|
"""Two prep zones (Grill, Bar), one order with an item for each, a waiter on it."""
|
|
from models.order import Order, OrderItem, OrderWaiter
|
|
from models.prep_zone import PrepZone
|
|
from models.product import Category, Product
|
|
from models.settings import PosSettings
|
|
waiter = make_user(role="waiter")
|
|
grill, bar = PrepZone(name="Grill", notification_name="Grill station"), PrepZone(name="Bar")
|
|
cat = Category(name="Food")
|
|
db.add_all([grill, bar, cat])
|
|
db.flush()
|
|
steak = Product(name="Steak", base_price=20.0, primary_category_id=cat.id)
|
|
beer = Product(name="Beer", base_price=5.0, primary_category_id=cat.id)
|
|
steak.prep_zones, beer.prep_zones = [grill], [bar]
|
|
db.add_all([steak, beer])
|
|
db.flush()
|
|
order = Order(opened_by=waiter.id, order_type="takeaway")
|
|
db.add(order)
|
|
db.flush()
|
|
db.add(OrderWaiter(order_id=order.id, waiter_id=waiter.id))
|
|
db.add_all([OrderItem(order_id=order.id, product_id=p.id, added_by=waiter.id, quantity=1, unit_price=p.base_price)
|
|
for p in (steak, beer)])
|
|
for key, value in (("orders.courses_enabled", "true"), ("orders.courses", '[{"id": 1, "name": "Starters"}]')):
|
|
row = db.get(PosSettings, key)
|
|
if row is None:
|
|
db.add(PosSettings(key=key, value=value))
|
|
else:
|
|
row.value = value
|
|
db.commit()
|
|
return {"grill": grill, "bar": bar, "order": order, "waiter": waiter}
|
|
|
|
|
|
@pytest.fixture
|
|
def screen(client, pair, make_user, kitchen):
|
|
"""The 'Grill' screen with a chef signed in on it: headers for its calls."""
|
|
token, r = pair("kds", "Grill screen", config={"prep_zone_id": kitchen["grill"].id})
|
|
assert r.status_code == 201, r.text
|
|
chef = make_user(role="chef")
|
|
jwt = terminal_login(client, token, chef)
|
|
return {"id": r.json()["id"], "token": token,
|
|
"h": {"X-Xenia-Device": token, "Authorization": f"Bearer {jwt}"}}
|
|
|
|
|
|
def names(orders):
|
|
return sorted(it["product_name"] for o in orders for it in o["items"])
|
|
|
|
|
|
def test_screen_sees_only_its_zone(client, screen, kitchen, owner_h): # "done when" 1
|
|
h = screen["h"]
|
|
assert names(client.get("/api/kds/orders", headers=h).json()["orders"]) == ["Steak"]
|
|
# choosing another zone on the screen doesn't work
|
|
assert names(client.get(f"/api/kds/orders?zone_id={kitchen['bar'].id}", headers=h).json()["orders"]) == ["Steak"]
|
|
# the manager's page still chooses (no device)
|
|
assert names(client.get(f"/api/kds/orders?zone_id={kitchen['bar'].id}", headers=owner_h).json()["orders"]) == ["Beer"]
|
|
assert names(client.get("/api/kds/orders", headers=owner_h).json()["orders"]) == ["Beer", "Steak"]
|
|
# a screen without a zone sees every zone
|
|
assert client.patch(f"/api/devices/{screen['id']}", headers=owner_h,
|
|
json={"config": {"prep_zone_id": None}}).status_code == 200
|
|
assert names(client.get("/api/kds/orders", headers=h).json()["orders"]) == ["Beer", "Steak"]
|
|
|
|
|
|
def test_context_gives_settings_zone_and_courses(client, screen, kitchen, owner_h):
|
|
ctx = client.get("/api/kds/context", headers=screen["h"]).json()
|
|
assert ctx["device"] == {"id": screen["id"], "name": "Grill screen"}
|
|
assert ctx["zone"]["name"] == "Grill" and ctx["zone"]["notification_name"] == "Grill station"
|
|
assert {z["name"] for z in ctx["prep_zones"]} >= {"Grill", "Bar"}
|
|
assert ctx["settings"]["prep_zone_id"] == kitchen["grill"].id and ctx["settings"]["font_scale"] == 100
|
|
assert ctx["courses_enabled"] is True and ctx["courses"] == [{"id": 1, "name": "Starters"}]
|
|
# the manager's page: default settings, no device
|
|
page = client.get("/api/kds/context", headers=owner_h).json()
|
|
assert page["device"] is None and page["zone"] is None
|
|
assert page["settings"] == {f.key: f.default for f in device_config.for_app("kds")}
|
|
|
|
|
|
def test_no_kds_call_is_refused_by_the_scope(client, screen, kitchen): # "done when" 2
|
|
h, oid = screen["h"], kitchen["order"].id
|
|
item = client.get("/api/kds/orders", headers=h).json()["orders"][0]["items"][0]["id"]
|
|
calls = [
|
|
("get", "/api/devices/me", None),
|
|
("get", "/api/kds/context", None),
|
|
("get", "/api/kds/orders", None),
|
|
("get", "/api/kds/on-shift-waiters", None),
|
|
("put", f"/api/kds/orders/{oid}/items/{item}/kds_status", {"status": "preparing"}),
|
|
("put", f"/api/kds/orders/{oid}/kds_status", {"status": "preparing"}),
|
|
("post", f"/api/kds/orders/{oid}/notify-items-ready", {"ready_count": 1, "total_count": 2}),
|
|
("post", f"/api/kds/orders/{oid}/notify-complete", {}),
|
|
("post", f"/api/kds/orders/{oid}/call-waiter", {}),
|
|
("post", "/api/kds/call-waiter-general", {"waiter_ids": [kitchen["waiter"].id]}),
|
|
("post", f"/api/kds/orders/{oid}/retry-print", None),
|
|
("post", f"/api/kds/orders/{oid}/print", {"mode": "primary"}),
|
|
("put", f"/api/kds/orders/{oid}/items/{item}/decline", {"decline_note": "out of stock"}),
|
|
]
|
|
for method, url, body in calls:
|
|
r = getattr(client, method)(url, headers=h, **({"json": body} if body is not None else {}))
|
|
assert r.status_code < 400, f"{method} {url} -> {r.status_code} {r.text}"
|
|
# what the old page used instead is out of scope for a kitchen screen
|
|
assert err(client.get("/api/prep-zones", headers=h)) == "device.out_of_scope"
|
|
assert err(client.post(f"/api/orders/{oid}/retry-print", headers=h)) == "device.out_of_scope"
|
|
|
|
|
|
def test_messages_are_signed_by_the_screen(client, db, screen, kitchen, owner_h):
|
|
from models.message import StaffMessage
|
|
oid = kitchen["order"].id
|
|
client.post(f"/api/kds/orders/{oid}/call-waiter", headers=screen["h"], json={"kds_zone": "Fake"})
|
|
assert db.query(StaffMessage).order_by(StaffMessage.id.desc()).first().kds_zone == "Grill station"
|
|
assert client.patch(f"/api/devices/{screen['id']}", headers=owner_h,
|
|
json={"config": {"prep_zone_id": None}}).status_code == 200
|
|
client.post(f"/api/kds/orders/{oid}/call-waiter", headers=screen["h"], json={})
|
|
assert db.query(StaffMessage).order_by(StaffMessage.id.desc()).first().kds_zone == "Grill screen"
|
|
client.post(f"/api/kds/orders/{oid}/call-waiter", headers=owner_h, json={"kds_zone": "Pass"})
|
|
assert db.query(StaffMessage).order_by(StaffMessage.id.desc()).first().kds_zone == "Pass"
|
|
|
|
|
|
def test_settings_changed_from_the_screen(client, screen, make_user, kitchen):
|
|
# the chef doesn't hold core.devices.manage
|
|
r = client.patch("/api/devices/me/config", headers=screen["h"], json={"config": {"font_scale": 140}})
|
|
assert err(r) == "access.denied"
|
|
# a manager signed in on the same screen does; sent keys are merged
|
|
boss = make_user(role="store_manager")
|
|
h = {"X-Xenia-Device": screen["token"], "Authorization": f"Bearer {terminal_login(client, screen['token'], boss)}"}
|
|
r = client.patch("/api/devices/me/config", headers=h, json={"config": {"font_scale": 140, "sound": False}})
|
|
assert r.status_code == 200, r.text
|
|
cfg = r.json()["config"]
|
|
assert cfg["font_scale"] == 140 and cfg["sound"] is False and cfg["prep_zone_id"] == kitchen["grill"].id
|
|
assert client.get("/api/kds/context", headers=h).json()["settings"]["font_scale"] == 140
|
|
assert err(client.patch("/api/devices/me/config", headers=h,
|
|
json={"config": {"age_late_minutes": 2}})) == "validation.device_config"
|
|
assert err(client.patch("/api/devices/me/config", headers=h,
|
|
json={"config": {"colour_late": "red"}})) == "validation.device_config"
|
|
# without a device it's not a screen
|
|
assert err(client.patch("/api/devices/me/config", headers={"Authorization": h["Authorization"]},
|
|
json={"config": {}})) in ("device.not_paired", "device.required")
|
|
|
|
|
|
def test_item_back_to_pending_reopens_the_order(client, screen, kitchen):
|
|
"""Undo on the screen (3.2b): an item done → back to pending puts the order back too."""
|
|
h, oid = screen["h"], kitchen["order"].id
|
|
item = client.get("/api/kds/orders", headers=h).json()["orders"][0]["items"][0]["id"]
|
|
for status, expected in (("done", "preparing"), ("pending", "pending")): # the Bar item is still to do
|
|
assert client.put(f"/api/kds/orders/{oid}/items/{item}/kds_status", headers=h, json={"status": status}).status_code == 200
|
|
order = next(o for o in client.get("/api/kds/orders", headers=h).json()["orders"] if o["id"] == oid)
|
|
assert order["kds_status"] == expected
|