app-save-lists-from-json.test.mjs 16.4 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 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import { createRequire } from 'node:module'
import {
	buildAppNormalizedListsFromJsonContent,
	buildAppFieldDataListFromJsonContent,
	buildAppSubformDataListFromJsonContent,
	extractStorageValuesFromJsonContent,
	getSectionRowFieldsList,
	serializeFormListForAppJsonStorage,
	buildAppJsonContent,
	buildAppDailyReportSubmitPayload
} from '../form-engine/app-subform-json.js'

const require = createRequire(import.meta.url)
const cjsModule = require('../form-engine/app-subform-json.js')

const originChildren = [
	{ key: 'F2-9-1', label: '交付专员', type: 'select', optionsKey: '4sStaffList' },
	{ key: 'F2-9-2', label: '今日交车', type: 'numberInput' }
]

const subformJsonContent = JSON.stringify([
	{
		key: 'F2-9',
		label: '问界鸿蒙售前数据',
		type: 'subform',
		origin_children: originChildren,
		children: [
			[
				{
					key: 'F2-9-1',
					label: '交付专员',
					type: 'select',
					optionsKey: '4sStaffList',
					value: '张三',
					'F2-9-1': 'uuid-staff-001'
				},
				{
					key: 'F2-9-2',
					label: '今日交车',
					type: 'numberInput',
					value: 5
				}
			]
		]
	}
])

function buildStaleCustomerReasonSection() {
	return {
		key: 'F2-107',
		label: '今日非成交客户',
		type: 'subform',
		childrenShowRules: {
			'F2-107-18': {
				keys: ['F2-107-7'],
				result: "val1 && Array.isArray(val1) && val1.includes('1')"
			}
		},
		origin_children: [
			{
				key: 'F2-107-7',
				label: '客户标签',
				type: 'multipleSelect',
				options: [{ label: '已触客', value: '1' }]
			},
			{ key: 'F2-107-18', label: '未成交原因', type: 'input' }
		],
		childrenValue: [{
			'F2-107': {
				'F2-107-7': { text: '已触客', value: ['1'] },
				'F2-107-18': '保存前的旧原因'
			}
		}]
	}
}

const fullJsonContent = JSON.stringify([
	{
		key: 'F2-1',
		label: '今日驻店人员',
		type: 'multipleSelect',
		optionsKey: 'storeStaffList',
		value: [{ id: 'staff-001', name: '张三' }]
	},
	...JSON.parse(subformJsonContent)
])

describe('App save lists from jsonContent', () => {
	it('IT-APP-LISTS-000: app-subform-json.js has no external imports', () => {
		const source = readFileSync(
			new URL('../form-engine/app-subform-json.js', import.meta.url),
			'utf8'
		)
		assert.ok(!/from\s+['"]\.\//.test(source), 'should not import local modules')
	})

	it('IT-APP-LISTS-001: extractStorageValuesFromJsonContent reads subform dual-key rows', () => {
		const { formList, values } = extractStorageValuesFromJsonContent(subformJsonContent)
		assert.equal(formList.length, 1)
		assert.deepEqual(values['F2-9'][0]['F2-9-1'], {
			id: 'uuid-staff-001',
			name: '张三'
		})
		assert.equal(values['F2-9'][0]['F2-9-2'], 5)
	})

	it('IT-APP-LISTS-002: buildAppNormalizedListsFromJsonContent builds subformDataList', () => {
		const { fieldDataList, subformDataList } =
			buildAppNormalizedListsFromJsonContent(subformJsonContent)

		const subformItem = subformDataList.find(
			item =>
				item.parentFieldKey === 'F2-9' &&
				item.columnFieldKey === 'F2-9-1' &&
				item.rowIndex === 0
		)
		assert.ok(subformItem)
		assert.equal(subformItem.fieldType, 'JSON')
		assert.deepEqual(JSON.parse(subformItem.fieldValue), {
			id: 'uuid-staff-001',
			name: '张三'
		})

		assert.ok(!fieldDataList.some(item => item.groupKey === 'F2-9'))
	})

	it('IT-APP-LISTS-003: split builders match combined result', () => {
		const combined = buildAppNormalizedListsFromJsonContent(subformJsonContent)
		const fieldDataList = buildAppFieldDataListFromJsonContent(subformJsonContent)
		const subformDataList = buildAppSubformDataListFromJsonContent(subformJsonContent)
		assert.deepEqual(fieldDataList, combined.fieldDataList)
		assert.deepEqual(subformDataList, combined.subformDataList)
	})

	it('IT-APP-LISTS-004: top-level fields appear in fieldDataList only', () => {
		const { fieldDataList, subformDataList } =
			buildAppNormalizedListsFromJsonContent(fullJsonContent)

		const topLevel = fieldDataList.find(item => item.fieldKey === 'F2-1')
		assert.ok(topLevel)
		assert.equal(topLevel.fieldType, 'JSON')
		assert.deepEqual(JSON.parse(topLevel.fieldValue), [{ id: 'staff-001', name: '张三' }])
		assert.ok(!subformDataList.some(item => item.columnFieldKey === 'F2-1'))
	})

	it('IT-APP-LISTS-005: CommonJS require matches ESM import', () => {
		assert.equal(typeof cjsModule.buildAppNormalizedListsFromJsonContent, 'function')
		const fromEsm = buildAppNormalizedListsFromJsonContent(subformJsonContent)
		const fromCjs = cjsModule.buildAppNormalizedListsFromJsonContent(subformJsonContent)
		assert.deepEqual(fromCjs, fromEsm)
	})

	it('IT-APP-LISTS-006: childrenValue jsonContent yields same normalized lists as children', () => {
		const formList = JSON.parse(subformJsonContent)
		const childrenValueJson = JSON.stringify(serializeFormListForAppJsonStorage(formList))
		const fromChildren = buildAppNormalizedListsFromJsonContent(subformJsonContent)
		const fromChildrenValue = buildAppNormalizedListsFromJsonContent(childrenValueJson)
		assert.deepEqual(fromChildrenValue.fieldDataList, fromChildren.fieldDataList)
		assert.deepEqual(fromChildrenValue.subformDataList, fromChildren.subformDataList)
	})

	it('IT-APP-LISTS-007: empty subform section does not write fieldDataList placeholder', () => {
		const emptySubformJson = JSON.stringify([
			{
				key: 'F3-1',
				label: '洽谈明细',
				type: 'subform',
				origin_children: [{ key: 'F3-1-3', label: '驻店人员', type: 'select' }],
				children: []
			}
		])
		const { fieldDataList } = buildAppNormalizedListsFromJsonContent(emptySubformJson)
		assert.equal(fieldDataList.length, 0)
		assert.ok(!fieldDataList.some(item => item.fieldType === 'subform'))
	})

	it('IT-APP-LISTS-008: subform row data stays in subformDataList only', () => {
		const { fieldDataList, subformDataList } =
			buildAppNormalizedListsFromJsonContent(subformJsonContent)
		assert.ok(!fieldDataList.some(item => item.fieldType === 'subform'))
		assert.ok(!fieldDataList.some(item => item.groupKey === 'F2-9'))
		assert.ok(subformDataList.some(item => item.parentFieldKey === 'F2-9'))
	})

	it('IT-APP-LISTS-F21-001: F2-1 dual-key jsonContent yields fieldDataList with real names', () => {
		const jsonContent = JSON.stringify([
			{
				key: 'F2-1',
				label: '今日驻店人员',
				type: 'multipleSelect',
				optionsKey: 'storeStaffList',
				value: '沸洋洋/新的驻店/小程序小羊',
				'F2-1': [
					'019b8b8a-7b5e-7b0a-8b0a-7b0a8b0a7b0a',
					'019b8b8b-8c6f-8c1b-9c1b-8c1b9c1b8c1b',
					'019b8b8c-9d70-9d2c-ad2c-9d2cad2c9d2c'
				]
			}
		])
		const { fieldDataList } = buildAppNormalizedListsFromJsonContent(jsonContent)
		const f21 = fieldDataList.find(item => item.fieldKey === 'F2-1')
		assert.ok(f21)
		assert.equal(f21.fieldType, 'JSON')
		assert.deepEqual(JSON.parse(f21.fieldValue), [
			{ id: '019b8b8a-7b5e-7b0a-8b0a-7b0a8b0a7b0a', name: '沸洋洋' },
			{ id: '019b8b8b-8c6f-8c1b-9c1b-8c1b9c1b8c1b', name: '新的驻店' },
			{ id: '019b8b8c-9d70-9d2c-ad2c-9d2cad2c9d2c', name: '小程序小羊' }
		])

		const { values } = extractStorageValuesFromJsonContent(jsonContent)
		assert.deepEqual(values['F2-1'], [
			{ id: '019b8b8a-7b5e-7b0a-8b0a-7b0a8b0a7b0a', name: '沸洋洋' },
			{ id: '019b8b8b-8c6f-8c1b-9c1b-8c1b9c1b8c1b', name: '新的驻店' },
			{ id: '019b8b8c-9d70-9d2c-ad2c-9d2cad2c9d2c', name: '小程序小羊' }
		])
	})

	it('IT-APP-LISTS-F2107-001: visible uncontacted reason survives save payload and echo', () => {
		const customerSection = {
			key: 'F2-107',
			label: '非成交客户信息',
			type: 'subform',
			childrenShowRules: {
				'F2-107-17': {
					keys: ['F2-107-7'],
					result: "!Array.isArray(val1) || (val1 && !val1.includes('已触客'))"
				}
			},
			origin_children: [
				{ key: 'F2-107-7', label: '客户标签', type: 'multipleSelect' },
				{ key: 'F2-107-17', label: '未触客原因', type: 'input' }
			]
		}
		const uncontactedRow = [
			{ key: 'F2-107-7', label: '客户标签', type: 'multipleSelect', value: ['已拉群'] },
			{ key: 'F2-107-17', label: '未触客原因', type: 'input', value: '客户未回复' }
		]
		const formList = buildAppJsonContent(
			[customerSection],
			{ 'F2-107': [uncontactedRow] }
		)
		const payload = buildAppDailyReportSubmitPayload(formList)
		const parsed = JSON.parse(payload.jsonContent)

		assert.equal(
			parsed[0].childrenValue[0]['F2-107']['F2-107-17'],
			'客户未回复'
		)
		assert.ok(payload.subformDataList.some(item =>
			item.parentFieldKey === 'F2-107' &&
			item.columnFieldKey === 'F2-107-17' &&
			item.fieldValue === '客户未回复'
		))
		assert.equal(
			extractStorageValuesFromJsonContent(payload.jsonContent)
				.values['F2-107'][0]['F2-107-17'],
			'客户未回复'
		)
	})

	it('IT-APP-LISTS-F2107-002: hidden uncontacted reason stays out of save payload', () => {
		const customerSection = {
			key: 'F2-107',
			label: '非成交客户信息',
			type: 'subform',
			childrenShowRules: {
				'F2-107-17': {
					keys: ['F2-107-7'],
					result: "!Array.isArray(val1) || (val1 && !val1.includes('已触客'))"
				}
			},
			origin_children: [
				{ key: 'F2-107-7', label: '客户标签', type: 'multipleSelect' },
				{ key: 'F2-107-17', label: '未触客原因', type: 'input' }
			]
		}
		const contactedRow = [
			{ key: 'F2-107-7', label: '客户标签', type: 'multipleSelect', value: ['已触客'] },
			{ key: 'F2-107-17', label: '未触客原因', type: 'input', value: '不应保存' }
		]
		const formList = buildAppJsonContent(
			[customerSection],
			{ 'F2-107': [contactedRow] }
		)
		const payload = buildAppDailyReportSubmitPayload(formList)
		const row = JSON.parse(payload.jsonContent)[0].childrenValue[0]['F2-107']

		assert.equal(Object.hasOwn(row, 'F2-107-17'), false)
		assert.equal(payload.subformDataList.some(item =>
			item.parentFieldKey === 'F2-107' && item.columnFieldKey === 'F2-107-17'
		), false)
	})

	it('IT-APP-LISTS-F2107-003: App save filters customer tags by mixed-brand context', () => {
		const customerTagField = {
			key: 'F2-107-7',
			label: '客户标签',
			type: 'multipleSelect',
			options: [
				{ label: '已触客', value: '已触客' },
				{
					label: '已拉群',
					value: '已拉群',
					baseSceneCode: ['100101'],
					brandId: ['3454']
				},
				{
					label: '已交付引荐',
					value: '已交付引荐',
					baseSceneCode: ['100101'],
					filterBrandId: ['3454'],
					matchEffect: 'exclude'
				}
			]
		}
		const customerSection = {
			key: 'F2-107',
			label: '非成交客户信息',
			type: 'subform',
			origin_children: [customerTagField]
		}
		const formList = buildAppJsonContent(
			[customerSection],
			{
				'F2-107': [[{
					...customerTagField,
					value: ['已触客', '已拉群', '已交付引荐']
				}]]
			},
			{
				sceneList: [{ baseSceneCode: '100101' }],
				brandIds: ['3454', '9999']
			}
		)
		const payload = buildAppDailyReportSubmitPayload(formList)
		const storedTags = JSON.parse(payload.jsonContent)[0]
			.childrenValue[0]['F2-107']['F2-107-7']
		assert.deepEqual(storedTags, {
			text: '已触客/已拉群',
			value: ['已触客', '已拉群']
		})

		const tagListItem = payload.subformDataList.find(item =>
			item.parentFieldKey === 'F2-107' && item.columnFieldKey === 'F2-107-7'
		)
		assert.deepEqual(JSON.parse(tagListItem.fieldValue), [
			{ id: '已触客/已拉群', name: '已触客/已拉群' }
		])
	})

	it('IT-APP-LISTS-F2107-004: App save resolves object selections by option id', () => {
		const customerTagField = {
			key: 'F2-107-7',
			label: '客户标签',
			type: 'multipleSelect',
			options: [
				{ id: 'tag-contacted', label: '已触客', value: '已触客' },
				{
					id: 'tag-referral',
					label: '已交付引荐',
					value: '已交付引荐',
					baseSceneCode: ['100101'],
					filterBrandId: ['3454'],
					matchEffect: 'exclude'
				},
				{ id: 'tag-invalid', label: '非法标签', value: '非法标签', matchEffect: 'invalid' }
			]
		}
		const customerSection = {
			key: 'F2-107',
			label: '非成交客户信息',
			type: 'subform',
			origin_children: [customerTagField]
		}
		const formList = buildAppJsonContent(
			[customerSection],
			{
				'F2-107': [[{
					...customerTagField,
					value: [
						{ id: 'tag-contacted', name: '已触客' },
						{ id: 'tag-referral', name: '已交付引荐' },
						{ id: 'tag-invalid', name: '非法标签' },
						{ id: 'obsolete', name: '旧标签' }
					]
				}]]
			},
			{
				sceneList: [{ baseSceneCode: '100101' }],
				brandIds: ['9999']
			}
		)
		const payload = buildAppDailyReportSubmitPayload(formList)
		assert.deepEqual(
			JSON.parse(payload.jsonContent)[0].childrenValue[0]['F2-107']['F2-107-7'],
			{ text: '已触客', value: ['tag-contacted'] }
		)
		const tagListItem = payload.subformDataList.find(item =>
			item.parentFieldKey === 'F2-107' && item.columnFieldKey === 'F2-107-7'
		)
		assert.deepEqual(JSON.parse(tagListItem.fieldValue), [
			{ id: 'tag-contacted', name: '已触客' }
		])

		const savedSection = JSON.parse(payload.jsonContent)[0]
		const echoedRow = getSectionRowFieldsList(savedSection)[0]
		const echoedTags = echoedRow.find(field => field.key === 'F2-107-7')
		assert.deepEqual(echoedTags.value, ['已触客'])
	})

	it('IT-APP-LISTS-F2107-005: object-array customer tags drive dependent childrenShowRules on save', () => {
		const customerSection = {
			key: 'F2-107',
			label: '非成交客户信息',
			type: 'subform',
			childrenShowRules: {
				'F2-107-18': {
					keys: ['F2-107-7'],
					result: "val1 && Array.isArray(val1) && val1.includes('已触客')"
				}
			},
			origin_children: [
				{ key: 'F2-107-7', label: '客户标签', type: 'multipleSelect' },
				{ key: 'F2-107-18', label: '未成交原因', type: 'input' }
			]
		}
		const formList = buildAppJsonContent(
			[customerSection],
			{
				'F2-107': [[
					{
						key: 'F2-107-7',
						label: '客户标签',
						type: 'multipleSelect',
						value: [{ id: 'tag-contacted', name: '已触客' }]
					},
					{
						key: 'F2-107-18',
						label: '未成交原因',
						type: 'input',
						value: '价格原因'
					}
				]]
			}
		)
		const row = JSON.parse(
			buildAppDailyReportSubmitPayload(formList).jsonContent
		)[0].childrenValue[0]['F2-107']

		assert.equal(row['F2-107-18'], '价格原因')
	})

	it('IT-APP-LISTS-F2107-006: dual-key customer tags drive childrenShowRules after App echo', () => {
		const customerSection = {
			key: 'F2-107',
			label: '非成交客户信息',
			type: 'subform',
			childrenShowRules: {
				'F2-107-18': {
					keys: ['F2-107-7'],
					result: "val1 && Array.isArray(val1) && val1.includes('已触客')"
				}
			},
			origin_children: [
				{ key: 'F2-107-7', label: '客户标签', type: 'multipleSelect' },
				{ key: 'F2-107-18', label: '未成交原因', type: 'input' }
			]
		}
		const formList = buildAppJsonContent(
			[customerSection],
			{
				'F2-107': [[
					{
						key: 'F2-107-7',
						label: '客户标签',
						type: 'multipleSelect',
						'F2-107-7': { text: ['已触客'], value: ['已触客'] }
					},
					{
						key: 'F2-107-18',
						label: '未成交原因',
						type: 'input',
						value: '价格原因'
					}
				]]
			}
		)
		const row = JSON.parse(
			buildAppDailyReportSubmitPayload(formList).jsonContent
		)[0].childrenValue[0]['F2-107']

		assert.equal(row['F2-107-18'], '价格原因')
	})

	it('IT-APP-LISTS-CURRENT-SUBFORM-001: clearing a reason does not reuse stale childrenValue', () => {
		const formList = buildAppJsonContent(
			[buildStaleCustomerReasonSection()],
			{
				'F2-107': [[
					{
						key: 'F2-107-7',
						label: '客户标签',
						type: 'multipleSelect',
						options: [{ label: '已触客', value: '1' }],
						value: ['1']
					},
					{ key: 'F2-107-18', label: '未成交原因', type: 'input' }
				]]
			}
		)
		const payload = buildAppDailyReportSubmitPayload(formList)

		assert.ok(!payload.subformDataList.some(item => (
			item.parentFieldKey === 'F2-107' &&
			item.columnFieldKey === 'F2-107-18'
		)))
		const savedRow = JSON.parse(payload.jsonContent)[0]
			.childrenValue[0]['F2-107']
		assert.equal(savedRow['F2-107-18'], '')
		assert.notEqual(savedRow['F2-107-18'], '保存前的旧原因')
	})

	it('IT-APP-LISTS-CURRENT-SUBFORM-002: deleting the current row removes stale childrenValue rows', () => {
		const formList = buildAppJsonContent(
			[buildStaleCustomerReasonSection()],
			{ 'F2-107': [] }
		)
		const payload = buildAppDailyReportSubmitPayload(formList)

		assert.ok(!payload.subformDataList.some(item => item.parentFieldKey === 'F2-107'))
		assert.deepEqual(JSON.parse(payload.jsonContent)[0].childrenValue, [])
	})
})