// The overview's arithmetic and the online orders' steps (plan step 6.2). import { test } from 'node:test' import assert from 'node:assert/strict' import { bookingBody, cashCheck, dayKpis, dayOrders, durationParts, hourBuckets, initials, itemKind, itemShown, messageAcks, messageTargets, notesPreview, orderPairs, orderTotal, paymentTotals, quickAmounts, shiftCounts, shiftMoney, tableCards, tableName, tablesByZone, toneOf, TONES, venueWallClock, } from '../src/pages/overview/overviewLogic.js' import { onlineKind, onlineNextStep } from '../src/pages/online/onlineOrdersLogic.js' const item = (o = {}) => ({ status: 'active', unit_price: 3, price_adjustment: 0, quantity: 2, printed: true, ...o }) test('an order total leaves out cancelled lines and counts by-hand changes', () => { assert.equal(orderTotal([item(), item({ price_adjustment: -1 }), item({ status: 'cancelled' })]), 6 + 4) assert.equal(orderTotal(), 0) }) test('tables get their live order, state and waiting prints; KPIs add up', () => { const tables = [{ id: 1, number: 1 }, { id: 2, label: 'Bar' }, { id: 3, number: 3 }] const orders = [ { id: 10, table_id: 1, status: 'open', opened_at: '2026-11-18T09:00:00Z', items: [item({ printed: false })] }, { id: 11, table_id: 2, status: 'partially_paid', opened_at: '2026-11-18T11:00:00Z', items: [item({ paid_by: 4 })] }, { id: 12, table_id: 3, status: 'closed', opened_at: '2026-11-18T11:00:00Z', items: [item()] }, ] const cards = tableCards(tables, orders) assert.deepEqual(cards.map(c => c.state), ['open', 'partially_paid', 'free']) assert.deepEqual(cards.map(c => c.pendingPrint), [true, false, false]) assert.equal(tableName(tables[0]), 'T1') assert.equal(tableName(tables[1]), 'Bar') const today = dayOrders(orders, '2026-11-18T10:00:00Z') assert.deepEqual(today.map(o => o.id), [11, 12]) assert.equal(dayOrders(orders, null).length, 3) const k = dayKpis({ cards, orders: today, shifts: [{ total_collected: 5 }, { total_collected: 7.5 }] }) assert.equal(k.revenue, 12) assert.equal(k.avgTicket, 12) // one order with a paid line assert.equal(k.occupied, 2) assert.equal(k.free, 1) assert.equal(Math.round(k.occupiedPct), 67) assert.equal(k.collected, 12.5) }) test('money per hour covers the usual hours and any hour with money', () => { const hourOf = iso => new Date(iso).getUTCHours() const b = hourBuckets(orderPairs([ { opened_at: '2026-11-18T08:30:00Z', items: [item()] }, { opened_at: '2026-11-18T12:10:00Z', items: [item(), item()] }, ]), hourOf) assert.equal(b[0].hour, 8) assert.equal(b.at(-1).hour, 23) assert.equal(b.find(x => x.hour === 12).value, 12) assert.equal(b.find(x => x.hour === 9).value, 0) // a shift's bars: only the hours with money assert.deepEqual(hourBuckets([['2026-11-18T19:00:00Z', 4]], hourOf, { from: 24, to: -1 }), [{ hour: 19, value: 4 }]) }) test('a shift\'s money, counts and its lines seen from the waiter', () => { assert.deepEqual(shiftMoney({ starting_cash: 20, cash_sales: 30, card_sales: 10 }), { cash: 30, card: 10, other: 0, tabCash: 0, tabCard: 0, tabOther: 0, tab: 0, refunded: 0, start: 20, total: 40, expected: 50, cashPct: 75, cardPct: 25 }) // 6.13c: tab cash is in the drawer, tab card isn't; "other" (on the house) in no drawer; the backend's figure wins const m = shiftMoney({ starting_cash: 10, cash_sales: 8, other_sales: 4, tab_cash: 5, tab_card: 3, cash_refunds: 2 }) assert.deepEqual([m.expected, m.tab, m.total], [21, 8, 20]) assert.equal(shiftMoney({ starting_cash: 10, cash_sales: 8, net_to_deliver: 17 }).expected, 17) assert.equal(shiftMoney({}).cardPct, 0) const summary = { orders: [ { table_id: 1, items: [{ status: 'paid', quantity: 2 }, { status: 'cancelled', quantity: 5 }] }, { table_id: 1, items: [{ status: 'paid', quantity: 1 }] }, { table_id: null, items: [] }, ] } assert.deepEqual(shiftCounts(summary), { items: 3, tables: 1, orders: 3 }) const me = 7 assert.equal(itemKind({ status: 'paid', added_by_id: me, paid_by_id: me }, me), 'both') assert.equal(itemKind({ status: 'paid', added_by_id: 2, paid_by_id: me }, me), 'paid') assert.equal(itemKind({ status: 'paid', added_by_id: me, paid_by_id: 2 }, me), 'ordered') assert.equal(itemKind({ status: 'active', added_by_id: me }, me), 'unpaid') assert.equal(itemKind({ status: 'cancelled' }, me), 'cancelled') assert.equal(itemKind({ status: 'active', added_by_id: 2 }, me), 'anomaly') const card = { status: 'paid', added_by_id: me, paid_by_id: me, payment_method: 'card' } assert.equal(itemShown(card, me, 'both', 'card'), true) assert.equal(itemShown(card, me, 'both', 'cash'), false) assert.equal(itemShown({ ...card, payment_method: null }, me, 'all', 'cash'), true) // no method = cash assert.equal(itemShown({ status: 'cancelled' }, me, 'all', 'card'), true) // cancelled lines ignore the method assert.equal(itemShown(card, me, 'unpaid', 'any'), false) }) test('counting cash: short, over, even (to the cent)', () => { assert.equal(cashCheck(null, 50), null) assert.equal(cashCheck(NaN, 50), null) assert.deepEqual(cashCheck(50, 50), { diff: 0, state: 'even' }) assert.deepEqual(cashCheck(45.5, 50), { diff: -4.5, state: 'short' }) assert.deepEqual(cashCheck(50.1, 50), { diff: 0.1, state: 'over' }) assert.deepEqual(quickAmounts(47.3), [47.3, 40, 50]) assert.deepEqual(quickAmounts(50), [50]) }) test('people: durations, initials, a steady tone', () => { assert.deepEqual(durationParts(0, 95 * 60000), { h: 1, m: 35, mins: 95 }) assert.equal(durationParts(null), null) assert.equal(initials('Maria Papadopoulou'), 'MP') assert.equal(initials('nikos'), 'NI') assert.equal(initials(''), '?') assert.equal(toneOf('Maria'), toneOf('Maria')) assert.ok(TONES.includes(toneOf('Giorgos'))) }) test('the day\'s payments, messages, notes, print retries', () => { assert.deepEqual(paymentTotals([{ cash: 5, card: 2 }, { cash: 1, treat: 3 }]), { cash: 6, card: 2, transfer: 0, treat: 3, other: 0 }) const staff = [{ id: 1, nickname: 'Mary' }, { id: 2, username: 'nick' }] assert.equal(messageTargets({ target_waiter_ids: '[]' }, staff), null) assert.deepEqual(messageTargets({ target_waiter_ids: [1, 9] }, staff), ['Mary', '#9']) assert.deepEqual(messageAcks({ target_waiter_ids: '[]', acked_by: '[1,2]' }, 2), { read: 2, of: 2, all: true }) assert.deepEqual(messageAcks({ target_waiter_ids: [1], acked_by: 'bad json' }, 2), { read: 0, of: 1, all: false }) const notes = [{ id: 1, is_pinned: true }, { id: 2 }, { id: 3 }, { id: 4 }, { id: 5 }] const todos = [{ id: 9, priority: 'high' }, { id: 8, priority: 'high', is_done: true }, { id: 7, priority: 'low' }] assert.deepEqual(notesPreview(notes, todos).map(x => `${x.kind}${x.id}`), ['note1', 'todo9', 'note2', 'note3', 'note4']) }) test('bookings are typed in venue time', () => { // 18 Nov 2026 10:00 UTC is 12:00 in Athens (EET, +2) assert.equal(venueWallClock(new Date('2026-11-18T10:00:00Z'), 'Europe/Athens'), '2026-11-18T12:00') assert.equal(venueWallClock(new Date('2026-07-01T23:30:00Z'), 'Europe/Athens'), '2026-07-02T02:30') assert.deepEqual(bookingBody({ guest_name: ' Eleni ', party_size: '4', phone: ' ', email: '', note: 'window', table_id: 3, reserved_for: '2026-11-18T21:00' }), { guest_name: 'Eleni', party_size: 4, phone: null, email: null, note: 'window', table_id: 3, reserved_for: '2026-11-18T21:00:00' }) const zones = tablesByZone([{ id: 1, group: { name: 'Garden' } }, { id: 2 }, { id: 3, group: { name: 'Garden' } }], 'No zone') assert.deepEqual(zones.map(z => [z.zone, z.tables.map(t => t.id)]), [['Garden', [1, 3]], ['No zone', [2]]]) }) test('online orders move on step by step', () => { const o = (status, type = 'takeaway') => ({ online_status: status, online_order_type: type }) assert.equal(onlineNextStep(o('pending_acceptance')), null) // accept / refuse first assert.equal(onlineNextStep(o('accepted')), 'preparing') assert.equal(onlineNextStep(o('preparing')), 'ready') assert.equal(onlineNextStep(o('preparing', 'delivery')), 'out_for_delivery') assert.equal(onlineNextStep(o('out_for_delivery', 'delivery')), 'delivered') assert.equal(onlineNextStep(o('delivered')), null) assert.equal(onlineKind(o('x', null)), 'dine_in') assert.equal(onlineKind(o('x', 'delivery')), 'delivery') })