menuSync.test.mjs
1.89 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
import { describe, it, after } from 'node:test'
import assert from 'node:assert/strict'
import { setupWxMock } from './menuTestHelpers.mjs'
const globalRef = globalThis
const originalWx = globalRef.wx
describe('tabIndex menuSyncHelpers', async () => {
setupWxMock(globalRef)
after(() => {
globalRef.wx = originalWx
})
const { buildHomeMenuFingerprint, createMenuRebuildCoordinator } =
await import('../services/menuSyncHelpers.js')
const { getMenuFingerprint } = await import('../menu/context.js')
const user = { uuid: 'u1', shopMerchantId: 's1' }
it('IT-MENU-SYNC-001: coordinator dedupes concurrent rebuilds', async () => {
const coord = createMenuRebuildCoordinator()
let calls = 0
const fingerprint = buildHomeMenuFingerprint(getMenuFingerprint, user, {
isStaff: true,
includeCustomerClue: false
})
const rebuild = async () => {
calls++
await new Promise(r => setTimeout(r, 20))
}
await Promise.all([
coord.sync({ fingerprint, rebuild }),
coord.sync({ fingerprint, rebuild }),
coord.sync({ fingerprint, rebuild })
])
assert.equal(calls, 1)
assert.equal(coord.getStoredFingerprint(), fingerprint)
})
it('IT-MENU-SYNC-002: same fingerprint skips second sync', async () => {
const coord = createMenuRebuildCoordinator()
let calls = 0
const fingerprint = buildHomeMenuFingerprint(getMenuFingerprint, user, {
includeCustomerClue: true
})
await coord.sync({
fingerprint,
rebuild: async () => {
calls++
}
})
await coord.sync({
fingerprint,
rebuild: async () => {
calls++
}
})
assert.equal(calls, 1)
})
it('IT-MENU-SYNC-003: includeCustomerClue changes fingerprint', () => {
const without = buildHomeMenuFingerprint(getMenuFingerprint, user, {
includeCustomerClue: false
})
const withClue = buildHomeMenuFingerprint(getMenuFingerprint, user, {
includeCustomerClue: true
})
assert.notEqual(without, withClue)
})
})