Files
fpv-rid-workbench/scripts/test-history-map-adapter.mjs
T

85 lines
4.3 KiB
JavaScript

// Deterministic map-adapter checks without an HTTP server or physical receiver.
import {test} from 'node:test';
import assert from 'node:assert/strict';
import {readFile} from 'node:fs/promises';
import vm from 'node:vm';
const source=(await readFile(new URL('../crates/web-ui/public/history-map-adapter.js',import.meta.url),'utf8'))
.replace(/^import .*;$/gm,'').replaceAll('export function ','function ');
const markerSource=await readFile(new URL('../crates/web-ui/workbench/drone-map.js',import.meta.url),'utf8');
test('replay keeps real segments, changes its basemap and advances the marker on the route',async()=>{
let map,marker,fitCount=0,removed=false;
const sources=new Map();
class FakeMap{
constructor(options){
map=this;this.handlers={};this.style=options.style;
for(const [id,value] of Object.entries(this.style.sources)) this.addSource(id,value);
}
on(name,fn){this.handlers[name]=fn;}
addControl(){}
addSource(id,value){sources.set(id,{...value,setData(data){this.data=data;},setTiles(tiles){this.tiles=tiles;}});}
addLayer(){}
getSource(id){return sources.get(id);}
getLayer(id){return this.style.layers.find(layer=>layer.id===id);}
setLayoutProperty(id,key,value){this.getLayer(id).layout[key]=value;}
fitBounds(){fitCount++;}
resize(){}
remove(){removed=true;}
}
class Marker{
constructor({element}){this.element=element;}
getElement(){return this.element;}
setLngLat(point){this.position=[...point];marker=this;return this;}
getLngLat(){return {lng:this.position[0],lat:this.position[1]};}
setRotation(angle){this.angle=angle;return this;}
getRotation(){return this.angle;}
addTo(){return this;}
remove(){}
}
class MercatorCoordinate{
constructor(x,y){Object.assign(this,{x,y});}
static fromLngLat([lng,lat]){return new MercatorCoordinate(lng/360,lat/360);}
toLngLat(){return {toArray:()=>[this.x*360,this.y*360]};}
}
class Bounds{constructor(){this.points=[];}extend(point){this.points.push(point);}isEmpty(){return this.points.length===0;}}
function element(){
const styles = new Map();
return {setAttribute(){},append(){},style:{setProperty:(name,value)=>styles.set(name,value),getPropertyValue:name=>styles.get(name)}};
}
const context=vm.createContext({window:{dispatchEvent(){}},document:{getElementById(){return {};},createElement:element},
ResizeObserver:class{observe(){}disconnect(){}},Event:class{},Number,Array,Math,URLSearchParams,
fetch:async()=>({ok:true,json:async()=>({entries:[{registered:true}]})}),
ml:{setWorkerUrl(){},Map:FakeMap,Marker,MercatorCoordinate,LngLatBounds:Bounds,NavigationControl:class{},AttributionControl:class{}},
attachTileBuffer:()=>({destroy(){},schedule(){}})});
vm.runInContext(markerSource,context);
vm.runInContext(source,context);
vm.runInContext('open({longitude:118.77,latitude:32.04,zoom:15,basemap:"road",labels:false})',context);
vm.runInContext(`setPoints([
{longitude:118,latitude:32,timeMs:1000,headingDeg:90,segmentStart:true},
{longitude:119,latitude:32,timeMs:2000,headingDeg:90,segmentStart:false},
{longitude:120,latitude:32,timeMs:40000,headingDeg:180,segmentStart:true},
{longitude:121,latitude:32,timeMs:41000,headingDeg:180,segmentStart:false}
], 'TEST-OWN')`,context);
map.handlers['style.load']();
await new Promise(resolve=>setImmediate(resolve));
assert.equal(marker.getElement().style.getPropertyValue('--rid-drone-fill'),'#26ad72');
const features=sources.get('history-track').data.features;
assert.deepEqual([...features].map(feature=>Array.from(feature.geometry.coordinates, point=>[...point])),
[[[118,32],[119,32]],[[120,32],[121,32]]]);
assert.equal(fitCount,1);
vm.runInContext('setCursor(0,.5)',context);
assert.deepEqual(marker.position,[118.5,32]);
vm.runInContext('setCursor(1,.5)',context);
assert.deepEqual(marker.position,[119,32]);
vm.runInContext('setMapOptions({basemap:"imagery",labels:true}); setCursor(2,0)',context);
assert.deepEqual([...sources.get('base').tiles],['/api/maps/tile/img_w/{z}/{x}/{y}']);
assert.equal(map.getLayer('labels').layout.visibility,'visible');
assert.deepEqual(marker.position,[120,32]);
assert.equal(marker.angle,180);
assert.equal(fitCount,1);
vm.runInContext('close()',context);
assert.ok(removed);
});