homeRefreshService.test.mjs 1.33 KB
import { describe, it, beforeEach } from 'node:test'
import assert from 'node:assert/strict'
import {
	buildHomeRefreshFingerprint,
	withHomeRefreshDedup,
	clearHomeRefreshState
} from '../services/homeRefreshService.js'

describe('homeRefreshService', () => {
	beforeEach(() => {
		clearHomeRefreshState()
	})

	it('IT-EQ-HOME-001: buildHomeRefreshFingerprint includes dataActive merchant isWait', () => {
		const fp = buildHomeRefreshFingerprint({
			dataActive: 'month',
			shopMerchantId: 'store-1',
			isWait: true
		})
		assert.equal(fp, 'month|store-1|1')
	})

	it('IT-EQ-HOME-002: withHomeRefreshDedup dedupes concurrent calls', async () => {
		let runs = 0
		const fp = buildHomeRefreshFingerprint({
			dataActive: 'today',
			shopMerchantId: 'm1',
			isWait: false
		})
		const task = () =>
			withHomeRefreshDedup(fp, {}, async () => {
				runs++
				await new Promise(r => setTimeout(r, 10))
			})

		await Promise.all([task(), task()])
		assert.equal(runs, 1)
	})

	it('IT-EQ-HOME-003: withHomeRefreshDedup allows sequential tab refresh', async () => {
		let runs = 0
		const fp = buildHomeRefreshFingerprint({
			dataActive: 'today',
			shopMerchantId: 'm1',
			isWait: true
		})
		await withHomeRefreshDedup(fp, {}, async () => {
			runs++
		})
		await withHomeRefreshDedup(fp, {}, async () => {
			runs++
		})
		assert.equal(runs, 2)
	})
})