// Editing and deleting his own message in a WhatsApp chat (FEEDBACK-3, // CONTRACT.md "Editing and deleting his own message"), over Chat Dev's invented // chats (-chat-warm-seed YES -chat-warm-wa-changes: the core's own answers on a // copy linked through an invented Mac, written by write_chat_warm_fixture). In // Tara's chat his text of two minutes ago offers Edit, "Delete for everyone" // and "Delete for me", as the bubble's actions say; his of hours ago no Edit; // hers Delete for me only. An edit goes through the writing box's edit strip // and its new words stand at once, "Edit waiting to send"; one the Mac could // not send says why, with Retry. A delete for everyone leaves "You deleted this // message", "Waiting to delete for everyone"; Delete for me takes the message // from his copy. Past WhatsApp's windows each is refused in the core's words. // At the default text size and the largest. Shots wa-edit-*. import XCTest final class ChatWhatsAppEditDeleteTests: XCTestCase { private let tara = "11111111-1111-4111-8111-0000000000f1" private static let largestText = ["-UIPreferredContentSizeCategoryName", "UICTContentSizeCategoryAccessibilityXXXL"] // As write_chat_warm_fixture wrote and asked them (invented). private let fresh = "m-t3", hours = "m-t4", hers = "m-t5" private let before = "Running ten minutes late", after = "Running fifteen minutes late" private let why = "WhatsApp did not take it: the Mac's link is not signed in" 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 bubble(_ id: String, in app: XCUIApplication) -> XCUIElement { app.descendants(matching: .any)["chat-bubble-\(id)"].firstMatch } @MainActor private func said(_ words: String, in app: XCUIApplication) -> XCUIElement { app.descendants(matching: .any).matching(NSPredicate(format: "label CONTAINS %@", words)).firstMatch } /// Brought into reach: the chat scrolled until it can be held. @MainActor private func reach(_ element: XCUIElement, in app: XCUIApplication) { let table = app.tables["chat-thread"] for _ in 0..<8 where !element.isHittable { table.swipeDown() } for _ in 0..<8 where !element.isHittable { table.swipeUp() } XCTAssertTrue(element.isHittable, "\(element) in reach") // A tap while the chat still glides after a swipe only stops it: let it come to rest first. Thread.sleep(forTimeInterval: 1.2) } /// Held: the menu the bubble's actions make. @MainActor private func hold(_ id: String, in app: XCUIApplication) { let held = bubble(id, in: app) XCTAssertTrue(held.waitForExistence(timeout: 10), "\(id) stands") reach(held, in: app) held.press(forDuration: 1.0) } @MainActor private func act(_ act: String, in app: XCUIApplication) -> XCUIElement { let button = app.buttons["chat-act-\(act)"] XCTAssertTrue(button.waitForExistence(timeout: 5), "\(act) is offered") return button } // MARK: the menu and an edit @MainActor private func edited(_ extra: [String], _ shots: String) { let app = open(["-chat-warm-wa-changes", "YES"] + extra) hold(fresh, in: app) XCTAssertEqual(act("delete", in: app).label, "Delete for everyone", "in the core's words") XCTAssertEqual(act("deleteMine", in: app).label, "Delete for me") let edit = act("edit", in: app) keep("wa-edit-\(shots)-menu") edit.tap() let strip = app.descendants(matching: .any)["chat-strip"].firstMatch XCTAssertTrue(strip.waitForExistence(timeout: 5), "the edit strip stands over the field") let field = app.textViews["chat-composer-text"] XCTAssertEqual(field.value as? String, before, "the field holds his words") field.typeText(String(repeating: XCUIKeyboardKey.delete.rawValue, count: before.count) + after) XCTAssertTrue(strip.exists, "the strip stays while writing") keep("wa-edit-\(shots)-writing") app.buttons["chat-composer-send"].tap() let mine = bubble(fresh, in: app) XCTAssertTrue(mine.waitForExistence(timeout: 5)) let label = mine.label XCTAssertTrue(label.contains(after), "the new words at once: \(label)") XCTAssertTrue(label.contains("Edit waiting to send"), "waiting for the Mac: \(label)") XCTAssertFalse(strip.exists, "the strip goes with the send") XCTAssertEqual(field.value as? String ?? "", "", "the field is empty again") Thread.sleep(forTimeInterval: 1) keep("wa-edit-\(shots)-sent") } // RULE: his fresh text offers Edit, "Delete for everyone" and "Delete for me"; an edit's new words stand at once, // "Edit waiting to send". @MainActor func testAnEditShowsAtOnceWaitingToSend() { edited([], "default") } @MainActor func testAnEditShowsAtOnceWaitingToSendAtTheLargestText() { edited(Self.largestText, "largest") } // MARK: not sent, and Retry @MainActor private func failed(_ extra: [String], _ shots: String) { let app = open(["-chat-warm-wa-changes", "failed"] + extra) let mine = bubble(fresh, in: app) XCTAssertTrue(mine.waitForExistence(timeout: 10)) reach(mine, in: app) XCTAssertTrue(mine.label.contains("Edit not sent: \(why)"), "why it did not go: \(mine.label)") let marker = said("You deleted this message", in: app) XCTAssertTrue(marker.exists, "the marker stands where his message was") XCTAssertTrue(marker.label.contains("Not deleted for everyone: \(why)"), marker.label) keep("wa-edit-\(shots)-failed") let again = app.buttons["chat-retry-\(fresh)"] XCTAssertTrue(again.exists, "Retry under the edit") reach(again, in: app) again.tap() XCTAssertTrue(said("Edit waiting to send", in: app).waitForExistence(timeout: 5), "the edit waits again") let retry = app.buttons.matching(NSPredicate(format: "identifier BEGINSWITH 'chat-retry-' AND label CONTAINS %@", "Not deleted for everyone")).firstMatch XCTAssertTrue(retry.exists, "Retry under the marker") reach(retry, in: app) retry.tap() XCTAssertTrue(said("Waiting to delete for everyone", in: app).waitForExistence(timeout: 5), "the delete waits again") keep("wa-edit-\(shots)-retried") } // RULE: an edit or a delete for everyone the Mac could not send says why, in the core's words, with Retry. @MainActor func testAChangeNotSentSaysWhyAndRetries() { failed([], "default") } @MainActor func testAChangeNotSentSaysWhyAndRetriesAtTheLargestText() { failed(Self.largestText, "largest") } // MARK: delete for everyone @MainActor private func deleted(_ extra: [String], _ shots: String) { let app = open(["-chat-warm-wa-changes", "YES"] + extra) hold(hours, in: app) XCTAssertFalse(app.buttons["chat-act-edit"].exists, "hours on, no Edit") act("delete", in: app).tap() let marker = said("You deleted this message", in: app) XCTAssertTrue(marker.waitForExistence(timeout: 5), "the marker stands in its place") XCTAssertTrue(marker.label.contains("Waiting to delete for everyone"), marker.label) XCTAssertFalse(bubble(hours, in: app).exists, "his message is gone") XCTAssertFalse(said("I'll wait by the bridge", in: app).exists, "its words with it") Thread.sleep(forTimeInterval: 1) keep("wa-edit-\(shots)-deleted") } // RULE: a delete for everyone leaves "You deleted this message", "Waiting to delete for everyone". @MainActor func testADeleteForEveryoneLeavesItsMarkerWaitingToSend() { deleted([], "default") } @MainActor func testADeleteForEveryoneLeavesItsMarkerWaitingToSendAtTheLargestText() { deleted(Self.largestText, "largest") } // MARK: delete for me @MainActor private func deletedForMe(_ extra: [String], _ shots: String) { let app = open(["-chat-warm-wa-changes", "YES"] + extra) hold(hers, in: app) XCTAssertFalse(app.buttons["chat-act-delete"].exists, "hers: no delete for everyone") XCTAssertFalse(app.buttons["chat-act-edit"].exists, "hers: no Edit") act("deleteMine", in: app).tap() let gone = bubble(hers, in: app) XCTAssertTrue(gone.waitForNonExistence(timeout: 5), "her message is gone from his copy") XCTAssertFalse(said("Platform 4, not 3", in: app).exists, "its words with it") Thread.sleep(forTimeInterval: 1) keep("wa-edit-\(shots)-deleted-for-me") } // RULE: Delete for me takes any message from his copy only, and leaves nothing in its place. @MainActor func testDeleteForMeTakesItFromHisCopy() { deletedForMe([], "default") } @MainActor func testDeleteForMeTakesItFromHisCopyAtTheLargestText() { deletedForMe(Self.largestText, "largest") } // MARK: too late @MainActor private func tooLate(_ extra: [String], _ shots: String) { let app = open(["-chat-warm-wa-changes", "YES", "-chat-warm-wa-late", "YES"] + extra) hold(fresh, in: app) act("edit", in: app).tap() let field = app.textViews["chat-composer-text"] XCTAssertTrue(app.descendants(matching: .any)["chat-strip"].firstMatch.waitForExistence(timeout: 5)) field.typeText(String(repeating: XCUIKeyboardKey.delete.rawValue, count: before.count) + after) app.buttons["chat-composer-send"].tap() XCTAssertTrue(said("It's too late to edit that message.", in: app).waitForExistence(timeout: 5), "the core's words") XCTAssertTrue(bubble(fresh, in: app).label.contains(before), "his words as they were") keep("wa-edit-\(shots)-too-late") Thread.sleep(forTimeInterval: 4) hold(hours, in: app) act("delete", in: app).tap() XCTAssertTrue(said("It's too late to delete that message for everyone.", in: app).waitForExistence(timeout: 5), "the core's words") XCTAssertTrue(bubble(hours, in: app).exists, "his message stays") keep("wa-edit-\(shots)-too-late-delete") } // RULE: past WhatsApp's windows (between the long press and the tap) the edit and the delete for everyone are // refused in the core's words (too_late, too_late_delete), and nothing changes. @MainActor func testPastWhatsAppsWindowsEachIsRefusedInTheCoresWords() { tooLate([], "default") } @MainActor func testPastWhatsAppsWindowsEachIsRefusedInTheCoresWordsAtTheLargestText() { tooLate(Self.largestText, "largest") } }