customerProfileEffects.test.mjs 7.65 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import {
  isRemarkEmpty,
  isStandNoEmpty,
  isCarModelEmpty,
  resolveCustomerId,
  shouldFetchCustomerProfile,
  parseCustomerProfileResponse,
  buildCustomerProfileEchoPatch,
  buildCustomerOnlyOrder
} from '../services/customerProfileEffects.js'

describe('customerProfileEffects', () => {
  it('buildCustomerOnlyOrder only imports customer, order code and remark', () => {
    const current = {
      lowPriceFlag: 1,
      merchantId: 100,
      userId: 'CURRENT-SALES',
      employeeName: '当前销售顾问',
      selectPayType: 3,
      payeeType: 1,
      remark: '旧客户备注',
      subOrderList: [{ goodsCode: 'CURRENT' }],
      customer: { customerId: 1, customerName: '旧客户' }
    }
    const selected = {
      orderCode: 'ARCHIVE-ORDER-001',
      userId: 'HISTORY-SALES',
      employeeName: '历史销售顾问',
      baseSceneCode: 'HISTORY-SCENE',
      baseSceneName: '历史场景',
      selectPayType: 5,
      payeeType: 0,
      remark: '新客户备注',
      subOrderList: [{ goodsCode: 'HISTORY' }],
      packageGoodsList: [{ packageCode: 'HISTORY-PKG' }],
      customer: {
        customerId: 2,
        customerName: '新客户',
        customerPhone: '13800000000',
        standNo: 'VIN-NEW'
      }
    }

    const result = buildCustomerOnlyOrder(current, selected)

    assert.notEqual(result, current)
    assert.notEqual(result.customer, selected.customer)
    assert.deepEqual(result.customer, {
      ...selected.customer,
      customerSex: 1,
      experienceFlag: 'N',
      salesType: 0
    })
    assert.equal(result.selectPayType, 3)
    assert.equal(result.payeeType, 1)
    assert.equal(result.orderCode, 'ARCHIVE-ORDER-001')
    assert.equal(result.remark, '新客户备注')
    assert.equal(result.userId, 'CURRENT-SALES')
    assert.equal(result.employeeName, '当前销售顾问')
    assert.equal(result.baseSceneCode, undefined)
    assert.equal(result.baseSceneName, undefined)
    assert.deepEqual(result.subOrderList, [{ goodsCode: 'CURRENT' }])
    assert.equal(result.packageGoodsList, undefined)
    assert.deepEqual(current.customer, {
      customerId: 1,
      customerName: '旧客户'
    })
  })

  it('isRemarkEmpty treats blank and whitespace as empty', () => {
    assert.equal(isRemarkEmpty(''), true)
    assert.equal(isRemarkEmpty('   '), true)
    assert.equal(isRemarkEmpty(null), true)
    assert.equal(isRemarkEmpty(undefined), true)
    assert.equal(isRemarkEmpty('有备注'), false)
  })

  it('isStandNoEmpty treats blank and whitespace as empty', () => {
    assert.equal(isStandNoEmpty(''), true)
    assert.equal(isStandNoEmpty('  '), true)
    assert.equal(isStandNoEmpty('LSVAA4182E2123456'), false)
  })

  it('isCarModelEmpty checks carSeriesName', () => {
    assert.equal(isCarModelEmpty({}), true)
    assert.equal(isCarModelEmpty({ carBrand: '宝马' }), true)
    assert.equal(isCarModelEmpty({ carSeriesName: '3系' }), false)
    assert.equal(isCarModelEmpty({ carSeriesName: '  ' }), true)
  })

  it('resolveCustomerId prefers customerId then id', () => {
    assert.equal(resolveCustomerId({ customerId: 1, id: 2 }), 1)
    assert.equal(resolveCustomerId({ id: 2 }), 2)
    assert.equal(resolveCustomerId(null), undefined)
    assert.equal(resolveCustomerId({}), undefined)
  })

  it('shouldFetchCustomerProfile when any of remark standNo car model is empty', () => {
    const full = {
      remark: '备注',
      customer: {
        standNo: 'LSVAA4182E2123456',
        carSeriesName: '3系'
      }
    }
    assert.equal(shouldFetchCustomerProfile(full), false)

    assert.equal(
      shouldFetchCustomerProfile({ remark: '', customer: full.customer }),
      true
    )
    assert.equal(
      shouldFetchCustomerProfile({
        remark: 'x',
        customer: { standNo: '', carSeriesName: '3系' }
      }),
      true
    )
    assert.equal(
      shouldFetchCustomerProfile({
        remark: 'x',
        customer: { standNo: 'VIN', carSeriesName: '' }
      }),
      true
    )
    assert.equal(shouldFetchCustomerProfile(null), false)
  })

  it('parseCustomerProfileResponse accepts string code "0"', () => {
    const res = {
      code: '0',
      data: {
        id: 33842,
        remark: '888888888',
        standNo: '33333333333333333',
        carBrand: 'AITO 问界',
        carChildsBrandName: '问界M5',
        carSeriesName: '问界M5',
        carBrandId: 3454,
        carChildsBrandId: 1149146,
        carSeriesId: 1149146
      }
    }
    assert.deepEqual(parseCustomerProfileResponse(res), {
      remark: '888888888',
      standNo: '33333333333333333',
      carBrand: 'AITO 问界',
      carChildsBrandName: '问界M5',
      carSeriesName: '问界M5',
      carBrandId: 3454,
      carChildsBrandId: 1149146,
      carSeriesId: 1149146
    })
  })

  it('parseCustomerProfileResponse reads profile fields', () => {
    const res = {
      code: 0,
      data: {
        remark: '  备注  ',
        standNo: ' LSVAA4182E2123456 ',
        carBrand: '宝马',
        carChildsBrandName: '华晨宝马',
        carSeriesName: '3系',
        carBrandId: 1,
        carChildsBrandId: 2,
        carSeriesId: 3
      }
    }
    assert.deepEqual(parseCustomerProfileResponse(res), {
      remark: '备注',
      standNo: 'LSVAA4182E2123456',
      carBrand: '宝马',
      carChildsBrandName: '华晨宝马',
      carSeriesName: '3系',
      carBrandId: 1,
      carChildsBrandId: 2,
      carSeriesId: 3
    })
    assert.equal(parseCustomerProfileResponse({ code: 1, data: {} }), null)
    assert.equal(parseCustomerProfileResponse({ code: 0, data: {} }), null)
    assert.equal(parseCustomerProfileResponse(null), null)
  })

  it('buildCustomerProfileEchoPatch only fills empty fields', () => {
    const orderObj = {
      remark: '已有备注',
      customer: {
        standNo: '',
        carSeriesName: ''
      }
    }
    const profile = {
      remark: '接口备注',
      standNo: 'LSVAA4182E2123456',
      carBrand: '宝马',
      carChildsBrandName: '华晨宝马',
      carSeriesName: '3系',
      carBrandId: 10,
      carChildsBrandId: 20,
      carSeriesId: 30
    }

    const { patch, echoedStandNo } = buildCustomerProfileEchoPatch(
      orderObj,
      profile
    )

    assert.equal(patch['orderObj.remark'], undefined)
    assert.equal(patch['orderObj.customer.standNo'], 'LSVAA4182E2123456')
    assert.equal(patch['orderObj.customer.carBrand'], '宝马')
    assert.equal(patch['orderObj.customer.carChildsBrandName'], '华晨宝马')
    assert.equal(patch['orderObj.customer.carSeriesName'], '3系')
    assert.equal(patch['orderObj.customer.carBrandId'], 10)
    assert.equal(patch['orderObj.customer.carChildsBrandId'], 20)
    assert.equal(patch['orderObj.customer.carSeriesId'], 30)
    assert.equal(echoedStandNo, 'LSVAA4182E2123456')
  })

  it('buildCustomerProfileEchoPatch patches remark with word count', () => {
    const { patch } = buildCustomerProfileEchoPatch(
      { remark: '', customer: { standNo: 'VIN', carSeriesName: 'X' } },
      { remark: 'abc', standNo: '', carBrand: '', carChildsBrandName: '', carSeriesName: '' }
    )
    assert.deepEqual(patch, {
      'orderObj.remark': 'abc',
      currentWordNumber: 3
    })
  })

  it('buildCustomerProfileEchoPatch returns empty patch when nothing to fill', () => {
    const orderObj = {
      remark: 'x',
      customer: { standNo: 'VIN', carSeriesName: '车系' }
    }
    const { patch, echoedStandNo } = buildCustomerProfileEchoPatch(orderObj, {
      remark: 'y',
      standNo: 'OTHER',
      carBrand: '宝马',
      carChildsBrandName: '子品牌',
      carSeriesName: '3系'
    })
    assert.deepEqual(patch, {})
    assert.equal(echoedStandNo, '')
  })
})