dailyReportInconsistency.helpers.test.mjs 2.69 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import {
	isStaffCountInconsistent,
	findInconsistentBrandField,
	hasExistingInputErrors,
	buildBrandCheckWarningLines,
	STAFF_DAILY_FIELD_KEYS
} from '../services/dailyReportInconsistency.helpers.js'

const userInfo = { uuid: 'user-b' }
const orderObjBase = {
	lastStaffUuid: 'user-a',
	lastStaffName: '张三',
	recordStaffCount: 3
}
const fieldMeta = [{ key: 'deliveryCount', label: '店端实际交车数' }]

describe('dailyReportInconsistency.helpers', () => {
	it('isStaffCountInconsistent returns true when staff count differs', () => {
		assert.equal(
			isStaffCountInconsistent(
				{ ...orderObjBase, staffCount: 5 },
				userInfo,
				() => false
			),
			true
		)
	})

	it('isStaffCountInconsistent returns false when can skip validation', () => {
		assert.equal(
			isStaffCountInconsistent(
				{ ...orderObjBase, staffCount: 5 },
				userInfo,
				() => true
			),
			false
		)
	})

	it('findInconsistentBrandField finds first mismatched field', () => {
		const result = findInconsistentBrandField({
			carBandList: [{ deliveryCount: 2, deliveryCountRecord: 1 }],
			orderObj: orderObjBase,
			userInfo,
			fieldMeta,
			canSkipValidation: () => false
		})
		assert.deepEqual(result, {
			index: 0,
			name: 'deliveryCount',
			label: '店端实际交车数'
		})
	})

	it('hasExistingInputErrors detects staffCountError and inputError flags', () => {
		assert.equal(
			hasExistingInputErrors({
				orderObj: { staffCountError: true },
				carBandList: []
			}),
			true
		)
		assert.equal(
			hasExistingInputErrors({
				orderObj: {},
				carBandList: [{ deliveryCountinputError: true }]
			}),
			true
		)
		assert.equal(
			hasExistingInputErrors({
				orderObj: {},
				carBandList: [{ deliveryCount: 1 }]
			}),
			false
		)
	})

	it('buildBrandCheckWarningLines returns one line per failed brand', () => {
		const lines = buildBrandCheckWarningLines([
			{ isCheckPass: true, brandName: '问界' },
			{ isCheckPass: false, brandName: '鸿蒙', differenceValue: 2 }
		])
		assert.equal(lines.length, 1)
		assert.match(lines[0], /【鸿蒙】/)
		assert.match(lines[0], /差异值2/)
	})

	it('findInconsistentBrandField uses staffDaily field keys', () => {
		assert.ok(STAFF_DAILY_FIELD_KEYS.includes('extendedWarrantyToShopCount'))
		const result = findInconsistentBrandField({
			carBandList: [
				{ extendedWarrantyToShopCount: 2, extendedWarrantyToShopCountRecord: 1 }
			],
			orderObj: orderObjBase,
			userInfo,
			fieldMeta: [{ key: 'extendedWarrantyToShopCount', label: '延保到店流量' }],
			canSkipValidation: () => false,
			fieldKeys: STAFF_DAILY_FIELD_KEYS
		})
		assert.equal(result?.name, 'extendedWarrantyToShopCount')
	})
})