```
feat(geofences): 新增地理围栏模块并优化地图瓦片缓存 在 rid-shared 中实现二维 WGS84 地理围栏核心逻辑,支持圆形与多边形 两种几何体,包含坐标合法性校验、边界包含判断及自相交检测。 realtime-core 的 Store 新增观察者回调,在观测数据接受后按到达顺序 通知监听器。 前端方面,新增 geofence-map-adapter.js 提供基于 MapLibre 的围栏绘制 与编辑能力,配套 geofences.css 完成响应式布局样式。map-loader.js 增加懒加载入口,仅在实时工作区按需引入围栏地图模块。 同时引入 map-tile-buffer 瓦片缓冲机制,改善地图切换与刷新时的加载 体验;将默认缩放级别从 11 调整为 15,使初始视图更贴近实际监测场景。 ```
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
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);
|
||||
}));
|
||||
Reference in New Issue
Block a user