const test = require('node:test'); const assert = require('node:assert/strict'); const { mapOrderDiscounts } = require('../src/order-discounts'); const Module = require('node:module'); const path = require('node:path'); const source = { total_discounts: '75.35', total_discounts_tax_excl: '63.51', total_discounts_tax_incl: '75.35' }; const product = { qty: 3, unitPrice: 62.03, tax: 0.23 }; test('6Q3-M9R: inconsistent source totals cannot manufacture 19% VAT', () => { assert.deepEqual(mapOrderDiscounts(source, [product]), [ { name: 'Rabat', type: 'product', amount: 61.26, gross: 75.35, tax: 0.23 }, ]); }); test('discounts follow actual VAT, including exempt and fractional rates', () => { for (const tax of [0, 0.08, 0.19, 0.077]) { const [discount] = mapOrderDiscounts(source, [{ ...product, tax }]); assert.equal(discount.tax, tax); assert.equal(discount.amount, Math.round(75.35 / (1 + tax) * 100) / 100); assert.equal(discount.gross, 75.35); } assert.equal(mapOrderDiscounts(source, [product, { ...product, unitPrice: 0, tax: 0.08 }])[0].tax, 0.23); assert.deepEqual(mapOrderDiscounts({ total_discounts: '0' }, []), []); }); test('mixed or missing VAT requires a verified breakdown instead of a synthetic rate', () => { for (const products of [[], [{ ...product, tax: undefined }], [product, { ...product, tax: 0.08 }], [{ ...product, isPack: true, pack: [{ qty: 1, tax: 0.23 }, { qty: 1, tax: 0.08 }] }]]) { assert.throws(() => mapOrderDiscounts(source, products), { code: 'DISCOUNT_TAX_AMBIGUOUS' }); } assert.throws(() => mapOrderDiscounts({ ...source, total_discounts_tax_incl: 'invalid' }, [product]), /brutto/); }); async function importThroughHooks(providers) { const created = []; const original = Module._load; const Core = { getModuleInfo: () => ({ code: 'core/prestashop' }), async execHook(name, envelope) { if (name.endsWith('.providers')) return providers.map(instanceId => ({ protocol: 'orbito.contract/1', moduleCode: 'alternative/orders', instanceId })); let data; if (name === 'orders.status.search') data = { results: [{ _id: '000000000000000000000001' }] }; else if (name === 'orders.create') { created.push(envelope.params); data = { _id: 'created-1' }; } else throw new Error(name); return [{ protocol: 'orbito.contract/1', instanceId: envelope.targetInstanceId, ok: true, data }]; }, }; Module._load = function(request, ...args) { if (request === '@orbito/api-lib') return { Core, Log: { debug() {}, info() {}, warn() {}, error() {} } }; if (request === './models/PsOrder') return { update: async () => {}, findOne: async () => null }; return original.call(this, request, ...args); }; const filename = path.resolve(__dirname, '../src/OrderImporter'); delete require.cache[require.resolve(filename)]; let importer; try { importer = require(filename); } finally { Module._load = original; } const order = { ...source, id: '4858', reference: 'MWSKTWLNP', id_address_delivery: '1', id_customer: '1', id_currency: '1', total_shipping_tax_excl: '12', payment: 'B2B', date_add: '2026-10-08 11:40:00', associations: { order_rows: [{ product_id: '1', product_name: 'PAVING', product_reference: 'NS.02.J', product_attribute_id: '0', product_quantity: '3', unit_price_tax_excl: '62.03', unit_price_tax_incl: '76.30' }] }, }; const api = { isBLSupported: async () => false, makeRequest: async endpoint => { if (endpoint === 'orders/4858') return { order }; if (endpoint === 'addresses/1') return { address: { id_country: '1', address1: 'Street', postcode: '00-001' } }; if (endpoint === 'customers/1') return { customer: { firstname: 'Test', lastname: 'Test', email: 'test@example.com' } }; throw new Error(endpoint); }, getProduct: async () => ({ name: 'PAVING', type: 'standard' }), getProductCoverImageUrl: async () => null, getCurrency: async () => ({ iso_code: 'PLN' }), getCountry: async () => ({ iso_code: 'PL' }), getCustomerNotes: async () => [], }; try { await importer.importFromPSOrder('4858', api, { _id: 'shop-1', orderStatus: '000000000000000000000001' }); return { created }; } catch (error) { return { created, error }; } } test('real hook import delivers corrected discount to an alternative orders provider once', async () => { const { created, error } = await importThroughHooks(['orders-account-1']); assert.equal(error, undefined); assert.equal(created.length, 1); assert.deepEqual(created[0].discounts, [{ name: 'Rabat', type: 'product', amount: 61.26, gross: 75.35, tax: 0.23 }]); }); test('absent or multiple orders accounts do not create an order', async () => { for (const providers of [[], ['orders-account-1', 'orders-account-2']]) { const result = await importThroughHooks(providers); assert.equal(result.created.length, 0); assert.match(result.error.code, /^HOOK_PROVIDER_(UNAVAILABLE|AMBIGUOUS)$/); } });