Files
Homelab-manager/server/test/mapLines.test.ts
T
bobbanandClaude Sonnet 5.5 2986734196 Map: a Lines setting for curved or right-angled connections
Both map views can draw their lines as right-angled runs with rounded corners instead of curves; the choice is remembered per browser.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-14 01:54:22 +01:00

80 lines
4.3 KiB
TypeScript

/**
* How the maps draw the lines between their boxes: smooth curves (the original look) or right-angled lines — the path each makes, that an unknown stored choice is the original look, and that both maps
* draw through the one function and offer the choice.
*
* Run with: npm test --workspace server
*/
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { CORNER_RADIUS, edgePath, MAP_LINE_STYLES, parseMapLineStyle } from "../../web/src/utils/mapLines.js";
const web = (...p: string[]) => readFileSync(new URL(`../../web/${p.join("/")}`, import.meta.url), "utf8");
describe("the path of a line", () => {
test("curved is the S it always was: out from the box, through the middle, into the next", () => {
assert.equal(edgePath(100, 20, 300, 120, "curved"), "M100,20 C200,20 200,120 300,120");
assert.equal(edgePath(100, 20, 300, 20, "curved"), "M100,20 C200,20 200,20 300,20", "level boxes: the same, flat");
});
test("right-angled goes out to the middle, up or down, and on in — with the corners rounded", () => {
const down = edgePath(100, 20, 300, 120, "right-angled");
assert.equal(down, `M100,20 H${200 - CORNER_RADIUS} Q200,20 200,${20 + CORNER_RADIUS} V${120 - CORNER_RADIUS} Q200,120 ${200 + CORNER_RADIUS},120 H300`);
const up = edgePath(100, 120, 300, 20, "right-angled");
assert.equal(up, `M100,120 H${200 - CORNER_RADIUS} Q200,120 200,${120 - CORNER_RADIUS} V${20 + CORNER_RADIUS} Q200,20 ${200 + CORNER_RADIUS},20 H300`);
});
test("every segment is horizontal or vertical: no diagonal, so the lines meet the boxes at right angles", () => {
for (const [x1, y1, x2, y2] of [[0, 0, 400, 300], [0, 300, 400, 0], [10, 50, 260, 70], [10, 50, 30, 52]] as const) {
const d = edgePath(x1, y1, x2, y2, "right-angled");
assert.doesNotMatch(d, /[LC]/, d);
assert.match(d, /^M[\d.]+,[\d.]+( [HVQ][\d.,\- ]+)+$/);
}
});
test("a line between boxes at the same height is one straight line", () => {
assert.equal(edgePath(100, 40, 300, 40, "right-angled"), "M100,40 H300");
assert.equal(edgePath(100, 40, 300, 40.3, "right-angled"), "M100,40 H300", "a third of a pixel is level");
});
test("the corners are no rounder than the room they have: small steps and short runs get hard corners, or none", () => {
// 6 px step: a radius of 3 at the most
assert.match(edgePath(100, 20, 300, 26, "right-angled"), /H197 Q200,20 200,23 V23 Q200,26 203,26 H300/);
// two boxes nearly touching: no room for a curve at all
assert.equal(edgePath(100, 20, 103, 90, "right-angled"), "M100,20 H101.5 V90 H103");
});
test("the line ends where it is told to start and finish", () => {
for (const style of ["curved", "right-angled"] as const) {
const d = edgePath(12.5, 33, 211.25, 90.75, style);
assert.ok(d.startsWith("M12.5,33"), d);
assert.ok(d.endsWith("211.3,90.8") || d.endsWith("H211.3") || d.endsWith(" 211.3,90.8"), d);
}
});
});
describe("the choice", () => {
test("two looks; the original is the one when nothing, or something unknown, is stored", () => {
assert.deepEqual(MAP_LINE_STYLES.map((s) => s.value), ["curved", "right-angled"]);
assert.equal(parseMapLineStyle("right-angled"), "right-angled");
for (const odd of [null, undefined, "", "curved", "straight", "RIGHT-ANGLED"]) assert.equal(parseMapLineStyle(odd), "curved", String(odd));
});
test("both maps draw their lines through the one function, and both offer the choice", () => {
for (const file of [["src", "pages", "Map.tsx"], ["src", "components", "DependencyMap.tsx"]]) {
const source = web(...file);
assert.match(source, /edgePath\(x1, y1, x2, y2, lineStyle\)/, file.join("/"));
assert.match(source, /<MapLinesToggle \/>/, file.join("/"));
assert.match(source, /useMapLineStyle\(\)/, file.join("/"));
assert.doesNotMatch(source, /C\$\{mid\}/, "no curve written out by hand any more");
}
});
test("it is remembered in this browser, under one key, and the original look removes it", () => {
const toggle = web("src", "components", "MapLinesToggle.tsx");
assert.match(toggle, /localStorage\.removeItem\(MAP_LINES_KEY\)/);
assert.match(toggle, /localStorage\.setItem\(MAP_LINES_KEY, next\)/);
assert.match(toggle, /aria-pressed=\{style === s\.value\}/);
});
});