Cotton code
120 lines · 6.3 KB · SwiftRaw
// Comms' safety number over Chat Dev's invented people (CONTRACT.md "Safety// number"; the core's own answers between invented phones, written by// write_chat_warm_fixture): the person's page opens the screen, a scan is fed// by `-chat-safety-scan` (the simulator has no camera), and Asha's chat says// once that her safety number changed. Each at the default size and the largest.import XCTestfinal class ChatSafetyTests: XCTestCase {    private let asha = "11111111-1111-4111-8111-0000000000a1"    private func launched(_ extra: [String], large: Bool) -> XCUIApplication {        let app = XCUIApplication()        app.launchArguments += ["-chat-warm-seed", "YES"] + extra        if large { app.launchArguments += ["-UIPreferredContentSizeCategoryName", "UICTContentSizeCategoryAccessibilityXXXL"] }        app.launch()        return app    }    private func element(_ app: XCUIApplication, _ id: String) -> XCUIElement {        app.descendants(matching: .any)[id].firstMatch    }    /// Scrolled into reach, then tapped.    private func tap(_ app: XCUIApplication, _ id: String) {        let found = element(app, id)        XCTAssertTrue(found.waitForExistence(timeout: 30), "\(id): " + String(app.debugDescription.prefix(3000)))        for _ in 0..<6 where !found.isHittable { app.swipeUp() }        found.tap()    }    /// Asha's page, then her safety number.    private func safetyScreen(_ extra: [String] = [], large: Bool) -> XCUIApplication {        let app = launched(["-chat-warm-open", asha, "-chat-warm-person", "YES"] + extra, large: large)        tap(app, "chat-person-safety")        XCTAssertTrue(element(app, "chat-safety-scan").waitForExistence(timeout: 15), "the screen opened: " + String(app.debugDescription.prefix(3000)))        return app    }    // RULE: the person's page has Safety number, and it opens the screen: twelve groups of five digits and a code.    func testThePersonsPageOpensTheSafetyNumber() {        for large in [false, true] {            let app = safetyScreen(large: large)            for i in 0..<12 {                let group = element(app, "chat-safety-group-\(i)")                XCTAssertTrue(group.exists, "group \(i) (large: \(large))")                XCTAssertEqual(group.label.count, 5, "five digits: \(group.label)")                XCTAssertTrue(group.label.allSatisfy(\.isNumber), group.label)            }            XCTAssertFalse(element(app, "chat-safety-group-12").exists, "twelve, no more")            XCTAssertTrue(element(app, "chat-safety-code").exists, "the code, drawn")            XCTAssertTrue(element(app, "chat-safety-check-again").exists, "verified before the change: asked to compare again")            app.terminate()        }    }    // RULE: scanning their phone's code marks them verified, and the person's page shows the mark.    func testAMatchingScanVerifiesThem() {        for large in [false, true] {            let app = safetyScreen(["-chat-safety-scan", "theirs"], large: large)            XCTAssertFalse(element(app, "chat-safety-verified").exists, "not verified yet")            tap(app, "chat-safety-scan")            let said = element(app, "chat-safety-said")            XCTAssertTrue(said.waitForExistence(timeout: 10))            XCTAssertTrue(said.label.contains("The numbers match"), said.label)            XCTAssertTrue(element(app, "chat-safety-verified").waitForExistence(timeout: 10), "verified, above the code")            XCTAssertFalse(element(app, "chat-safety-check-again").exists, "nothing left to compare")            XCTAssertTrue(element(app, "chat-safety-verify").label.contains("Clear verified"))            tap(app, "chat-safety-back")            let row = element(app, "chat-person-safety")            XCTAssertTrue(row.waitForExistence(timeout: 10))            // The page reads chat.contact again once the screen closes.            for _ in 0..<20 where !row.label.contains("Verified") { Thread.sleep(forTimeInterval: 0.5) }            XCTAssertTrue(row.label.contains("Verified"), "the page shows the mark: \(row.label)")            app.terminate()        }    }    // RULE: a code from their phone before it changed says the numbers don't match, and offers Scan again.    func testAWrongCodeSaysNotMatched() {        for large in [false, true] {            let app = safetyScreen(["-chat-safety-scan", "old"], large: large)            tap(app, "chat-safety-scan")            let said = element(app, "chat-safety-said")            XCTAssertTrue(said.waitForExistence(timeout: 10))            XCTAssertTrue(said.label.contains("don't match"), said.label)            XCTAssertFalse(element(app, "chat-safety-verified").exists, "not verified")            XCTAssertTrue(element(app, "chat-safety-scan").label.contains("Scan again"))            app.terminate()        }    }    // RULE: another chat's code says so.    func testAnotherChatsCodeSaysSo() {        let app = safetyScreen(["-chat-safety-scan", "other"], large: false)        tap(app, "chat-safety-scan")        let said = element(app, "chat-safety-said")        XCTAssertTrue(said.waitForExistence(timeout: 10))        XCTAssertTrue(said.label.contains("different chat"), said.label)    }    // RULE: the key change is said once in the chat, quietly, and a tap on it opens the safety number.    func testTheKeyChangeLineShowsOnce() {        for large in [false, true] {            let app = launched(["-chat-warm-open", asha], large: large)            let thread = app.tables["chat-thread"]            XCTAssertTrue(thread.waitForExistence(timeout: 30))            let lines = app.descendants(matching: .any).matching(identifier: "chat-safety-line")            for _ in 0..<6 where lines.count == 0 { thread.swipeDown(velocity: .slow) }            XCTAssertEqual(lines.count, 1, "said once: " + String(app.debugDescription.prefix(3000)))            let line = lines.firstMatch            XCTAssertTrue(line.label.hasSuffix("safety number changed"), line.label)            XCTAssertTrue(line.label.hasPrefix("Asha Verma"), "the name this phone knows her by: \(line.label)")            for _ in 0..<4 where !line.isHittable { thread.swipeDown(velocity: .slow) }            line.tap()            XCTAssertTrue(element(app, "chat-safety-scan").waitForExistence(timeout: 15), "the line opens the safety number")            app.terminate()        }    }}