staffDailyReportAdapters.test.mjs 13.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
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({
			getUserScene: async () => {
				calls += 1
				return { code: 0, message: 'ok', data: ['100101'] }
			}
		})
		try {
			await adapters.fetchSceneAdapter({
				merchantId: 'm-cache',
				staffUuid: 's1',
				phone: '13800000000',
				sceneType: 1
			})
			await adapters.fetchSceneAdapter({
				merchantId: 'm-cache',
				staffUuid: 's1',
				phone: '13800000000',
				sceneType: 1
			})
			assert.equal(calls, 1)
		} finally {
			adapters.__setDynamicDailyReportApi(null)
		}
	})

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

	it('fetchSceneAdapter calls getUserScene with sceneType phone merchantId', async () => {
		let captured = null
		adapters.__setDynamicDailyReportApi({
			getUserScene: async data => {
				captured = data
				return {
					code: 0,
					message: 'ok',
					data: [{ baseSceneCode: '110201', name: '售前延保' }]
				}
			}
		})
		try {
			const res = await adapters.fetchSceneAdapter({
				merchantId: 'm1',
				shopMerchantId: 'shop-1',
				staffUuid: 's1',
				phone: '13800000000',
				sceneType: 0
			})
			assert.deepEqual(captured, {
				sceneType: '0',
				phone: '13800000000',
				merchantId: 'shop-1'
			})
			assert.deepEqual(res.data, [{ baseSceneCode: 110201 }])
		} 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('IT-STAFF-CACHE-REVALIDATE-001: skipCacheRead forces network and updates brand cache', async () => {
		const original = STAFF_DAILY_API_SWITCH.brand
		STAFF_DAILY_API_SWITCH.brand = 'real'
		let callCount = 0
		let version = 1
		adapters.__setFetchMerchantOperatingBrandList(async () => {
			callCount += 1
			const name = version === 1 ? '岚图汽车' : '岚图汽车新版'
			version += 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' },
				{ skipCacheRead: true }
			)
			const res3 = await adapters.fetchBrandAdapter({ merchantId: 'm1' })
			assert.equal(callCount, 2)
			assert.equal(res1.data[0].name, '岚图汽车')
			assert.equal(res2.data[0].name, '岚图汽车新版')
			assert.equal(res3.data[0].name, '岚图汽车新版')
		} finally {
			adapters.__setFetchMerchantOperatingBrandList(null)
			STAFF_DAILY_API_SWITCH.brand = original
		}
	})

	it('IT-STAFF-CACHE-REVALIDATE-002: skipCacheRead bypasses schema and config center cache', 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: `F${calls}`, type: 'radio', label: 'x' } }
			})
		})
		try {
			const params = { reportTemplateId: 10001, baseSceneCode: 100101, merchantId: 'm1' }
			await adapters.fetchSchemaRootAdapter(params)
			await adapters.fetchSchemaRootAdapter(params, { skipCacheRead: true })
			assert.equal(calls, 2)
		} finally {
			__setConfigCenterRequest(null)
			STAFF_DAILY_API_SWITCH.schema = 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
		}
	})
})