"""The menu in English and Greek (plan step 6.4, owner 2026-11-18): translations of product names, descriptions, quick options and category names; a missing one shows the base text. Customer screens use them (cloud menu, menu tablet, signage boards); tickets and staff screens don't.""" from tests.test_live import err, owner_h, pair # noqa: F401 (fixtures) from tests.test_menu_tablet import menu, tablet # noqa: F401 (fixtures) EN = {"en": {"name": "Pork souvlaki", "description": "On a skewer", "quick": {"Pita": "Pitta bread"}}} def test_translations_are_stored_cleaned_and_validated(client, owner_h, menu): pid = menu["souvlaki"].id r = client.put(f"/api/products/{pid}", headers=owner_h, json={"translations": {"en": {"name": " Pork souvlaki ", "description": ""}}}) assert r.status_code == 200, r.text assert r.json()["translations"] == {"en": {"name": "Pork souvlaki"}} # trimmed, empty dropped assert client.put(f"/api/products/{pid}", headers=owner_h, json={"base_price": 4}).json()["translations"]["en"]["name"] == "Pork souvlaki" for bad in ({"fr": {"name": "x"}}, {"en": {"colour": "x"}}, {"en": {"quick": ["x"]}}): r = client.put(f"/api/products/{pid}", headers=owner_h, json={"translations": bad}) assert r.status_code == 422 and err(r) == "catalog.translation", bad cat = menu["cats"]["grill"] r = client.put(f"/api/products/categories/{cat}", headers=owner_h, json={"translations": {"en": {"name": "From the grill"}}}) assert r.status_code == 200 and r.json()["translations"] == {"en": {"name": "From the grill"}} assert client.put(f"/api/products/categories/{cat}", headers=owner_h, json={"translations": {"en": {"title": "x"}}}).status_code == 422 def test_the_tablet_switches_names_and_falls_back_to_the_base(client, owner_h, menu, tablet): client.put(f"/api/products/{menu['souvlaki'].id}", headers=owner_h, json={"translations": EN}) client.put(f"/api/products/categories/{menu['cats']['grill']}", headers=owner_h, json={"translations": {"en": {"name": "Grill (en)"}}}) data = client.get("/api/menu-tablet/menu", headers=tablet["h"]).json() souv = data["products"][str(menu["souvlaki"].id)] assert souv["names"] == {"el": "Souvlaki", "en": "Pork souvlaki"} and souv["name"] == "Souvlaki" assert souv["descriptions"]["en"] == "On a skewer" steak = data["products"][str(menu["steak"].id)] assert steak["names"] == {"el": "Steak", "en": "Steak"} # not translated: the base name grill = next(c for c in data["categories"] if c["id"] == menu["cats"]["grill"]) assert grill["names"] == {"el": "Grill", "en": "Grill (en)"} def test_the_cloud_menu_carries_names_per_language(client, db, owner_h, menu): from models.product import ProductQuickOption db.add(ProductQuickOption(product_id=menu["souvlaki"].id, name="Pita", price=0.5)) db.commit() client.put(f"/api/products/{menu['souvlaki'].id}", headers=owner_h, json={"translations": EN}) from services.cloud_sync import menu_snapshot snap, _ = menu_snapshot(db) grill = next(c for c in snap["categories"] if c["id"] == menu["cats"]["grill"]) souv = next(p for p in grill["products"] if p["id"] == menu["souvlaki"].id) assert souv["names"] == {"el": "Souvlaki", "en": "Pork souvlaki"} and souv["name"] == "Souvlaki" assert souv["quick_options"][0]["names"] == {"el": "Pita", "en": "Pitta bread"} assert grill["names"] == {"el": "Grill", "en": "Grill"} def test_a_board_in_english_and_tickets_stay_in_the_base(client, db, owner_h, menu): client.put(f"/api/products/{menu['souvlaki'].id}", headers=owner_h, json={"translations": EN}) from services.signage_menu import resolve names = lambda lang: [i["name"] for g in resolve(db, {"select": "all", "language": lang})["groups"] for i in g["items"]] # noqa: E731 assert "Pork souvlaki" in names("en") and "Souvlaki" not in names("en") assert "Souvlaki" in names(None) and "Souvlaki" in names("el") # what the staff see (phones, counter): the base name assert next(p for p in client.get("/api/products/", headers=owner_h).json() if p["id"] == menu["souvlaki"].id)["name"] == "Souvlaki" def test_catalogue_bundle_keeps_translations(client, db, owner_h, menu): client.put(f"/api/products/{menu['beer'].id}", headers=owner_h, json={"translations": {"en": {"name": "Lager"}}}) client.put(f"/api/products/categories/{menu['cats']['drinks']}", headers=owner_h, json={"translations": {"en": {"name": "Drinks (en)"}}}) bundle = client.get("/api/data-transfer/export/catalog", headers=owner_h).json() client.put(f"/api/products/{menu['beer'].id}", headers=owner_h, json={"translations": {}}) client.put(f"/api/products/categories/{menu['cats']['drinks']}", headers=owner_h, json={"translations": {}}) assert client.post("/api/data-transfer/import/catalog", headers=owner_h, json=bundle).status_code < 300 db.expire_all() from core.menu_languages import of from models.product import Category, Product assert of(db.get(Product, menu["beer"].id)) == {"en": {"name": "Lager"}} assert of(db.get(Category, menu["cats"]["drinks"])) == {"en": {"name": "Drinks (en)"}} def test_the_frontend_list_matches_the_backend(): """packages/access/src/menuLanguages.js keeps the same languages.""" import re from pathlib import Path from core.menu_languages import MENU_LANGUAGES js = (Path(__file__).resolve().parents[2] / "packages" / "access" / "src" / "menuLanguages.js").read_text(encoding="utf-8") listed = re.search(r"export const MENU_LANGUAGES = \[([^\]]*)\]", js).group(1) assert [x.strip().strip("'") for x in listed.split(",") if x.strip()] == list(MENU_LANGUAGES) def test_every_choice_keeps_its_names_per_language(client, db, owner_h, menu): """Owner 2026-11-21 (0042): every choice has its name per language; empty ones dropped, unknown languages refused; sub-choices and the shared sub-set keep theirs in their JSON.""" pid = menu["souvlaki"].id body = { "modifier_groups": [{"modifier_type": "ingredient", "name": "Sauces", "translations": {"el": "Σάλτσες", "en": "Sauces"}}], "quick_options": [{"name": "Πίτα", "translations": {"el": "Πίτα", "en": " Pitta ", "fr": ""}}], "ingredients": [{"name": "Κρεμμύδι", "group_id": 0, "translations": {"el": "Κρεμμύδι"}}], "options": [{"name": "Σιρόπι", "translations": {"en": "Syrup"}, "sub_choices": [{"name": "Καραμέλα", "translations": {"en": "Caramel", "el": ""}}]}], "preference_sets": [{"name": "Ζάχαρη", "translations": {"en": "Sugar"}, "choices": [{"name": "Σκέτος", "translations": {"en": "No sugar"}, "sub_choices": [{"name": "Στέβια", "translations": {"en": "Stevia"}}]}], "shared_subset": {"name": "Γάλα", "translations": {"en": "Milk"}, "choices": [{"name": "Φρέσκο", "translations": {"en": "Fresh"}}]}}], } r = client.put(f"/api/products/{pid}", headers=owner_h, json=body) assert r.status_code == 200, r.text p = r.json() assert p["modifier_groups"][0]["translations"] == {"el": "Σάλτσες", "en": "Sauces"} assert p["quick_options"][0]["translations"] == {"el": "Πίτα", "en": "Pitta"} assert p["ingredients"][0]["translations"] == {"el": "Κρεμμύδι"} assert p["options"][0]["translations"] == {"en": "Syrup"} assert p["options"][0]["sub_choices"][0]["translations"] == {"en": "Caramel"} ps = p["preference_sets"][0] assert ps["translations"] == {"en": "Sugar"} and ps["choices"][0]["translations"] == {"en": "No sugar"} assert ps["choices"][0]["sub_choices"][0]["translations"] == {"en": "Stevia"} assert ps["shared_subset"]["translations"] == {"en": "Milk"} and ps["shared_subset"]["choices"][0]["translations"] == {"en": "Fresh"} bad = client.put(f"/api/products/{pid}", headers=owner_h, json={"quick_options": [{"name": "x", "translations": {"de": "x"}}]}) assert bad.status_code == 422 and err(bad) == "catalog.translation" # the cloud's quick option names come from the option itself now from services.cloud_sync import menu_snapshot db.expire_all() snap, _ = menu_snapshot(db) souv = next(x for c in snap["categories"] for x in c["products"] if x["id"] == pid) assert souv["quick_options"][0]["names"] == {"el": "Πίτα", "en": "Pitta"} def test_migration_0042_puts_the_texts_in_their_language(tmp_path): """0042: texts typed before had no language: Greek letters → el, else the venue's language; the product's old quick translations move onto the quick options.""" import json from alembic import command from sqlalchemy import create_engine, text from core.schema import alembic_config, upgrade_to_head eng = create_engine(f"sqlite:///{(tmp_path / 'm.db').as_posix()}") with eng.begin() as conn: command.upgrade(alembic_config(conn), "0041_floor_item_color") conn.execute(text("insert into pos_settings (key, value, updated_at) values ('venue.language', 'el', '2026-01-01')")) conn.execute(text("insert into categories (id, name, sort_order, general_sort_order, auto_expanded) values (1, 'Ψητά', 0, 0, 0)")) conn.execute(text("insert into products (id, name, description, base_price, is_available, lifecycle_status, sort_order, sold_at, digital_available, " "unit_type, quick_add_enabled, is_service_item, translations) values (1, 'Burger', 'Με πατάτες', 8, 1, 'active', 0, '[]', 1, 'piece', 1, 0, " "'{\"en\": {\"description\": \"With fries\", \"quick\": {\"Μπέικον\": \"Bacon\"}}}')")) conn.execute(text("insert into product_quick_options (id, product_id, name, price, allow_multiple, sort_order, is_favorite, favorite_sort_order, is_compact) " "values (1, 1, 'Μπέικον', 1, 0, 0, 0, 0, 0)")) conn.execute(text("insert into product_options (id, product_id, name, extra_cost, allow_multiple, multi_select, is_favorite, favorite_sort_order, is_compact, sub_choices) " "values (1, 1, 'Σάλτσα', 0, 0, 0, 0, 0, 0, '[{\"name\": \"BBQ\"}]')")) upgrade_to_head(eng) with eng.connect() as conn: p = json.loads(conn.execute(text("select translations from products where id = 1")).scalar()) q = json.loads(conn.execute(text("select translations from product_quick_options where id = 1")).scalar()) o = conn.execute(text("select translations, sub_choices from product_options where id = 1")).fetchone() c = json.loads(conn.execute(text("select translations from categories where id = 1")).scalar()) eng.dispose() # "Burger" has no Greek letters: the venue's language (el); the description is Greek; the English one is kept assert p == {"el": {"name": "Burger", "description": "Με πατάτες"}, "en": {"description": "With fries"}} assert q == {"el": "Μπέικον", "en": "Bacon"} assert json.loads(o[0]) == {"el": "Σάλτσα"} and json.loads(o[1])[0]["translations"] == {"el": "BBQ"} assert c == {"el": {"name": "Ψητά"}}