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

describe('customerProfileEffects', () => {
  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, '')
  })
})