forked from linkong/planet
31 lines
1.6 KiB
JavaScript
31 lines
1.6 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: 600, y: 400 };
|
|
const result = placeObjectCallout(source, { width: 280, height: 360 },
|
|
{ width: 1200, height: 800 }, [{ x: 660, y: 0, width: 540, height: 800 }]);
|
|
expect(result.x + result.width).toBeLessThan(source.x);
|
|
expect(result.x).toBeGreaterThanOrEqual(12);
|
|
expect(result.y).toBeGreaterThanOrEqual(12);
|
|
expect(result.y + result.height).toBeLessThanOrEqual(788);
|
|
});
|
|
|
|
test("a narrow viewport retains a readable card beside the source", () => {
|
|
const result = placeObjectCallout({ x: 180, y: 420 }, { width: 280, height: 100 },
|
|
{ width: 390, height: 844 });
|
|
expect(result.x + result.width).toBeLessThanOrEqual(378);
|
|
expect(result.y + result.height < 420 || result.y > 420).toBe(true);
|
|
});
|
|
|
|
test("an upper-left object uses the nearby edge without covering a small globe", () => {
|
|
const viewport = { width: 1600, height: 900, globe: { x: 800, y: 450, radius: 250 } };
|
|
const source = { x: 670, y: 290 };
|
|
const result = placeObjectCallout(source, { width: 280, height: 330 }, viewport);
|
|
expect(result.x === 12 || result.y === 12).toBe(true);
|
|
expect(result.x + result.width / 2).toBeLessThan(viewport.globe.x);
|
|
const nearX = Math.max(result.x, Math.min(viewport.globe.x, result.x + result.width));
|
|
const nearY = Math.max(result.y, Math.min(viewport.globe.y, result.y + result.height));
|
|
expect(Math.hypot(nearX - viewport.globe.x, nearY - viewport.globe.y)).toBeGreaterThan(250);
|
|
});
|