menuApiCache.test.mjs 1.97 KB
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)
	})
})