serviceCenterNoticeService.test.mjs 2.17 KB
import { describe, it, beforeEach, afterEach } from 'node:test'
import assert from 'node:assert/strict'

describe('serviceCenterNoticeService', async () => {
	const {
		fetchServiceCenterUnread,
		resolveFirstUnreadNotice,
		__setListApiForTest,
		clearServiceCenterNoticeCache
	} = await import('../services/serviceCenterNoticeService.js')

	let apiCalls

	beforeEach(() => {
		apiCalls = []
		clearServiceCenterNoticeCache()
		if (!Date.prototype.format) {
			Date.prototype.format = function () {
				return '2026-06-09'
			}
		}
		globalThis.wx = {}
		__setListApiForTest(async params => {
			apiCalls.push(params.moduleType)
			if (params.moduleType === 6) {
				return { data: { list: [{ id: 1, msgTitle: '课程', reminderType: 1 }] } }
			}
			return { data: { list: [] } }
		})
	})

	afterEach(() => {
		__setListApiForTest(null)
		clearServiceCenterNoticeCache()
	})

	it('IT-EQ-NOTICE-001: fetchServiceCenterUnread caches within session', async () => {
		const a = await fetchServiceCenterUnread()
		const b = await fetchServiceCenterUnread()
		assert.equal(a, 1)
		assert.equal(b, 1)
		assert.equal(apiCalls.length, 3)
	})

	it('IT-EQ-NOTICE-002: resolveFirstUnreadNotice returns first unread by noticeSort', async () => {
		const first = await resolveFirstUnreadNotice()
		assert.ok(first)
		assert.equal(first.moduleType, 6)
		clearServiceCenterNoticeCache()
		__setListApiForTest(async params => {
			apiCalls.push(params.moduleType)
			if (params.moduleType === 6) {
				return { data: { list: [] } }
			}
			if (params.moduleType === 7) {
				return { data: { list: [{ id: 2, msgTitle: '案例', reminderType: 1 }] } }
			}
			return { data: { list: [] } }
		})
		const second = await resolveFirstUnreadNotice()
		assert.ok(second)
		assert.equal(second.moduleType, 7)
	})

	it('IT-EQ-NOTICE-003: probe scans all module types once per cache window', async () => {
		__setListApiForTest(async params => {
			apiCalls.push(params.moduleType)
			if (params.moduleType === 6) {
				return { data: { list: [{ id: 1, msgTitle: '课程', reminderType: 1 }] } }
			}
			return { data: { list: [] } }
		})
		await fetchServiceCenterUnread()
		await resolveFirstUnreadNotice()
		assert.equal(apiCalls.length, 3)
	})
})