// The counter POS's menu and cart logic (plan step 3.3a). No prices here: they come from the // backend; this checks what's chosen and how the cart groups it. import assert from 'node:assert/strict' import { test } from 'node:test' import { addLine, categoriesWithProducts, defaultSelection, fold, lineKey, missingChoices, needsChoices, pickChoice, pickChoiceSub, pickOptionSub, pickShared, productsIn, searchProducts, setLineQty, setQuick, toLine, toggleOption, toggleRemoved, } from '../src/apps/pos/logic/cart.js' const burger = { id: 1, name: 'Burger', categories: [{ category_id: 10, sort_order: 2 }, { category_id: 20, sort_order: 0 }], quick: [{ id: 5, name: 'Cheese', allow_multiple: true }], options: [{ id: 7, name: 'Sauce', multi_select: false, subs: [{ name: 'BBQ', is_default: true }, { name: 'Mayo' }] }, { id: 8, name: 'Toppings', multi_select: true, subs: [{ name: 'Egg' }, { name: 'Bacon' }] }], prefs: [{ id: 3, name: 'Cooking', multi: false, default_choice_id: null, shared: { name: 'Side', subs: [{ name: 'Fries', is_default: true }, { name: 'Salad' }] }, choices: [{ id: 30, name: 'Rare', subs: [] }, { id: 31, name: 'Well done', disables_shared: true, subs: [] }, { id: 32, name: 'Medium', subs: [{ name: 'pink' }, { name: 'not pink', is_default: true }] }] }], ingredients: [{ id: 9, name: 'Onion' }], } const coffee = { id: 2, name: 'Φρέντο Εσπρέσο', categories: [{ category_id: 20, sort_order: 1 }], quick: [], options: [], prefs: [], ingredients: [] } test('search ignores accents, case and word order; finds a product once', () => { assert.equal(fold('Φρέντο'), 'φρεντο') assert.deepEqual(searchProducts([burger, coffee], 'εσπρεσο φρεντ').map(p => p.id), [2]) assert.deepEqual(searchProducts([burger, coffee], 'bur').map(p => p.id), [1]) assert.deepEqual(searchProducts([burger, coffee], ' '), []) }) test('categories: a product listed in several shows in each, in that category\'s order', () => { assert.deepEqual(productsIn([burger, coffee], 20).map(p => p.id), [1, 2]) assert.deepEqual(productsIn([burger, coffee], 10).map(p => p.id), [1]) assert.equal(productsIn([burger, coffee], null).length, 2) assert.deepEqual(categoriesWithProducts([{ id: 10 }, { id: 20 }, { id: 30 }], [burger, coffee]).map(c => c.id), [10, 20]) }) test('choices: a preference must be picked; subs and shared subs come with their defaults', () => { assert.equal(needsChoices(burger), true) assert.equal(needsChoices(coffee), false) let sel = defaultSelection(burger) assert.deepEqual(missingChoices(burger, sel), [{ kind: 'pref', name: 'Cooking' }]) sel = pickChoice(burger, sel, 3, 30) assert.deepEqual(sel.prefs[3], { choices: [{ id: 30, qty: 1, sub: null }], shared: 'Fries' }) assert.deepEqual(missingChoices(burger, sel), []) sel = pickShared(sel, 3, 'Salad') sel = pickChoice(burger, sel, 3, 31) // well done: no side assert.equal(sel.prefs[3].shared, null) sel = pickChoice(burger, sel, 3, 32) // medium: its default sub, side comes back assert.deepEqual(sel.prefs[3], { choices: [{ id: 32, qty: 1, sub: 'not pink' }], shared: 'Fries' }) sel = pickChoiceSub(sel, 3, 'pink') assert.equal(sel.prefs[3].choices[0].sub, 'pink') }) test('extras, add-ons, without, note → the line the backend prices', () => { let sel = pickChoice(burger, defaultSelection(burger), 3, 30) sel = toggleOption(burger, sel, 7) // Sauce with BBQ sel = pickOptionSub(burger, sel, 7, 'Mayo') // single: replaces sel = toggleOption(burger, sel, 8) // the first sub comes pre-selected (as on the waiter app) assert.deepEqual(sel.options[8].subs, ['Egg']) sel = pickOptionSub(burger, sel, 8, 'Egg') // multi: tapping it again takes it off… assert.deepEqual(missingChoices(burger, sel), [{ kind: 'option_sub', name: 'Toppings' }]) sel = pickOptionSub(burger, sel, 8, 'Egg') sel = pickOptionSub(burger, sel, 8, 'Bacon') // …and adds the others sel = setQuick(sel, 5, 2) sel = toggleRemoved(sel, 9) sel = { ...sel, notes: ' no salt ' } assert.deepEqual(toLine(burger, sel, 1), { product_id: 1, quantity: 1, quick: [{ id: 5, qty: 2 }], options: [{ id: 7, qty: 1, subs: ['Mayo'] }, { id: 8, qty: 1, subs: ['Egg', 'Bacon'] }], prefs: [{ set_id: 3, choices: [{ id: 30, qty: 1, sub: null }], shared: 'Fries' }], removed: [9], notes: 'no salt', }) assert.deepEqual(toLine(coffee, defaultSelection(coffee), 2), { product_id: 2, quantity: 2 }) assert.deepEqual(setQuick(sel, 5, 0).quick, {}) }) test('cart: the same choices add up, different choices are separate lines; 0 removes', () => { const a = { product_id: 2, quantity: 1 } const b = { product_id: 1, quantity: 1, prefs: [{ set_id: 3, choices: [{ id: 30, qty: 1, sub: null }], shared: 'Fries' }] } const c = { product_id: 1, quantity: 1, prefs: [{ set_id: 3, choices: [{ id: 31, qty: 1, sub: null }], shared: null }] } let cart = addLine(addLine(addLine([], a), b), a) assert.deepEqual(cart.map(l => [l.product_id, l.quantity]), [[2, 2], [1, 1]]) cart = addLine(cart, c) assert.equal(cart.length, 3) assert.equal(lineKey({ ...b, quantity: 5 }), lineKey(b)) assert.notEqual(lineKey(b), lineKey(c)) assert.deepEqual(setLineQty(cart, 0, 0).map(l => l.product_id), [1, 1]) assert.equal(setLineQty(cart, 0, 0.30000000000000004)[0].quantity, 0.3) }) test('paying: quick amounts, change, split parts (3.3b)', async () => { const { changeFor, quickAmounts, splitPayments } = await import('../src/apps/pos/logic/cart.js') assert.deepEqual(quickAmounts(12.2), [12.2, 15, 20, 50, 100]) assert.deepEqual(quickAmounts(20), [20, 25, 30, 40, 50]) assert.equal(changeFor(20, 16), 4) assert.equal(changeFor(15, 16), null) assert.equal(changeFor(16, 16), 0) assert.deepEqual(splitPayments(['card', 'cash', 'cash'], [10, 3, 2.5]), { cash: 5.5, card: 10, payments: [{ method: 'card', lines: [0] }, { method: 'cash', lines: [1, 2] }] }) }) test('tables at the counter: state, zones, order lines, what Pay takes (3.3c)', async () => { const { itemPayments, optionNames, orderLines, orderSums, qtyLabel, tableState, tablesIn, toPay, zoneCounts } = await import('../src/apps/pos/logic/tables.js') const free = { id: 1, group_id: 7, order: null } const mine = { id: 2, group_id: 7, order: { items: 2, unpaid: 6, total: 6, mine: true } } const theirs = { id: 3, group_id: 8, order: { items: 1, unpaid: 3, total: 3, mine: false } } const paid = { id: 4, group_id: 8, order: { items: 1, unpaid: 0, total: 3, mine: false } } assert.deepEqual([free, mine, theirs, paid].map(tableState), ['free', 'mine', 'open', 'paid']) assert.deepEqual(tablesIn([free, mine, theirs], 8).map(t => t.id), [3]) assert.deepEqual(zoneCounts([free, mine, theirs, paid]), { all: { total: 4, busy: 3 }, 7: { total: 2, busy: 1 }, 8: { total: 2, busy: 2 } }) assert.deepEqual(optionNames('[{"name":"Rare"},{"name":"Fries"}]'), ['Rare', 'Fries']) assert.deepEqual(optionNames('not json'), []) const order = { items: [ { id: 10, product_name: 'Freddo', quantity: 2, unit_price: 3, price_adjustment: 0, status: 'paid', payment_method: 'cash' }, { id: 11, product_name: 'Burger', quantity: 1, unit_price: 10, price_adjustment: -1, status: 'active', selected_options: '[{"name":"Rare"}]' }, { id: 12, product_name: 'Old', quantity: 1, unit_price: 5, status: 'cancelled' }, { id: 13, product_name: 'Wine', quantity: 0.5, unit_type: 'liter', unit_price: 12, status: 'ready', kds_status: 'done' }, ] } const lines = orderLines(order) assert.deepEqual(lines.map(l => [l.id, l.total, l.paid]), [[11, 9, false], [13, 6, false], [10, 6, true]]) assert.deepEqual(lines[0].options, ['Rare']) assert.deepEqual(orderSums(lines), { total: 21, paid: 6, unpaid: 15, unpaidIds: [11, 13] }) assert.deepEqual(toPay(lines, []).map(l => l.id), [11, 13]) // nothing selected → everything unpaid assert.deepEqual(toPay(lines, [13, 10]).map(l => l.id), [13]) // a paid line can't be chosen const rows = toPay(lines, []).map(l => ({ id: l.id, total: l.total })) assert.deepEqual(itemPayments([{ method: 'card', lines: [1] }, { method: 'cash', lines: [0] }], rows), [{ method: 'card', item_ids: [13] }, { method: 'cash', item_ids: [11] }]) assert.equal(qtyLabel(2, 'piece'), '2×') assert.equal(qtyLabel(0.5, 'liter'), '0.5 L') }) test('online orders: the next step and finding an order (3.3d)', async () => { const { findOnline, minutesSince, nextStep } = await import('../src/apps/pos/logic/online.js') assert.deepEqual(nextStep({ status: 'pending_acceptance' }), { kind: 'answer' }) assert.deepEqual(nextStep({ status: 'accepted' }), { kind: 'status', to: 'preparing' }) assert.deepEqual(nextStep({ status: 'preparing', type: 'delivery' }), { kind: 'status', to: 'out_for_delivery' }) assert.deepEqual(nextStep({ status: 'preparing', type: 'dine_in' }), { kind: 'status', to: 'ready' }) assert.deepEqual(nextStep({ status: 'out_for_delivery', unpaid: 22 }), { kind: 'finish', pay: true }) assert.deepEqual(nextStep({ status: 'ready', unpaid: 0 }), { kind: 'finish', pay: false }) assert.equal(nextStep({ status: 'delivered' }), null) assert.equal(nextStep({ status: 'rejected' }), null) const board = { incoming: [{ id: 1 }], in_progress: [{ id: 2 }], finished: [{ id: 3 }] } assert.equal(findOnline(board, 2).id, 2) assert.equal(findOnline(board, 3).id, 3) assert.equal(findOnline(board, 9), null) assert.equal(findOnline(null, 1), null) const at = Date.parse('2026-11-15T10:00:00Z') assert.equal(minutesSince('2026-11-15T09:57:30Z', at), 2) assert.equal(minutesSince('2026-11-15T10:01:00Z', at), 0) })