// 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([390,125,2,2400,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').age.textContent,'数据年龄 390 ms'); assert.equal(entries.get('TWO').age.textContent,'数据年龄 2.4 秒'); 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'); 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').age.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 age and height reference','click requests selection and selected card is on top','disconnect state','bounded card rendering']}));