negotiationCustomerImport.test.mjs
9.81 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
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 {
buildCustomerRowValueMap,
validateCustomerImportCapacity,
applyCustomersToEngine
} = require('../services/negotiationCustomerImport.js')
const { clearPartialPlanImportMarker } = require('../form-engine/car-brand-value.js')
const {
clearPlanImportDateMarker,
hasPlanImportDateMarker,
wrapPlanImportDate
} = require('../form-engine/plan-import-date-value.js')
const sampleCustomer = {
inviteDate: '2026-06-10',
customerName: '张三',
customerPhone: '13800138000',
advanceUserId: 'staff-1',
advanceUserName: '李璐',
carBrandId: 3454,
carBrand: '问界',
carChildsBrandId: 1,
carChildsBrandName: '问界',
carSeriesId: 101,
carSeriesName: 'M7 Plus'
}
const subformGroup = {
key: 'F3-2',
type: 'subform',
label: '问界鸿蒙洽谈明细 (延保)',
rules: { maxLength: 20 },
origin_children: [
{ key: 'F3-2-1', type: 'date', label: '交车日期' },
{ key: 'F3-2-4', type: 'input', label: '客户名称' },
{ key: 'F3-2-5', type: 'phoneInput', label: '客户手机号' },
{ key: 'F3-2-6', type: 'carBrandSelect', label: '车型配置', required: true },
{
key: 'F3-2-8',
type: 'select',
label: '驻店人员',
optionsKey: 'storeStaffList'
}
],
children: [[]]
}
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: [[]]
}
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('negotiationCustomerImport', () => {
it('IT-PLAN-IMPORT-001: buildCustomerRowValueMap maps F3-2 fields', () => {
const map = buildCustomerRowValueMap(sampleCustomer, 'F3-2')
assert.equal(map['F3-2-4'], '张三')
assert.equal(map['F3-2-5'], '13800138000')
assert.equal(map['F3-2-1'], '2026-06-10')
assert.equal(map['F3-2-8'].id, 'staff-1')
assert.equal(map['F3-2-6'].vehicleSeriesName, 'M7 Plus')
assert.equal(map['F3-2-6'].partialFromPlanImport, true)
})
it('IT-PLAN-IMPORT-002: validateCustomerImportCapacity blocks over 20', () => {
const fail = validateCustomerImportCapacity([subformGroup], { 'F3-2': 18 }, 3)
assert.equal(fail.ok, false)
assert.equal(fail.groupKey, 'F3-2')
assert.match(fail.groupLabel, /问界/)
const ok = validateCustomerImportCapacity([subformGroup], { 'F3-2': 18 }, 2)
assert.equal(ok.ok, true)
})
it('IT-PLAN-IMPORT-004: applyCustomersToEngine no visible subform returns empty label', () => {
const engine = createFormEngine({ list: [], version: 1 })
engine.init({ list: [] }, { initialValues: {} })
const result = applyCustomersToEngine(engine, [sampleCustomer])
assert.equal(result.ok, false)
assert.equal(result.groupKey, '')
assert.equal(result.groupLabel, '')
})
it('IT-PLAN-IMPORT-003: applyCustomersToEngine appends rows', () => {
const engine = createFormEngine({
list: [subformGroup],
version: 1
})
engine.init({ list: [subformGroup] }, { initialValues: {} })
const result = applyCustomersToEngine(engine, [sampleCustomer, sampleCustomer])
assert.equal(result.ok, true)
const state = engine.getViewState()
assert.equal((state.groupRowsCache['F3-2'] || []).length, 2)
})
it('IT-PLAN-IMPORT-005: imported partial carInfo passes carBrandSelect validation', async () => {
const { validateForm } = await import('../form-engine/validator.js')
const engine = createFormEngine({
list: [subformGroup],
version: 1
})
engine.init({ list: [subformGroup] }, { initialValues: {} })
const result = applyCustomersToEngine(engine, [sampleCustomer])
assert.equal(result.ok, true)
const state = engine.getViewState()
const validation = validateForm(
[subformGroup],
{},
state.values,
state.groupRowsCache
)
const carErrors = validation.errors.filter(e => e.label?.includes('车型配置'))
assert.equal(carErrors.length, 0)
})
it('IT-PLAN-IMPORT-006: unmarked three-level carInfo passes validation', async () => {
const { validateForm } = await import('../form-engine/validator.js')
const engine = createFormEngine({
list: [subformGroup],
version: 1
})
engine.init({ list: [subformGroup] }, { initialValues: {} })
const result = applyCustomersToEngine(engine, [sampleCustomer])
assert.equal(result.ok, true)
const carInfo = engine.getViewState().values['F3-2'][0]['F3-2-6']
const cleared = clearPartialPlanImportMarker(carInfo)
const state = engine.mutateSubform(
'F3-2',
0,
'F3-2-6',
{ metaValue: cleared },
{ appDualKey: true }
)
const validation = validateForm([subformGroup], {}, state.values, state.groupRowsCache)
const carErrors = validation.errors.filter(e => e.message?.includes('车型配置不能为空'))
assert.equal(carErrors.length, 0)
})
it('IT-PLAN-IMPORT-007: clearing import marker preserves three-level validation', async () => {
const { validateForm } = await import('../form-engine/validator.js')
const engine = createFormEngine({
list: [subformGroup],
version: 1
})
engine.init({ list: [subformGroup] }, { initialValues: {} })
applyCustomersToEngine(engine, [sampleCustomer])
const carInfo = engine.getViewState().values['F3-2'][0]['F3-2-6']
const cleared = clearPartialPlanImportMarker(carInfo)
assert.equal(cleared.partialFromPlanImport, undefined)
const state = engine.mutateSubform(
'F3-2',
0,
'F3-2-6',
{ metaValue: cleared },
{ appDualKey: true }
)
const validation = validateForm([subformGroup], {}, state.values, state.groupRowsCache)
assert.equal(validation.ok, true)
})
it('IT-PLAN-IMPORT-008: F3-4 inviteDate wrapped with partialFromPlanImport', () => {
const future = tomorrowYmd()
const customer = { ...sampleCustomer, inviteDate: future }
const map = buildCustomerRowValueMap(customer, 'F3-4')
assert.equal(hasPlanImportDateMarker(map['F3-4-1']), true)
assert.equal(map['F3-4-1'].value, future)
const mapF32 = buildCustomerRowValueMap(customer, 'F3-2')
assert.equal(mapF32['F3-2-1'], future)
assert.equal(typeof mapF32['F3-2-1'], 'string')
})
it('IT-PLAN-IMPORT-009: imported future F3-4-1 passes validation until marker cleared', async () => {
const { validateForm } = await import('../form-engine/validator.js')
const future = tomorrowYmd()
const customer = { ...sampleCustomer, inviteDate: future }
const engine = createFormEngine({
list: [subformGroupF34],
version: 1
})
engine.init({ list: [subformGroupF34] }, { initialValues: {} })
applyCustomersToEngine(engine, [customer])
const state = engine.getViewState()
const okValidation = validateForm(
[subformGroupF34],
{},
state.values,
state.groupRowsCache
)
const dateErrors = okValidation.errors.filter(e => e.message?.includes('不能晚于今天'))
assert.equal(dateErrors.length, 0)
const cleared = clearPlanImportDateMarker(state.values['F3-4'][0]['F3-4-1'])
const clearedState = engine.mutateSubform('F3-4', 0, 'F3-4-1', cleared)
const failValidation = validateForm(
[subformGroupF34],
{},
clearedState.values,
clearedState.groupRowsCache
)
const failDateErrors = failValidation.errors.filter(e => e.message?.includes('不能晚于今天'))
assert.equal(failDateErrors.length, 1)
})
it('IT-PLAN-IMPORT-010: validator reads marker from row map when cache value empty', async () => {
const { validateForm } = await import('../form-engine/validator.js')
const future = tomorrowYmd()
const wrapped = wrapPlanImportDate(future)
const groupRowsCache = {
'F3-4': [
[
{
key: 'F3-4-1',
type: 'datetime',
label: '洽谈日期',
required: true,
rules: { format: 'date', maxDate: 'today' },
value: '',
'F3-4-1': wrapped
}
]
]
}
const values = {
'F3-4': [{ 'F3-4-1': wrapped }]
}
const validation = validateForm(
[subformGroupF34],
{},
values,
groupRowsCache
)
const dateErrors = validation.errors.filter(e => e.message?.includes('不能晚于今天'))
assert.equal(dateErrors.length, 0)
})
it('IT-PLAN-IMPORT-011: detail echo without markers restores exemption after parse', 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': {
vehicleBrandId: 3454,
vehicleBrandName: '问界',
vehicleSeriesId: 101,
vehicleSeriesName: 'M7 Plus'
}
}
]
}
])
const values = parseJsonContentDetailValues(jsonContent, [subformGroupF34])
assert.equal(hasPlanImportDateMarker(values['F3-4'][0]['F3-4-1']), 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('不能晚于今天'))
assert.equal(dateErrors.length, 0)
const carErrors = validation.errors.filter(e => e.message?.includes('车型配置不能为空'))
assert.equal(carErrors.length, 0)
})
})