Cotton code
97 lines · 4.1 KB · SwiftRaw
import XCTest@testable import CottonUI@MainActor final class FanTiersTests: XCTestCase {    #if os(iOS)    func testTwelveCardsUseTheToolsWindowAndRiffleToTheTwelfth() {        let fan = FanGeometry(scale: Theme.paper.scale).withHub(diameter: 64)        let motion = FanMotion(geometry: fan, spring: .init(response: 0.3, damping: 0.8))        motion.recount(12)        motion.turn(to: 5, animated: false)        let window = (0..<12).filter { fan.placement(index: $0, theta: motion.theta, width: 393) != nil }        XCTAssertEqual(window, [3, 4, 5, 6, 7])        let tools = (0..<24).filter { fan.placement(index: $0, theta: motion.theta, width: 393) != nil }        XCTAssertEqual(window.count, tools.count)        for current in 5..<11 {            let next = fan.target(from: current, releasing: Double(current + 1) * fan.step, pointsPerSecond: 0, count: 12)            motion.turn(to: next, animated: false)        }        XCTAssertEqual(motion.settled, 11)        XCTAssertNotNil(fan.placement(index: 11, theta: motion.theta, width: 393))    }    #endif    private func tier(_ id: String) -> FanTier {        FanTier(id: id, cards: [.init(id: id, label: id)], raised: id)    }    func testPushShowsOnlyOpenAndPopRestoresParent() {        let stack = FanTiers()        stack.push(tier("tools")) { _ in }        XCTAssertFalse(stack.closes)        stack.push(tier("views")) { _ in }        XCTAssertEqual(stack.open?.id, "views")        XCTAssertEqual(stack.visible.map(\.id), ["views"])        XCTAssertTrue(stack.closes)        XCTAssertEqual(stack.pop()?.id, "views")        XCTAssertEqual(stack.open?.id, "tools")        XCTAssertEqual(stack.visible.map(\.id), ["tools"])        XCTAssertNil(stack.pop(), "the root cannot be removed")    }    /// RULE: a crumb tap leaves only its path's whole tiers standing: a    /// whole tier beneath an opened record's acts goes with them    /// (`alignWholes` popped only while the open tier was a whole's).    func testACrumbTapLeavesNoStaleWholeTier() {        let stack = FanTiers()        stack.push(tier("tools")) { _ in }        let series = WholeLevel(table: "series", id: "s-1", title: "Invented series")        let season = WholeLevel(table: "season", id: "se-1", title: "Invented season")        stack.alignWholes([series, season])        stack.push(tier("acts")) { _ in }        stack.alignWholes([series])        XCTAssertEqual(stack.depth, 2)        XCTAssertEqual(stack.open?.id, FanTiers.wholeID(series))        stack.alignWholes([])        XCTAssertEqual(stack.open?.id, "tools")    }    func testOnlyOpenTierIsVisibleButEveryLevelCanBePopped() {        let stack = FanTiers()        for id in ["tools", "views", "acts", "days", "times"] { stack.push(tier(id)) { _ in } }        XCTAssertEqual(stack.depth, 5)        XCTAssertEqual(stack.visible.map(\.id), ["times"])        for id in ["times", "days", "acts", "views"] { XCTAssertEqual(stack.pop()?.id, id) }        XCTAssertEqual(stack.visible.map(\.id), ["tools"])        XCTAssertFalse(stack.closes)    }    func testClosingAReplacedChoiceRunsItsCleanupOnce() {        let stack = FanTiers()        var closed = 0        stack.setRoot(tier("tools"))        stack.push(tier("days"), onPop: { closed += 1 }) { _ in }        stack.replaceOpen(tier("times")) { _ in }        stack.raise("missing")        XCTAssertEqual(stack.open?.raised, "times")        stack.pop()        stack.pop()        XCTAssertEqual(closed, 1)    }    func testReplacementKeepsParentAndUsesNewTapHandler() {        let stack = FanTiers()        var chosen: String?        stack.push(tier("tools")) { _ in XCTFail("tucked cards cannot be chosen") }        stack.push(tier("days")) { _ in XCTFail("replaced handler") }        stack.replaceOpen(tier("times")) { chosen = $0.id }        stack.choose("tools")        XCTAssertNil(chosen)        stack.choose("times")        XCTAssertEqual(chosen, "times")        XCTAssertEqual(stack.depth, 2)        XCTAssertEqual(stack.visible.map(\.id), ["times"])        stack.raise("times")        XCTAssertEqual(stack.open?.raised, "times")    }}