/** * Task 1.8, the pure half. Runs in Node with no renderer. * * Three laws, each from design §11 / §7.1: * - the width ladder remaps to `NodeKindSpec.measure.width` and never touches * the control-height recipe; * - `capContainerRadius` behaviour at all five radius stops; * - `portSpacing` floors `minHeight` at ports × portSpacing. */ import { capContainerRadius, defaultKnobs, getActiveScaleTokens, setActiveScaleTokens, generateScale, type Knobs, MIN_PRESS_TARGET, panelPaddingPx, resolveKnobs, resolveRadiusForPart, } from "@multiplatform.one/theme"; import { describe, expect, it } from "vitest"; import { defaultNodeMeasure, containerRadiusPx, nodeRadiusTokenPx, FINE_PORT_PITCH, type GraphNodePort, graphNodeMeasure, graphNodeMinHeight, graphNodeRadius, graphNodeRadiusPx, graphNodeWidth, type GraphNodeSizeKnob, portSpacing, verticalPortCrowd, } from "./nodeMeasure"; const sizes = ["small", "medium", "large"] as const satisfies readonly GraphNodeSizeKnob[]; const sizeTokens = { small: "3", medium: "4", large: "5" } as const; const radiusStops = [ "none", "small", "medium", "large", "full", ] as const satisfies readonly Knobs["borderRadius"][]; function port(id: string, side: GraphNodePort["side"]): GraphNodePort { return { id, side, direction: side === "left" ? "in" : "out" }; } describe("width: remapped to the per-kind table, never the control ladder", () => { it("returns the declared measure width unchanged at every size stop", () => { for (const size of sizes) { expect(graphNodeWidth({ size, measure: { width: 240 } })).toBe(240); } }); it("falls back to the 172 / 190 / 216 table when a kind declares no width", () => { expect(graphNodeWidth({ size: "small" })).toBe(172); expect(graphNodeWidth({ size: "medium" })).toBe(190); expect(graphNodeWidth({ size: "large" })).toBe(216); }); it("never lands on a control height — the ladder a container must not ride", () => { const controlHeights = new Set( sizes.map( (size) => resolveKnobs(defaultKnobs, undefined, "light", { sizeToken: sizeTokens[size] }).knobProps .control.height, ), ); expect(controlHeights.size).toBeGreaterThan(0); for (const size of sizes) { expect(controlHeights.has(graphNodeWidth({ size }))).toBe(false); } }); it("steps monotonically, so the size knob still means something", () => { expect(graphNodeWidth({ size: "small" })).toBeLessThan(graphNodeWidth({ size: "medium" })); expect(graphNodeWidth({ size: "medium" })).toBeLessThan(graphNodeWidth({ size: "large" })); }); }); describe("radius: CONTAINER-CAP at all five stops", () => { it("caps at the panelPadding role, the same px capContainerRadius uses", () => { expect(capContainerRadius("full")).toBe(panelPaddingPx()); }); it("paints exactly what capContainerRadius resolves, at every stop", () => { for (const borderRadius of radiusStops) { expect(graphNodeRadius({ borderRadius })).toBe(capContainerRadius(borderRadius)); } }); it("agrees with the DG-RAD doctrine table at every stop", () => { for (const borderRadius of radiusStops) { expect(graphNodeRadiusPx({ borderRadius })).toBe( resolveRadiusForPart({ radiusClass: "CONTAINER-CAP", stop: borderRadius, paddingPx: panelPaddingPx(), }).px, ); } }); it("never exceeds the node's own padding — the cap is what stops a child clip", () => { for (const borderRadius of radiusStops) { expect(graphNodeRadiusPx({ borderRadius })).toBeLessThanOrEqual(panelPaddingPx()); } }); it("caps rather than passes through where the token would overflow the padding", () => { // `full` (44px) exceeds the 16px panelPadding role, so it caps. expect(graphNodeRadius({ borderRadius: "full" })).toBe(panelPaddingPx()); // `medium` (14px) fits inside it and keeps its token. expect(typeof graphNodeRadius({ borderRadius: "medium" })).toBe("string"); expect(graphNodeRadius({ borderRadius: "large" })).toBe(panelPaddingPx()); }); it("is 0 at `none`", () => { expect(graphNodeRadiusPx({ borderRadius: "none" })).toBe(0); }); }); describe("portSpacing floors minHeight at ports × portSpacing", () => { it("is the press floor on coarse pointers and the fine hit diameter otherwise", () => { expect(portSpacing({ coarsePointer: true })).toBe(MIN_PRESS_TARGET); expect(portSpacing({ coarsePointer: false })).toBe(FINE_PORT_PITCH); expect(FINE_PORT_PITCH).toBeLessThan(MIN_PRESS_TARGET); }); it("floors a single-edge node at ports × portSpacing", () => { const ports = [ port("a", "left"), port("b", "left"), port("c", "left"), port("d", "left"), ] satisfies GraphNodePort[]; const portSpacingPx = portSpacing({ coarsePointer: true }); expect(graphNodeMinHeight({ size: "medium", ports, portSpacingPx })).toBe( ports.length * portSpacingPx, ); }); it("counts the busiest EDGE, not the node — opposite sides never collide", () => { const split = [ port("in1", "left"), port("in2", "left"), port("out1", "right"), port("out2", "right"), ] satisfies GraphNodePort[]; expect(verticalPortCrowd(split)).toBe(2); const portSpacingPx = portSpacing({ coarsePointer: true }); expect(graphNodeMinHeight({ size: "medium", ports: split, portSpacingPx })).toBe( 2 * portSpacingPx, ); }); it("ignores top and bottom ports, which stack across the width", () => { const horizontal = [ port("t1", "top"), port("t2", "top"), port("b1", "bottom"), ] satisfies GraphNodePort[]; expect(verticalPortCrowd(horizontal)).toBe(0); expect( graphNodeMinHeight({ size: "medium", ports: horizontal, portSpacingPx: MIN_PRESS_TARGET }), ).toBe(defaultNodeMeasure.medium.minHeight); }); it("keeps the declared minHeight when the ports fit inside it", () => { const ports = [port("only", "left")] satisfies GraphNodePort[]; expect( graphNodeMinHeight({ size: "medium", ports, portSpacingPx: MIN_PRESS_TARGET, measure: { minHeight: 120 }, }), ).toBe(120); }); it("raises a declared minHeight that is too short for its own ports", () => { const ports = [port("a", "left"), port("b", "left"), port("c", "left")]; expect( graphNodeMinHeight({ size: "medium", ports, portSpacingPx: MIN_PRESS_TARGET, measure: { minHeight: 40 }, }), ).toBe(3 * MIN_PRESS_TARGET); }); it("is taller on a coarse pointer than on a fine one for the same ports", () => { const ports = [port("a", "left"), port("b", "left"), port("c", "left")]; const coarse = graphNodeMinHeight({ size: "medium", ports, portSpacingPx: portSpacing({ coarsePointer: true }), }); const fine = graphNodeMinHeight({ size: "medium", ports, portSpacingPx: portSpacing({ coarsePointer: false }), }); expect(coarse).toBeGreaterThan(fine); }); }); describe("graphNodeMeasure seeds both axes before first paint", () => { it("carries the width from the table and the height from the port floor", () => { const ports = [port("a", "left"), port("b", "left")]; expect( graphNodeMeasure({ size: "large", ports, portSpacingPx: MIN_PRESS_TARGET }), ).toStrictEqual({ width: 216, minHeight: 2 * MIN_PRESS_TARGET, }); }); it("lets a declared kind measure win on both axes when it is large enough", () => { expect( graphNodeMeasure({ size: "small", ports: [], portSpacingPx: MIN_PRESS_TARGET, measure: { width: 300, minHeight: 200 }, }), ).toStrictEqual({ width: 300, minHeight: 200 }); }); }); it("container radius tokens read the active scale, including the 8 container key", () => { const previous = getActiveScaleTokens(); try { for (const control of [40, 36, 44]) { const tokens = generateScale({ control, type: 15 }).tokens; setActiveScaleTokens(tokens); for (const key of ["0", "4", "6", "8", "12"] as const) { expect(containerRadiusPx(key)).toBe(tokens.radius[key]); } expect(containerRadiusPx(13)).toBe(13); } expect(() => containerRadiusPx("missing")).toThrow(); expect(() => containerRadiusPx("$8")).toThrow(); expect(nodeRadiusTokenPx["8"]).toBe(20); } finally { setActiveScaleTokens(previous); } });