app-subform-json.test.mjs 10.6 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { createFormEngine } from '../form-engine/core.js'

function selectValueMapId(value) {
	if (value != null && typeof value === 'object' && !Array.isArray(value)) {
		return value.id ?? value.value
	}
	return value
}

const carInfo = {
	vehicleBrandId: 1,
	vehicleBrandName: '问界',
	vehicleSubBrandId: 2,
	vehicleSubBrandName: '鸿蒙',
	vehicleSeriesId: 3,
	vehicleSeriesName: 'M7',
	vehicleShapeId: 4,
	vehicleShapeName: '增程版'
}

function buildAppSubformSection(carDisplayValue) {
	return {
		key: 'F3-1',
		label: '洽谈明细',
		type: 'subform',
		children: [
			[
				{
					key: 'F3-1-3',
					label: '推荐人',
					type: 'select',
					optionsKey: '4sStaffList',
					value: '张三',
					'F3-1-3': 'uuid-staff-001'
				},
				{
					key: 'F3-1-13',
					label: '订单',
					type: 'select',
					optionsKey: 'orderList',
					value: '延保订单A',
					'F3-1-13': 'order-001'
				},
				{
					key: 'F3-1-6',
					label: '车型配置',
					type: 'carBrandSelect',
					value: carDisplayValue,
					'F3-1-6': carInfo
				}
			]
		]
	}
}

function isRowFieldsList(value) {
	if (!Array.isArray(value) || !value.length) return false
	const first = value[0]
	return Array.isArray(first) && first[0]?.key != null
}

describe('app-subform-json', async () => {
	const {
		extractSelectIdAndLabel,
		formatFieldForAppJsonContent,
		buildAppCarBrandDisplayString,
		buildAppJsonContent,
		rowFieldsToChildrenValueRow,
		mergeAppJsonIntoSchema,
		getEchoSubformKeysFromJsonContent
	} = await import('../form-engine/app-subform-json.js')
	const { parseJsonContentDetailValues } = await import(
		'../form-engine/submit-adapter.js'
	)
	const { buildAppDailySavePayload } = await import(
		'../form-engine/app-daily-save.js'
	)

	it('IT-NEG-013: getEchoSubformKeysFromJsonContent lists all subform keys in json', () => {
		const jsonContent = JSON.stringify([
			{ key: 'F3-1', type: 'subform', childrenValue: [{ 'F3-1': {} }] },
			{ key: 'F3-3', type: 'subform', childrenValue: [{ 'F3-3': {} }] },
			{ key: 'F3-4', type: 'subform', childrenValue: [] }
		])
		assert.deepEqual(getEchoSubformKeysFromJsonContent(jsonContent), [
			'F3-1',
			'F3-3',
			'F3-4'
		])
	})

	it('IT-APP-001: extractSelectIdAndLabel from object', () => {
		assert.deepEqual(extractSelectIdAndLabel({ id: 'u-1', name: '张三', phone: '138' }), {
			id: 'u-1',
			label: '张三'
		})
	})

	it('IT-APP-002: formatFieldForAppJsonContent select object → dual key with full meta', () => {
		const field = {
			key: 'F2-9-1',
			type: 'select',
			optionsKey: '4sStaffList',
			value: { id: 'uuid-1', name: '张三', phone: '13800138000' },
			'F2-9-1': { id: 'uuid-1', name: '张三', phone: '13800138000' }
		}
		const formatted = formatFieldForAppJsonContent(field)
		assert.equal(formatted.value, '张三')
		assert.deepEqual(formatted['F2-9-1'], {
			id: 'uuid-1',
			name: '张三',
			phone: '13800138000'
		})
		const jsonStored = formatFieldForAppJsonContent(field, { jsonStorage: true })
		assert.equal(jsonStored.value, '张三')
		assert.equal(jsonStored['F2-9-1'], 'uuid-1')
	})

	it('IT-APP-003: formatFieldForAppJsonContent carBrand dual key', () => {
		const formatted = formatFieldForAppJsonContent({
			key: 'F3-1-6',
			type: 'carBrandSelect',
			value: carInfo,
			'F3-1-6': carInfo
		})
		assert.equal(formatted.value, buildAppCarBrandDisplayString(carInfo))
		assert.equal(formatted['F3-1-6'].vehicleShapeId, 4)
		assert.equal(formatted['F3-1-6'].displayText, undefined)
	})

	it('IT-APP-003b: formatFieldForAppJsonContent storeStaffList multipleSelect dual key', () => {
		const formatted = formatFieldForAppJsonContent({
			key: 'F2-1',
			type: 'multipleSelect',
			optionsKey: 'storeStaffList',
			value: [
				{ id: 'staff-001', name: '张三' },
				{ id: 'staff-002', name: '李四' }
			]
		})
		assert.equal(formatted.value, '张三/李四')
		assert.deepEqual(formatted['F2-1'], ['staff-001', 'staff-002'])
	})

	it('IT-APP-003c: childrenValue keeps authoritative ids for remote same-name staff', () => {
		const row = rowFieldsToChildrenValueRow('F2-1', [{
			key: 'F2-1-1',
			type: 'multipleSelect',
			optionsKey: 'storeStaffList',
			options: [
				{ id: 'u-1', name: '张三', value: 'u-1', label: '张三' },
				{ id: 'u-2', name: '张三', value: 'u-2', label: '张三' }
			],
			value: [{ id: 'u-2', name: '张三' }]
		}])
		assert.deepEqual(row['F2-1']['F2-1-1'], {
			text: '张三',
			value: ['u-2']
		})
	})

	it('IT-APP-004: parseJsonContentDetailValues from App children', () => {
		const values = parseJsonContentDetailValues(
			JSON.stringify([
				{
					key: 'F2-9',
					type: 'subform',
					children: [
						[
							{
								key: 'F2-9-1',
								type: 'select',
								optionsKey: '4sStaffList',
								value: '张三',
								'F2-9-1': 'uuid-1'
							}
						]
					]
				}
			]),
			[]
		)
		assert.equal(selectValueMapId(values['F2-9'][0]['F2-9-1']), 'uuid-1')
	})

	it('IT-NEG-002: parse App jsonContent children with dual-key fields', () => {
		const section = buildAppSubformSection(buildAppCarBrandDisplayString(carInfo))
		const values = parseJsonContentDetailValues(JSON.stringify([section]), [])

		assert.equal(selectValueMapId(values['F3-1'][0]['F3-1-3']), 'uuid-staff-001')
		assert.equal(selectValueMapId(values['F3-1'][0]['F3-1-13']), 'order-001')
		assert.equal(values['F3-1'][0]['F3-1-6'].vehicleShapeId, 4)
	})

	it('IT-NEG-003: legacy mini-program value subform still parses', () => {
		const values = parseJsonContentDetailValues(
			JSON.stringify([
				{
					key: 'F3-1',
					type: 'subform',
					value: [[{ key: 'F3-1-3', type: 'select', optionsKey: '4sStaffList', value: 'uuid-legacy' }]]
				}
			]),
			[]
		)
		assert.equal(values['F3-1'][0]['F3-1-3'], 'uuid-legacy')
	})

	it('IT-NEG-002b: buildAppJsonContent outputs children not value', () => {
		const engine = createFormEngine({
			list: [
				{
					key: 'F3-1',
					label: '洽谈',
					type: 'subform',
					children: [[{ key: 'F3-1-3', type: 'select', optionsKey: '4sStaffList' }]]
				}
			]
		})
		engine.init(
			{
				list: [
					{
						key: 'F3-1',
						label: '洽谈',
						type: 'subform',
						children: [[{ key: 'F3-1-3', type: 'select', optionsKey: '4sStaffList' }]]
					}
				]
			},
			{ baseSceneCode: 110201, brandIds: [3454] }
		)
		engine.setNegotiationGroupFieldValue('F3-1', 0, 'F3-1-3', {
			id: 'uuid-staff-002',
			displayLabel: '李四'
		})

		const visibleList = engine.getVisibleList()
		const cache = engine.getGroupRowsCache()
		const jsonList = buildAppJsonContent(visibleList, cache)

		assert.equal(jsonList.length, 1)
		assert.ok(Array.isArray(jsonList[0].children))
		assert.equal(jsonList[0].value, undefined)
		const staffCell = jsonList[0].children[0].find(f => f.key === 'F3-1-3')
		assert.equal(staffCell.value, '李四')
		assert.deepEqual(staffCell['F3-1-3'], { id: 'uuid-staff-002', name: '李四' })
	})

	it('IT-NEG-002c: buildAppDailySavePayload includes jsonContent and field lists', () => {
		const engine = createFormEngine({
			list: [
				{
					key: 'F3-1',
					label: '洽谈',
					type: 'subform',
					children: [[{ key: 'F3-1-3', type: 'select', optionsKey: '4sStaffList' }]]
				}
			]
		})
		engine.init(
			{
				list: [
					{
						key: 'F3-1',
						label: '洽谈',
						type: 'subform',
						children: [[{ key: 'F3-1-3', type: 'select', optionsKey: '4sStaffList' }]]
					}
				]
			},
			{ baseSceneCode: 110201, brandIds: [3454] }
		)
		engine.setNegotiationGroupFieldValue('F3-1', 0, 'F3-1-3', {
			id: 'uuid-staff-003',
			displayLabel: '王五'
		})

		const payload = buildAppDailySavePayload({
			reportTemplateId: 10003,
			engine
		})

		assert.equal(payload.fieldDataList.length, 0)
		assert.ok(
			payload.subformDataList.some(
				item => item.columnFieldKey === 'F3-1-3' && item.parentFieldKey === 'F3-1'
			)
		)
		const parsed = JSON.parse(payload.jsonContent)
		assert.deepEqual(parsed[0].childrenValue[0]['F3-1']['F3-1-3'], {
			text: '王五',
			value: 'uuid-staff-003'
		})
	})

	it('IT-NEG-002d: subform select with full object saves phone in subformDataList', () => {
		const engine = createFormEngine({
			list: [
				{
					key: 'F3-1',
					label: '洽谈',
					type: 'subform',
					children: [[{ key: 'F3-1-3', type: 'select', optionsKey: '4sStaffList' }]]
				}
			]
		})
		engine.init(
			{
				list: [
					{
						key: 'F3-1',
						label: '洽谈',
						type: 'subform',
						children: [[{ key: 'F3-1-3', type: 'select', optionsKey: '4sStaffList' }]]
					}
				]
			},
			{ baseSceneCode: 110201, brandIds: [3454] }
		)
		const staff = {
			id: '982873a3744e44fbb439432e51edadef',
			name: '林永健test003',
			phone: '17875511831'
		}
		engine.setAppGroupFieldValue('F3-1', 0, 'F3-1-3', {
			metaValue: staff,
			displayLabel: staff.name
		})

		const payload = buildAppDailySavePayload({
			reportTemplateId: 10003,
			engine
		})

		const subformItem = payload.subformDataList.find(
			item => item.parentFieldKey === 'F3-1' && item.columnFieldKey === 'F3-1-3'
		)
		assert.deepEqual(JSON.parse(subformItem.fieldValue), staff)
	})

	it('IT-NEG-005: object select saves full option in field[key]', () => {
		const engine = createFormEngine({
			list: [
				{
					key: 'F3-1',
					label: '洽谈',
					type: 'subform',
					children: [[{ key: 'F3-1-3', type: 'select', optionsKey: '4sStaffList' }]]
				}
			]
		})
		engine.init(
			{
				list: [
					{
						key: 'F3-1',
						label: '洽谈',
						type: 'subform',
						children: [[{ key: 'F3-1-3', type: 'select', optionsKey: '4sStaffList' }]]
					}
				]
			},
			{ baseSceneCode: 110201, brandIds: [3454] }
		)
		engine.setAppGroupFieldValue('F3-1', 0, 'F3-1-3', {
			id: { id: 'uuid-obj', name: '赵六', phone: '13900001111' },
			displayLabel: '赵六'
		})

		const jsonList = buildAppJsonContent(engine.getVisibleList(), engine.getGroupRowsCache())
		const staffCell = jsonList[0].children[0].find(f => f.key === 'F3-1-3')
		assert.equal(staffCell.value, '赵六')
		assert.deepEqual(staffCell['F3-1-3'], {
			id: 'uuid-obj',
			name: '赵六',
			phone: '13900001111'
		})
	})

	it('IT-NEG-003b: mergeAppJsonIntoSchema preserves FormField rows', () => {
		const section = buildAppSubformSection(buildAppCarBrandDisplayString(carInfo))
		const schema = mergeAppJsonIntoSchema(
			{
				list: [
					{
						key: 'F3-1',
						type: 'subform',
						children: [[{ key: 'F3-1-3', type: 'select', optionsKey: '4sStaffList' }]]
					}
				]
			},
			{ jsonContent: JSON.stringify([section]) }
		)
		const subform = schema.list.find(f => f.key === 'F3-1')
		assert.ok(isRowFieldsList(subform.value))
		assert.equal(subform.value[0].find(f => f.key === 'F3-1-3')['F3-1-3'], 'uuid-staff-001')
	})
})