storeDailyReportDetail.test.mjs
10.2 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
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import { createRequire } from 'node:module'
const require = createRequire(import.meta.url)
const {
parseJsonContentDetailValues,
mergeValuesIntoSchema
} = await import('../form-engine/submit-adapter.js')
const { createFormEngine } = await import('../form-engine/core.js')
const { isReportSubmitted, normalizeReportStatus } = require('../utils/report-status.js')
const {
buildStoreDailyDetailResponseData
} = require('../utils/store-daily-detail-response.js')
const { DEFAULT_REPORT_TEMPLATE_ID } = require('../api/mockStoreDynamicDetail.js')
const { loadSchemaFixture } = await import('./schemaFixture.mjs')
const storeDailyApiUrl = new URL('../api/storeDailyReport.js', import.meta.url)
function loadStoreDailyApi(overrides = {}) {
const apiRequire = createRequire(storeDailyApiUrl)
const actualAdapters = apiRequire('./storeDailyReportAdapters.js')
const requireForApi = specifier => {
if (specifier === '~/config.js') {
return { envIndex: 0 }
}
if (specifier === './dynamicDailyReport.js') {
return { dynamicDailyReportSave: async () => ({ code: 0, data: {} }) }
}
if (specifier === './storeDailyReportAdapters.js') {
return { ...actualAdapters, ...overrides }
}
return apiRequire(specifier)
}
const source = readFileSync(storeDailyApiUrl, 'utf8').replace(
/import\s*\{([\s\S]*?)\}\s*from\s*['"]([^'"]+)['"]/g,
(_match, imports, specifier) => `const {${imports}} = require('${specifier}')`
)
const module = { exports: {} }
new Function('require', 'module', 'exports', 'wx', source)(
requireForApi,
module,
module.exports,
{}
)
return module.exports
}
function selectValueMapId(value) {
if (value != null && typeof value === 'object' && !Array.isArray(value)) {
return value.id ?? value.value
}
return value
}
function normalizeDetailFixture(detail = {}, reportTemplateId) {
const jsonContent = detail.jsonContent || ''
return buildStoreDailyDetailResponseData({
detail,
reportTemplateId,
defaultReportTemplateId: DEFAULT_REPORT_TEMPLATE_ID,
jsonContent,
values: parseJsonContentDetailValues(jsonContent, [])
})
}
describe('storeDailyReport detail response', () => {
it('IT-STORE-DETAIL-007: valid detail jsonContent is the schema snapshot without loading the current template', async () => {
let currentTemplateCalls = 0
const api = loadStoreDailyApi({
fetchSchemaRootAdapter: async () => {
currentTemplateCalls += 1
return {
code: 0,
message: 'ok',
data: {
list: [
{ key: 'F2-101', label: '新模板字段', type: 'numberInput' }
]
}
}
}
})
const historicalList = [
{
label: '其他品牌售后部数据 (轻改)',
key: 'F2-58',
type: 'subform',
rules: { maxRows: 20 },
origin_children: [
{
label: 'SA',
key: 'F2-58-1',
type: 'select',
optionsKey: '4sStaffList'
},
{ label: '今日谈客数', key: 'F2-58-2', type: 'numberInput' },
{ label: '今日成交数', key: 'F2-58-3', type: 'numberInput' },
{ label: '今日成交金额', key: 'F2-58-4', type: 'numberInput' }
],
childrenValue: [
{
'F2-58': {
'F2-58-1': {
text: '测试1230622',
value: '1a1ba1477f684b7886be8aa01440a260'
},
'F2-58-2': 2,
'F2-58-3': 3,
'F2-58-4': 15
}
}
]
}
]
const result = await api.fetchSchema({
reportTemplateId: 10002,
baseSceneCode: 100101,
merchantId: 'm-old-template',
detail: { jsonContent: JSON.stringify(historicalList) }
})
assert.equal(currentTemplateCalls, 0)
assert.equal(result.code, 0)
assert.deepEqual(result.data.list.map(field => field.key), ['F2-58'])
assert.equal(result.data.list[0].label, '其他品牌售后部数据 (轻改)')
assert.equal(
result.initialValues['F2-58'][0]['F2-58-1'].id,
'1a1ba1477f684b7886be8aa01440a260'
)
assert.equal(result.initialValues['F2-58'][0]['F2-58-2'], 2)
assert.equal(result.initialValues['F2-58'][0]['F2-58-3'], 3)
assert.equal(result.initialValues['F2-58'][0]['F2-58-4'], 15)
})
it('IT-STORE-DETAIL-008: incomplete detail schema falls back to the current template', async () => {
let currentTemplateCalls = 0
const api = loadStoreDailyApi({
fetchSchemaRootAdapter: async () => {
currentTemplateCalls += 1
return {
code: 0,
message: 'ok',
data: {
list: [
{ key: 'F2-101', label: '当前模板字段', type: 'numberInput' }
]
}
}
}
})
const result = await api.fetchSchema({
reportTemplateId: 10002,
baseSceneCode: 100101,
merchantId: 'm-invalid-snapshot',
detail: {
jsonContent: JSON.stringify([
{ key: 'F2-58', label: '损坏的历史子表单', type: 'subform' }
])
}
})
assert.equal(currentTemplateCalls, 1)
assert.equal(result.code, 0)
assert.deepEqual(result.data.list.map(field => field.key), ['F2-101'])
})
it('IT-STORE-DETAIL-009: detail snapshot revalidation skips only the current schema refresh', async () => {
const calls = { brand: 0, scene: 0, schema: 0 }
const api = loadStoreDailyApi({
fetchBrandAdapter: async () => {
calls.brand += 1
return { code: 0, data: [] }
},
fetchSceneAdapter: async () => {
calls.scene += 1
return { code: 0, data: [] }
},
fetchSchemaRootAdapter: async () => {
calls.schema += 1
return { code: 0, data: { list: [] } }
}
})
await api.revalidateCachedMetadata({
userContext: { merchantId: 'm-old-template', staffUuid: 's1' },
baseSceneCode: 100101,
reportTemplateId: 10002,
skipSchema: true
})
assert.deepEqual(calls, { brand: 1, scene: 1, schema: 0 })
})
it('IT-STORE-DETAIL-001: normalizeDetailResponse maps reportStatus', () => {
const data = normalizeDetailFixture(
{
id: 99001,
reportTemplateId: 10002,
reportStatus: 1,
jsonContent: JSON.stringify([{ key: 'F2-2', type: 'numberInput', value: 3 }])
},
10002
)
assert.equal(data.dailyReportRecordId, 99001)
assert.equal(data.reportStatus, '1')
assert.equal(data.values['F2-2'], 3)
})
it('IT-STORE-DETAIL-002: empty detail defaults reportStatus to 0', () => {
const data = normalizeDetailFixture({}, 10002)
assert.equal(data.reportStatus, '0')
assert.equal(data.dailyReportRecordId, null)
assert.equal(data.reportTemplateId, DEFAULT_REPORT_TEMPLATE_ID)
})
it('IT-STORE-DETAIL-004: F2-1 echoes from jsonContent through schema merge and engine init', () => {
const storeSchema = loadSchemaFixture('store')
const detail = normalizeDetailFixture({
jsonContent: JSON.stringify([
{ key: 'F2-2', type: 'numberInput', value: 3 },
{ key: 'F2-1', type: 'multipleSelect', value: ['staff-001', 'staff-002'] }
])
})
const values = parseJsonContentDetailValues(detail.jsonContent, storeSchema.list)
const mergedSchema = mergeValuesIntoSchema(storeSchema, values)
const engine = createFormEngine()
const viewState = engine.init(mergedSchema, {
baseSceneCode: 110201,
brandIds: [3067, 3454],
sceneList: [{ baseSceneCode: 110201 }]
})
assert.deepEqual(viewState.values['F2-1'], ['staff-001', 'staff-002'])
assert.equal(viewState.values['F2-2'], 3)
})
it('IT-STORE-DETAIL-006: F2-1 dual key jsonContent echoes id and name pairs', () => {
const storeSchema = loadSchemaFixture('store')
const detail = normalizeDetailFixture({
jsonContent: JSON.stringify([
{ key: 'F2-2', type: 'numberInput', value: 3 },
{
key: 'F2-1',
type: 'multipleSelect',
optionsKey: 'storeStaffList',
value: '张三/李四',
'F2-1': ['staff-001', 'staff-002']
}
])
})
const values = parseJsonContentDetailValues(detail.jsonContent, storeSchema.list)
const mergedSchema = mergeValuesIntoSchema(storeSchema, values)
const engine = createFormEngine()
const viewState = engine.init(mergedSchema, {
baseSceneCode: 110201,
brandIds: [3067, 3454],
sceneList: [{ baseSceneCode: 110201 }]
})
assert.deepEqual(viewState.values['F2-1'], [
{ id: 'staff-001', name: '张三' },
{ id: 'staff-002', name: '李四' }
])
})
it('IT-STORE-SUB-001: F2-9 dual rows echo via valueMap jsonContent', () => {
const storeSchema = loadSchemaFixture('store')
const detail = normalizeDetailFixture({
jsonContent: JSON.stringify([
{
key: 'F2-9',
type: 'subform',
value: [
{ 'F2-9-1': '4s-001', 'F2-9-2': 1 },
{ 'F2-9-1': '4s-002', 'F2-9-2': 2 }
]
}
])
})
const values = parseJsonContentDetailValues(detail.jsonContent, storeSchema.list)
const mergedSchema = mergeValuesIntoSchema(storeSchema, values)
const engine = createFormEngine()
const viewState = engine.init(mergedSchema, {
baseSceneCode: 110201,
brandIds: [3067, 3454],
sceneList: [{ baseSceneCode: 110201 }]
})
assert.equal(viewState.values['F2-9'].length, 2)
assert.equal(viewState.groupRowsCache['F2-9'].length, 2)
assert.equal(viewState.values['F2-9'][1]['F2-9-1'], '4s-002')
})
it('IT-STORE-DETAIL-005: F2-9-1 delivery specialist echoes from jsonContent App children', () => {
const storeSchema = loadSchemaFixture('store')
const detail = normalizeDetailFixture({
jsonContent: JSON.stringify([
{
key: 'F2-9',
type: 'subform',
children: [
[
{
key: 'F2-9-1',
type: 'select',
optionsKey: '4sStaffList',
value: '交付专员B',
'F2-9-1': '4s-002'
},
{ key: 'F2-9-2', type: 'numberInput', value: 1 }
]
]
}
])
})
const values = parseJsonContentDetailValues(detail.jsonContent, storeSchema.list)
const mergedSchema = mergeValuesIntoSchema(storeSchema, values)
const engine = createFormEngine()
const viewState = engine.init(mergedSchema, {
baseSceneCode: 110201,
brandIds: [3067, 3454],
sceneList: [{ baseSceneCode: 110201 }]
})
assert.equal(selectValueMapId(viewState.values['F2-9'][0]['F2-9-1']), '4s-002')
})
})
describe('report-status utils', () => {
it('IT-STORE-DETAIL-003: isReportSubmitted handles string and number', () => {
assert.equal(isReportSubmitted('1'), true)
assert.equal(isReportSubmitted(1), true)
assert.equal(isReportSubmitted('0'), false)
assert.equal(isReportSubmitted(null), false)
assert.equal(normalizeReportStatus(undefined), '0')
})
})