// Documents to WhatsApp through the Mac (FEEDBACK-3, CONTRACT.md "Documents and // votes"), 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). A file picked with the plus's File goes at once as a // WhatsApp document under the name it was picked by, and shows from the phone's // own copy, "Waiting to send". A send the core refuses (one over WhatsApp's // 100 MB; a server whose outbox has no file_name yet) says why in the core's // words and puts the file back in the writing box, under its name, with the // words being written still in the field. Invented files only // (-chat-warm-pick-file YES | large). At the default text size and the largest. import XCTest final class ChatWhatsAppDocumentTests: XCTestCase { private let tara = "11111111-1111-4111-8111-0000000000f1" private static let largestText = ["-UIPreferredContentSizeCategoryName", "UICTContentSizeCategoryAccessibilityXXXL"] private let words = "The times for Friday" 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 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, then File (the invented document, as Files hands it over). @MainActor private func pickFile(in app: XCUIApplication) { let plus = app.buttons["chat-attach"] XCTAssertTrue(plus.waitForExistence(timeout: 5), "the plus: the core's writing box takes files here") plus.tap() let file = app.buttons["chat-attach-file"] XCTAssertTrue(file.waitForExistence(timeout: 5), "File in the attach sheet") file.tap() } // MARK: a document, at once @MainActor private func atOnce(_ extra: [String], _ shots: String) { let app = open(["-chat-warm-pick-file", "YES"] + extra) XCTAssertEqual(app.descendants(matching: .any)["chat-composer-place"].firstMatch.label, "via WhatsApp", "the box says where it goes") pickFile(in: app) let sent = pending(["Train times.pdf"], in: app) XCTAssertTrue(sent.waitForExistence(timeout: 3), "the document stands at once, under the name it was picked by") let said = sent.label XCTAssertTrue(said.contains("File"), "a file, drawn from the phone's own copy: \(said)") XCTAssertTrue(said.contains("Waiting to send"), "waiting for the Mac, never sent: \(said)") XCTAssertFalse(said.contains("-Train times.pdf"), "the name as picked, not the copy's: \(said)") XCTAssertTrue(sent.staticTexts["Train times.pdf"].exists || app.staticTexts["Train times.pdf"].exists, "its name is drawn on the chip") XCTAssertFalse(app.descendants(matching: .any)["chat-attached"].exists, "nothing waits in the box") Thread.sleep(forTimeInterval: 1) keep("wa-document-\(shots)-sent") } // RULE: a file picked with File goes to WhatsApp as a document under its own name, shown at once, "Waiting to send". @MainActor func testADocumentShowsAtOnceUnderItsNameWaitingToSend() { atOnce([], "default") } @MainActor func testADocumentShowsAtOnceUnderItsNameWaitingToSendAtTheLargestText() { atOnce(Self.largestText, "largest") } // MARK: a refused document goes back in the box @MainActor private func refused(_ extra: [String], name: String, why: String, _ shots: String) { let app = open(extra) let field = app.textViews["chat-composer-text"] field.tap() XCTAssertTrue(app.keyboards.firstMatch.waitForExistence(timeout: 10), "the field takes the keyboard") field.typeText(words) pickFile(in: app) 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: \(why)") let box = app.descendants(matching: .any)["chat-attached"].firstMatch XCTAssertTrue(box.waitForExistence(timeout: 5), "the file is back in the box") XCTAssertTrue(box.descendants(matching: .any).matching(NSPredicate(format: "label CONTAINS %@", name)).firstMatch.exists, "under the name it was picked by") XCTAssertEqual(field.value as? String, words, "the words being written stay in the field") XCTAssertFalse(pending([name], in: app).exists, "nothing was sent") XCTAssertTrue(app.buttons["chat-composer-send"].isEnabled, "a second tap sends them") keep("wa-document-\(shots)") } // RULE: a document over WhatsApp's 100 MB is refused in the core's words, and goes back in the box. @MainActor func testADocumentOverWhatsAppsLimitIsRefusedAndKept() { refused(["-chat-warm-pick-file", "large"], name: "Hill line map.pdf", why: "too large to send to WhatsApp", "default-too-large") } @MainActor func testADocumentOverWhatsAppsLimitIsRefusedAndKeptAtTheLargestText() { refused(["-chat-warm-pick-file", "large"] + Self.largestText, name: "Hill line map.pdf", why: "too large to send to WhatsApp", "largest-too-large") } // RULE (44cc5e5989): where the server's outbox has no file_name yet, a document is refused in the core's words // ("Only pictures, videos and voice notes go to WhatsApp for now."), and goes back in the box. @MainActor func testADocumentOnAnOutboxOfBeforeIsRefusedAndKept() { refused(["-chat-warm-pick-file", "YES", "-chat-warm-wa-no-documents", "YES"], name: "Train times.pdf", why: "Only pictures, videos and voice notes go to WhatsApp for now.", "default-not-yet") } @MainActor func testADocumentOnAnOutboxOfBeforeIsRefusedAndKeptAtTheLargestText() { refused(["-chat-warm-pick-file", "YES", "-chat-warm-wa-no-documents", "YES"] + Self.largestText, name: "Train times.pdf", why: "Only pictures, videos and voice notes go to WhatsApp for now.", "largest-not-yet") } }