/** * FZ-9208: no edge is drawn through a node box. * * The graph is NodeGraph → Main as the live Storybook measures it * (`demoDocumentLarge(12)`: a 280 x 200 grid, Source and Sink 128 tall, * Transform 176, Sink 240 wide). Two of its edges run backward, from the last * column of one row to the first column of the next, and before this card one * cut diagonally through two nodes while the other rode the next row's top * border. * * The sampler here is the spec's own, independent of the router's, so the * router cannot pass by agreeing with itself. */ import { createHash } from "node:crypto"; import { describe, expect, it } from "vitest"; import { Position } from "../react-flow/xy"; import { buildEdgePath } from "./edgePath"; import { routeAroundBoxes, routeOnTrack, samplePath } from "./edgeRoute"; import type { EdgeSide } from "./edgeRoute"; import type { EdgePathKind } from "./edgeStyle"; interface Box { readonly id: string; readonly x: number; readonly y: number; readonly width: number; readonly height: number; } interface Point { readonly x: number; readonly y: number; } const clearance = 8; const paths: readonly EdgePathKind[] = ["bezier", "smoothstep", "step", "straight"]; const kinds = ["source", "transform", "sink"] as const; const size = { source: { width: 190, height: 128 }, transform: { width: 190, height: 176 }, sink: { width: 240, height: 128 }, } as const; /** Port centre below the node's top edge, as measured: the first port row, then the second. */ const firstPortY = 99; const secondPortY = 123; const boxes: readonly Box[] = Array.from({ length: 12 }, (_, index) => { const kind = kinds[index % kinds.length]; return { id: `n${index}`, x: (index % 4) * 280, y: Math.floor(index / 4) * 200, ...size[kind], }; }); interface MainEdge { readonly id: string; readonly source: Box; readonly target: Box; readonly sourcePortY: number; } const mainEdges: readonly MainEdge[] = boxes.flatMap((from, index) => { const to = boxes[index + 1]; const kind = kinds[index % kinds.length]; if (!to || kind === "sink") return []; return [ { id: `${from.id}->${to.id}`, source: from, target: to, sourcePortY: kind === "transform" ? secondPortY : firstPortY, }, ]; }); function numbersAndCommands(d: string): (string | number)[] { const tokens = d.match(/[MLQCZ]|-?(?:\d+\.?\d*|\.\d+)(?:e[-+]?\d+)?/gi) ?? []; return tokens.map((token) => (/^[MLQCZ]$/i.test(token) ? token.toUpperCase() : Number(token))); } /** Absolute M, L, Q and C, which is every command React Flow and the router write. */ function sample(d: string): Point[] { const tokens = numbersAndCommands(d); const points: Point[] = []; let at: Point = { x: 0, y: 0 }; let command = "M"; let index = 0; const take = (): number => { const value = tokens[index]; index += 1; if (typeof value !== "number") throw new Error(`expected a number in ${d}`); return value; }; while (index < tokens.length) { if (typeof tokens[index] === "string") { command = tokens[index] as string; index += 1; if (command === "Z") continue; } if (command === "M") { at = { x: take(), y: take() }; points.push(at); command = "L"; } else if (command === "L") { const to = { x: take(), y: take() }; const steps = Math.max(1, Math.ceil(Math.hypot(to.x - at.x, to.y - at.y))); for (let step = 1; step <= steps; step += 1) { const t = step / steps; points.push({ x: at.x + (to.x - at.x) * t, y: at.y + (to.y - at.y) * t }); } at = to; } else if (command === "Q") { const control = { x: take(), y: take() }; const to = { x: take(), y: take() }; for (let step = 1; step <= 32; step += 1) { const t = step / 32; const u = 1 - t; points.push({ x: u * u * at.x + 2 * u * t * control.x + t * t * to.x, y: u * u * at.y + 2 * u * t * control.y + t * t * to.y, }); } at = to; } else if (command === "C") { const first = { x: take(), y: take() }; const second = { x: take(), y: take() }; const to = { x: take(), y: take() }; for (let step = 1; step <= 256; step += 1) { const t = step / 256; const u = 1 - t; points.push({ x: u * u * u * at.x + 3 * u * u * t * first.x + 3 * u * t * t * second.x + t * t * t * to.x, y: u * u * u * at.y + 3 * u * u * t * first.y + 3 * u * t * t * second.y + t * t * t * to.y, }); } at = to; } else { throw new Error(`unsupported command ${command} in ${d}`); } } return points; } function distanceToBox(point: Point, box: Box): number { const dx = Math.max(box.x - point.x, 0, point.x - (box.x + box.width)); const dy = Math.max(box.y - point.y, 0, point.y - (box.y + box.height)); return Math.hypot(dx, dy); } interface Endpoints { readonly from: Point; readonly to: Point; } /** * `inset` is how far inside its frame the port's edge point sits: 0 once ports * sit on the frame edge (FZ-9012), the frame padding before that. The router * has to hold for both. */ function endpoints(edge: MainEdge, inset: number): Endpoints { return { from: { x: edge.source.x + edge.source.width - inset, y: edge.source.y + edge.sourcePortY }, to: { x: edge.target.x + inset, y: edge.target.y + firstPortY }, }; } function route(edge: MainEdge, path: EdgePathKind, inset: number): { d: string; ends: Endpoints } { const ends = endpoints(edge, inset); const [d] = buildEdgePath( path, { sourceX: ends.from.x, sourceY: ends.from.y, targetX: ends.to.x, targetY: ends.to.y, sourcePosition: Position.Right, targetPosition: Position.Left, }, { boxes, clearance }, ); return { d, ends }; } /** Each sample closer than the clearance to a node box, outside the port stubs at the ends. */ function intrusions(d: string, ends: Endpoints, inset: number): string[] { const stub = inset + clearance; const found: string[] = []; for (const point of sample(d)) { if (Math.hypot(point.x - ends.from.x, point.y - ends.from.y) <= stub) continue; if (Math.hypot(point.x - ends.to.x, point.y - ends.to.y) <= stub) continue; for (const box of boxes) { const distance = distanceToBox(point, box); if (distance < clearance - 0.5) { found.push( `${box.id} at (${point.x.toFixed(1)}, ${point.y.toFixed(1)}), ${distance.toFixed(1)} away`, ); } } } return found; } describe("edges keep out of node boxes (FZ-9208)", () => { for (const inset of [0, 12]) { for (const path of paths) { it.each(mainEdges.map((edge) => [edge.id, edge] as const))( `%s, ${path}, port inset ${inset}: no sample comes within the clearance of a node box`, (_id, edge) => { const { d, ends } = route(edge, path, inset); const hits = intrusions(d, ends, inset); expect(hits.slice(0, 3), `${hits.length} samples intrude; d=${d}`).toEqual([]); }, ); } } it.each(paths)("routes the backward n7->n8 %s edge around the boxes, not across them", (path) => { const edge = mainEdges.find((candidate) => candidate.id === "n7->n8"); if (!edge) throw new Error("Main has no n7->n8 edge"); const { d, ends } = route(edge, path, 0); expect(intrusions(d, ends, 0)).toEqual([]); const points = sample(d); // Around means through the gap between the rows: the path's lowest run // above row 2 sits below row 1's tallest box. const between = points.filter((point) => point.x > 240 + clearance && point.x < 840); expect(between.length).toBeGreaterThan(0); for (const point of between) { expect(point.y).toBeGreaterThanOrEqual(200 + 176 + clearance - 0.5); expect(point.y).toBeLessThanOrEqual(400 - clearance + 0.5); } }); /** Every turn in `d`: a Q bend with its radius, or a sharp L-to-L corner as radius 0. */ function turns(d: string): number[] { const tokens = numbersAndCommands(d); const segments: { command: string; points: Point[] }[] = []; let index = 0; let command = "M"; while (index < tokens.length) { if (typeof tokens[index] === "string") { command = tokens[index] as string; index += 1; } const count = command === "Q" ? 2 : command === "C" ? 3 : 1; const points: Point[] = []; for (let point = 0; point < count; point += 1) { points.push({ x: tokens[index] as number, y: tokens[index + 1] as number }); index += 2; } segments.push({ command, points }); } const found: number[] = []; for (let at = 1; at < segments.length; at += 1) { const segment = segments[at]; const start = segments[at - 1].points[segments[at - 1].points.length - 1]; if (segment.command === "Q") { const [control] = segment.points; found.push(Math.hypot(control.x - start.x, control.y - start.y)); continue; } const next = segments[at + 1]; if (segment.command !== "L" || next?.command !== "L") continue; const corner = segment.points[0]; const after = next.points[0]; const cross = (corner.x - start.x) * (after.y - corner.y) - (corner.y - start.y) * (after.x - corner.x); if (Math.abs(cross) > 1e-6) found.push(0); } return found; } const rounded: readonly ["bezier" | "smoothstep", number][] = [ ["bezier", 24], ["smoothstep", 5], ]; for (const inset of [0, 12]) { it.each(rounded)( `bends a rerouted %s edge round at every turn, port inset ${inset}: radius at least min(%d, corridor / 2)`, (path, radius) => { // The tightest corridor in Main: the 24-unit row gap less the clearance on both sides. const corridor = 200 - 176 - clearance * 2; const least = Math.min(radius, corridor / 2); for (const id of ["n3->n4", "n7->n8"]) { const edge = mainEdges.find((candidate) => candidate.id === id); if (!edge) throw new Error(`Main has no ${id} edge`); const { d, ends } = route(edge, path, inset); expect(intrusions(d, ends, inset)).toEqual([]); const bends = turns(d); expect(bends.length, `${id} d=${d}`).toBeGreaterThan(1); for (const bend of bends) expect(bend, `${id} d=${d}`).toBeGreaterThanOrEqual(least - 0.01); } }, ); } it.each(paths)("keeps the %s path's ends on the ports, so the arrowhead still lands", (path) => { for (const edge of mainEdges) { const { d, ends } = route(edge, path, 0); const points = sample(d); const first = points[0]; const last = points[points.length - 1]; expect(Math.hypot(first.x - ends.from.x, first.y - ends.from.y)).toBeLessThan(0.5); expect(Math.hypot(last.x - ends.to.x, last.y - ends.to.y)).toBeLessThan(0.5); } }); it("leaves an edge with a clear run alone: n0->n1 smoothstep is React Flow's own path", () => { const edge = mainEdges[0]; const ends = endpoints(edge, 0); const geometry = { sourceX: ends.from.x, sourceY: ends.from.y, targetX: ends.to.x, targetY: ends.to.y, sourcePosition: Position.Right, targetPosition: Position.Left, }; expect(buildEdgePath("smoothstep", geometry, { boxes, clearance })[0]).toBe( buildEdgePath("smoothstep", geometry)[0], ); }); }); describe("edgePath purity (FZ-8149)", () => { it("draws edges without evaluating @xyflow/react or the React Flow store", async () => { const { readFileSync } = await import("node:fs"); const { fileURLToPath } = await import("node:url"); const read = (rel: string) => readFileSync(fileURLToPath(new URL(rel, import.meta.url)), "utf8"); const imports = (src: string) => [...src.matchAll(/from "([^"]+)"/g)].map((m) => m[1]); expect(imports(read("./edgePath.ts"))).toEqual([ "../react-flow/xyPaths", "../react-flow/xyPaths", "./edgeRoute", "./edgeRoute", "./edgeStyle", ]); expect(imports(read("../react-flow/xyPaths.ts"))).toEqual(["@xyflow/system"]); }); }); /** * FZ-9291: edges that cross the same gap between two ranks used to take the * gap's middle line, all of them, so on Ogden's apps graph eight edges into * two targets read as one bus. Same-target edges now share a trunk and every * other edge in the gap gets its own track. */ describe("edges sharing a rank gap get their own tracks (FZ-9291)", () => { const width = 96; const height = 72; const sources: readonly Box[] = Array.from({ length: 5 }, (_, index) => ({ id: `s${index}`, x: index * 112, y: 0, width, height, })); const targets: readonly Box[] = [ { id: "tA", x: 56, y: 113, width, height }, { id: "tB", x: 336, y: 113, width, height }, ]; const rows = [...sources, ...targets]; const links = [ { source: "s0", target: "tA" }, { source: "s1", target: "tA" }, { source: "s3", target: "tA" }, { source: "s2", target: "tB" }, { source: "s4", target: "tB" }, ]; /** `across` lays the ranks left to right instead of top to bottom by swapping x and y. */ function layout(across: boolean) { const turn = (box: Box): Box => across ? { id: box.id, x: box.y, y: box.x, width: box.height, height: box.width } : box; const placed = rows.map(turn); const at = new Map(placed.map((box) => [box.id, box])); const draw = (path: EdgePathKind, withLinks: boolean) => links.map((link) => { const source = at.get(link.source)!; const target = at.get(link.target)!; const from = across ? { x: source.x + source.width, y: source.y + source.height / 2 } : { x: source.x + source.width / 2, y: source.y + source.height }; const to = across ? { x: target.x, y: target.y + target.height / 2 } : { x: target.x + target.width / 2, y: target.y }; const [d] = buildEdgePath( path, { sourceX: from.x, sourceY: from.y, targetX: to.x, targetY: to.y, sourcePosition: across ? Position.Right : Position.Bottom, targetPosition: across ? Position.Left : Position.Top, }, withLinks ? { boxes: placed, clearance, links } : { boxes: placed, clearance }, ); return { link, d, from, to }; }); return { placed, draw }; } interface Run { readonly at: number; readonly from: number; readonly to: number; } /** The straight runs across the gap: perpendicular to the rank direction, longer than 2 units. */ function runs(d: string, across: boolean): Run[] { const points = sample(d).map((point) => (across ? { x: point.y, y: point.x } : point)); const found: Run[] = []; let start = 0; for (let index = 1; index <= points.length; index += 1) { const flat = index < points.length && Math.abs(points[index].y - points[start].y) < 0.01; if (flat) continue; const end = index - 1; if (Math.abs(points[end].x - points[start].x) > 2) { found.push({ at: points[start].y, from: Math.min(points[start].x, points[end].x), to: Math.max(points[start].x, points[end].x), }); } start = index; } return found; } function shared(a: readonly Run[], b: readonly Run[]): Run[] { return a.filter((run) => b.some( (other) => Math.abs(run.at - other.at) < 2 && Math.min(run.to, other.to) - Math.max(run.from, other.from) > 1, ), ); } for (const across of [false, true]) { const direction = across ? "LR" : "TB"; for (const path of ["smoothstep", "step"] as const) { it(`${direction} ${path}: two edges into different targets never draw the same run`, () => { const drawn = layout(across).draw(path, true); for (const a of drawn) { for (const b of drawn) { if (a.link.target === b.link.target) continue; const overlap = shared(runs(a.d, across), runs(b.d, across)); expect( overlap, `${a.link.source}->${a.link.target} and ${b.link.source}->${b.link.target}`, ).toEqual([]); } } }); it(`${direction} ${path}: edges into one target merge into a trunk that ends at that target`, () => { const drawn = layout(across).draw(path, true); for (const target of ["tA", "tB"]) { const into = drawn.filter((edge) => edge.link.target === target); const levels = new Set( into.flatMap((edge) => runs(edge.d, across).map((run) => Math.round(run.at * 100))), ); expect(levels.size, target).toBe(1); for (const edge of into) { const points = sample(edge.d); const last = points[points.length - 1]; expect(Math.hypot(last.x - edge.to.x, last.y - edge.to.y)).toBeLessThan(0.5); } } }); it(`${direction} ${path}: every track keeps the clearance from every box`, () => { const { placed, draw } = layout(across); for (const edge of draw(path, true)) { const stub = clearance; for (const point of sample(edge.d)) { if (Math.hypot(point.x - edge.from.x, point.y - edge.from.y) <= stub) continue; if (Math.hypot(point.x - edge.to.x, point.y - edge.to.y) <= stub) continue; for (const box of placed) expect(distanceToBox(point, box), `${edge.link.source}->${box.id}`).toBeGreaterThan( clearance - 0.5, ); } } }); } } it("an edge that skips a rank doesn't cost the gap its tracks", () => { const far: Box = { id: "far", x: 448, y: -113, width, height }; const placed = [...rows, far]; const withSkip = [...links, { source: "far", target: "tA" }]; const level = (source: string, target: string) => { const from = placed.find((box) => box.id === source)!; const to = placed.find((box) => box.id === target)!; const [d] = buildEdgePath( "smoothstep", { sourceX: from.x + width / 2, sourceY: from.y + height, targetX: to.x + width / 2, targetY: to.y, sourcePosition: Position.Bottom, targetPosition: Position.Top, }, { boxes: placed, clearance, links: withSkip }, ); return runs(d, false).map((run) => Math.round(run.at * 100)); }; expect(level("s0", "tA")).not.toEqual(level("s2", "tB")); expect(level("s1", "tA")).toEqual(level("s3", "tA")); }); it("an edge that skips a rank passes the rank between boxes and joins its target's trunk", () => { const far: Box = { id: "far", x: 448, y: -113, width, height }; const placed = [...rows, far]; const withSkip = [...links, { source: "far", target: "tA" }]; const draw = (source: string, target: string) => { const from = placed.find((box) => box.id === source)!; const to = placed.find((box) => box.id === target)!; const ends = { from: { x: from.x + width / 2, y: from.y + height }, to: { x: to.x + width / 2, y: to.y }, }; const [d] = buildEdgePath( "smoothstep", { sourceX: ends.from.x, sourceY: ends.from.y, targetX: ends.to.x, targetY: ends.to.y, sourcePosition: Position.Bottom, targetPosition: Position.Top, }, { boxes: placed, clearance, links: withSkip }, ); return { d, ...ends }; }; const skip = draw("far", "tA"); for (const point of sample(skip.d)) { if (Math.hypot(point.x - skip.from.x, point.y - skip.from.y) <= clearance) continue; if (Math.hypot(point.x - skip.to.x, point.y - skip.to.y) <= clearance) continue; for (const box of placed) expect(distanceToBox(point, box), box.id).toBeGreaterThan(clearance - 0.5); } const last = runs(skip.d, false).at(-1)!; const trunk = runs(draw("s0", "tA").d, false).at(-1)!; expect(Math.round(last.at * 100)).toBe(Math.round(trunk.at * 100)); }); it("an edge that skips a rank holds one line through the channel between that rank's boxes", () => { const top: Box = { id: "top", x: 0, y: 0, width, height }; const left: Box = { id: "left", x: 0, y: 113, width, height }; const right: Box = { id: "right", x: 224, y: 113, width, height }; const bottom: Box = { id: "bottom", x: 112, y: 226, width, height }; const placed = [top, left, right, bottom]; const chain = [ { source: "top", target: "left" }, { source: "top", target: "right" }, { source: "left", target: "bottom" }, { source: "right", target: "bottom" }, { source: "top", target: "bottom" }, ]; for (const path of ["smoothstep", "step"] as const) { const [d] = buildEdgePath( path, { sourceX: 48, sourceY: 72, targetX: 160, targetY: 226, sourcePosition: Position.Bottom, targetPosition: Position.Top, }, { boxes: placed, clearance, links: chain }, ); const through = sample(d).filter((point) => point.y >= 113 && point.y <= 185); expect(through.length, path).toBeGreaterThan(0); for (const point of through) expect(Math.abs(point.x - 160), path).toBeLessThan(0.5); expect(runs(d, false), path).toHaveLength(1); } }); it("without the edge list every edge keeps its own path, as before", () => { const { draw } = layout(false); const plain = draw("smoothstep", false); for (const edge of plain) { const alone = buildEdgePath("smoothstep", { sourceX: edge.from.x, sourceY: edge.from.y, targetX: edge.to.x, targetY: edge.to.y, sourcePosition: Position.Bottom, targetPosition: Position.Top, }); expect(edge.d).toBe(alone[0]); } }); it("Main's chain keeps every path when the edge list rides along: no two of its edges share a gap", () => { const mainLinks = mainEdges.map((edge) => ({ source: edge.source.id, target: edge.target.id })); for (const path of paths) { for (const edge of mainEdges) { const ends = endpoints(edge, 0); const geometry = { sourceX: ends.from.x, sourceY: ends.from.y, targetX: ends.to.x, targetY: ends.to.y, sourcePosition: Position.Right, targetPosition: Position.Left, }; expect(buildEdgePath(path, geometry, { boxes, clearance, links: mainLinks })[0]).toBe( buildEdgePath(path, geometry, { boxes, clearance })[0], ); } } }); }); describe("routeOnTrack characterization (Sonar S3776 planTracks split)", () => { const local: readonly Box[] = [ { id: "a0", x: -400, y: 0, width: 60, height: 40 }, { id: "a1", x: -330, y: 0, width: 60, height: 40 }, { id: "a2", x: 0, y: 0, width: 60, height: 40 }, { id: "a3", x: 100, y: 0, width: 60, height: 40 }, { id: "a4", x: 200, y: 0, width: 60, height: 40 }, { id: "a5", x: 300, y: 0, width: 60, height: 40 }, { id: "a6", x: 500, y: 0, width: 60, height: 40 }, { id: "a7", x: 570, y: 0, width: 60, height: 40 }, { id: "b0", x: -200, y: 80, width: 60, height: 40 }, { id: "b1", x: 0, y: 90, width: 60, height: 30 }, { id: "b2", x: 280, y: 80, width: 60, height: 40 }, { id: "b3", x: 380, y: 85, width: 40, height: 40 }, { id: "c0", x: -380, y: 170, width: 60, height: 40 }, { id: "c1", x: 120, y: 170, width: 60, height: 40 }, { id: "c2", x: 160, y: 175, width: 60, height: 40 }, { id: "c3", x: 560, y: 170, width: 60, height: 40 }, { id: "d0", x: 120, y: 220, width: 60, height: 40 }, { id: "d1", x: 400, y: 222, width: 60, height: 40 }, { id: "e0", x: 1100, y: 0, width: 40, height: 40 }, { id: "e1", x: 1300, y: 0, width: 40, height: 40 }, { id: "h0", x: 1500, y: 0, width: 40, height: 40 }, { id: "h1", x: 1560, y: 0, width: 40, height: 40 }, { id: "f0", x: 1000, y: 80, width: 40, height: 40 }, { id: "f1", x: 1400, y: 80, width: 40, height: 40 }, { id: "g0", x: 1100, y: 170, width: 40, height: 40 }, { id: "g1", x: 1300, y: 170, width: 40, height: 40 }, { id: "g2", x: 1520, y: 170, width: 40, height: 40 }, { id: "g3", x: 1400, y: 170, width: 40, height: 40 }, ]; const links = [ { source: "a0", target: "c0" }, { source: "a1", target: "c0" }, { source: "a2", target: "c1" }, { source: "a3", target: "c1" }, { source: "a4", target: "c2" }, { source: "a3", target: "c2" }, { source: "a5", target: "c3" }, { source: "a6", target: "c3" }, { source: "a7", target: "c3" }, { source: "a2", target: "b1" }, { source: "a3", target: "b1" }, { source: "a5", target: "b2" }, { source: "b2", target: "c3" }, { source: "b3", target: "c3" }, { source: "a4", target: "d0" }, { source: "c1", target: "d0" }, { source: "c3", target: "d1" }, { source: "a2", target: "c1" }, { source: "c0", target: "a0" }, { source: "b0", target: "b1" }, { source: "a2", target: "ghost" }, { source: "ghost", target: "c1" }, { source: "e0", target: "g0" }, { source: "e1", target: "g1" }, { source: "h0", target: "g2" }, { source: "h1", target: "g2" }, { source: "f1", target: "g3" }, ]; const toWorld: Record Point> = { bottom: ({ x, y }) => ({ x, y }), top: ({ x, y }) => ({ x, y: -y }), right: ({ x, y }) => ({ x: y, y: x }), left: ({ x, y }) => ({ x: -y, y: x }), }; const facing: Record = { bottom: "top", top: "bottom", right: "left", left: "right", }; function placeAll(side: EdgeSide): Box[] { return local.map((box) => { const a = toWorld[side]({ x: box.x, y: box.y }); const b = toWorld[side]({ x: box.x + box.width, y: box.y + box.height }); return { id: box.id, x: Math.min(a.x, b.x), y: Math.min(a.y, b.y), width: Math.abs(a.x - b.x), height: Math.abs(a.y - b.y), }; }); } function routesFor(side: EdgeSide): string[] { const placed = placeAll(side); const byId = new Map(local.map((box) => [box.id, box])); const out: string[] = []; for (const path of ["step", "smoothstep"] as const) { for (const shift of [0, 3, -5]) { for (const link of links) { const source = byId.get(link.source); const target = byId.get(link.target); if (!source || !target) continue; const from = toWorld[side]({ x: source.x + source.width / 2 + shift, y: source.y + source.height, }); const to = toWorld[side]({ x: target.x + target.width / 2 - shift, y: target.y }); const result = routeOnTrack( path, { sourceX: from.x, sourceY: from.y, targetX: to.x, targetY: to.y, sourceSide: side, targetSide: facing[side], }, { boxes: placed, clearance, links }, ); out.push( `${path} ${shift} ${link.source}->${link.target}: ${ result ? `${result[0]} @ ${result[1]},${result[2]}` : "null" }`, ); } } } return out; } const recorded: readonly [EdgeSide, number, string][] = [ ["bottom", 112, "bfb31d3cbbf75bd981bba5e41cd547efde48c3399572445904a55eccd51140cb"], ["top", 112, "5e83c4f862f1cb517b4b14f4572ece910c8351c94403dc45e6b71beb97ade226"], ["right", 112, "e54a2cd3691ab3a11ba8ac60bf1f6b23534e86525eb80dc1373a63f64e59080e"], ["left", 112, "34c34cab9b3b4e36c1818b99780f57df157e0a8f38789dc92c9bd045be6ce153"], ]; it.each(recorded)("%s: %d links route on tracks, exactly as recorded", (side, routed, digest) => { const routes = routesFor(side); expect({ side, routed: routes.filter((line) => !line.endsWith(": null")).length, digest: createHash("sha256").update(routes.join("\n")).digest("hex"), }).toEqual({ side, routed, digest }); }); }); describe("routeAroundBoxes and samplePath characterization (Sonar S3776 splits, FZ-10868)", () => { const field: readonly Box[] = [ { id: "p0", x: 0, y: 0, width: 120, height: 80 }, { id: "p1", x: 200, y: 0, width: 120, height: 80 }, { id: "p2", x: 400, y: 30, width: 90, height: 120 }, { id: "p3", x: 60, y: 160, width: 180, height: 70 }, { id: "p4", x: 300, y: 200, width: 100, height: 100 }, { id: "p5", x: -150, y: 100, width: 100, height: 160 }, { id: "p6", x: 140, y: 300, width: 60, height: 40 }, { id: "p7", x: 152, y: 252, width: 30, height: 30 }, { id: "p8", x: 500, y: 260, width: 80, height: 20 }, { id: "w0", x: 700, y: 0, width: 200, height: 20 }, { id: "w1", x: 700, y: 180, width: 200, height: 20 }, { id: "w2", x: 700, y: 20, width: 20, height: 160 }, { id: "w3", x: 880, y: 20, width: 20, height: 160 }, { id: "w4", x: 770, y: 70, width: 60, height: 60 }, ]; const sidePairs: readonly [EdgeSide, EdgeSide][] = [ ["right", "left"], ["left", "right"], ["bottom", "top"], ["top", "bottom"], ["right", "right"], ["bottom", "left"], ]; const portOn: Record Point> = { right: (box) => ({ x: box.x + box.width, y: box.y + box.height / 2 }), left: (box) => ({ x: box.x, y: box.y + box.height / 2 }), top: (box) => ({ x: box.x + box.width / 2, y: box.y }), bottom: (box) => ({ x: box.x + box.width / 2, y: box.y + box.height }), }; function routesAround(gap: number, sourceIds: readonly string[]): string[] { const out: string[] = []; for (const source of field.filter((box) => sourceIds.includes(box.id))) { for (const target of field) { if (source === target) continue; for (const [sourceSide, targetSide] of sidePairs) { const from = portOn[sourceSide](source); const to = portOn[targetSide](target); const result = routeAroundBoxes( "smoothstep", { sourceX: from.x, sourceY: from.y, targetX: to.x, targetY: to.y, sourceSide, targetSide, }, { boxes: field, clearance: gap }, ); const drawn = result ? result.join(" @ ") : "null"; out.push(`${source.id}.${sourceSide}>${target.id}.${targetSide}: ${drawn}`); } } } return out; } const recordedRoutes: readonly [number, string[], number, string][] = [ [ 8, ["p0", "p3", "p7", "w4"], 192, "7bfb34d8c765d7aa381f8a9e38c81c206775c851370a7c69c5610f4341b20d6e", ], [14, ["p2", "p5"], 116, "24dae2b6b12ea3f44ddcde8c005099ed9411862cf5aebc4570c860cb8bcabe01"], ]; it.each(recordedRoutes)( "clearance %d from %j: %d routes, exactly as recorded", (gap, sourceIds, routed, digest) => { const routes = routesAround(gap, sourceIds); expect({ gap, routed: routes.filter((line) => !line.endsWith(": null")).length, digest: createHash("sha256").update(routes.join("\n")).digest("hex"), }).toEqual({ gap, routed, digest }); }, ); const drawnPaths = [ "M0 0 L10 0 L10 25", "M 5,5 Q 20,40 60,5", "M0 0C 10 30, 40 30, 50 0", "M0 0 C0 100 200 100 200 0 L 300 -40", "m1 2 l3 4 q5 6 7 8 c9 10 11 12 13 14 z", "M0 0 L10 10 Z 5 5", "M1e1 2.5e-1 L.5 -3.25", "10 20 30 40", "M0 0 L 5", "M0 0 Q 1 1", "", "M0 0 Z M 10 10 L 20 20 Z L 40 0", "M0 0 H 10 V 10", "M0 0 L0 0 Q0 0 0 0 C0 0 0 0 0 0", ]; it("samplePath reads every command form exactly as recorded", () => { const sampled = drawnPaths.map((d) => JSON.stringify(samplePath(d))); expect(createHash("sha256").update(sampled.join("\n")).digest("hex")).toBe( "dd8aec08d401c68d3bbd937db6d5086c24fcd59d078a5add13d7c2b7d61d9138", ); }); }); /** * FZ-11137: the path tokenizer's number pattern was simplified for Sonar * S5843. The pattern it replaced is kept here, so every form React Flow or an * SVG path can carry is read into the same token array as before, and * `samplePath` turns those tokens into the same geometry. */ describe("the path tokenizer reads every number form as the old pattern did (Sonar S5843, FZ-11137)", () => { function oldTokens(d: string): (string | number)[] { const raw = d.match(/[MLQCZ]|-?(?:\d+\.?\d*|\.\d+)(?:e[-+]?\d+)?/gi) ?? []; return raw.map((token) => (/^[MLQCZ]$/i.test(token) ? token.toUpperCase() : Number(token))); } function newTokens(d: string): (string | number)[] { const out: (string | number)[] = []; for (const part of d.split(/([MLQCZ])/i)) { if (/^[MLQCZ]$/i.test(part)) { out.push(part.toUpperCase()); continue; } for (const value of part.match(/-?(?=\.?\d)\d*\.?\d*(?:e[-+]?\d+)?/gi) ?? []) out.push(Number(value)); } return out; } const spread = [ "M0 0 L10 0 L10 25", "m0 0 l10 20 q5 6 7 8 c9 10 11 12 13 14 z", "M0 0L10-5L20-10", "M.5 .25 L-.5 -.25", "M3. 4. L5.5 6.", "M1e1 2.5e-1 L1E2 -3.5E+1 L2e+2 -4e-3", "M3.e2 2 L5.e-3 1", "M1.2.3.4 0 L.5.5 1", "M0,0 L 10 , 20", "M0 0 H10 V10 S1 2 3 4 A5 5 0 0 1 10 10 Z", "M0 0 Z M10 10 L20 20 Z L40 0", "qQcCzZlLmM", "M- . -. e5 +5 .e1", "M0 0 L 5", "", ]; it.each(spread)("%j reads into the same tokens", (d) => { expect(newTokens(d)).toEqual(oldTokens(d)); }); it("samplePath draws the same points from every one", () => { const sampled = spread.map((d) => JSON.stringify(samplePath(d))); expect(createHash("sha256").update(sampled.join("\n")).digest("hex")).toBe( "1d07afeeedd845d9b4ae1aafaad92418fd573812830ebb7d1a8daae65eabe6f7", ); }); });