2026-10-20 13:39:44 +08:00
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 ( ) } ) )
2026-10-28 17:30:35 +08:00
const files = vi . hoisted ( ( ) => ( { download : vi . fn ( ) } ) )
2026-10-20 13:39:44 +08:00
const identity = reactive ( { id : 1 } )
2026-10-21 15:13:39 +08:00
const navigation = vi . hoisted ( ( ) => ( { push : vi . fn ( ) } ) )
2026-10-20 13:39:44 +08:00
vi . mock ( '@/store/modules/user' , ( ) => ( { default : ( ) => identity } ) )
2026-10-21 15:13:39 +08:00
vi . mock ( 'vue-router' , ( ) => ( { useRouter : ( ) => navigation } ) )
2026-10-20 13:39:44 +08:00
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 } ) )
2026-10-28 17:30:35 +08:00
vi . mock ( '@/api/system/files' , ( ) => ( { downloadFile : files . download , fileSize : bytes => bytes + ' B' } ) )
2026-10-20 13:39:44 +08:00
let wrapper
const dom = ( ) => new DOMWrapper ( document . body )
const message = ( id = 7 ) => ( { id , title : '模拟站内消息' , content : '<img src=x onerror="alert(1)">\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
2026-10-28 17:30:35 +08:00
files . download . mockResolvedValue ( )
2026-10-20 13:39:44 +08:00
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 ( '<img' ) ; expect ( dom ( ) . find ( '.message-content img' ) . exists ( ) ) . toBe ( false )
expect ( api . read ) . not . toHaveBeenCalled ( )
api . unread . mockResolvedValue ( { data : { count : 0 , exact : true } } )
await button ( '标为已读' ) . trigger ( 'click' ) ; await flushPromises ( )
expect ( api . read ) . toHaveBeenCalledExactlyOnceWith ( [ 7 ] ) ; expect ( dom ( ) . text ( ) ) . toContain ( '已读' )
await button ( '← 返回列表' ) . trigger ( 'click' ) ; expect ( dom ( ) . find ( '.message-item' ) . classes ( ) ) . toContain ( 'is-read' )
} )
it ( 'empty permission-filtered pages only advance on manual clicks and repeated cursors cannot loop' , async ( ) => {
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 : '<p>公告正文</p><script>not displayed</script>' } } )
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 ( )
2026-10-28 17:30:35 +08:00
} )
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 : '<p><strong>使用说明</strong></p><ol><li>安装</li><li>登录</li><li>下载附件</li></ol><ul><li>补充说明</li></ul><script>hidden-script</script>' } } )
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 ( ) . findAll ( '.message-richcontent ol>li' ) ) . toHaveLength ( 3 ) ; expect ( dom ( ) . findAll ( '.message-richcontent ul>li' ) ) . toHaveLength ( 1 ) ; expect ( dom ( ) . find ( '.message-richcontent strong' ) . text ( ) ) . toBe ( '使用说明' ) ; expect ( dom ( ) . text ( ) ) . not . toContain ( 'hidden-script' ) ; expect ( api . readNotices ) . not . toHaveBeenCalled ( )
2026-10-20 13:39:44 +08:00
} )
2026-10-21 15:13:39 +08:00
it ( 'KPI reminders expose a validated personal assessment link only after opening the message' , async ( ) => {
api . detail . mockResolvedValue ( { data : { ... message ( ) , businessType : 'KPI_REMINDER' , sourcePath : '/kpi/assessment/42' } } )
await open ( ) ; expect ( button ( '查看KPI' ) ) . toBeUndefined ( )
await dom ( ) . find ( '.message-item' ) . trigger ( 'click' ) ; await flushPromises ( )
await button ( '查看KPI' ) . trigger ( 'click' ) ; await flushPromises ( )
expect ( navigation . push ) . toHaveBeenCalledExactlyOnceWith ( '/kpi/assessment/42' )
expect ( api . read ) . not . toHaveBeenCalled ( )
} )
it . each ( [ 'https://example.com' , '//example.com' , '/kpi/assessment/42?redirect=other' , '/system/user' ] ) ( 'a KPI notification cannot navigate to an unexpected path: %s' , async path => {
api . detail . mockResolvedValue ( { data : { ... message ( ) , businessType : 'KPI_REMINDER' , sourcePath : path } } )
await open ( ) ; await dom ( ) . find ( '.message-item' ) . trigger ( 'click' ) ; await flushPromises ( )
expect ( button ( '查看KPI' ) ) . toBeUndefined ( ) ; expect ( navigation . push ) . not . toHaveBeenCalled ( )
} )