field-format-rules.test.mjs
4.79 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
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
describe('field-format-rules', async () => {
const {
validatePhoneFormat,
validateVinSyncFormat,
resolveVinInputMaxLength,
isVinScanField,
shouldCheckVinApi,
VIN_MAX_LENGTH
} = await import('../form-engine/field-format-rules.js')
it('IT-PHONE-001: phone format matches createOrder phoneRex', () => {
assert.equal(validatePhoneFormat('13800138000'), null)
assert.equal(validatePhoneFormat('12800138000'), '请输入正确的手机号')
assert.equal(validatePhoneFormat('1380013800'), '请输入正确的手机号')
assert.equal(validatePhoneFormat(''), null)
})
it('IT-VIN-SYNC-001: vin sync format validation', () => {
assert.equal(validateVinSyncFormat('LSGGF53W8CH045613'), null)
assert.equal(validateVinSyncFormat('LSGGF53W8CH04561'), null)
assert.equal(validateVinSyncFormat('LSGGF53W8CH04561!'), '请输入正确的VIN码')
})
it('IT-VIN-SYNC-002: vin excludes I O Q', () => {
assert.equal(
validateVinSyncFormat('LSGGF53W8CH04561I'),
'请输入最多 20 位有效字符(支持大写 A-H/J-N/P-Z、0-9)'
)
assert.equal(
validateVinSyncFormat('LSGGF53W8CH04561O'),
'请输入最多 20 位有效字符(支持大写 A-H/J-N/P-Z、0-9)'
)
})
it('IT-VIN-SYNC-003: isVinScanField and shouldCheckVinApi', () => {
assert.equal(isVinScanField({ prefixComponent: 'scan' }), true)
assert.equal(isVinScanField({ type: 'input' }), false)
assert.equal(shouldCheckVinApi('LSGGF53W8CH045613'), true)
assert.equal(shouldCheckVinApi('SHORT'), false)
assert.equal(shouldCheckVinApi('LSGGF53W8CH04561'), false)
})
it('IT-VIN-SYNC-004: vin max length 20 and resolveVinInputMaxLength', () => {
const twenty = 'A'.repeat(20)
const twentyOne = 'A'.repeat(21)
assert.equal(validateVinSyncFormat(twenty), null)
assert.match(validateVinSyncFormat(twentyOne), /不能超过20位/)
assert.equal(
resolveVinInputMaxLength({
prefixComponent: 'scan',
rules: { maxLength: 20 }
}),
20
)
assert.equal(resolveVinInputMaxLength({ prefixComponent: 'scan' }), VIN_MAX_LENGTH)
})
})
describe('vin-api-valid-tracker', async () => {
const {
buildVinErrorKey,
shouldRunVinApiValidate,
runVinApiValidate
} = await import('../form-engine/vin-api-valid-tracker.js')
it('IT-VIN-API-001: buildVinErrorKey for subform row', () => {
assert.equal(
buildVinErrorKey({ groupKey: 'F3-1', rowIndex: 0, childKey: 'F3-1-7' }),
'F3-1__0__F3-1-7'
)
})
it('IT-VIN-API-002: runVinApiValidate parses API valid flag', async () => {
assert.equal(shouldRunVinApiValidate('LSGGF53W8CH045613'), true)
const ok = await runVinApiValidate('LSGGF53W8CH045613', async () => ({
data: { data: { valid: true } }
}))
assert.equal(ok.ok, true)
assert.equal(ok.valid, true)
const fail = await runVinApiValidate('LSGGF53W8CH045613', async () => {
throw new Error('fail')
})
assert.equal(fail.ok, false)
assert.equal(fail.valid, false)
})
})
describe('validateForm phone and vin', async () => {
const { validateForm } = await import('../form-engine/validator.js')
const { VIN_API_FAIL_MSG, PHONE_FORMAT_MSG } = await import(
'../form-engine/field-format-rules.js'
)
it('IT-PHONE-002: validateForm phoneInput uses boutique message', () => {
const phoneChild = {
key: 'F3-1-5',
type: 'phoneInput',
label: '客户手机号',
required: true,
brandId: []
}
const list = [
{
key: 'G1',
type: 'subform',
label: '组',
origin_children: [phoneChild],
children: [[phoneChild]]
}
]
const values = { G1: [{ 'F3-1-5': '12800138000' }] }
const groupRowsCache = {
G1: [
[
{
key: 'F3-1-5',
type: 'phoneInput',
label: '客户手机号',
required: true,
value: '12800138000'
}
]
]
}
const result = validateForm(list, {}, values, groupRowsCache)
assert.equal(result.ok, false)
assert.equal(result.errorMap['G1__0__F3-1-5'], PHONE_FORMAT_MSG)
})
it('IT-VIN-VALID-001: vinApiValidMap false blocks submit validation', () => {
const vinChild = {
key: 'F3-1-7',
type: 'input',
label: '车架号',
required: true,
prefixComponent: 'scan',
brandId: []
}
const list = [
{
key: 'G1',
type: 'subform',
label: '组',
origin_children: [vinChild],
children: [[vinChild]]
}
]
const vin = 'LSGGF53W8CH045613'
const values = { G1: [{ 'F3-1-7': vin }] }
const groupRowsCache = {
G1: [
[
{
key: 'F3-1-7',
type: 'input',
label: '车架号',
required: true,
prefixComponent: 'scan',
value: vin
}
]
]
}
const result = validateForm(list, {}, values, groupRowsCache, {
vinApiValidMap: { 'G1__0__F3-1-7': false }
})
assert.equal(result.ok, false)
assert.equal(result.errorMap['G1__0__F3-1-7'], VIN_API_FAIL_MSG)
})
})