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