| 6dd74de | | | 1 | import { describe, it, expect } from 'vitest'; |
| 6dd74de | | | 2 | import { |
| 6dd74de | | | 3 | intersection, |
| 6dd74de | | | 4 | ensureTrulyOutside, |
| 6dd74de | | | 5 | makeInsidePoint, |
| 6dd74de | | | 6 | tryNodeIntersect, |
| 6dd74de | | | 7 | replaceEndpoint, |
| 6dd74de | | | 8 | type RectLike, |
| 6dd74de | | | 9 | type P, |
| 6dd74de | | | 10 | } from '../geometry.js'; |
| 6dd74de | | | 11 | |
| 6dd74de | | | 12 | const approx = (a: number, b: number, eps = 1e-6) => Math.abs(a - b) < eps; |
| 6dd74de | | | 13 | |
| 6dd74de | | | 14 | describe('geometry helpers', () => { |
| 6dd74de | | | 15 | it('intersection: vertical approach hits bottom border', () => { |
| 6dd74de | | | 16 | const rect: RectLike = { x: 0, y: 0, width: 100, height: 50 }; |
| 6dd74de | | | 17 | const h = rect.height / 2; // 25 |
| 6dd74de | | | 18 | const outside: P = { x: 0, y: 100 }; |
| 6dd74de | | | 19 | const inside: P = { x: 0, y: 0 }; |
| 6dd74de | | | 20 | const res = intersection(rect, outside, inside); |
| 6dd74de | | | 21 | expect(approx(res.x, 0)).toBe(true); |
| 6dd74de | | | 22 | expect(approx(res.y, h)).toBe(true); |
| 6dd74de | | | 23 | }); |
| 6dd74de | | | 24 | |
| 6dd74de | | | 25 | it('ensureTrulyOutside nudges near-boundary point outward', () => { |
| 6dd74de | | | 26 | const rect: RectLike = { x: 0, y: 0, width: 100, height: 50 }; |
| 6dd74de | | | 27 | // near bottom boundary (y ~ h) |
| 6dd74de | | | 28 | const near: P = { x: 0, y: rect.height / 2 - 0.2 }; |
| 6dd74de | | | 29 | const out = ensureTrulyOutside(rect, near, 10); |
| 6dd74de | | | 30 | expect(out.y).toBeGreaterThan(rect.height / 2); |
| 6dd74de | | | 31 | }); |
| 6dd74de | | | 32 | |
| 6dd74de | | | 33 | it('makeInsidePoint keeps x for vertical and y from center', () => { |
| 6dd74de | | | 34 | const rect: RectLike = { x: 10, y: 5, width: 100, height: 50 }; |
| 6dd74de | | | 35 | const outside: P = { x: 10, y: 40 }; |
| 6dd74de | | | 36 | const center: P = { x: 99, y: -123 }; // center y should be used |
| 6dd74de | | | 37 | const inside = makeInsidePoint(rect, outside, center); |
| 6dd74de | | | 38 | expect(inside.x).toBe(outside.x); |
| 6dd74de | | | 39 | expect(inside.y).toBe(center.y); |
| 6dd74de | | | 40 | }); |
| 6dd74de | | | 41 | |
| 6dd74de | | | 42 | it('tryNodeIntersect returns null for wrong-side intersections', () => { |
| 6dd74de | | | 43 | const rect: RectLike = { x: 0, y: 0, width: 100, height: 50 }; |
| 6dd74de | | | 44 | const outside: P = { x: -50, y: 0 }; |
| 6dd74de | | | 45 | const node = { intersect: () => ({ x: 10, y: 0 }) } as any; // right side of center |
| 6dd74de | | | 46 | const res = tryNodeIntersect(node, rect, outside); |
| 6dd74de | | | 47 | expect(res).toBeNull(); |
| 6dd74de | | | 48 | }); |
| 6dd74de | | | 49 | |
| 6dd74de | | | 50 | it('replaceEndpoint dedup removes end/start appropriately', () => { |
| 6dd74de | | | 51 | const pts: P[] = [ |
| 6dd74de | | | 52 | { x: 0, y: 0 }, |
| 6dd74de | | | 53 | { x: 1, y: 1 }, |
| 6dd74de | | | 54 | ]; |
| 6dd74de | | | 55 | // remove duplicate end |
| 6dd74de | | | 56 | replaceEndpoint(pts, 'end', { x: 1, y: 1 }); |
| 6dd74de | | | 57 | expect(pts.length).toBe(1); |
| 6dd74de | | | 58 | |
| 6dd74de | | | 59 | const pts2: P[] = [ |
| 6dd74de | | | 60 | { x: 0, y: 0 }, |
| 6dd74de | | | 61 | { x: 1, y: 1 }, |
| 6dd74de | | | 62 | ]; |
| 6dd74de | | | 63 | // remove duplicate start |
| 6dd74de | | | 64 | replaceEndpoint(pts2, 'start', { x: 0, y: 0 }); |
| 6dd74de | | | 65 | expect(pts2.length).toBe(1); |
| 6dd74de | | | 66 | }); |
| 6dd74de | | | 67 | }); |