goodsPriceEdit.helpers.test.mjs 5.67 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import {
  applyBoutiqueDiscount,
  compareBoutiqueMoney,
  computeBoutiqueLineAmounts,
  divideBoutiqueMoneyByQuantity,
  isMarketingGoodsPriceLocked,
  isBoutiqueErrorOrderBalanced,
  isBoutiqueTotalAtLeastOneCent,
  multiplyBoutiqueMoney,
  normalizeBoutiqueMoney,
  sumBoutiqueAssociatedFeeCents
} from '../services/goodsPriceEdit.helpers.js'

describe('goodsPriceEdit.helpers', () => {
  it('compares money in integer cents without float coercion', () => {
    assert.deepEqual(compareBoutiqueMoney('0.29', 0.29000000000000004), {
      ok: true,
      value: 0
    })
    assert.equal(compareBoutiqueMoney('invalid', 1).ok, false)
  })

  it('divides authoritative totals with explicit half-up cent rounding', () => {
    assert.deepEqual(divideBoutiqueMoneyByQuantity('9999.95', 5), {
      ok: true,
      value: 1999.99,
      cents: 199999
    })
    assert.equal(divideBoutiqueMoneyByQuantity('1', 3).value, 0.33)
    assert.equal(divideBoutiqueMoneyByQuantity('0.02', 3).value, 0.01)
    assert.equal(divideBoutiqueMoneyByQuantity('1', 0).ok, false)
  })

  it('applies ratio, fixed and gift discounts in integer cents', () => {
    assert.deepEqual(applyBoutiqueDiscount('0.29', 1, '8.5'), {
      ok: true,
      finalPrice: 0.25,
      discountAmount: 0.04,
      finalCents: 25,
      discountCents: 4
    })
    assert.equal(applyBoutiqueDiscount('10', 2, '0.01').finalPrice, 9.99)
    assert.equal(applyBoutiqueDiscount('10', 4).finalPrice, 0)
    assert.equal(applyBoutiqueDiscount('10', 1, '10.001').ok, false)
  })

  it('locks price when marketingFlag and activityId are both present', () => {
    assert.equal(
      isMarketingGoodsPriceLocked({
        marketingFlag: 1,
        activityId: 9,
        goods: {}
      }),
      true
    )
  })

  it('does not lock when marketingFlag is inactive', () => {
    assert.equal(
      isMarketingGoodsPriceLocked({
        marketingFlag: 0,
        activityId: 9
      }),
      false
    )
  })

  it('does not lock when activityId is missing', () => {
    assert.equal(
      isMarketingGoodsPriceLocked({
        marketingFlag: 1,
        goods: { marketingFlag: 1 }
      }),
      false
    )
  })

  it('reads marketing meta from goods body', () => {
    assert.equal(
      isMarketingGoodsPriceLocked({
        goods: { marketingFlag: 1, activityId: 3 }
      }),
      true
    )
  })

  it('normalizes exact yuan values and API numeric float tails to cents', () => {
    assert.deepEqual(normalizeBoutiqueMoney('0.29'), {
      ok: true,
      value: 0.29,
      cents: 29
    })
    assert.deepEqual(normalizeBoutiqueMoney(1999.9900000000002), {
      ok: true,
      value: 1999.99,
      cents: 199999
    })
  })

  it('rejects missing, negative, non-finite and over-precision user money', () => {
    for (const value of ['', null, undefined, '-1', '0.001', Infinity]) {
      assert.equal(normalizeBoutiqueMoney(value).ok, false, String(value))
    }
  })

  it('multiplies in integer cents and rejects zero quantity', () => {
    assert.deepEqual(multiplyBoutiqueMoney('1999.99', 5), {
      ok: true,
      value: 9999.95,
      cents: 999995
    })
    assert.equal(multiplyBoutiqueMoney('1.00', 0).ok, false)
    assert.deepEqual(multiplyBoutiqueMoney('1.00', undefined), {
      ok: true,
      value: 1,
      cents: 100
    })
  })

  it('uses an authoritative line total before unit-price multiplication', () => {
    assert.deepEqual(
      computeBoutiqueLineAmounts({
        unitPrice: '1999.99',
        quantity: 5,
        discountType: 3,
        authoritativeTotalPrice: '9999.96'
      }),
      {
        ok: true,
        unitPrice: 1999.99,
        finalPrice: 9999.96,
        singleProductFinalPrice: 1999.99,
        discountAmount: 0
      }
    )
  })

  it('gift line remains zero regardless of source price', () => {
    const result = computeBoutiqueLineAmounts({
      unitPrice: '88.88',
      quantity: 2,
      discountType: 4
    })
    assert.equal(result.ok, true)
    assert.equal(result.finalPrice, 0)
    assert.equal(result.singleProductFinalPrice, 0)
  })

  it('balances error-order yuan totals against an associated fee in cents', () => {
    assert.deepEqual(
      isBoutiqueErrorOrderBalanced({
        originalPrice: '0.10',
        associatedFeeCents: 20,
        currentTotal: '0.30'
      }),
      { ok: true, value: true }
    )
    assert.equal(
      isBoutiqueErrorOrderBalanced({
        originalPrice: '0.10',
        associatedFeeCents: 20,
        currentTotal: '0.31'
      }).value,
      false
    )
  })

  it('sums associated fee cents exactly across safe integers and digit strings', () => {
    const input = [1, '2', '0003', 9007199254740980]
    const snapshot = structuredClone(input)

    assert.deepEqual(sumBoutiqueAssociatedFeeCents(input), {
      ok: true,
      value: 9007199254740986
    })
    assert.deepEqual(input, snapshot)
    assert.deepEqual(sumBoutiqueAssociatedFeeCents([]), { ok: true, value: 0 })
  })

  it('rejects invalid, negative, fractional and overflowing associated fees', () => {
    for (const values of [
      [''],
      [null],
      [undefined],
      ['1.5'],
      [-1],
      ['-1'],
      [Number.MAX_SAFE_INTEGER, 1],
      ['9007199254740992']
    ]) {
      assert.equal(sumBoutiqueAssociatedFeeCents(values).ok, false, String(values))
    }
    assert.equal(sumBoutiqueAssociatedFeeCents('1').ok, false)
  })

  it('validates the one-cent minimum without Number coercion', () => {
    assert.deepEqual(isBoutiqueTotalAtLeastOneCent('0.01'), {
      ok: true,
      value: true
    })
    assert.equal(isBoutiqueTotalAtLeastOneCent(0).value, false)
    assert.equal(isBoutiqueTotalAtLeastOneCent('invalid').ok, false)
  })
})