Files
fpv-rid-workbench/scripts/test-rid-map-state.mjs
T
JiuContinent 1d6d7a277e feat(realtime): 增加目标接收计数与频率计算支持
- 在 Target 结构体中新增 received_count 字段,用于跟踪每个目标在当前内存轨迹中的接收更新次数。
- 实现 ReceptionRate 结构体,计算目标更新频率,并在 State 中维护每个目标的接收率。
- 更新相关测试,确保接收计数和频率计算的准确性。
- 修改 Web UI 以显示目标的接收频率,提升用户体验。
- 升级协议版本至 4,确保后端、Worker 和前端的一致性。
2026-11-02 17:32:44 +08:00

105 lines
9.3 KiB
JavaScript

// Unit-level map adapter contract with a deterministic WebGL/MapLibre test double.
import {readFile} from 'node:fs/promises';
import vm from 'node:vm';
import assert from 'node:assert/strict';
const source=(await readFile(new URL('../crates/web-ui/public/map-adapter.js',import.meta.url),'utf8')).replace(/^import .*;$/gm,'');
let markers=0,drawCount=0,trail=[],markerPositions=[];
const gl={ARRAY_BUFFER:1,bindBuffer(){},bufferSubData(_t,_o,data){assert.ok([...data].every(Number.isFinite));},getExtension(){return null;}};
let flights=[];
const map={getSource(){return{setData(data){trail=data.features;}};},triggerRepaint(){},getCanvas(){return{clientWidth:1000,clientHeight:700};},project(p){assert.ok(p.every(Number.isFinite));return{x:1,y:2};},getCenter(){return{toArray(){return[120,30];}};},getZoom(){return 15;},getPitch(){return 0;},getBearing(){return 0;},easeTo(){},flyTo(options){flights.push(options.center);},isStyleLoaded(){return true;}};
class Marker{constructor(opts){markers++;this.element=opts?.element||{dataset:{},querySelector(){return{dataset:{}};}};}setLngLat(p){assert.ok(p.every(Number.isFinite));markerPositions.push([...p]);return this;}addTo(){return this;}getElement(){return this.element;}remove(){markers--;}}
function domNode(){const node={dataset:{},style:{},attributes:{},listeners:{},className:'',textContent:'',innerHTML:'',children:[],append(...items){this.children.push(...items);},setAttribute(name,value){this.attributes[name]=value;},addEventListener(name,handler){this.listeners[name]=handler;},querySelector(sel){const name=sel.slice(1);const walk=item=>item.className===name?item:(item.children||[]).map(walk).find(Boolean);return walk(node)||null;}};return node;}
let now=1000;
const events=[];
const context=vm.createContext({window:{dispatchEvent(event){events.push(event);}},CustomEvent:class{constructor(type,options){this.type=type;this.detail=options.detail;}},document:{createElement:domNode,getElementById(){return null;}},performance:{now:()=>now},requestAnimationFrame(){},maplibre:{setWorkerUrl(){},Marker,MercatorCoordinate:{fromLngLat(p){assert.ok(p.every(Number.isFinite));return{x:p[0],y:p[1]};}}},console,Float32Array,Map,Set,Number,Array,Math,mockMap:map,mockGl:gl});
vm.runInContext(source,context);
vm.runInContext('map=mockMap;layer={gl:mockGl,instances:{},origin:{x:0,y:0}};centered=true;',context);
const update=(positions,generation)=>{context.input=new Float64Array(positions);context.generation=generation;vm.runInContext('window.ridMap.update(input,["TEST"],[generation]);flush();',context);drawCount=vm.runInContext('layer.count',context);};
update([NaN,NaN,NaN,90],0);assert.equal(drawCount,0);assert.equal(vm.runInContext('window.ridMap.select("TEST").length',context),0);assert.equal(flights.length,0);
update([120,30,NaN,NaN],1);assert.equal(drawCount,1);vm.runInContext('window.ridMap.select("TEST");flush();',context);assert.equal(markers,1);assert.deepEqual([...flights.at(-1)],[120,30]);
now+=200;update([120.01,30.01,NaN,90],1);assert.equal(trail.length,1);
now+=200;update([NaN,NaN,NaN,90],2);assert.equal(drawCount,0);assert.equal(markers,0);assert.equal(trail.length,0);
now+=200;update([121,31,NaN,90],3);assert.equal(markers,1);assert.equal(trail.length,0);
now+=200;update([121.01,31.01,NaN,90],3);assert.equal(trail.length,1);
now+=200;update([122,32,NaN,90],5);assert.equal(trail.length,0,'coalesced loss/recovery must start a fresh trail');
vm.runInContext('window.ridMap.setSources([]);',context);
assert.equal(vm.runInContext('layer.count',context),0,'source filter hides map instances');assert.equal(markers,0);
vm.runInContext('window.ridMap.setSources(["TEST"]);',context);
assert.equal(vm.runInContext('layer.count',context),1,'source filter restores matching targets');assert.equal(markers,1);
vm.runInContext('window.ridMap.setSources(null);',context);
const beforeRaw=flights.length;
vm.runInContext('window.ridMap.focusRaw([118.5768,31.9547]);',context);
assert.deepEqual([...flights.at(-1)],[118.5768,31.9547]);
vm.runInContext('window.ridMap.focusRaw([200,0]);',context);
assert.equal(flights.length,beforeRaw+1);
vm.runInContext('window.ridMap.setFences([{id:"F1",name:"园区",enabled:true,geometry:{type:"circle",center:[120.15,30.28],radius_m:400}}]);',context);
assert.equal(vm.runInContext('window.ridMap.metrics().fences',context),1);
assert.equal(trail[0].geometry.type,'Polygon');
assert.ok(trail[0].geometry.coordinates[0].length>8);
assert.equal(markers,2);
vm.runInContext('window.ridMap.setFences([{id:"BAD",geometry:{type:"circle",center:[999,0],radius_m:10}}]);',context);
assert.equal(trail.length,0);
vm.runInContext('window.ridMap.select("");',context);
update([NaN,NaN,NaN,90],6);
vm.runInContext('window.ridMap.select("TEST");',context);
const beforeZero=flights.length;
vm.runInContext('window.ridMap.focusRaw([0,0]);',context);
assert.equal(flights.length,beforeZero,'missing GPS coordinates do not move the map');
assert.equal(markers,0,'missing GPS coordinates do not create a marker');
vm.runInContext('window.ridMap.focusRaw([118,31]);',context);
assert.equal(markers,1);
const afterFocus=flights.length;
vm.runInContext('window.ridMap.updateRaw([119,32]);',context);
assert.deepEqual(markerPositions.at(-1),[119,32],'raw marker follows updates');
assert.equal(flights.length,afterFocus,'raw updates do not repeatedly recenter the map');
vm.runInContext('window.ridMap.setSources([]);',context);
assert.equal(markers,0,'source filtering hides unverified markers');
vm.runInContext('window.ridMap.setSources(["TEST"]);',context);
assert.equal(markers,1,'restoring the source restores the active marker');
vm.runInContext('window.ridMap.updateRaw(null);',context);
assert.equal(markers,0,'losing raw coordinates removes the marker');
vm.runInContext('window.ridMap.updateRaw([119,32]);',context);
assert.equal(markers,1);
vm.runInContext('window.ridMap.update(new Float64Array([]),[],[]);flush();',context);
assert.equal(markers,0,'target expiration removes the unverified marker');
vm.runInContext('window.ridMap.setSources(null);',context);
context.twoPositions=new Float64Array([120,30,NaN,0,121,31,NaN,0]);
context.twoInfos=new Float64Array([1.5,125,2,0,NaN,0]);
vm.runInContext('window.ridMap.update(twoPositions,["TEST","TWO"],[1,1],twoInfos);flush();',context);
assert.equal(markers,2,'each located target receives its own card');
let entries=vm.runInContext('markers',context);
assert.equal(entries.get('TEST').frequency.textContent,'数据频率 1.5 Hz');
assert.equal(entries.get('TWO').frequency.textContent,'数据频率 0 Hz');
assert.equal(entries.get('TEST').height.textContent,'几何高度 125.0 m');
assert.equal(entries.get('TWO').height.textContent,'高度 —');
assert.equal(entries.get('TEST').longitude.textContent,'经度 120.000000');
now+=250;context.twoInfos[0]=NaN;
vm.runInContext('window.ridMap.update(twoPositions,undefined,[1,1],twoInfos);flush();',context);
assert.equal(entries.get('TEST').frequency.textContent,'数据频率 —','frequency is unknown until enough samples arrive');
now+=250;context.twoInfos[0]=0.05;
vm.runInContext('window.ridMap.update(twoPositions,undefined,[1,1],twoInfos);flush();',context);
assert.equal(entries.get('TEST').frequency.textContent,'数据频率 0.05 Hz','low but nonzero rates are not rounded to zero');
vm.runInContext('window.ridMap.select("TWO");',context);
assert.equal(entries.get('TWO').root.style.zIndex,'10','selected target renders above other cards');
assert.equal(entries.get('TEST').root.style.zIndex,'1');
assert.equal(entries.get('TWO').icon.attributes['aria-pressed'],'true');
entries.get('TEST').icon.listeners.click({stopPropagation(){}});
assert.equal(events.at(-1).detail,'TEST','clicking a logo requests selection of that target');
vm.runInContext('window.ridMap.connection(false);',context);
assert.equal(entries.get('TWO').frequency.textContent,'数据频率 连接断开');
vm.runInContext('window.ridMap.connection(true);',context);
context.longIds=Array.from({length:130},(_,i)=>`T${i}`);
context.longPositions=new Float64Array(context.longIds.flatMap((_,i)=>[120+i/10000,30,NaN,0]));
vm.runInContext('window.ridMap.update(longPositions,longIds,[],null);flush();',context);
assert.equal(markers,120,'card count is bounded when many targets are visible');
vm.runInContext('window.ridMap.select("T129");',context);
entries=vm.runInContext('markers',context);
assert.equal(entries.get('T129').root.style.zIndex,'10','a selected target is retained even when the card limit is reached');
context.zeroPosition=new Float64Array([0,0,NaN,0]);
vm.runInContext('window.ridMap.update(zeroPosition,["ZERO"],[],null);flush();',context);
assert.equal(markers,0,'zero coordinate pair must not create a map marker');
context.zeroComponent=new Float64Array([0,30,NaN,0]);
vm.runInContext('window.ridMap.update(zeroComponent,["ZERO"],[],null);flush();',context);
assert.equal(markers,1,'a single zero coordinate component remains valid');
console.log(JSON.stringify({status:'PASS',scenarios:['unknown position excluded from GPU and selection','unknown altitude/heading do not block valid position','loss removes marker and trail','recovery starts new trail','coalesced loss breaks trail','source filter hides and restores targets','saved geofence renders on the realtime map','unverified marker follows its lifecycle and ignores zero GPS','per-target card frequency and height reference','click requests selection and selected card is on top','disconnect state','bounded card rendering']}));