lowPriceGoodsValidation.helpers.test.mjs 5.13 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import {
  applyLowPriceGoodsPresentation,
  LOW_PRICE_NOT_TRIGGERED_MESSAGE,
  resolveLowPriceGoodsEntries,
  validateLowPriceGoods,
  validateLowPriceGoodsList
} from '../services/lowPriceGoodsValidation.helpers.js'

describe('lowPriceGoodsValidation.helpers', () => {
  it('IT-WO-LPG-001: accepts an enabled product only when input price is strictly below minSellPrice', () => {
    assert.deepEqual(
      validateLowPriceGoods({
        validMinSellPrice: 'Y',
        minSellPrice: 100,
        sellPrice: 99.99
      }),
      {
        valid: true,
        enabled: true,
        minPrice: 100,
        currentPrice: 99.99
      }
    )

    assert.equal(
      validateLowPriceGoods({
        validMinSellPrice: 'Y',
        minSellPrice: 100,
        sellPrice: 100
      }).valid,
      false
    )
    assert.equal(
      validateLowPriceGoods({
        validMinSellPrice: 'Y',
        minSellPrice: 100,
        sellPrice: 101
      }).valid,
      false
    )
  })

  it('IT-WO-LPG-002: rejects disabled, missing-minimum and empty-price products', () => {
    const invalidGoods = [
      { validMinSellPrice: 'N', minSellPrice: 100, sellPrice: 50 },
      { validMinSellPrice: 'Y', sellPrice: 50 },
      { validMinSellPrice: 'Y', minSellPrice: '', sellPrice: 50 },
      { validMinSellPrice: 'Y', minSellPrice: 100, sellPrice: '' }
    ]

    invalidGoods.forEach(goods => {
      assert.equal(validateLowPriceGoods(goods).valid, false)
    })
  })

  it('IT-WO-LPG-003: validates the edited basePrice before catalog sellPrice', () => {
    const result = validateLowPriceGoods({
      validMinSellPrice: 'Y',
      minSellPrice: 100,
      basePrice: 80,
      sellPrice: 120,
      price: 130
    })

    assert.equal(result.currentPrice, 80)
    assert.equal(result.valid, true)
  })

  it('IT-WO-LPG-004: flattens single goods, selected SKU and every package child', () => {
    const single = { sellPrice: 10 }
    const sku = { sellPrice: 20 }
    const packageChildA = { sellPrice: 30 }
    const packageChildB = { sellPrice: 40 }

    assert.deepEqual(
      resolveLowPriceGoodsEntries([
        { goodsFlag: 1, goods: single },
        {
          goodsFlag: 2,
          multipleSpecificationsGoods: { goods: sku }
        },
        {
          goodsFlag: 3,
          packageGoods: { goodsList: [packageChildA, packageChildB] }
        }
      ]),
      [single, sku, packageChildA, packageChildB]
    )
  })

  it('IT-WO-LPG-005: whole-order validation passes when any leaf triggers minimum price', () => {
    const result = validateLowPriceGoodsList([
      {
        goodsFlag: 1,
        goods: {
          validMinSellPrice: 'Y',
          minSellPrice: 100,
          sellPrice: 80
        }
      },
      {
        goodsFlag: 3,
        packageGoods: {
          goodsList: [
            {
              validMinSellPrice: 'Y',
              minSellPrice: 50,
              sellPrice: 50
            }
          ]
        }
      }
    ])

    assert.equal(result.valid, true)
    assert.equal(result.matchedGoods.length, 1)
    assert.equal(result.invalidGoods.length, 1)
  })

  it('IT-WO-LPG-006: whole-order validation fails when no leaf triggers minimum price', () => {
    const result = validateLowPriceGoodsList([
      {
        goodsFlag: 1,
        goods: {
          validMinSellPrice: 'Y',
          minSellPrice: 100,
          sellPrice: 100
        }
      },
      {
        goodsFlag: 1,
        goods: {
          validMinSellPrice: 'N',
          minSellPrice: 60,
          sellPrice: 30
        }
      }
    ])

    assert.equal(result.valid, false)
    assert.deepEqual(result.matchedGoods, [])
    assert.equal(result.invalidGoods.length, 2)
  })

  it('IT-WO-LPG-007: empty goods cannot satisfy whole-order minimum price', () => {
    const result = validateLowPriceGoodsList([])

    assert.equal(result.valid, false)
    assert.deepEqual(result.matchedGoods, [])
    assert.deepEqual(result.invalidGoods, [])
  })

  it('IT-WO-LPG-008: exposes the required reminder copy', () => {
    assert.equal(
      LOW_PRICE_NOT_TRIGGERED_MESSAGE,
      '价格未触发最低价,其他正常单请到生意助手侧录单'
    )
  })

  it('IT-WO-LPG-009: presentation text never creates or changes input errors', () => {
    const enabled = {
      validMinSellPrice: 'Y',
      minSellPrice: 100,
      sellPrice: 120
    }
    const disabled = {
      validMinSellPrice: 'N',
      minSellPrice: 80,
      sellPrice: 20,
      errorTarget: false,
      checkError: false,
      basePriceError: false
    }

    applyLowPriceGoodsPresentation([
      { goodsFlag: 1, goods: enabled },
      { goodsFlag: 1, goods: disabled }
    ])

    assert.equal(enabled.lowPriceMinPriceText, '最低价:¥100')
    assert.equal(disabled.lowPriceMinPriceText, '最低价:未开启')
    assert.equal(Object.hasOwn(enabled, 'errorTarget'), false)
    assert.equal(Object.hasOwn(enabled, 'checkError'), false)
    assert.equal(Object.hasOwn(enabled, 'basePriceError'), false)
    assert.equal(disabled.errorTarget, false)
    assert.equal(disabled.checkError, false)
    assert.equal(disabled.basePriceError, false)
  })
})