goodsChooseEcho.helpers.test.mjs 3.5 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import {
  mergeSubOrderListForEcho,
  findCatalogSingleGoodsItem,
  resolveSubOrderUnitPrice,
  applySubOrderDiscountEcho,
  buildSingleGoodsChooseEntry,
  resolveSubOrderMarketingEcho,
  isMultiSpecSubOrderLine,
  normalizeOrderMoney
} from '../services/goodsChooseEcho.helpers.js'

describe('goodsChooseEcho.helpers', () => {
  it('IT-WO-ECHO-001: merge filters package rows from subOrder', () => {
    const merged = mergeSubOrderListForEcho(
      [{ id: 1 }, { packageName: 'pkg' }],
      [{ id: 2 }]
    )
    assert.equal(merged.length, 2)
    assert.equal(merged[0].id, 1)
    assert.equal(merged[1].id, 2)
  })

  it('IT-WO-ECHO-002: findCatalogSingleGoodsItem', () => {
    const cat = findCatalogSingleGoodsItem(
      [{ goodsFlag: 1, goods: { merchantGoodsId: 9 } }],
      9
    )
    assert.equal(cat.goods.merchantGoodsId, 9)
  })

  it('IT-WO-ECHO-003: resolveSubOrderUnitPrice resetOrder uses sellPrice', () => {
    assert.equal(
      resolveSubOrderUnitPrice({ sellPrice: 88, number: 2 }, true),
      88
    )
  })

  it('IT-WO-ECHO-004: resolveSubOrderUnitPrice from total', () => {
    assert.equal(
      resolveSubOrderUnitPrice({ totalPrice: 200, number: 2 }, false),
      100
    )
  })

  it('IT-WO-ECHO-010: total price division is normalized to cents', () => {
    for (const resetOrder of [false, true]) {
      assert.equal(
        resolveSubOrderUnitPrice(
          { totalPrice: 9999.95, number: 5 },
          resetOrder
        ),
        1999.99,
        `resetOrder=${resetOrder}`
      )
    }
  })

  it('IT-WO-ECHO-011: echoed sell price drops binary floating-point tails', () => {
    assert.equal(
      resolveSubOrderUnitPrice(
        { sellPrice: 1999.9900000000002, number: 5 },
        true
      ),
      1999.99
    )
  })

  it('IT-WO-ECHO-012: missing echoed money is distinct from explicit zero', () => {
    assert.equal(normalizeOrderMoney(null), undefined)
    assert.equal(normalizeOrderMoney(undefined), undefined)
    assert.equal(normalizeOrderMoney(''), undefined)
    assert.equal(normalizeOrderMoney('   '), undefined)
    assert.equal(normalizeOrderMoney(0), 0)
    assert.equal(normalizeOrderMoney('0'), 0)
  })

  it('IT-WO-ECHO-005: applySubOrderDiscountEcho ratio', () => {
    const g = { sellPrice: 100, discountType: 1, discountValue: 8 }
    applySubOrderDiscountEcho(g)
    assert.equal(g.finalPrice, 80)
  })

  it('IT-WO-ECHO-006: applySubOrderDiscountEcho gift', () => {
    const g = { sellPrice: 50, discountType: 4 }
    applySubOrderDiscountEcho(g)
    assert.equal(g.finalPrice, 0)
  })

  it('IT-WO-ECHO-007: resolveSubOrderMarketingEcho', () => {
    const meta = resolveSubOrderMarketingEcho(
      { marketingFlag: 1, activityId: 3 },
      { marketingFlag: 0, activityId: 9 }
    )
    assert.equal(meta.activityId, 3)
    assert.equal(meta.isMarketingGoods, true)
  })

  it('IT-WO-ECHO-008: buildSingleGoodsChooseEntry', () => {
    const line = {
      merchantGoodsId: 1,
      number: 2,
      specificationData: 'S'
    }
    const entry = buildSingleGoodsChooseEntry(line, {
      marketingFlag: 1,
      activityId: 5
    })
    assert.equal(entry.goodsFlag, 1)
    assert.equal(entry.count, 2)
    assert.equal(entry.activityId, 5)
  })

  it('IT-WO-ECHO-009: skips multi-spec lines', () => {
    assert.equal(
      buildSingleGoodsChooseEntry({
        specificationData: 'A;~;B'
      }),
      null
    )
    assert.equal(isMultiSpecSubOrderLine({ specificationData: 'A;~;B' }), true)
  })
})