plan-import-echo-normalize.test.mjs
5.74 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
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { createRequire } from 'node:module'
const require = createRequire(import.meta.url)
const { createFormEngine } = require('../form-engine/core.js')
const { normalizePlanImportEchoValues } = require('../form-engine/plan-import-echo-normalize.js')
const { maybeRestoreCarBrandPlanImportMarker } = require('../form-engine/car-brand-value.js')
const {
hasPlanImportDateMarker,
PLAN_IMPORT_DATE_FIELD_KEY,
wrapPlanImportDate,
unwrapPlanImportDate
} = require('../form-engine/plan-import-date-value.js')
const subformGroupF34 = {
key: 'F3-4',
type: 'subform',
label: '洽谈明细',
rules: { maxLength: 20 },
origin_children: [
{
key: 'F3-4-1',
type: 'datetime',
label: '洽谈日期',
required: true,
rules: { format: 'date', maxDate: 'today' }
},
{ key: 'F3-4-5', type: 'input', label: '客户名称' },
{ key: 'F3-4-7', type: 'carBrandSelect', label: '车型配置', required: true }
],
children: [[]]
}
const partialCarFlat = {
vehicleBrandId: 3454,
vehicleBrandName: '问界',
vehicleSubBrandId: 1,
vehicleSubBrandName: '问界',
vehicleSeriesId: 101,
vehicleSeriesName: 'M7 Plus'
}
const fullCarFlat = {
...partialCarFlat,
vehicleShapeId: 201,
vehicleShapeName: '2024款'
}
function tomorrowYmd() {
const d = new Date()
d.setDate(d.getDate() + 1)
const y = d.getFullYear()
const m = `${d.getMonth() + 1}`.padStart(2, '0')
const day = `${d.getDate()}`.padStart(2, '0')
return `${y}-${m}-${day}`
}
describe('plan-import-echo-normalize', () => {
it('IT-ECHO-PLAN-001: flat three-level carInfo restores partialFromPlanImport', () => {
const restored = maybeRestoreCarBrandPlanImportMarker(partialCarFlat)
assert.equal(restored.partialFromPlanImport, true)
assert.equal(restored.vehicleSeriesId, 101)
assert.equal(restored.vehicleShapeId, undefined)
})
it('IT-ECHO-PLAN-002: level-four carInfo has no import marker', () => {
const restored = maybeRestoreCarBrandPlanImportMarker(fullCarFlat)
assert.equal(restored.partialFromPlanImport, undefined)
assert.equal(restored.vehicleShapeId, 201)
})
it('IT-ECHO-PLAN-003: future F3-4-1 plain string wraps with marker', () => {
const future = tomorrowYmd()
const values = normalizePlanImportEchoValues([subformGroupF34], {
'F3-4': [{ 'F3-4-1': future, 'F3-4-5': '张三' }]
})
assert.equal(hasPlanImportDateMarker(values['F3-4'][0]['F3-4-1']), true)
assert.equal(values['F3-4'][0]['F3-4-1'].value, future)
})
it('IT-ECHO-PLAN-004: parseJsonContentDetailValues echo + engine.init passes validation', async () => {
const { parseJsonContentDetailValues } = await import('../form-engine/submit-adapter.js')
const { validateForm } = await import('../form-engine/validator.js')
const future = tomorrowYmd()
const jsonContent = JSON.stringify([
{
key: 'F3-4',
type: 'subform',
value: [
{
'F3-4-1': future,
'F3-4-5': '张三',
'F3-4-7': { ...partialCarFlat }
}
]
}
])
const values = parseJsonContentDetailValues(jsonContent, [subformGroupF34])
assert.equal(hasPlanImportDateMarker(values['F3-4'][0][PLAN_IMPORT_DATE_FIELD_KEY]), true)
assert.equal(values['F3-4'][0]['F3-4-7'].partialFromPlanImport, true)
const engine = createFormEngine({
list: [subformGroupF34],
version: 1
})
engine.init({ list: [subformGroupF34] }, { initialValues: values })
const state = engine.getViewState()
const validation = validateForm(
[subformGroupF34],
{},
state.values,
state.groupRowsCache
)
const dateErrors = validation.errors.filter(e => e.message?.includes('不能晚于今天'))
const carErrors = validation.errors.filter(e => e.message?.includes('车型配置不能为空'))
assert.equal(dateErrors.length, 0, JSON.stringify(validation.errors))
assert.equal(carErrors.length, 0, JSON.stringify(validation.errors))
})
it('IT-ECHO-PLAN-005: save unwraps plan import date in childrenValue then parse echoes', async () => {
const { parseJsonContentDetailValues, buildDynamicSavePayload } = await import(
'../form-engine/submit-adapter.js'
)
const future = tomorrowYmd()
const wrapped = wrapPlanImportDate(future)
const initialValues = {
'F3-4': [
{
'F3-4-1': wrapped,
'F3-4-5': '张三',
'F3-4-7': { ...partialCarFlat }
}
]
}
const engine = createFormEngine({
list: [subformGroupF34],
version: 1
})
engine.init({ list: [subformGroupF34] }, { initialValues })
const payload = buildDynamicSavePayload({
reportTemplateId: 10003,
engine,
normalizedFieldDataList: true
})
const parsed = JSON.parse(payload.jsonContent)
const f34 = parsed.find(item => item.key === 'F3-4')
assert.ok(f34, 'F3-4 section missing')
const storedDate = f34.childrenValue[0]['F3-4']['F3-4-1']
assert.equal(typeof storedDate, 'string', JSON.stringify(storedDate))
assert.equal(storedDate, future)
const values = parseJsonContentDetailValues(payload.jsonContent, [subformGroupF34])
assert.equal(unwrapPlanImportDate(values['F3-4'][0]['F3-4-1']), future)
assert.equal(hasPlanImportDateMarker(values['F3-4'][0]['F3-4-1']), true)
})
it('IT-ECHO-PLAN-006: bad childrenValue object wrapper still echoes date', async () => {
const { parseJsonContentDetailValues } = await import('../form-engine/submit-adapter.js')
const future = tomorrowYmd()
const badWrapper = wrapPlanImportDate(future)
const jsonContent = JSON.stringify([
{
key: 'F3-4',
type: 'subform',
origin_children: subformGroupF34.origin_children,
childrenValue: [
{
'F3-4': {
'F3-4-1': badWrapper,
'F3-4-5': '张三'
}
}
]
}
])
const values = parseJsonContentDetailValues(jsonContent, [subformGroupF34])
assert.equal(unwrapPlanImportDate(values['F3-4'][0]['F3-4-1']), future)
})
})