import test from 'node:test'; import assert from 'node:assert/strict'; import {createMessageCenter,messageBadge,announcementText} from '../src/core/message-center.ts'; const item={id:7,title:'模拟消息',content:'',readAt:null}; function fixture(handler){const calls=[],states=[];const api={request:async(path,options)=>{calls.push({path,options});return handler(path,options);}};const center=createMessageCenter(api,state=>states.push(state));return {center,calls,states};} test('message badges keep bounded counts honest and native announcement text cannot execute HTML',()=>{ assert.equal(messageBadge(0),'');assert.equal(messageBadge(0,false),'…');assert.equal(messageBadge(7,false),'7+');assert.equal(messageBadge(150),'99+'); assert.equal(announcementText('
说明
第二段 <内容>
'),'说明\n第二段 <内容>'); }); test('list and detail are read-only until the user explicitly acknowledges reading',async()=>{ const {center,calls}=fixture(async path=>path==='/system/notifications'?{data:{items:[item],hasMore:false}}:path.endsWith('/read-batch')?{code:200}:{data:item}); await center.load();await center.detail(item);assert.equal(calls.length,2);assert.ok(calls.every(call=>!call.options?.method));assert.equal(center.snapshot().detail.content,item.content); await center.mark(true);assert.deepEqual(calls[2],{path:'/system/notifications/read-batch',options:{method:'PUT',body:[7]}});assert.equal(center.snapshot().detail.read,true); center.back();assert.ok(center.snapshot().items[0].readAt);center.dispose(); }); test('permission-filtered empty pages advance manually and a non-advancing cursor ends pagination',async()=>{ const {center,calls}=fixture(async()=>({data:{items:[],hasMore:true,nextCursor:100}}));await center.load();assert.equal(calls.length,1);assert.equal(center.snapshot().hasMore,true); await center.load(true);assert.equal(calls.length,2);assert.equal(calls[1].options.query.beforeId,'100');assert.equal(center.snapshot().hasMore,false);assert.match(center.snapshot().error,/分页位置/); await center.load(true);assert.equal(calls.length,2); }); test('late inbox responses cannot overwrite a different tab or a disposed screen',async()=>{ let resolve;const {center}=fixture(path=>path==='/system/notifications'?new Promise(done=>{resolve=done;}):Promise.resolve({data:[{noticeId:7,noticeTitle:'模拟公告',isRead:false}]})); const pending=center.load();await center.tab('notices');resolve({data:{items:[item],hasMore:false}});await pending;assert.equal(center.snapshot().tab,'notices');assert.equal(center.snapshot().items.length,0); const other=center.tab('inbox');center.dispose();resolve({data:{items:[item]}});await other;assert.equal(center.snapshot().items.length,0); }); test('a failed acknowledgement keeps unread messages intact and never changes other message families',async()=>{ const {center,calls}=fixture(async path=>{if(path.endsWith('/read-batch'))throw new Error('离线');return {data:{items:[item],hasMore:false}};}); await center.load();await center.mark();assert.match(center.snapshot().error,/离线/);assert.equal(center.snapshot().items[0].readAt,null);assert.equal(calls.filter(call=>call.options?.method).length,1); }); test('announcements use only their own read API, and remounting after disposal safely restarts',async()=>{ const {center,calls}=fixture(async path=>path==='/system/notifications'?{data:{items:[],hasMore:false}}:path==='/system/notice/published'?{data:{items:[{noticeId:7,noticeTitle:'公告',isRead:false}],hasMore:false}}:{code:200}); await center.tab('notices');await center.mark();assert.equal(calls.at(-1).path,'/system/notice/markReadAll');assert.deepEqual(calls.at(-1).options,{method:'POST',query:{ids:'7'}}); center.dispose();center.start();await new Promise(resolve=>setImmediate(resolve));assert.equal(center.snapshot().busy,false);assert.equal(center.snapshot().tab,'inbox');center.dispose(); });