SHA256
Добавить предпросмотр DM в push-уведомлениях
This commit is contained in:
@@ -3,6 +3,14 @@ const path=require('path');
|
||||
const vm=require('vm');
|
||||
const assert=require('assert');
|
||||
const registrations=[];
|
||||
let previewHidden = false;
|
||||
let previewCalls = 0;
|
||||
let previewText = '';
|
||||
let liveEvents = [];
|
||||
let previewHook = null;
|
||||
let activeSession = { login: 'AnNa', sessionId: 'session', serverUrl: 'wss://example.test/ws' };
|
||||
const traces = [];
|
||||
const deferred = () => { let resolve; const promise = new Promise(r => { resolve = r; }); return { promise, resolve }; };
|
||||
const events={};
|
||||
const stateDb=new Map();
|
||||
const notifications=new Map();
|
||||
@@ -11,6 +19,10 @@ const cacheObj={
|
||||
async put(key, response){stateDb.set(key, response)},
|
||||
};
|
||||
const context={
|
||||
async resolveDmPushPreview(payload, emit, trace) { previewCalls++; liveEvents.splice(0).forEach(emit); trace('test_preview'); return previewHook ? previewHook(payload) : previewHidden ? '' : previewText; },
|
||||
async getDmPreviewSession() { return activeSession; },
|
||||
async saveDmPreviewDiagnostics(value) { traces.push(value); },
|
||||
async isDmPreviewHidden() { return previewHidden; },
|
||||
self:{
|
||||
__shineStoppedCalls:new Map(),
|
||||
registration:{scope:'https://shineup.me/',
|
||||
@@ -25,7 +37,7 @@ const context={
|
||||
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);
|
||||
vm.runInContext(fs.readFileSync(path.resolve(__dirname, '../firebase-messaging-sw.js'),'utf8').replace(/^import .*;\n/gm, ''), 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()=>{
|
||||
@@ -41,7 +53,8 @@ async function read(to,from){const pending=[];events.message({data:{type:'SHINE_
|
||||
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');
|
||||
assert.strictEqual(registrations.filter(x=>x.options.data.fromLogin==='Vasya').at(-1).options.body,'5 непрочитанных сообщений','late push cannot downgrade unread count');
|
||||
assert.strictEqual(registrations.length,6,'late push cannot replace the latest card');
|
||||
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});
|
||||
@@ -50,5 +63,52 @@ async function read(to,from){const pending=[];events.message({data:{type:'SHINE_
|
||||
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 непрочитанное сообщение');
|
||||
previewText = 'Привет!';
|
||||
await push({kind:'new_message',toLogin:'AnNa',fromLogin:'Vasya',messageKey:'m9',unreadCount:2,notificationVersionMs:1009});
|
||||
assert.strictEqual(registrations.at(-1).options.body,'Привет!');
|
||||
assert.strictEqual(registrations.at(-2).options.body,'2 непрочитанных сообщения');
|
||||
assert.strictEqual(registrations.at(-1).options.renotify,false);
|
||||
assert.strictEqual(registrations.at(-1).options.silent,true);
|
||||
assert.strictEqual(registrations.at(-2).options.tag,registrations.at(-1).options.tag);
|
||||
assert(traces.at(-1).steps.some(step=>step.stage==='notification_updated'));
|
||||
const callsBeforeHidden = previewCalls;
|
||||
previewHidden = true;
|
||||
await push({kind:'new_message',toLogin:'AnNa',fromLogin:'Vasya',messageKey:'m10',unreadCount:3,notificationVersionMs:1010});
|
||||
assert.strictEqual(registrations.at(-1).options.body,'3 непрочитанных сообщения');
|
||||
assert.strictEqual(previewCalls,callsBeforeHidden,'disabled means no resolver call');
|
||||
previewHidden = false;
|
||||
liveEvents.push({kind:'new_message',toLogin:'AnNa',fromLogin:'NewPeer',messageKey:'other1',unreadCount:1,notificationVersionMs:4000});
|
||||
const callsBefore = previewCalls;
|
||||
await push({kind:'new_message',toLogin:'AnNa',fromLogin:'Vasya',messageKey:'m11',unreadCount:4,notificationVersionMs:1011});
|
||||
assert.strictEqual(previewCalls,callsBefore+1,'live event does not create another background login');
|
||||
assert(registrations.some(row=>row.options.data?.messageKey==='other1' && row.options.body==='1 непрочитанное сообщение'));
|
||||
// A slow preview must not block later pushes or a read action.
|
||||
const dm = (key, version, peer='Race') => ({kind:'new_message',toLogin:'AnNa',fromLogin:peer,messageKey:key,unreadCount:1,notificationVersionMs:version});
|
||||
let gate=deferred(), entered=deferred();
|
||||
previewHook = payload=>{ if(payload.messageKey==='slow'){ entered.resolve(); return gate.promise; } return 'new text'; };
|
||||
const slow = push(dm('slow',5001));
|
||||
await entered.promise;
|
||||
assert.strictEqual(registrations.at(-1).options.body,'1 непрочитанное сообщение','first alert before preview resolves');
|
||||
await push(dm('new',5002));
|
||||
assert.strictEqual(registrations.at(-1).options.body,'new text');
|
||||
gate.resolve('old text'); await slow;
|
||||
assert.strictEqual(registrations.at(-1).options.body,'new text','old result cannot overwrite a newer push');
|
||||
// Dismissed/read notification cannot be recreated.
|
||||
for(const mode of ['dismiss','read','optout','session-change']) {
|
||||
gate=deferred(); entered=deferred();
|
||||
previewHook=()=>{ entered.resolve(); return gate.promise; };
|
||||
const pending=push(dm(mode,6000,mode)); await entered.promise;
|
||||
const tag=registrations.at(-1).options.tag;
|
||||
if(mode==='dismiss') notifications.get(tag).close();
|
||||
if(mode==='read') await read('AnNa',mode);
|
||||
if(mode==='optout') previewHidden=true;
|
||||
if(mode==='session-change') activeSession={...activeSession,sessionId:'different'};
|
||||
const count=registrations.length;
|
||||
gate.resolve('must not display'); await pending;
|
||||
assert.strictEqual(registrations.length,count,mode+' prevents late update');
|
||||
previewHidden=false;
|
||||
activeSession={...activeSession,sessionId:'session'};
|
||||
}
|
||||
assert(!JSON.stringify(traces).includes('must not display'));
|
||||
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});
|
||||
|
||||
Reference in New Issue
Block a user