Cotton code
213 lines · 10.4 KB · SwiftRaw
// Comms on iPhone: voice messages (owner's feedback item 5 and H, Comms// build 19, 22 Nov 2026: there was no way to send a voice note, and two voice// buttons stood in the writing box). Over invented chats (-chat-warm-seed// YES), the microphone held records an invented recording (-chat-warm-voice// YES; the simulator has no microphone): held, the writing box shows the// recording; slid away, nothing goes; let go, the voice message lands at once// and the words its phone heard follow under the player. A tap on the same// microphone still starts voice to text, and it is the only voice control.// Shots fb-voice-*.import XCTestfinal class ChatVoiceTests: XCTestCase {    private let asha = "11111111-1111-4111-8111-0000000000a1"    private let meera = "11111111-1111-4111-8111-0000000000e1"    private static let largestText = ["-UIPreferredContentSizeCategoryName", "UICTContentSizeCategoryAccessibilityXXXL"]    /// What the invented recording says (ChatWarmSeed.voiceWords).    private let recorded = "Running ten minutes late"    /// The words under Meera's voice message (ChatWarmSeed.heardWords).    private let heard = "The invented market opens at nine"    override func setUp() {        continueAfterFailure = false        XCUIDevice.shared.orientation = .portrait    }    func keepMidway(_ name: String) { keep(name) }    private func keep(_ name: String) {        let shot = XCTAttachment(screenshot: XCUIScreen.main.screenshot())        shot.name = name        shot.lifetime = .keepAlways        add(shot)    }    @MainActor private func open(_ thread: String, _ extra: [String]) -> XCUIApplication {        let app = XCUIApplication()        app.launchArguments += ["-chat-warm-seed", "YES", "-chat-warm-voice", "YES", "-chat-warm-open", thread] + extra        app.launch()        XCTAssertTrue(app.descendants(matching: .any)["chat-composer"].firstMatch.waitForExistence(timeout: 30), "the conversation opened")        // 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    }    /// Every voice control in the writing box.    @MainActor private func voiceControls(_ app: XCUIApplication) -> [XCUIElement] {        let box = app.descendants(matching: .any)["chat-composer"].firstMatch        let ids = NSPredicate(format: "identifier CONTAINS[c] 'mic' OR identifier CONTAINS[c] 'voice' OR identifier CONTAINS[c] 'record'")        return box.buttons.matching(ids).allElementsBoundByIndex.filter(\.exists)    }    // MARK: Hold, slide, release    @MainActor private func holdSlideRelease(_ extra: [String], _ shots: String) {        let app = open(asha, extra)        let mic = app.buttons["chat-composer-mic"]        XCTAssertTrue(mic.waitForExistence(timeout: 10), "the microphone stands in the empty writing box")        keep("fb-voice-\(shots)-rest")        // Held: the writing box shows the recording (its time) while the finger stays down. The press        // blocks this thread but runs its run loop, so a timer looks in the middle of it.        let recording = app.descendants(matching: .any)["chat-composer-recording"].firstMatch        let midway = Midway(test: self, app: app, recording: recording, shot: "fb-voice-\(shots)-holding")        let look = Timer(timeInterval: 2, repeats: false) { _ in MainActor.assumeIsolated { midway.look() } }        RunLoop.main.add(look, forMode: .common)        mic.press(forDuration: 3.5)        look.invalidate()        XCTAssertTrue(midway.seen, "holding the microphone shows the recording")        XCTAssertTrue(midway.timed, "the recording's time stands in the box")        // Let go: the voice message lands at once (item 2), the box is back at rest.        let sent = app.descendants(matching: .any)["chat-bubble-seed-sent-1"].firstMatch        XCTAssertTrue(sent.waitForExistence(timeout: 2), "the voice message landed the moment it was let go")        XCTAssertFalse(recording.exists, "the box is back at rest")        XCTAssertTrue(sent.label.contains("Voice message"), "the bubble is a voice message: \(sent.label)")        let box = app.descendants(matching: .any)["chat-composer"].firstMatch        XCTAssertLessThanOrEqual(sent.frame.maxY, box.frame.minY + 1, "the voice message stands above the writing box")        keep("fb-voice-\(shots)-sent")        // Its words follow, under the player (read after the mark).        let worded = NSPredicate(format: "label CONTAINS %@", recorded)        expectation(for: worded, evaluatedWith: sent)        waitForExpectations(timeout: 15)        let said = sent.label        if let mark = said.range(of: "Voice message"), let words = said.range(of: recorded) {            XCTAssertLessThan(mark.lowerBound, words.lowerBound, "the words are read after the voice message: \(said)")        }        Thread.sleep(forTimeInterval: 1.5)        keep("fb-voice-\(shots)-words")        // Held again and slid away before letting go: nothing goes.        let plus = app.buttons["chat-attach"]        mic.press(forDuration: 1.2, thenDragTo: plus, withVelocity: .slow, thenHoldForDuration: 0.4)        Thread.sleep(forTimeInterval: 2)        XCTAssertFalse(app.descendants(matching: .any)["chat-bubble-seed-sent-2"].exists, "a recording slid away sends nothing")        XCTAssertFalse(recording.exists, "the box is back at rest after the slide")        keep("fb-voice-\(shots)-slid")    }    // RULE (owner, item 5): holding the microphone records, sliding away cancels, letting go sends at once with the words after.    @MainActor func testHoldRecordsSlideCancelsReleaseSends() {        holdSlideRelease([], "default")    }    // RULE: the same at the largest text.    @MainActor func testHoldRecordsSlideCancelsReleaseSendsAtTheLargestText() {        holdSlideRelease(Self.largestText, "largest")    }    // MARK: One control; a tap dictates    @MainActor private func oneControlTapDictates(_ extra: [String], _ shots: String) {        let app = open(asha, extra)        let mic = app.buttons["chat-composer-mic"]        XCTAssertTrue(mic.waitForExistence(timeout: 10))        // H: one voice control, never a waveform button beside the plus.        XCTAssertFalse(app.buttons["chat-voice"].exists, "the second voice button is gone")        XCTAssertEqual(voiceControls(app).map(\.identifier), ["chat-composer-mic"], "one voice control in the empty box")        // A tap is voice to text: the dictation's row, the system asking for the microphone for it (allowed), or, the        // microphone refused before, the microphone saying why; never a voice message.        let idle = mic.label        mic.tap()        let dictating = app.buttons["chat-composer-cancel"]        let asking = XCUIApplication(bundleIdentifier: "com.apple.springboard").alerts.firstMatch        var started = false, shot = false        for _ in 0..<20 {            if dictating.exists || asking.exists || mic.label != idle {                started = true                if !shot { keep("fb-voice-\(shots)-tap"); shot = true }            }            if asking.exists {                let allow = asking.buttons["Allow"]                (allow.exists ? allow : asking.buttons.element(boundBy: asking.buttons.count - 1)).tap()            } else if dictating.exists || mic.label != idle {                break            }            Thread.sleep(forTimeInterval: 0.5)        }        XCTAssertTrue(started, "a tap on the microphone starts voice to text")        if dictating.exists { dictating.tap() }        Thread.sleep(forTimeInterval: 1)        XCTAssertFalse(app.descendants(matching: .any)["chat-bubble-seed-sent-1"].exists, "a tap sends no voice message")        // Something written: the microphone still dictates beside Send, and it is still the one voice control.        let field = app.textViews["chat-composer-text"]        field.tap()        field.typeText("On my way")        XCTAssertEqual(voiceControls(app).map(\.identifier), ["chat-composer-mic"], "one voice control with words written")        XCTAssertTrue(app.buttons["chat-composer-send"].isEnabled)        keep("fb-voice-\(shots)-written")    }    // RULE (owner, H): one voice control; a tap on it keeps voice to text.    @MainActor func testOneVoiceControlAndATapDictates() {        oneControlTapDictates([], "default")    }    // RULE: the same at the largest text.    @MainActor func testOneVoiceControlAndATapDictatesAtTheLargestText() {        oneControlTapDictates(Self.largestText, "largest")    }    // MARK: Received    @MainActor private func receivedWords(_ extra: [String], _ shots: String) {        let app = open(meera, extra)        let theirs = app.descendants(matching: .any)["chat-bubble-m-media-4"].firstMatch        XCTAssertTrue(theirs.waitForExistence(timeout: 10), "Meera's voice message stands in her chat")        let said = theirs.label        let mark = said.range(of: "Voice message"), words = said.range(of: heard)        XCTAssertNotNil(mark, "it is a voice message: \(said)")        XCTAssertNotNil(words, "its words are with it: \(said)")        if let mark, let words { XCTAssertLessThan(mark.lowerBound, words.lowerBound, "the words come after the player: \(said)") }        let box = app.descendants(matching: .any)["chat-composer"].firstMatch        XCTAssertLessThanOrEqual(theirs.frame.maxY, box.frame.minY + 1, "it stands above the writing box")        keep("fb-voice-\(shots)-received")    }    // RULE (owner, item 5): a voice message received with its words shows them under the player.    @MainActor func testAReceivedVoiceMessageShowsItsWords() {        receivedWords([], "default")    }    // RULE: the same at the largest text.    @MainActor func testAReceivedVoiceMessageShowsItsWordsAtTheLargestText() {        receivedWords(Self.largestText, "largest")    }}/// What a look in the middle of a held press saw (the timer's block runs on the main run loop the press spins).private final class Midway: @unchecked Sendable {    let test: ChatVoiceTests    let app: XCUIApplication    let recording: XCUIElement    let shot: String    var seen = false    var timed = false    init(test: ChatVoiceTests, app: XCUIApplication, recording: XCUIElement, shot: String) {        self.test = test        self.app = app        self.recording = recording        self.shot = shot    }    @MainActor func look() {        seen = recording.exists        timed = app.staticTexts["chat-composer-record-time"].exists        test.keepMidway(shot)    }}