- connect_frontend/menu-app rebuilt like the order app: the same tokens (index.css, tailwind.config.js), texts in i18n.js (en, el; the old 'ot_lang' choice carries over), pure logic in src/logic.js with tests (menuFor in the guest's language, QR prices, search in either language, cart, order body, refusals). MenuPage (menu, search, dish sheet, cart, checkout at the table) and ConfirmPage; App.jsx, components/primitives and OrderConfirm removed; react-hot-toast dropped. - Allergens on every dish (contains on the card; contains / may contain on the dish sheet) and in the order app's cards too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
62 lines
3.7 KiB
JavaScript
62 lines
3.7 KiB
JavaScript
// The QR menu's logic (plan step 6.5).
|
|
import assert from 'node:assert/strict'
|
|
import { test } from 'node:test'
|
|
import { allergenList, cartCount, cartLines, cartTotal, fold, menuFor, orderBody, refusalKey, search, setQty } from '../src/logic.js'
|
|
|
|
const data = { categories: [
|
|
{ id: 1, name: 'Ψητά', names: { el: 'Ψητά', en: 'Grill' }, products: [
|
|
{ id: 5, name: 'Σουβλάκι', names: { el: 'Σουβλάκι', en: 'Pork skewer' }, descriptions: { el: 'Με πίτα', en: 'With pitta' },
|
|
prices: { qr_table: 3.5, online_delivery: 4 }, sold_at: ['qr_table', 'online_delivery'], allergens: { gluten: 'contains', sesame: 'may_contain' } },
|
|
{ id: 6, name: 'Μπριζόλα', prices: { qr_table: 12 }, sold_at: ['qr_table'], digital_available: false },
|
|
{ id: 7, name: 'Delivery only', prices: { online_delivery: 9 }, sold_at: ['online_delivery'] },
|
|
] },
|
|
{ id: 2, name: 'Ορεκτικά', products: [{ id: 5, name: 'Σουβλάκι', names: { el: 'Σουβλάκι', en: 'Pork skewer' }, prices: { qr_table: 3.5 }, sold_at: ['qr_table'] }] },
|
|
{ id: 3, name: 'Empty', products: [{ id: 7, name: 'x', prices: {}, sold_at: ['online_takeaway'] }] },
|
|
] }
|
|
|
|
test('the menu in the guest\'s language, the QR menu\'s prices, only what it sells', () => {
|
|
const en = menuFor(data, 'en')
|
|
assert.deepEqual(en.map(c => c.name), ['Grill', 'Ορεκτικά']) // a category without a translation: as typed
|
|
const souv = en[0].products[0]
|
|
assert.equal(souv.name, 'Pork skewer')
|
|
assert.equal(souv.description, 'With pitta')
|
|
assert.equal(souv.price, 3.5)
|
|
assert.equal(en[0].products.length, 2) // the delivery-only dish isn't here
|
|
assert.equal(en[0].products[1].available, false)
|
|
assert.equal(en[1].products[0].repeat, true) // listed twice: search shows it once
|
|
assert.equal(menuFor(data, 'el')[0].products[0].name, 'Σουβλάκι')
|
|
})
|
|
|
|
test('allergens: contains first, then may contain; older array data still read', () => {
|
|
assert.deepEqual(allergenList({ sesame: 'may_contain', milk: 'contains', pollen: 'contains' }), [{ key: 'milk', level: 'contains' }, { key: 'sesame', level: 'may_contain' }])
|
|
assert.deepEqual(allergenList(['gluten', 'dairy']), [{ key: 'gluten', level: 'contains' }])
|
|
assert.deepEqual(allergenList(null), [])
|
|
})
|
|
|
|
test('search: either language, no accents, each dish once', () => {
|
|
const en = menuFor(data, 'en')
|
|
assert.deepEqual(search(en, 'σουβλακι').map(p => p.id), ['5'])
|
|
assert.deepEqual(search(en, 'pork').map(p => p.id), ['5'])
|
|
assert.deepEqual(search(en, 'pitta').map(p => p.id), ['5'])
|
|
assert.deepEqual(search(en, ''), [])
|
|
assert.equal(fold('Σουβλάκι'), 'σουβλακι')
|
|
})
|
|
|
|
test('the cart, its total and the order the cloud takes', () => {
|
|
const en = menuFor(data, 'en')
|
|
let cart = setQty({}, '5', 2)
|
|
cart = setQty(cart, '6', 1) // sold out: left out
|
|
cart = setQty(cart, '99', 1) // not on the menu any more
|
|
const lines = cartLines(cart, en)
|
|
assert.deepEqual(lines.map(l => [l.product.id, l.qty, l.total]), [['5', 2, 7]])
|
|
assert.equal(cartTotal(lines), 7)
|
|
assert.equal(cartCount(lines), 2)
|
|
assert.deepEqual(setQty({ 5: 1 }, '5', 0), {})
|
|
assert.deepEqual(orderBody(lines, { tableToken: 'tok', name: ' ', tableLabel: '5', tableWord: 'Table' }), {
|
|
order_type: 'dine_in', table_token: 'tok', customer_name: 'Table 5', items: [{ product_id: 5, quantity: 2, unit_price: 3.5 }], total: 7,
|
|
})
|
|
assert.equal(refusalKey('order.price_changed'), 'refused.changed')
|
|
assert.equal(refusalKey('order.table_invalid'), 'tableInvalid')
|
|
assert.equal(refusalKey('nope'), 'refused.failed')
|
|
})
|