quotationPayload.helpers.test.mjs 5.37 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import {
  buildInsurancePayload,
  buildOwnerPayload,
  parseInsureListToForm,
  mapQuotationGoodsList,
  mapOwnerSalesTypeForApi,
  mapOwnerSalesTypeFromApi
} from '../pages/carQuotation/services/quotationPayload.helpers.js'

describe('quotationPayload.helpers', () => {
  it('IT-EQ-PAY-001: buildInsurancePayload flattens insureList', () => {
    const payload = buildInsurancePayload(
      {
        business: { type: 0, base: 100 },
        mutualStrength: { type: 1, payPrice: 50 },
        vehiclesAndVessels: { type: 2 },
        valueAddedVehicleLoss: { type: 3 },
        otherInsuranceList: [{ type: 4, base: 10 }]
      },
      { merchantId: 'm1', merchantName: '店A' }
    )
    assert.equal(payload.insureList.length, 5)
    assert.equal(payload.business, undefined)
    assert.equal(payload.merchantId4s, 'm1')
    assert.equal(payload.insureList[0].base, 10000)
  })

  it('IT-EQ-PAY-002: vdQuotationGoodsList defaults', () => {
    const payload = buildInsurancePayload(
      {
        business: {},
        mutualStrength: {},
        vehiclesAndVessels: {},
        valueAddedVehicleLoss: {},
        otherInsuranceList: []
      },
      {}
    )
    assert.deepEqual(payload.vdQuotationGoodsList, [])
    assert.deepEqual(payload.paymentDocList, [])
  })

  it('IT-EQ-PAY-003: buildOwnerPayload scales price', () => {
    const owner = buildOwnerPayload(
      {
        price: 12.5,
        salesType: 0,
        buyCarTime: '2020',
        customerTags: '3,1,3',
        actualDeliveryDate: '2026-08-04'
      },
      undefined,
      { baseSceneName: '2.3.1 权益场景' }
    )
    assert.equal(owner.price, 1250)
    assert.equal(owner.buyCarTime, undefined)
    assert.equal(owner.customerTags, '1,3')
    assert.equal(owner.actualDeliveryDate, '2026-08-04 00:00:00')
  })

  it('IT-EQ-PAY-003b: buildOwnerPayload keeps empty daily fields', () => {
    const owner = buildOwnerPayload({ price: '', customerTags: [], actualDeliveryDate: '' })
    assert.equal(owner.customerTags, '')
    assert.equal(owner.actualDeliveryDate, '')
  })

  it('IT-EQ-PAY-003c: buildOwnerPayload clears values hidden by source', () => {
    const afterSales = buildOwnerPayload(
      {
        salesType: 1,
        customerTags: '1,3',
        actualDeliveryDate: '2026-08-04'
      },
      undefined,
      { baseSceneName: '2.3.1 权益场景' }
    )
    assert.equal(afterSales.customerTags, '1,3')
    assert.equal(afterSales.actualDeliveryDate, '')

    const natural = buildOwnerPayload(
      {
        salesType: 2,
        customerTags: '1,3',
        actualDeliveryDate: '2026-08-04'
      },
      undefined,
      { baseSceneName: '2.3.1 权益场景' }
    )
    assert.equal(natural.customerTags, '')
    assert.equal(natural.actualDeliveryDate, '')
  })

  it('IT-EQ-PAY-003d: buildOwnerPayload clears date outside template scenes', () => {
    const owner = buildOwnerPayload(
      {
        salesType: 0,
        customerTags: '1',
        actualDeliveryDate: '2026-08-04'
      },
      undefined,
      { baseSceneName: '2.4.1 非适用场景' }
    )
    assert.equal(owner.customerTags, '1')
    assert.equal(owner.actualDeliveryDate, '')
  })

  it('IT-EQ-PAY-003e: buildOwnerPayload retains date for a numeric equity scene', () => {
    const owner = buildOwnerPayload(
      {
        salesType: 0,
        customerTags: '1',
        actualDeliveryDate: '2026-08-04'
      },
      undefined,
      { baseSceneName: '', baseSceneCode: 110101 }
    )

    assert.equal(owner.actualDeliveryDate, '2026-08-04 00:00:00')
  })

  it('IT-EQ-PAY-004: parseInsureListToForm type 4 multiple', () => {
    const form = parseInsureListToForm([
      { type: 0, base: 10000 },
      { type: 4, base: 5000 },
      { type: 4, base: 3000 }
    ])
    assert.equal(form.business.base, 100)
    assert.equal(form.otherInsuranceList.length, 2)
  })

  it('IT-EQ-PAY-005: mapQuotationGoodsList field map', () => {
    const rows = mapQuotationGoodsList([
      {
        upgradeGoodsName: '权益A',
        upgradeGoodsServiceItemsList: [1],
        goodsInfo: { thumbUrl: 'http://x' }
      }
    ])
    assert.equal(rows[0].goodsName, '权益A')
    assert.equal(rows[0].titlePictureUrl, 'http://x')
  })

  it('IT-EQ-PAY-006: mapQuotationGoodsList isSeletct with outTradeNo', () => {
    const rows = mapQuotationGoodsList(
      [{ shopMinPrice: 100, upgradeGoodsName: 'G' }],
      { outTradeNo: 't', totalFee: 5000 }
    )
    assert.equal(rows[0].isSeletct, true)
  })

  it('IT-EQ-PAY-007: salesType mapping', () => {
    assert.equal(mapOwnerSalesTypeForApi('售后'), 1)
    assert.equal(mapOwnerSalesTypeForApi('售前'), 0)
    assert.equal(mapOwnerSalesTypeFromApi(1), '售后')
    assert.equal(mapOwnerSalesTypeForApi(2), 2)
    assert.equal(mapOwnerSalesTypeFromApi(2), 2)
  })

  it('IT-EQ-PAY-008: invalid standard quotation amounts fail instead of becoming zero', () => {
    assert.throws(
      () => buildInsurancePayload({ business: { base: '0.001' } }),
      /business\.base/
    )
    assert.throws(() => buildOwnerPayload({ price: '-1' }), /owner\.price/)
  })

  it('IT-EQ-PAY-009: minimum-price selection compares integer cents at the safe limit', () => {
    const [mapped] = mapQuotationGoodsList(
      [{ shopMinPrice: '90071992547409.90' }],
      { outTradeNo: 'trade-1', totalFee: '9007199254740989' }
    )
    assert.equal(mapped.isSeletct, true)
  })
})