cmvr-wms/mobile-app/tests/message-center.test.mjs
lixiaolong d37b4ff379 refactor(mobile): 重构移动端应用结构和配置
- 将 Account 组件中的 Text 导入移动到顶部
- 移除 App.tsx 中的 expo-updates 相关代码
- 添加 PackageUpdates 相关组件和 hooks
- 更新应用名称从 "CMVR 物资管理" 为 "车研物资"
- 添加应用图标和自适应图标配置
- 更新 Android 权限配置,添加 REQUEST_INSTALL_PACKAGES 权限
- 修改堆栈导航数据结构,支持路由参数传递
- 添加阅读尺寸偏好设置功能
- 重构登录界面为独立组件 LoginView
- 添加文件库界面组件 FileLibrary
- 更新状态栏样式逻辑
- 调整 Tab 栏样式和尺寸
- 集成包更新和应用更新对话框
- 修复导入语句顺序和类型引用
- 优化业务列表组件的路由参数处理
- 添加作用域参数用于 API 请求区分
2026-10-28 17:30:35 +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/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();
});