negotiation-summary-transform.test.mjs 3 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { buildNegotiationSummaryTables } from '../form-engine/negotiation-summary-transform.js'
import { serializeFormListForAppJsonStorage } from '../form-engine/app-subform-json.js'

describe('negotiation summary transform', () => {
	it('IT-SUMMARY-001: App jsonContent children dual-key → recordRows', () => {
		const jsonContent = JSON.stringify([
			{
				key: 'F3-1',
				label: '洽谈明细',
				type: 'subform',
				children: [
					[
						{
							key: 'F3-1-3',
							label: '推荐人',
							type: 'select',
							value: '张三',
							'F3-1-3': 'uuid-staff-001'
						},
						{
							key: 'F3-1-6',
							label: '车型配置',
							type: 'carBrandSelect',
							value: '问界-鸿蒙-M7-增程版'
						}
					],
					[
						{
							key: 'F3-1-3',
							label: '推荐人',
							type: 'select',
							value: '李四',
							'F3-1-3': 'uuid-staff-002'
						},
						{
							key: 'F3-1-6',
							label: '车型配置',
							type: 'carBrandSelect',
							value: '理想-L9-Max'
						}
					]
				]
			}
		])

		const sections = buildNegotiationSummaryTables(jsonContent)
		assert.equal(sections.length, 1)
		assert.equal(sections[0].label, '洽谈明细')
		assert.equal(sections[0].recordRows.length, 2)

		const row1Staff = sections[0].recordRows[0].fields.find(f => f.label === '推荐人')
		assert.equal(row1Staff.value, '张三')
		const row2Staff = sections[0].recordRows[1].fields.find(f => f.label === '推荐人')
		assert.equal(row2Staff.value, '李四')

		const row1Car = sections[0].recordRows[0].fields.find(f => f.label === '车型配置')
		assert.equal(row1Car.value, '问界-鸿蒙-M7-增程版')
		const row2Car = sections[0].recordRows[1].fields.find(f => f.label === '车型配置')
		assert.equal(row2Car.value, '理想-L9-Max')
	})

	it('IT-SUMMARY-002: empty or invalid jsonContent → []', () => {
		assert.deepEqual(buildNegotiationSummaryTables(''), [])
		assert.deepEqual(buildNegotiationSummaryTables('not-json'), [])
		assert.deepEqual(buildNegotiationSummaryTables(JSON.stringify([{ key: 'F3-1', type: 'radio' }])), [])
	})

	it('IT-SUMMARY-003: childrenValue jsonContent builds same summary as children', () => {
		const withChildren = JSON.stringify([
			{
				key: 'F3-1',
				label: '洽谈明细',
				type: 'subform',
				origin_children: [
					{ key: 'F3-1-3', label: '推荐人', type: 'select' },
					{ key: 'F3-1-6', label: '车型配置', type: 'carBrandSelect' }
				],
				children: [
					[
						{ key: 'F3-1-3', label: '推荐人', type: 'select', value: '张三' },
						{ key: 'F3-1-6', label: '车型配置', type: 'carBrandSelect', value: '问界-M7' }
					]
				]
			}
		])
		const withChildrenValue = JSON.stringify(
			serializeFormListForAppJsonStorage(JSON.parse(withChildren))
		)
		const fromChildren = buildNegotiationSummaryTables(withChildren)
		const fromChildrenValue = buildNegotiationSummaryTables(withChildrenValue)
		assert.deepEqual(fromChildrenValue, fromChildren)
	})
})