quotationPricing.workflow.test.mjs 2.64 KB
import assert from 'node:assert/strict'
import { describe, it } from 'node:test'
import {
  buildPricingPayload,
  requestQuotationPrice,
  validateRecordedPriceLimit
} from '../pages/carQuotation/services/quotationPricing.workflow.js'

describe('quotationPricing.workflow', () => {
  it('accepts only numeric or string zero success codes', async () => {
    for (const code of [0, '0']) {
      const result = await requestQuotationPrice(
        { goodsId: 12 },
        { priceCounter: async () => ({ code, data: { quotedPriceUpdate: 9900 } }) }
      )
      assert.equal(result.status, 'success')
      assert.equal(result.data.quotedPriceUpdate, 9900)
    }

    for (const code of [1, '1', true, undefined]) {
      const result = await requestQuotationPrice(
        { goodsId: 12 },
        { priceCounter: async () => ({ code, message: '不可报价' }) }
      )
      assert.deepEqual(result, {
        status: 'business-error',
        data: undefined,
        message: '不可报价',
        code
      })
    }
  })

  it('preserves payload identity and propagates reject', async () => {
    const payload = { goodsId: 12, owner: { owner: '李四' } }
    let received
    await assert.rejects(
      requestQuotationPrice(payload, {
        priceCounter: async value => {
          received = value
          throw new Error('counter unavailable')
        }
      }),
      /counter unavailable/
    )
    assert.equal(received, payload)
  })

  it('builds a new payload without mutating owner or quotation', () => {
    const owner = { owner: '王五', buyCarTime: '2026-01-01', salesType: '售后' }
    const quotation = {
      owner: { stale: true },
      quotedPriceUpdate: 100,
      insureList: [{ type: 3, appreciation: 1 }]
    }
    const result = buildPricingPayload({
      owner,
      quotation,
      goods: { id: 8, goodsName: '延保', appreciation: 8 }
    })

    assert.deepEqual(result.owner, { owner: '王五', salesType: 1 })
    assert.equal(result.goodsId, 8)
    assert.equal(result.goodsName, '延保')
    assert.equal(result.quotedPriceUpdate, undefined)
    assert.deepEqual(owner, {
      owner: '王五',
      buyCarTime: '2026-01-01',
      salesType: '售后'
    })
    assert.deepEqual(quotation.insureList, [{ type: 3, appreciation: 1 }])
  })

  it('compares recorded totals and minimum equity prices as integer cents', () => {
    assert.deepEqual(validateRecordedPriceLimit(100, 100), { ok: true })
    assert.deepEqual(validateRecordedPriceLimit('101', 100), {
      ok: false,
      reason: 'above-recorded-total'
    })
    assert.deepEqual(validateRecordedPriceLimit('1.01', 100), {
      ok: false,
      reason: 'invalid'
    })
  })
})