Files
xenia-pos-local/terminal/tests/pos.test.mjs
T
bonaminandClaude Opus 5.5 3eeafb565a feat(backend,waiter,terminal,manager): split the bill by amount
Owner 2026-11-23 (plan 6F.6). Venue setting payments.split_amount (off | two_equal | two_custom |
multiple_equal, each level with the ones before; default off) and payments.split_max_parts (10);
key ordering.payments.split_amount (everyone who takes payments; Alembic 0050 grants it).
POST /api/orders/{id}/split-bills settles the chosen unpaid lines (status 'split': sold, no shift,
no payment method) and adds part lines (system product split_part, "Split bill 1/3", printed and
served; equal parts with the remainder on the last, or two parts with part 1 typed). Parts are paid
like any line (cash, card, tab). POST .../split-bills/{bill}/undo while no part is paid or tabbed.
Counting (services/sales.py): settled lines are the sales, parts never (a refunded part comes off
the settled lines by share); drawers and payment methods count the parts. Older reports use
split_bills.sold_filter / counts_as_sale. Locks: parts can't be cancelled, moved, combined or
re-priced; settled lines are no longer active; closing or cancelling an order with unpaid parts is
refused. Fiscal: one receipt with the settled lines when the last part is paid, with each method's
amount (txt driver: CD per card amount, then CR for cash).
Waiter: Pay -> "Split the amount" (pre-filled from the customers), the split bill above the lines
with Undo. Counter: "By amount" in the payment (quick sale split and paid at once, Q-4; a table's
lines split then paid). Manager: the pay dialog splits, the order shows its split bills, Settings ->
Operation card. Shared rules in @xenia/ordering/split.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 10:57:54 +02:00

248 lines
16 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 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('a split bill at the counter: parts are lines, settled lines add up to nothing (6F.6)', async () => {
const { orderLines, orderSums, partPayments, toPay } = await import('../src/apps/pos/logic/tables.js')
const order = { items: [
{ id: 1, product_name: 'Burger', quantity: 1, unit_price: 10, status: 'split', split_bill_id: 4 },
{ id: 2, product_name: 'Freddo', quantity: 2, unit_price: 2.5, status: 'split', split_bill_id: 4 },
{ id: 3, product_name: 'Split bill 1/2', quantity: 1, unit_price: 4.5, status: 'paid', payment_method: 'cash', split_bill_id: 4 },
{ id: 4, product_name: 'Split bill 2/2', quantity: 1, unit_price: 10.5, status: 'active', split_bill_id: 4 },
] }
const lines = orderLines(order)
assert.deepEqual(lines.map(l => [l.id, l.paid, !!l.settled, !!l.part]), [[4, false, false, true], [1, true, true, false], [2, true, true, false], [3, true, false, true]])
assert.deepEqual(orderSums(lines), { total: 15, paid: 4.5, unpaid: 10.5, unpaidIds: [4] })
assert.deepEqual(toPay(lines, []).map(l => l.id), [4])
assert.deepEqual(partPayments([7, 8, 9], ['cash', 'card', 'cash']), [{ method: 'cash', item_ids: [7, 9] }, { method: 'card', item_ids: [8] }])
})
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)
})
test('online orders: kind, finish word, the price check rows (S.5)', async () => {
const { diffRows, finishWord, kindOf, nextStep } = await import('../src/apps/pos/logic/online.js')
assert.equal(kindOf({ type: 'takeaway' }), 'takeaway')
assert.equal(kindOf({ type: 'dine_in' }), 'dine_in')
assert.equal(kindOf({}), 'dine_in')
assert.deepEqual(nextStep({ status: 'preparing', type: 'takeaway' }), { kind: 'status', to: 'ready' })
assert.equal(finishWord({ type: 'delivery' }), 'delivered')
assert.equal(finishWord({ type: 'takeaway' }), 'collected')
assert.equal(finishWord({ type: 'dine_in' }), 'served')
assert.deepEqual(diffRows(null), [])
assert.deepEqual(diffRows({
lines: [{ name: 'Burger', quantity: 2, cloud: 8, site: 8.5, reason: 'price' }],
fee: { cloud: 2, site: null, reason: 'area' },
}), [
{ key: 'l0', name: 'Burger', quantity: 2, cloud: 8, site: 8.5, reason: 'price' },
{ key: 'fee', name: null, quantity: 1, cloud: 2, site: null, reason: 'fee_area' },
])
})
test('refunds: picking items, quantities, total, method, approval (3.3e)', async () => {
const { byPiece, defaultMethod, needsApproval, pickAll, refundLines, refundTotal, setPickQty, togglePick } = await import('../src/apps/pos/logic/refunds.js')
const coffee = { id: 1, unit_type: 'piece', refundable: 2, unit_total: 3, method: 'cash', mine: true }
const feta = { id: 2, unit_type: 'kg', refundable: 0.25, unit_total: 12, method: 'card', mine: true }
const done = { id: 3, unit_type: 'piece', refundable: 0, unit_total: 5, method: 'card', mine: false }
const items = [coffee, feta, done]
assert.equal(byPiece(coffee), true)
assert.equal(byPiece(feta), false)
let pick = togglePick({}, coffee)
assert.deepEqual(pick, { 1: 2 })
pick = setPickQty(pick, coffee, 1)
assert.deepEqual(refundLines(items, pick), [{ item_id: 1, quantity: 1 }])
assert.equal(refundTotal(items, pick), 3)
assert.deepEqual(setPickQty(pick, coffee, 9), { 1: 2 }, 'not more than what is left')
assert.deepEqual(setPickQty(pick, coffee, 0), {}, '0 removes it')
assert.deepEqual(togglePick(pick, coffee), {}, 'a second tap removes it')
assert.deepEqual(togglePick({}, done), {}, 'nothing left to refund')
assert.deepEqual(pickAll(items), { 1: 2, 2: 0.25 })
assert.equal(refundTotal(items, pickAll(items)), 9)
assert.equal(defaultMethod(items, { 2: 0.25 }), 'card')
assert.equal(defaultMethod(items, pickAll(items)), 'cash')
assert.equal(needsApproval(items, pickAll(items)), false)
assert.equal(needsApproval([{ ...coffee, mine: false }], { 1: 1 }), true)
})
test('sales points at the counter: what is sold and at which price (phase 3S)', async () => {
const { forSalesPoint, salesPointFor } = await import('../src/apps/pos/logic/cart.js')
assert.equal(salesPointFor('takeaway'), 'in_store_takeaway')
assert.equal(salesPointFor('here'), 'in_store')
const cola = { id: 1, price: 3.5, prices: { in_store: 3.5, in_store_takeaway: 2.5 }, sold_at: ['in_store', 'in_store_takeaway'] }
const soup = { id: 2, price: 6, prices: { in_store: 6, in_store_takeaway: 6 }, sold_at: ['in_store'] }
const old = { id: 3, price: 4 }
assert.deepEqual(forSalesPoint([cola, soup, old], 'in_store_takeaway').map(p => [p.id, p.price]), [[1, 2.5], [3, 4]])
assert.deepEqual(forSalesPoint([cola, soup, old], 'in_store').map(p => [p.id, p.price]), [[1, 3.5], [2, 6], [3, 4]])
})
test('table bookings at the counter (S.6)', async () => {
const { bookingSteps, findBooking, foodState, freeTables, whenLabel } = await import('../src/apps/pos/logic/bookings.js')
const data = { requested: [{ id: 1, status: 'requested', lines: [] }], upcoming: [{ id: 2, status: 'pending', lines: [{}], order_id: null }] }
assert.equal(findBooking(data, 2).id, 2)
assert.equal(findBooking(data, 9), null)
assert.deepEqual(bookingSteps(data.requested[0]), ['confirm', 'decline'])
assert.deepEqual(bookingSteps(data.upcoming[0]), ['arrive', 'cancel', 'no_show'])
assert.deepEqual(bookingSteps({ status: 'arrived' }), [])
assert.equal(foodState(data.requested[0]), 'none')
assert.equal(foodState(data.upcoming[0]), 'waiting')
assert.equal(foodState({ lines: [{}], order_id: 5 }), 'kitchen')
assert.deepEqual(freeTables([{ id: 1, order: null }, { id: 2, order: { id: 9 } }]).map(t => t.id), [1])
const fmt = { formatDate: d => new Date(d).toISOString().slice(0, 10), formatDateTime: () => 'other day', formatTime: () => '21:30' }
const t = (k, p) => `${k}:${p?.time ?? ''}`
assert.equal(whenLabel(new Date().toISOString(), fmt, t), 'pos.bookings.today:21:30')
assert.equal(whenLabel('2020-01-01T10:00:00Z', fmt, t), 'other day')
})
test('caller ID: only fresh calls pop up (3.5)', async () => {
const { isFresh, FRESH_MS } = await import('../src/apps/pos/logic/calls.js')
const now = Date.parse('2026-11-17T10:00:00Z')
assert.equal(isFresh({ at: '2026-11-17T09:59:30Z' }, now), true)
assert.equal(isFresh({ at: new Date(now - FRESH_MS - 1).toISOString() }, now), false)
assert.equal(isFresh({}, now), true)
assert.equal(isFresh(null, now), false)
})