customerDailyFields.integration.test.mjs 4.67 KB
import assert from 'node:assert/strict'
import { register } from 'node:module'
import test from 'node:test'

register(
  '../../../../tests/work-order-migration/shims/create-order-context-loader.mjs',
  import.meta.url
)

let pageConfig
globalThis.getApp = () => ({ globalData: {} })
globalThis.Page = config => {
  pageConfig = config
}
globalThis.require = specifier => {
  if (specifier === '~/utils/timeFormat/time-utils.js') {
    return { getTodayDateStr: () => '2026-08-26' }
  }
  throw new Error(`Unexpected CommonJS dependency in page test: ${specifier}`)
}
globalThis.wx = {
  getAccountInfoSync: () => ({
    miniProgram: { appId: 'test-app-id', envVersion: 'develop' }
  }),
  getDeviceInfo: () => ({ platform: 'devtools' }),
  getWindowInfo: () => ({ screenWidth: 375, windowWidth: 375 }),
  getAppBaseInfo: () => ({ host: { env: '' }, SDKVersion: 'test' }),
  getSystemInfoSync: () => ({ platform: 'devtools' }),
  getStorageSync: () => '',
  $util: {
    getUserInfo: () => ({}),
    showLoading() {}
  }
}

await import(`../createOrder.js?customer-daily-fields=${Date.now()}`)

function createPage(currentMerchantId, cachedBrandMerchantId = currentMerchantId) {
  return {
    ...pageConfig,
    data: {
      ...structuredClone(pageConfig.data),
      orderObj: {
        merchantId: currentMerchantId,
        baseSceneCode: 100101,
        baseSceneName: '1.1 轻改场景',
        customer: {
          customerSex: 1,
          experienceFlag: 'N',
          salesType: 0
        }
      },
      customerDailyFieldMerchantBrands: [
        {
          id: 2978,
          name: '鸿蒙智行',
          childs: [{ id: 3454, name: 'AITO 问界' }]
        }
      ],
      customerDailyFieldBrandMerchantId: cachedBrandMerchantId,
      showActualDeliveryDate: false
    },
    setData(patch, callback) {
      Object.assign(this.data, patch)
      callback?.()
    },
    loadBoutiqueCustomerTagOptions() {},
    echoCustomerProfileIfNeeded: async () => {},
    getSelectOrderGoodsList() {}
  }
}

function createSelectedCustomer(merchantId) {
  return {
    merchantId,
    merchantName: `门店-${merchantId}`,
    remark: '已有备注',
    customer: {
      customerSex: 1,
      experienceFlag: 'N',
      salesType: 3,
      standNo: 'LSVAA4182E2123456',
      carSeriesName: '问界M5',
      actualDeliveryDate: ''
    }
  }
}

test('IT-WO-DAILY-020: customer profile immediately reuses brands only for the same merchant', () => {
  const sameMerchantPage = createPage(10001)
  pageConfig.setOrderObj.call(sameMerchantPage, createSelectedCustomer(10001))

  assert.equal(sameMerchantPage.data.orderObj.customer.salesType, 3)
  assert.equal(sameMerchantPage.data.showActualDeliveryDate, true)

  const changedMerchantPage = createPage(10001)
  pageConfig.setOrderObj.call(
    changedMerchantPage,
    createSelectedCustomer(20002)
  )

  assert.equal(changedMerchantPage.data.orderObj.customer.salesType, 3)
  assert.equal(changedMerchantPage.data.showActualDeliveryDate, false)

  const pendingBrandPage = createPage(20002, 10001)
  pageConfig.setOrderObj.call(
    pendingBrandPage,
    createSelectedCustomer(20002)
  )

  assert.equal(pendingBrandPage.data.orderObj.customer.salesType, 3)
  assert.equal(pendingBrandPage.data.showActualDeliveryDate, false)
})

test('IT-WO-DAILY-021: resolved brand context records its merchant provenance', async () => {
  globalThis.__WORK_ORDER_MOCK_API__ = {
    getMerchantCarBrandList: () => ({
      code: 0,
      data: {
        data: [
          {
            carBrandList: [
              {
                id: 2978,
                name: '鸿蒙智行',
                childs: [{ id: 3454, name: 'AITO 问界' }]
              }
            ]
          }
        ]
      }
    })
  }
  const page = createPage(10001, '')
  page.loadBoutiqueCustomerTagOptions =
    pageConfig.loadBoutiqueCustomerTagOptions

  try {
    await page.loadBoutiqueCustomerTagOptions(10001)

    assert.equal(page.data.customerDailyFieldBrandMerchantId, 10001)
  } finally {
    delete globalThis.__WORK_ORDER_MOCK_API__
  }
})

test('IT-WO-DAILY-022: opening customer source picker selects the current source', () => {
  const page = createPage(10001)
  page.data.orderObj.customer.salesType = 3

  pageConfig.toSelectCustomerSource.call(page)

  assert.equal(page.data.showSource, true)
  assert.equal(page.data.customerSourcePickerIndex, 1)

  pageConfig.onCloseSource.call(page)
  page.data.orderObj.customer.salesType = '1'
  pageConfig.toSelectCustomerSource.call(page)

  assert.equal(page.data.customerSourcePickerIndex, 2)

  pageConfig.onCloseSource.call(page)
  page.data.orderObj.customer.salesType = 99
  pageConfig.toSelectCustomerSource.call(page)

  assert.equal(page.data.customerSourcePickerIndex, 0)
})