From 9ec99ed03e03830a1c2e5fe9e5f31267d7ec7f5e Mon Sep 17 00:00:00 2001 From: que01 Date: Wed, 9 Sep 2026 11:39:03 +0800 Subject: [PATCH] test(junction): enforce core boundary invariants --- test/junction-build-plan-counterexamples.js | 65 +++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/test/junction-build-plan-counterexamples.js b/test/junction-build-plan-counterexamples.js index df29bbe..0200d4d 100644 --- a/test/junction-build-plan-counterexamples.js +++ b/test/junction-build-plan-counterexamples.js @@ -229,3 +229,68 @@ assert.deepEqual(rotatedTopology.arms.map((arm) => arm.key).sort(), sixArm.topol assert.equal(rotatedTopology.arms.length, sixArm.topology.arms.length); console.log('junction build-plan counterexample tests passed'); + +function ringArea(ring) { + return Math.abs( + ring + .slice(1) + .reduce((area, coordinate, index) => area + ring[index][0] * coordinate[1] - coordinate[0] * ring[index][1], 0) / + 2, + ); +} + +function ringHasSelfIntersection(ring) { + const cross = (a, b, c) => (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]); + const intersects = (a, b, c, d) => { + const abC = cross(a, b, c); + const abD = cross(a, b, d); + const cdA = cross(c, d, a); + const cdB = cross(c, d, b); + return ((abC > 0 && abD < 0) || (abC < 0 && abD > 0)) && ((cdA > 0 && cdB < 0) || (cdA < 0 && cdB > 0)); + }; + for (let first = 0; first < ring.length - 1; first += 1) { + for (let second = first + 2; second < ring.length - 1; second += 1) { + if (first === 0 && second === ring.length - 2) continue; + if (intersects(ring[first], ring[first + 1], ring[second], ring[second + 1])) return true; + } + } + return false; +} + +function assertCoreGeometry(topologyValue, graphValue, centerValue) { + const closure = buildCoreClosure(topologyValue, graphValue, centerValue); + assert.equal(closure.status, 'supported'); + assert.deepEqual(closure.ring[0], closure.ring.at(-1), 'core ring must be explicitly closed'); + assert.ok(closure.ring.slice(0, -1).every((coordinate) => coordinate.every(Number.isFinite))); + assert.ok(ringArea(closure.ring) > 0, 'core ring must have positive area'); + assert.equal(ringHasSelfIntersection(closure.ring), false, 'core ring must not self-intersect'); + for (const section of graphValue.sections) { + const samePoint = (first, second) => first?.[0] === second?.[0] && first?.[1] === second?.[1]; + const hasSeam = closure.ring.slice(0, -1).some((coordinate, index) => { + const next = closure.ring[index + 1]; + return ( + (samePoint(coordinate, section.inner.left) && samePoint(next, section.inner.right)) || + (samePoint(coordinate, section.inner.right) && samePoint(next, section.inner.left)) + ); + }); + assert.equal(hasSeam, true, `inner seam must remain a contiguous ring edge for ${section.carriagewayKey}`); + } +} + +assertCoreGeometry(tee.topology, teeBoundaryGraph, point(0, 0)); +assertCoreGeometry(sixArm.topology, sixArmGraph, point(0, 0)); + +const fiveArm = topology( + 'five-arm', + [0, 72, 144, 216, 288].map((angle) => ({ + widthMeters: 5, + line: [point(0, 0), point(Math.sin((angle * Math.PI) / 180) * 45, Math.cos((angle * Math.PI) / 180) * 45)], + })), +); +const fiveArmGraph = buildJunctionBoundaryGraph( + fiveArm.topology, + point(0, 0), + { outerRadiusFor: () => 30, innerRadiusFor: () => 10 }, + fiveArm.helpers, +); +assertCoreGeometry(fiveArm.topology, fiveArmGraph, point(0, 0));