import {test} from 'node:test'; import assert from 'node:assert/strict'; import {readFile} from 'node:fs/promises'; let serial=0; async function fixture(run){ const names=['document','window','ResizeObserver','CustomEvent','testMapLibre'];const saved=new Map(names.map(k=>[k,globalThis[k]])); const emitted=[];let current; const canvas=()=>({handlers:{},addEventListener(k,f){this.handlers[k]=f},getBoundingClientRect(){return {left:0,top:0,width:800,height:500}},setPointerCapture(){},style:{},focus(){this.focused=true}}); const root={appendChild(e){this.selection=e}}; class FakeMap{ constructor(p){current=this;this.options=p;this.handlers={};this.canvas=canvas();this.sources=structuredClone(p.style.sources);this.loadedStyle=false;this.container={clientWidth:800};} addControl(){}on(name,f){(this.handlers[name]??=[]).push(f)}getCanvasContainer(){return this.canvas}getCanvas(){return this.canvas}getContainer(){return this.container} getSource(k){if(!this.loadedStyle)return;return {setTiles:tiles=>this.sources[k].tiles=tiles}} load(){this.loadedStyle=true;for(const f of this.handlers.load??[])f()} dragPan={disable(){},enable(){}};unproject([x,y]){return {lng:x/100,lat:10-y/100}}remove(){}resize(){} } class Marker{setLngLat(){return this}addTo(){return this}} globalThis.testMapLibre={Map:FakeMap,Marker,NavigationControl:class{},AttributionControl:class{},setWorkerUrl(){}}; globalThis.document={getElementById(){return root},createElement(){return {style:{},hidden:false}}}; globalThis.window={dispatchEvent:e=>emitted.push(e)};globalThis.CustomEvent=class{constructor(type,p){this.type=type;this.detail=p.detail}}; globalThis.ResizeObserver=class{observe(){}disconnect(){}}; try{ const source=(await readFile('crates/web-ui/public/map-settings-adapter.js','utf8')).replace(/import \{attachTileBuffer\} from '[^']+';/, 'const attachTileBuffer=()=>({destroy(){},schedule(){},metrics(){return {}}});').replace(/import \* as ml from '[^']+';/,'const ml=globalThis.testMapLibre;'); const api=await import('data:text/javascript;base64,'+Buffer.from(source+`\n// test ${serial++}`).toString('base64')); api.open({basemap:'imagery',longitude:120.15,latitude:30.28,zoom:11}); await run({api,map:()=>current,root,emitted});api.close(); }finally{for(const k of names){if(saved.get(k)===undefined)delete globalThis[k];else globalThis[k]=saved.get(k)}} } test('basemap selected before style load is applied when ready',()=>fixture(({api,map})=>{ api.setBasemap('road');map().load();assert.match(map().sources.base.tiles[0],/vec_w/);assert.match(map().sources.labels.tiles[0],/cva_w/); })); test('closing during selection cannot leak a rectangle into the next map',()=>fixture(({api,map,emitted})=>{ api.tool('box');map().canvas.handlers.pointerdown({button:0,clientX:20,clientY:30,pointerId:1,preventDefault(){}}); map().canvas.handlers.pointermove({clientX:160,clientY:170});api.close();api.open({basemap:'imagery',longitude:120,latitude:30,zoom:11}); map().canvas.handlers.pointermove({clientX:200,clientY:210});map().canvas.handlers.pointerup();assert.equal(emitted.filter(e=>e.type==='rid-map-bounds').length,0); })); test('Escape is reachable after selecting a toolbar tool',()=>fixture(({api,map})=>{api.tool('point');assert.equal(map().canvas.focused,true);})); test('panning does not leave an obsolete screen rectangle over another area',()=>fixture(({api,map,root})=>{ api.tool('box');map().canvas.handlers.pointerdown({button:0,clientX:20,clientY:30,pointerId:1,preventDefault(){}});map().canvas.handlers.pointermove({clientX:160,clientY:170});map().canvas.handlers.pointerup(); for(const f of map().handlers.movestart??[])f();assert.equal(root.selection.hidden,true); }));