quotationMoney.helpers.test.mjs 3.5 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import {
  divideByHundred,
  multipliedByHundred,
  divideByHundredObj,
  multipliedByHundredObj,
  DEFAULT_NEED_CHANGE_LIST,
  yuanToCentInteger,
  centToYuanText,
  compareCentAmounts,
  validateQuotationOrderTotal
} from '../pages/carQuotation/services/quotationMoney.helpers.js'

describe('quotationMoney.helpers', () => {
  it('IT-EQ-MNY-001: divideByHundred 10000 → 100', () => {
    assert.equal(divideByHundred(10000), 100)
  })

  it('IT-EQ-MNY-002: multipliedByHundred 100 → 10000', () => {
    assert.equal(multipliedByHundred(100), 10000)
  })

  it('IT-EQ-MNY-003: NaN returns 0', () => {
    assert.equal(divideByHundred('abc'), 0)
    assert.equal(multipliedByHundred('abc'), 0)
  })

  it('IT-EQ-MNY-004: divideByHundredObj only needChangeList keys', () => {
    const out = divideByHundredObj(
      { price: 10000, name: 'x' },
      DEFAULT_NEED_CHANGE_LIST
    )
    assert.equal(out.price, 100)
    assert.equal(out.name, 'x')
  })

  it('IT-EQ-MNY-005: multipliedByHundredObj', () => {
    const out = multipliedByHundredObj({ base: 1.5 }, DEFAULT_NEED_CHANGE_LIST)
    assert.equal(out.base, 150)
  })

  it('IT-EQ-MNY-006: exact decimal yuan converts to integer cents without float tails', () => {
    assert.deepEqual(yuanToCentInteger('0.29'), { ok: true, value: 29 })
    assert.deepEqual(yuanToCentInteger('1000000.01'), {
      ok: true,
      value: 100000001
    })
    assert.deepEqual(yuanToCentInteger(0), { ok: true, value: 0 })
  })

  it('IT-EQ-MNY-007: invalid, negative and over-precision yuan are rejected explicitly', () => {
    for (const value of ['', null, undefined, 'abc', '-1', '0.001']) {
      assert.equal(yuanToCentInteger(value).ok, false, String(value))
    }
  })

  it('IT-EQ-MNY-008: cents render as an exact two-decimal display string', () => {
    assert.deepEqual(centToYuanText(1), { ok: true, value: '0.01' })
    assert.deepEqual(centToYuanText('10000'), {
      ok: true,
      value: '100.00'
    })
    assert.equal(centToYuanText('1.2').ok, false)
    assert.equal(centToYuanText(-1).ok, false)
  })

  it('IT-EQ-MNY-009: integer-cent comparison never converts through floating point', () => {
    assert.deepEqual(compareCentAmounts('9007199254740990', '9007199254740989'), {
      ok: true,
      value: 1
    })
    assert.equal(compareCentAmounts('', 0).ok, false)
  })

  it('IT-EQ-MNY-010: validates normal and error-order totals in integer cents', () => {
    assert.deepEqual(
      validateQuotationOrderTotal({ resetOrder: false, total: '0.01' }),
      { ok: true }
    )
    assert.deepEqual(
      validateQuotationOrderTotal({ resetOrder: false, total: '0' }),
      { ok: false, reason: 'below-minimum' }
    )
    assert.deepEqual(
      validateQuotationOrderTotal({
        resetOrder: true,
        originalPrice: '0.10',
        associatedFeeCents: 20,
        total: '0.30'
      }),
      { ok: true }
    )
    assert.equal(
      validateQuotationOrderTotal({
        resetOrder: true,
        originalPrice: '0.10',
        associatedFeeCents: 19,
        total: '0.30'
      }).reason,
      'unbalanced'
    )
  })

  it('IT-EQ-MNY-011: rejects malformed totals and accepts API float tails', () => {
    assert.deepEqual(yuanToCentInteger(1999.9900000000002), {
      ok: true,
      value: 199999
    })
    for (const total of ['', 'invalid', '-1', '0.001', Infinity]) {
      assert.equal(
        validateQuotationOrderTotal({ resetOrder: false, total }).reason,
        'invalid'
      )
    }
  })
})