1
0
Fork 0
polaris-task-force/tests/int/map-grid.int.spec.ts
Z8MB1E 12cb62b487 feat(map): add Arma-style coordinate grid config, overlay and mission-map wiring
Per-map grid config (gridEnabled, easting/northing origin, spacing, label
digits, over-lines) rides on the Maps collection and surfaces via
/api/map-features. Pure math in src/lib/map/grid.ts mirrors CfgWorlds Grid
(floor truncation, mod-10^digits wrapping, nested levels). GridLayer renders
hairlines + edge labels on the world map and mission planning maps, and
armaGridRef now delegates to gridRef. Migration 20261005_001212_add_map_grid_config.
2026-10-05 03:17:52 -04:00

260 lines
12 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { armaGridRef } from "@/lib/arma-markers/missionMarkers";
import {
DEFAULT_MAP_GRID,
GRID_MIN_CELL_PX,
gridConfigFromMapDoc,
gridLabel,
gridLevelFor,
gridLinesForBounds,
gridRef,
} from "@/lib/map/grid";
const WORLD = 8192;
const FULL_WORLD = { minX: 0, minY: 0, maxX: WORLD, maxY: WORLD };
describe("gridLabel", () => {
it("zero-pads to the digit width", () => {
expect(gridLabel(7, 3)).toBe("007");
expect(gridLabel(0, 2)).toBe("00");
expect(gridLabel(42, 4)).toBe("0042");
});
it("wraps at the digit width", () => {
expect(gridLabel(1234, 3)).toBe("234");
expect(gridLabel(100, 2)).toBe("00");
});
it("wraps negatives like Arma", () => {
expect(gridLabel(-1, 3)).toBe("999");
expect(gridLabel(-25, 2)).toBe("75");
});
it("truncates fractional input", () => {
expect(gridLabel(12.9, 2)).toBe("12");
expect(gridLabel(-1.5, 2)).toBe("99");
});
});
describe("gridLevelFor", () => {
it("walks the A3 ladder with the default config", () => {
expect(gridLevelFor(8, DEFAULT_MAP_GRID)).toEqual({ spacing: 1000, digits: 2 });
expect(gridLevelFor(0.5, DEFAULT_MAP_GRID)).toEqual({ spacing: 100, digits: 3 });
expect(gridLevelFor(0.0625, DEFAULT_MAP_GRID)).toEqual({ spacing: 10, digits: 4 });
// Far out: nothing finer reads, the coarsest level wins.
expect(gridLevelFor(64, DEFAULT_MAP_GRID)).toEqual({ spacing: 10000, digits: 1 });
});
it("keeps the coarsest level when zoomed out past every threshold", () => {
expect(gridLevelFor(1000, DEFAULT_MAP_GRID)).toEqual({ spacing: 10000, digits: 1 });
});
it("chooses a level exactly at GRID_MIN_CELL_PX and never a finer one below it", () => {
expect(GRID_MIN_CELL_PX).toBe(56);
// 5600 m base cells: at 100 m/px the base cell is exactly 56 px on screen.
const config = gridConfigFromMapDoc({ gridEnabled: true, gridSpacingMeters: 5600 });
expect(gridLevelFor(100, config)).toEqual({ spacing: 5600, digits: 2 });
// A hair further out the base cell drops under the threshold; the finer
// levels are smaller still, so the ladder falls back to the coarse level.
expect(gridLevelFor(101, config)).toEqual({ spacing: 56000, digits: 1 });
// Same crossing on the default ladder between mpp 1.7 and 2 (100 m cell
// sits at 58.8 px, then 50 px).
expect(gridLevelFor(1.7, DEFAULT_MAP_GRID)).toEqual({ spacing: 100, digits: 3 });
expect(gridLevelFor(2, DEFAULT_MAP_GRID)).toEqual({ spacing: 1000, digits: 2 });
});
});
describe("gridRef", () => {
it("reproduces the legacy armaGridRef output with DEFAULT_MAP_GRID", () => {
const point: [number, number] = [4567, WORLD - 1234];
expect(gridRef(point, WORLD, DEFAULT_MAP_GRID, 6)).toBe("045 012");
expect(gridRef(point, WORLD, DEFAULT_MAP_GRID, 8)).toBe("0456 0123");
// Default arguments land on the same 6-digit reference.
expect(gridRef(point, WORLD)).toBe("045 012");
});
it("armaGridRef still returns the legacy strings (it delegates to gridRef)", () => {
expect(armaGridRef([4567, WORLD - 1234], WORLD)).toBe("045 012");
expect(armaGridRef([4567, WORLD - 1234], WORLD, 8)).toBe("0456 0123");
});
it("uses 100 m cells at 6 digits and 10 m cells at 8", () => {
const point: [number, number] = [4567, WORLD - 1234];
const [e6, n6] = gridRef(point, WORLD, DEFAULT_MAP_GRID, 6).split(" ");
const [e8, n8] = gridRef(point, WORLD, DEFAULT_MAP_GRID, 8).split(" ");
// The 6-digit groups are the truncation of the 8-digit groups.
expect(e8.startsWith(e6)).toBe(true);
expect(n8.startsWith(n6)).toBe(true);
// 100 m granularity at 6 digits: 4599 stays, 4600 rolls over.
expect(gridRef([4599, WORLD - 1234], WORLD, DEFAULT_MAP_GRID, 6)).toBe("045 012");
expect(gridRef([4600, WORLD - 1234], WORLD, DEFAULT_MAP_GRID, 6)).toBe("046 012");
// 10 m granularity at 8 digits: 4569 stays, 4570 rolls over.
expect(gridRef([4569, WORLD - 1234], WORLD, DEFAULT_MAP_GRID, 8)).toBe("0456 0123");
expect(gridRef([4570, WORLD - 1234], WORLD, DEFAULT_MAP_GRID, 8)).toBe("0457 0123");
});
it("shifts the zero line with gridOriginEasting/gridOriginNorthing", () => {
const config = gridConfigFromMapDoc({
gridEnabled: true,
gridOriginEasting: 2000,
gridOriginNorthing: 3000,
});
// A point exactly on the configured origin reads zero at every precision.
const atOrigin: [number, number] = [2000, WORLD - 3000];
expect(gridRef(atOrigin, WORLD, config, 6)).toBe("000 000");
expect(gridRef(atOrigin, WORLD, config, 8)).toBe("0000 0000");
// The vanilla default would read raw meters at the same spot.
expect(gridRef(atOrigin, WORLD, DEFAULT_MAP_GRID, 6)).toBe("020 030");
});
it("reads the configured starting numbers on the origin line", () => {
const config = gridConfigFromMapDoc({
gridEnabled: true,
gridOriginEasting: 2000,
gridOriginNorthing: 3000,
gridStartEasting: 5,
gridStartNorthing: 7,
});
// Start numbers are defined at the base spacing and scale into finer
// levels: 5 at 1 km reads "050" at 100 m.
expect(gridRef([2000, WORLD - 3000], WORLD, config, 6)).toBe("050 070");
});
it("scales starting numbers across levels and keeps the nesting mod rule", () => {
const config = gridConfigFromMapDoc({ gridEnabled: true, gridStartEasting: 100 });
// Base level (1 km, 2 digits): the first cell east of the zero line adds
// the start value, then wraps to the digit width.
const set = gridLinesForBounds(config, WORLD, FULL_WORLD, 8);
expect(set.level).toEqual({ spacing: 1000, digits: 2 });
const baseLabel = set.eastingLabels.find((l) => l.at === 500);
expect(baseLabel?.value).toBe("00"); // 0 + 100, wrapped to 2 digits
// 100 m level (6-digit ref) at the zero line: 0 + 100*10 = 1000 -> "000".
const [easting6] = gridRef([0, WORLD - 1234], WORLD, config, 6).split(" ");
expect(easting6).toBe("000");
// Nesting: the finer label's trailing digits match the base label.
expect(easting6.slice(-2)).toBe(baseLabel?.value);
// 10 m level (8-digit ref): 0 + 100*100 = 10000 -> "0000".
expect(gridRef([0, WORLD - 1234], WORLD, config, 8).split(" ")[0]).toBe("0000");
});
it("wraps labels west of an origin that sits east of the point", () => {
const config = gridConfigFromMapDoc({ gridEnabled: true, gridOriginEasting: 5000 });
// x = 1000 lies 4000 m west of the zero line: -40 cells at 100 m.
expect(gridRef([1000, WORLD - 1000], WORLD, config, 6)).toBe("960 010");
// Base level: the cell holding x = 1000 is index -4, wrapped to "96".
const set = gridLinesForBounds(
config,
WORLD,
{ minX: 0, minY: 0, maxX: 2000, maxY: 2000 },
8,
);
expect(set.eastingLabels.find((l) => l.at === 1500)?.value).toBe("96");
});
});
describe("gridLinesForBounds", () => {
it("covers the viewport plus one padded cell of lines at the 1 km level", () => {
const set = gridLinesForBounds(DEFAULT_MAP_GRID, WORLD, FULL_WORLD, 8);
expect(set.level).toEqual({ spacing: 1000, digits: 2 });
// Lines span one padded cell past each edge; the east/south side ceils to
// the next line because 8192 is not cell-aligned.
expect(set.verticalLines).toEqual([
-1000, 0, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000,
]);
expect(set.horizontalLines).toEqual([
9192, 8192, 7192, 6192, 5192, 4192, 3192, 2192, 1192, 192, -808, -1808,
]);
});
it("centers easting labels in their cells by default", () => {
const set = gridLinesForBounds(DEFAULT_MAP_GRID, WORLD, FULL_WORLD, 8);
expect(set.eastingLabels).toHaveLength(10);
expect(set.eastingLabels[1]).toEqual({ value: "00", at: 500 });
expect(set.eastingLabels[2]).toEqual({ value: "01", at: 1500 });
// One padded cell west of the zero line wraps to "99".
expect(set.eastingLabels[0]).toEqual({ value: "99", at: -500 });
});
it("places northing labels in map space (y south) with the Arma y-flip", () => {
const set = gridLinesForBounds(DEFAULT_MAP_GRID, WORLD, FULL_WORLD, 8);
// Arma northing cell 0 centers 500 m above the south edge.
expect(set.northingLabels.find((l) => l.value === "00")?.at).toBe(WORLD - 500);
// Cell 7 centers at 7500 m Arma-north: map y = 8192 - 7500 = 692.
const seven = set.northingLabels.find((l) => l.value === "07");
expect(seven?.at).toBe(WORLD - 7500);
expect(seven?.at).toBe(692);
});
it("puts labels on the lines when numbersOverLines is set", () => {
const config = gridConfigFromMapDoc({ gridEnabled: true, gridNumbersOverLines: true });
const set = gridLinesForBounds(config, WORLD, FULL_WORLD, 8);
expect(set.eastingLabels.find((l) => l.at === 0)?.value).toBe("00");
expect(set.eastingLabels.find((l) => l.at === 1000)?.value).toBe("01");
expect(set.northingLabels.find((l) => l.at === WORLD)?.value).toBe("00");
// Every anchor is a line position, not a cell center.
expect(set.eastingLabels.every((l) => set.verticalLines.includes(l.at))).toBe(true);
expect(set.northingLabels.every((l) => set.horizontalLines.includes(l.at))).toBe(true);
});
it("extends lines and labels past the world edge, counting and wrapping", () => {
const set = gridLinesForBounds(DEFAULT_MAP_GRID, WORLD, FULL_WORLD, 8);
// Lines run past the east and south edges.
expect(set.verticalLines).toContain(9000);
expect(set.verticalLines).toContain(10000);
expect(set.horizontalLines).toContain(-808);
// Values keep counting past the east edge (cell 8 centered at 8500).
expect(set.eastingLabels.find((l) => l.at === 8500)?.value).toBe("08");
// Past the north edge (map y negative): northing cell 8 at y = -308.
expect(set.northingLabels.find((l) => l.at === -308)?.value).toBe("08");
// West of the zero line and south of the world the values wrap.
expect(set.eastingLabels.find((l) => l.at === -500)?.value).toBe("99");
expect(set.northingLabels.find((l) => l.at === WORLD + 500)?.value).toBe("99");
});
});
describe("gridConfigFromMapDoc", () => {
it("returns DEFAULT_MAP_GRID for null, undefined and empty docs", () => {
expect(gridConfigFromMapDoc(null)).toEqual(DEFAULT_MAP_GRID);
expect(gridConfigFromMapDoc(undefined)).toEqual(DEFAULT_MAP_GRID);
expect(gridConfigFromMapDoc({})).toEqual(DEFAULT_MAP_GRID);
});
it("tolerates null fields and falls back to defaults", () => {
const config = gridConfigFromMapDoc({
gridEnabled: null,
gridOriginEasting: null,
gridOriginNorthing: null,
gridStartEasting: null,
gridStartNorthing: null,
gridSpacingMeters: null,
gridLabelDigits: null,
gridNumbersOverLines: null,
});
expect(config).toEqual(DEFAULT_MAP_GRID);
});
it("falls back to 1000 m when gridSpacingMeters is 0 or missing", () => {
expect(gridConfigFromMapDoc({ gridSpacingMeters: 0 }).spacingMeters).toBe(1000);
expect(gridConfigFromMapDoc({}).spacingMeters).toBe(1000);
});
it("clamps gridLabelDigits to [1, 6]", () => {
expect(gridConfigFromMapDoc({ gridLabelDigits: 0 }).labelDigits).toBe(1);
expect(gridConfigFromMapDoc({ gridLabelDigits: -4 }).labelDigits).toBe(1);
expect(gridConfigFromMapDoc({ gridLabelDigits: 9 }).labelDigits).toBe(6);
expect(gridConfigFromMapDoc({ gridLabelDigits: 3 }).labelDigits).toBe(3);
});
it("truncates fractional starting numbers", () => {
expect(gridConfigFromMapDoc({ gridStartEasting: 3.9 }).startEasting).toBe(3);
expect(gridConfigFromMapDoc({ gridStartNorthing: -2.7 }).startNorthing).toBe(-2);
});
it("reads the boolean flags strictly", () => {
expect(gridConfigFromMapDoc({ gridEnabled: true }).enabled).toBe(true);
expect(gridConfigFromMapDoc({ gridEnabled: 1 }).enabled).toBe(false);
expect(gridConfigFromMapDoc({ gridNumbersOverLines: true }).numbersOverLines).toBe(true);
expect(gridConfigFromMapDoc({ gridNumbersOverLines: "yes" }).numbersOverLines).toBe(false);
});
});