Cotton code
150 lines · 8.3 KB · SwiftRaw
// A text written in Comms in a WhatsApp chat goes through the owner's Mac// (CONTRACT.md "WhatsApp from Comms", 22 Nov 2026), over// Chat Dev's invented chats (-chat-warm-seed YES: the core's own answers,// written by write_chat_warm_fixture). Tara's chat came from WhatsApp and// Cotton knows her number: on a server whose outbox takes no file yet it// answers a writing box of words only (no attach; the core's button is never// the voice recorder's; pictures are ChatWhatsAppMediaTests'), a text// sent stands at once saying "Waiting to send", each text on its way says// where it stands in the core's words, and a failed one says why and offers// Retry first. At the default text size and the largest.import XCTestfinal class ChatWhatsAppSendTests: XCTestCase {    private let tara = "11111111-1111-4111-8111-0000000000f1"    private static let largestText = ["-UIPreferredContentSizeCategoryName", "UICTContentSizeCategoryAccessibilityXXXL"]    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    }    @MainActor private func bubbles(_ app: XCUIApplication) -> [XCUIElement] {        app.descendants(matching: .any).matching(NSPredicate(format: "identifier BEGINSWITH 'chat-bubble-outbox:'")).allElementsBoundByIndex    }    /// The pending bubble whose words hold `words`.    @MainActor private func pending(_ words: String, in app: XCUIApplication) -> XCUIElement {        app.descendants(matching: .any).matching(NSPredicate(format: "identifier BEGINSWITH 'chat-bubble-outbox:' AND label CONTAINS %@", words)).firstMatch    }    // MARK: the writing box    @MainActor private func textOnly(_ extra: [String], _ shots: String) {        let app = open(["-chat-warm-wa-words-only", "YES"] + extra)        XCTAssertFalse(app.descendants(matching: .any)["chat-read-only"].exists, "a WhatsApp chat whose number is known is not read only")        XCTAssertTrue(app.textViews["chat-composer-text"].exists, "words can be written")        XCTAssertFalse(app.buttons["chat-attach"].exists, "no attach: words only")        keep("whatsapp-send-\(shots)-composer")        // Her message's acts stand when held (Reply among them where the core offers it, FEEDBACK-3).        let theirs = app.descendants(matching: .any)["chat-bubble-m-t1"].firstMatch        XCTAssertTrue(theirs.waitForExistence(timeout: 5))        theirs.press(forDuration: 1.0)        XCTAssertTrue(app.buttons["chat-act-copy"].waitForExistence(timeout: 5), "the message's acts stand")    }    // RULE: a WhatsApp chat whose number is known answers a writing box; where the server's outbox takes no file yet,    // of words only (no attach, no voice).    @MainActor func testAWhatsAppChatShowsATextOnlyComposer() { textOnly([], "default") }    @MainActor func testAWhatsAppChatShowsATextOnlyComposerAtTheLargestText() { textOnly(Self.largestText, "largest") }    // MARK: each state in words    @MainActor private func states(_ extra: [String], _ shots: String) {        let app = open(extra)        for words in ["Waiting to send", "Sending…", "Not sent: "] {            let bubble = pending(words, in: app)            XCTAssertTrue(bubble.waitForExistence(timeout: 10), "a text on its way says \"\(words)\": \(bubbles(app).map(\.label))")            XCTAssertFalse(bubble.label.contains("Read"), "never drawn as read: \(bubble.label)")        }        keep("whatsapp-send-\(shots)-states")    }    // RULE: each text on its way through the Mac draws the core's words under it: "Waiting to send", "Sending…",    // "Not sent: <why>".    @MainActor func testEachPendingTextSaysWhereItStands() { states([], "default") }    @MainActor func testEachPendingTextSaysWhereItStandsAtTheLargestText() { states(Self.largestText, "largest") }    // MARK: sending    @MainActor private func sends(_ extra: [String], _ shots: String) {        let app = open(extra)        let field = app.textViews["chat-composer-text"]        field.tap()        field.typeText("See you at the station")        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 text stands at once")        XCTAssertTrue(sent.label.contains("See you at the station"), sent.label)        XCTAssertTrue(sent.label.contains("Waiting to send"), "it waits for the Mac, never sent: \(sent.label)")        keep("whatsapp-send-\(shots)-sent")    }    // RULE: a text sent stands at once as the owner's, saying "Waiting to send", until the Mac takes it.    @MainActor func testSendingShowsTheBubbleAtOnceWithItsState() { sends([], "default") }    @MainActor func testSendingShowsTheBubbleAtOnceWithItsStateAtTheLargestText() { sends(Self.largestText, "largest") }    // MARK: a failed text, and Retry    /// Short drags of the chat until `element`'s top stands between the header and the writing box.    @MainActor private func bringIntoView(_ element: XCUIElement, in app: XCUIApplication) {        let table = app.tables["chat-thread"]        let top = app.buttons["chat-person"].frame.maxY + 10        let foot = app.descendants(matching: .any)["chat-composer"].firstMatch.frame.minY - 120        for _ in 0..<16 {            let y = element.frame.minY            if y > top, y < foot { return }            let from = table.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5))            from.press(forDuration: 0.05, thenDragTo: from.withOffset(CGVector(dx: 0, dy: y < top ? 160 : -160)))        }    }    @MainActor private func retries(_ extra: [String], _ shots: String) {        let app = open(extra)        // The text the Mac could not send (a picture of hers may also say "Not sent:", ChatWhatsAppMediaTests').        let failed = pending("Not sent: WhatsApp did not take it", in: app)        XCTAssertTrue(failed.waitForExistence(timeout: 10))        XCTAssertTrue(failed.label.contains("Not sent: WhatsApp did not take it"), "it says why: \(failed.label)")        let id = failed.identifier        // Her chat holds his pictures and voice note on their way below it: the text is brought into view first.        bringIntoView(failed, in: app)        keep("whatsapp-send-\(shots)-failed")        // Held by its words, above the state line (whose own tap is Retry).        failed.coordinate(withNormalizedOffset: CGVector(dx: 0.75, dy: 0.15)).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))")        keep("whatsapp-send-\(shots)-retry-offered")        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 text waits again: \(again.label)")        keep("whatsapp-send-\(shots)-retried")    }    // RULE: a failed text says "Not sent:" and why, offers Retry first, and Retry answers it waiting again.    @MainActor func testAFailedTextSaysWhyAndRetryAnswers() { retries([], "default") }    @MainActor func testAFailedTextSaysWhyAndRetryAnswersAtTheLargestText() { retries(Self.largestText, "largest") }}