Cotton code
140 lines · 7.6 KB · SwiftRaw
// Making a poll in a WhatsApp chat (FEEDBACK-3, CONTRACT.md "Making a poll"),// over Chat Dev's invented chats (-chat-warm-seed YES: the core's own answers// on a copy linked through an invented Mac, written by write_chat_warm_fixture).// Where the composer's answer carries `poll`, the plus's sheet offers Poll,// alone where no file goes. The poll's sheet takes the question, one option a// line (a new line under the last one written), whether more than one may be// chosen, and Send, live with a question and two options. The poll's card// stands at once, "Waiting to send"; a refusal is said in the core's words and// the sheet stays. At the default text size and the largest. Shots// wa-poll-make-*.import XCTestfinal class ChatWhatsAppPollMakingTests: XCTestCase {    private let tara = "11111111-1111-4111-8111-0000000000f1"    private static let largestText = ["-UIPreferredContentSizeCategoryName", "UICTContentSizeCategoryAccessibilityXXXL"]    // As write_chat_warm_fixture asked the core (invented).    private let question = "Which train on Friday?"    private let options = ["The 7:40", "The 9:15", "The 11:05"]    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)    }    @MainActor private func open(_ extra: [String]) -> XCUIApplication {        let app = XCUIApplication()        app.launchArguments += ["-chat-warm-seed", "YES", "-chat-warm-open", tara] + extra        app.launch()        XCTAssertTrue(app.descendants(matching: .any)["chat-composer"].firstMatch.waitForExistence(timeout: 30), "Tara's chat opened with a writing box")        // 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)        return app    }    /// The plus, then Poll: the poll's sheet.    @MainActor private func openSheet(in app: XCUIApplication, files: Bool) -> XCUIElement {        let plus = app.buttons["chat-attach"]        XCTAssertTrue(plus.waitForExistence(timeout: 5), "the plus stands for a poll")        plus.tap()        let poll = app.buttons["chat-attach-poll"]        XCTAssertTrue(poll.waitForExistence(timeout: 5), "Poll in the attach sheet")        XCTAssertEqual(poll.label, "Poll", "in the core's word")        XCTAssertEqual(app.buttons["chat-attach-file"].exists, files, files ? "files beside it" : "Poll alone where no file goes")        XCTAssertEqual(app.buttons["chat-attach-photo"].exists, files)        return poll    }    /// The question and the options written, more than one allowed; Send live only once two options stand.    @MainActor private func write(in app: XCUIApplication) {        let sheet = app.descendants(matching: .any)["chat-poll-sheet"].firstMatch        XCTAssertTrue(sheet.waitForExistence(timeout: 5), "the poll's sheet")        let send = app.buttons["chat-poll-send"]        XCTAssertFalse(send.isEnabled, "nothing to send yet")        let asked = app.textFields["chat-poll-question"]        asked.tap()        // Return goes to the next line, so the keyboard never stands over the line being written.        asked.typeText(question + "\n")        for (i, option) in options.enumerated() {            let line = app.textFields["chat-poll-option-\(i)"]            XCTAssertTrue(line.waitForExistence(timeout: 3), "option \(i) stands")            line.typeText(option)            if i == 0 { XCTAssertFalse(send.isEnabled, "one option is not a poll") }            XCTAssertTrue(app.textFields["chat-poll-option-\(i + 1)"].waitForExistence(timeout: 3), "a new line stands under the last one written")            line.typeText("\n")        }        XCTAssertTrue(send.isEnabled, "a question and two options: Send is live")        // Return on the empty last line puts the keyboard away.        app.textFields["chat-poll-option-\(options.count)"].typeText("\n")        let many = app.switches["chat-poll-many"]        XCTAssertTrue(many.exists, "the switch")        XCTAssertTrue(many.label.contains("Let people choose more than one"), many.label)        if !many.isHittable { app.swipeUp() }        // The switch itself, not its words (a tap on the words is not a tap on the switch).        if many.switches.firstMatch.exists { many.switches.firstMatch.tap() } else { many.tap() }        XCTAssertEqual(many.value as? String, "1", "more than one may be chosen")    }    // MARK: Poll among the files, sent    @MainActor private func made(_ extra: [String], _ shots: String) {        let app = open(extra)        openSheet(in: app, files: true).tap()        write(in: app)        keep("wa-poll-make-\(shots)-sheet")        app.buttons["chat-poll-send"].tap()        let card = app.descendants(matching: .any).matching(NSPredicate(format: "identifier BEGINSWITH 'chat-bubble-outbox:' AND label CONTAINS %@", question)).firstMatch        XCTAssertTrue(card.waitForExistence(timeout: 5), "the poll's card stands at once")        XCTAssertFalse(app.descendants(matching: .any)["chat-poll-sheet"].exists, "the sheet goes once the core took it")        let said = card.label        XCTAssertTrue(said.contains("Waiting to send"), "waiting for the Mac, never sent: \(said)")        XCTAssertTrue(said.contains("Select one or more"), "more than one may be chosen: \(said)")        for option in options { XCTAssertTrue(said.contains(option), "\(option) on the card: \(said)") }        Thread.sleep(forTimeInterval: 1)        keep("wa-poll-make-\(shots)-sent")    }    // RULE: the plus offers Poll beside the files; the poll goes as written and its card stands at once, "Waiting to send".    @MainActor func testAPollHeMakesShowsAtOnceWaitingToSend() { made([], "default") }    @MainActor func testAPollHeMakesShowsAtOnceWaitingToSendAtTheLargestText() { made(Self.largestText, "largest") }    // MARK: Poll alone    @MainActor private func alone(_ extra: [String], _ shots: String) {        let app = open(["-chat-warm-wa-poll", "alone"] + extra)        _ = openSheet(in: app, files: false)        Thread.sleep(forTimeInterval: 0.5)        keep("wa-poll-make-\(shots)-alone")    }    // RULE: where no file goes but a poll does, the plus still stands, with Poll alone.    @MainActor func testWherePollsGoButNoFileThePlusOffersPollAlone() { alone([], "default") }    @MainActor func testWherePollsGoButNoFileThePlusOffersPollAloneAtTheLargestText() { alone(Self.largestText, "largest") }    // MARK: refused    @MainActor private func refused(_ extra: [String], _ shots: String) {        let app = open(["-chat-warm-wa-poll", "refuse"] + extra)        openSheet(in: app, files: true).tap()        write(in: app)        app.buttons["chat-poll-send"].tap()        let why = "A poll needs at least two options."        let toast = app.descendants(matching: .any).matching(NSPredicate(format: "label CONTAINS %@", why)).firstMatch        XCTAssertTrue(toast.waitForExistence(timeout: 5), "the refusal is said in the core's words")        XCTAssertTrue(app.descendants(matching: .any)["chat-poll-sheet"].exists, "the sheet stays, as he wrote it")        XCTAssertEqual(app.textFields["chat-poll-question"].value as? String, question)        keep("wa-poll-make-\(shots)-refused")    }    // RULE: a poll the core refuses is said in its words, as an error, and nothing stands in the chat.    @MainActor func testARefusedPollIsSaidInTheCoresWords() { refused([], "default") }    @MainActor func testARefusedPollIsSaidInTheCoresWordsAtTheLargestText() { refused(Self.largestText, "largest") }}