plan-import-echo-normalize.test.mjs 5.74 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 { normalizePlanImportEchoValues } = require('../form-engine/plan-import-echo-normalize.js')
const { maybeRestoreCarBrandPlanImportMarker } = require('../form-engine/car-brand-value.js')
const {
	hasPlanImportDateMarker,
	PLAN_IMPORT_DATE_FIELD_KEY,
	wrapPlanImportDate,
	unwrapPlanImportDate
} = require('../form-engine/plan-import-date-value.js')

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: [[]]
}

const partialCarFlat = {
	vehicleBrandId: 3454,
	vehicleBrandName: '问界',
	vehicleSubBrandId: 1,
	vehicleSubBrandName: '问界',
	vehicleSeriesId: 101,
	vehicleSeriesName: 'M7 Plus'
}

const fullCarFlat = {
	...partialCarFlat,
	vehicleShapeId: 201,
	vehicleShapeName: '2024款'
}

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('plan-import-echo-normalize', () => {
	it('IT-ECHO-PLAN-001: flat three-level carInfo restores partialFromPlanImport', () => {
		const restored = maybeRestoreCarBrandPlanImportMarker(partialCarFlat)
		assert.equal(restored.partialFromPlanImport, true)
		assert.equal(restored.vehicleSeriesId, 101)
		assert.equal(restored.vehicleShapeId, undefined)
	})

	it('IT-ECHO-PLAN-002: level-four carInfo has no import marker', () => {
		const restored = maybeRestoreCarBrandPlanImportMarker(fullCarFlat)
		assert.equal(restored.partialFromPlanImport, undefined)
		assert.equal(restored.vehicleShapeId, 201)
	})

	it('IT-ECHO-PLAN-003: future F3-4-1 plain string wraps with marker', () => {
		const future = tomorrowYmd()
		const values = normalizePlanImportEchoValues([subformGroupF34], {
			'F3-4': [{ 'F3-4-1': future, 'F3-4-5': '张三' }]
		})
		assert.equal(hasPlanImportDateMarker(values['F3-4'][0]['F3-4-1']), true)
		assert.equal(values['F3-4'][0]['F3-4-1'].value, future)
	})

	it('IT-ECHO-PLAN-004: parseJsonContentDetailValues echo + engine.init passes validation', 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': { ...partialCarFlat }
					}
				]
			}
		])
		const values = parseJsonContentDetailValues(jsonContent, [subformGroupF34])
		assert.equal(hasPlanImportDateMarker(values['F3-4'][0][PLAN_IMPORT_DATE_FIELD_KEY]), 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('不能晚于今天'))
		const carErrors = validation.errors.filter(e => e.message?.includes('车型配置不能为空'))
		assert.equal(dateErrors.length, 0, JSON.stringify(validation.errors))
		assert.equal(carErrors.length, 0, JSON.stringify(validation.errors))
	})

	it('IT-ECHO-PLAN-005: save unwraps plan import date in childrenValue then parse echoes', async () => {
		const { parseJsonContentDetailValues, buildDynamicSavePayload } = await import(
			'../form-engine/submit-adapter.js'
		)
		const future = tomorrowYmd()
		const wrapped = wrapPlanImportDate(future)
		const initialValues = {
			'F3-4': [
				{
					'F3-4-1': wrapped,
					'F3-4-5': '张三',
					'F3-4-7': { ...partialCarFlat }
				}
			]
		}
		const engine = createFormEngine({
			list: [subformGroupF34],
			version: 1
		})
		engine.init({ list: [subformGroupF34] }, { initialValues })

		const payload = buildDynamicSavePayload({
			reportTemplateId: 10003,
			engine,
			normalizedFieldDataList: true
		})
		const parsed = JSON.parse(payload.jsonContent)
		const f34 = parsed.find(item => item.key === 'F3-4')
		assert.ok(f34, 'F3-4 section missing')
		const storedDate = f34.childrenValue[0]['F3-4']['F3-4-1']
		assert.equal(typeof storedDate, 'string', JSON.stringify(storedDate))
		assert.equal(storedDate, future)

		const values = parseJsonContentDetailValues(payload.jsonContent, [subformGroupF34])
		assert.equal(unwrapPlanImportDate(values['F3-4'][0]['F3-4-1']), future)
		assert.equal(hasPlanImportDateMarker(values['F3-4'][0]['F3-4-1']), true)
	})

	it('IT-ECHO-PLAN-006: bad childrenValue object wrapper still echoes date', async () => {
		const { parseJsonContentDetailValues } = await import('../form-engine/submit-adapter.js')
		const future = tomorrowYmd()
		const badWrapper = wrapPlanImportDate(future)
		const jsonContent = JSON.stringify([
			{
				key: 'F3-4',
				type: 'subform',
				origin_children: subformGroupF34.origin_children,
				childrenValue: [
					{
						'F3-4': {
							'F3-4-1': badWrapper,
							'F3-4-5': '张三'
						}
					}
				]
			}
		])
		const values = parseJsonContentDetailValues(jsonContent, [subformGroupF34])
		assert.equal(unwrapPlanImportDate(values['F3-4'][0]['F3-4-1']), future)
	})
})