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

import {
  normalizeSimplePaymentDocuments,
  shouldRequestSimplePaymentDocuments,
  validateSimpleQuotationSubmission
} from '../pages/carQuotation/services/simpleQuotationValidation.helpers.js'

const goods = item => [{ goodsFlag: 1, goods: { merchantGoodsId: 'g-1', ...item } }]

describe('simpleQuotationValidation.helpers', () => {
  it('IT-EQ-SIMPLE-VALID-001: returns the first existing submit message in business order', () => {
    assert.equal(
      validateSimpleQuotationSubmission({ vdQuotationGoodsList: [] }).message,
      '请选择增值车损险商品'
    )
    assert.equal(
      validateSimpleQuotationSubmission({
        vdQuotationGoodsList: goods({}),
        payType: 2,
        resetOrder: false
      }).message,
      '请选择4S店收款时间'
    )
    assert.equal(
      validateSimpleQuotationSubmission({
        vdQuotationGoodsList: goods({}),
        payType: 7,
        pay2Time: '2026-09-08',
        notes: '',
        resetOrder: false
      }).message,
      '备注不能为空'
    )
  })

  it('IT-EQ-SIMPLE-VALID-002: accepts an explicit zero discount and rejects missing strategy values', () => {
    assert.equal(
      validateSimpleQuotationSubmission({
        vdQuotationGoodsList: goods({ discountType: 1, discountValue: 0 }),
        orderType: 2,
        payType: 1,
        total: '1.00'
      }).ok,
      true
    )
    assert.equal(
      validateSimpleQuotationSubmission({
        vdQuotationGoodsList: goods({ discountType: 1, discountValue: '' }),
        orderType: 2,
        payType: 1,
        total: '1.00'
      }).message,
      '商品折扣值不能为空'
    )
  })

  it('IT-EQ-SIMPLE-VALID-003: preserves buy-gift and combined-payment strategy rules', () => {
    assert.equal(
      validateSimpleQuotationSubmission({
        vdQuotationGoodsList: goods({ discountType: 4 }),
        orderType: 3,
        payType: 1,
        total: '1.00'
      }).message,
      '买赠策略至少需要选择两个商品'
    )
    assert.equal(
      validateSimpleQuotationSubmission({
        vdQuotationGoodsList: [
          ...goods({ merchantGoodsId: 'g-1', discountType: 0 }),
          ...goods({ merchantGoodsId: 'g-2', discountType: 0 })
        ],
        orderType: 3,
        payType: 1,
        total: '1.00'
      }).message,
      '买赠策略需要至少有一个商品是赠品'
    )
    assert.equal(
      validateSimpleQuotationSubmission({
        vdQuotationGoodsList: goods({ discountType: 0 }),
        orderType: 5,
        payType: 1,
        total: '1.00'
      }).message,
      '组合付款需要至少有一个商品选择折扣'
    )
  })

  it('IT-EQ-SIMPLE-VALID-004: keeps amount validation and payment-document decisions deterministic', () => {
    assert.equal(
      validateSimpleQuotationSubmission({
        vdQuotationGoodsList: goods({}),
        orderType: 1,
        payType: 1,
        total: '0'
      }).message,
      '订单总价最低不能小于0.01元'
    )
    assert.equal(
      shouldRequestSimplePaymentDocuments({
        payType: 6,
        resetOrder: false,
        transactionNo: '',
        paymentDocList: []
      }),
      true
    )
    assert.equal(
      shouldRequestSimplePaymentDocuments({
        payType: 6,
        resetOrder: true,
        transactionNo: '',
        paymentDocList: []
      }),
      false
    )
  })

  it('IT-EQ-SIMPLE-VALID-005: normalizes popup strings and detail documents without mutating either input', () => {
    const documents = [
      'https://example.test/new.png',
      { paymentDocUrl: 'https://example.test/echo.png', type: 2 }
    ]
    const snapshot = structuredClone(documents)

    assert.deepEqual(normalizeSimplePaymentDocuments(documents), [
      { paymentDocUrl: 'https://example.test/new.png', type: 1 },
      { paymentDocUrl: 'https://example.test/echo.png', type: 1 }
    ])
    assert.deepEqual(documents, snapshot)
  })
})