tabIndexMenu.test.mjs
16.9 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/**
* 精品首页 Menu 模块单元测试
*
* 运行:npm run test:tabIndex
* 用例前缀:IT-MENU-*
*/
import { describe, it, beforeEach, afterEach } from 'node:test'
import assert from 'node:assert/strict'
import { setupWxMock, createDefaultAsyncDeps } from './menuTestHelpers.mjs'
const globalRef = globalThis
describe('tabIndexMenu', async () => {
setupWxMock(globalRef)
const { buildTabIndexMenusSync, buildTabIndexMenusAsync } = await import(
'../menu/buildMenus.js'
)
const { createMenuContext, getMenuFingerprint } = await import(
'../menu/context.js'
)
const { createPolicies } = await import('../menu/policies.js')
const { MENU_REGISTRY, ASYNC_MENU_REGISTRY, DYNAMIC_FORM_MENU_IDS } = await import(
'../menu/registry.js'
)
const { registryItemToViewModel } = await import('../menu/toViewModel.js')
const { resolveAsyncMenuProviders, resolveCustomerClue } = await import(
'../menu/asyncProviders.js'
)
const { __setApiClientForTest, clearDailyReportNewVersionCache } =
await import('../../utils/dailyReportNewVersionGate.js')
const { clearMenuApiCache } = await import('../../utils/menuApiCache.js')
let originalGetApp
let originalWx
beforeEach(() => {
clearMenuApiCache()
clearDailyReportNewVersionCache()
__setApiClientForTest(null)
originalGetApp = globalRef.getApp
originalWx = globalRef.wx
setupWxMock(globalRef)
globalRef.getApp = () => ({ globalData: { isStaff: false } })
})
afterEach(() => {
clearDailyReportNewVersionCache()
__setApiClientForTest(null)
globalRef.getApp = originalGetApp
globalRef.wx = originalWx
})
// --- Registry ---
it('IT-MENU-REG-001: MENU_REGISTRY entries have stable id and label', () => {
const ids = new Set()
MENU_REGISTRY.forEach(entry => {
assert.ok(entry.id)
assert.ok(entry.label)
assert.ok(entry.action || entry.provider, `${entry.id} needs action or provider`)
assert.ok(!ids.has(entry.id), `duplicate id ${entry.id}`)
ids.add(entry.id)
})
})
it('IT-MENU-REG-002: ASYNC_MENU_REGISTRY entries have unique id and provider', () => {
const ids = new Set()
ASYNC_MENU_REGISTRY.forEach(entry => {
assert.ok(entry.provider)
assert.ok(!ids.has(entry.id))
ids.add(entry.id)
})
})
it('IT-MENU-REG-002b: dynamic registry identifies the route-backed store daily entry', () => {
assert.deepEqual(
DYNAMIC_FORM_MENU_IDS.map(item => ({ id: item.id, label: item.label })),
[{ id: 'store_daily_report', label: '门店日报' }]
)
})
it('IT-MENU-GUIDE-001: registry ids are stable snake_case', () => {
const idPattern = /^[a-z][a-z0-9_]*$/
const allEntries = [...MENU_REGISTRY, ...ASYNC_MENU_REGISTRY]
allEntries.forEach(entry => {
assert.match(
entry.id,
idPattern,
`${entry.id} must be snake_case (see ADD-MENU-GUIDE.md)`
)
})
})
it('IT-MENU-REG-003: registry entries have required schema fields', () => {
const requiredSync = ['id', 'label', 'icon', 'section', 'action']
MENU_REGISTRY.forEach(entry => {
requiredSync.forEach(key => {
assert.ok(entry[key] != null && entry[key] !== '', `${entry.id} missing ${key}`)
})
assert.equal(typeof entry.visible, 'function', `${entry.id} needs visible(ctx)`)
assert.ok(entry.action?.type, `${entry.id} action.type required`)
})
ASYNC_MENU_REGISTRY.forEach(entry => {
requiredSync.forEach(key => {
assert.ok(entry[key] != null && entry[key] !== '', `${entry.id} missing ${key}`)
})
assert.equal(typeof entry.provider, 'string', `${entry.id} needs provider`)
assert.ok(entry.action?.type, `${entry.id} action.type required`)
})
})
// --- Sync pipeline ---
it('IT-MENU-SYNC-001: technician gets only repair_order menu', () => {
globalRef.getApp = () => ({ globalData: { isStaff: true } })
const result = buildTabIndexMenusSync(
createMenuContext({
userInfo: { loginRole: 7, sceneType: 1 },
isStaff: true
})
)
assert.equal(result.menu.length, 1)
assert.equal(result.menu[0].id, 'repair_order')
assert.equal(result.newMenu.length, 0)
})
it('IT-MENU-SYNC-002: resident staff gets default work order menus', () => {
globalRef.getApp = () => ({ globalData: { isStaff: true } })
const result = buildTabIndexMenusSync(
createMenuContext({
userInfo: { loginRole: 4, sceneType: 1 },
isStaff: true
})
)
const ids = result.menu.map(i => i.id)
assert.ok(ids.includes('create_order'))
assert.ok(ids.includes('staff_daily_report'))
assert.ok(ids.includes('repair_order'))
})
it('IT-MENU-SYNC-003: non-staff non-mentor gets appointment_manage only', () => {
// loginRole 2(园区运营):非驻店、非导师、非 canSelectMultiStore
const result = buildTabIndexMenusSync(
createMenuContext({
userInfo: { loginRole: 2, sceneType: 1, accountName: 'yq_operator' },
isStaff: false
})
)
assert.equal(result.menu.length, 1)
assert.equal(result.menu[0].id, 'appointment_manage')
})
it('IT-MENU-SYNC-004: supervisor role gets BI report', () => {
const result = buildTabIndexMenusSync(
createMenuContext({
userInfo: { loginRole: 3, sceneType: 1 },
isStaff: false
})
)
assert.ok(result.menu.some(i => i.id === 'bi_report'))
})
it('IT-MENU-SYNC-005: super puts salary_performance in newMenu only', () => {
const result = buildTabIndexMenusSync(
createMenuContext({
userInfo: { isSuper: true, loginRole: 1, sceneType: 1 },
isStaff: false
})
)
assert.ok(result.newMenu.some(i => i.id === 'salary_performance_super'))
assert.ok(!result.menu.some(i => i.label === '工资绩效'))
})
it('IT-MENU-SYNC-006: mentorFlag enables script_extract', () => {
const result = buildTabIndexMenusSync(
createMenuContext({
userInfo: { loginRole: 4, sceneType: 1 },
isStaff: true,
mentorFlag: true
})
)
assert.ok(result.menu.some(i => i.id === 'script_extract'))
})
it('IT-MENU-SYNC-007: supervisor keeps one business_assistant entry', () => {
const result = buildTabIndexMenusSync(
createMenuContext({
userInfo: { isSuper: true, loginRole: 1, sceneType: 1 },
isStaff: false
})
)
const bizCount = result.menu.filter(
i => i.id === 'business_assistant_default' || i.id === 'business_assistant_supervisor'
).length
assert.ok(bizCount <= 1)
})
// --- View model ---
it('IT-MENU-VM-001: registryItemToViewModel maps navigate action', () => {
const vm = registryItemToViewModel({
id: 'scene_market',
label: '场景集市',
icon: 'sceneMarket',
section: 'main',
action: {
type: 'navigate',
target: '/packageEquity/pages/sceneMarket/index/index'
}
})
assert.equal(vm.name, '场景集市')
assert.equal(vm.navigateToUrl, '/packageEquity/pages/sceneMarket/index/index')
})
it('IT-MENU-VM-002: registryItemToViewModel maps router extract to path', () => {
const vm = registryItemToViewModel({
id: 'script_extract',
label: '话术萃取',
icon: 'extract_icon',
action: {
type: 'router',
target: 'packageEquity.extract',
params: { type: 1 }
}
})
assert.equal(vm.path, 'packageEquity.extract')
assert.deepEqual(vm.params, { type: 1 })
})
// --- Fingerprint ---
it('IT-MENU-FP-001: getMenuFingerprint stable for same user', () => {
const user = { uuid: 'u1', shopMerchantId: 's1', loginRole: 4 }
assert.equal(getMenuFingerprint(user), getMenuFingerprint(user))
})
it('IT-MENU-FP-002: getMenuFingerprint changes when shopMerchantId changes', () => {
const a = getMenuFingerprint({ uuid: 'u1', shopMerchantId: 's1' })
const b = getMenuFingerprint({ uuid: 'u1', shopMerchantId: 's2' })
assert.notEqual(a, b)
})
it('IT-MENU-FP-003: getMenuFingerprint changes when mentorFlag or isStaff changes', () => {
const user = { uuid: 'u1', shopMerchantId: 's1', loginRole: 4 }
const base = getMenuFingerprint(user, { isStaff: true, mentorFlag: false })
const mentor = getMenuFingerprint(user, { isStaff: true, mentorFlag: true })
const notStaff = getMenuFingerprint(user, { isStaff: false, mentorFlag: false })
assert.notEqual(base, mentor)
assert.notEqual(base, notStaff)
})
it('IT-MENU-FP-004: getMenuFingerprint changes when default4SStoreInfo.merchantId changes', () => {
const a = getMenuFingerprint({
uuid: 'u1',
shopMerchantId: 's1',
default4SStoreInfo: { merchantId: 'store-a' }
})
const b = getMenuFingerprint({
uuid: 'u1',
shopMerchantId: 's1',
default4SStoreInfo: { merchantId: 'store-b' }
})
assert.notEqual(a, b)
})
it('IT-MENU-FP-005: getMenuFingerprint changes when loginRoleOnSelectMerchant changes', () => {
const a = getMenuFingerprint({
uuid: 'u1',
shopMerchantId: 's1',
loginRoleOnSelectMerchant: 3
})
const b = getMenuFingerprint({
uuid: 'u1',
shopMerchantId: 's1',
loginRoleOnSelectMerchant: 4
})
assert.notEqual(a, b)
})
it('IT-MENU-FP-006: getMenuFingerprint changes when includeCustomerClue changes', () => {
const user = { uuid: 'u1', shopMerchantId: 's1' }
const without = getMenuFingerprint(user, { includeCustomerClue: false })
const withClue = getMenuFingerprint(user, { includeCustomerClue: true })
assert.notEqual(without, withClue)
})
// --- Role snapshots (sync menu ids) ---
function syncMenuIds(ctx) {
const result = buildTabIndexMenusSync(ctx)
return [...result.menu, ...result.newMenu].map(i => i.id).sort()
}
it('IT-MENU-SNAP-001: super admin menu id snapshot', () => {
const ids = syncMenuIds(
createMenuContext({
userInfo: { isSuper: true, loginRole: 1, sceneType: 1 },
isStaff: false
})
)
assert.ok(ids.includes('bi_report'))
assert.ok(ids.includes('salary_performance_super'))
assert.ok(!ids.includes('repair_order'))
})
it('IT-MENU-SNAP-002: regional supervisor menu id snapshot', () => {
const ids = syncMenuIds(
createMenuContext({
userInfo: { loginRole: 3, sceneType: 1 },
isStaff: false
})
)
assert.ok(ids.includes('bi_report'))
assert.ok(ids.includes('pending_approval'))
assert.ok(!ids.includes('recruit_manage'))
})
it('IT-MENU-SNAP-003: resident staff menu id snapshot', () => {
globalRef.getApp = () => ({ globalData: { isStaff: true } })
const ids = syncMenuIds(
createMenuContext({
userInfo: { loginRole: 4, sceneType: 1 },
isStaff: true
})
)
assert.ok(ids.includes('create_order'))
assert.ok(ids.includes('abnormal_flow_approval'))
assert.ok(ids.includes('defeat_order'))
assert.ok(ids.includes('repair_order'))
assert.ok(ids.includes('staff_daily_report'))
assert.ok(ids.includes('scan_code_order'))
})
it('IT-MENU-SNAP-004: technician menu id snapshot', () => {
globalRef.getApp = () => ({ globalData: { isStaff: true } })
const ids = syncMenuIds(
createMenuContext({
userInfo: { loginRole: 7, sceneType: 1 },
isStaff: true
})
)
assert.deepEqual(ids, ['repair_order'])
})
it('IT-MENU-SNAP-005: flight mentor menu id snapshot', () => {
const ids = syncMenuIds(
createMenuContext({
userInfo: { loginRole: 6, sceneType: 1 },
isStaff: false
})
)
assert.ok(ids.includes('bi_report'))
assert.ok(ids.includes('scene_market'))
})
// --- Policies ---
it('IT-MENU-POL-001: createPolicies staffOrMultiStoreDaily', () => {
const ctx = createMenuContext({
userInfo: { loginRole: 4 },
isStaff: true
})
assert.equal(ctx.policies.staffOrMultiStoreDaily(), true)
})
// --- Async providers ---
it('IT-MENU-ASYNC-001: salary appeal when hasPerformanceConfig true', async () => {
globalRef.getApp = () => ({ globalData: { isStaff: true } })
const result = await buildTabIndexMenusAsync({
userInfo: { loginRole: 4, sceneType: 1, shopMerchantId: 'm1' },
isStaff: true,
deps: createDefaultAsyncDeps({
tabIndexApi: {
hasPerformanceConfig: async () => ({ data: true })
}
})
})
assert.ok(result.menu.some(i => i.id === 'salary_appeal'))
})
it('IT-MENU-ASYNC-002: super gets salary appeal without API call', async () => {
const result = await buildTabIndexMenusAsync({
userInfo: { isSuper: true, loginRole: 1, sceneType: 1 },
isStaff: false,
deps: createDefaultAsyncDeps({
tabIndexApi: {
hasPerformanceConfig: async () => {
throw new Error('should not call API for super')
}
}
})
})
assert.ok(result.menu.some(i => i.id === 'salary_appeal'))
})
it('IT-MENU-ASYNC-003: script verify when extractCheck returns true', async () => {
const result = await buildTabIndexMenusAsync({
userInfo: { loginRole: 4, sceneType: 1 },
isStaff: true,
deps: createDefaultAsyncDeps({
tabIndexApi: {
extractCheck: async () => true
}
})
})
assert.ok(result.menu.some(i => i.id === 'script_verify'))
})
it('IT-MENU-ASYNC-004: missing reception returns sideEffects', async () => {
globalRef.getApp = () => ({ globalData: { isStaff: true } })
const result = await buildTabIndexMenusAsync({
userInfo: {
loginRole: 4,
sceneType: 1,
shopMerchantId: 'm1',
uuid: 'u1',
merchantId: 'yq1'
},
isStaff: true,
deps: createDefaultAsyncDeps({
tabIndexApi: {
checkMerchantDirect: async () => ({ data: true }),
checkMissingReception: async () => ({
code: 0,
data: [
{
missingReceptionRecord: true,
hasReceptionReport: false,
reportDate: '2026-06-01'
}
]
})
}
})
})
assert.ok(result.menu.some(i => i.id === 'missing_reception'))
assert.equal(result.sideEffects.missingRecordTodoList.length, 1)
})
it('IT-MENU-ASYNC-005: resident target sets taskId sideEffect', async () => {
globalRef.getApp = () => ({ globalData: { isStaff: true } })
const result = await buildTabIndexMenusAsync({
userInfo: {
loginRole: 4,
sceneType: 1,
default4SStoreInfo: { merchantId: 'store1' }
},
isStaff: true,
deps: createDefaultAsyncDeps({
commonApi: {
getStaffTargetTaskApi: async () => ({
code: 0,
data: [{ id: 'task-99', readFlag: 'N', promptFlag: 'N' }]
})
}
})
})
assert.ok(result.menu.some(i => i.id === 'resident_target'))
assert.equal(result.sideEffects.taskId, 'task-99')
assert.equal(result.sideEffects.showTaskDialog, true)
})
it('IT-MENU-ASYNC-006: salary performance via getBusinessSchoolScene', async () => {
const result = await buildTabIndexMenusAsync({
userInfo: { loginRole: 4, sceneType: 1 },
isStaff: true,
deps: createDefaultAsyncDeps({
getBusinessSchoolScene: async () => true
})
})
assert.ok(result.menu.some(i => i.id === 'salary_performance_api'))
})
it('IT-MENU-ASYNC-007: customer clue when exchange flag Y', async () => {
const ctx = createMenuContext({
userInfo: { loginRole: 4, merchantId: 'yq1', sceneType: 1 },
isStaff: true
})
const result = await resolveCustomerClue(ctx, null, {
getYqStoreConfig: async () => ({
data: { customerExchangeFlag: 'Y' }
})
})
assert.equal(result.customerClue.visible, true)
})
it('IT-MENU-ASYNC-008: gate true replaces legacy staff daily with exactly one store daily', async () => {
globalRef.getApp = () => ({ globalData: { isStaff: true } })
__setApiClientForTest({
dailyReportNewVersionIsNeed: async () => ({ code: 0, data: true })
})
const result = await buildTabIndexMenusAsync({
userInfo: {
loginRole: 4,
sceneType: 1,
shopMerchantId: 'merchant-1'
},
isStaff: true,
deps: createDefaultAsyncDeps()
})
const dailyItems = result.menu.filter(i =>
['staff_daily_report', 'store_daily_report'].includes(i.id)
)
assert.deepEqual(
dailyItems.map(item => ({ id: item.id, name: item.name })),
[{ id: 'store_daily_report', name: '门店日报' }]
)
})
it('IT-MENU-ASYNC-008b: gate false preserves legacy staff daily without store daily', async () => {
globalRef.getApp = () => ({ globalData: { isStaff: true } })
__setApiClientForTest({
dailyReportNewVersionIsNeed: async () => ({ code: 0, data: false })
})
const result = await buildTabIndexMenusAsync({
userInfo: {
loginRole: 4,
sceneType: 1,
shopMerchantId: 'merchant-1'
},
isStaff: true,
deps: createDefaultAsyncDeps()
})
assert.equal(result.menu.some(item => item.id === 'staff_daily_report'), true)
assert.equal(result.menu.some(item => item.id === 'store_daily_report'), false)
})
it('IT-MENU-ASYNC-008c: gate error preserves legacy staff daily without store daily', async () => {
globalRef.getApp = () => ({ globalData: { isStaff: true } })
__setApiClientForTest({
dailyReportNewVersionIsNeed: async () => {
throw new Error('gate unavailable')
}
})
const result = await buildTabIndexMenusAsync({
userInfo: {
loginRole: 4,
sceneType: 1,
shopMerchantId: 'merchant-1'
},
isStaff: true,
deps: createDefaultAsyncDeps()
})
assert.equal(result.menu.some(item => item.id === 'staff_daily_report'), true)
assert.equal(result.menu.some(item => item.id === 'store_daily_report'), false)
})
it('IT-MENU-ASYNC-009: technician skips all async providers', async () => {
const result = await resolveAsyncMenuProviders(
createMenuContext({
userInfo: { loginRole: 7, sceneType: 1 },
isStaff: true
}),
{
tabIndexApi: {},
commonApi: {}
}
)
assert.equal(Object.keys(result).length, 0)
})
})