Files
bpq_gui/terminal-window.test.mjs
alberto c98c67e18b Add detached service windows and selectable terminal themes
Support standalone APRS, Monitor, Chat and Terminal views with courtesy screens, window focus and return controls, shared sessions and suspended embedded polling.

Add five persistent phosphor palettes, remove Console terminal idle disconnections, and display decoded Mic-E movement and altitude with expandable original packets.

Document the new behavior and add regression coverage. Validation: 182 tests passed; the known hanging PowerShell startup readiness test was excluded.
2026-10-19 22:15:15 +02:00

47 lines
4.5 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import vm from 'node:vm';
import {readFileSync} from 'node:fs';
function harness(detached=false,saved='amber'){
const nodes=new Map(),listeners={},calls=[],storage=new Map([['bpq-terminal-theme',saved]]);
const element=()=>({dataset:{},hidden:false,value:'',textContent:'',innerHTML:'',attributes:{},classList:{toggle(){}},addEventListener(){},append(){},focus(){},setAttribute(k,v){this.attributes[k]=v;},querySelector:s=>$(s),querySelectorAll:()=>buttons});
const $=s=>{if(!nodes.has(s))nodes.set(s,element());return nodes.get(s);};
const buttons=['amber','green','ice','violet','white'].map(theme=>Object.assign(element(),{dataset:{terminalTheme:theme}}));
let focused=0;
const window={addEventListener:(name,fn)=>listeners[name]=fn,open:(...args)=>{calls.push(args);return {focus(){focused++;}};},location:{assign:url=>calls.push(['navigate',url])}};
const context=vm.createContext({$,window,document:{body:{classList:{contains:()=>detached}},createElement:element,addEventListener:(name,fn)=>listeners[name]=fn},
localStorage:{getItem:key=>storage.get(key),setItem:(key,value)=>storage.set(key,value)},location:{hash:''},BPQI18n:{t:s=>s},
csrf:'test',page:'terminal',station:{features:{terminal:true}},navigate(){},api:async(...args)=>{calls.push(args);return {state:'online',latest:0,events:[],port:8100};},toast:text=>calls.push(['toast',text]),setInterval(){},setTimeout(){},clearTimeout(){}});
vm.runInContext(readFileSync(new URL('./public/terminal.js',import.meta.url),'utf8').replace(/\}\)\(\);\s*$/, 'window.testSection=section;})();'),context);
return {$,window,listeners,storage,buttons,calls,focused:()=>focused};
}
test('terminal themes persist, synchronize between windows and reject invalid stored values',()=>{
const h=harness(false,'green');assert.equal(h.window.testSection.dataset.theme,'green');
const violet=h.buttons.find(b=>b.dataset.terminalTheme==='violet');
h.$('.terminal-theme-bar').onclick({target:{closest:()=>violet}});
assert.equal(h.window.testSection.dataset.theme,'violet');assert.equal(h.storage.get('bpq-terminal-theme'),'violet');
assert.equal(violet.attributes['aria-pressed'],'true');
h.listeners.storage({key:'bpq-terminal-theme',newValue:'ice'});assert.equal(h.window.testSection.dataset.theme,'ice');
assert.equal(harness(false,'<bad>').window.testSection.dataset.theme,'amber');
});
test('detach opens a reusable standalone window without connecting, disconnecting or exposing credentials',()=>{
const h=harness();h.$('#terminal-detach').onclick();
assert.deepEqual(h.calls,[['/terminal-window','bpq-console-terminal','popup=yes,width=1120,height=780,resizable=yes,scrollbars=yes']]);assert.equal(h.focused(),1);
h.window.open=()=>null;h.$('#terminal-detach').onclick();assert.match(h.calls.at(-1)[1],/popup/);
});
test('returning from a detached terminal focuses the original panel without closing the Telnet session',()=>{
const h=harness(true);let focused=0,closed=0;h.window.opener={closed:false,location:{},focus(){focused++;}};h.window.close=()=>closed++;
assert.equal(h.$('#terminal-detach').hidden,true);assert.equal(h.$('#terminal-dock').hidden,false);
h.$('#terminal-dock').onclick();assert.equal(h.window.opener.location.hash,'#terminal');assert.equal(focused,1);assert.equal(closed,1);assert.equal(h.calls.length,0);
h.window.opener=null;h.$('#terminal-dock').onclick();assert.deepEqual(h.calls,[['navigate','/#terminal']]);
});
test('detached terminal restores the existing session before polling and never reconnects automatically',async()=>{
const nodes=new Map(),listeners={},calls=[],$=s=>{if(!nodes.has(s))nodes.set(s,{hidden:false,textContent:'',onclick:null});return nodes.get(s);};
const context=vm.createContext({document:{querySelector:$,addEventListener:(name,fn)=>listeners[name]=fn,dispatchEvent:event=>calls.push(['event',event.type])},
window:{},Event,BPQI18n:{t:s=>s},setTimeout(){},clearTimeout(){},fetch:async(url,options)=>{calls.push([url,options]);return {ok:true,json:async()=>({csrf:'private-csrf',config:{call:'N0TEST',features:{terminal:true}}})};}});
vm.runInContext(readFileSync(new URL('./public/terminal-window.js',import.meta.url),'utf8'),context);
await listeners.DOMContentLoaded();assert.equal(calls[0][0],'/api/session');assert.deepEqual(calls[1],['event','station-ready']);
assert.equal(calls.filter(([url])=>url.includes('connect')).length,0);assert.equal(context.document.title,'BPQ Console · N0TEST · Terminal');
});