Fix detached controls on LAN HTTP and late-loaded views
This commit is contained in:
1 parent
3643638090
commit
bd8e521711
3 files changed
+74
-59
No files matched your search
+63
-51
@@ -1,52 +1,64 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import vm from 'node:vm';
|
||||
import {readFileSync} from 'node:fs';
|
||||
const source=readFileSync(new URL('./public/detach.js',import.meta.url),'utf8');
|
||||
function harness(search='',shared=new Map()){
|
||||
const docEvents={},events={},sections={},calls=[];let serial=0,tick,popup;
|
||||
function element(){const children=new Map();return {hidden:false,dataset:{},textContent:'',className:'',innerHTML:'',classes:new Set(),
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import vm from 'node:vm';
|
||||
import {readFileSync} from 'node:fs';
|
||||
const source=readFileSync(new URL('./public/detach.js',import.meta.url),'utf8');
|
||||
function harness(search='',shared=new Map(),options={}){
|
||||
const docEvents={},events={},sections={},calls=[];let serial=0,tick,popup;
|
||||
function element(){const children=new Map();return {hidden:false,dataset:{},textContent:'',className:'',innerHTML:'',classes:new Set(),
|
||||
setAttribute(name,value){this[name]=value;},classList:{toggle(name,on){this.owner.classes[on?'add':'delete'](name);}},append(child){this.child=child;},
|
||||
querySelector(selector){if(selector==='.detached-placeholder')return this.child||null;if(!children.has(selector))children.set(selector,element());return children.get(selector);},
|
||||
querySelectorAll(){return [this.querySelector('first'),this.querySelector('second')];}};}
|
||||
for(const id of ['terminal','aprs','monitor','chat']){sections[id]=element();sections[id].classList.owner=sections[id];}
|
||||
const main={hidden:false};const document={body:{append(node){calls.push(['notice',node.className]);}},querySelector:()=>main,readyState:'complete',documentElement:{dataset:{}},getElementById:id=>sections[id],createElement:element,addEventListener:(name,fn)=>docEvents[name]=fn};
|
||||
const window={name:'',addEventListener:(name,fn)=>events[name]=fn,focus:()=>calls.push('focus'),close:()=>calls.push('close'),
|
||||
open:(...args)=>{calls.push(args);popup={closed:false,focus(){},close(){this.closed=true;}};return popup;}};
|
||||
const location={pathname:'/',search,hash:'',assign:url=>calls.push(['assign',url])};
|
||||
const context=vm.createContext({window,document,location,URLSearchParams,crypto:{randomUUID:()=>String(++serial)},
|
||||
localStorage:{getItem:key=>shared.get(key)||null,setItem:(key,value)=>shared.set(key,value),removeItem:key=>shared.delete(key)},
|
||||
Date,Set,Map,BPQI18n:{t:s=>s},toast:text=>calls.push(['toast',text]),navigate:id=>calls.push(['navigate',id]),setInterval:fn=>{tick=fn;}});
|
||||
vm.runInContext(source,context);docEvents.DOMContentLoaded();
|
||||
return {api:window.BPQDetached,window,document,docEvents,events,sections,calls,shared,main,tick:()=>tick(),popup:()=>popup};
|
||||
}
|
||||
test('a page shows courtesy only after its detached view is ready, and another page stays usable',()=>{
|
||||
const h=harness();h.api.open('aprs');assert.equal(h.api.isAway('aprs'),false);
|
||||
assert.match(h.calls[0][0],/^\/\?detached=aprs&window=\d+#aprs$/);
|
||||
h.shared.set('bpq-detached:aprs',JSON.stringify({owner:'2',at:Date.now()}));h.tick();
|
||||
assert.equal(h.sections.aprs.child.hidden,false);assert.equal(h.api.shouldPoll('aprs'),false);assert.equal(h.api.shouldPoll('chat'),true);
|
||||
assert.equal(h.sections.aprs.classes.has('section-detached-away'),true);
|
||||
h.api.dock('aprs');assert.equal(h.sections.aprs.child.hidden,true);assert.equal(h.api.shouldPoll('aprs'),true);assert.equal(h.popup().closed,true);
|
||||
});
|
||||
test('closing a child restores its parent and stale records cannot strand a page on the courtesy screen',()=>{
|
||||
const h=harness();h.api.open('monitor');h.shared.set('bpq-detached:monitor',JSON.stringify({owner:'2',at:Date.now()}));h.tick();
|
||||
h.popup().closed=true;h.tick();assert.equal(h.api.isAway('monitor'),false);assert.equal(h.sections.monitor.child.hidden,true);
|
||||
h.shared.set('bpq-detached:chat',JSON.stringify({owner:'old',at:Date.now()-130000}));h.tick();assert.equal(h.api.isAway('chat'),false);
|
||||
});
|
||||
test('blocked popups keep the embedded view and never claim a detached session',()=>{
|
||||
const h=harness();h.window.open=()=>null;h.api.open('chat');assert.equal(h.api.isAway('chat'),false);assert.equal(h.shared.has('bpq-detached:chat'),false);assert.match(h.calls[0][1],/popup/);
|
||||
});
|
||||
test('a standalone page claims just its service, releases on close and never reconnects or disconnects BPQ',()=>{
|
||||
const shared=new Map(),h=harness('?detached=chat&window=child',shared);h.docEvents['station-ready']();
|
||||
assert.equal(h.api.view,'chat');assert.equal(h.api.shouldPoll('chat'),true);assert.equal(h.api.shouldPoll('aprs'),false);
|
||||
assert.equal(JSON.parse(shared.get('bpq-detached:chat')).owner,'child');assert.equal(h.document.documentElement.dataset.detachedView,'chat');
|
||||
h.events.pagehide();assert.equal(shared.has('bpq-detached:chat'),false);h.tick();assert.equal(shared.has('bpq-detached:chat'),false);
|
||||
assert.ok(h.calls.every(call=>call[0]==='navigate'));
|
||||
});
|
||||
|
||||
|
||||
test('a dock request stops the detached view even when its browser refuses scripted window closing',()=>{
|
||||
const h=harness('?detached=monitor&window=child');h.docEvents['station-ready']();
|
||||
h.events.storage({key:'bpq-detached-command',newValue:JSON.stringify({id:'monitor',action:'dock',at:Date.now()})});
|
||||
assert.equal(h.shared.has('bpq-detached:monitor'),false);assert.equal(h.api.shouldPoll('monitor'),false);assert.equal(h.main.hidden,true);assert.ok(h.calls.includes('close'));
|
||||
});
|
||||
querySelector(selector){if(selector==='.detached-placeholder')return this.child||null;if(!children.has(selector))children.set(selector,element());return children.get(selector);},
|
||||
querySelectorAll(){return [this.querySelector('first'),this.querySelector('second')];}};}
|
||||
for(const id of ['terminal','aprs','monitor','chat']){sections[id]=element();sections[id].classList.owner=sections[id];}
|
||||
const main={hidden:false};const document={body:{append(node){calls.push(['notice',node.className]);}},querySelector:()=>main,readyState:'complete',documentElement:{dataset:{}},getElementById:id=>options.late?.includes(id)?null:sections[id],createElement:element,addEventListener:(name,fn)=>docEvents[name]=fn};
|
||||
const window={name:'',addEventListener:(name,fn)=>events[name]=fn,focus:()=>calls.push('focus'),close:()=>calls.push('close'),
|
||||
open:(...args)=>{calls.push(args);popup={closed:false,focus(){},close(){this.closed=true;}};return popup;}};
|
||||
const location={pathname:'/',search,hash:'',assign:url=>calls.push(['assign',url])};
|
||||
const context=vm.createContext({window,document,location,URLSearchParams,crypto:options.insecure?{}:{randomUUID:()=>String(++serial)},
|
||||
localStorage:{getItem:key=>shared.get(key)||null,setItem:(key,value)=>shared.set(key,value),removeItem:key=>shared.delete(key)},
|
||||
Date,Set,Map,BPQI18n:{t:s=>s},toast:text=>calls.push(['toast',text]),navigate:id=>calls.push(['navigate',id]),setInterval:fn=>{tick=fn;}});
|
||||
vm.runInContext(source,context);docEvents.DOMContentLoaded();
|
||||
return {api:window.BPQDetached,window,document,docEvents,events,sections,calls,shared,main,tick:()=>tick(),popup:()=>popup};
|
||||
}
|
||||
test('a page shows courtesy only after its detached view is ready, and another page stays usable',()=>{
|
||||
const h=harness();h.api.open('aprs');assert.equal(h.api.isAway('aprs'),false);
|
||||
assert.match(h.calls[0][0],/^\/\?detached=aprs&window=\d+#aprs$/);
|
||||
h.shared.set('bpq-detached:aprs',JSON.stringify({owner:'2',at:Date.now()}));h.tick();
|
||||
assert.equal(h.sections.aprs.child.hidden,false);assert.equal(h.api.shouldPoll('aprs'),false);assert.equal(h.api.shouldPoll('chat'),true);
|
||||
assert.equal(h.sections.aprs.classes.has('section-detached-away'),true);
|
||||
h.api.dock('aprs');assert.equal(h.sections.aprs.child.hidden,true);assert.equal(h.api.shouldPoll('aprs'),true);assert.equal(h.popup().closed,true);
|
||||
});
|
||||
test('closing a child restores its parent and stale records cannot strand a page on the courtesy screen',()=>{
|
||||
const h=harness();h.api.open('monitor');h.shared.set('bpq-detached:monitor',JSON.stringify({owner:'2',at:Date.now()}));h.tick();
|
||||
h.popup().closed=true;h.tick();assert.equal(h.api.isAway('monitor'),false);assert.equal(h.sections.monitor.child.hidden,true);
|
||||
h.shared.set('bpq-detached:chat',JSON.stringify({owner:'old',at:Date.now()-130000}));h.tick();assert.equal(h.api.isAway('chat'),false);
|
||||
});
|
||||
test('blocked popups keep the embedded view and never claim a detached session',()=>{
|
||||
const h=harness();h.window.open=()=>null;h.api.open('chat');assert.equal(h.api.isAway('chat'),false);assert.equal(h.shared.has('bpq-detached:chat'),false);assert.match(h.calls[0][1],/popup/);
|
||||
});
|
||||
test('a standalone page claims just its service, releases on close and never reconnects or disconnects BPQ',()=>{
|
||||
const shared=new Map(),h=harness('?detached=chat&window=child',shared);h.docEvents['station-ready']();
|
||||
assert.equal(h.api.view,'chat');assert.equal(h.api.shouldPoll('chat'),true);assert.equal(h.api.shouldPoll('aprs'),false);
|
||||
assert.equal(JSON.parse(shared.get('bpq-detached:chat')).owner,'child');assert.equal(h.document.documentElement.dataset.detachedView,'chat');
|
||||
h.events.pagehide();assert.equal(shared.has('bpq-detached:chat'),false);h.tick();assert.equal(shared.has('bpq-detached:chat'),false);
|
||||
assert.ok(h.calls.every(call=>call[0]==='navigate'));
|
||||
});
|
||||
|
||||
|
||||
test('a dock request stops the detached view even when its browser refuses scripted window closing',()=>{
|
||||
const h=harness('?detached=monitor&window=child');h.docEvents['station-ready']();
|
||||
h.events.storage({key:'bpq-detached-command',newValue:JSON.stringify({id:'monitor',action:'dock',at:Date.now()})});
|
||||
assert.equal(h.shared.has('bpq-detached:monitor'),false);assert.equal(h.api.shouldPoll('monitor'),false);assert.equal(h.main.hidden,true);assert.ok(h.calls.includes('close'));
|
||||
});
|
||||
|
||||
test('LAN HTTP without randomUUID supports all detached services',()=>{
|
||||
const h=harness('',new Map(),{insecure:true});
|
||||
for(const id of ['terminal','aprs','monitor','chat'])h.api.open(id);
|
||||
assert.equal(h.calls.filter(c=>Array.isArray(c)&&typeof c[0]==='string'&&c[0].includes('window=')).length,4);
|
||||
});
|
||||
test('late sections receive controls and courtesy placeholders on the next setup pass',()=>{
|
||||
const options={late:['chat','monitor','aprs']},h=harness('',new Map(),options);
|
||||
assert.equal(h.sections.chat.child,undefined);options.late=[];h.tick();
|
||||
for(const id of ['chat','monitor','aprs'])assert.ok(h.sections[id].child);
|
||||
const placeholder=h.sections.chat.child;h.tick();assert.equal(h.sections.chat.child,placeholder);
|
||||
});
|
||||
Reference in new issue
Block a user