app-save-lists-from-json.test.mjs
6.62 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
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,
serializeFormListForAppJsonStorage
} 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
}
]
]
}
])
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: '小程序小羊' }
])
})
})