Files
bpq_gui/map-refresh-ui.test.mjs
T
alberto 33f1294405 Polish dashboard layout and stabilize live map refreshes
Preserve map markers, zoom and open details during polling; consolidate technical notes and align interface spacing. Fix empty English translations inserting undefined text and add regression coverage.
2026-10-18 21:14:35 +02:00

86 lines
6.9 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import vm from 'node:vm';
import {readFileSync} from 'node:fs';
function element(){
let html='';
return {writes:0,value:'',defaultValue:'',dataset:{},attributes:{},clientWidth:700,clientHeight:440,hidden:false,textContent:'',
get innerHTML(){return html;},set innerHTML(value){html=value;this.writes++;},
querySelector(){return element();},querySelectorAll(){return [];},setAttribute(k,v){this.attributes[k]=v;},
addEventListener(){},append(){},prepend(){},after(){},contains(){return false;},classList:{toggle(){}}};
}
function harness(file,exportCode,api=async()=>{}){
const nodes=new Map(),$=selector=>{if(!nodes.has(selector))nodes.set(selector,element());return nodes.get(selector);};
const context=vm.createContext({$, $$:()=>[],window:{},document:{hidden:false,activeElement:null,createElement:element,addEventListener(){},getElementById(){return null;}},
BPQI18n:{t:s=>s,locale:()=> 'en-GB'},csrf:'test',page:'none',navigate(){},api,fetch:async()=>({ok:true,json:async()=>({features:[]})}),
size:n=>String(n),mailTimezone:()=> 'UTC',esc:s=>String(s??''),toast(){},setInterval(){},setTimeout(){},requestAnimationFrame:f=>f()});
const source=readFileSync(new URL('./public/'+file,import.meta.url),'utf8');
vm.runInContext(source.replace(/\}\)\(\);\s*$/,exportCode+'\n})();'),context);
return {context,$,fixture:context.fixture};
}
function leaflet(){
const state={created:[],fits:0,removals:[],zoom:9},map={setView(){return this;},fitBounds(){state.fits++;return this;},getZoom:()=>state.zoom};
const layers={addTo(){return this;},removeLayer(m){state.removals.push(m);}};
function marker(point,options){
let popup='',tooltip='';const m={point,options,moves:0,popupWrites:0,open:false,
bindTooltip(s){tooltip=s;return this;},bindPopup(s,o){popup=s;this.popupOptions=o;return this;},on(){return this;},addTo(){return this;},
getLatLng(){return {lat:this.point[0],lng:this.point[1]};},setLatLng(p){this.point=p;this.moves++;return this;},setStyle(o){Object.assign(this.options,o);},
getPopup(){return {getContent:()=>popup};},setPopupContent(s){popup=s;this.popupWrites++;},
getTooltip(){return {getContent:()=>tooltip};},setTooltipContent(s){tooltip=s;},openPopup(){this.open=true;}};
state.created.push(m);return m;
}
return {state,L:{map:()=>map,tileLayer:()=>({addTo(){}}),layerGroup:()=>layers,circleMarker:marker,latLngBounds:p=>p}};
}
const station=(call,lat=45)=>({call,transport:'Radio',position:{lat,lon:9,locator:'JN45MM'},lastAt:'2026-01-01T12:00:00Z',status:'On air',lastBody:'',path:'TEST',services:['BBS']});
const aprsData=()=>({stations:[station('N0AAA'),station('N0BBB',46)],own:{call:'N0LOCAL',position:{lat:44,lon:9}}});
test('APRS polling keeps marker identity, popup and viewport; changed positions update in place',()=>{
const h=harness('aprs-activity.js','globalThis.fixture={setData(value){data=value;},renderMap};'),l=leaflet();
h.context.L=l.L;h.context.window.L=l.L;h.context.page='aprs';h.$('#aprs-source').value='Radio';
let data=aprsData();h.fixture.setData(data);h.fixture.renderMap();
const [first,second,own]=l.state.created;first.openPopup();
for(let i=0;i<5;i++){h.fixture.setData(structuredClone(data));h.fixture.renderMap();}
assert.equal(l.state.created.length,3);assert.equal(l.state.fits,1);assert.equal(l.state.removals.length,0);
assert.equal(first.open,true);assert.equal(first.moves,0);assert.equal(first.popupWrites,0);assert.equal(own.moves,0);
data=structuredClone(data);data.stations[0].position.lat=45.1;data.stations[0].status='Updated';h.fixture.setData(data);h.fixture.renderMap();
assert.equal(first.moves,1);assert.equal(first.popupWrites,1);assert.equal(first.open,true);assert.equal(second.moves,0);assert.equal(l.state.fits,1);
assert.equal(first.popupOptions.autoPan,false);
});
test('APRS removes only stations outside the filter and retains the local marker',()=>{
const h=harness('aprs-activity.js','globalThis.fixture={setData(value){data=value;},renderMap};'),l=leaflet();
h.context.L=l.L;h.context.window.L=l.L;h.context.page='aprs';const data=aprsData();h.fixture.setData(data);h.fixture.renderMap();
const [first,second]=l.state.created;h.$('#aprs-search').value='N0AAA';h.fixture.renderMap();
assert.deepEqual(l.state.removals,[second]);assert.equal(l.state.created.length,3);assert.equal(first.moves,0);assert.equal(l.state.fits,1);
});
const journalData=()=>({since:'2026-01-01',warnings:['fixture.log: partial tail'],limited:false,own:{call:'N0LOCAL',position:{lat:44,lon:9}},stations:[station('N0AAA')],sessions:[{id:'one',call:'N0AAA',startedAt:new Date().toISOString(),connectedAt:new Date().toISOString(),service:'BBS',direction:'incoming',transport:'Radio',status:'complete',duration:12}]});
function journalHarness(){
let next=journalData();const h=harness('journal.js','globalThis.fixture={load,zoom,renderMap,mapInfo,getView:()=>[...view]};',async()=>structuredClone(next));
h.context.page='map';h.$('#map-period').value='7';h.$('#journal-period').value='7';h.$('#map-tracks').checked=true;
return {...h,setNext:value=>next=value};
}
test('journal refresh keeps the zoom and unchanged SVG instead of fitting or rebuilding the map',async()=>{
const h=journalHarness();await h.fixture.load();h.fixture.zoom(.65);const view=h.fixture.getView(),svg=h.$('#station-map'),writes=svg.writes;
await h.fixture.load(true,true);await h.fixture.load(true,true);
assert.deepEqual(h.fixture.getView(),view);assert.equal(svg.writes,writes);
assert.equal(h.$('#journal-warning').hidden,false);assert.match(h.$('#journal-warning-detail').textContent,/partial tail/);
});
test('map refresh preserves an unsaved locator even after the field loses focus',async()=>{
const h=journalHarness();await h.fixture.load();const s=station('N0AAA');h.fixture.mapInfo(s);
const input=h.$('#station-locator');input.defaultValue='JN45MM';input.value='JN45NN';const writes=h.$('#map-info').writes;
h.fixture.mapInfo({...s,lastAt:new Date().toISOString()});assert.equal(input.value,'JN45NN');assert.equal(h.$('#map-info').writes,writes);
});
test('background API polls stay quiet, while interactive requests finish the busy indicator even on error',async()=>{
let starts=0,finishes=0,request,fail=false;
const context=vm.createContext({FormData,csrf:'test',window:{BPQBusy:{start(){starts++;return ()=>finishes++;}}},
fetch:async(url,options)=>{request=options;if(fail)throw Error('offline');return {ok:true,json:async()=>({ok:true})};}});
const source=readFileSync(new URL('./public/app.js',import.meta.url),'utf8').split('\n').find(line=>line.startsWith('async function api('));
vm.runInContext(source,context);
await context.api('/journal',{busy:false});assert.equal(starts,0);assert.equal(finishes,0);assert.equal('busy' in request,false);
await context.api('/journal');assert.equal(starts,1);assert.equal(finishes,1);
fail=true;await assert.rejects(context.api('/journal'),/offline/);assert.equal(starts,2);assert.equal(finishes,2);
});