import Foundation import HelderCore import Testing @Suite struct SchemaGeometryTests { private func p(_ x: Double, _ y: Double) -> SchemaPoint { SchemaPoint(x: x, y: y) } @Test func cardSizesMatchTheCard() { #expect(SchemaGeometry.cardHeight(columns: 0) == 35) #expect(SchemaGeometry.cardHeight(columns: 3) == 101) #expect(SchemaGeometry.columnCentreY(top: 100, index: 0) == 145) #expect(SchemaGeometry.columnCentreY(top: 100, index: 2) == 189) } @Test func aTargetToTheRightLeavesTheRightEdgeAndEntersTheLeft() { let path = SchemaGeometry.edgePath(from: p(40, 40), columnIndex: 1, to: p(380, 40), targetColumnIndex: 0) #expect(path == SchemaEdgePath(start: p(280, 107), control1: p(330, 107), control2: p(330, 85), end: p(380, 85))) } @Test func aTargetToTheLeftIsTheMirror() { let path = SchemaGeometry.edgePath(from: p(380, 40), columnIndex: 0, to: p(40, 100), targetColumnIndex: 0) #expect(path == SchemaEdgePath(start: p(380, 85), control1: p(330, 85), control2: p(330, 145), end: p(280, 145))) } @Test func aNearTargetStillCurvesAtLeastFortyPoints() { let path = SchemaGeometry.edgePath(from: p(0, 0), columnIndex: 0, to: p(250, 0), targetColumnIndex: 0) #expect(path.control1.x == 280) #expect(path.control2.x == 210) } @Test func theSameLaneLoopsOutOnTheRight() { let path = SchemaGeometry.edgePath(from: p(40, 40), columnIndex: 2, to: p(100, 300), targetColumnIndex: 0) let loop = 46 + 216 * 0.12 #expect(path == SchemaEdgePath(start: p(280, 129), control1: p(280 + loop, 129), control2: p(340 + loop, 345), end: p(340, 345))) } @Test func centresExactlyOneCardApartAreNotTheSameLane() { let path = SchemaGeometry.edgePath(from: p(0, 0), columnIndex: 0, to: p(240, 0), targetColumnIndex: 0) #expect(path.end.x == 240) } @Test func anEdgeWithAnUnplacedTableHasNoPath() { let edge = SchemaFixtures.edge("a", "b") #expect(SchemaGeometry.edgePath(edge, positions: ["a": p(0, 0)]) == nil) #expect(SchemaGeometry.edgePath(edge, positions: ["a": p(0, 0), "b": p(400, 0)])?.start == p(240, 67)) } @Test func boundsHoldEveryPlacedCard() { let model = SchemaFixtures.model(["a", "b", "c"], edges: []) let bounds = SchemaGeometry.bounds(model, positions: ["a": p(40, 40), "b": p(400, 10)]) #expect(bounds == SchemaRect(x: 40, y: 10, width: 600, height: 109)) #expect(SchemaGeometry.bounds(model, positions: [:]) == nil) } @Test func fitCentresAndCapsTheScale() { let view = SchemaGeometry.fit(bounds: SchemaRect(x: 40, y: 40, width: 240, height: 200), in: SchemaSize(width: 1000, height: 800)) #expect(view == SchemaViewport(x: 291, y: 246, scale: 1.1)) } @Test func fitNeverGoesBelowThirtyPercent() { let view = SchemaGeometry.fit(bounds: SchemaRect(x: 0, y: 0, width: 5000, height: 5000), in: SchemaSize(width: 200, height: 200)) #expect(view.scale == 0.3) } @Test func fitShrinksToTheTighterSide() { let view = SchemaGeometry.fit(bounds: SchemaRect(x: 0, y: 0, width: 1940, height: 400), in: SchemaSize(width: 1096, height: 800)) #expect(view.scale == 0.5) #expect(view.x == 48) } @Test func zoomKeepsThePointUnderTheAnchor() { let view = SchemaViewport(x: 30, y: -20, scale: 0.8) let anchor = p(300, 200) let before = SchemaGeometry.canvasPoint(anchor, in: view) let zoomed = SchemaGeometry.zoom(view, by: 1.2, around: anchor) let after = SchemaGeometry.canvasPoint(anchor, in: zoomed) #expect(abs(zoomed.scale - 0.96) < 1e-9) #expect(abs(before.x - after.x) < 1e-9) #expect(abs(before.y - after.y) < 1e-9) } @Test func zoomIsClampedBetweenThirtyAndTwoHundredPercent() { let view = SchemaViewport(x: 0, y: 0, scale: 1) #expect(SchemaGeometry.zoom(view, by: 10, around: p(0, 0)).scale == 2) #expect(SchemaGeometry.zoom(view, by: 0.01, around: p(0, 0)).scale == 0.3) #expect(SchemaGeometry.zoom(view, by: 2, in: SchemaSize(width: 200, height: 200)) == SchemaViewport(x: -100, y: -100, scale: 2)) #expect(SchemaViewport(scale: 0.96).percentLabel == "96%") } @Test func centreKeepsTheScaleAndRounds() { let table = SchemaFixtures.table("a") let view = SchemaGeometry.centre(table, at: p(100, 200), in: SchemaSize(width: 800, height: 600), view: SchemaViewport(x: 5, y: 5, scale: 1)) #expect(view == SchemaViewport(x: 180, y: 61, scale: 1)) } }