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('the reason for a price changed by hand travels with it, never alone (6F.3)', () => { assert.deepEqual(wireLine({ product_id: 1, quantity: 1, price_adjustment: -0.5, price_reason: 'Regular customer' }, burger), { product_id: 1, quantity: 1, price_adjustment: -0.5, price_reason: 'Regular customer' }) assert.deepEqual(wireLine({ product_id: 1, quantity: 1, price_reason: 'nothing changed' }, burger), { product_id: 1, quantity: 1 }) }) 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 }) })