Cotton code
175 lines · 11.5 KB · SwiftRaw
// The deck moves by tap (owner, 29 Oct 2026): the screen's left third is the// record before, its right third the next, at once; the middle third is the// object's own tap; a tagged part never moves the deck; past either end the// deck rolls into the neighbouring view; VoiceOver has "Next record" and// "Previous record"; nothing is drawn behind the card.import SwiftUIimport XCTest@testable import CottonUI@MainActorfinal class DeckZonesTests: XCTestCase {    func testOpenedRecordPinchInKeepsTheSharedCloseThreshold() throws {        let opened = try source("Deck/OpenedCard.swift")        XCTAssertTrue(opened.contains(".simultaneousGesture(MagnifyGesture()"))        XCTAssertTrue(opened.contains("pinch.magnification < DeckWholeCardKnobs.closes"))        XCTAssertLessThan(DeckWholeCardKnobs.closes, 1)    }    /// A phone's screen, 390 points across.    private let zones = DeckZones(minX: 0, width: 390)    private func source(_ path: String) throws -> String {        SourceSweep.withoutComments(try SourceSweep.mustRead("apps/cotton/Sources/CottonUI/\(path)", ownedBy: "deck"))    }    /// RULE: the screen's width in thirds — the left the previous record, the right the next, the middle the object's own tap — on the card and on the ground beside and above it alike.    func testTheThirdsOfTheScreen() {        XCTAssertEqual(zones.step(at: CGPoint(x: 10, y: 300)), -1, "the left edge")        XCTAssertEqual(zones.step(at: CGPoint(x: 129, y: 80)), -1, "the left third, above the card")        XCTAssertNil(zones.step(at: CGPoint(x: 131, y: 300)), "the middle third is the object's own")        XCTAssertNil(zones.step(at: CGPoint(x: 259, y: 300)))        XCTAssertTrue(zones.isOwn(CGPoint(x: 195, y: 300)))        XCTAssertEqual(zones.step(at: CGPoint(x: 261, y: 300)), 1, "the right third")        XCTAssertEqual(zones.step(at: CGPoint(x: 385, y: 700)), 1, "the right edge, below the card")        XCTAssertFalse(zones.isOwn(CGPoint(x: 385, y: 300)))        // The zones are the screen's, wherever the page stands in it.        let page = DeckZones(CGRect(x: 0, y: 120, width: 390, height: 600))        XCTAssertEqual(page, zones)        XCTAssertEqual(DeckZones(minX: 0, width: 0).zone(at: 300), .own, "no width yet, no move")    }    /// RULE: a tap that lands on a tagged part never moves the deck, in any zone: the part does what it does, or nothing.    func testATaggedPartInTheRightThirdNeverMoves() {        let title = CGRect(x: 40, y: 140, width: 310, height: 60)        XCTAssertNil(zones.step(at: CGPoint(x: 330, y: 170), kept: [title]), "the title, in the right third")        XCTAssertNil(zones.step(at: CGPoint(x: 60, y: 170), kept: [title]), "the title, in the left third")        XCTAssertEqual(zones.step(at: CGPoint(x: 330, y: 260), kept: [title]), 1, "the bare card under it moves")        XCTAssertEqual(zones.step(at: CGPoint(x: 60, y: 260), kept: [title]), -1)        // The card reads its parts off the face's tap map, and takes the        // side thirds alongside the face's own taps (a todo's page turn).        let zoned = try? source("Deck/DeckZones.swift")        XCTAssertTrue(zoned?.contains("overlayPreferenceValue(FacePartsKey.self)") == true, "the parts are read off the tap map")        XCTAssertTrue(zoned?.contains(".simultaneousGesture(SpatialTapGesture(coordinateSpace: .global)") == true)    }    /// RULE: past the last record the right third goes on to the next view's first, past the first the left third back to the previous view's last — the swipe's rollover.    func testMultiCardStepsClampAtBothEnds() {        XCTAssertEqual(DeckStep.of(24, from: 3, count: 100), .card(27))        XCTAssertEqual(DeckStep.of(-24, from: 30, count: 100), .card(6))        XCTAssertEqual(DeckStep.of(24, from: 90, count: 100), .card(99))        XCTAssertEqual(DeckStep.of(-24, from: 3, count: 100), .card(0))    }    func testRolloverAtTheEnds() throws {        XCTAssertEqual(DeckStep.of(1, from: 0, count: 3), .card(1))        XCTAssertEqual(DeckStep.of(-1, from: 2, count: 3), .card(1))        XCTAssertEqual(DeckStep.of(1, from: 2, count: 3), .past(forward: true), "the last card's right tap rolls on")        XCTAssertEqual(DeckStep.of(-1, from: 0, count: 3), .past(forward: false), "the first card's left tap rolls back")        XCTAssertEqual(DeckStep.of(1, from: 0, count: 0), .past(forward: true))        let deck = try source("Deck/DeckSurface.swift")        XCTAssertTrue(deck.contains("case .past(let forward):"))        XCTAssertTrue(deck.contains("roll(forward: forward, held: cards.count)"), "a step past an end is the swipe's roll")        // A tool's fan tile opens its page with the ring's chosen view in        // hand (`ToolViewRoute`): that list rolls over too.        let route = try source("Surfaces/TableRoute.swift")        XCTAssertTrue(route.contains("rollover: rolls ? { forward in rollover(forward: forward) } : nil"), "the deck's rollover is not wired")        XCTAssertTrue(route.contains("view.name == current.drawnRead(of: key).name"), "a list handed the ring's view does not roll")    }    /// RULE: every deck the phone draws — a tool's records, Home's cards, the tools, a business's customer decks — moves by the zones, card and ground.    func testEveryDeckMovesByTheZones() throws {        for file in ["Deck/DeckSurface.swift", "Deck/CardDeck.swift"] {            let code = try source(file)            XCTAssertTrue(code.contains(".deckZoned(zones, move:"), "\(file)'s card is not zoned")            XCTAssertTrue(code.contains(".deckZonedGround("), "\(file)'s ground is not zoned")            XCTAssertTrue(code.contains(".deckMoveActions {"), "\(file)'s card has no Next and Previous record")            XCTAssertFalse(code.contains(".onTapGesture"), "\(file) keeps a tap outside the zones")        }        for file in ["Surfaces/Board/HomeToday.swift", "Deck/Faces/ToolsDeck.swift", "Join/CustomerDeck.swift"] {            XCTAssertTrue(try source(file).contains("CardDeck("), "\(file) draws its own deck")        }        XCTAssertTrue(try source("Surfaces/Board/HomeToday.swift").contains(".deckKeepsTaps()"), "Home's own rows keep their taps")    }    /// RULE: a card that reads — a note's page, a memory — opens its reading page by its own tap in the middle third (the core says it, `shell.deck`'s `tap.card` `open.reading`), the very opening its pinch out stands up (one `opened`, `DeckOpening.open`), not a second presentation.    func testAReadingCardsTapOpensTheReader() throws {        func kit(_ name: String) throws -> TableDefinition {            let url = SourceSweep.repoRoot.appendingPathComponent("crates/cotton-core/kits/personal/tables/\(name).json")            return try TableDefinition.decode(Data(contentsOf: url))        }        for table in ["note", "memory"] {            let definition = try kit(table)            let route = FullScreenRoute.of(definition)            XCTAssertTrue(route.reads, "\(table)'s card does not read")            var shown = false            let record = TableRecord(id: "r1", values: ["title": .text("Invented title"), "body": .text("Invented words.")])            let opening = DeckOpening.of(record, card: nil, in: [record], definition: definition, presenter: nil)            guard case .reading = opening else { return XCTFail("\(table) opens no reading page") }            opening.open(Binding(get: { shown }, set: { shown = $0 }))            XCTAssertTrue(shown, "\(table)'s opening does not stand up")        }        let deck = try source("Deck/DeckSurface.swift")        XCTAssertTrue(deck.contains("opening.open($opened)"), "the tap opens by its own path")        XCTAssertTrue(deck.contains(".pinchOpens(opening(record, card: card, cards: cards), opened: $opened)"),                      "the pinch and the tap do not share the one opening")    }    func testEditablePartsOpenAndActionPartsKeepTheirActs() {        let zones = DeckZones(width: 300)        let editable = CGRect(x: 10, y: 10, width: 40, height: 30)        let action = CGRect(x: 130, y: 10, width: 40, height: 30)        XCTAssertTrue(zones.opens(at: CGPoint(x: 25, y: 20), parts: [editable], acts: [action], kept: []))        XCTAssertTrue(zones.opens(at: CGPoint(x: 150, y: 80), parts: [], acts: [action], kept: []))        XCTAssertFalse(zones.opens(at: CGPoint(x: 150, y: 20), parts: [action], acts: [action], kept: []))        XCTAssertFalse(zones.opens(at: CGPoint(x: 150, y: 20), parts: [], acts: [], kept: [action]))        XCTAssertFalse(zones.opens(at: CGPoint(x: 280, y: 80), parts: [], acts: [], kept: []))    }    /// RULE: VoiceOver moves the deck by the card's "Next record" and "Previous record", so the zones are not the only way.    func testTheCardSaysNextAndPreviousRecord() {        XCTAssertEqual(DeckZoneWords.next, "Next record")        XCTAssertEqual(DeckZoneWords.previous, "Previous record")    }    /// RULE: ordinary faces keep the plain cut; a face that declares turns receives its neighbour and commits only after settling.    func testAMoveIsAPlainCut() throws {        for file in ["Deck/DeckSurface.swift", "Deck/CardDeck.swift"] {            let code = try source(file)            XCTAssertFalse(code.contains(".transition(.asymmetric"), "\(file) slides its card in")            XCTAssertFalse(code.contains("withAnimation(.snappy(duration: 0.28))"), "\(file) springs to the next card")        }    }    func testTheRolodexOwnsItsTurnAndVoiceOverKeepsTheDirectMove() throws {        let code = try source("Deck/DeckSurface.swift")        XCTAssertTrue(code.contains(".environment(\\.faceTurn, faceTurn(in: cards, at: at))"))        XCTAssertTrue(code.contains("window[index - at] = row.card"), "the arriving card is the core's answered card, as the resting one is")        XCTAssertTrue(code.contains(".deckMoveActions { move($0, in: cards, from: at) }"))        XCTAssertTrue(code.contains(".steps == .turns"))    }    /// RULE: nothing is drawn behind the card in place — the stack of sheets is gone, its code too; the counter stays.    func testNoStackBehindTheCard() throws {        let sources = SourceSweep.iosSources        let files = FileManager.default.enumerator(at: sources, includingPropertiesForKeys: nil)?            .compactMap { $0 as? URL }.filter { $0.pathExtension == "swift" } ?? []        XCTAssertFalse(files.isEmpty, "\(sources.path) read as empty")        for file in files {            let code = SourceSweep.withoutComments(try String(contentsOf: file, encoding: .utf8))            for gone in ["CardSheetsUnder", "underRoom", "sheetsUnder", "stockUnder", "underMost", "edgeTap"] {                XCTAssertFalse(code.contains(gone), "\(file.lastPathComponent) still draws the stack (\(gone))")            }        }        XCTAssertTrue(try source("Deck/DeckSurface.swift").contains("accessibilityIdentifier(\"deck-position\")"), "the counter stays")    }    /// RULE (the Cinemas walk, 5 Nov 2026): the record a deck was opened at (an intake's landing) is brought once, on the view it opened on; a view the tier chooses after is that view's own deck — the landed card never stands again as a visitor on each.    func testALandedCardIsBroughtOnceNotOnEveryView() throws {        let deck = try source("Deck/DeckSurface.swift")        XCTAssertTrue(deck.contains("if let opensAt, openedAtOn == nil {\n                openedAtOn = reading\n                await bring(opensAt)"),                      "the deck brings its opening record on every view it is shown")        XCTAssertFalse(deck.contains("if let opensAt { await bring(opensAt) }"), "every read brings the landed card again")    }}