fieldOptionsAdapters.test.mjs 9.84 KB
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 {
	STORE_DAILY_API_SWITCH,
	STAFF_DAILY_API_SWITCH,
	NEGOTIATION_DAILY_API_SWITCH
} = require('../config/apiSwitch.js')
const { fetchFieldOptionsAdapter } = require('../api/fieldOptionsAdapters.js')
const {
	mapEmployeeListToOptions,
	fetchStoreStaffListOptions,
	__setEmployeeListApi,
	__setOrderListApi
} = require('../api/fieldOptionProviders.js')
const { clearFieldOptionsCache } = require('../api/fieldOptionsService.js')
const { MOCK_STORE_STAFF_LIST } = require('../fixtures/mock-field-options.js')

describe('fieldOptionProviders', () => {
	afterEach(() => {
		__setEmployeeListApi(null)
	})

	it('IT-FIELD-OPT-001: mapEmployeeListToOptions maps staffName and uuid', () => {
		assert.deepEqual(
			mapEmployeeListToOptions([
				{ staffName: '张三', uuid: 'u-1', phone: '13800138000' },
				{ name: '李四', staffUuid: 'u-2' }
			]),
			[
				{ label: '张三', value: 'u-1', id: 'u-1', name: '张三', phone: '13800138000' },
				{ label: '李四', value: 'u-2', id: 'u-2', name: '李四' }
			]
		)
	})

	it('IT-FIELD-OPT-002: fetchStoreStaffListOptions requires merchantId', async () => {
		const res = await fetchStoreStaffListOptions({})
		assert.equal(res.code, -1)
		assert.equal(res.message, '缺少门店信息')
	})

	it('IT-FIELD-OPT-003: onTheJob API receives merchantId only (no sceneList)', async () => {
		let captured = null
		__setEmployeeListApi({
			getEmployeeList: async params => {
				captured = params
				return {
					code: 0,
					message: 'ok',
					data: [{ staffName: '王五', uuid: 'u-3' }]
				}
			}
		})

		const res = await fetchStoreStaffListOptions({
			merchantId: 'yq-1',
			sceneType: 1,
			reportType: 'staff'
		})

		assert.equal(res.code, 0)
		assert.deepEqual(captured, { merchantId: 'yq-1' })
		assert.deepEqual(res.data, [{ label: '王五', value: 'u-3', id: 'u-3', name: '王五' }])
	})

	it('IT-FIELD-OPT-003b: all reportTypes omit sceneList even when sceneType set', async () => {
		for (const reportType of ['staff', 'store', 'negotiation']) {
			let captured = null
			__setEmployeeListApi({
				getEmployeeList: async params => {
					captured = params
					return { code: 0, message: 'ok', data: [] }
				}
			})

			await fetchStoreStaffListOptions({
				merchantId: 'shop-1',
				sceneType: 1,
				reportType
			})

			assert.deepEqual(captured, { merchantId: 'shop-1' }, reportType)
		}
	})
})

describe('fieldOptionsAdapters', () => {
	const originalStoreFieldOptions = STORE_DAILY_API_SWITCH.fieldOptions
	const originalStaffFieldOptions = STAFF_DAILY_API_SWITCH.fieldOptions
	const originalNegotiationFieldOptions = NEGOTIATION_DAILY_API_SWITCH.fieldOptions

	afterEach(() => {
		STORE_DAILY_API_SWITCH.fieldOptions = originalStoreFieldOptions
		STAFF_DAILY_API_SWITCH.fieldOptions = originalStaffFieldOptions
		NEGOTIATION_DAILY_API_SWITCH.fieldOptions = originalNegotiationFieldOptions
		__setEmployeeListApi(null)
		__setOrderListApi(null)
		clearFieldOptionsCache()
	})

	it('IT-FIELD-OPT-004: mock mode returns fixture for storeStaffList', async () => {
		STORE_DAILY_API_SWITCH.fieldOptions = 'mock'
		const res = await fetchFieldOptionsAdapter({
			optionsKey: 'storeStaffList',
			reportType: 'store',
			merchantId: 'm1'
		})
		assert.equal(res.code, 0)
		assert.deepEqual(res.data, MOCK_STORE_STAFF_LIST)
	})

	it('IT-FIELD-OPT-005: real mode routes storeStaffList with yqMerchantId', async () => {
		STORE_DAILY_API_SWITCH.fieldOptions = 'real'
		let captured = null
		__setEmployeeListApi({
			getEmployeeList: async params => {
				captured = params
				return {
					code: 0,
					message: 'ok',
					data: [{ staffName: '赵六', uuid: 'u-4' }]
				}
			}
		})

		const res = await fetchFieldOptionsAdapter({
			optionsKey: 'storeStaffList',
			reportType: 'store',
			merchantId: 'shop-2',
			yqMerchantId: 'yq-store',
			sceneType: 1
		})

		assert.deepEqual(captured, { merchantId: 'yq-store' })
		assert.equal(res.code, 0)
		assert.deepEqual(res.data, [{ label: '赵六', value: 'u-4', id: 'u-4', name: '赵六' }])
	})

	it('IT-FIELD-OPT-005b: store 4sStaffList still uses shop merchantId', async () => {
		STORE_DAILY_API_SWITCH.fieldOptions = 'real'
		let captured = null
		__setEmployeeListApi({
			getEmployeeList: async params => {
				captured = params
				return { code: 0, message: 'ok', data: [] }
			}
		})

		await fetchFieldOptionsAdapter({
			optionsKey: '4sStaffList',
			reportType: 'store',
			merchantId: 'shop-2',
			yqMerchantId: 'yq-store',
			sceneType: 1
		})

		assert.deepEqual(captured, { merchantId: 'shop-2' })
	})

	it('IT-FIELD-OPT-006: real mode routes 4sStaffList to employee API without sceneList', async () => {
		STORE_DAILY_API_SWITCH.fieldOptions = 'real'
		let captured = null
		__setEmployeeListApi({
			getEmployeeList: async params => {
				captured = params
				return {
					code: 0,
					message: 'ok',
					data: [{ staffName: '交付专员甲', uuid: 'u-5' }]
				}
			}
		})

		const res = await fetchFieldOptionsAdapter({
			optionsKey: '4sStaffList',
			reportType: 'store',
			merchantId: 'shop-3',
			sceneType: 1
		})

		assert.deepEqual(captured, { merchantId: 'shop-3' })
		assert.equal(res.code, 0)
		assert.deepEqual(res.data, [{ label: '交付专员甲', value: 'u-5', id: 'u-5', name: '交付专员甲' }])
	})

	it('IT-FIELD-OPT-008: 4sStaffList passes name to getEmployeeList for popup search', async () => {
		STORE_DAILY_API_SWITCH.fieldOptions = 'real'
		let captured = null
		__setEmployeeListApi({
			getEmployeeList: async params => {
				captured = params
				return {
					code: 0,
					message: 'ok',
					data: [{ staffName: '张三', uuid: 'u-8' }]
				}
			}
		})

		const res = await fetchFieldOptionsAdapter({
			optionsKey: '4sStaffList',
			reportType: 'negotiation',
			merchantId: 'shop-3',
			name: '张'
		})

		assert.deepEqual(captured, { merchantId: 'shop-3', name: '张' })
		assert.equal(res.code, 0)
		assert.deepEqual(res.data, [{ label: '张三', value: 'u-8', id: 'u-8', name: '张三' }])
	})

	it('IT-FIELD-OPT-008b: storeStaffList passes name with yqMerchantId for popup search', async () => {
		STAFF_DAILY_API_SWITCH.fieldOptions = 'real'
		let captured = null
		__setEmployeeListApi({
			getEmployeeList: async params => {
				captured = params
				return {
					code: 0,
					message: 'ok',
					data: [{ staffName: '驻店张', uuid: 'u-9' }]
				}
			}
		})

		const res = await fetchFieldOptionsAdapter({
			optionsKey: 'storeStaffList',
			reportType: 'store',
			merchantId: 'shop-4s',
			yqMerchantId: 'yq-park',
			name: '张'
		})

		assert.deepEqual(captured, { merchantId: 'yq-park', name: '张' })
		assert.equal(res.code, 0)
		assert.deepEqual(res.data, [{ label: '驻店张', value: 'u-9', id: 'u-9', name: '驻店张' }])
	})

	it('IT-FIELD-OPT-007: storeStaffList uses yqMerchantId over merchantId (all pages)', async () => {
		STAFF_DAILY_API_SWITCH.fieldOptions = 'real'
		let captured = null
		__setEmployeeListApi({
			getEmployeeList: async params => {
				captured = params
				return {
					code: 0,
					message: 'ok',
					data: [{ staffName: '驻店甲', uuid: 'u-6' }]
				}
			}
		})

		const res = await fetchFieldOptionsAdapter({
			optionsKey: 'storeStaffList',
			reportType: 'staff',
			merchantId: 'shop-4s',
			yqMerchantId: 'yq-park',
			sceneType: 1
		})

		assert.deepEqual(captured, { merchantId: 'yq-park' })
		assert.equal(res.code, 0)
		assert.deepEqual(res.data, [{ label: '驻店甲', value: 'u-6', id: 'u-6', name: '驻店甲' }])
	})

	it('IT-FIELD-OPT-007b: staff falls back to merchantId when yqMerchantId missing', async () => {
		STAFF_DAILY_API_SWITCH.fieldOptions = 'real'
		let captured = null
		__setEmployeeListApi({
			getEmployeeList: async params => {
				captured = params
				return { code: 0, message: 'ok', data: [] }
			}
		})

		await fetchFieldOptionsAdapter({
			optionsKey: 'storeStaffList',
			reportType: 'staff',
			merchantId: 'shop-fallback',
			sceneType: 1
		})

		assert.deepEqual(captured, { merchantId: 'shop-fallback' })
	})

	it('IT-NEG-001: negotiation orderList real mode routes goodsOrder API', async () => {
		NEGOTIATION_DAILY_API_SWITCH.fieldOptions = 'real'
		let captured = null
		__setOrderListApi({
			getGoodsOrderOutputValueList: async params => {
				captured = params
				return {
					code: 0,
					message: 'ok',
					data: [{ orderCode: 'order-001', productName: '延保订单A' }]
				}
			}
		})

		const res = await fetchFieldOptionsAdapter({
			optionsKey: 'orderList',
			reportType: 'negotiation',
			merchantId: 'shop-neg',
			staffUuid: 'staff-uuid-1',
			reportDate: '2026-06-02',
			sceneList: [{ baseSceneCode: 110201 }, { baseSceneCode: 110202 }],
			carBrandTypeFlag: '2',
			groupBrandIds: [3067]
		})

		assert.deepEqual(captured, {
			merchantId: 'shop-neg',
			sellerUserId: 'staff-uuid-1',
			carBrandTypeFlag: '2',
			saleDate: ''
		})
		assert.equal(captured.baseGoodsCodeList, undefined)
		assert.equal(res.code, 0)
		const row = res.data[0]
		assert.equal(row.label, 'order-001')
		assert.equal(row.value, 'order-001')
		assert.equal(row.raw?.productName, '延保订单A')
	})

	it('IT-NEG-006b: orderList adapter passes explicit saleDate only', async () => {
		clearFieldOptionsCache()
		NEGOTIATION_DAILY_API_SWITCH.fieldOptions = 'real'
		let captured = null
		__setOrderListApi({
			getGoodsOrderOutputValueList: async params => {
				captured = params
				return { code: 0, message: 'ok', data: [] }
			}
		})

		await fetchFieldOptionsAdapter({
			optionsKey: 'orderList',
			reportType: 'negotiation',
			merchantId: 'shop-neg',
			staffUuid: 'staff-uuid-1',
			reportDate: '2026-06-02',
			saleDate: '2026-06-15',
			sceneList: [{ baseSceneCode: 110101 }],
			carBrandTypeFlag: '1',
			groupBrandIds: [3454]
		})

		assert.equal(captured.saleDate, '2026-06-15')
		assert.equal(captured.baseGoodsCodeList, undefined)
		assert.equal(captured.carBrandTypeFlag, '1')
	})
})