Files
bpq_gui/map-refresh-ui.test.mjs
T
alberto 8ee4e9636a Decode APRS station symbols and improve map centering
Render local APRS symbol sprites with table and overlay details while preserving markers and open popups during refresh.

Open the map around the local station with a 30 km radius and fit the densest station group without distant outliers. Add localization, attribution and regression coverage.
2026-10-19 14:08:49 +02:00

115 lines
9.6 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()});
vm.runInContext(readFileSync(new URL('./public/aprs-symbols.js',import.meta.url),'utf8'),context);
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(bounds,options){state.fits++;state.bounds=bounds;state.fitOptions=options;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;},
setIcon(icon){this.options.icon=icon;this.iconWrites=(this.iconWrites||0)+1;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,marker,divIcon:options=>options,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);
data.stations[0].position.symset='W';data.stations[0].position.symbol='#';h.fixture.setData(data);h.fixture.renderMap();
assert.equal(first.iconWrites,1);assert.match(first.options.icon.html,/alternate.*overlay/);assert.equal(first.open,true);
h.fixture.setData(structuredClone(data));h.fixture.renderMap();assert.equal(first.iconWrites,1);assert.equal(l.state.created.length,3);assert.equal(l.state.fits,1);
});
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);
});
test('APRS density fit excludes isolated distant stations and favours a tied group near home',()=>{
const h=harness('aprs-activity.js','globalThis.fixture={densePositions,distanceKm,localBounds};');
const home={lat:45.57,lon:9.13},near=[home,{lat:45.7,lon:9.4},{lat:45.4,lon:9.3}],remote=[{lat:51.5,lon:-.1},{lat:51.6,lon:-.2},{lat:51.4,lon:0}];
const result=h.fixture.densePositions([...remote,...near,{lat:30,lon:120},null,{lat:NaN,lon:4}],home);
assert.equal(result.length,4);assert.ok(result.every(p=>h.fixture.distanceKm(home,p)<100));
assert.equal(h.fixture.densePositions([],null).length,0);
assert.equal(h.fixture.densePositions([],home).length,1);
});
test('APRS opens at a 30 km radius and refresh does not reset a manually selected density fit',()=>{
const h=harness('aprs-activity.js','globalThis.fixture={setData(value){data=value;},renderMap,fit,fitLocal,distanceKm};'),l=leaflet();
h.context.L=l.L;h.context.window.L=l.L;h.context.page='aprs';
const data=aprsData();data.own.position={lat:45.57,lon:9.13};data.stations.push(station('DISTANT',-30));
h.fixture.setData(data);h.fixture.renderMap();
const [southwest,northeast]=l.state.bounds,home=data.own.position;
assert.ok(Math.abs(h.fixture.distanceKm(home,{lat:southwest[0],lon:home.lon})-30)<.001);
assert.ok(Math.abs(h.fixture.distanceKm(home,{lat:home.lat,lon:northeast[1]})-30)<.01);
assert.equal(h.$('#aprs-map-local').attributes['aria-pressed'],'true');assert.equal((southwest[0]+northeast[0])/2,home.lat);assert.equal(h.$('#aprs-map-local').disabled,false);
h.fixture.fit();assert.equal(h.$('#aprs-map-fit').attributes['aria-pressed'],'true');const bounds=JSON.stringify(l.state.bounds);assert.ok(l.state.bounds.every(p=>p[0]>0));
h.fixture.setData(structuredClone(data));h.fixture.renderMap();assert.equal(l.state.fits,2);assert.equal(JSON.stringify(l.state.bounds),bounds);
h.fixture.fitLocal();assert.equal(l.state.fits,3);
data.own.position=null;h.fixture.setData(data);h.fixture.renderMap();assert.equal(h.$('#aprs-map-local').disabled,true);h.fixture.fitLocal();assert.equal(l.state.fits,3);
});