negotiationCustomerImport.test.mjs 9.81 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { createRequire } from 'node:module'

const require = createRequire(import.meta.url)
const { createFormEngine } = require('../form-engine/core.js')
const {
	buildCustomerRowValueMap,
	validateCustomerImportCapacity,
	applyCustomersToEngine
} = require('../services/negotiationCustomerImport.js')
const { clearPartialPlanImportMarker } = require('../form-engine/car-brand-value.js')
const {
	clearPlanImportDateMarker,
	hasPlanImportDateMarker,
	wrapPlanImportDate
} = require('../form-engine/plan-import-date-value.js')

const sampleCustomer = {
	inviteDate: '2026-06-10',
	customerName: '张三',
	customerPhone: '13800138000',
	advanceUserId: 'staff-1',
	advanceUserName: '李璐',
	carBrandId: 3454,
	carBrand: '问界',
	carChildsBrandId: 1,
	carChildsBrandName: '问界',
	carSeriesId: 101,
	carSeriesName: 'M7 Plus'
}

const subformGroup = {
	key: 'F3-2',
	type: 'subform',
	label: '问界鸿蒙洽谈明细 (延保)',
	rules: { maxLength: 20 },
	origin_children: [
		{ key: 'F3-2-1', type: 'date', label: '交车日期' },
		{ key: 'F3-2-4', type: 'input', label: '客户名称' },
		{ key: 'F3-2-5', type: 'phoneInput', label: '客户手机号' },
		{ key: 'F3-2-6', type: 'carBrandSelect', label: '车型配置', required: true },
		{
			key: 'F3-2-8',
			type: 'select',
			label: '驻店人员',
			optionsKey: 'storeStaffList'
		}
	],
	children: [[]]
}

const subformGroupF34 = {
	key: 'F3-4',
	type: 'subform',
	label: '洽谈明细',
	rules: { maxLength: 20 },
	origin_children: [
		{
			key: 'F3-4-1',
			type: 'datetime',
			label: '洽谈日期',
			required: true,
			rules: { format: 'date', maxDate: 'today' }
		},
		{ key: 'F3-4-5', type: 'input', label: '客户名称' },
		{ key: 'F3-4-7', type: 'carBrandSelect', label: '车型配置', required: true }
	],
	children: [[]]
}

function tomorrowYmd() {
	const d = new Date()
	d.setDate(d.getDate() + 1)
	const y = d.getFullYear()
	const m = `${d.getMonth() + 1}`.padStart(2, '0')
	const day = `${d.getDate()}`.padStart(2, '0')
	return `${y}-${m}-${day}`
}

describe('negotiationCustomerImport', () => {
	it('IT-PLAN-IMPORT-001: buildCustomerRowValueMap maps F3-2 fields', () => {
		const map = buildCustomerRowValueMap(sampleCustomer, 'F3-2')
		assert.equal(map['F3-2-4'], '张三')
		assert.equal(map['F3-2-5'], '13800138000')
		assert.equal(map['F3-2-1'], '2026-06-10')
		assert.equal(map['F3-2-8'].id, 'staff-1')
		assert.equal(map['F3-2-6'].vehicleSeriesName, 'M7 Plus')
		assert.equal(map['F3-2-6'].partialFromPlanImport, true)
	})

	it('IT-PLAN-IMPORT-002: validateCustomerImportCapacity blocks over 20', () => {
		const fail = validateCustomerImportCapacity([subformGroup], { 'F3-2': 18 }, 3)
		assert.equal(fail.ok, false)
		assert.equal(fail.groupKey, 'F3-2')
		assert.match(fail.groupLabel, /问界/)
		const ok = validateCustomerImportCapacity([subformGroup], { 'F3-2': 18 }, 2)
		assert.equal(ok.ok, true)
	})

	it('IT-PLAN-IMPORT-004: applyCustomersToEngine no visible subform returns empty label', () => {
		const engine = createFormEngine({ list: [], version: 1 })
		engine.init({ list: [] }, { initialValues: {} })
		const result = applyCustomersToEngine(engine, [sampleCustomer])
		assert.equal(result.ok, false)
		assert.equal(result.groupKey, '')
		assert.equal(result.groupLabel, '')
	})

	it('IT-PLAN-IMPORT-003: applyCustomersToEngine appends rows', () => {
		const engine = createFormEngine({
			list: [subformGroup],
			version: 1
		})
		engine.init({ list: [subformGroup] }, { initialValues: {} })
		const result = applyCustomersToEngine(engine, [sampleCustomer, sampleCustomer])
		assert.equal(result.ok, true)
		const state = engine.getViewState()
		assert.equal((state.groupRowsCache['F3-2'] || []).length, 2)
	})

	it('IT-PLAN-IMPORT-005: imported partial carInfo passes carBrandSelect validation', async () => {
		const { validateForm } = await import('../form-engine/validator.js')
		const engine = createFormEngine({
			list: [subformGroup],
			version: 1
		})
		engine.init({ list: [subformGroup] }, { initialValues: {} })
		const result = applyCustomersToEngine(engine, [sampleCustomer])
		assert.equal(result.ok, true)
		const state = engine.getViewState()
		const validation = validateForm(
			[subformGroup],
			{},
			state.values,
			state.groupRowsCache
		)
		const carErrors = validation.errors.filter(e => e.label?.includes('车型配置'))
		assert.equal(carErrors.length, 0)
	})

	it('IT-PLAN-IMPORT-006: unmarked three-level carInfo passes validation', async () => {
		const { validateForm } = await import('../form-engine/validator.js')
		const engine = createFormEngine({
			list: [subformGroup],
			version: 1
		})
		engine.init({ list: [subformGroup] }, { initialValues: {} })
		const result = applyCustomersToEngine(engine, [sampleCustomer])
		assert.equal(result.ok, true)
		const carInfo = engine.getViewState().values['F3-2'][0]['F3-2-6']
		const cleared = clearPartialPlanImportMarker(carInfo)
		const state = engine.mutateSubform(
			'F3-2',
			0,
			'F3-2-6',
			{ metaValue: cleared },
			{ appDualKey: true }
		)
		const validation = validateForm([subformGroup], {}, state.values, state.groupRowsCache)
		const carErrors = validation.errors.filter(e => e.message?.includes('车型配置不能为空'))
		assert.equal(carErrors.length, 0)
	})

	it('IT-PLAN-IMPORT-007: clearing import marker preserves three-level validation', async () => {
		const { validateForm } = await import('../form-engine/validator.js')
		const engine = createFormEngine({
			list: [subformGroup],
			version: 1
		})
		engine.init({ list: [subformGroup] }, { initialValues: {} })
		applyCustomersToEngine(engine, [sampleCustomer])
		const carInfo = engine.getViewState().values['F3-2'][0]['F3-2-6']
		const cleared = clearPartialPlanImportMarker(carInfo)
		assert.equal(cleared.partialFromPlanImport, undefined)
		const state = engine.mutateSubform(
			'F3-2',
			0,
			'F3-2-6',
			{ metaValue: cleared },
			{ appDualKey: true }
		)
		const validation = validateForm([subformGroup], {}, state.values, state.groupRowsCache)
		assert.equal(validation.ok, true)
	})

	it('IT-PLAN-IMPORT-008: F3-4 inviteDate wrapped with partialFromPlanImport', () => {
		const future = tomorrowYmd()
		const customer = { ...sampleCustomer, inviteDate: future }
		const map = buildCustomerRowValueMap(customer, 'F3-4')
		assert.equal(hasPlanImportDateMarker(map['F3-4-1']), true)
		assert.equal(map['F3-4-1'].value, future)
		const mapF32 = buildCustomerRowValueMap(customer, 'F3-2')
		assert.equal(mapF32['F3-2-1'], future)
		assert.equal(typeof mapF32['F3-2-1'], 'string')
	})

	it('IT-PLAN-IMPORT-009: imported future F3-4-1 passes validation until marker cleared', async () => {
		const { validateForm } = await import('../form-engine/validator.js')
		const future = tomorrowYmd()
		const customer = { ...sampleCustomer, inviteDate: future }
		const engine = createFormEngine({
			list: [subformGroupF34],
			version: 1
		})
		engine.init({ list: [subformGroupF34] }, { initialValues: {} })
		applyCustomersToEngine(engine, [customer])
		const state = engine.getViewState()
		const okValidation = validateForm(
			[subformGroupF34],
			{},
			state.values,
			state.groupRowsCache
		)
		const dateErrors = okValidation.errors.filter(e => e.message?.includes('不能晚于今天'))
		assert.equal(dateErrors.length, 0)

		const cleared = clearPlanImportDateMarker(state.values['F3-4'][0]['F3-4-1'])
		const clearedState = engine.mutateSubform('F3-4', 0, 'F3-4-1', cleared)
		const failValidation = validateForm(
			[subformGroupF34],
			{},
			clearedState.values,
			clearedState.groupRowsCache
		)
		const failDateErrors = failValidation.errors.filter(e => e.message?.includes('不能晚于今天'))
		assert.equal(failDateErrors.length, 1)
	})

	it('IT-PLAN-IMPORT-010: validator reads marker from row map when cache value empty', async () => {
		const { validateForm } = await import('../form-engine/validator.js')
		const future = tomorrowYmd()
		const wrapped = wrapPlanImportDate(future)
		const groupRowsCache = {
			'F3-4': [
				[
					{
						key: 'F3-4-1',
						type: 'datetime',
						label: '洽谈日期',
						required: true,
						rules: { format: 'date', maxDate: 'today' },
						value: '',
						'F3-4-1': wrapped
					}
				]
			]
		}
		const values = {
			'F3-4': [{ 'F3-4-1': wrapped }]
		}
		const validation = validateForm(
			[subformGroupF34],
			{},
			values,
			groupRowsCache
		)
		const dateErrors = validation.errors.filter(e => e.message?.includes('不能晚于今天'))
		assert.equal(dateErrors.length, 0)
	})

	it('IT-PLAN-IMPORT-011: detail echo without markers restores exemption after parse', async () => {
		const { parseJsonContentDetailValues } = await import('../form-engine/submit-adapter.js')
		const { validateForm } = await import('../form-engine/validator.js')
		const future = tomorrowYmd()
		const jsonContent = JSON.stringify([
			{
				key: 'F3-4',
				type: 'subform',
				value: [
					{
						'F3-4-1': future,
						'F3-4-5': '张三',
						'F3-4-7': {
							vehicleBrandId: 3454,
							vehicleBrandName: '问界',
							vehicleSeriesId: 101,
							vehicleSeriesName: 'M7 Plus'
						}
					}
				]
			}
		])
		const values = parseJsonContentDetailValues(jsonContent, [subformGroupF34])
		assert.equal(hasPlanImportDateMarker(values['F3-4'][0]['F3-4-1']), true)
		assert.equal(values['F3-4'][0]['F3-4-7'].partialFromPlanImport, true)

		const engine = createFormEngine({
			list: [subformGroupF34],
			version: 1
		})
		engine.init({ list: [subformGroupF34] }, { initialValues: values })
		const state = engine.getViewState()
		const validation = validateForm(
			[subformGroupF34],
			{},
			state.values,
			state.groupRowsCache
		)
		const dateErrors = validation.errors.filter(e => e.message?.includes('不能晚于今天'))
		assert.equal(dateErrors.length, 0)
		const carErrors = validation.errors.filter(e => e.message?.includes('车型配置不能为空'))
		assert.equal(carErrors.length, 0)
	})
})