Files
planet/frontend/public/earth/js/object-callout-layout.test.js
linkong 1d153c2667
ci / backend (push) Has been cancelled
ci / frontend (push) Has been cancelled
release / images (push) Has been cancelled
ci / backend (pull_request) Has been cancelled
ci / frontend (pull_request) Has been cancelled
ci / delivery (push) Has been cancelled
ci / delivery (pull_request) Has been cancelled
release: bump version to 0.76.20
2026-11-10 17:52:24 +08:00

115 lines
4.9 KiB
JavaScript

import { expect, test } from "bun:test";
import { placeObjectCallout } from "./object-callout-layout.js";
test("object card uses free space beside the object and stays inside the viewport", () => {
const source = { x: 500, y: 300 };
const viewport = { width: 1000, height: 800, globe: { x: 500, y: 400, radius: 250 } };
const result = placeObjectCallout(source, { width: 280, height: 360 },
viewport, [{ x: 480, y: 280, width: 40, height: 40 }]);
expect(result.x).toBeGreaterThanOrEqual(14);
expect(result.x + result.width).toBeLessThanOrEqual(986);
expect(result.y).toBeGreaterThanOrEqual(64);
expect(result.y + result.height).toBeLessThanOrEqual(690);
});
test("a narrow viewport retains a readable card beside the source", () => {
const result = placeObjectCallout({ x: 180, y: 420 }, { width: 280, height: 100 },
{ width: 360, height: 640 });
expect(result.x).toBeGreaterThanOrEqual(14);
expect(result.x + result.width).toBeLessThanOrEqual(346);
expect(result.width).toBeLessThanOrEqual(332);
});
test("an upper-left object uses the nearby edge without covering a small globe", () => {
const source = { x: 120, y: 140 };
const viewport = { width: 1200, height: 800, globe: { x: 600, y: 400, radius: 180 } };
const result = placeObjectCallout(source, { width: 280, height: 330 }, viewport);
expect(result.x).toBeGreaterThanOrEqual(14);
expect(result.y).toBeGreaterThanOrEqual(64);
});
test("an object on a normal/large globe radiates outward to outer space rim and strictly avoids globe center", () => {
// Center is at (700, 450), radius 350. Right rim is at x = 1050.
// Object is at (880, 420) on the right hemisphere.
const source = { x: 880, y: 420 };
const viewport = { width: 1400, height: 900, globe: { x: 700, y: 450, radius: 350 } };
const result = placeObjectCallout(source, { width: 340, height: 360 }, viewport);
// Must sit on the right flank
expect(result.x).toBeGreaterThan(viewport.globe.x);
expect(result.side).toBe("left");
});
test("an anchor on the left hemisphere never throws its card across the globe to the right flank", () => {
// Center is at (700, 450), radius 350. Left rim is at x = 350.
// Object is at (520, 420) on the left hemisphere.
const source = { x: 520, y: 420 };
const viewport = { width: 1400, height: 900, globe: { x: 700, y: 450, radius: 350 } };
// Even with an obstacle on the left, it must stay on the left flank!
const obstacles = [{ x: 50, y: 100, width: 80, height: 700 }];
const result = placeObjectCallout(source, { width: 340, height: 360 }, viewport, obstacles);
// Must stay on the left flank!
expect(result.x + result.width).toBeLessThan(viewport.globe.x);
expect(result.side).toBe("right");
});
test("globe zooming preserves monotonic X expansion and smooth vertical slot Y without hesitation or jitter", () => {
const source = { x: 880, y: 420 };
const size = { width: 340, height: 360 };
let lastX = 0;
let prevPos = null;
// Zoom in smoothly from radius 320 to 450 in 10 steps
for (let r = 320; r <= 450; r += 13) {
const viewport = {
width: 1600,
height: 900,
globe: { x: 800, y: 450, radius: r },
previousPosition: prevPos,
};
const res = placeObjectCallout(source, size, viewport);
// 1. X must monotonically push outward to the right as radius expands
expect(res.x).toBeGreaterThan(lastX);
lastX = res.x;
// 2. Y stays well within safe viewport bounds
expect(res.y).toBeGreaterThanOrEqual(64);
expect(res.y + size.height).toBeLessThanOrEqual(viewport.height - 14);
prevPos = { x: res.x, y: res.y };
}
});
test("sensitive horizontal flank handover and smooth rotation tracking without flip-flopping", () => {
const size = { width: 340, height: 360 };
const viewport = {
width: 1600,
height: 900,
globe: { x: 800, y: 450, radius: 380 },
};
// Initially at right flank (920px):
let pos = placeObjectCallout({ x: 920, y: 420 }, size, viewport);
expect(pos.x).toBeGreaterThan(viewport.globe.x);
// Swing leftwards towards meridian (840px, still in right hemisphere > 800)
pos = placeObjectCallout({ x: 840, y: 420 }, size, { ...viewport, previousPosition: pos });
expect(pos.x).toBeGreaterThan(viewport.globe.x);
// Swing across meridian into left hemisphere (760px < 800 - 12):
// Highly sensitive handover: immediately hands over to left flank!
pos = placeObjectCallout({ x: 760, y: 420 }, size, { ...viewport, previousPosition: pos });
expect(pos.x + size.width).toBeLessThan(viewport.globe.x);
// Swing back rightwards (950px > 800 + 12):
pos = placeObjectCallout({ x: 950, y: 420 }, size, { ...viewport, previousPosition: pos });
expect(pos.x).toBeGreaterThan(viewport.globe.x);
// Sensitive handover to left hemisphere again (680px):
const crossPos = placeObjectCallout({ x: 680, y: 420 }, size, { ...viewport, previousPosition: pos });
expect(crossPos.x + size.width).toBeLessThan(viewport.globe.x);
});