staffDailyReportAdapters.test.mjs 10.7 KB
import { describe, it, beforeEach } from 'node:test'
import assert from 'node:assert/strict'
import { createRequire } from 'node:module'

const require = createRequire(import.meta.url)
const {
	STAFF_DAILY_API_SWITCH,
	STORE_DAILY_API_SWITCH,
	isStaffMockMode,
	isStaffSaveEchoEnabled,
	isStoreMockMode,
	isStoreSaveEchoEnabled
} = require('../config/apiSwitch.js')
const adapters = require('../api/staffDailyReportAdapters.js')
const { clearStaffDailyReportCache } = require('../api/staffDailyReportCache.js')
const {
	clearConfigCenterTemplateCache,
	__setConfigCenterRequest,
	DYNAMIC_FORM_TEMPLATE_MODULE
} = require('../config/configCenter.js')
const { DAILY_REPORT_TEMPLATE_TYPE } = require('../config/dailyReportConstants.js')
const schemaIndexed = require('../fixtures/schema-staff-daily-full.js')
const { normalizeConfigToSchemaRoot } = require('../config/configCenter.js')
const schemaTemplate = normalizeConfigToSchemaRoot(schemaIndexed)

describe('apiSwitch', () => {
	it('IT-API-SWITCH-001: staff daily defaults to real APIs including brand init', () => {
		assert.equal(STAFF_DAILY_API_SWITCH.detail, 'real')
		assert.equal(STAFF_DAILY_API_SWITCH.schema, 'real')
		assert.equal(STAFF_DAILY_API_SWITCH.brand, 'real')
		assert.equal(STAFF_DAILY_API_SWITCH.fieldOptions, 'real')
		assert.equal(isStaffMockMode('detail'), false)
		assert.equal(isStaffMockMode('schema'), false)
		assert.equal(isStaffMockMode('brand'), false)
		assert.equal(isStaffMockMode('fieldOptions'), false)
		assert.equal(isStaffMockMode('save'), false)
	})

	it('IT-API-SWITCH-002: saveEcho off disables echo adapter', () => {
		assert.equal(STAFF_DAILY_API_SWITCH.saveEcho, 'off')
		assert.equal(isStaffSaveEchoEnabled(), false)
	})

	it('IT-STORE-API-SWITCH-001: store daily detail and brand init real', () => {
		assert.equal(STORE_DAILY_API_SWITCH.detail, 'real')
		assert.equal(STORE_DAILY_API_SWITCH.schema, 'real')
		assert.equal(STORE_DAILY_API_SWITCH.brand, 'real')
		assert.equal(STORE_DAILY_API_SWITCH.saveEcho, 'off')
		assert.equal(isStoreMockMode('detail'), false)
		assert.equal(isStoreMockMode('brand'), false)
		assert.equal(isStoreSaveEchoEnabled(), false)
	})
})

describe('staffDailyReportAdapters', () => {
	beforeEach(() => {
		clearStaffDailyReportCache()
		clearConfigCenterTemplateCache()
		__setConfigCenterRequest(null)
	})

	it('IT-API-SWITCH-001: fetchDetailAdapter uses mock without HTTP', async () => {
		const original = STAFF_DAILY_API_SWITCH.detail
		STAFF_DAILY_API_SWITCH.detail = 'mock'
		try {
			const res = await adapters.fetchDetailAdapter({
				merchantId: 'm1',
				staffUuid: 's1',
				reportDate: '2026-05-26',
				sceneType: 1,
				reportTemplateId: 10001
			})
			assert.equal(res.code, 0)
			assert.ok(res.data)
		} finally {
			STAFF_DAILY_API_SWITCH.detail = original
		}
	})

	it('fetchDetailAdapter real mode calls dailyReportDetailGet with templateType=1', async () => {
		const original = STAFF_DAILY_API_SWITCH.detail
		STAFF_DAILY_API_SWITCH.detail = 'real'
		let captured = null
		adapters.__setDynamicDailyReportApi({
			dailyReportDetailGet: async data => {
				captured = data
				return { code: 0, message: 'ok', data: { id: 1 } }
			}
		})
		try {
			await adapters.fetchDetailAdapter({
				merchantId: 'm1',
				staffUuid: 's1',
				reportDate: '2026-05-26',
				sceneType: 1,
				reportTemplateId: 10001
			})
			assert.equal(captured.merchantId, 'm1')
			assert.equal(captured.staffUuid, 's1')
			assert.equal(captured.reportDate, '2026-05-26')
			assert.equal(captured.sceneType, 1)
			assert.equal(captured.templateType, DAILY_REPORT_TEMPLATE_TYPE.staff)
			assert.equal(captured.reportTemplateId, undefined)
		} finally {
			adapters.__setDynamicDailyReportApi(null)
			STAFF_DAILY_API_SWITCH.detail = original
		}
	})

	it('IT-API-SWITCH-002: fetchSchemaRootAdapter returns fixture in mock mode', async () => {
		const original = STAFF_DAILY_API_SWITCH.schema
		STAFF_DAILY_API_SWITCH.schema = 'mock'
		try {
			const res = await adapters.fetchSchemaRootAdapter({
				reportTemplateId: 10001,
				baseSceneCode: 100101,
				merchantId: 'm1'
			})
			assert.equal(res.code, 0)
			assert.ok(Array.isArray(res.data.list))
			assert.equal(res.data.list.length, schemaTemplate.list.length)
		} finally {
			STAFF_DAILY_API_SWITCH.schema = original
		}
	})

	it('IT-API-SWITCH-002: fetchSchemaRootAdapter caches config center responses', async () => {
		const original = STAFF_DAILY_API_SWITCH.schema
		STAFF_DAILY_API_SWITCH.schema = 'real'
		let calls = 0
		__setConfigCenterRequest(() => {
			calls += 1
			return Promise.resolve({
				data: { 0: { key: 'F1', type: 'radio', label: 'x' } }
			})
		})
		try {
			const params = { reportTemplateId: 10001, baseSceneCode: 100101, merchantId: 'm1' }
			await adapters.fetchSchemaRootAdapter(params)
			await adapters.fetchSchemaRootAdapter(params)
			assert.equal(calls, 1)
		} finally {
			__setConfigCenterRequest(null)
			STAFF_DAILY_API_SWITCH.schema = original
		}
	})

	it('fetchSchemaRootAdapter real mode uses staff-daily-template moduleUrl', async () => {
		const original = STAFF_DAILY_API_SWITCH.schema
		STAFF_DAILY_API_SWITCH.schema = 'real'
		let capturedUrl = ''
		__setConfigCenterRequest(options => {
			capturedUrl = options.url
			return Promise.resolve({ data: schemaIndexed })
		})
		try {
			await adapters.fetchSchemaRootAdapter({
				reportTemplateId: 10001,
				baseSceneCode: 100101,
				merchantId: 'm1'
			})
			assert.ok(capturedUrl.includes(`moduleUrl=${DYNAMIC_FORM_TEMPLATE_MODULE.staff}`))
		} finally {
			__setConfigCenterRequest(null)
			STAFF_DAILY_API_SWITCH.schema = original
		}
	})

	it('fetchSceneAdapter caches scene list for merchant', async () => {
		let calls = 0
		adapters.__setDynamicDailyReportApi({
			merchantBaseSceneCodeList: async () => {
				calls += 1
				return { code: 0, message: 'ok', data: ['100101'] }
			}
		})
		try {
			await adapters.fetchSceneAdapter({
				merchantId: 'm-cache',
				staffUuid: 's1',
				sceneType: 1
			})
			await adapters.fetchSceneAdapter({
				merchantId: 'm-cache',
				staffUuid: 's1',
				sceneType: 1
			})
			assert.equal(calls, 1)
		} finally {
			adapters.__setDynamicDailyReportApi(null)
		}
	})

	it('fetchSceneAdapter normalizes baseSceneCode list strings', async () => {
		adapters.__setDynamicDailyReportApi({
			merchantBaseSceneCodeList: async () => ({
				code: 0,
				message: 'ok',
				data: ['110201', '100101']
			})
		})
		try {
			const res = await adapters.fetchSceneAdapter({
				merchantId: 'm1',
				staffUuid: 's1',
				sceneType: 0
			})
			assert.equal(res.code, 0)
			assert.deepEqual(res.data, [
				{ baseSceneCode: 110201 },
				{ baseSceneCode: 100101 }
			])
		} finally {
			adapters.__setDynamicDailyReportApi(null)
		}
	})

	it('fetchBrandAdapter mock mode returns fixture brand list', async () => {
		const original = STAFF_DAILY_API_SWITCH.brand
		STAFF_DAILY_API_SWITCH.brand = 'mock'
		const { mapBrandListToBrandIds } = await import('../form-engine/submit-adapter.js')
		const { MOCK_MERCHANT_BRAND_IDS } = require('../fixtures/mock-meta.js')
		try {
			const res = await adapters.fetchBrandAdapter({ merchantId: 'm1', sceneType: 1 })
			assert.equal(res.code, 0)
			assert.deepEqual(mapBrandListToBrandIds(res.data).sort(), MOCK_MERCHANT_BRAND_IDS.sort())
		} finally {
			STAFF_DAILY_API_SWITCH.brand = original
		}
	})

	it('fetchBrandAdapter real mode calls getMerchantCarBrandList', async () => {
		const original = STAFF_DAILY_API_SWITCH.brand
		STAFF_DAILY_API_SWITCH.brand = 'real'
		let captured = null
		adapters.__setFetchMerchantOperatingBrandList(async merchantId => {
			captured = merchantId
			return {
				code: 0,
				message: 'ok',
				data: [
					{
						id: 30671,
						name: '岚图汽车',
						brandId: 30671,
						firstBrandId: 3067
					}
				]
			}
		})
		try {
			const res = await adapters.fetchBrandAdapter({ merchantId: 'm1', sceneType: 1 })
			assert.equal(captured, 'm1')
			assert.equal(res.code, 0)
			assert.equal(res.data.length, 1)
			assert.equal(res.data[0].brandId, 30671)
			assert.equal(res.data[0].firstBrandId, 3067)
		} finally {
			adapters.__setFetchMerchantOperatingBrandList(null)
			STAFF_DAILY_API_SWITCH.brand = original
		}
	})

	it('IT-STAFF-BRAND-002: fetchBrandAdapter real mode parses paginated merchants.info childs ids', async () => {
		const original = STAFF_DAILY_API_SWITCH.brand
		STAFF_DAILY_API_SWITCH.brand = 'real'
		const carBrandApi = require('../api/carBrandApi.js')
		const { mapBrandListToBrandIds } = await import('../form-engine/submit-adapter.js')
		const { PAGINATED_MERCHANT_BRAND_RESPONSE } = require('./fixtures/paginated-merchant-brand-response.js')
		adapters.__setFetchMerchantOperatingBrandList(null)
		carBrandApi.__setGetMerchantCarBrandList(async () => PAGINATED_MERCHANT_BRAND_RESPONSE)
		try {
			const res = await adapters.fetchBrandAdapter({ merchantId: 'm1' })
			assert.equal(res.code, 0)
			const brandIds = mapBrandListToBrandIds(res.data)
			assert.ok(brandIds.includes(3454))
			assert.ok(brandIds.includes(2180))
		} finally {
			carBrandApi.__setGetMerchantCarBrandList(null)
			STAFF_DAILY_API_SWITCH.brand = original
		}
	})

	it('IT-STAFF-BRAND-CACHE-001: fetchBrandAdapter caches by merchantId in real mode', async () => {
		const original = STAFF_DAILY_API_SWITCH.brand
		STAFF_DAILY_API_SWITCH.brand = 'real'
		let callCount = 0
		adapters.__setFetchMerchantOperatingBrandList(async merchantId => {
			callCount += 1
			return {
				code: 0,
				message: 'ok',
				data: [
					{
						id: 30671,
						name: '岚图汽车',
						brandId: 30671,
						firstBrandId: 3067
					}
				]
			}
		})
		try {
			const res1 = await adapters.fetchBrandAdapter({ merchantId: 'm1' })
			const res2 = await adapters.fetchBrandAdapter({ merchantId: 'm1' })
			assert.equal(callCount, 1)
			assert.equal(res1.code, 0)
			assert.deepEqual(res2, res1)
		} finally {
			adapters.__setFetchMerchantOperatingBrandList(null)
			STAFF_DAILY_API_SWITCH.brand = original
		}
	})

	it('fetchWeeklyAdapter real mode uses dailyReportWeeklyList', async () => {
		const original = STAFF_DAILY_API_SWITCH.weekly
		STAFF_DAILY_API_SWITCH.weekly = 'real'
		let calls = 0
		adapters.__setDynamicDailyReportApi({
			dailyReportWeeklyList: async params => {
				calls += 1
				assert.equal(params.templateType, 1)
				return { code: 0, message: 'ok', data: ['2026-05-26'] }
			}
		})
		try {
			await adapters.fetchWeeklyAdapter({
				merchantId: 'm1',
				staffUuid: 's1',
				reportStartDate: '2026-05-20',
				reportEndDate: '2026-05-26'
			})
			assert.equal(calls, 1)
		} finally {
			adapters.__setDynamicDailyReportApi(null)
			STAFF_DAILY_API_SWITCH.weekly = original
		}
	})

	it('saveEchoAdapter is no-op when saveEcho is off', () => {
		const original = STAFF_DAILY_API_SWITCH.saveEcho
		STAFF_DAILY_API_SWITCH.saveEcho = 'off'
		try {
			assert.equal(adapters.saveEchoAdapter({ merchantId: 'm1' }), null)
		} finally {
			STAFF_DAILY_API_SWITCH.saveEcho = original
		}
	})
})