menuApiCache.test.mjs
1.7 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
import { describe, it, beforeEach, afterEach } from 'node:test'
import assert from 'node:assert/strict'
import {
cachedHasPerformanceConfig,
cachedGetStaffTargetTask,
cachedCheckMerchantDirect,
clearMenuApiCache
} from '../menu/menuApiCache.js'
describe('menuApiCache', () => {
beforeEach(() => {
clearMenuApiCache()
})
afterEach(() => {
clearMenuApiCache()
})
it('IT-EQ-CACHE-001: hasPerformanceConfig dedupes concurrent calls', async () => {
let calls = 0
const commonApi = {
hasPerformanceConfig: async () => {
calls++
await new Promise(r => setTimeout(r, 10))
return { data: true }
}
}
const [a, b] = await Promise.all([
cachedHasPerformanceConfig(commonApi, 'm1'),
cachedHasPerformanceConfig(commonApi, 'm1')
])
assert.equal(calls, 1)
assert.equal(a.data, true)
assert.equal(b.data, true)
})
it('IT-EQ-CACHE-002: getStaffTargetTask uses TTL cache', async () => {
let calls = 0
const commonApi = {
getStaffTargetTaskApi: async () => {
calls++
return { code: 0, data: [] }
}
}
await cachedGetStaffTargetTask(commonApi, {
merchantId: 'store-1',
sceneType: 0
})
await cachedGetStaffTargetTask(commonApi, {
merchantId: 'store-1',
sceneType: 0
})
assert.equal(calls, 1)
})
it('IT-EQ-CACHE-003: checkMerchantDirect dedupes concurrent calls', async () => {
let calls = 0
const api = {
checkMerchantDirect: async () => {
calls++
return { data: true }
}
}
const [a, b] = await Promise.all([
cachedCheckMerchantDirect(api, { merchantId: 's1', scene: 0 }),
cachedCheckMerchantDirect(api, { merchantId: 's1', scene: 0 })
])
assert.equal(calls, 1)
assert.equal(a.data, true)
assert.equal(b.data, true)
})
})