Files
fpv-rid-workbench/scripts/test-map-settings-adapter.mjs
JiuContinent 461e2c8e7f ```
feat(web-ui): 重构地图设置与目标列表页面并优化实时地图导航体验

- 重构地图设置页面布局,采用 flex 替代 grid,新增导航栏、标签页
  和下载步骤组件,改善响应式断点适配
- 将目标列表管理从侧边抽屉方案改为全页面内联编辑器,包含表格视图
  与分类侧栏,支持移动端卡片式降级展示
- 为实时地图屏幕添加半透明毛玻璃导航栏样式,支持 backdrop-filter
  降级,并在地图模式下调整内容区域偏移量
- 修复地图渲染器在画布尺寸为 0 时的边界异常,增加宽高校验
- 优化 fitBounds 的 padding 计算,改为基于容器宽度的比例值并
  限制最大值,避免小屏下地图偏移过大
- 新增 B5、B6、B7 三组 SVG 壁纸资源
```
2026-10-23 14:50:27 +08:00

49 lines
4.1 KiB
JavaScript

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()}
fitBounds(bounds,options){this.fit={bounds,options}}
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);
}));
test('region framing uses the actual map viewport without obsolete drawer padding',()=>fixture(({api,map})=>{
const bounds={west:118,south:32,east:119,north:33};
api.bounds(bounds);assert.equal(map().fit.options.padding.left,60);assert.equal(map().fit.options.padding.right,60);
map().container.clientWidth=200;api.bounds(bounds);assert.equal(map().fit.options.padding.left,40);assert.equal(map().fit.options.padding.right,40);
}));