json-content-persist.test.mjs
5.28 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
/**
* jsonContent 场景持久化与回显单测
* 运行:npm run test:dynamicForm
*/
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { loadSchemaFixture } from './schemaFixture.mjs'
describe('jsonContentPersist', async () => {
const { createFormEngine } = await import('../form-engine/core.js')
const { buildDynamicSavePayload } = await import('../form-engine/submit-adapter.js')
const { getVisibleList } = await import('../form-engine/visibility.js')
const { enrichPageContextWithDetailEcho } = await import(
'../form-engine/app-subform-json.js'
)
const { mergeTemplateDefaultsIntoInitialValues } = await import(
'../form-engine/values.js'
)
const staffSchema = loadSchemaFixture('staff')
const boutiqueCtx = {
baseSceneCode: 100101,
brandIds: [3454],
merchantId: '100',
sceneList: [{ baseSceneCode: 100101 }]
}
const warrantyCtx = {
baseSceneCode: 110201,
brandIds: [3454],
merchantId: '100',
sceneList: [{ baseSceneCode: 110201 }]
}
function assertGroupChildrenKeys(group, expectedKeys, label) {
const childKeys = (group?.children || []).map(item => item.key).sort()
assert.deepEqual(childKeys, [...expectedKeys].sort(), label)
}
it('IT-STAFF-SAVE-SCENE-001: boutique save excludes warranty-only fields and template-0 cross-scene groups', () => {
const initialValues = mergeTemplateDefaultsIntoInitialValues(
staffSchema.list,
{ 'F1-1': '在岗' }
)
const engine = createFormEngine(staffSchema)
engine.init(staffSchema, { ...boutiqueCtx, initialValues })
const payload = buildDynamicSavePayload({
reportTemplateId: 1001,
engine,
reportDate: '2026-06-15',
normalizedFieldDataList: true
})
const jsonKeys = JSON.parse(payload.jsonContent).map(item => item.key)
assert.ok(jsonKeys.includes('F1-1'))
assert.ok(!jsonKeys.includes('F1-2'), '延保专属 F1-2 不应入库')
const warrantyGroup = staffSchema.list.find(item => item.key === 'F1-6')
if (warrantyGroup) {
assert.ok(!jsonKeys.includes('F1-6'), '跨场景 brand group 仅模板 0 不应入库')
}
})
it('IT-STAFF-SAVE-SCENE-002: boutique save group children exclude warranty sub-fields', () => {
const engine = createFormEngine(staffSchema)
engine.init(staffSchema, boutiqueCtx)
engine.setFieldValue('F1-1', '在岗')
engine.setFieldValue('F1-5-1', 1)
engine.setFieldValue('F1-5-2', 2)
engine.setFieldValue('F1-5-3', 3)
const payload = buildDynamicSavePayload({
reportTemplateId: 1001,
engine,
reportDate: '2026-06-15',
normalizedFieldDataList: true
})
const f15 = JSON.parse(payload.jsonContent).find(item => item.key === 'F1-5')
assert.ok(f15)
assertGroupChildrenKeys(
f15,
['F1-5-1', 'F1-5-2', 'F1-5-3'],
'精品保存不应写入延保子字段'
)
})
it('IT-STAFF-SAVE-SCENE-003: warranty save group children exclude boutique sub-fields', () => {
const engine = createFormEngine(staffSchema)
engine.init(staffSchema, warrantyCtx)
engine.setFieldValue('F1-1', '在岗')
engine.setFieldValue('F1-5-4', 4)
engine.setFieldValue('F1-5-5', 5)
engine.setFieldValue('F1-5-6', 6)
const payload = buildDynamicSavePayload({
reportTemplateId: 1001,
engine,
reportDate: '2026-06-15',
normalizedFieldDataList: true
})
const f15 = JSON.parse(payload.jsonContent).find(item => item.key === 'F1-5')
assert.ok(f15)
assertGroupChildrenKeys(
f15,
['F1-5-4', 'F1-5-5', 'F1-5-6'],
'延保保存不应写入精品子字段'
)
const jsonKeys = JSON.parse(payload.jsonContent).map(item => item.key)
assert.ok(jsonKeys.includes('F1-2'), '延保顶层字段应入库')
})
it('IT-ECHO-SCENE-001: wrong-scene group in jsonContent does not expand children via forceEchoBlock', () => {
const warrantyGroup = staffSchema.list.find(
item =>
item.type === 'group' &&
item.baseSceneCode?.length &&
!item.baseSceneCode.includes(100101)
)
if (!warrantyGroup) {
return
}
const jsonContent = JSON.stringify([
{ key: 'F1-1', type: 'radio', label: '出勤', value: '在岗' },
{
key: warrantyGroup.key,
type: 'group',
label: warrantyGroup.label,
children: warrantyGroup.children?.map(child => ({
...child,
value: child.type === 'numberInput' ? 0 : child.value
}))
}
])
const ctx = enrichPageContextWithDetailEcho(
{ ...boutiqueCtx },
{ jsonContent }
)
const values = { 'F1-1': '在岗' }
const visible = getVisibleList(staffSchema.list, ctx, values)
const leaked = visible.find(item => item.key === warrantyGroup.key)
assert.equal(leaked, undefined, '跨场景 group 不应出现在可见列表')
})
it('IT-ECHO-SCENE-002: off-duty brand group still persists when user entered values', () => {
const engine = createFormEngine(staffSchema)
engine.init(staffSchema, boutiqueCtx)
engine.setFieldValue('F1-1', '在岗')
engine.setFieldValue('F1-5-1', 2)
engine.setFieldValue('F1-1', '调休')
const payload = buildDynamicSavePayload({
reportTemplateId: 1001,
engine,
reportDate: '2026-06-15',
normalizedFieldDataList: true
})
const jsonContent = JSON.parse(payload.jsonContent)
const f15 = jsonContent.find(item => item.key === 'F1-5')
assert.ok(f15, '调休时应持久化已填品牌 group')
assertGroupChildrenKeys(
f15,
['F1-5-1', 'F1-5-2', 'F1-5-3'],
'调休持久化 group 仍只含精品子字段'
)
})
})