Files
xenia-pos-cloud/connect_frontend/order-app/tests/logic.test.mjs
T
bonaminandClaude Opus 5.5 9f9b882806 feat(menu-app): the QR menu and the order app in the guest's language (contract C-2 names, platform-v2 step 6.4)
The site now sends names / descriptions {el, en} per product, quick option and category
(falling back to the text as typed). The menu app maps them onto its en / gr switch (byLang);
the order app localises its menu by the guest's language (localizeMenu, tested). The menu app
also reads 6.3's allergens object (what a product contains).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 10:46:59 +02:00

99 lines
6.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// The online ordering app's logic (plan phase 3S, step S.4).
import assert from 'node:assert/strict'
import { test } from 'node:test'
import {
localizeMenu, productName,
addToCart, cartCount, cartLines, checkoutProblems, menuFor, orderBody, priceFor, refusal, setQty, slotDays, trackSteps,
} from '../src/logic.js'
const cola = { id: 1, name: 'Cola', sold_at: ['qr_table', 'online_takeaway', 'online_delivery'], prices: { qr_table: 3.5, online_takeaway: 2.5, online_delivery: 2.8 },
quick_options: [{ id: 7, name: 'Ice', price: 0.2 }] }
const pie = { id: 2, name: 'Pie', digital_name: 'Spinach pie', sold_at: ['online_takeaway'], prices: { online_takeaway: 4 }, digital_available: false }
const old = { id: 3, name: 'Old', digital_price: 5 }
const cats = [{ id: 10, name: 'Drinks', products: [cola] }, { id: 11, name: 'Food', products: [pie, old] }, { id: 12, name: 'Also', products: [cola] }]
test('the menu and prices per service', () => {
assert.equal(priceFor(cola, 'takeaway'), 2.5)
assert.equal(priceFor(cola, 'delivery'), 2.8)
assert.equal(priceFor(pie, 'delivery'), null)
assert.equal(priceFor(old, 'delivery'), 5)
const m = menuFor(cats, 'delivery')
assert.deepEqual(m.map(c => [c.name, c.products.map(p => [p.id, p.price, p.repeat])]), [['Drinks', [[1, 2.8, false]]], ['Food', [[3, 5, false]]], ['Also', [[1, 2.8, true]]]])
assert.equal(menuFor(cats, 'takeaway')[1].products[0].available, false)
})
test('the cart', () => {
let cart = addToCart([], cola)
cart = addToCart(cart, cola)
cart = addToCart(cart, cola, [7])
assert.deepEqual(cart.map(l => [l.key, l.qty]), [['1', 2], ['1:7', 1]])
assert.equal(cartCount(cart), 3)
const lines = cartLines(cart, [cola, pie], 'takeaway')
assert.deepEqual(lines.map(l => [l.unit, l.total, l.optionNames]), [[2.5, 5, []], [2.7, 2.7, ['Ice']]])
assert.deepEqual(cartLines(addToCart(cart, pie), [cola, pie], 'delivery').length, 2) // the pie isn't delivered
assert.deepEqual(setQty(cart, '1', 0).map(l => l.key), ['1:7'])
assert.equal(setQty(cart, '1', 500)[0].qty, 99)
})
test('checkout checks and the order body', () => {
const form = { name: ' Maria ', phone: '690 000 0000', address: '', notes: '', where: 'area', areaId: '', postcode: '', when: 'asap' }
assert.deepEqual(checkoutProblems(form, 'takeaway'), [])
assert.deepEqual(checkoutProblems(form, 'delivery', { areas: [{ id: 1 }] }), ['address', 'area'])
assert.deepEqual(checkoutProblems({ ...form, name: '', phone: 'abc' }, 'takeaway'), ['name', 'phone'])
assert.deepEqual(checkoutProblems(form, 'takeaway', { asap: false }), ['when'])
assert.deepEqual(checkoutProblems({ ...form, when: 'X' }, 'takeaway', { slots: ['X'] }), [])
const lines = cartLines(addToCart([], cola), [cola], 'delivery')
const body = orderBody({ ...form, address: 'Main sq 3', areaId: '2', notes: ' bell ' }, 'delivery', lines, { total: 4.8 })
assert.deepEqual(body, {
order_type: 'delivery', customer_name: 'Maria', customer_phone: '690 000 0000', customer_address: 'Main sq 3', customer_notes: 'bell',
area_id: 2, postcode: null, requested_for: null, items: [{ product_id: 1, quantity: 1, options: [], unit_price: 2.8 }], total: 4.8,
})
assert.equal(orderBody({ ...form, when: '2026-11-16T10:00:00+00:00' }, 'takeaway', lines, null).requested_for, '2026-11-16T10:00:00+00:00')
})
test('slots by day in the venue time zone, tracking steps, refusals', () => {
const days = slotDays(['2026-11-15T19:45:00+00:00', '2026-11-15T22:00:00+00:00'], 'Europe/Athens')
assert.deepEqual(days, [{ day: '2026-11-15', times: [{ iso: '2026-11-15T19:45:00+00:00', label: '21:45' }] },
{ day: '2026-11-16', times: [{ iso: '2026-11-15T22:00:00+00:00', label: '00:00' }] }])
assert.deepEqual(trackSteps('delivery', 'preparing'), { steps: ['pending_acceptance', 'accepted', 'preparing', 'out_for_delivery', 'delivered'], at: 2, done: false })
assert.equal(trackSteps('takeaway', 'ready').at, 3)
assert.deepEqual(trackSteps('takeaway', 'rejected'), { ...trackSteps('takeaway', 'rejected'), at: -1, done: true })
assert.deepEqual(refusal({ response: { status: 409, data: { detail: { code: 'order.minimum_not_met', params: { minimum: 10 } } } } }), ['minimum_not_met', { minimum: 10 }])
assert.deepEqual(refusal({ response: { status: 429, data: {} } }), ['rate', {}])
assert.deepEqual(refusal(new Error('x')), ['failed', {}])
})
test('booking a table, with or without food (S.6)', async () => {
const { bookingBody, bookingProblems } = await import('../src/logic.js')
const form = { name: 'Eleni', phone: '6977777777', notes: '' }
const booking = { party: 4, when: '2026-11-16T18:00:00+00:00' }
assert.deepEqual(bookingProblems(form, booking, 8), [])
assert.deepEqual(bookingProblems({ ...form, name: ' ' }, { party: 9, when: null }, 8), ['name', 'party', 'when'])
assert.deepEqual(bookingBody(form, booking, [], null), {
order_type: 'reservation', customer_name: 'Eleni', customer_phone: '6977777777', customer_notes: null,
party_size: 4, requested_for: booking.when, items: [], total: undefined,
})
const food = bookingBody(form, booking, [{ productId: 1, qty: 2, options: [], unit: 2.6 }], { total: 5.2 })
assert.equal(food.order_type, 'preorder')
assert.deepEqual(food.items, [{ product_id: 1, quantity: 2, options: [], unit_price: 2.6 }])
assert.equal(food.total, 5.2)
assert.deepEqual(trackSteps('reservation', 'accepted'), { steps: ['pending_acceptance', 'accepted', 'delivered'], at: 1, done: false })
assert.equal(trackSteps('preorder', 'cancelled').done, true)
assert.equal(trackSteps('preorder', 'cancelled').at, -1)
assert.equal(priceFor({ sold_at: ['online_preorder'], prices: { online_preorder: 2.6 } }, 'preorder'), 2.6)
})
test('the menu in the guest’s language (6.4): names, descriptions, extras; old menus untouched', () => {
const cats = [{ id: 1, name: 'Ψητά', names: { el: 'Ψητά', en: 'Grill' }, products: [
{ id: 5, name: 'Σουβλάκι', digital_name: 'Σουβλάκι χοιρινό', names: { el: 'Σουβλάκι χοιρινό', en: 'Pork skewer' }, descriptions: { el: 'Με πίτα', en: 'With pitta' },
quick_options: [{ id: 9, name: 'Πίτα', names: { el: 'Πίτα', en: 'Pitta' } }] },
{ id: 6, name: 'Old', digital_name: 'Old one' }] }]
const en = localizeMenu(cats, 'en')
assert.equal(en[0].name, 'Grill')
assert.equal(productName(en[0].products[0]), 'Pork skewer')
assert.equal(en[0].products[0].digital_description, 'With pitta')
assert.equal(en[0].products[0].quick_options[0].name, 'Pitta')
assert.equal(productName(en[0].products[1]), 'Old one')
assert.equal(productName(localizeMenu(cats, 'el')[0].products[0]), 'Σουβλάκι χοιρινό')
})