Cotton code
102 lines · 6.2 KB · SwiftRaw
#if os(iOS)import XCTest@testable import CottonUI/// The tiles shrinking under a settled fan (sharing2 walk, 16 Oct 2026): a/// removed member's phone, sent back to Personal from its shared database's/// Notes, drew the page area at the old settled index over Home alone — the/// page's `tiles[index]` ran past the end and the app trapped.@MainActorfinal class FanShrinkTests: XCTestCase {    private let fan = FanGeometry(scale: Theme.paper.scale)    /// GUARD: a shrinking arrangement under a settled fan never subscripts out of range — what the page area draws is always a tile the fan holds now (the nearest card; Home when the open database left and Home alone stands), and nothing when there is no tile (coordinator, 16 Oct 2026: the removed member's phone died in `BoardSurface.fanHome`; a removed tool does the same).    func testAShrinkingArrangementNeverDrawsPastItsTiles() {        for was in 0..<12 {            for count in 0...12 {                for shows in [FanPageRule.Shows.live(was), .picture(was)] {                    guard let held = FanPageRule.held(shows, count: count) else {                        XCTAssertEqual(count, 0, "only a fan with no tile draws nothing")                        continue                    }                    switch held {                    case .live(let index), .picture(let index):                        XCTAssertTrue((0..<count).contains(index), "\(shows) over \(count) tiles drew \(held)")                        XCTAssertEqual(index, min(was, count - 1), "the nearest card")                    }                    XCTAssertEqual(FanPageRule.mounted(held).isEmpty, FanPageRule.mounted(shows).isEmpty,                                   "a live page stays live and a picture a picture")                }            }        }        XCTAssertEqual(FanPageRule.held(.live(7), count: 1), .live(0), "Home alone: the page area draws Home")    }    /// GUARD: the fan's own settled tile follows the tiles down — a recount under a fan settled past the new end rests it on the last card, a removed tool's neighbour, and one that still holds it keeps it.    func testARecountRestsTheFanOnACardItHolds() {        let motion = FanMotion(geometry: fan, spring: FanSpring(scale: Theme.paper.scale))        motion.recount(12)        motion.turn(to: 9, animated: false)        XCTAssertEqual(motion.settled, 9)        motion.recount(10)        XCTAssertEqual(motion.settled, 9, "a tile it still holds stays settled")        motion.recount(4)        XCTAssertEqual(motion.settled, 3, "past the end: the last card")        XCTAssertEqual(motion.shows, .live(3))        motion.recount(1)        XCTAssertEqual(motion.settled, 0, "Home alone")        XCTAssertEqual(motion.shows, .live(0))    }    /// RULE: a change of tiles keeps the fan on the tile it rested on, by key — at launch and after a switch that is Home, wherever the tiles read after the arrangement put Home; a tile gone rests it on Home — never on whatever card took its slot (walker pass 3, 16 Oct 2026: a cold launch and a database switch rested on a turned Documents card with a ghost mid-screen, because the fan kept its slot while tools' definitions arrived and shifted Home along the arc).    func testATileChangeKeepsTheFanOnItsTile() {        let motion = FanMotion(geometry: fan, spring: FanSpring(scale: Theme.paper.scale))        var keys = ["home", "tools"]        motion.recount(keys.count)        XCTAssertEqual(keys[motion.settled], "home", "the fan opens on Home")        // The tools' own tiles arrive after the arrangement: Home moves to the centre.        let rests = { (on: String?, opens: Int) in            motion.recount(keys.count)            let at = FanPageRule.rest(on: on, keys: keys, opens: opens)            if at != motion.settled { motion.turn(to: at, animated: false) }        }        keys = ["notes", "journal", "documents", "home", "photos", "money", "tools"]        rests("home", 3)        XCTAssertEqual(keys[motion.settled], "home", "still on Home, not the card in its old slot")        motion.turn(to: 1, animated: false)        keys = ["notes", "news", "journal", "documents", "home", "photos", "money", "tools"]        rests("journal", 4)        XCTAssertEqual(keys[motion.settled], "journal", "a card turned to is kept wherever it moves")        keys = ["notes", "news", "documents", "home", "photos", "money", "tools"]        rests("journal", 3)        XCTAssertEqual(keys[motion.settled], "home", "its tile gone: Home")        XCTAssertEqual(FanPageRule.rest(on: nil, keys: keys, opens: 3), 3, "nothing rested on yet: Home")    }    /// RULE: a route opened by a link, a slip or at launch draws its page above the dial — it lands on the stack of the card the fan shows, because the fan tells the navigator its card after every change of tiles; a fan that kept its slot while tiles arrived showed one card and named another, so a deep link folded the dial over a card whose page never drew (sharing2 and walker pass 3, 16 Oct 2026: the launch ghost and the deep link that never drew, one cause).    func testARouteOpenedAtLaunchLandsOnTheCardShown() {        let motion = FanMotion(geometry: fan, spring: FanSpring(scale: Theme.paper.scale))        let navigator = Navigator()        navigator.fanOpens = { _ in false } // Mounted fan with no matching tile.        var keys = ["home", "tools"]        var restsOn: String?        // The fan as FanHome drives it: rest on the opening card, tell the        // navigator; tiles arrive, rest by key, tell the navigator again.        func settle() {            restsOn = keys[motion.settled]            navigator.tile = keys[motion.settled]        }        motion.recount(keys.count)        settle()        keys = ["notes", "journal", "documents", "home", "photos", "money", "tools"]        motion.recount(keys.count)        let at = FanPageRule.rest(on: restsOn, keys: keys, opens: 3)        if at != motion.settled { motion.turn(to: at, animated: false) }        settle()        navigator.open(.device)        XCTAssertEqual(keys[motion.settled], "home", "the card shown")        XCTAssertEqual(navigator.tileStacks["home"] ?? [], [.device], "the link's page is on the card shown")        XCTAssertNil(navigator.tileStacks["documents"], "and on no card the fan left")    }}#endif