store-app-json-echo.test.mjs 7.61 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { getSubformRowTemplate } from '../form-engine/field-types.js'
import {
	mergeAppJsonIntoSchema,
	serializeFormListForAppJsonStorage
} from '../form-engine/app-subform-json.js'
import {
	mergeValuesIntoSchema,
	parseJsonContentDetailValues
} from '../form-engine/submit-adapter.js'
import { createFormEngine } from '../form-engine/core.js'
import { buildDailyReportSaveRequest } from '../form-engine/save-request.js'
import { DAILY_REPORT_TEMPLATE_TYPE } from '../config/dailyReportConstants.js'
import { isRowFieldsList } from '../form-engine/group-row-adapter.js'

function selectValueMapId(value) {
	if (value != null && typeof value === 'object' && !Array.isArray(value)) {
		return value.id ?? value.value
	}
	return value
}
import { loadSchemaFixture } from './schemaFixture.mjs'

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

const schemaRoot = {
	list: [
		{
			key: 'F2-9',
			label: '问界鸿蒙售前数据',
			type: 'subform',
			children: []
		}
	]
}

describe('store App jsonContent subform echo', () => {
	it('IT-STORE-ECHO-001: getSubformRowTemplate falls back to origin_children', () => {
		const template = getSubformRowTemplate({
			key: 'F2-9',
			type: 'subform',
			children: [],
			origin_children: originChildren
		})
		assert.equal(template.length, 2)
		assert.equal(template[0].key, 'F2-9-1')
	})

	it('IT-STORE-ECHO-002: mergeAppJsonIntoSchema syncs origin_children and children template', () => {
		const jsonContent = JSON.stringify([
			{
				key: 'F2-9',
				type: 'subform',
				children: [],
				origin_children: originChildren
			}
		])
		const merged = mergeAppJsonIntoSchema(schemaRoot, { jsonContent })
		const subform = merged.list.find(f => f.key === 'F2-9')
		assert.ok(Array.isArray(subform.origin_children))
		assert.equal(subform.origin_children.length, 2)
		assert.ok(Array.isArray(subform.children[0]))
		assert.equal(subform.children[0].length, 2)
	})

	it('IT-STORE-ECHO-003: App dual-key row in jsonContent merges into schema value', () => {
		const jsonContent = JSON.stringify([
			{
				key: 'F2-9',
				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
						}
					]
				]
			}
		])
		const merged = mergeAppJsonIntoSchema(schemaRoot, { jsonContent })
		const subform = merged.list.find(f => f.key === 'F2-9')
		assert.ok(isRowFieldsList(subform.value))
		assert.equal(subform.value[0].find(f => f.key === 'F2-9-1').value, '张三')
		assert.equal(subform.value[0].find(f => f.key === 'F2-9-1')['F2-9-1'], 'uuid-staff-001')
	})

	it('IT-STORE-ECHO-004: parseJsonContentDetailValues reads App children rows', () => {
		const jsonContent = JSON.stringify([
			{
				key: 'F2-9',
				type: 'subform',
				origin_children: originChildren,
				children: [
					[
						{
							key: 'F2-9-1',
							type: 'select',
							optionsKey: '4sStaffList',
							value: '交付专员A',
							'F2-9-1': 'uuid-staff-002'
						},
						{ key: 'F2-9-2', type: 'numberInput', value: 3 }
					]
				]
			}
		])
		let schema = mergeAppJsonIntoSchema(schemaRoot, { jsonContent })
		const values = parseJsonContentDetailValues(jsonContent, schemaRoot.list)
		schema = mergeValuesIntoSchema(schema, values)
		const engine = createFormEngine()
		engine.init(schema, { baseSceneCode: 110201, brandIds: [3454] })
		const rows = engine.getGroupRows('F2-9')
		assert.equal(rows.length, 1)
		assert.equal(selectValueMapId(rows[0]['F2-9-1']), 'uuid-staff-002')
		assert.equal(rows[0]['F2-9-2'], 3)
	})

	it('IT-STORE-ECHO-005: legacy subform.value in jsonContent merges when children empty', () => {
		const jsonContent = JSON.stringify([
			{
				key: 'F2-9',
				type: 'subform',
				children: [],
				origin_children: originChildren,
				value: [{ 'F2-9-1': '4s-001', 'F2-9-2': 2 }]
			}
		])
		const values = parseJsonContentDetailValues(jsonContent, schemaRoot.list)
		assert.equal(values['F2-9'].length, 1)
		assert.equal(selectValueMapId(values['F2-9'][0]['F2-9-1']), '4s-001')
		assert.equal(values['F2-9'][0]['F2-9-2'], 2)
	})

	it('IT-STORE-ECHO-006: normalizeJsonContentList accepts { list: [] } wrapper', () => {
		const wrapped = {
			list: [
				{
					key: 'F2-9',
					type: 'subform',
					children: [
						[
							{ key: 'F2-9-1', type: 'select', value: '4s-001' },
							{ key: 'F2-9-2', type: 'numberInput', value: 4 }
						]
					]
				}
			]
		}
		const values = parseJsonContentDetailValues(JSON.stringify(wrapped), schemaRoot.list)
		assert.equal(selectValueMapId(values['F2-9'][0]['F2-9-1']), '4s-001')
		assert.equal(values['F2-9'][0]['F2-9-2'], 4)
	})

	it('IT-STORE-ECHO-007: template-only jsonContent (empty children, no lists) yields no subform rows', () => {
		const jsonContent = JSON.stringify([
			{
				key: 'F2-9',
				type: 'subform',
				children: [],
				origin_children: originChildren
			}
		])
		const values = parseJsonContentDetailValues(jsonContent, schemaRoot.list)
		assert.equal(values['F2-9'], undefined)
		let schema = mergeAppJsonIntoSchema(schemaRoot, { jsonContent })
		schema = mergeValuesIntoSchema(schema, values)
		const engine = createFormEngine()
		engine.init(schema, { baseSceneCode: 110201, brandIds: [3454] })
		assert.equal(engine.getGroupRows('F2-9').length, 0)
	})

	it('IT-STORE-ECHO-008: save jsonContent includes populated subform childrenValue when form filled', () => {
		const storeSchema = loadSchemaFixture('store')
		const engine = createFormEngine(storeSchema)
		engine.setPageContext({
			baseSceneCode: 110201,
			brandIds: [3454, 3067],
			merchantId: '1001',
			sceneList: [{ baseSceneCode: 110201 }]
		})
		engine.setAppGroupFieldValue('F2-9', 0, 'F2-9-1', {
			id: '4s-001',
			displayLabel: '交付专员A'
		})
		engine.setGroupFieldValue('F2-9', 0, 'F2-9-2', 5)

		const { request } = buildDailyReportSaveRequest({
			reportDate: '2026-06-02',
			userContext: { merchantId: 1001, staffUuid: 's1' },
			pageContext: { merchantId: 1001, brandIds: [3454] },
			engine,
			reportTemplateId: 10002,
			templateType: DAILY_REPORT_TEMPLATE_TYPE.store
		})

		const parsed = JSON.parse(request.jsonContent)
		const f29 = parsed.find(item => item.key === 'F2-9')
		assert.ok(f29, 'F2-9 should be in save jsonContent')
		assert.ok(
			Array.isArray(f29.childrenValue) && f29.childrenValue.length > 0,
			'save should write subform rows'
		)
		assert.equal(f29.childrenValue[0]['F2-9']['F2-9-2'], 5)
	})

	it('IT-STORE-ECHO-009: childrenValue jsonContent echoes via parseJsonContentDetailValues', () => {
		const originChildren = [
			{ key: 'F2-9-1', label: '交付专员', type: 'select', optionsKey: '4sStaffList' },
			{ key: 'F2-9-2', label: '今日交车', type: 'numberInput' }
		]
		const jsonContent = JSON.stringify(
			serializeFormListForAppJsonStorage([
				{
					key: 'F2-9',
					type: 'subform',
					origin_children: originChildren,
					children: [
						[
							{
								key: 'F2-9-1',
								type: 'select',
								optionsKey: '4sStaffList',
								value: '交付专员A',
								'F2-9-1': { id: '4s-001', name: '交付专员A' }
							},
							{ key: 'F2-9-2', type: 'numberInput', value: 5 }
						]
					]
				}
			])
		)
		const values = parseJsonContentDetailValues(jsonContent, [])
		assert.equal(values['F2-9'][0]['F2-9-2'], 5)
		assert.equal(selectValueMapId(values['F2-9'][0]['F2-9-1']), '4s-001')
	})
})