import XCTest @testable import CottonUI /// While reading, the bottom of Home folds into one circle; only the circle /// brings it back (lane fancollapse, 15 Oct 2026; owner, 21 Oct 2026). @MainActor final class FanCollapseTests: XCTestCase { private let scale = Theme.paper.scale private var after: Double { Double(scale[.fanCollapseScroll]) } private var bloomAfter: Double { Double(scale[.fanBloomScroll]) } /// A finger moving the page from offset to offset, a point at a time. private func drag(_ collapse: FanCollapse, from: Double, to: Double, end: Double = 2000) { let step = from < to ? 1.0 : -1.0 var offset = from while offset != to { offset += step collapse.scrolled(offset: offset, end: end, byFinger: true, scale: scale, animation: nil) } } /// The page moving by itself: a reload, a page growing, a programmatic scroll. private func moves(_ collapse: FanCollapse, to offset: Double, end: Double = 2000) { collapse.scrolled(offset: offset, end: end, byFinger: false, scale: scale, animation: nil) } /// RULE: a finger scrolling down past fan_collapse_scroll folds the fan into the circle, and no scroll up blooms it — only the circle does (owner, 21 Oct 2026); less travel, or travel that turns back, changes nothing — a reader's small corrections never flap the fan (fancollapse rule 2). func testAFingersScrollCollapsesAndNoScrollBlooms() { let collapse = FanCollapse() moves(collapse, to: 0) drag(collapse, from: 0, to: after - 1) XCTAssertFalse(collapse.collapsed, "short of fan_collapse_scroll") drag(collapse, from: after - 1, to: after - 10) drag(collapse, from: after - 10, to: after + 5) XCTAssertFalse(collapse.collapsed, "a turn back starts the travel again") drag(collapse, from: after + 5, to: 400) XCTAssertTrue(collapse.collapsed, "a scroll down to read folds it") drag(collapse, from: 400, to: 0) XCTAssertTrue(collapse.collapsed, "a scroll back up leaves it folded") drag(collapse, from: 0, to: -(bloomAfter * 3), end: 0) XCTAssertTrue(collapse.collapsed, "a pull past the top leaves it folded") } /// The fan meeting the navigator as it stands now: a page over the tile's own or none. private func meets(_ navigator: Navigator, _ collapse: FanCollapse) { collapse.page(open: !navigator.stack.isEmpty, animation: nil) } /// RULE: a page opened over a tile's own folds the dial, and every way it closes — the X, the edge swipe, a long-press jump, back to the root, a database switch, a turn to a tile with no page open — blooms it in the same call, whatever folded it before the page opened: the fan is under the finger the moment the page goes, never after a timer, a read or a scroll (walker pass 3, 16 Oct 2026: the fan never came back after a record closed; architect: the dial rises in the close's own motion). func testAPageClosesWithTheDialRisen() { let closes: [(String, (Navigator) -> Void)] = [ ("the X, the edge swipe", { $0.pop() }), ("a long-press jump", { $0.back(to: 0) }), ("back to the root", { $0.popToRoot() }), ("a database switch", { $0.resetForDatabaseChange() }), ] for foldedBefore in [false, true] { for (verb, close) in closes { let collapse = FanCollapse() moves(collapse, to: 0) if foldedBefore { drag(collapse, from: 0, to: 300) } let navigator = Navigator() meets(navigator, collapse) XCTAssertEqual(collapse.collapsed, foldedBefore, "no page yet: the state is left as it is") navigator.push(.tools) meets(navigator, collapse) XCTAssertTrue(collapse.collapsed, "a page opened folds the dial") close(navigator) meets(navigator, collapse) XCTAssertFalse(collapse.collapsed, "\(verb) blooms it at once (folded before: \(foldedBefore))") } } } /// RULE: a pull on any list never raises a fan turned to another card — on a page that stands over a tile's own (Tools opened from Me, Goals from Tools) no scroll blooms the dial, whose fan is turned to the tile and not the page; only the page's close or the circle brings it back (walker pass 4, 16 Oct 2026: a pull past Goals' top raised Me's card over Goals and it stayed up across every later page). func testAPullOnAPushedListNeverRaisesTheFan() { let collapse = FanCollapse() let navigator = Navigator() navigator.tile = "invented-db\u{1F}me" moves(collapse, to: 0) navigator.push(.tools) navigator.push(.tableList(table: "goals", filter: nil, title: nil, query: nil)) meets(navigator, collapse) XCTAssertTrue(collapse.collapsed, "the pushed list folds the dial") collapse.watching() moves(collapse, to: 0) drag(collapse, from: 0, to: -(bloomAfter * 3), end: 0) XCTAssertTrue(collapse.collapsed, "a pull past the pushed list's top leaves the dial folded") drag(collapse, from: 0, to: 400) drag(collapse, from: 400, to: 0) XCTAssertTrue(collapse.collapsed, "a scroll back up on it leaves the dial folded") navigator.pop() meets(navigator, collapse) XCTAssertTrue(collapse.collapsed, "a page still stands over the tile's own") navigator.pop() meets(navigator, collapse) XCTAssertFalse(collapse.collapsed, "the last page's close brings the dial back") collapse.watching() moves(collapse, to: 0) drag(collapse, from: 0, to: 300) XCTAssertTrue(collapse.collapsed) drag(collapse, from: 300, to: -(bloomAfter + 10)) XCTAssertTrue(collapse.collapsed, "on the tile's own page too, only the circle blooms it") } /// RULE: with no page opened or closed, nothing but a person's scroll or the circle's swipe changes the collapse — a page landed on at its top or wherever it was left, a reload and a programmatic scroll leave it as it is, and at a page's very top nothing blooms it by itself (fancollapse rules 2 and 3: the state is the session's). func testNothingButAFingerChangesTheCollapseBetweenPages() { for collapsed in [true, false] { let collapse = FanCollapse() moves(collapse, to: 0) drag(collapse, from: 0, to: 300) if !collapsed { collapse.swipedUp(animation: nil) } XCTAssertEqual(collapse.collapsed, collapsed) for landing in [0.0, 640] { collapse.page(open: false, animation: nil) collapse.watching() moves(collapse, to: landing) XCTAssertEqual(collapse.collapsed, collapsed, "landing at \(landing)") } moves(collapse, to: 1200) moves(collapse, to: 0) XCTAssertEqual(collapse.collapsed, collapsed, "the page moving by itself") } } /// RULE: past the bottom the page is bouncing back, or its end moved as the fan's room changed under it — travel there is nobody reading on, so it never blooms the fan just folded (fancollapse rule 2). func testTheBottomsBounceNeverBlooms() { let collapse = FanCollapse() moves(collapse, to: 0, end: 500) drag(collapse, from: 0, to: 560, end: 500) XCTAssertTrue(collapse.collapsed) drag(collapse, from: 560, to: 500, end: 500) XCTAssertTrue(collapse.collapsed, "settling back to the end is not a scroll up") } /// RULE: a swipe up on the circle blooms the fan, and a fresh session starts bloomed (fancollapse rules 2 and 3). func testASwipeUpOnTheCircleBloomsAndASessionStartsBloomed() { XCTAssertFalse(FanCollapse().collapsed, "a fresh launch starts bloomed") let collapse = FanCollapse() moves(collapse, to: 0) drag(collapse, from: 0, to: 300) collapse.swipedUp(animation: nil) XCTAssertFalse(collapse.collapsed) // The swipe ends any travel: the next scroll down needs its own. drag(collapse, from: 300, to: 300 + after - 1) XCTAssertFalse(collapse.collapsed) } }