simpleQuotationCatalog.helpers.test.mjs 9.06 KB
import assert from 'node:assert/strict'
import { describe, it } from 'node:test'

import * as catalogHelpers from '../pages/carQuotation/services/simpleQuotationCatalog.helpers.js'

const {
  buildSimpleQuotationSelectedGoods,
  normalizeSimpleQuotationCatalog,
  prepareSimpleQuotationDetail
} = catalogHelpers

describe('simpleQuotationCatalog.helpers', () => {
  it('IT-EQ-SIMPLE-SELECT-001: merges selected goods without losing edited strategy fields or mutating inputs', () => {
    assert.equal(
      typeof catalogHelpers.mergeSimpleQuotationGoodsSelection,
      'function'
    )

    const selected = [
      {
        goodsFlag: 1,
        count: 3,
        goods: { goodsId: 'single-1', price: 1000 }
      },
      {
        goodsFlag: 3,
        count: 2,
        packageGoods: { packageCode: 'package-1', price: 500 }
      },
      {
        goodsFlag: 2,
        count: 4,
        multipleSpecificationsGoods: { goods: { goodsId: 'sku-1', price: 300 } }
      }
    ]
    const existing = [
      {
        goodsFlag: 1,
        count: 1,
        goods: {
          goodsId: 'single-1',
          price: 800,
          discountType: 1,
          discountValue: 8
        }
      },
      {
        goodsFlag: 3,
        count: 1,
        packageGoods: {
          packageCode: 'package-1',
          price: 450,
          discountType: 2,
          discountAmount: 50
        }
      },
      {
        goodsFlag: 2,
        count: 1,
        multipleSpecificationsGoods: {
          goods: {
            goodsId: 'sku-1',
            price: 250,
            discountType: 2,
            discountAmount: 50
          }
        }
      }
    ]
    const selectedSnapshot = structuredClone(selected)
    const existingSnapshot = structuredClone(existing)

    const result = catalogHelpers.mergeSimpleQuotationGoodsSelection(
      selected,
      existing
    )

    assert.deepEqual(selected, selectedSnapshot)
    assert.deepEqual(existing, existingSnapshot)
    assert.equal(result[0].count, 3)
    assert.equal(result[0].goods.price, 800)
    assert.equal(result[0].goods.discountValue, 8)
    assert.equal(result[1].count, 2)
    assert.equal(result[1].packageGoods.discountAmount, 50)
    assert.equal(result[2].count, 4)
    assert.equal(
      result[2].multipleSpecificationsGoods.goods.discountAmount,
      50
    )
  })

  it('IT-EQ-SIMPLE-CATALOG-001: normalizes single, package and multi-spec catalog rows without mutating the API response', () => {
    const rows = [
      {
        id: 'single-row',
        goodsFlag: 1,
        goods: {
          merchantGoodsId: 'single-1',
          price: '12.34',
          sellPrice: 9999,
          upgradeGoodsAlias: '单品别名',
          upgradeGoodsName: '单品',
          position: '4大件',
          specificationTypeSequence: 'type',
          specificationNameSequence: '颜色;~;尺寸',
          specificationData: '红;~;大',
          upgradeGoodsServiceItemsList: [
            { serviceName: '服务A' },
            { serviceName: '服务B' }
          ],
          goodsInfo: { thumbUrl: 'https://example.test/single.png' }
        }
      },
      {
        id: 'zero-row',
        goodsFlag: 1,
        goods: {
          merchantGoodsId: 'zero-1',
          price: 0,
          sellPrice: '88.88',
          upgradeGoodsName: '赠品候选'
        }
      },
      {
        id: 'package-row',
        goodsFlag: 3,
        packageGoods: {
          packageCode: 'package-1',
          goodsList: [
            { merchantGoodsId: 'package-good-1', price: '1.25', sellPrice: 125 },
            { merchantGoodsId: 'package-good-2', price: '2.50', sellPrice: 250 }
          ]
        }
      },
      {
        id: 'multi-row',
        goodsFlag: 2,
        multipleSpecificationsGoods: {
          specificationGoodsMap: {
            '红;~;大': [
              { merchantGoodsId: 'sku-1', price: '3.50', sellPrice: 350 }
            ]
          },
          specificationList: [
            {
              specificationValueList: [
                { specificationValue: '红', show: true },
                { specificationValue: '蓝', show: false }
              ]
            }
          ]
        }
      }
    ]
    const snapshot = structuredClone(rows)

    const result = normalizeSimpleQuotationCatalog(rows)

    assert.deepEqual(rows, snapshot)
    assert.equal(result[0].goods.price, 1234)
    assert.equal(result[0].goods.serveName, '燃油车4大件服务')
    assert.equal(result[0].goods.specificationNameSequence, '颜色/尺寸')
    assert.equal(result[0].goods.specificationData, '红/大')
    assert.equal(result[0].goods.titlePictureUrl, 'https://example.test/single.png')
    assert.equal(result[1].goods.price, 0)
    assert.equal(result[2].packageGoods.goodsList[0].price, 125)
    assert.equal(result[2].packageGoods.sellPrice, 375)
    assert.equal(
      result[3].multipleSpecificationsGoods.specificationGoodsMap['红;~;大'][0].price,
      350
    )
    assert.deepEqual(
      result[3].multipleSpecificationsGoods.specificationList[0]
        .specificationSelectValueList,
      ['红']
    )
  })

  it('IT-EQ-SIMPLE-ECHO-001: rebuilds detail goods from the catalog and preserves authoritative detail prices', () => {
    const catalog = normalizeSimpleQuotationCatalog([
      {
        goodsFlag: 1,
        goods: {
          merchantGoodsId: 'single-1',
          price: '10.00',
          minSellPrice: 900,
          validMinSellPrice: true
        }
      },
      {
        goodsFlag: 3,
        packageGoods: {
          packageCode: 'package-1',
          goodsList: [
            {
              merchantGoodsId: 'package-good-1',
              price: '2.00',
              sellPrice: 200,
              minSellPrice: 180,
              validMinSellPrice: false
            },
            {
              merchantGoodsId: 'package-good-2',
              price: '3.00',
              sellPrice: 300
            }
          ]
        }
      },
      {
        goodsFlag: 2,
        multipleSpecificationsGoods: {
          specificationGoodsMap: {
            '红;~;大': [
              { merchantGoodsId: 'sku-1', price: '5.00', sellPrice: 500 }
            ]
          },
          specificationList: []
        }
      }
    ])
    const detailRows = [
      { goodsId: 'single-1', goodsName: '单品', goodsPrice: 1200, finalPrice: 1100 },
      {
        packageId: 'package-1',
        packageName: '套餐',
        goodsId: 'package-good-1',
        goodsName: '套餐一',
        goodsPrice: 210,
        finalPrice: 200
      },
      {
        packageId: 'package-1',
        packageName: '套餐',
        goodsId: 'package-good-2',
        goodsName: '套餐二',
        goodsPrice: 320,
        finalPrice: 300
      },
      {
        goodsId: 'sku-1',
        goodsName: '多规格',
        goodsPrice: 550,
        finalPrice: 525,
        specificationsValue: '红/大',
        discountType: 2,
        discountValue: 0.25,
        discountAmount: 25
      }
    ]
    const detailSnapshot = structuredClone(detailRows)
    const catalogSnapshot = structuredClone(catalog)

    const result = buildSimpleQuotationSelectedGoods(detailRows, catalog)

    assert.deepEqual(detailRows, detailSnapshot)
    assert.deepEqual(catalog, catalogSnapshot)
    assert.equal(result.length, 3)
    assert.equal(result[0].goods.price, 1200)
    assert.equal(result[0].goods.minSellPrice, 900)
    assert.equal(result[1].packageGoods.goodsList.length, 2)
    assert.equal(result[1].price, 530)
    assert.equal(result[2].multipleSpecificationsGoods.goods.price, 550)
    assert.equal(result[2].multipleSpecificationsGoods.goods.finalPrice, 525)
    assert.equal(result[2].multipleSpecificationsGoods.goods.specificationStr, '红/大')
  })

  it('IT-EQ-SIMPLE-ECHO-002: prepares edit and error-order detail state without dereferencing a missing response body', () => {
    assert.equal(prepareSimpleQuotationDetail(null, [], {}).ok, false)

    const detail = {
      id: 7,
      owner: { price: 12345, salesType: 1 },
      insurancePrice: 101,
      quotedPriceUpdate: 202,
      finalPrice: 303,
      payType: 2,
      outTradeNo: 'out-1',
      transactionNo: '',
      vdQuotationGoodsList: []
    }

    const result = prepareSimpleQuotationDetail(detail, [], {
      resetOrder: true,
      salesTypeOptions: [
        { optionId: 0, optionLabel: '售前' },
        { optionId: 1, optionLabel: '售后' }
      ]
    })

    assert.equal(result.ok, true)
    assert.equal(result.detail.owner.price, 123.45)
    assert.equal(result.detail.insurancePrice, 1.01)
    assert.equal(result.detail.owner.salesTypeLabel, '售后')
    assert.equal(result.detail.owner.ownerGender, '男')
    assert.equal(result.detail.resetOrder, true)
    assert.equal(result.detail.payType, 5)
    assert.equal(result.detail.payee, 0)
    assert.equal(result.detail.transactionNo, 'out-1')
    assert.equal('outTradeNo' in result.detail, false)
    assert.deepEqual(detail, {
      id: 7,
      owner: { price: 12345, salesType: 1 },
      insurancePrice: 101,
      quotedPriceUpdate: 202,
      finalPrice: 303,
      payType: 2,
      outTradeNo: 'out-1',
      transactionNo: '',
      vdQuotationGoodsList: []
    })
  })
})