Cotton code
121 lines · 7.0 KB · SwiftRaw
import CoreGraphicsimport XCTest@testable import CottonUI/// The fan's turn as functions: which tile a release settles on, how far past/// an end a finger can pull it, and which tile a tap lands on. Logic a/// compiler cannot check; no screen is pinned (lane fan, 14 Oct 2026).final class FanGeometryTests: XCTestCase {    /// The shipped paper theme's fan, so the tests follow a retuned theme.    private let fan = FanGeometry(scale: Theme.paper.scale)    func testTheNearestCardIsClampedToTheFan() {        XCTAssertEqual(fan.nearest(-fan.step * 3, count: 10), 0)        XCTAssertEqual(fan.nearest(fan.step * 0.49, count: 10), 0)        XCTAssertEqual(fan.nearest(fan.step * 0.51, count: 10), 1)        XCTAssertEqual(fan.nearest(fan.step * 40, count: 10), 9)        XCTAssertEqual(fan.nearest(fan.step * 4, count: 0), 0)    }    /// RULE: a swipe turns ONE tile — the next one the way it leaned, however far a slow finger dragged — and one that barely leaned goes back; the fan under the finger never runs past the next tile (architect, walker pass 2, 16 Oct 2026).    func testASwipeTurnsOneTile() {        let slow = fan.flingMin / 2        XCTAssertEqual(fan.target(from: 5, releasing: fan.step * 5.4, pointsPerSecond: -slow, count: 30), 6)        XCTAssertEqual(fan.target(from: 5, releasing: fan.step * 4.6, pointsPerSecond: slow, count: 30), 4)        XCTAssertEqual(fan.target(from: 5, releasing: fan.step * 5.05, pointsPerSecond: -slow, count: 30), 5, "a lean under swipe_lean turns nothing")        XCTAssertLessThan(fan.shown(raw: fan.step * 9, swipingFrom: 5, count: 30), fan.step * 6 + fan.overscroll * fan.step)        XCTAssertEqual(fan.target(from: 29, releasing: fan.step * 29.5, pointsPerSecond: -slow, count: 30), 29, "the last tile stays the last")    }    /// RULE: a fling's reach scales with its speed and the fan's length — at `fan_fling_full` it crosses `fan_fling_reach` of the fan, so Tools to Me is two or three flings on any fan (walker pass 2).    func testAFlingCrossesAShareOfTheFan() {        for count in [12, 30] {            let full = fan.target(from: 0, releasing: 0, pointsPerSecond: -fan.flingFull, count: count)            let flings = Int((Double(count - 1) / Double(max(full, 1))).rounded(.up))            XCTAssertTrue((2...3).contains(flings), "\(count) tiles: \(flings) full flings end to end")        }        XCTAssertGreaterThanOrEqual(fan.target(from: 0, releasing: 0, pointsPerSecond: -fan.flingMin, count: 30), 1, "the slowest fling turns at least one")        XCTAssertEqual(fan.target(from: 0, releasing: 0, pointsPerSecond: -fan.flingMin, count: 3), 1, "on a short fan the slowest fling turns one")        XCTAssertEqual(fan.target(from: 20, releasing: fan.step * 20, pointsPerSecond: -fan.flingFull * 100, count: 30), 29, "a fling past the last tile stops on it")        XCTAssertEqual(fan.target(from: 3, releasing: fan.step * 3, pointsPerSecond: fan.flingFull * 100, count: 30), 0)    }    /// RULE: with Reduce Motion the fan never flings — every release is a swipe of one tile (ruling 7).    func testWithoutFlingEveryReleaseIsASwipe() {        XCTAssertEqual(fan.target(from: 2, releasing: fan.step * 2.3, pointsPerSecond: -10_000, count: 30, flings: false), 3)    }    func testInsideTheEndsTheFanFollowsTheFinger() {        XCTAssertEqual(fan.shown(raw: fan.step * 3.3, count: 10), fan.step * 3.3, accuracy: 1e-9)    }    func testPastAnEndTheRubberBandNeverReachesItsReach() {        let reach = fan.overscroll * fan.step        let pulled = fan.shown(raw: -fan.step * 50, count: 10)        XCTAssertLessThan(pulled, 0)        XCTAssertGreaterThan(pulled, -reach)        let end = fan.end(count: 10)        let pushed = fan.shown(raw: end + fan.step * 50, count: 10)        XCTAssertGreaterThan(pushed, end)        XCTAssertLessThan(pushed, end + reach)        // Ever less: a further pull moves it less than the first.        let first = fan.shown(raw: -fan.step, count: 10)        let second = fan.shown(raw: -fan.step * 2, count: 10)        XCTAssertLessThan(abs(second - first), abs(first))    }    func testATapLandsOnTheCardUnderIt() throws {        let width = 390.0        let settled = try XCTUnwrap(fan.placement(index: 4, theta: fan.step * 4, width: width))        XCTAssertEqual(fan.tile(at: settled.centre, theta: fan.step * 4, count: 10, width: width), 4)        let right = try XCTUnwrap(fan.placement(index: 5, theta: fan.step * 4, width: width))        // Out on the neighbour's far side, clear of the settled tile.        let outer = CGPoint(x: right.centre.x + CGFloat(fan.tileWidth) * 0.3, y: right.centre.y)        XCTAssertEqual(fan.tile(at: outer, theta: fan.step * 4, count: 10, width: width), 5)        XCTAssertNil(fan.tile(at: CGPoint(x: width / 2, y: fan.height * 5), theta: fan.step * 4, count: 10, width: width))    }    func testATurnTakesLongerTheFurtherItGoesWithinItsBounds() {        XCTAssertEqual(fan.turnDuration(degrees: 0), fan.turnShortest / 1000, accuracy: 1e-9)        XCTAssertEqual(fan.turnDuration(degrees: 10_000), fan.turnLongest / 1000, accuracy: 1e-9)        let near = fan.turnDuration(degrees: fan.step * 5)        let far = fan.turnDuration(degrees: fan.step * 8)        XCTAssertLessThanOrEqual(near, far)    }    func testTheSpringComesToRestOnItsTarget() {        let spring = FanSpring(scale: Theme.paper.scale)        var theta = 0.0, velocity = 400.0        let target = fan.step * 3        var frames = 0        while !FanSpring.rests(theta: theta, velocity: velocity, target: target), frames < 1200 {            (theta, velocity) = spring.step(theta: theta, velocity: velocity, target: target, dt: 1.0 / 120)            frames += 1        }        XCTAssertLessThan(frames, 1200, "settles within ten seconds of frames")        XCTAssertEqual(theta, target, accuracy: 0.01)    }}extension FanGeometryTests {    func testPeekIsOneTightHandAboveItsCardAndSharesItsTilt() throws {        let fan = FanGeometry(scale: Theme.paper.scale).withHub(diameter: 64)        let front = fan.peekPlacement(index: 0)        let back = fan.peekPlacement(index: 2)        XCTAssertEqual(front.width, fan.tileWidth * 0.90)        XCTAssertEqual(front.height, fan.tileWidth * 0.18)        XCTAssertEqual(back.rise - front.rise, fan.tileWidth * 0.28)        XCTAssertEqual(back.angle, -2)        XCTAssertEqual(back.typeSize, fan.tileWidth * 0.09)        let card = try XCTUnwrap(fan.placement(angle: 0, width: 390))        let header = fan.peekCentre(attachedTo: card)        XCTAssertEqual(header.x, card.centre.x)        XCTAssertEqual(header.y + fan.peekHeight / 2 * card.scale, card.centre.y - fan.tileHeight * card.scale / 2 + fan.tileWidth * 0.02 * card.scale, accuracy: 0.0001)        let tilted = try XCTUnwrap(fan.placement(angle: 12, width: 390))        let attached = fan.peekCentre(attachedTo: tilted)        XCTAssertGreaterThan(attached.x, tilted.centre.x)        XCTAssertLessThan(attached.y, tilted.centre.y)        XCTAssertEqual(fan.hub(width: 390, diameter: 64).y + 32, fan.height)    }}