Cotton code
88 lines · 4.9 KB · SwiftRaw
#if os(iOS)import CottonFacesimport SwiftUIimport UIKitimport XCTest@testable import CottonUI@MainActorfinal class DeckWholeCardTests: XCTestCase {    private func source(_ path: String) throws -> String {        SourceSweep.withoutComments(try SourceSweep.mustRead("apps/cotton/Sources/CottonUI/" + path, ownedBy: "deck"))    }    func testNonMediaPinchIsInert() {        var shown = false        DeckOpening.whole.open(Binding(get: { shown }, set: { shown = $0 }))        XCTAssertFalse(shown)        XCTAssertGreaterThan(DeckWholeCardKnobs.opens, 1)        XCTAssertLessThan(DeckWholeCardKnobs.closes, 1)    }    /// RULE: VoiceOver is offered the pinch's named action only on a card whose pinch opens something; on any other card it would be an action that does nothing.    func testThePinchsVoiceOverActionStandsOnlyWhereItOpens() async throws {        XCTAssertTrue(HostedAccessibility.enable(), "the hosted page's accessibility tree is read")        let name = ShellLines.line("cards", "deck.whole")        func offered(_ opening: DeckOpening) async throws -> Bool {            let host = UIHostingController(rootView: Text(verbatim: "Synthetic card")                .pinchOpens(opening, opened: .constant(false)))            let scene = try XCTUnwrap(UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.first)            let window = UIWindow(windowScene: scene)            window.rootViewController = host            window.makeKeyAndVisible()            defer { window.isHidden = true; window.rootViewController = nil }            host.view.layoutIfNeeded()            await Task.yield()            func nodes(_ root: NSObject) -> [NSObject] {                var children: [NSObject] = (root.accessibilityElements as? [NSObject]) ?? []                let count = root.accessibilityElementCount()                if children.isEmpty, count > 0, count != NSNotFound {                    children = (0..<count).compactMap { root.accessibilityElement(at: $0) as? NSObject }                }                if let view = root as? UIView { children += view.subviews }                return [root] + children.flatMap(nodes)            }            let card = try XCTUnwrap(nodes(host.view).first { $0.accessibilityLabel == "Synthetic card" }, "the card is read out")            return card.accessibilityCustomActions?.contains { $0.name == name } == true        }        let viewer = try await offered(.viewer {})        XCTAssertTrue(viewer, "a card the viewer opens offers the pinch")        let inert = try await offered(.whole)        XCTAssertFalse(inert, "a card that opens nothing offers no pinch")    }    func testOnlyTheOpenedRecordInstallsTheEditableTapMap() throws {        let deck = try source("Deck/DeckSurface.swift")        XCTAssertFalse(deck.contains(".deckEditable("))        XCTAssertTrue(deck.contains(".opensInPlace(openedInPlace == record.id, definition: own(record), record: record"))        XCTAssertTrue(try source("Deck/OpenedCard.swift").contains(".deckEditable(definition: definition, record: record, live: opened)"))        let editable = try source("Deck/Shell/DeckEditable.swift")        let recordModifier = editable.components(separatedBy: "struct DeckEmptyActs")[0]        XCTAssertFalse(recordModifier.contains(".onLongPressGesture(minimumDuration:"))        XCTAssertTrue(deck.contains(".onLongPressGesture(minimumDuration: DeckEditableKnobs.hold)"))        XCTAssertFalse(deck.contains(".pageDestination("), "an opened card stays where it is")        XCTAssertFalse(try source("Deck/CardDeck.swift").contains(".pinchOpens(.whole)"))    }    func testTheOpenedGroundIsTheFacesDeclaredGround() {        for face in FaceRegistry.all {            let card = DeckCard(face: face.id, corner: nil, pressing: false, pace: nil, plate: nil, fields: [:])            let ground = FaceRegistry.ground(face.id)            XCTAssertEqual(DeckGround.of(card), ground == .paper ? nil : ground)        }    }    func testBackupAndRecordsShareTheCardContainerAndActsUseTheArc() throws {        XCTAssertTrue(try source("Join/BackupRoutes.swift").contains("OpenedCardContent {"))        let opened = try source("Deck/OpenedCard.swift")        XCTAssertTrue(opened.contains("OpenedCardContent {"))        XCTAssertFalse(opened.contains("PaperScreen(clearsFan: true"), "opened cards have no tool rail to clear")        XCTAssertTrue(try source("Deck/Shell/EditingFan.swift").contains("editor.navigation.replaceOpen(tier)"))        XCTAssertFalse(try source("Deck/Shell/EditingFan.swift").contains("TypedRow("))        let customer = try source("Join/CustomerDeck.swift")        XCTAssertFalse(customer.contains("OpenedCardPage("), "a business's card opens in place too")        XCTAssertTrue(customer.contains(".pageSurface()"), "the direct business shell hosts its pushed layers")        XCTAssertTrue(opened.contains("onBack: close"))    }}#endif