simpleQuotationPayload.helpers.test.mjs 4.56 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import {
  buildSimpleQuotationPayload,
  mapSimpleQuotationGoods
} from '../pages/carQuotation/services/simpleQuotationPayload.helpers.js'

describe('simpleQuotationPayload.helpers', () => {
  it('IT-EQ-SIMPLE-PAY-001: maps single, package and multi-spec goods without re-deriving authoritative line totals', () => {
    const goods = [
      {
        goods: {
          merchantGoodsId: 'single-1',
          goodsName: '单品',
          goodsAlias: '单品别名',
          specificationNameSequence: '颜色',
          specificationData: '红;~;大',
          price: 100,
          finalPrice: 123,
          quantity: 3,
          discountType: 1,
          discountValue: 9,
          discountAmount: 7
        }
      },
      {
        packageGoods: {
          packageCode: 'package-1',
          packageName: '套餐',
          packageAlias: '套餐别名',
          goodsList: [
            {
              merchantGoodsId: 'package-good-1',
              goodsName: '套餐商品',
              price: 200,
              finalPrice: 150,
              quantity: 0,
              discountType: 2
            }
          ]
        }
      },
      {
        multipleSpecificationsGoods: {
          goods: {
            merchantGoodsId: 'sku-1',
            upgradeGoodsName: '多规格商品',
            price: 300,
            finalPrice: 260,
            discountType: 3
          }
        }
      },
      {
        goods: {
          merchantGoodsId: 'gift-1',
          goodsName: '赠品',
          price: 999,
          finalPrice: 999,
          discountType: 4
        }
      }
    ]

    const mapped = mapSimpleQuotationGoods(goods)

    assert.equal(mapped.length, 4)
    assert.equal(mapped[0].finalPrice, 123)
    assert.equal(mapped[0].singleProductFinalPrice, 123)
    assert.equal(mapped[1].packageId, 'package-1')
    assert.equal(mapped[1].finalPrice, 150)
    assert.equal(mapped[2].goodsId, 'sku-1')
    assert.equal(mapped[2].finalPrice, 260)
    assert.equal(mapped[3].finalPrice, 0)
  })

  it('IT-EQ-SIMPLE-PAY-002: builds the existing API payload in integer cents without mutating page state', () => {
    const input = {
      owner: {
        customerName: '张三',
        price: '0.29',
        salesType: '售后'
      },
      orderInfo: {
        id: 8,
        insurancePrice: '1.01',
        total: '2.02',
        quotedPriceUpdate: '3.03',
        finalPrice: '4.04',
        minEquityPrice: '5.05',
        paymentDocList: undefined,
        vdQuotationGoodsList: [
          {
            goods: {
              merchantGoodsId: 'g-1',
              goodsName: '商品',
              price: 99,
              discountType: 4
            }
          }
        ]
      },
      context: {
        merchant4s: { merchantId: 11, merchantName: '测试门店' }
      }
    }
    const snapshot = structuredClone(input)

    const result = buildSimpleQuotationPayload(input)

    assert.equal(result.ok, true)
    assert.equal(result.payload.insurancePrice, 101)
    assert.equal(result.payload.total, 202)
    assert.equal(result.payload.quotedPriceUpdate, 303)
    assert.equal(result.payload.finalPrice, 404)
    assert.equal(result.payload.minEquityPrice, 505)
    assert.equal(result.payload.owner.price, 29)
    assert.equal(result.payload.owner.salesType, 1)
    assert.deepEqual(result.payload.paymentDocList, [])
    assert.deepEqual(result.payload.vdQuotationGoodsList[0], {
      goodsId: 'g-1',
      goodsName: '商品',
      goodsNameAlias: undefined,
      specificationsName: undefined,
      specificationsValue: '',
      goodsPrice: 99,
      finalPrice: 0,
      singleProductFinalPrice: 0,
      discountType: 4,
      discountValue: undefined,
      discountAmount: 0
    })
    assert.equal(result.payload.merchantId4s, 11)
    assert.deepEqual(input, snapshot)
  })

  it('IT-EQ-SIMPLE-PAY-003: missing optional amounts become zero but invalid amounts block submission', () => {
    const empty = buildSimpleQuotationPayload({
      owner: {},
      orderInfo: { vdQuotationGoodsList: [] },
      context: {}
    })
    assert.equal(empty.ok, true)
    assert.equal(empty.payload.total, 0)
    assert.deepEqual(empty.payload.vdQuotationGoodsList, [])

    const invalid = buildSimpleQuotationPayload({
      owner: {},
      orderInfo: {
        total: '0.001',
        finalPrice: '-1',
        vdQuotationGoodsList: []
      },
      context: {}
    })
    assert.equal(invalid.ok, false)
    assert.deepEqual(
      invalid.errors.map(item => item.field),
      ['total', 'finalPrice']
    )
  })
})