fieldOptionsService.test.mjs
5.26 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
/**
* fieldOptionsService 进程内缓存 — useCache 开关
*/
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 { NEGOTIATION_DAILY_API_SWITCH } = require('../config/apiSwitch.js')
const { fetchFieldOptions, clearFieldOptionsCache } = require('../api/fieldOptionsService.js')
const { __setOrderListApi, __setEmployeeListApi } = require('../api/fieldOptionProviders.js')
const ORDER_LIST_PARAMS = {
optionsKey: 'orderList',
reportType: 'negotiation',
merchantId: 'shop-neg',
staffUuid: 'staff-uuid-1',
reportDate: '2026-06-02',
saleDate: '2026-06-15',
carBrandTypeFlag: '2',
groupBrandIds: [3067],
sceneList: [{ baseSceneCode: 110201 }]
}
const STORE_STAFF_LIST_PARAMS = {
optionsKey: 'storeStaffList',
reportType: 'store',
yqMerchantId: 'yq-shop-1',
merchantId: 'shop-1',
staffUuid: 'staff-uuid-1',
reportDate: '2026-06-02'
}
describe('fieldOptionsService', () => {
afterEach(() => {
clearFieldOptionsCache()
__setOrderListApi(null)
__setEmployeeListApi(null)
})
it('IT-NEG-012: useCache false always hits adapter (orderList popup refresh)', async () => {
NEGOTIATION_DAILY_API_SWITCH.fieldOptions = 'real'
let callCount = 0
__setOrderListApi({
getGoodsOrderOutputValueList: async () => {
callCount += 1
return {
code: 0,
message: 'ok',
data: [{ orderCode: `order-${callCount}`, productName: '延保订单' }]
}
}
})
const first = await fetchFieldOptions(ORDER_LIST_PARAMS, { useCache: false })
const second = await fetchFieldOptions(ORDER_LIST_PARAMS, { useCache: false })
assert.equal(callCount, 2)
assert.equal(first.data[0].value, 'order-1')
assert.equal(second.data[0].value, 'order-2')
})
it('IT-NEG-012b: useCache true reuses cached orderList result', async () => {
NEGOTIATION_DAILY_API_SWITCH.fieldOptions = 'real'
let callCount = 0
__setOrderListApi({
getGoodsOrderOutputValueList: async () => {
callCount += 1
return {
code: 0,
message: 'ok',
data: [{ orderCode: 'order-cached', productName: '延保订单' }]
}
}
})
const first = await fetchFieldOptions(ORDER_LIST_PARAMS, { useCache: true })
const second = await fetchFieldOptions(ORDER_LIST_PARAMS, { useCache: true })
assert.equal(callCount, 1)
assert.equal(first.data[0].value, 'order-cached')
assert.equal(second.data[0].value, 'order-cached')
})
it('IT-FIELD-OPT-009: useCache false always hits adapter (storeStaffList popup refresh)', async () => {
const { STORE_DAILY_API_SWITCH } = require('../config/apiSwitch.js')
STORE_DAILY_API_SWITCH.fieldOptions = 'real'
let callCount = 0
__setEmployeeListApi({
getEmployeeList: async () => {
callCount += 1
return {
code: 0,
message: 'ok',
data: [{ staffName: `员工${callCount}`, uuid: `u-${callCount}` }]
}
}
})
const first = await fetchFieldOptions(STORE_STAFF_LIST_PARAMS, { useCache: false })
const second = await fetchFieldOptions(STORE_STAFF_LIST_PARAMS, { useCache: false })
assert.equal(callCount, 2)
assert.equal(first.data[0].value, 'u-1')
assert.equal(second.data[0].value, 'u-2')
})
it('IT-FIELD-OPT-010: 4sStaffList name keyword uses separate cache key', async () => {
const { STORE_DAILY_API_SWITCH } = require('../config/apiSwitch.js')
STORE_DAILY_API_SWITCH.fieldOptions = 'real'
let callCount = 0
__setEmployeeListApi({
getEmployeeList: async params => {
callCount += 1
const label = params.name ? `员工-${params.name}` : '员工-全量'
return {
code: 0,
message: 'ok',
data: [{ staffName: label, uuid: `u-${callCount}` }]
}
}
})
const baseParams = {
optionsKey: '4sStaffList',
reportType: 'store',
merchantId: 'shop-1'
}
const full = await fetchFieldOptions(baseParams, { useCache: true })
const searched = await fetchFieldOptions({ ...baseParams, name: '张' }, { useCache: true })
const fullAgain = await fetchFieldOptions(baseParams, { useCache: true })
assert.equal(callCount, 2)
assert.equal(full.data[0].label, '员工-全量')
assert.equal(searched.data[0].label, '员工-张')
assert.equal(fullAgain.data[0].label, '员工-全量')
})
it('IT-FIELD-OPT-010b: storeStaffList name keyword uses separate cache key', async () => {
const { STORE_DAILY_API_SWITCH } = require('../config/apiSwitch.js')
STORE_DAILY_API_SWITCH.fieldOptions = 'real'
let callCount = 0
__setEmployeeListApi({
getEmployeeList: async params => {
callCount += 1
const label = params.name ? `驻店-${params.name}` : '驻店-全量'
return {
code: 0,
message: 'ok',
data: [{ staffName: label, uuid: `u-${callCount}` }]
}
}
})
const baseParams = {
optionsKey: 'storeStaffList',
reportType: 'store',
yqMerchantId: 'yq-shop-1',
merchantId: 'shop-1'
}
const full = await fetchFieldOptions(baseParams, { useCache: true })
const searched = await fetchFieldOptions({ ...baseParams, name: '李' }, { useCache: true })
const fullAgain = await fetchFieldOptions(baseParams, { useCache: true })
assert.equal(callCount, 2)
assert.equal(full.data[0].label, '驻店-全量')
assert.equal(searched.data[0].label, '驻店-李')
assert.equal(fullAgain.data[0].label, '驻店-全量')
})
})