menuApiCache.test.mjs
1.97 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
import { describe, it, beforeEach, afterEach } from 'node:test'
import assert from 'node:assert/strict'
import {
cachedHasPerformanceConfig,
cachedCheckMerchantDirect,
invalidateMenuAndApiCaches
} from '../utils/menuApiCache.js'
import {
__setApiClientForTest,
clearDailyReportNewVersionCache,
fetchDailyReportNewVersionMenuVisible
} from '../utils/dailyReportNewVersionGate.js'
describe('utils/menuApiCache', () => {
beforeEach(() => {
invalidateMenuAndApiCaches()
clearDailyReportNewVersionCache()
})
afterEach(() => {
invalidateMenuAndApiCaches()
clearDailyReportNewVersionCache()
__setApiClientForTest(null)
})
it('IT-MENU-CACHE-001: invalidateMenuAndApiCaches clears hasPerformanceConfig TTL', async () => {
let calls = 0
const api = {
hasPerformanceConfig: async () => {
calls++
return { data: true }
}
}
await cachedHasPerformanceConfig(api, 'm1')
await cachedHasPerformanceConfig(api, 'm1')
assert.equal(calls, 1)
invalidateMenuAndApiCaches()
await cachedHasPerformanceConfig(api, 'm1')
assert.equal(calls, 2)
})
it('IT-MENU-CACHE-002: checkMerchantDirect dedupes by merchantId and scene', async () => {
let calls = 0
const api = {
checkMerchantDirect: async params => {
calls++
return { data: params.scene === 1 }
}
}
await Promise.all([
cachedCheckMerchantDirect(api, { merchantId: 'a', scene: 1 }),
cachedCheckMerchantDirect(api, { merchantId: 'a', scene: 1 })
])
assert.equal(calls, 1)
})
it('IT-MENU-CACHE-003: invalidateMenuAndApiCaches clears isNeed gate cache', async () => {
let isNeedCalls = 0
__setApiClientForTest({
dailyReportNewVersionIsNeed: async () => {
isNeedCalls++
return { code: 0, data: true }
}
})
await fetchDailyReportNewVersionMenuVisible('store-a')
await fetchDailyReportNewVersionMenuVisible('store-a')
assert.equal(isNeedCalls, 1)
invalidateMenuAndApiCaches()
await fetchDailyReportNewVersionMenuVisible('store-a')
assert.equal(isNeedCalls, 2)
})
})