|
| 1 | +import FlowTile from "./FlowTile" |
| 2 | +import { tileRenderers } from "./tileRenderers" |
| 3 | +import { tileBounds, getEdgePoints, getEdgeMidpoints } from "./geometry" |
| 4 | + |
| 5 | +describe("FlowTile", () => { |
| 6 | + let shape |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + shape = new FlowTile() |
| 10 | + }) |
| 11 | + |
| 12 | + describe("drawOuterBorder", () => { |
| 13 | + it("creates segments for tile perimeter", () => { |
| 14 | + const segments = shape.drawOuterBorder(3, 3) |
| 15 | + |
| 16 | + // Each edge has 2 segments per tile (split at midpoint) |
| 17 | + // 3 tiles per edge * 2 segments * 4 edges = 24 segments |
| 18 | + expect(segments.length).toBe(24) |
| 19 | + }) |
| 20 | + |
| 21 | + it("segments connect at tile midpoints", () => { |
| 22 | + const segments = shape.drawOuterBorder(2, 2) |
| 23 | + const topSegments = segments.slice(0, 4) // First 4 are top edge |
| 24 | + |
| 25 | + // Check midpoint connections |
| 26 | + expect(topSegments[0][1].x).toBe(topSegments[1][0].x) |
| 27 | + expect(topSegments[0][1].y).toBe(topSegments[1][0].y) |
| 28 | + }) |
| 29 | + }) |
| 30 | +}) |
| 31 | + |
| 32 | +describe("tileRenderers", () => { |
| 33 | + const bounds = tileBounds(0, 0, 2) |
| 34 | + |
| 35 | + describe("Arc", () => { |
| 36 | + it("produces 2 arc paths without stroke", () => { |
| 37 | + const paths = tileRenderers.Arc(bounds, 0, 0, false) |
| 38 | + |
| 39 | + expect(paths.length).toBe(2) |
| 40 | + }) |
| 41 | + |
| 42 | + it("produces 4 paths with stroke (inner/outer for each arc)", () => { |
| 43 | + const paths = tileRenderers.Arc(bounds, 0, 0.25, false) |
| 44 | + |
| 45 | + expect(paths.length).toBe(4) |
| 46 | + }) |
| 47 | + |
| 48 | + it("produces 6 paths with border (2 arcs + 4 border segments)", () => { |
| 49 | + const paths = tileRenderers.Arc(bounds, 0, 0, true) |
| 50 | + |
| 51 | + expect(paths.length).toBe(6) |
| 52 | + }) |
| 53 | + |
| 54 | + it("produces different paths for different orientations", () => { |
| 55 | + const paths0 = tileRenderers.Arc(bounds, 0, 0, false) |
| 56 | + const paths1 = tileRenderers.Arc(bounds, 1, 0, false) |
| 57 | + |
| 58 | + // Arc endpoints should differ between orientations |
| 59 | + const start0 = paths0[0][0] |
| 60 | + const start1 = paths1[0][0] |
| 61 | + |
| 62 | + expect(start0.x === start1.x && start0.y === start1.y).toBe(false) |
| 63 | + }) |
| 64 | + }) |
| 65 | + |
| 66 | + describe("Diagonal", () => { |
| 67 | + it("produces 2 line paths without stroke", () => { |
| 68 | + const paths = tileRenderers.Diagonal(bounds, 0, 0, false) |
| 69 | + |
| 70 | + expect(paths.length).toBe(2) |
| 71 | + }) |
| 72 | + |
| 73 | + it("produces 4 paths with stroke", () => { |
| 74 | + const paths = tileRenderers.Diagonal(bounds, 0, 0.3, false) |
| 75 | + |
| 76 | + expect(paths.length).toBe(4) |
| 77 | + }) |
| 78 | + |
| 79 | + it("produces 6 paths with border", () => { |
| 80 | + const paths = tileRenderers.Diagonal(bounds, 0, 0, true) |
| 81 | + |
| 82 | + expect(paths.length).toBe(6) |
| 83 | + }) |
| 84 | + |
| 85 | + it("line paths have 2 points each", () => { |
| 86 | + const paths = tileRenderers.Diagonal(bounds, 0, 0, false) |
| 87 | + |
| 88 | + expect(paths[0].length).toBe(2) |
| 89 | + expect(paths[1].length).toBe(2) |
| 90 | + }) |
| 91 | + }) |
| 92 | +}) |
| 93 | + |
| 94 | +describe("geometry", () => { |
| 95 | + describe("tileBounds", () => { |
| 96 | + it("computes bounds centered on given point", () => { |
| 97 | + const bounds = tileBounds(4, 6, 2) |
| 98 | + |
| 99 | + expect(bounds.cx).toBe(4) |
| 100 | + expect(bounds.cy).toBe(6) |
| 101 | + expect(bounds.left).toBe(3) |
| 102 | + expect(bounds.right).toBe(5) |
| 103 | + expect(bounds.top).toBe(5) |
| 104 | + expect(bounds.bottom).toBe(7) |
| 105 | + }) |
| 106 | + |
| 107 | + it("defaults to size 2", () => { |
| 108 | + const bounds = tileBounds(0, 0) |
| 109 | + |
| 110 | + expect(bounds.size).toBe(2) |
| 111 | + expect(bounds.right - bounds.left).toBe(2) |
| 112 | + }) |
| 113 | + }) |
| 114 | + |
| 115 | + describe("getEdgePoints", () => { |
| 116 | + it("returns midpoints for fraction 0.5", () => { |
| 117 | + const bounds = tileBounds(0, 0, 2) |
| 118 | + const points = getEdgePoints(bounds, [0.5]) |
| 119 | + |
| 120 | + expect(points.left[0].x).toBe(-1) |
| 121 | + expect(points.left[0].y).toBe(0) |
| 122 | + expect(points.right[0].x).toBe(1) |
| 123 | + expect(points.right[0].y).toBe(0) |
| 124 | + expect(points.top[0].x).toBe(0) |
| 125 | + expect(points.top[0].y).toBe(-1) |
| 126 | + expect(points.bottom[0].x).toBe(0) |
| 127 | + expect(points.bottom[0].y).toBe(1) |
| 128 | + }) |
| 129 | + |
| 130 | + it("returns multiple points for multiple fractions", () => { |
| 131 | + const bounds = tileBounds(0, 0, 3) |
| 132 | + const points = getEdgePoints(bounds, [1 / 3, 2 / 3]) |
| 133 | + |
| 134 | + expect(points.left.length).toBe(2) |
| 135 | + expect(points.top.length).toBe(2) |
| 136 | + }) |
| 137 | + }) |
| 138 | + |
| 139 | + describe("getEdgeMidpoints", () => { |
| 140 | + it("returns named midpoint properties", () => { |
| 141 | + const bounds = tileBounds(0, 0, 2) |
| 142 | + const { midLeft, midRight, midTop, midBottom } = getEdgeMidpoints(bounds) |
| 143 | + |
| 144 | + expect(midLeft.x).toBe(-1) |
| 145 | + expect(midLeft.y).toBe(0) |
| 146 | + expect(midRight.x).toBe(1) |
| 147 | + expect(midRight.y).toBe(0) |
| 148 | + expect(midTop.x).toBe(0) |
| 149 | + expect(midTop.y).toBe(-1) |
| 150 | + expect(midBottom.x).toBe(0) |
| 151 | + expect(midBottom.y).toBe(1) |
| 152 | + }) |
| 153 | + }) |
| 154 | +}) |
0 commit comments