fieldOptionsAdapters.test.mjs
9.84 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import { describe, it, afterEach } from 'node:test'
import assert from 'node:assert/strict'
import { createRequire } from 'node:module'
const require = createRequire(import.meta.url)
const {
STORE_DAILY_API_SWITCH,
STAFF_DAILY_API_SWITCH,
NEGOTIATION_DAILY_API_SWITCH
} = require('../config/apiSwitch.js')
const { fetchFieldOptionsAdapter } = require('../api/fieldOptionsAdapters.js')
const {
mapEmployeeListToOptions,
fetchStoreStaffListOptions,
__setEmployeeListApi,
__setOrderListApi
} = require('../api/fieldOptionProviders.js')
const { clearFieldOptionsCache } = require('../api/fieldOptionsService.js')
const { MOCK_STORE_STAFF_LIST } = require('../fixtures/mock-field-options.js')
describe('fieldOptionProviders', () => {
afterEach(() => {
__setEmployeeListApi(null)
})
it('IT-FIELD-OPT-001: mapEmployeeListToOptions maps staffName and uuid', () => {
assert.deepEqual(
mapEmployeeListToOptions([
{ staffName: '张三', uuid: 'u-1', phone: '13800138000' },
{ name: '李四', staffUuid: 'u-2' }
]),
[
{ label: '张三', value: 'u-1', id: 'u-1', name: '张三', phone: '13800138000' },
{ label: '李四', value: 'u-2', id: 'u-2', name: '李四' }
]
)
})
it('IT-FIELD-OPT-002: fetchStoreStaffListOptions requires merchantId', async () => {
const res = await fetchStoreStaffListOptions({})
assert.equal(res.code, -1)
assert.equal(res.message, '缺少门店信息')
})
it('IT-FIELD-OPT-003: onTheJob API receives merchantId only (no sceneList)', async () => {
let captured = null
__setEmployeeListApi({
getEmployeeList: async params => {
captured = params
return {
code: 0,
message: 'ok',
data: [{ staffName: '王五', uuid: 'u-3' }]
}
}
})
const res = await fetchStoreStaffListOptions({
merchantId: 'yq-1',
sceneType: 1,
reportType: 'staff'
})
assert.equal(res.code, 0)
assert.deepEqual(captured, { merchantId: 'yq-1' })
assert.deepEqual(res.data, [{ label: '王五', value: 'u-3', id: 'u-3', name: '王五' }])
})
it('IT-FIELD-OPT-003b: all reportTypes omit sceneList even when sceneType set', async () => {
for (const reportType of ['staff', 'store', 'negotiation']) {
let captured = null
__setEmployeeListApi({
getEmployeeList: async params => {
captured = params
return { code: 0, message: 'ok', data: [] }
}
})
await fetchStoreStaffListOptions({
merchantId: 'shop-1',
sceneType: 1,
reportType
})
assert.deepEqual(captured, { merchantId: 'shop-1' }, reportType)
}
})
})
describe('fieldOptionsAdapters', () => {
const originalStoreFieldOptions = STORE_DAILY_API_SWITCH.fieldOptions
const originalStaffFieldOptions = STAFF_DAILY_API_SWITCH.fieldOptions
const originalNegotiationFieldOptions = NEGOTIATION_DAILY_API_SWITCH.fieldOptions
afterEach(() => {
STORE_DAILY_API_SWITCH.fieldOptions = originalStoreFieldOptions
STAFF_DAILY_API_SWITCH.fieldOptions = originalStaffFieldOptions
NEGOTIATION_DAILY_API_SWITCH.fieldOptions = originalNegotiationFieldOptions
__setEmployeeListApi(null)
__setOrderListApi(null)
clearFieldOptionsCache()
})
it('IT-FIELD-OPT-004: mock mode returns fixture for storeStaffList', async () => {
STORE_DAILY_API_SWITCH.fieldOptions = 'mock'
const res = await fetchFieldOptionsAdapter({
optionsKey: 'storeStaffList',
reportType: 'store',
merchantId: 'm1'
})
assert.equal(res.code, 0)
assert.deepEqual(res.data, MOCK_STORE_STAFF_LIST)
})
it('IT-FIELD-OPT-005: real mode routes storeStaffList with yqMerchantId', async () => {
STORE_DAILY_API_SWITCH.fieldOptions = 'real'
let captured = null
__setEmployeeListApi({
getEmployeeList: async params => {
captured = params
return {
code: 0,
message: 'ok',
data: [{ staffName: '赵六', uuid: 'u-4' }]
}
}
})
const res = await fetchFieldOptionsAdapter({
optionsKey: 'storeStaffList',
reportType: 'store',
merchantId: 'shop-2',
yqMerchantId: 'yq-store',
sceneType: 1
})
assert.deepEqual(captured, { merchantId: 'yq-store' })
assert.equal(res.code, 0)
assert.deepEqual(res.data, [{ label: '赵六', value: 'u-4', id: 'u-4', name: '赵六' }])
})
it('IT-FIELD-OPT-005b: store 4sStaffList still uses shop merchantId', async () => {
STORE_DAILY_API_SWITCH.fieldOptions = 'real'
let captured = null
__setEmployeeListApi({
getEmployeeList: async params => {
captured = params
return { code: 0, message: 'ok', data: [] }
}
})
await fetchFieldOptionsAdapter({
optionsKey: '4sStaffList',
reportType: 'store',
merchantId: 'shop-2',
yqMerchantId: 'yq-store',
sceneType: 1
})
assert.deepEqual(captured, { merchantId: 'shop-2' })
})
it('IT-FIELD-OPT-006: real mode routes 4sStaffList to employee API without sceneList', async () => {
STORE_DAILY_API_SWITCH.fieldOptions = 'real'
let captured = null
__setEmployeeListApi({
getEmployeeList: async params => {
captured = params
return {
code: 0,
message: 'ok',
data: [{ staffName: '交付专员甲', uuid: 'u-5' }]
}
}
})
const res = await fetchFieldOptionsAdapter({
optionsKey: '4sStaffList',
reportType: 'store',
merchantId: 'shop-3',
sceneType: 1
})
assert.deepEqual(captured, { merchantId: 'shop-3' })
assert.equal(res.code, 0)
assert.deepEqual(res.data, [{ label: '交付专员甲', value: 'u-5', id: 'u-5', name: '交付专员甲' }])
})
it('IT-FIELD-OPT-008: 4sStaffList passes name to getEmployeeList for popup search', async () => {
STORE_DAILY_API_SWITCH.fieldOptions = 'real'
let captured = null
__setEmployeeListApi({
getEmployeeList: async params => {
captured = params
return {
code: 0,
message: 'ok',
data: [{ staffName: '张三', uuid: 'u-8' }]
}
}
})
const res = await fetchFieldOptionsAdapter({
optionsKey: '4sStaffList',
reportType: 'negotiation',
merchantId: 'shop-3',
name: '张'
})
assert.deepEqual(captured, { merchantId: 'shop-3', name: '张' })
assert.equal(res.code, 0)
assert.deepEqual(res.data, [{ label: '张三', value: 'u-8', id: 'u-8', name: '张三' }])
})
it('IT-FIELD-OPT-008b: storeStaffList passes name with yqMerchantId for popup search', async () => {
STAFF_DAILY_API_SWITCH.fieldOptions = 'real'
let captured = null
__setEmployeeListApi({
getEmployeeList: async params => {
captured = params
return {
code: 0,
message: 'ok',
data: [{ staffName: '驻店张', uuid: 'u-9' }]
}
}
})
const res = await fetchFieldOptionsAdapter({
optionsKey: 'storeStaffList',
reportType: 'store',
merchantId: 'shop-4s',
yqMerchantId: 'yq-park',
name: '张'
})
assert.deepEqual(captured, { merchantId: 'yq-park', name: '张' })
assert.equal(res.code, 0)
assert.deepEqual(res.data, [{ label: '驻店张', value: 'u-9', id: 'u-9', name: '驻店张' }])
})
it('IT-FIELD-OPT-007: storeStaffList uses yqMerchantId over merchantId (all pages)', async () => {
STAFF_DAILY_API_SWITCH.fieldOptions = 'real'
let captured = null
__setEmployeeListApi({
getEmployeeList: async params => {
captured = params
return {
code: 0,
message: 'ok',
data: [{ staffName: '驻店甲', uuid: 'u-6' }]
}
}
})
const res = await fetchFieldOptionsAdapter({
optionsKey: 'storeStaffList',
reportType: 'staff',
merchantId: 'shop-4s',
yqMerchantId: 'yq-park',
sceneType: 1
})
assert.deepEqual(captured, { merchantId: 'yq-park' })
assert.equal(res.code, 0)
assert.deepEqual(res.data, [{ label: '驻店甲', value: 'u-6', id: 'u-6', name: '驻店甲' }])
})
it('IT-FIELD-OPT-007b: staff falls back to merchantId when yqMerchantId missing', async () => {
STAFF_DAILY_API_SWITCH.fieldOptions = 'real'
let captured = null
__setEmployeeListApi({
getEmployeeList: async params => {
captured = params
return { code: 0, message: 'ok', data: [] }
}
})
await fetchFieldOptionsAdapter({
optionsKey: 'storeStaffList',
reportType: 'staff',
merchantId: 'shop-fallback',
sceneType: 1
})
assert.deepEqual(captured, { merchantId: 'shop-fallback' })
})
it('IT-NEG-001: negotiation orderList real mode routes goodsOrder API', async () => {
NEGOTIATION_DAILY_API_SWITCH.fieldOptions = 'real'
let captured = null
__setOrderListApi({
getGoodsOrderOutputValueList: async params => {
captured = params
return {
code: 0,
message: 'ok',
data: [{ orderCode: 'order-001', productName: '延保订单A' }]
}
}
})
const res = await fetchFieldOptionsAdapter({
optionsKey: 'orderList',
reportType: 'negotiation',
merchantId: 'shop-neg',
staffUuid: 'staff-uuid-1',
reportDate: '2026-06-02',
sceneList: [{ baseSceneCode: 110201 }, { baseSceneCode: 110202 }],
carBrandTypeFlag: '2',
groupBrandIds: [3067]
})
assert.deepEqual(captured, {
merchantId: 'shop-neg',
sellerUserId: 'staff-uuid-1',
carBrandTypeFlag: '2',
saleDate: ''
})
assert.equal(captured.baseGoodsCodeList, undefined)
assert.equal(res.code, 0)
const row = res.data[0]
assert.equal(row.label, 'order-001')
assert.equal(row.value, 'order-001')
assert.equal(row.raw?.productName, '延保订单A')
})
it('IT-NEG-006b: orderList adapter passes explicit saleDate only', async () => {
clearFieldOptionsCache()
NEGOTIATION_DAILY_API_SWITCH.fieldOptions = 'real'
let captured = null
__setOrderListApi({
getGoodsOrderOutputValueList: async params => {
captured = params
return { code: 0, message: 'ok', data: [] }
}
})
await fetchFieldOptionsAdapter({
optionsKey: 'orderList',
reportType: 'negotiation',
merchantId: 'shop-neg',
staffUuid: 'staff-uuid-1',
reportDate: '2026-06-02',
saleDate: '2026-06-15',
sceneList: [{ baseSceneCode: 110101 }],
carBrandTypeFlag: '1',
groupBrandIds: [3454]
})
assert.equal(captured.saleDate, '2026-06-15')
assert.equal(captured.baseGoodsCodeList, undefined)
assert.equal(captured.carBrandTypeFlag, '1')
})
})