fieldOptionsService.test.mjs 5.26 KB
/**
 * fieldOptionsService 进程内缓存 — useCache 开关
 */
import { describe, it, afterEach } from 'node:test'
import assert from 'node:assert/strict'
import { createRequire } from 'node:module'

const require = createRequire(import.meta.url)
const { NEGOTIATION_DAILY_API_SWITCH } = require('../config/apiSwitch.js')
const { fetchFieldOptions, clearFieldOptionsCache } = require('../api/fieldOptionsService.js')
const { __setOrderListApi, __setEmployeeListApi } = require('../api/fieldOptionProviders.js')

const ORDER_LIST_PARAMS = {
	optionsKey: 'orderList',
	reportType: 'negotiation',
	merchantId: 'shop-neg',
	staffUuid: 'staff-uuid-1',
	reportDate: '2026-06-02',
	saleDate: '2026-06-15',
	carBrandTypeFlag: '2',
	groupBrandIds: [3067],
	sceneList: [{ baseSceneCode: 110201 }]
}

const STORE_STAFF_LIST_PARAMS = {
	optionsKey: 'storeStaffList',
	reportType: 'store',
	yqMerchantId: 'yq-shop-1',
	merchantId: 'shop-1',
	staffUuid: 'staff-uuid-1',
	reportDate: '2026-06-02'
}

describe('fieldOptionsService', () => {
	afterEach(() => {
		clearFieldOptionsCache()
		__setOrderListApi(null)
		__setEmployeeListApi(null)
	})

	it('IT-NEG-012: useCache false always hits adapter (orderList popup refresh)', async () => {
		NEGOTIATION_DAILY_API_SWITCH.fieldOptions = 'real'
		let callCount = 0
		__setOrderListApi({
			getGoodsOrderOutputValueList: async () => {
				callCount += 1
				return {
					code: 0,
					message: 'ok',
					data: [{ orderCode: `order-${callCount}`, productName: '延保订单' }]
				}
			}
		})

		const first = await fetchFieldOptions(ORDER_LIST_PARAMS, { useCache: false })
		const second = await fetchFieldOptions(ORDER_LIST_PARAMS, { useCache: false })

		assert.equal(callCount, 2)
		assert.equal(first.data[0].value, 'order-1')
		assert.equal(second.data[0].value, 'order-2')
	})

	it('IT-NEG-012b: useCache true reuses cached orderList result', async () => {
		NEGOTIATION_DAILY_API_SWITCH.fieldOptions = 'real'
		let callCount = 0
		__setOrderListApi({
			getGoodsOrderOutputValueList: async () => {
				callCount += 1
				return {
					code: 0,
					message: 'ok',
					data: [{ orderCode: 'order-cached', productName: '延保订单' }]
				}
			}
		})

		const first = await fetchFieldOptions(ORDER_LIST_PARAMS, { useCache: true })
		const second = await fetchFieldOptions(ORDER_LIST_PARAMS, { useCache: true })

		assert.equal(callCount, 1)
		assert.equal(first.data[0].value, 'order-cached')
		assert.equal(second.data[0].value, 'order-cached')
	})

	it('IT-FIELD-OPT-009: useCache false always hits adapter (storeStaffList popup refresh)', async () => {
		const { STORE_DAILY_API_SWITCH } = require('../config/apiSwitch.js')
		STORE_DAILY_API_SWITCH.fieldOptions = 'real'
		let callCount = 0
		__setEmployeeListApi({
			getEmployeeList: async () => {
				callCount += 1
				return {
					code: 0,
					message: 'ok',
					data: [{ staffName: `员工${callCount}`, uuid: `u-${callCount}` }]
				}
			}
		})

		const first = await fetchFieldOptions(STORE_STAFF_LIST_PARAMS, { useCache: false })
		const second = await fetchFieldOptions(STORE_STAFF_LIST_PARAMS, { useCache: false })

		assert.equal(callCount, 2)
		assert.equal(first.data[0].value, 'u-1')
		assert.equal(second.data[0].value, 'u-2')
	})

	it('IT-FIELD-OPT-010: 4sStaffList name keyword uses separate cache key', async () => {
		const { STORE_DAILY_API_SWITCH } = require('../config/apiSwitch.js')
		STORE_DAILY_API_SWITCH.fieldOptions = 'real'
		let callCount = 0
		__setEmployeeListApi({
			getEmployeeList: async params => {
				callCount += 1
				const label = params.name ? `员工-${params.name}` : '员工-全量'
				return {
					code: 0,
					message: 'ok',
					data: [{ staffName: label, uuid: `u-${callCount}` }]
				}
			}
		})

		const baseParams = {
			optionsKey: '4sStaffList',
			reportType: 'store',
			merchantId: 'shop-1'
		}
		const full = await fetchFieldOptions(baseParams, { useCache: true })
		const searched = await fetchFieldOptions({ ...baseParams, name: '张' }, { useCache: true })
		const fullAgain = await fetchFieldOptions(baseParams, { useCache: true })

		assert.equal(callCount, 2)
		assert.equal(full.data[0].label, '员工-全量')
		assert.equal(searched.data[0].label, '员工-张')
		assert.equal(fullAgain.data[0].label, '员工-全量')
	})

	it('IT-FIELD-OPT-010b: storeStaffList name keyword uses separate cache key', async () => {
		const { STORE_DAILY_API_SWITCH } = require('../config/apiSwitch.js')
		STORE_DAILY_API_SWITCH.fieldOptions = 'real'
		let callCount = 0
		__setEmployeeListApi({
			getEmployeeList: async params => {
				callCount += 1
				const label = params.name ? `驻店-${params.name}` : '驻店-全量'
				return {
					code: 0,
					message: 'ok',
					data: [{ staffName: label, uuid: `u-${callCount}` }]
				}
			}
		})

		const baseParams = {
			optionsKey: 'storeStaffList',
			reportType: 'store',
			yqMerchantId: 'yq-shop-1',
			merchantId: 'shop-1'
		}
		const full = await fetchFieldOptions(baseParams, { useCache: true })
		const searched = await fetchFieldOptions({ ...baseParams, name: '李' }, { useCache: true })
		const fullAgain = await fetchFieldOptions(baseParams, { useCache: true })

		assert.equal(callCount, 2)
		assert.equal(full.data[0].label, '驻店-全量')
		assert.equal(searched.data[0].label, '驻店-李')
		assert.equal(fullAgain.data[0].label, '驻店-全量')
	})
})