// Order channel prices as plain data (2026-11-20): what a rule makes of the default price, what // a rule needs, and the product editor's quick edit. import { test } from 'node:test' import assert from 'node:assert/strict' import { applyRule, levelOf, pointRowFromApi, pointsBody, rowPrice, ruleBody, ruleProblems, blankRule } from '../src/pages/pricing/channelPricing.js' test('a rule makes the price there from the default price', () => { assert.equal(applyRule(10, { action_type: 'set', value: 8.5 }), 8.5) assert.equal(applyRule(10, { action_type: 'add_amount', value: -1.2 }), 8.8) assert.equal(applyRule(3, { action_type: 'add_percent', value: 10 }), 3.3) assert.equal(applyRule(9.3, { action_type: 'add_percent', value: 0, round_to: '0.50' }), 9.5) assert.equal(applyRule(1, { action_type: 'add_amount', value: -5 }), 0) assert.equal(levelOf({ product_ids: [1], category_ids: [] }), 'product') assert.equal(levelOf({ product_ids: [], category_ids: [2] }), 'category') assert.equal(levelOf({ product_ids: [], category_ids: [] }), 'channel') }) test('what a rule needs: a sales point, a value, a set price for one product', () => { const f = blankRule() assert.deepEqual(ruleProblems(f), ['points', 'value']) assert.deepEqual(ruleProblems({ ...f, sales_points: ['qr_table'], value: '-10' }), []) assert.deepEqual(ruleProblems({ ...f, sales_points: ['qr_table'], action_type: 'set', value: '2', category_ids: [3] }), ['set_one']) assert.deepEqual(ruleBody({ ...f, sales_points: ['qr_table'], value: '-10,5' }).value, -10.5) }) test('the product quick edit: default rows send null, own rows their rule; the price shown', () => { const rows = [ pointRowFromApi({ sales_point: 'in_store', base_price: 10, own: null, fallback: null }), pointRowFromApi({ sales_point: 'online_delivery', base_price: 10, own: null, fallback: { level: 'channel', action_type: 'add_percent', value: 10, round_to: null } }), pointRowFromApi({ sales_point: 'in_store_takeaway', base_price: 10, own: { action_type: 'set', value: 9, round_to: null }, fallback: null }), ] assert.deepEqual(rows.map(rowPrice), [10, 11, 9]) assert.deepEqual(pointsBody(rows), { in_store: null, online_delivery: null, in_store_takeaway: { action_type: 'set', value: 9, round_to: null } }) })