159 lines
7.2 KiB
JavaScript
159 lines
7.2 KiB
JavaScript
import { describe, expect, it } from "vitest";
|
|
import {
|
|
CLICK_TOLERANCE_PX, chunkNameAtMap, clampView, fitView, headingFromDirection, hoverLabel, isClick,
|
|
isMapToggleKey, landingFor, mapToWorld, nearestWalkable, screenToMap, sha256Hex, worldToMap, zoomAt,
|
|
} from "../src/ui/mapMath.js";
|
|
|
|
const INFO = { scale: 1, walkable_scale: 0.25, slice: { x0: -1000, x1: 2000, y0: -500, y1: 2500 } };
|
|
|
|
describe("Welt und Karte", () => {
|
|
it("setzt den Spawn an die richtige Kartenstelle", () => {
|
|
expect(worldToMap(INFO, 1699, -743)).toEqual({ px: 2699, py: 1757 });
|
|
});
|
|
|
|
it("legt die vier Ecken des Ausschnitts auf die vier Bildecken — Norden oben", () => {
|
|
expect(worldToMap(INFO, -1000, -2500)).toEqual({ px: 0, py: 0 }); // Nordwest
|
|
expect(worldToMap(INFO, 2000, -2500)).toEqual({ px: 3000, py: 0 }); // Nordost
|
|
expect(worldToMap(INFO, -1000, 500)).toEqual({ px: 0, py: 3000 }); // Südwest
|
|
expect(worldToMap(INFO, 2000, 500)).toEqual({ px: 3000, py: 3000 }); // Südost
|
|
});
|
|
|
|
it("kommt hin und zurück", () => {
|
|
for (const [x, z] of [[1699, -743], [-511, -254], [0, 0], [123.5, -2001.25]]) {
|
|
const { px, py } = worldToMap(INFO, x, z);
|
|
const back = mapToWorld(INFO, px, py);
|
|
expect(back.x).toBeCloseTo(x, 9);
|
|
expect(back.z).toBeCloseTo(z, 9);
|
|
}
|
|
});
|
|
|
|
it("liegt ein nördlicherer Punkt weiter oben — fängt eine gespiegelte Achse", () => {
|
|
const south = worldToMap(INFO, 0, -100); // Welt y = 100
|
|
const north = worldToMap(INFO, 0, -900); // Welt y = 900
|
|
expect(north.py).toBeLessThan(south.py);
|
|
});
|
|
});
|
|
|
|
describe("Ansicht", () => {
|
|
it("passt die ganze Karte zentriert ins Fenster", () => {
|
|
expect(fitView(3000, 1200, 900)).toEqual({ scale: 0.3, offsetX: 150, offsetY: 0, minScale: 0.3 });
|
|
});
|
|
|
|
it("hält beim Zoomen den Punkt unter dem Mauszeiger fest", () => {
|
|
const view = fitView(3000, 1200, 900);
|
|
const before = screenToMap(view, 700, 400);
|
|
const after = screenToMap(zoomAt(view, 2, 700, 400), 700, 400);
|
|
expect(after.px).toBeCloseTo(before.px, 9);
|
|
expect(after.py).toBeCloseTo(before.py, 9);
|
|
});
|
|
|
|
it("zoomt nie weiter heraus als der ganze Ausschnitt und nie weiter hinein als vierfach", () => {
|
|
const view = fitView(3000, 1200, 900);
|
|
expect(zoomAt(view, 0.01, 600, 450).scale).toBeCloseTo(0.3, 9);
|
|
expect(zoomAt(view, 100, 600, 450).scale).toBeCloseTo(1.2, 9);
|
|
});
|
|
|
|
it("zentriert eine Karte, die kleiner als das Fenster ist", () => {
|
|
const view = { scale: 0.3, offsetX: -500, offsetY: 999, minScale: 0.3 };
|
|
expect(clampView(view, 3000, 1200, 900)).toMatchObject({ offsetX: 150, offsetY: 0 });
|
|
});
|
|
|
|
it("lässt eine vergrößerte Karte nicht über ihren Rand hinaus verschieben", () => {
|
|
const view = { scale: 1.2, offsetX: 50, offsetY: -99999, minScale: 0.3 }; // Inhalt 3600 px
|
|
const clamped = clampView(view, 3000, 1200, 900);
|
|
expect(clamped.offsetX).toBe(0);
|
|
expect(clamped.offsetY).toBe(900 - 3600);
|
|
});
|
|
});
|
|
|
|
describe("Klick gegen Ziehen", () => {
|
|
it("zählt unter 4 px Bewegung als Klick und ab 4 px als Ziehen", () => {
|
|
expect(CLICK_TOLERANCE_PX).toBe(4);
|
|
expect(isClick({ x: 10, y: 10 }, { x: 13.9, y: 10 })).toBe(true);
|
|
expect(isClick({ x: 10, y: 10 }, { x: 14, y: 10 })).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("Landeplatz", () => {
|
|
const blank = (w, h) => new Uint8Array(w * h);
|
|
|
|
it("bleibt bei einem begehbaren Treffer genau am Klickpunkt", () => {
|
|
const mask = blank(10, 10);
|
|
mask[2 * 10 + 5] = 255;
|
|
expect(nearestWalkable(mask, 10, 10, 5.4, 2.2, 5)).toEqual({ x: 5.4, y: 2.2 });
|
|
});
|
|
|
|
it("findet von einem Gebäude aus die Straße daneben", () => {
|
|
const mask = blank(10, 10);
|
|
for (let y = 0; y < 10; y++) mask[y * 10 + 5] = 255; // Straße in Spalte 5
|
|
expect(nearestWalkable(mask, 10, 10, 3.2, 3.7, 5)).toEqual({ x: 5.5, y: 3.5 });
|
|
});
|
|
|
|
it("findet nichts, wenn im Umkreis nur Wasser ist", () => {
|
|
expect(nearestWalkable(blank(10, 10), 10, 10, 4, 4, 5)).toBeNull();
|
|
});
|
|
|
|
it("zählt einen Treffer genau auf dem Radius, aber nicht knapp dahinter", () => {
|
|
const onRadius = blank(20, 20);
|
|
onRadius[3 * 20 + 8] = 255; // Abstand 5
|
|
expect(nearestWalkable(onRadius, 20, 20, 3, 3, 5)).toEqual({ x: 8.5, y: 3.5 });
|
|
const beyond = blank(20, 20);
|
|
beyond[4 * 20 + 8] = 255; // Abstand √26 ≈ 5,1
|
|
expect(nearestWalkable(beyond, 20, 20, 3, 3, 5)).toBeNull();
|
|
});
|
|
|
|
it("rechnet einen Kartenklick über die Maske in eine Weltposition um", () => {
|
|
const walk = { mask: blank(750, 750), width: 750, height: 750 };
|
|
walk.mask[200 * 750 + 100] = 255; // Maskenpixel (100, 200)
|
|
expect(landingFor(INFO, walk, 400, 800)).toEqual({ x: -600, z: -1700 }); // Treffer: genau dorthin
|
|
const nearby = landingFor(INFO, walk, 412, 800); // 3 Maskenpixel daneben
|
|
expect(nearby.x).toBeCloseTo(-598, 9); // Mitte von (100, 200) = Karte (402, 802)
|
|
expect(nearby.z).toBeCloseTo(-1698, 9);
|
|
expect(landingFor(INFO, walk, 2000, 2000)).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("Chunk-Namen und Anzeige", () => {
|
|
it("nennt am Kartenpunkt des Downtown-Kerns B9", () => {
|
|
expect(chunkNameAtMap(INFO, 489, 2246)).toBe("B9");
|
|
});
|
|
|
|
it("liefert am östlichen Kartenrand keinen Namen mehr", () => {
|
|
expect(chunkNameAtMap(INFO, 3000, 10)).toBeNull();
|
|
});
|
|
|
|
it("zeigt Chunk und Weltkoordinate im Format der Spec", () => {
|
|
expect(hoverLabel(INFO, 489, 2246)).toBe("B9 · x -511 / y 254");
|
|
});
|
|
});
|
|
|
|
describe("Richtung und Tasten", () => {
|
|
it("misst die Richtung ab Norden im Uhrzeigersinn", () => {
|
|
expect(headingFromDirection(0, -1)).toBeCloseTo(0, 9);
|
|
expect(headingFromDirection(1, 0)).toBeCloseTo(Math.PI / 2, 9);
|
|
expect(Math.abs(headingFromDirection(0, 1))).toBeCloseTo(Math.PI, 9);
|
|
expect(headingFromDirection(-1, 0)).toBeCloseTo(-Math.PI / 2, 9);
|
|
});
|
|
|
|
it("schaltet mit M, aber nicht beim Tippen, nicht bei Wiederholung und nicht mit Strg", () => {
|
|
const key = (extra) => ({ code: "KeyM", repeat: false, ctrlKey: false, metaKey: false, altKey: false, ...extra });
|
|
expect(isMapToggleKey(key({}), "BODY")).toBe(true);
|
|
expect(isMapToggleKey(key({}), "INPUT")).toBe(false);
|
|
expect(isMapToggleKey(key({}), "SELECT")).toBe(false);
|
|
expect(isMapToggleKey(key({ repeat: true }), "BODY")).toBe(false);
|
|
expect(isMapToggleKey(key({ ctrlKey: true }), "BODY")).toBe(false);
|
|
expect(isMapToggleKey(key({ code: "KeyN" }), "BODY")).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("Fingerabdruck", () => {
|
|
it("liefert SHA-256 als Hex", async () => {
|
|
expect(await sha256Hex(new TextEncoder().encode("abc"))).toBe(
|
|
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
|
|
});
|
|
|
|
it("liefert null ohne sicheren Kontext, statt zu werfen", async () => {
|
|
expect(await sha256Hex(new TextEncoder().encode("abc"), null)).toBeNull();
|
|
});
|
|
});
|