Cotton code
78 lines · 4.2 KB · SwiftRaw
// The fold is a threshold act, not a per-frame binding (291 row F, architect// 01:05 IST 17 Oct 2026): past fan_collapse_scroll the fan folds once in its// own spring and a pull at the top blooms it once; the list's offset never// drives the band's layout, and the fold never lays the list out again.import Observationimport XCTest@testable import CottonUI@MainActorfinal class FoldLaysNothingOutTests: XCTestCase {    private let scale = Theme.paper.scale    /// RULE: a finger's scroll reaches the fan only through the fold's threshold — every scrolled point short of fan_collapse_scroll changes nothing anything draws from, and crossing it changes the fold once — because a per-frame binding from the list's offset redrew the dial in every frame of a fling (291 row F).    func testAScrollShortOfTheThresholdInvalidatesNothing() {        let collapse = FanCollapse()        let counted = Changes()        func watch() {            withObservationTracking { _ = collapse.collapsed } onChange: { counted.add() }        }        watch()        let threshold = Double(scale[.fanCollapseScroll])        collapse.scrolled(offset: 0, end: 4000, byFinger: true, scale: scale, animation: nil)        var offset = 0.0        while offset < threshold - 1 {            offset += 0.5            collapse.scrolled(offset: offset, end: 4000, byFinger: true, scale: scale, animation: nil)        }        XCTAssertEqual(counted.count, 0, "a scroll short of the threshold is observed by nothing")        while offset < 2000 {            offset += 7            collapse.scrolled(offset: offset, end: 4000, byFinger: true, scale: scale, animation: nil)            if counted.count > 0 { watch() }        }        XCTAssertTrue(collapse.collapsed)        XCTAssertEqual(counted.count, 1, "a fling past the threshold folds the fan once")    }    /// RULE: the fold lays nothing out — the page's inset, the arcs' frames and the record button's frame are the same folded and bloomed, and the fold reaches them only as a scale and an opacity — because a fold that moved the page's inset or shrank the band laid the list out again in every frame of its spring (291 row F).    func testTheFoldIsATransformNeverAFrame() throws {        XCTAssertEqual(FanHub.foldScale(collapsed: false, scale: scale), 1)        XCTAssertEqual(FanHub.foldScale(collapsed: true, scale: scale) * scale[.fanRecordSize],                       scale[.fanCollapsedSize], accuracy: 0.001, "folded, the button is drawn at the circle's size")        let fan = SourceSweep.withoutComments(            try SourceSweep.mustRead("apps/cotton/Sources/CottonUI/Surfaces/Board/FanSurface.swift", ownedBy: "scroll"))        let record = SourceSweep.withoutComments(            try SourceSweep.mustRead("apps/cotton/Sources/CottonUI/Surfaces/Board/FanRecord.swift", ownedBy: "scroll"))        let folds = ["collapse", "collapsed", "folded", "fanDown"]        func layoutLines(_ source: String) -> [Substring] {            source.split(separator: "\n").filter { line in                [".padding(", ".frame(", ".safeAreaPadding(", ".ignoresSafeArea(", ".offset("].contains { line.contains($0) }            }        }        for line in layoutLines(fan) + layoutLines(record) {            for word in folds {                XCTAssertNil(line.range(of: "\\b\(word)\\b", options: .regularExpression),                             "a frame reads the fold: \(line.trimmingCharacters(in: .whitespaces))")            }        }        // The page's room and the deck's frame are computed without the fold.        for name in ["private var room: Double {", "private var deckHeight: Double {"] {            let start = try XCTUnwrap(fan.range(of: name), name)            let body = fan[start.upperBound...].prefix { $0 != "}" }            for word in folds {                XCTAssertNil(body.range(of: "\\b\(word)\\b", options: .regularExpression), "\(name) reads the fold")            }        }    }}/// The changes an observation saw: its callback may run off the main actor.private final class Changes: @unchecked Sendable {    private let lock = NSLock()    private var seen = 0    var count: Int { lock.withLock { seen } }    func add() { lock.withLock { seen += 1 } }}