Cotton code
149 lines · 6.6 KB · SwiftRaw
import CottonFacesimport CottonSpecimensimport SwiftUI#if canImport(AppKit)import AppKit#endifimport XCTest@MainActorfinal class FaceTurnTests: XCTestCase {    func testPositionFollowsScrubWithoutCommittingSelection() {        let motion = FaceTurnMotion(automatic: false)        motion.scrub(90, velocity: 0, bounds: -10...20)        XCTAssertEqual(motion.index, 2)        XCTAssertEqual(motion.position(from: 10, count: 31), 12)        motion.scrub(-90, velocity: 0, bounds: -10...20)        XCTAssertEqual(motion.position(from: 10, count: 31), 8)        motion.scrub(-9999, velocity: 0, bounds: -10...20)        XCTAssertEqual(motion.position(from: 10, count: 31), 0)        motion.scrub(9999, velocity: 0, bounds: -10...20)        XCTAssertEqual(motion.position(from: 10, count: 31), 30)    }    func testTravelFollowsFingerInBothDirectionsAndClamps() {        XCTAssertEqual(FaceTurnMotion.travel(-130, width: 400), 0.5)        XCTAssertEqual(FaceTurnMotion.travel(130, width: 400), -0.5)        XCTAssertEqual(FaceTurnMotion.travel(-900, width: 400), 1)        XCTAssertEqual(FaceTurnMotion.travel(900, width: 400), -1)    }    func testShortPullFallsBackAndFlickCrossesTheTop() {        XCTAssertFalse(FaceTurnMotion.crossesTop(0.3, predicted: 0.4))        XCTAssertTrue(FaceTurnMotion.crossesTop(0.3, predicted: 0.8))        XCTAssertTrue(FaceTurnMotion.crossesTop(-0.3, predicted: -0.8))        XCTAssertFalse(FaceTurnMotion.crossesTop(0.3, predicted: -0.8))        XCTAssertTrue(FaceTurnMotion.crossesTop(0.6, predicted: 0.4))        XCTAssertFalse(FaceTurnMotion.crossesTop(0, predicted: 0.8))    }    func testCancellationClearsTravelWithoutCommitting() {        var time = 0.0        let motion = FaceTurnMotion(clock: { time }, automatic: false)        var commits: [Int] = []        motion.tap(1, available: true, bounds: -5...5) { commits.append($0) }        time = 0.1; motion.advance()        XCTAssertGreaterThan(motion.progress, 0)        motion.cancel()        XCTAssertEqual(motion.progress, 0)        XCTAssertFalse(motion.settling)        time = 1; motion.advance()        XCTAssertTrue(commits.isEmpty)    }    func testATapTurnsOneCardAndCommitsOnceAtRest() {        var time = 0.0        let motion = FaceTurnMotion(clock: { time }, automatic: false)        var commits: [Int] = []        motion.tap(-1, available: true, bounds: -5...5) { commits.append($0) }        time = 0.2; motion.advance()        XCTAssertLessThan(motion.progress, 0)        XCTAssertTrue(commits.isEmpty, "the selection waits for the detent")        time = 0.4; motion.advance()        XCTAssertEqual(commits, [-1])        XCTAssertEqual(motion.progress, 0)        XCTAssertFalse(motion.settling)        time = 1; motion.advance()        XCTAssertEqual(commits, [-1])    }    /// RULE (owner, 29 Oct 2026): keep tapping and the records fly by. A    /// second tap while the first is still settling moves a second card; it    /// used to be dropped, and the touch that made it cancelled the first.    func testTapsWhileSettlingEachMoveACard() {        var time = 0.0        let motion = FaceTurnMotion(clock: { time }, automatic: false)        var commits: [Int] = []        motion.tap(1, available: true, bounds: -5...5) { commits.append($0) }        time = 0.1; motion.advance()        XCTAssertFalse(motion.coasting, "a settling tap is not a coast a touch catches")        motion.tap(1, available: true, bounds: -5...5) { commits.append($0) }        time = 0.2; motion.advance()        motion.tap(1, available: false, bounds: -5...5) { commits.append($0) }        time = 1; motion.advance()        XCTAssertEqual(commits, [3])        XCTAssertFalse(motion.settling)    }    func testTapsStopAtTheDeckEndAndAtRestAnUnavailableNeighbourStepsAtOnce() {        var time = 0.0        let motion = FaceTurnMotion(clock: { time }, automatic: false)        var commits: [Int] = []        motion.tap(1, available: false, bounds: 0...0) { commits.append($0) }        XCTAssertEqual(commits, [1], "past the end at rest, the caller's own step (the roll)")        XCTAssertFalse(motion.settling)        commits = []        motion.tap(1, available: true, bounds: -2...1) { commits.append($0) }        motion.tap(1, available: true, bounds: -2...1) { commits.append($0) }        motion.tap(1, available: true, bounds: -2...1) { commits.append($0) }        time = 1; motion.advance()        XCTAssertEqual(commits, [1])    }    func testATapAfterCatchingACoastTurnsFromTheCaughtCard() {        var time = 0.0        let motion = FaceTurnMotion(clock: { time }, automatic: false)        var commits: [Int] = []        motion.spinRelease(velocity: 30, bounds: -50...50) { commits.append($0) }        time = 0.2; motion.advance()        XCTAssertTrue(motion.coasting)        motion.catchWheel()        let caught = Int(motion.progress)        motion.tap(1, available: true, bounds: -50...50) { commits.append($0) }        time = 1; motion.advance()        XCTAssertEqual(commits, [caught + 1])    }    func testWheelPhasesDrawBothDirections() throws {        let face = RolodexFaceCatalogue.face        let leaving = face.specimens[0]        let arriving = face.specimens[1]        func drawn(_ progress: Double) throws -> CGImage {            let view = SpecimenCard(face, leaving)                .environment(\.faceTurn, FaceTurn(progress: progress, window: [-1: arriving.card, 1: arriving.card]))                .frame(width: 340, height: 451)            let renderer = ImageRenderer(content: view)            renderer.scale = 3            return try XCTUnwrap(renderer.cgImage)        }        let still = try XCTUnwrap(try drawn(0).dataProvider?.data as Data?)        for progress in [-0.75, -0.5, -0.25, -0.1, 0.1, 0.25, 0.5, 0.75] {            let image = try drawn(progress)            XCTAssertNotEqual(image.dataProvider?.data as Data?, still, "phase \(progress) draws the turn")            #if canImport(AppKit)            if let path = ProcessInfo.processInfo.environment["COTTON_TURN_SHOTS"] {                let directory = URL(fileURLWithPath: path)                try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)                let data = try XCTUnwrap(NSBitmapImageRep(cgImage: image).representation(using: .png, properties: [:]))                try data.write(to: directory.appendingPathComponent("turn-\(progress).png"))            }            #endif        }    }    func testOnlyRolodexesDeclareTurns() {        XCTAssertEqual(FaceRegistry.all.filter { $0.steps == .turns }.map(\.id).sorted(), ["event.rolodex", "person.rolodex"])    }}