Files
planet/frontend/public/earth/js/cluster-presentation.test.js
rayd1o 05377bf5c6
ci / backend (push) Has been cancelled
ci / frontend (push) Has been cancelled
ci / delivery (push) Has been cancelled
release / images (push) Has been cancelled
release: bump version to 0.76.0
2026-09-30 17:49:29 +08:00

195 lines
13 KiB
JavaScript

import {expect,test,spyOn} from "bun:test";
import * as THREE from "three";
import {createInteractableLayer,updateInteractableClusters,clearInteractableClusters} from "./interactable.js";
import {distanceForGlobeZoom} from "./globe-navigation.js";
import {createClusterPresentation} from "./cluster-presentation.js";
class Node {
constructor(tag){this.tag=tag;this.children=[];this.style={setProperty(){}};this.classes=new Set();this.classList={add:(name)=>this.classes.add(name),toggle:(name,on)=>on?this.classes.add(name):this.classes.delete(name)};this.attrs={};this.events={};this.hidden=false;}
toDataURL(){return "data:image/png;base64,";}
getContext(){return new Proxy({}, {get:()=>()=>{}});}
append(node){node.parent=this;this.children.push(node);}
remove(){if(this.parent)this.parent.children=this.parent.children.filter(child=>child!==this);}
setAttribute(key,value){this.attrs[key]=String(value);}
addEventListener(name,handler){this.events[name]=handler;}
contains(target){return target===this||this.children.some(child=>child.contains(target));}
}
test("500% co-located groups finish spiderfy, keep source positions and clean up on filtering",()=>{
const original={document:globalThis.document,window:globalThis.window,innerWidth:globalThis.innerWidth,innerHeight:globalThis.innerHeight};
let now=0;const clock=spyOn(performance,"now").mockImplementation(()=>now);
const root=new Node("root"),listeners=new Map();const events=[];
globalThis.document={createElement:tag=>new Node(tag),createElementNS:(_,tag)=>new Node(tag),getElementById:()=>root,addEventListener:(key,fn)=>listeners.set(key,fn),removeEventListener:key=>listeners.delete(key)};
globalThis.window={addEventListener:(key,fn)=>listeners.set(key,fn),removeEventListener:key=>listeners.delete(key),dispatchEvent:event=>events.push(event)};
globalThis.innerWidth=1200;globalThis.innerHeight=800;
const earth=new THREE.Object3D(),group=new THREE.Group();earth.add(group);earth.updateMatrixWorld(true);
const camera=new THREE.PerspectiveCamera(75,1.5,.1,5000);camera.position.z=350;camera.updateMatrixWorld();
const markers=[1,2].map(id=>{const m=new THREE.Object3D();m.position.set(0,0,100.5);m.userData={id,name:`site ${id}`,type:"compute_center",icon_layer_id:"compute",icon_kind:"gpu_cluster",latitude:0,longitude:-90};return m;});
let visible=true;
const owner={group,isMarkerVisible:()=>visible,getMarkerColor:()=>"#a855f7",getMarkerImage:()=>"data:image/png;base64,"};
const record={clusterId:"same-place",position:new THREE.Vector3(0,0,100.5),markers,expanded:true};
let liveLayer;
const presentation=createClusterPresentation({getOwner:()=>owner,onHiddenChange(){}});
try {
const satellite = { properties: { name:"STARLINK-2507", norad_id:48316 }, geometry:{coordinates:[10,20]} };
expect(()=>presentation.update([record],camera,5,record.markers,satellite)).not.toThrow();
expect(()=>presentation.update([record],camera,5,record.markers,{userData:{cableId:"cable"}})).not.toThrow();
for(const type of ["bgp","bgp_collector","compute_center","vessel","earth_interactable"]) {
const selection=new THREE.Object3D();
selection.userData={type,icon_layer_id:type,id:`selected-${type}`};
expect(()=>presentation.update([record],camera,5,record.markers,selection)).not.toThrow();
}
expect(()=>presentation.update([record],camera,5,record.markers,null)).not.toThrow();
presentation.update([record],camera,5);now=320;presentation.update([record],camera,5);
const nodes=root.children[0].children;
const badge=nodes.find(node=>node.className==="map-cluster-badge");
const children=nodes.filter(node=>node.className==="map-cluster-child");
expect(badge.hidden).toBe(true);expect(children.length).toBe(2);
expect(children.every(child=>!child.hidden)).toBe(true);
expect(children[0].style.transform).not.toBe(children[1].style.transform);
children[0].events.click();expect(events.at(-1).detail.marker).toBe(markers[0]);
const leg=nodes.find(node=>node.tag==="svg").children.find(node=>node.classes.has("map-cluster-leg-hit"));
leg.events.pointerenter();expect(children[0].classes.has("is-highlighted")).toBe(true);
expect(children[1].classes.has("is-highlighted")).toBe(false);
leg.events.click();expect(events.at(-1).detail.marker).toBe(markers[0]);
leg.events.pointerleave();expect(children[0].classes.has("is-highlighted")).toBe(false);
children[0].events.pointerenter();expect(children[0].classes.has("is-highlighted")).toBe(true);
children[0].events.pointerleave();
const originalSlots=children.map(child=>child.style.transform);
markers[0].userData.state="locked";now=350;presentation.update([record],camera,5);
expect(children[0].style.zIndex).toBe("1000");
expect(children[0].attrs["aria-pressed"]).toBe("true");
expect(children[0].style.transform).not.toBe(originalSlots[0]);
markers[0].userData.state="normal";now=360;presentation.update([record],camera,5);
expect(children.map(child=>child.style.transform)).toEqual(originalSlots);
expect(markers.every(marker=>marker.position.equals(record.position))).toBe(true);
listeners.get("wheel")();now=700;presentation.update([record],camera,5);
expect(badge.hidden).toBe(true);
const finalTransforms = children.map(child => child.style.transform);
now=1100;presentation.update([record],camera,5);
expect(children.map(child => child.style.transform)).toEqual(finalTransforms);
// Geographically distinct children end on their actual projected positions,
// and retain the same DOM nodes instead of handing off to a second renderer.
presentation.clear();
markers[0].position.set(-50,0,Math.sqrt(100.5**2-50**2));markers[0].userData.longitude=-95;
markers[1].position.set(50,0,Math.sqrt(100.5**2-50**2));markers[1].userData.longitude=-85;
presentation.update([record],camera,5);
now=1260;presentation.update([record],camera,5);
const inFlight=root.children[0].children.filter(node=>node.className==="map-cluster-child");
expect(inFlight.every(node=>!node.hidden)).toBe(true);
const midFlight=inFlight.map(node=>node.style.transform);
now=1420;presentation.update([record],camera,5);
const stableChildren=root.children[0].children.filter(node=>node.className==="map-cluster-child");
const stableTransforms=stableChildren.map(child=>child.style.transform);
expect(stableTransforms).not.toEqual(midFlight);
stableChildren[0].events.click();
const projected=markers[0].position.clone().project(camera);
expect(events.at(-1).detail.screen.x).toBeCloseTo((projected.x+1)*600,6);
expect(events.at(-1).detail.screen.y).toBeCloseTo((1-projected.y)*400,6);
now=2400;presentation.update([record],camera,5);
expect(stableChildren.map(child=>child.style.transform)).toEqual(stableTransforms);
expect(root.children[0].children.filter(node=>node.className==="map-cluster-child")[0]).toBe(stableChildren[0]);
// Refreshing the same business IDs replaces Object3D instances. Old DOM
// members must not survive alongside their replacements or after collapse.
const retired = [...stableChildren];
record.markers = markers.map(marker => {
const replacement = new THREE.Object3D();
replacement.position.copy(marker.position);
replacement.userData = {...marker.userData};
return replacement;
});
now=2500;presentation.update([record],camera,5);
expect(root.children[0].children.filter(node=>node.className==="map-cluster-child").length).toBe(2);
expect(retired.every(node=>!root.children[0].children.includes(node))).toBe(true);
presentation.update([record],camera,5,record.markers,markers[0]);
const refreshedSelected=root.children[0].children.find(node=>node.attrs["aria-pressed"]==="true");
expect(refreshedSelected.style.zIndex).toBe("1000");
presentation.update([record],camera,5,record.markers,null);
expect(root.children[0].children.some(node=>node.attrs["aria-pressed"]==="true")).toBe(false);
for (let cycle=0;cycle<4;cycle++) {
record.expanded=false;now+=400;presentation.update([record],camera,1);
now+=320;presentation.update([record],camera,1);
expect(root.children[0].children.filter(node=>node.className==="map-cluster-child").length).toBe(0);
record.expanded=true;now+=400;presentation.update([record],camera,5);
now+=320;presentation.update([record],camera,5);
}
visible=false;presentation.update([record],camera,5);
expect(markers.every(marker=>!marker.userData.icon_presentation_hidden)).toBe(true);
expect(root.children[0].children.filter(node=>node.className==="map-cluster-child").length).toBe(0);
presentation.destroy();
liveLayer=createInteractableLayer({id:"lifecycle-regression",objectType:"compute_center",cluster:{enabled:true},
getPosition:item=>item,icon:{draw(){}}});
liveLayer.attach(earth);liveLayer.setVisible(true);
for(const selection of [satellite,{userData:{cableId:"cable"}},null]) {
expect(()=>updateInteractableClusters(camera,selection)).not.toThrow();
}
const data=[{id:"a",latitude:0,longitude:-90},{id:"b",latitude:0.01,longitude:-90}];
const gpuCount=()=>{let count=0;liveLayer.group.traverse(node=>{count+=node.userData.markers?.length||0;});return count;};
for(let cycle=0;cycle<4;cycle++) {
camera.position.z=distanceForGlobeZoom(5,100,300);camera.updateMatrixWorld();
liveLayer.setData(data);now+=400;updateInteractableClusters(camera);
now+=320;updateInteractableClusters(camera);
expect(gpuCount()).toBe(0);
// Same IDs with fresh marker instances cannot keep a stale GPU batch.
liveLayer.setData(data);now+=400;updateInteractableClusters(camera);
expect(gpuCount()).toBe(0);
camera.position.z=distanceForGlobeZoom(1,100,300);camera.updateMatrixWorld();
now+=400;updateInteractableClusters(camera);now+=320;updateInteractableClusters(camera);
expect(gpuCount()).toBe(0);
expect(root.children[0].children.filter(node=>node.className==="map-cluster-child").length).toBe(0);
}
clearInteractableClusters();
visible=true;record.expanded=true;
// Different geographic anchors can overlap on screen. Their endpoints
// must still be exact projections, with no screen-space stack offsets.
record.markers.forEach((marker,index)=>{
marker.position.set(index === 0 ? -0.05 : 0.05,0,100.5);
marker.userData.longitude=index === 0 ? -90.03 : -89.97;
});
now+=400;presentation.update([record],camera,5);
now+=320;presentation.update([record],camera,5);
const closeChildren=root.children[0].children.filter(node=>node.className==="map-cluster-child");
expect(closeChildren.length).toBe(2);
closeChildren.forEach((child,index)=>{
child.events.click();
const target=record.markers[index].position.clone().project(camera);
expect(events.at(-1).detail.screen.x).toBeCloseTo((target.x+1)*600,6);
expect(events.at(-1).detail.screen.y).toBeCloseTo((1-target.y)*400,6);
});
presentation.clear();
// A selected unclustered marker must share the same foreground presentation,
// including overlap with an icon from a higher WebGL layer.
const independent=record.markers[0];
independent.userData.collector="rrc13";
independent.userData.name="rrc13";
independent.userData.type="bgp_collector";
now+=400;presentation.update([{clusterId:"collapsed-selected",markers:[independent],position:independent.position,expanded:false}],camera,1,[independent],independent);
now+=320;presentation.update([{clusterId:"collapsed-selected",markers:[independent],position:independent.position,expanded:false}],camera,1,[independent],independent);
const focusedStandalone=root.children[0].children.find(node=>node.className==="map-cluster-child");
expect(focusedStandalone.attrs["aria-pressed"]).toBe("true");
expect(focusedStandalone.style.zIndex).toBe("1000");
expect(focusedStandalone.hidden).toBe(false);
expect(independent.userData.icon_presentation_hidden).toBe(true);
presentation.update([],camera,1,[independent],null);
expect(root.children[0].children.filter(node=>node.className==="map-cluster-child").length).toBe(0);
expect(independent.userData.icon_presentation_hidden).toBe(false);
presentation.clear();
owner.getMarkerImage=()=>{now+=400;return "data:image/png;base64,";};
presentation.update([record],camera,5);
now+=160;presentation.update([record],camera,5);
const preparedChildren=root.children[0].children.filter(node=>node.className==="map-cluster-child");
expect(preparedChildren.length).toBe(2);
expect(Number(preparedChildren[0].style.opacity)).toBeCloseTo(0.5);
now+=16;presentation.update([record],camera,5);
expect(Number(preparedChildren[0].style.opacity)).toBeLessThan(0.8);
// Cached parent projection must refresh on the very next rotated frame.
earth.rotation.y=.2; now+=400; presentation.update([record],camera,5);
const rotated=root.children[0].children.filter(node=>node.className==="map-cluster-child");
rotated.forEach((child,index)=>{
child.events.click();
const expected=record.markers[index].position.clone().applyMatrix4(earth.matrixWorld).project(camera);
expect(events.at(-1).detail.screen.x).toBeCloseTo((expected.x+1)*600,6);
expect(events.at(-1).detail.screen.y).toBeCloseTo((1-expected.y)*400,6);
});
}finally{liveLayer?.clearData();clearInteractableClusters();presentation.destroy();clock.mockRestore();Object.assign(globalThis,original);}
});