cmvr-wms/mobile-app/tests/message-center.test.mjs
lixiaolong 966df3ed57 feat(app): 添加消息中心功能并优化AI助手体验
- 集成消息中心屏幕和未读消息徽章显示
- 实现消息中心后台轮询和应用状态监听
- 添加应用启动时的消息刷新逻辑
- 更新导航标题配置增加消息中心选项
- 在助手界面改进报价更正说明显示
- 增强AI助手报价单处理和文档识别功能
- 添加管理员消息发送权限控制和验证
- 优化采购参考价格操作的业务规则说明
- 修复AI助手权限验证中的通知操作检查
2026-10-20 13:39:44 +08:00

36 lines
3.9 KiB
JavaScript

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:'<script>literal text only</script>',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('<p>说明</p><script>hidden</script><p>第二段&nbsp;&lt;内容&gt;</p>'),'说明\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/listTop'?{data:[{noticeId:7,noticeTitle:'公告',isRead: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();
});