// The pricing page's rules as data (plan step 6.7): forms, what is sent, what's missing, summaries. import { test } from 'node:test' import assert from 'node:assert/strict' import { actionText, blankCondition, conditionComplete, dealBody, dealForm, dealProblems, examplePrice, groupBody, groupForm, groupProblems, modifierBody, modifierForm, modifierProblems, moveItem, parseLimit, } from '../src/pages/pricing/pricingLogic.js' const money = v => `€${v.toFixed(2)}` test('a condition is complete only with all its values (the server refuses the rest)', () => { assert.equal(conditionComplete(blankCondition('time_range')), true) assert.equal(conditionComplete({ condition_type: 'time_range', params: { from: '17:00', to: '' } }), false) assert.equal(conditionComplete({ condition_type: 'time_range', params: { from: '22:00', to: '02:00' } }), true) // crosses midnight assert.equal(conditionComplete(blankCondition('date_range')), false) assert.equal(conditionComplete({ condition_type: 'date_range', params: { from: '2026-12-31', to: '2026-12-01' } }), false) assert.equal(conditionComplete(blankCondition('day_of_week')), false) assert.equal(conditionComplete({ condition_type: 'day_of_week', params: { days: [5, 6] } }), true) assert.equal(conditionComplete(blankCondition('price_group_active')), false) assert.equal(conditionComplete({ condition_type: 'min_category_qty', params: { category_id: 3, min: 2 } }), true) assert.equal(conditionComplete({ condition_type: 'user_tier', params: {} }), true) // old placeholder: kept }) test('a happy-hour rule: form → body', () => { const f = modifierForm(null, '#000') assert.deepEqual(modifierProblems(f), ['name']) f.name = ' Happy hour ' f.targets = [{ target_type: 'category', target_ids: [], target_tags: [] }] f.conditions = [blankCondition('time_range')] assert.deepEqual(modifierProblems(f), ['targets']) f.targets[0].target_ids = [4, 7] f.action_value = -20 f.round_to = '0.50' assert.deepEqual(modifierProblems(f), []) const body = modifierBody(f) assert.equal(body.name, 'Happy hour') assert.deepEqual(body.targets, [{ target_type: 'category', target_ids: [4, 7], target_id: 4, target_tags: null, target_tag: null }]) assert.deepEqual(body.conditions, [{ condition_type: 'time_range', params: { from: '17:00', to: '19:00' } }]) assert.equal(body.round_to, '0.50') // rounding only with a percentage; one product: no targets assert.equal(modifierBody({ ...f, action_type: 'add_amount' }).round_to, null) const one = modifierBody({ ...f, scope: 'item', item_id: 9 }) assert.deepEqual([one.item_id, one.targets], [9, []]) assert.deepEqual(modifierProblems({ ...f, scope: 'item', item_id: null }), ['product']) }) test('an old rule with single target ids loads as lists', () => { const f = modifierForm({ id: 1, name: 'x', action_type: 'set', action_value: 3, targets: [{ target_type: 'item', target_id: 5 }, { target_type: 'tag', target_tag: 'vegan' }], conditions: [] }, '#000') assert.deepEqual(f.targets, [{ target_type: 'item', target_ids: [5], target_tags: [] }, { target_type: 'tag', target_ids: [], target_tags: ['vegan'] }]) }) test('summaries and the example price (rounded to 0.10 like the server)', () => { assert.equal(actionText('add_percent', -10, money), '−10%') assert.equal(actionText('add_amount', 0.5, money), '+€0.50') assert.equal(actionText('set', 3, money), '= €3.00') assert.equal(examplePrice(5, 'add_percent', -20, ''), 4) assert.equal(examplePrice(5, 'add_percent', -15, ''), 4.3) // 4.25 → 4.30 assert.equal(examplePrice(5, 'add_percent', -15, 'x.99'), 5) // 4.25 → 4.99, then the system's 0.10 → 5.00 assert.equal(examplePrice(2, 'add_amount', -5, ''), 0) // never below 0 }) test('price groups: a schedule is all or nothing', () => { const f = groupForm(null, '#000') f.name = 'Happy hour' assert.deepEqual(groupProblems(f), []) f.auto_enable_time = '17:00' assert.deepEqual(groupProblems(f), ['schedule']) f.auto_disable_time = '19:00' f.auto_days = [4, 0] assert.deepEqual(groupProblems(f), []) assert.deepEqual(groupBody(f).auto_days, [0, 4]) assert.equal(groupBody({ ...f, auto_days: [] }).auto_enable_time, null) }) test('deals: what each kind needs', () => { const f = dealForm(null, '#000') f.name = '3 beers' assert.deepEqual(dealProblems(f), ['freeItem']) f.action_free_item_id = 12 assert.deepEqual(dealProblems(f), []) const body = dealBody(f) assert.equal(body.action_free_item_id, 12) assert.equal(body.action_value, null) assert.deepEqual(body.targets, [{ target_type: 'any', target_ids: null, target_id: null, target_tags: null, target_tag: null }]) assert.deepEqual(dealProblems({ ...f, action_type: 'free_choice' }), ['freeChoice']) assert.deepEqual(dealProblems({ ...f, action_type: 'apply_modifier' }), ['modifier']) const pct = dealBody({ ...f, action_type: 'add_percent', action_value: -50 }) assert.deepEqual([pct.action_value, pct.action_free_item_id], [-50, null]) }) test('limits typed in a box', () => { assert.deepEqual(parseLimit(''), { ok: true, value: null }) assert.deepEqual(parseLimit('12,5'), { ok: true, value: 12.5 }) assert.deepEqual(parseLimit('-1'), { ok: false, value: null }) assert.deepEqual(parseLimit('2.5', true), { ok: false, value: null }) assert.deepEqual(parseLimit('3', true), { ok: true, value: 3 }) }) test('moving a rule up and down', () => { assert.deepEqual(moveItem([1, 2, 3], 2, 1), [1, 3, 2]) assert.deepEqual(moveItem([1, 2, 3], 0, -1), [1, 2, 3]) })