Cotton code
75 lines · 2.5 KB · SwiftRaw
// The deck's ground keeps one identity whether or not it holds cards (8 Nov// 2026): writing "c" then "o" read only "c" when "c" emptied the view, because// the zoned ground swapped its wrapper as the deck emptied and rebuilt// everything under it, the ink ground's letter in progress with it.#if os(iOS)import SwiftUIimport XCTest@testable import CottonUI@MainActorfinal class DeckGroundIdentityTests: XCTestCase {    /// RULE: the deck emptying, refilling or opening a search never rebuilds what stands on its ground: the ground's tap stands down, the view beneath stays the same view.    func testTheGroundKeepsItsIdentityAcrossAnEmptyDeck() async {        let deck = ProbeDeck()        let life = ProbeLife()        let window = show(ZonedProbe(deck: deck, life: life))        defer { window.isHidden = true; window.rootViewController = nil }        await settle()        XCTAssertEqual(life.born.count, 1, "the probe never stood")        deck.zones = nil        await settle()        deck.zones = DeckZones(minX: 0, width: 390)        await settle()        deck.zones = nil        await settle()        XCTAssertEqual(life.born.count, 1, "the deck emptying rebuilt the ground beneath it")        XCTAssertEqual(life.gone, 0, "the deck emptying took the ground beneath it away")    }    private func show<V: View>(_ view: V) -> UIWindow {        let scene = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.first!        let window = UIWindow(windowScene: scene)        window.frame = CGRect(x: 0, y: 0, width: 390, height: 844)        window.rootViewController = UIHostingController(rootView: view)        window.makeKeyAndVisible()        window.layoutIfNeeded()        return window    }    private func settle() async { try? await Task.sleep(for: .milliseconds(150)) }}@MainActor @Observable private final class ProbeDeck {    var zones: DeckZones? = DeckZones(minX: 0, width: 390)}/// Each time the probe stood (its own state's id) and each time it went.@MainActor private final class ProbeLife {    var born: Set<UUID> = []    var gone = 0}private struct ZonedProbe: View {    let deck: ProbeDeck    let life: ProbeLife    var body: some View {        GroundProbe(life: life).deckZonedGround(deck.zones) { _ in }    }}private struct GroundProbe: View {    let life: ProbeLife    @State private var id = UUID()    var body: some View {        Color.clear            .onAppear { life.born.insert(id) }            .onDisappear { life.gone += 1 }    }}#endif