Cotton code
139 lines · 7.6 KB · SwiftRaw
// Voting on a WhatsApp poll from Comms (FEEDBACK-3, CONTRACT.md "Documents and// votes": "A vote", "What the phones draw"), over Chat Dev's invented chats// (-chat-warm-seed YES -chat-warm-wa-vote: the core's own answers on a copy// linked through an invented Mac, written by write_chat_warm_fixture). Tara's// poll answers `votable`: each option is its own control and a tap sends// `chat.act vote`; the core's answer draws his choice at once (ticked, "You"// among its voters in Votes), "Vote waiting to send". A tap on the total opens// Votes. A vote the Mac could not send says why, with Retry. A refusal is said// in the core's words. At the default text size and the largest. Shots// wa-vote-*.import XCTestfinal class ChatWhatsAppVoteTests: XCTestCase {    private let tara = "11111111-1111-4111-8111-0000000000f1"    private let poll = "m-t-poll"    private static let largestText = ["-UIPreferredContentSizeCategoryName", "UICTContentSizeCategoryAccessibilityXXXL"]    private let why = "WhatsApp did not take it: the Mac's link is not signed in"    override func setUp() {        continueAfterFailure = false        XCUIDevice.shared.orientation = .portrait    }    private func keep(_ name: String) {        let shot = XCTAttachment(screenshot: XCUIScreen.main.screenshot())        shot.name = name        shot.lifetime = .keepAlways        add(shot)    }    /// Tara's chat, her poll brought into reach.    @MainActor private func open(_ mode: String, _ extra: [String]) -> XCUIApplication {        let app = XCUIApplication()        app.launchArguments += ["-chat-warm-seed", "YES", "-chat-warm-open", tara, "-chat-warm-wa-vote", mode] + extra        app.launch()        XCTAssertTrue(app.descendants(matching: .any)["chat-composer"].firstMatch.waitForExistence(timeout: 30), "Tara's chat opened")        // The card of a device asking to be let in can come down a few seconds in and move things: wait for it.        _ = ShellCard.refuse(app).waitForExistence(timeout: 6)        let card = app.descendants(matching: .any)["chat-bubble-\(poll)"].firstMatch        XCTAssertTrue(card.waitForExistence(timeout: 10), "her poll stands")        let table = app.tables["chat-thread"]        let first = option(0, in: app)        for _ in 0..<8 where !(first.exists && first.isHittable) { table.swipeDown() }        for _ in 0..<8 where !(first.exists && first.isHittable) { table.swipeUp() }        XCTAssertTrue(first.isHittable, "her poll in reach")        // Clear of the floating header, which stands over the page's top.        let middle = table.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5))        for _ in 0..<4 where option(0, in: app).frame.minY < 200 {            middle.press(forDuration: 0.05, thenDragTo: middle.withOffset(CGVector(dx: 0, dy: 160)))        }        XCTAssertGreaterThanOrEqual(option(0, in: app).frame.minY, 200, "her poll clear of the header")        return app    }    @MainActor private func option(_ i: Int, in app: XCUIApplication) -> XCUIElement {        app.buttons["chat-poll-\(poll)-option-\(i)"]    }    @MainActor private func card(in app: XCUIApplication) -> XCUIElement {        app.descendants(matching: .any)["chat-bubble-\(poll)"].firstMatch    }    // MARK: a vote    @MainActor private func voted(_ extra: [String], _ shots: String) {        let app = open("YES", extra)        XCTAssertTrue(card(in: app).label.contains("Tap an option to vote"), "read as one he can vote on: \(card(in: app).label)")        let pick = option(1, in: app)        XCTAssertFalse(pick.isSelected, "not his choice yet")        XCTAssertTrue(pick.label.contains("Sandwiches on the train"), pick.label)        keep("wa-vote-\(shots)-before")        pick.tap()        let waiting = card(in: app)        XCTAssertTrue(waiting.label.contains("Vote waiting to send") || app.descendants(matching: .any).matching(NSPredicate(format: "label CONTAINS %@", "Vote waiting to send")).firstMatch.waitForExistence(timeout: 5),                      "his vote waits for the Mac")        XCTAssertTrue(option(1, in: app).isSelected, "his choice is ticked at once")        XCTAssertTrue(option(1, in: app).label.contains("1 vote"), option(1, in: app).label)        Thread.sleep(forTimeInterval: 0.5)        keep("wa-vote-\(shots)-voted")        // The total opens Votes: "You" among those who chose it.        let total = app.buttons["chat-poll-\(poll)-said"]        XCTAssertTrue(total.exists, "the total")        // Clear of the writing box, which floats over the page's foot.        let box = app.descendants(matching: .any)["chat-composer"].firstMatch        let middle = app.tables["chat-thread"].coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5))        for _ in 0..<4 where total.frame.maxY > box.frame.minY - 8 {            middle.press(forDuration: 0.05, thenDragTo: middle.withOffset(CGVector(dx: 0, dy: -160)))        }        total.tap()        let votes = app.descendants(matching: .any)["chat-poll-votes-1"].firstMatch        XCTAssertTrue(votes.waitForExistence(timeout: 5), "Votes opened")        XCTAssertTrue(votes.label.contains("You"), "his name among its voters: \(votes.label)")        keep("wa-vote-\(shots)-votes")    }    // RULE: a tap on an option of a poll he can vote on votes; his choice stands at once, ticked, "Vote waiting to    // send", and Votes says "You".    @MainActor func testATapOnAnOptionVotesAndShowsAtOnce() { voted([], "default") }    @MainActor func testATapOnAnOptionVotesAndShowsAtOnceAtTheLargestText() { voted(Self.largestText, "largest") }    // MARK: not sent, and Retry    @MainActor private func failed(_ extra: [String], _ shots: String) {        let app = open("failed", extra)        let said = "Vote not sent: \(why)"        let retry = app.buttons["chat-retry-\(poll)"]        XCTAssertTrue(retry.waitForExistence(timeout: 5), "Retry under the poll")        XCTAssertTrue(retry.label.contains(said), retry.label)        if !retry.isHittable { app.tables["chat-thread"].swipeUp() }        // A tap while the chat still glides after a swipe only stops it: let it come to rest first.        Thread.sleep(forTimeInterval: 1.2)        keep("wa-vote-\(shots)-failed")        retry.tap()        let waiting = app.descendants(matching: .any).matching(NSPredicate(format: "label CONTAINS %@", "Vote waiting to send")).firstMatch        XCTAssertTrue(waiting.waitForExistence(timeout: 5), "the vote waits again")        XCTAssertFalse(app.buttons["chat-retry-\(poll)"].exists, "Retry goes")        keep("wa-vote-\(shots)-retried")    }    // RULE: a vote the Mac could not send says why in the core's words, with Retry; Retry sends it again.    @MainActor func testAVoteNotSentSaysWhyAndRetries() { failed([], "default") }    @MainActor func testAVoteNotSentSaysWhyAndRetriesAtTheLargestText() { failed(Self.largestText, "largest") }    // MARK: refused    @MainActor private func refused(_ extra: [String], _ shots: String) {        let app = open("refuse", extra)        option(1, in: app).tap()        let toast = app.descendants(matching: .any).matching(NSPredicate(format: "label CONTAINS %@", "This chat can't do that.")).firstMatch        XCTAssertTrue(toast.waitForExistence(timeout: 5), "the refusal in the core's words")        XCTAssertFalse(option(1, in: app).isSelected, "nothing changed")        keep("wa-vote-\(shots)-refused")    }    // RULE: a vote the core refuses is said in its words, and his choice does not change.    @MainActor func testARefusedVoteIsSaidInTheCoresWords() { refused([], "default") }    @MainActor func testARefusedVoteIsSaidInTheCoresWordsAtTheLargestText() { refused(Self.largestText, "largest") }}