Cotton code
155 lines · 9.1 KB · SwiftRaw
// Pictures with a caption, videos and voice notes to WhatsApp through the// Mac (FEEDBACK-3, CONTRACT.md "Pictures, voice notes and video"), 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). Tara's// WhatsApp chat offers the plus and the microphone exactly where the core's// writing box does (a server whose outbox takes no file yet answers words only,// c0dec3265d). A picture picked and captioned shows at once from the phone's// own copy, "Waiting to send"; a picture the Mac could not send says why and// offers Retry, which puts it back waiting; a send the core refuses (a file// over WhatsApp's 16 MB) says so and keeps what was picked and written in the// box. Invented pictures only (-chat-warm-pick YES). At the default text size// and the largest.import XCTestfinal class ChatWhatsAppMediaTests: XCTestCase {    private let tara = "11111111-1111-4111-8111-0000000000f1"    private static let largestText = ["-UIPreferredContentSizeCategoryName", "UICTContentSizeCategoryAccessibilityXXXL"]    private let caption = "The platform at dawn"    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-pick", "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 pending bubble whose words hold all of `words`.    @MainActor private func pending(_ words: [String], in app: XCUIApplication) -> XCUIElement {        let each = words.map { NSPredicate(format: "label CONTAINS %@", $0) }        let all = NSCompoundPredicate(andPredicateWithSubpredicates: [NSPredicate(format: "identifier BEGINSWITH 'chat-bubble-outbox:'")] + each)        return app.descendants(matching: .any).matching(all).firstMatch    }    /// The plus, Photos and videos (the invented picture), and `words` written as its caption.    @MainActor private func pick(_ words: String, in app: XCUIApplication) {        let plus = app.buttons["chat-attach"]        XCTAssertTrue(plus.waitForExistence(timeout: 5), "the plus: the core's writing box takes pictures here")        plus.tap()        let photos = app.buttons["chat-attach-photo"]        XCTAssertTrue(photos.waitForExistence(timeout: 5))        photos.tap()        XCTAssertTrue(app.descendants(matching: .any)["chat-attached"].firstMatch.waitForExistence(timeout: 10), "the picked picture waits in the writing box")        XCTAssertTrue(app.keyboards.firstMatch.waitForExistence(timeout: 10), "the field takes the keyboard for the caption")        app.textViews["chat-composer-text"].typeText(words)    }    // MARK: a picture with a caption, at once    @MainActor private func atOnce(_ extra: [String], _ shots: String) {        let app = open(extra)        XCTAssertEqual(app.descendants(matching: .any)["chat-composer-place"].firstMatch.label, "via WhatsApp", "the box says where it goes")        pick(caption, in: app)        keep("wa-media-\(shots)-picked")        app.buttons["chat-composer-send"].tap()        let sent = app.descendants(matching: .any)["chat-bubble-outbox:seed-sent-1"].firstMatch        XCTAssertTrue(sent.waitForExistence(timeout: 3), "the picture stands at once")        let said = sent.label        XCTAssertTrue(said.contains("Photo"), "a picture, drawn from the phone's own copy: \(said)")        XCTAssertTrue(said.contains(caption), "with its caption: \(said)")        XCTAssertTrue(said.contains("Waiting to send"), "waiting for the Mac, never sent: \(said)")        XCTAssertFalse(app.descendants(matching: .any)["chat-attached"].exists, "nothing waits in the box after Send")        // The voice note on its way says where it stands too.        XCTAssertTrue(pending(["Voice message", "Waiting to send"], in: app).exists, "a voice note waiting for the Mac")        Thread.sleep(forTimeInterval: 1)        keep("wa-media-\(shots)-sent")    }    // RULE: a picture with a caption sent to WhatsApp shows at once from the phone's own copy, "Waiting to send".    @MainActor func testAPictureWithACaptionShowsAtOnceWaitingToSend() { atOnce([], "default") }    @MainActor func testAPictureWithACaptionShowsAtOnceWaitingToSendAtTheLargestText() { atOnce(Self.largestText, "largest") }    // MARK: a failed picture, and Retry    @MainActor private func retries(_ extra: [String], _ shots: String) {        let app = open(extra)        let failed = pending(["Photo", "Not sent: Your Mac could not open this file."], in: app)        XCTAssertTrue(failed.waitForExistence(timeout: 10), "the picture the Mac could not send says why")        let id = failed.identifier        keep("wa-media-\(shots)-failed")        // Held by its picture, above the state line (whose own tap is Retry): Retry stands first.        failed.coordinate(withNormalizedOffset: CGVector(dx: 0.7, dy: 0.3)).press(forDuration: 1.0)        let retry = app.buttons["chat-act-retry"]        XCTAssertTrue(retry.waitForExistence(timeout: 5), "Retry is offered")        let acts = app.buttons.matching(NSPredicate(format: "identifier BEGINSWITH 'chat-act-'")).allElementsBoundByIndex        XCTAssertEqual(acts.min { $0.frame.minY < $1.frame.minY }?.identifier, "chat-act-retry", "Retry first: \(acts.map(\.identifier))")        retry.tap()        let again = app.descendants(matching: .any)[id].firstMatch        let waiting = NSPredicate(format: "label CONTAINS %@ AND NOT (label CONTAINS %@)", "Waiting to send", "Not sent")        XCTAssertEqual(XCTWaiter().wait(for: [XCTNSPredicateExpectation(predicate: waiting, object: again)], timeout: 5), .completed,                       "Retry answers: the same picture waits again: \(again.label)")        XCTAssertTrue(again.label.contains("Photo"), again.label)        keep("wa-media-\(shots)-retried")    }    // RULE: a picture the Mac could not send says "Not sent:" and why, with Retry; Retry puts the same one back waiting.    @MainActor func testAFailedPictureOffersRetry() { retries([], "default") }    @MainActor func testAFailedPictureOffersRetryAtTheLargestText() { retries(Self.largestText, "largest") }    @MainActor func testAPictureWithACaptionAndAFailedOneAtNight() {        XCUIDevice.shared.appearance = .dark        defer { XCUIDevice.shared.appearance = .light }        atOnce([], "night")        retries([], "night")    }    // MARK: a refused send keeps what was picked    @MainActor private func survives(_ extra: [String], _ shots: String) {        let app = open(["-chat-warm-wa-refuse", "YES"] + extra)        pick(caption, in: app)        app.buttons["chat-composer-send"].tap()        let toast = app.descendants(matching: .any).matching(NSPredicate(format: "label CONTAINS %@", "too large to send to WhatsApp")).firstMatch        XCTAssertTrue(toast.waitForExistence(timeout: 5), "the refusal is said in the core's words")        XCTAssertTrue(app.descendants(matching: .any)["chat-attached"].firstMatch.waitForExistence(timeout: 5), "the picked picture is back in the box")        let field = app.textViews["chat-composer-text"]        XCTAssertEqual(field.value as? String, caption, "and the caption in the field")        XCTAssertFalse(app.descendants(matching: .any)["chat-bubble-outbox:seed-sent-1"].exists, "nothing was sent")        XCTAssertTrue(app.buttons["chat-composer-send"].isEnabled, "a second tap sends them")        keep("wa-media-\(shots)-refused")    }    // RULE: a send the core refuses keeps the picked pictures and their caption in the writing box (as Android,    // a592647355), and says why.    @MainActor func testThePickedPicturesSurviveARefusal() { survives([], "default") }    @MainActor func testThePickedPicturesSurviveARefusalAtTheLargestText() { survives(Self.largestText, "largest") }    // MARK: words only where the server's outbox takes no file    // RULE (c0dec3265d): where the server's outbox takes no file yet, the core's writing box is words only, and the    // phone offers neither the plus nor the microphone; where it takes them, both.    @MainActor func testTheWritingBoxFollowsWhatTheOutboxTakes() {        var app = open([])        XCTAssertTrue(app.buttons["chat-attach"].exists, "the plus where the outbox takes files")        app.terminate()        app = open(["-chat-warm-wa-words-only", "YES"])        XCTAssertTrue(app.textViews["chat-composer-text"].exists, "words can be written")        XCTAssertFalse(app.buttons["chat-attach"].exists, "no plus: words only")        keep("wa-media-default-words-only")    }}