Cotton code
132 lines · 7.6 KB · SwiftRaw
// Writing to a number on a copy linked to WhatsApp through the Mac// (FEEDBACK-2 A, CONTRACT.md "Writing to a number"), over Chat Dev's invented// chats (-chat-warm-seed YES: the core's own answers on a linked copy, written// by write_chat_warm_fixture). New chat, a number typed: Noor is on WhatsApp// only and has no chat yet, so the core starts one whose header and writing box// say "via WhatsApp" before anything is sent, with no invite and no Cotton// call; Leela is one of his Cotton connections, so hers says "on Cotton". A// number WhatsApp holds no account for (Kiran's) says "Not sent: This number// is not on WhatsApp" with Retry, and the chat list says so after the line.// The phone decides nothing: every mark is the core's. At the default text// size and the largest.import XCTestfinal class ChatWhatsAppRouteTests: XCTestCase {    private static let largestText = ["-UIPreferredContentSizeCategoryName", "UICTContentSizeCategoryAccessibilityXXXL"]    private let noor = "15550107101"    private let leela = "15550107102"    private let kiran = "15550107103"    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)    }    /// New chat on the linked copy, `number` typed and written to: the chat the core started for it.    @MainActor private func writeTo(_ number: String, _ extra: [String]) -> XCUIApplication {        let app = XCUIApplication()        app.launchArguments += ["-chat-warm-seed", "YES", "-chat-warm-linked", "YES", "-chat-warm-new", "YES"] + extra        app.launch()        let field = app.textFields["chat-new-search"]        XCTAssertTrue(field.waitForExistence(timeout: 30), "New chat")        // 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)        field.tap()        field.typeText(number)        let write = app.descendants(matching: .any)["chat-new-write-to"].firstMatch        XCTAssertTrue(write.waitForExistence(timeout: 5), "the number typed can be written to")        write.tap()        XCTAssertTrue(app.descendants(matching: .any)["chat-composer"].firstMatch.waitForExistence(timeout: 15), "its chat opened with a writing box")        return app    }    /// The header and the writing box both say `mark`, before anything is sent.    @MainActor private func says(_ mark: String, in app: XCUIApplication) {        let header = app.buttons["chat-person"]        XCTAssertTrue(header.waitForExistence(timeout: 5))        XCTAssertTrue(header.label.contains(mark), "the header says where it goes: \(header.label)")        let field = app.descendants(matching: .any)["chat-composer-place"].firstMatch        XCTAssertTrue(field.waitForExistence(timeout: 5), "the writing box says where it goes")        XCTAssertEqual(field.label, mark)        let box = app.descendants(matching: .any)["chat-composer"].firstMatch        XCTAssertLessThanOrEqual(field.frame.maxY, app.textViews["chat-composer-text"].frame.minY + 1, "above the field")        XCTAssertGreaterThanOrEqual(field.frame.minY, box.frame.minY - 60, "by the field, at the writing box's top")    }    // MARK: a number not on Cotton: via WhatsApp    @MainActor private func viaWhatsApp(_ extra: [String], _ shots: String) {        let app = writeTo(noor, extra)        says("via WhatsApp", in: app)        XCTAssertFalse(app.descendants(matching: .any)["chat-reach"].exists, "no invite where the words go to WhatsApp")        XCTAssertFalse(app.buttons["chat-call"].exists, "no Cotton call to a person on WhatsApp")        XCTAssertFalse(app.buttons["chat-video"].exists)        keep("wa-route-\(shots)-via-whatsapp")        let field = app.textViews["chat-composer-text"]        field.tap()        field.typeText("Hello from Comms")        app.buttons["chat-composer-send"].tap()        let sent = app.descendants(matching: .any)["chat-bubble-outbox:seed-sent-1"].firstMatch        XCTAssertTrue(sent.waitForExistence(timeout: 5), "the text stands at once")        XCTAssertTrue(sent.label.contains("Waiting to send"), "it waits for the Mac: \(sent.label)")        keep("wa-route-\(shots)-via-whatsapp-sent")    }    // RULE: a number not on Cotton, with no chat yet, opens the chat the core starts: "via WhatsApp" in the header    // and by the field before Send, no invite, no Cotton call; what is sent waits for the Mac.    @MainActor func testANumberNotOnCottonSaysViaWhatsAppBeforeSend() { viaWhatsApp([], "default") }    @MainActor func testANumberNotOnCottonSaysViaWhatsAppBeforeSendAtTheLargestText() { viaWhatsApp(Self.largestText, "largest") }    @MainActor func testANumberNotOnCottonSaysViaWhatsAppBeforeSendAtNight() {        XCUIDevice.shared.appearance = .dark        defer { XCUIDevice.shared.appearance = .light }        viaWhatsApp([], "night")    }    // MARK: a Cotton connection: on Cotton    @MainActor private func onCotton(_ extra: [String], _ shots: String) {        let app = writeTo(leela, extra)        says("on Cotton", in: app)        XCTAssertFalse(app.descendants(matching: .any)["chat-reach"].exists, "she is on Cotton: no invite")        keep("wa-route-\(shots)-on-cotton")    }    // RULE: one of his Cotton connections is written to on Cotton, and the header and the field say "on Cotton".    @MainActor func testACottonConnectionSaysOnCotton() { onCotton([], "default") }    @MainActor func testACottonConnectionSaysOnCottonAtTheLargestText() { onCotton(Self.largestText, "largest") }    // MARK: a number WhatsApp does not know    @MainActor private func notOnWhatsApp(_ extra: [String], _ shots: String) {        let app = writeTo(kiran, extra)        says("via WhatsApp", in: app)        let failed = app.descendants(matching: .any).matching(NSPredicate(format: "identifier BEGINSWITH 'chat-bubble-outbox:' AND label CONTAINS %@", "Not sent")).firstMatch        XCTAssertTrue(failed.waitForExistence(timeout: 10), "the text the Mac could not send")        XCTAssertTrue(failed.label.contains("Not sent: This number is not on WhatsApp"), "it says why, in the Mac's words: \(failed.label)")        failed.coordinate(withNormalizedOffset: CGVector(dx: 0.75, dy: 0.15)).press(forDuration: 1.0)        XCTAssertTrue(app.buttons["chat-act-retry"].waitForExistence(timeout: 5), "with Retry")        keep("wa-route-\(shots)-not-on-whatsapp-retry")        // The menu put away by one of its own acts (Copy changes nothing in the chat).        app.buttons["chat-act-copy"].tap()        XCTAssertTrue(app.buttons["chat-act-retry"].waitForNonExistence(timeout: 5))        keep("wa-route-\(shots)-not-on-whatsapp")        // Back in the list, the chat's line says where it stands.        app.buttons["chat-back"].tap()        let row = app.descendants(matching: .any).matching(NSPredicate(format: "label CONTAINS %@ AND label CONTAINS %@", "Hello, is this Kiran?", "Not sent: This number is not on WhatsApp")).firstMatch        XCTAssertTrue(row.waitForExistence(timeout: 10), "the list says it after the line")        keep("wa-route-\(shots)-not-on-whatsapp-list")    }    // RULE: a number WhatsApp holds no account for says "Not sent: This number is not on WhatsApp" with Retry, in the    // chat and after its line in the list.    @MainActor func testANumberWhatsAppDoesNotKnowSaysNotSent() { notOnWhatsApp([], "default") }    @MainActor func testANumberWhatsAppDoesNotKnowSaysNotSentAtTheLargestText() { notOnWhatsApp(Self.largestText, "largest") }}