// The menu tablet's logic (plan step 5.7a). import assert from 'node:assert/strict' import { test } from 'node:test' import { allProducts, fold, idle, languagesOf, localize, matches, search, sections } from '../src/apps/menu-tablet/logic.js' const data = { categories: [{ id: 1, name: 'Grill', depth: 0, products: [2, 1] }, { id: 3, name: 'Pita', depth: 1, products: [1] }, { id: 4, name: 'Empty', depth: 0, products: [9] }], products: { 1: { id: 1, name: 'Σουβλάκι χοιρινό', tags: [] }, 2: { id: 2, name: 'Μπριζόλα', tags: ['gluten-free'] }, 5: { id: 5, name: 'Αναψυκτικό', tags: [] } }, } test('search finds Greek names with or without accents, in Latin letters, and by tag', () => { assert.equal(fold('Σουβλάκι'), 'σουβλακι') assert.ok(matches(data.products[1], 'σουβλακι')) assert.ok(matches(data.products[1], 'souvl')) assert.ok(matches(data.products[2], 'gluten')) assert.ok(!matches(data.products[2], 'souvl')) assert.deepEqual(search(data, 'ι').map(p => p.id).slice(0, 1).length, 1) assert.deepEqual(search(data, 'mpriz').map(p => p.id), [2]) assert.deepEqual(search(data, ' '), []) }) test('sections: grouped by category (empty ones left out) or one A–Z list', () => { assert.deepEqual(sections(data, true).map(s => [s.name, s.products.map(p => p.id)]), [['Grill', [2, 1]], ['Pita', [1]]]) assert.deepEqual(sections(data, false)[0].products.map(p => p.id), allProducts(data).map(p => p.id)) assert.deepEqual(allProducts(data).map(p => p.name), ['Αναψυκτικό', 'Μπριζόλα', 'Σουβλάκι χοιρινό']) assert.deepEqual(sections(null, true), []) }) test('back to the start after a while; the languages a guest can pick', () => { assert.equal(idle(0, 60, 59_000), false) assert.equal(idle(0, 60, 60_000), true) assert.equal(idle(0, 0, 9e9), false) assert.deepEqual(languagesOf({ languages: 'both' }), ['el', 'en']) assert.deepEqual(languagesOf({ languages: 'en' }), ['en']) assert.deepEqual(languagesOf(undefined), ['el', 'en']) }) test('the menu in the language of the tablet (6.4): names switch, the base name still finds it', () => { const data = { products: { 1: { id: 1, name: 'Σουβλάκι', names: { el: 'Σουβλάκι', en: 'Pork skewer' }, description: 'Χοιρινό', descriptions: { el: 'Χοιρινό', en: 'Pork' } }, 2: { id: 2, name: 'Μπύρα' } }, categories: [{ id: 5, name: 'Ψητά', names: { el: 'Ψητά', en: 'Grill' }, products: [1, 2] }] } const en = localize(data, 'en') assert.equal(en.products[1].name, 'Pork skewer') assert.equal(en.products[1].description, 'Pork') assert.equal(en.products[2].name, 'Μπύρα') // not translated: as typed assert.equal(en.categories[0].name, 'Grill') assert.ok(matches(en.products[1], 'souvl') && matches(en.products[1], 'pork')) assert.equal(localize(data, 'el').products[1].name, 'Σουβλάκι') }) test('the tablet\'s cart (6.6): add, join the same choices, more / less, count', async () => { const { addToCart, cartCount, cartLines, changeQty } = await import('../src/apps/menu-tablet/ordering.js') let cart = addToCart([], { line: { product_id: 5, quantity: 1, quick: [{ id: 9, qty: 1 }] }, name: 'Pork skewer' }) cart = addToCart(cart, { line: { product_id: 5, quantity: 2, quick: [{ id: 9, qty: 1 }] }, name: 'Pork skewer' }) cart = addToCart(cart, { line: { product_id: 6, quantity: 1 }, name: 'Beer' }) assert.equal(cart.length, 2) assert.equal(cart[0].line.quantity, 3) assert.equal(cartCount(cart), 4) assert.deepEqual(cartLines(changeQty(cart, 1, -1)), [{ product_id: 5, quantity: 3, quick: [{ id: 9, qty: 1 }] }]) assert.equal(changeQty(cart, 0, 1)[0].line.quantity, 4) })