Files
SHiNE-server/shine-UI/tests/dm-push-worker.test.cjs
T

55 lines
3.5 KiB
JavaScript

const fs=require('fs');
const path=require('path');
const vm=require('vm');
const assert=require('assert');
const registrations=[];
const events={};
const stateDb=new Map();
const notifications=new Map();
const cacheObj={
async match(key){return stateDb.get(key)?.clone() || null},
async put(key, response){stateDb.set(key, response)},
};
const context={
self:{
__shineStoppedCalls:new Map(),
registration:{scope:'https://shineup.me/',
async showNotification(title, options){ registrations.push({title,options}); if(options.tag) notifications.set(options.tag,{data:options.data, close(){ notifications.delete(options.tag) }}) },
async getNotifications({tag}){return notifications.get(tag)?[notifications.get(tag)]:[]},
},
clients:{async matchAll(){return []}},
addEventListener(name, handler){events[name]=handler},
skipWaiting(){},
},
caches:{async open(){return cacheObj}},
Response, URL, Promise, Date, Number, String, Math, Map, Object, JSON, encodeURIComponent,
};
vm.createContext(context);
vm.runInContext(fs.readFileSync(path.resolve(__dirname, '../firebase-messaging-sw.js'),'utf8'), context);
async function push(data){const pending=[];events.push({data:{text(){return JSON.stringify(data)}}, waitUntil(p){pending.push(p)}});await Promise.all(pending)}
async function read(to,from){const pending=[];events.message({data:{type:'SHINE_DM_READ',toLogin:to,fromLogin:from},waitUntil(p){pending.push(p)}});await Promise.all(pending)}
(async()=>{
for(let i=1;i<=5;i++){
await push({kind:'new_message',toLogin:'AnNa',fromLogin:'Vasya',messageKey:`m${i}`,unreadCount:i,notificationVersionMs:1000+i});
}
assert.strictEqual(registrations.length,5,'every new message renotifies');
assert(registrations.every(x=>x.options.renotify===true && x.options.silent===false));
assert.strictEqual(notifications.size,1,'one tag per conversation');
assert.strictEqual(registrations.at(-1).options.body,'5 непрочитанных сообщений');
await push({kind:'new_message',toLogin:'AnNa',fromLogin:'Kolya',messageKey:'k1',unreadCount:2,notificationVersionMs:2000});
assert.strictEqual(notifications.size,2);
await push({kind:'new_message',toLogin:'AnNa',fromLogin:'Vasya',messageKey:'m5',unreadCount:5,notificationVersionMs:1005});
assert.strictEqual(registrations.length,6,'duplicate push ignored');
await push({kind:'new_message',toLogin:'AnNa',fromLogin:'Vasya',messageKey:'m6',unreadCount:4,notificationVersionMs:1003});
assert.strictEqual(registrations.at(-1).options.body,'5 непрочитанных сообщений','late push cannot downgrade unread count');
await read('AnNa','Vasya');
assert.strictEqual(notifications.size,1,'read closes only Vasya notification');
await push({kind:'new_message',toLogin:'AnNa',fromLogin:'Vasya',messageKey:'m7',unreadCount:4,notificationVersionMs:1004});
assert.strictEqual(notifications.size,1,'old push after read suppressed');
await push({kind:'new_message',toLogin:'AnNa',fromLogin:'Vasya',messageKey:'m8',unreadCount:1,notificationVersionMs:1008});
assert.strictEqual(notifications.size,2,'new push after read can notify');
await push({kind:'new_message',toLogin:'AnNa',fromLogin:'MashA',messageKey:'z1',unreadCount:21,notificationVersionMs:3000});
assert.strictEqual(registrations.at(-1).options.body,'21 непрочитанное сообщение');
console.log('PASS: 5 alerts, tags per conversation, counter, replay suppression, late-push ordering, read close and newer-push delivery');
})().catch(e=>{console.error(e);process.exitCode=1});