minPriceApproval.test.mjs 3.03 KB
import { test } from 'node:test'
import assert from 'node:assert/strict'
import {
  buildMinPriceApprovalList,
  needsMinPriceApproval,
  needsPackageSubMinPriceApproval,
  resolveApprovalActivityPrice,
  resolveApprovalCheckPrice
} from '../services/minPriceApproval.js'

test('historical approval comparison uses exact cents and keeps gifts excluded', () => {
  assert.equal(
    needsMinPriceApproval({
      validMinSellPrice: 'Y',
      upgradeShopMinPrice: '200.00',
      minSellPrice: 100,
      sellPrice: '199.99'
    }),
    true
  )
  assert.equal(
    needsMinPriceApproval({
      validMinSellPrice: 'Y',
      minSellPrice: 200,
      sellPrice: 200
    }),
    false
  )
  assert.equal(
    needsMinPriceApproval({
      validMinSellPrice: 'Y',
      minSellPrice: 200,
      sellPrice: 100,
      discountType: 4
    }),
    false
  )
})

test('resolveApprovalCheckPrice prefers basePrice', () => {
  assert.equal(resolveApprovalCheckPrice({ basePrice: 100, sellPrice: 200 }), 100)
})

test('activity price preserves catalog authority and safe unit-price fallback', () => {
  assert.equal(
    resolveApprovalActivityPrice({ catalogSellPrice: 299, sellPrice: 100 }),
    299
  )
  assert.equal(
    resolveApprovalActivityPrice({ price: 598, number: 2 }),
    299
  )
  assert.equal(
    resolveApprovalActivityPrice({ price: '1.001', number: 1 }),
    0
  )
  assert.equal(
    resolveApprovalActivityPrice({ price: 100, number: 0 }),
    0
  )
})

test('approval list keeps single, package and multi-spec display contracts', () => {
  const list = buildMinPriceApprovalList([
    {
      goodsFlag: 1,
      count: 2,
      marketingFlag: 1,
      goods: {
        validMinSellPrice: 'Y',
        minSellPrice: 200,
        activitySellPrice: 299,
        sellPrice: 100,
        number: 1,
        upgradeGoodsName: '太阳膜'
      }
    },
    {
      goodsFlag: 3,
      count: 1,
      packageGoods: {
        packageName: '套餐',
        goodsList: [
          {
            validMinSellPrice: 'Y',
            minSellPrice: 199,
            sellPrice: 179,
            upgradeGoodsName: '车窗膜'
          },
          {
            validMinSellPrice: 'Y',
            minSellPrice: 320,
            sellPrice: 320,
            upgradeGoodsName: '正常商品'
          }
        ]
      }
    },
    {
      goodsFlag: 2,
      count: 1,
      multipleSpecificationsGoods: {
        goods: {
          validMinSellPrice: 'Y',
          minSellPrice: 100,
          sellPrice: 0.1,
          activitySellPrice: 30,
          upgradeGoodsName: '多规格 SKU'
        }
      }
    }
  ])

  assert.equal(list.length, 3)
  assert.equal(list[0].count, 2)
  assert.equal(list[0].activityPrice, 299)
  assert.equal(list[1].children.length, 1)
  assert.equal(list[1].totalPrice, 179)
  assert.equal(list[2].activityPrice, 30)
})

test('package approval compares its own minSellPrice', () => {
  assert.equal(
    needsPackageSubMinPriceApproval({
      validMinSellPrice: 'Y',
      minSellPrice: 30,
      upgradeShopMinPrice: 200,
      sellPrice: 40
    }),
    false
  )
})