// 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=[]; const gl={ARRAY_BUFFER:1,bindBuffer(){},bufferSubData(_t,_o,data){assert.ok([...data].every(Number.isFinite));}}; 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(){},isStyleLoaded(){return true;}}; class Marker{constructor(){markers++;this.element={dataset:{}};}setLngLat(p){assert.ok(p.every(Number.isFinite));return this;}addTo(){return this;}getElement(){return this.element;}remove(){markers--;}} let now=1000; const context=vm.createContext({window:{},document:{createElement(){return{};},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,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); update([120,30,NaN,NaN],1);assert.equal(drawCount,1);vm.runInContext('window.ridMap.select("TEST");flush();',context);assert.equal(markers,1); 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'); 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']}));