import { beforeEach, afterEach, expect, it, vi } from 'vitest'
import { reactive } from 'vue'
import { mount, flushPromises, DOMWrapper } from '@vue/test-utils'
import ElementPlus from 'element-plus'
import Center from '@/layout/components/HeaderNotice/index.vue'
const api = vi.hoisted(() => ({ list: vi.fn(), unread: vi.fn(), detail: vi.fn(), read: vi.fn(), notices: vi.fn(), notice: vi.fn(), readNotices: vi.fn() }))
const files = vi.hoisted(() => ({ download: vi.fn() }))
const identity = reactive({ id: 1 })
const navigation = vi.hoisted(() => ({ push: vi.fn() }))
vi.mock('@/store/modules/user', () => ({ default: () => identity }))
vi.mock('vue-router', () => ({ useRouter: () => navigation }))
vi.mock('@/api/system/notification', () => ({ listNotifications: api.list, notificationUnread: api.unread, getNotification: api.detail, readNotificationBatch: api.read }))
vi.mock('@/api/system/notice', () => ({ listNoticeTop: api.notices, getNotice: api.notice, markNoticeReadAll: api.readNotices }))
vi.mock('@/api/system/files', () => ({ downloadFile: files.download, fileSize: bytes => bytes+' B' }))
let wrapper
const dom = () => new DOMWrapper(document.body)
const message = (id = 7) => ({ id, title: '模拟站内消息', content: '\n纯文本正文', readAt: null, deliveredAt: '2026-10-20 11:00' })
const button = label => dom().findAll('button').find(item => item.text() === label)
async function open() {
wrapper = mount(Center, { attachTo: document.body, global: { plugins: [ElementPlus], stubs: { transition: true, SvgIcon: true }, config: { globalProperties: { $can: () => false } } } })
await dom().find('[aria-label="消息中心"]').trigger('click'); await flushPromises()
}
beforeEach(() => {
vi.clearAllMocks(); identity.id = 1
files.download.mockResolvedValue()
api.unread.mockResolvedValue({ data: { count: 1, exact: true } })
api.notices.mockResolvedValue({ data: [] })
api.list.mockResolvedValue({ data: { items: [message()], hasMore: false, nextCursor: null } })
api.detail.mockImplementation(async id => ({ data: message(id) }))
api.read.mockResolvedValue({ code: 200 }); api.readNotices.mockResolvedValue({ code: 200 })
})
afterEach(() => { wrapper?.unmount(); wrapper = null; document.body.innerHTML = '' })
it('opens the personal inbox without marking read or displaying message HTML as executable markup', async () => {
await open(); expect(dom().text()).toContain('站内消息'); expect(dom().text()).toContain('模拟站内消息')
expect(api.list).toHaveBeenCalledExactlyOnceWith({ beforeId: null, unreadOnly: false })
expect(api.read).not.toHaveBeenCalled(); expect(api.readNotices).not.toHaveBeenCalled()
await dom().find('.message-item').trigger('click'); await flushPromises()
expect(dom().find('.message-content').text()).toContain('
{
api.list.mockResolvedValueOnce({ data: { items: [], hasMore: true, nextCursor: 100 } }).mockResolvedValueOnce({ data: { items: [message(90)], hasMore: true, nextCursor: 100 } })
await open(); expect(api.list).toHaveBeenCalledTimes(1); expect(dom().text()).toContain('可继续加载')
await button('加载更早消息').trigger('click'); await flushPromises()
expect(api.list).toHaveBeenCalledTimes(2); expect(api.list.mock.calls[1][0].beforeId).toBe(100)
expect(dom().text()).toContain('分页位置未更新'); expect(button('加载更早消息')).toBeUndefined()
})
it('a failed read acknowledgement preserves unread state and does not hide the error', async () => {
api.read.mockRejectedValueOnce(new Error('暂时断网,请重试'))
await open(); await dom().find('.message-item').trigger('click'); await flushPromises()
await button('标为已读').trigger('click'); await flushPromises()
expect(dom().text()).toContain('暂时断网'); expect(button('标为已读')).toBeDefined()
await button('← 返回列表').trigger('click'); expect(dom().find('.message-item').classes()).not.toContain('is-read')
})
it('switching account clears private content and ignores a late detail response', async () => {
let finish; api.detail.mockImplementation(() => new Promise(resolve => { finish = resolve }))
await open(); await dom().find('.message-item').trigger('click'); await flushPromises()
identity.id = 2; await flushPromises(); finish({ data: { ...message(), content: '上一账号私有正文' } }); await flushPromises()
expect(dom().text()).not.toContain('上一账号私有正文'); expect(dom().text()).not.toContain('模拟站内消息')
})
it('announcement and message read operations stay separate and the badge respects lower-bound counts', async () => {
api.unread.mockResolvedValue({ data: { count: 0, exact: false } })
api.notices.mockResolvedValue({ data: [{ noticeId: 7, noticeTitle: '模拟公告', isRead: false }] })
api.notice.mockResolvedValue({ data: { noticeId: 7, noticeTitle: '模拟公告', noticeContent: '
公告正文
' } }) await open(); expect(dom().find('.notice-badge').text()).toBe('1+') const radio = dom().findAll('input[type="radio"]').find(item => item.element.value === 'notices') await radio.setValue(); await flushPromises(); await dom().find('.message-item').trigger('click'); await flushPromises() expect(dom().text()).toContain('公告正文'); expect(dom().text()).not.toContain('not displayed') await button('标为已读').trigger('click'); await flushPromises() expect(api.readNotices).toHaveBeenCalledExactlyOnceWith('7'); expect(api.read).not.toHaveBeenCalled() }) it('ordinary recipients can see and download saved announcement attachments without management permission', async () => { const attachment={id:'notice-file',name:'库存管理.apk',size:26400542} api.notices.mockResolvedValue({data:[{noticeId:8,noticeTitle:'下载公告',isRead:false}]}) api.notice.mockResolvedValue({data:{noticeTitle:'下载公告',noticeContent:'请下载附件',attachments:[attachment]}}) await open();await dom().findAll('input[type="radio"]').find(item=>item.element.value==='notices').setValue();await flushPromises();await dom().find('.message-item').trigger('click');await flushPromises() expect(dom().find('[aria-label="公告附件"]').text()).toContain('库存管理.apk');expect(dom().text()).toContain('附件(1)');expect(button('移除')).toBeUndefined();expect(button('公告管理')).toBeUndefined() await button('下载').trigger('click');await flushPromises();expect(files.download).toHaveBeenCalledWith(attachment,expect.anything());expect(api.read).not.toHaveBeenCalled();expect(api.readNotices).not.toHaveBeenCalled() await button('← 返回列表').trigger('click');await flushPromises();expect(dom().find('[aria-label="公告附件"]').exists()).toBe(false) }) it('empty announcements and inbox messages do not display stale announcement attachments', async () => { api.notices.mockResolvedValue({data:[{noticeId:8,noticeTitle:'无附件公告',isRead:false}]});api.notice.mockResolvedValue({data:{noticeTitle:'无附件公告',noticeContent:'正文',attachments:[]}}) await open();await dom().findAll('input[type="radio"]').find(item=>item.element.value==='notices').setValue();await flushPromises();await dom().find('.message-item').trigger('click');await flushPromises();expect(dom().find('[aria-label="公告附件"]').exists()).toBe(false) }) it('announcement detail retains numbered steps and bullets instead of flattening editor HTML',async()=>{ api.notices.mockResolvedValue({data:[{noticeId:9,noticeTitle:'操作步骤',isRead:false}]});api.notice.mockResolvedValue({data:{noticeTitle:'操作步骤',noticeContent:'使用说明