Files
planet/frontend/public/earth/js/spatial-callouts.test.js
linkong c1d651589a
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.19
2026-11-09 15:12:50 +08:00

197 lines
7.5 KiB
JavaScript

import { expect, test } from "bun:test";
import * as THREE from "three";
import { CalloutConnector, createConnectorPath } from "./callout-connector.js";
import { calculateOrbitalTarget, renderSpatialCalloutConnector } from "./spatial-callout-controller.js";
import { projectGeographicAnchor } from "./spatial-callouts.js";
test("geographic anchor preserves its real projection and front/rear depth while rotating", () => {
const earth = new THREE.Object3D();
const camera = new THREE.PerspectiveCamera(45, 1, 1, 1000);
camera.position.set(0, 0, 300);
camera.lookAt(0, 0, 0);
camera.updateMatrixWorld();
earth.updateMatrixWorld();
const front = projectGeographicAnchor({ latitude: 0, longitude: -90 }, earth, camera, 800, 800);
const rear = projectGeographicAnchor({ latitude: 0, longitude: 90 }, earth, camera, 800, 800);
expect(front.front).toBe(true);
expect(rear.front).toBe(false);
expect(Number.isFinite(rear.x)).toBe(true);
expect(Number.isFinite(rear.y)).toBe(true);
earth.rotation.y = Math.PI / 2;
earth.updateMatrixWorld();
const rotated = projectGeographicAnchor(
{ latitude: 0, longitude: -90 },
earth,
camera,
800,
800,
);
expect(rotated.front).toBe(false);
expect(rotated.x).not.toBe(front.x);
});
test("rear connector remains visible at the real projection and refreshes at the limb", () => {
const originalHTMLElement = globalThis.HTMLElement;
globalThis.HTMLElement = class {};
const classes = new Set();
const connectorElement = {
classList: {
toggle(name, active) { if (active) classes.add(name); else classes.delete(name); },
},
dataset: {},
};
const paths = [];
const connector = {
connectorEl: connectorElement,
hide() { classes.delete("is-visible"); },
isVisible() { return classes.has("is-visible"); },
render(path) { paths.push(path); classes.add("is-visible"); return true; },
};
const state = {
point: { x: 700, y: 260, front: true },
sourcePoint: { x: 430, y: 390, front: false },
x: 80,
y: 120,
targetX: 80,
targetY: 120,
width: 220,
height: 150,
connector,
};
try {
expect(renderSpatialCalloutConnector(state)).toBe(true);
expect(paths[0].start).toMatchObject({ x: 430, y: 390 });
expect(classes.has("is-rear")).toBe(true);
expect(connectorElement.dataset.anchorDepth).toBe("rear");
state.sourcePoint = { x: 430.2, y: 390.2, front: true };
expect(renderSpatialCalloutConnector(state)).toBe(true);
expect(paths.length).toBe(2);
expect(classes.has("is-rear")).toBe(false);
expect(connectorElement.dataset.anchorDepth).toBe("front");
connector.hide();
expect(renderSpatialCalloutConnector(state)).toBe(true);
expect(paths.length).toBe(3);
} finally {
globalThis.HTMLElement = originalHTMLElement;
}
});
for (const animate of [false, true]) {
test(`shared connector avoids SVG length reads and preserves animation=${animate}`, () => {
const originalWindow = globalThis.window;
globalThis.window = { innerWidth: 1000, innerHeight: 800 };
let animationLayoutReads = 0;
const svg = { setAttribute() {}, style: { setProperty() {} },
classList: { add() {}, remove() {} },
getBoundingClientRect() { animationLayoutReads++; },
};
const connector = new CalloutConnector();
connector.ensure = () => svg;
connector.polylineEl = { setAttribute() {}, style: {},
getTotalLength() { throw new Error("moving connectors must not force SVG layout"); },
};
connector.startpointEl = connector.endpointEl = { setAttribute() {}, getAttribute() { return "3"; } };
try {
expect(connector.render({ points: [{ x: 0, y: 0 }, { x: 3, y: 4 }, { x: 3, y: 16 }],
start: { x: 0, y: 0 }, end: { x: 3, y: 16 } }, { animate })).toBe(true);
expect(connector.polylineEl.style.strokeDasharray).toBe("17");
expect(connector.polylineEl.style.strokeDashoffset).toBe(animate ? "17" : "0");
expect(animationLayoutReads).toBe(animate ? 1 : 0);
} finally {
if (originalWindow === undefined) delete globalThis.window;
else globalThis.window = originalWindow;
}
});
}
test("compact card in lower hemisphere expands downward when space below is sufficient", () => {
const globe = { x: 700, y: 540, radius: 360 };
const viewport = { width: 1920, height: 1080 };
const classes = new Set();
const element = {
classList: {
toggle(name, val) { if (val) classes.add(name); else classes.delete(name); },
remove(name) { classes.delete(name); },
},
};
// Card anchor at y = 640 (lower hemisphere, but with ample room below)
const state = {
point: { x: 540, y: 640, front: true },
sourcePoint: { x: 540, y: 640, front: true },
width: 220,
height: 42,
element,
hoverExpanded: false,
};
const collapsed = calculateOrbitalTarget(state, globe, viewport, []);
expect(collapsed.y).toBe(625);
// Now hover-expanded:
state.hoverExpanded = true;
state.width = 340;
state.height = 340;
const expanded = calculateOrbitalTarget(state, globe, viewport, []);
// Must expand downwards, keeping card's top anchored directly to capsuleY (625)
expect(expanded.y).toBe(625);
expect(classes.has("hud-panel--expand-down")).toBe(true);
expect(classes.has("hud-panel--expand-up")).toBe(false);
});
test("compact card expands upward only when near the bottom edge", () => {
const globe = { x: 700, y: 540, radius: 360 };
const viewport = { width: 1920, height: 1080 };
const classes = new Set();
const element = {
classList: {
toggle(name, val) { if (val) classes.add(name); else classes.delete(name); },
remove(name) { classes.delete(name); },
},
};
// Card anchor at y = 960 (extreme bottom edge near toolbar)
const state = {
point: { x: 540, y: 960, front: true },
sourcePoint: { x: 540, y: 960, front: true },
width: 220,
height: 42,
element,
hoverExpanded: true,
};
const expanded = calculateOrbitalTarget(state, globe, viewport, []);
expect(classes.has("hud-panel--expand-up")).toBe(true);
expect(classes.has("hud-panel--expand-down")).toBe(false);
expect(expanded.y).toBeLessThan(900);
});
test("hover-expanded card registers only its base capsule footprint so neighbors are never displaced", () => {
const globe = { x: 700, y: 540, radius: 360 };
const viewport = { width: 1920, height: 1080 };
const createMock = (key, pt, hover = false) => ({
key,
point: { ...pt, front: true },
sourcePoint: { ...pt, front: true },
width: hover ? 340 : 220,
height: hover ? 340 : 42,
hoverExpanded: hover,
element: { classList: { toggle() {}, remove() {} } },
});
// 1. Calculate normal positions for Europe and Middle East
const normalOccupied = [];
const normalEurope = calculateOrbitalTarget(createMock("eu", { x: 700, y: 220 }), globe, viewport, normalOccupied);
const normalMiddleEast = calculateOrbitalTarget(createMock("me", { x: 680, y: 340 }), globe, viewport, normalOccupied);
// 2. Hover Europe: Middle East must NOT be displaced or pushed upward
const hoverOccupied = [];
const hoverEurope = calculateOrbitalTarget(createMock("eu", { x: 700, y: 220 }, true), globe, viewport, hoverOccupied);
const middleEastWithEuropeHovered = calculateOrbitalTarget(createMock("me", { x: 680, y: 340 }), globe, viewport, hoverOccupied);
expect(hoverEurope.width).toBe(340);
expect(hoverEurope.height).toBe(340);
// Middle East must retain the exact same position as in normal state!
expect(middleEastWithEuropeHovered.x).toBe(normalMiddleEast.x);
expect(middleEastWithEuropeHovered.y).toBe(normalMiddleEast.y);
});