Files
xenia-pos-local/packages/api/tests/lines.test.mjs
T
bonaminandClaude Opus 5.5 8e0a786bbc feat(ordering): orders priced by the server (platform-v2 step 4.4)
The waiter app (and the manager's ordering tab) send what was chosen, by id; the
server prices it with the counter's code. No app sends a price any more.

- POST /api/orders/{id}/items takes `lines` (pos_cart's line + course_id,
  price_adjustment, is_service_item): sales point checked first, refusals name the
  product and line (order.line.*, order.not_sold_here); prices or selected_options in a
  line -> order.line.client_price, the old `items` shape -> order.lines_required; a
  by-hand price change needs ordering.prices.modify or a manager's approval (audited
  PRICE_SET_BY_HAND). add_priced_items is the one writer of priced items (pos too).
- POST /api/orders/quote: the cart's total before sending; /api/pos/quote shares
  pos_cart.quote().
- pos_cart: a preference set left out takes its default; a set at its free default
  isn't stored; a default that costs is charged (the waiter never sent it: fixed);
  an extra's sub-choice is charged per extra (the counter charged it once: fixed).
- @xenia/api lines.js: lineFromOptions / wireLine / estimate / LineError.
- Waiter: lines on send; cart total from the quote ("≈ estimate" offline); summary
  shows the same prices; offline orders queued by id, priced on sync, refused lines
  shown on a "Δεν στάλθηκαν" sheet (no empty order opened); dead ItemOptionsModal gone.
- Tests: test_order_lines.py (14), lines.test.mjs, waiter e2e +6.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 03:35:03 +02:00

74 lines
4.2 KiB
JavaScript

import { test } from 'node:test'
import assert from 'node:assert/strict'
import { LineError, estimate, lineFromOptions, wireLine } from '../src/lines.js'
// the menu's burger as /api/products lists it (local_backend tests/test_order_lines.py `full`)
const burger = {
id: 1, name: 'Burger', base_price: 10,
quick_options: [{ id: 11, name: 'Extra cheese', price: 1, allow_multiple: true }],
options: [
{ id: 21, name: 'Sauce', extra_cost: 0.5, sub_choices: [{ name: 'BBQ', extra_cost: 0.2, is_default: true }, { name: 'Mayo' }] },
{ id: 22, name: 'Pickles', extra_cost: 0.4, allow_multiple: true, sub_choices: [{ name: 'Sweet', extra_cost: 0.1 }, { name: 'Sour' }] },
],
ingredients: [{ id: 31, name: 'Onion' }],
preference_sets: [
{ id: 41, name: 'Cooking', default_choice_id: null,
shared_subset: { name: 'Side', choices: [{ name: 'Fries', is_default: true }, { name: 'Salad', extra_cost: 1 }] },
choices: [{ id: 411, name: 'Rare' }, { id: 412, name: 'Well done', extra_cost: 0.5, disables_subset: true }] },
{ id: 42, name: 'Bread', default_choice_id: 421,
choices: [{ id: 421, name: 'Brioche', sub_choices: [{ name: 'Toasted', is_default: true }, { name: 'Soft' }] }, { id: 422, name: 'Rye', extra_cost: 0.3 }] },
{ id: 43, name: 'Size', default_choice_id: 431, choices: [{ id: 431, name: 'Double', extra_cost: 1 }, { id: 432, name: 'Single' }] },
],
}
const e = (type, id, name, price_delta = 0) => ({ id, name, price_delta, type })
test('the waiter drawer\'s cart item becomes a line by id', () => {
const item = {
product_id: 1, quantity: 2, notes: ' no salt ', course_id: 3,
selected_options: [
e('pref', 411, 'Rare'), e('pref_sub', null, 'Salad', 1),
e('pref', 421, 'Brioche'), e('pref_sub', null, 'Soft'),
e('quick', null, 'Extra cheese', 1), e('quick', null, 'Extra cheese', 1),
e('extra', 21, 'Sauce', 0.5), e('extra_sub', null, 'BBQ', 0.2),
e('extra', 22, 'Pickles', 0.4), e('extra_sub', null, 'Sweet', 0.1),
e('extra', 22, 'Pickles', 0.4), e('extra_sub', null, 'Sweet', 0.1),
],
removed_ingredients: ['Onion'],
}
assert.deepEqual(wireLine(item, burger), {
product_id: 1, quantity: 2, notes: 'no salt', course_id: 3,
quick: [{ id: 11, qty: 2 }],
options: [{ id: 21, subs: ['BBQ'] }, { id: 22, subs: ['Sweet'] }, { id: 22, subs: ['Sweet'] }],
prefs: [{ set_id: 41, choices: [{ id: 411 }], shared: 'Salad' }, { set_id: 42, choices: [{ id: 421, sub: 'Soft' }] }],
removed: [31],
})
// the cheaper default isn't listed; the default that costs (Size: Double +1) counts in the estimate
assert.equal(estimate(item, burger), Math.round((10 + 1 + 0 + 2 + 0.7 + 1 + 1) * 2 * 100) / 100)
})
test('an item stored by the counter (quantities in the names) — order again', () => {
const stored = {
product_id: 1, quantity: 1,
selected_options: [e('quick', 11, 'Extra cheese x2', 2), e('extra', 22, 'Pickles x2', 0.8), e('extra_sub', null, 'Sweet x2', 0.2),
e('pref', 412, 'Well done', 0.5), e('pref', 431, 'Double', 1)],
}
assert.deepEqual(lineFromOptions(burger, stored), {
product_id: 1, quick: [{ id: 11, qty: 2 }], options: [{ id: 22, qty: 2, subs: ['Sweet'] }],
prefs: [{ set_id: 41, choices: [{ id: 412 }] }, { set_id: 43, choices: [{ id: 431 }] }],
})
})
test('a price changed by hand and a service item travel with the line; nothing else priced', () => {
const line = wireLine({ product_id: 1, quantity: 1, price_adjustment: -1.004, is_service_item: true, unit_price: 0.01, selected_options: [] }, burger)
assert.deepEqual(line, { product_id: 1, quantity: 1, price_adjustment: -1, is_service_item: true })
})
test('what is not on the menu any more is refused, not dropped', () => {
assert.throws(() => lineFromOptions(burger, { selected_options: [e('extra', 99, 'Truffle')] }), LineError)
assert.throws(() => lineFromOptions(burger, { selected_options: [e('quick', null, 'Bacon')] }), LineError)
assert.throws(() => lineFromOptions(burger, { removed_ingredients: ['Tomato'] }), err => err instanceof LineError && err.what === 'Tomato' && err.product === 'Burger')
// a product missing from the menu: fine without options
assert.deepEqual(wireLine({ product_id: 7, quantity: 3 }, undefined), { product_id: 7, quantity: 3 })
})