import assert from 'node:assert/strict'; import { buildProductOrderReconciliation } from '../dist/products/product-order-service.js'; const detail = { id: '101', orderNo: 'PG101', status: 'COMPLETED', inventoryStatus: 'DEDUCTED', itemCount: 1, totalQuantity: 2, totalAmountCents: 500, paidAmountCents: 500, refundedAmountCents: 0, items: [{ id: '201', quantity: 2, subtotalCents: 500 }], payments: [{ id: '301', status: 'SUCCEEDED', amountCents: 500, refundedAmountCents: 0 }], refunds: [] }; const inventoryLedger = [ { id: '401', inventoryId: '51', skuId: '5', operation: 'LOCK', availableDelta: -2, lockedDelta: 2, lossDelta: 0, reservationDelta: 2, saleDelta: 0, availableAfter: 8, lockedAfter: 2, lossAfter: 0, versionAfter: 2 }, { id: '402', inventoryId: '51', skuId: '5', operation: 'DEDUCT', availableDelta: 0, lockedDelta: -2, lossDelta: 0, reservationDelta: -2, saleDelta: 2, availableAfter: 8, lockedAfter: 0, lossAfter: 0, versionAfter: 3 } ]; const storages = [{ id: '601', storageNo: 'PS601', status: 'PARTIALLY_RETRIEVED', recordTotalQuantity: 2, recordRemainingQuantity: 1, itemTotalQuantity: 2, itemRemainingQuantity: 1, itemCount: 1 }]; const consistent = buildProductOrderReconciliation(detail, inventoryLedger, storages); assert.equal(consistent.consistent, true); assert.equal(consistent.amounts.netRevenueCents, 500); assert.deepEqual( [consistent.inventory.reservationBalance, consistent.inventory.saleBalance], [0, 2] ); assert.equal(consistent.storages[0].consistent, true); assert.ok(consistent.checks.every((check) => check.passed)); const inconsistent = buildProductOrderReconciliation( { ...detail, paidAmountCents: 400, refundedAmountCents: 100 }, inventoryLedger, [{ ...storages[0], itemRemainingQuantity: 2 }] ); assert.equal(inconsistent.consistent, false); assert.equal( inconsistent.checks.find((check) => check.key === 'PAYMENT_CAPTURED_AMOUNT').passed, false ); assert.equal( inconsistent.checks.find((check) => check.key === 'REFUND_SUCCEEDED_AMOUNT').passed, false ); assert.equal( inconsistent.checks.find((check) => check.key === 'STORAGE_ITEM_BALANCE').passed, false ); const released = buildProductOrderReconciliation({ ...detail, status: 'CANCELLED', inventoryStatus: 'RELEASED', paidAmountCents: 0, payments: [], refunds: [] }, [ inventoryLedger[0], { ...inventoryLedger[1], operation: 'RELEASE', reservationDelta: -2, saleDelta: 0 } ], []); assert.equal(released.consistent, true); assert.deepEqual([released.inventory.reservationBalance, released.inventory.saleBalance], [0, 0]); console.log('PASS: M09-REGRESSION detects amount, payment, refund, inventory and storage drift.');