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

import { calculateSimpleQuotationTotal } from '../pages/carQuotation/services/simpleQuotationTotal.helpers.js'

describe('simpleQuotationTotal.helpers', () => {
  it('IT-EQ-SIMPLE-TOTAL-001: returns an exact zero total for an empty selection', () => {
    assert.deepEqual(calculateSimpleQuotationTotal([]), {
      goodsList: [],
      totalCents: 0,
      totalYuan: '0.00'
    })
  })

  it('IT-EQ-SIMPLE-TOTAL-002: adds normal, multi-spec and gift rows as integer cents', () => {
    const goodsList = [
      { count: 2, goods: { price: 101 } },
      {
        count: 3,
        multipleSpecificationsGoods: { goods: { price: 33 } }
      },
      {
        count: 99,
        goods: { price: 999, finalPrice: 0, discountType: 4 }
      }
    ]
    const snapshot = structuredClone(goodsList)

    const result = calculateSimpleQuotationTotal(goodsList)

    assert.equal(result.totalCents, 301)
    assert.equal(result.totalYuan, '3.01')
    assert.deepEqual(goodsList, snapshot)
    assert.notEqual(result.goodsList, goodsList)
  })

  it('IT-EQ-SIMPLE-TOTAL-003: recalculates package child discounts before applying quantity', () => {
    const goodsList = [
      {
        count: 2,
        price: 999999,
        packageGoods: {
          price: 999999,
          goodsList: [
            {
              price: 125,
              finalPrice: 999,
              discountType: 2,
              discountValue: '0.05'
            },
            {
              price: 250,
              finalPrice: 999,
              discountType: 1,
              discountValue: '9'
            }
          ]
        }
      }
    ]
    const snapshot = structuredClone(goodsList)

    const result = calculateSimpleQuotationTotal(goodsList)

    assert.equal(result.totalCents, 690)
    assert.equal(result.totalYuan, '6.90')
    assert.equal(result.goodsList[0].packageGoods.price, 345)
    assert.equal(result.goodsList[0].price, 345)
    assert.deepEqual(goodsList, snapshot)
  })

  it('IT-EQ-SIMPLE-TOTAL-006: recalculates a normal row from its current base price and discount', () => {
    const goodsList = [
      {
        count: 2,
        goods: {
          price: 101,
          finalPrice: 999,
          discountType: 1,
          discountValue: '8.5'
        }
      }
    ]
    const snapshot = structuredClone(goodsList)

    const result = calculateSimpleQuotationTotal(goodsList)

    assert.equal(result.totalCents, 172)
    assert.equal(result.totalYuan, '1.72')
    assert.equal(result.goodsList[0].goods.finalPrice, 86)
    assert.deepEqual(goodsList, snapshot)
  })

  it('IT-EQ-SIMPLE-TOTAL-004: formats a large safe integer total without a float tail', () => {
    const result = calculateSimpleQuotationTotal([
      { count: 1, goods: { price: 9007199254740900 } }
    ])

    assert.equal(result.totalCents, 9007199254740900)
    assert.equal(result.totalYuan, '90071992547409.00')
    assert.equal(Number.isSafeInteger(result.totalCents), true)
  })

  it('IT-EQ-SIMPLE-TOTAL-005: rejects malformed strategy totals instead of producing NaN or silently zeroing them', () => {
    assert.throws(
      () =>
        calculateSimpleQuotationTotal([
          {
            packageGoods: {
              price: 100,
              goodsList: [{ price: 'invalid' }]
            }
          }
        ]),
      /商品金额/
    )
  })

  it('IT-EQ-SIMPLE-TOTAL-007: rejects invalid normal and multi-spec cent prices instead of folding them to zero', () => {
    for (const goodsList of [
      [{ count: 1, goods: { price: 'invalid' } }],
      [
        {
          count: 1,
          multipleSpecificationsGoods: { goods: { price: '12.5' } }
        }
      ]
    ]) {
      assert.throws(
        () => calculateSimpleQuotationTotal(goodsList),
        RangeError
      )
    }
  })

  it('IT-EQ-SIMPLE-TOTAL-008: rejects invalid quantities instead of treating them as a zero subtotal', () => {
    for (const count of [-1, '1.5', 'invalid', Number.MAX_SAFE_INTEGER + 1]) {
      assert.throws(
        () =>
          calculateSimpleQuotationTotal([
            { count, goods: { price: 100 } }
          ]),
        RangeError
      )
    }
  })

  it('IT-EQ-SIMPLE-TOTAL-009: rejects a count-price product beyond the safe integer range', () => {
    assert.throws(
      () =>
        calculateSimpleQuotationTotal([
          { count: 2, goods: { price: Number.MAX_SAFE_INTEGER } }
        ]),
      RangeError
    )
  })

  it('IT-EQ-SIMPLE-TOTAL-010: preserves explicit zero prices, gifts and zero quantities as valid zero totals', () => {
    const result = calculateSimpleQuotationTotal([
      { count: 2, goods: { price: 0 } },
      { count: 3, goods: { price: 999, discountType: 4 } },
      { count: 0, goods: { price: 123 } }
    ])

    assert.equal(result.totalCents, 0)
    assert.equal(result.totalYuan, '0.00')
  })
})