// 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) })