// The voice-message player, finished (the owner's feedback item 2, Comms// build 23, 23 Nov 2026: voice notes played, but the player looked unfinished;// drawn in assets/design/chat/voice-player.html). Over Chat Dev's invented// chats (-chat-warm-seed YES, the core's answers from write_chat_warm_fixture):// Ravi's chat holds invented notes of known lengths. Play shows pause and the// length counts down from the player's real place; the speed goes 1×, 1.5×,// 2× and is kept when the chat is opened again; his note not played shows so,// and is played once it has played a second; a note whose file can't be// fetched says so in the core's words, with Try again. At the default size and// the largest, and at night. Shots voice-player-*.import XCTestfinalclassChatVoicePlayerTests: XCTestCase {privatelet ravi = "11111111-1111-4111-8111-0000000000d1"privatestaticlet largestText = ["-UIPreferredContentSizeCategoryName", "UICTContentSizeCategoryAccessibilityXXXL"]overridefunc setUp() { continueAfterFailure = false XCUIDevice.shared.orientation = .portrait }overridefunc tearDown() { XCUIDevice.shared.appearance = .light }privatefunc keep(_ name: String) {let shot = XCTAttachment(screenshot: XCUIScreen.main.screenshot()) shot.name = name shot.lifetime = .keepAlways add(shot) } @MainActor privatefunc opened(_ extra: [String]) -> XCUIApplication {let app = XCUIApplication() app.launchArguments += ["-chat-warm-seed", "YES", "-chat-warm-open", ravi] + extra app.launch() XCTAssertTrue(app.tables["chat-thread"].waitForExistence(timeout: 30), "Ravi's chat 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 } @MainActor privatefunc round(_ id: String, _ app: XCUIApplication) -> XCUIElement { app.buttons["chat-voice-play-\(id)"] } @MainActor privatefunc element(_ id: String, _ app: XCUIApplication) -> XCUIElement { app.descendants(matching: .any)[id].firstMatch }/// Brings an element into view: the chat is flipped (newest at the foot), so a pull down reads back. @MainActor privatefunc reach(_ element: XCUIElement, in app: XCUIApplication) { XCTAssertTrue(element.waitForExistence(timeout: 10), "\(element) stands")let table = app.tables["chat-thread"]// Short pulls toward the middle of the screen, until it stands clear of the header and the writing box.for _ in0..<16 {let screen = app.windows.firstMatch.framelet frame = element.frame// Below the header and the "Not on Cotton Comms yet" card (both tall at the largest text).let reachCard = app.scrollViews["chat-reach"]let top = max(screen.minY + 150, reachCard.exists ? reachCard.frame.maxY + 8 : 0)if element.isHittable, frame.minY > top, frame.maxY < screen.maxY - 170 { break }let from = table.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5))let down = !element.exists || frame.midY < screen.midYlet to = from.withOffset(CGVector(dx: 0, dy: down ? 180 : -180)) from.press(forDuration: 0.05, thenDragTo: to, withVelocity: .slow, thenHoldForDuration: 0.3) }// Still: a tap on a table still moving only stops it.var last = element.framefor _ in0..<10 {Thread.sleep(forTimeInterval: 0.25)let now = element.frameif now == last { break } last = now } XCTAssertTrue(element.isHittable, "\(element) in reach") }/// The seconds a value like "0:39 left" says.privatefunc seconds(_ value: String) -> Int? {guardlet time = value.split(separator: " ").first, time.contains(":") else { returnnil }let parts = time.split(separator: ":").compactMap { Int($0) }return parts.count == 2 ? parts[0] * 60 + parts[1] : nil }privatefunc value(_ element: XCUIElement) -> String { element.value as? String ?? "" }// MARK: Play, pause, the countdown @MainActor privatefunc playCountsDown(_ extra: [String], _ shots: String) {let app = opened(extra)let play = round("m-r3", app) reach(play, in: app) XCTAssertEqual(play.label, "Play voice message") XCTAssertEqual(value(play), "0:42, not played", "at rest: the length whole, and not played") keep("voice-player-\(shots)-rest") play.tap()let pausing = NSPredicate(format: "label == %@", "Pause") expectation(for: pausing, evaluatedWith: play) waitForExpectations(timeout: 10)Thread.sleep(forTimeInterval: 0.6)let first = seconds(value(play)) ?? 0 XCTAssertTrue(value(play).hasSuffix("left"), "playing, the round says what is left: \(value(play))") XCTAssertTrue(element("chat-voice-speed-m-r3", app).exists, "the speed shows once it plays") XCTAssertTrue(element("chat-voice-position-m-r3", app).exists, "the place is adjustable once it plays") keep("voice-player-\(shots)-playing")// The drawing itself moves, not only what VoiceOver reads (Comms 25: "the UI is not updated").let bars = element("chat-voice-position-m-r3", app)let drawnFirst = bars.screenshot().pngRepresentationThread.sleep(forTimeInterval: 2.5)let later = seconds(value(play)) ?? 99 XCTAssertLessThan(later, first, "the countdown moves: \(first) then \(later)") XCTAssertGreaterThan(later, 30, "from the player's place, not the clock's: \(later)") XCTAssertNotEqual(bars.screenshot().pngRepresentation, drawnFirst, "the bars fill as it plays") play.tap() expectation(for: NSPredicate(format: "label == %@", "Play voice message"), evaluatedWith: play) waitForExpectations(timeout: 5)let paused = seconds(value(play)) ?? 0let drawnPaused = bars.screenshot().pngRepresentationThread.sleep(forTimeInterval: 1.5) XCTAssertEqual(seconds(value(play)), paused, "paused keeps its place") XCTAssertEqual(bars.screenshot().pngRepresentation, drawnPaused, "paused, the bars stand still") keep("voice-player-\(shots)-paused")// Another note starts: the first keeps its place.let mine = round("m-r4", app) reach(mine, in: app) mine.tap() expectation(for: pausing, evaluatedWith: mine) waitForExpectations(timeout: 10) keep("voice-player-\(shots)-mine-playing") reach(play, in: app) XCTAssertEqual(seconds(value(play)), paused, "the first note kept its place when another started: \(value(play))") }// RULE: play shows pause, and the length counts down from the player's real place; pause keeps it. @MainActor func testPlayShowsPauseAndTheCountdownMoves() { playCountsDown([], "default") } @MainActor func testPlayShowsPauseAndTheCountdownMovesAtTheLargestText() { playCountsDown(Self.largestText, "largest") } @MainActor func testPlayShowsPauseAndTheCountdownMovesAtNight() { XCUIDevice.shared.appearance = .dark playCountsDown([], "night") }// MARK: Seeking (owner, Comms 26: "seeking restarts the note from the beginning") @MainActor privatefunc seeks(_ extra: [String], _ shots: String) {let app = opened(extra)let play = round("m-r3", app) reach(play, in: app) play.tap() expectation(for: NSPredicate(format: "label == %@", "Pause"), evaluatedWith: play) waitForExpectations(timeout: 10)let bars = element("chat-voice-position-m-r3", app) XCTAssertTrue(bars.waitForExistence(timeout: 5))// A tap three quarters along: about 10 of its 42 seconds left, and it plays on from there. bars.coordinate(withNormalizedOffset: CGVector(dx: 0.75, dy: 0.5)).tap()Thread.sleep(forTimeInterval: 0.5)let tapped = seconds(value(play)) ?? 99 XCTAssertLessThanOrEqual(tapped, 14, "the place moved to where he tapped: \(value(play))")Thread.sleep(forTimeInterval: 1.5) XCTAssertEqual(play.label, "Pause", "still playing after the tap")let after = seconds(value(play)) ?? 99 XCTAssertLessThanOrEqual(after, tapped, "it plays on from the tap, not from the start: \(tapped) then \(after)") keep("voice-player-\(shots)-seek-tap")// A drag back along the bars to a quarter: about 31 seconds left, still playing on from there.let from = bars.coordinate(withNormalizedOffset: CGVector(dx: 0.7, dy: 0.5)) from.press(forDuration: 0.05, thenDragTo: bars.coordinate(withNormalizedOffset: CGVector(dx: 0.25, dy: 0.5)), withVelocity: .slow, thenHoldForDuration: 0.2)Thread.sleep(forTimeInterval: 0.5)let dragged = seconds(value(play)) ?? 0 XCTAssertTrue((26...36).contains(dragged), "the place follows the drag: \(value(play))")Thread.sleep(forTimeInterval: 1.5) XCTAssertEqual(play.label, "Pause", "still playing after the drag")let later = seconds(value(play)) ?? 99 XCTAssertTrue((22...dragged).contains(later), "it plays on from the drag: \(dragged) then \(later)")// Paused, a tap moves the place; play goes on from there. play.tap() expectation(for: NSPredicate(format: "label == %@", "Play voice message"), evaluatedWith: play) waitForExpectations(timeout: 5) bars.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5)).tap()Thread.sleep(forTimeInterval: 0.5)let set = seconds(value(play)) ?? 0 XCTAssertTrue((17...25).contains(set), "paused, the place moves to the tap: \(value(play))") play.tap() expectation(for: NSPredicate(format: "label == %@", "Pause"), evaluatedWith: play) waitForExpectations(timeout: 10)Thread.sleep(forTimeInterval: 1.5)let resumed = seconds(value(play)) ?? 99 XCTAssertTrue((12...set).contains(resumed), "play goes on from the place set, not the start: \(set) then \(resumed)") keep("voice-player-\(shots)-seek-paused") }// RULE: a tap or a drag along the bars moves the place, playing or paused, and it plays on from there. @MainActor func testSeekingMovesThePlaceAndPlaysOnFromThere() { seeks([], "default") } @MainActor func testSeekingMovesThePlaceAndPlaysOnFromThereAtTheLargestText() { seeks(Self.largestText, "largest") }// RULE: a player that stands still a moment as its place moves (a phone's does; the simulator's does not) has not// ended the note: it plays on from the new place, never from the start (Comms 26, B5). @MainActor func testSeekingWhereThePlayerStallsStillPlaysOnFromThere() { seeks(["-chat-voice-seek-stalls", "YES"], "stalls") }// MARK: Speed @MainActor privatefunc speedIsKept(_ extra: [String], _ shots: String) {let app = opened(extra)let mine = round("m-r4", app) reach(mine, in: app) mine.tap() expectation(for: NSPredicate(format: "label == %@", "Pause"), evaluatedWith: mine) waitForExpectations(timeout: 10)let pill = app.buttons["chat-voice-speed-m-r4"] XCTAssertTrue(pill.waitForExistence(timeout: 5)) XCTAssertEqual(pill.label, "Playback speed") XCTAssertEqual(value(pill), "1 times") pill.tap() XCTAssertEqual(value(pill), "1.5 times") keep("voice-player-\(shots)-speed-1.5") pill.tap() XCTAssertEqual(value(pill), "2 times") pill.tap() XCTAssertEqual(value(pill), "1 times", "round again") pill.tap() XCTAssertEqual(value(pill), "1.5 times") XCTAssertEqual(mine.label, "Pause", "the note keeps playing through the change")// The chat left and opened again: the speed stands.let back = element("chat-back", app) XCTAssertTrue(back.waitForExistence(timeout: 5)) back.tap()let row = element("chat-row-\(ravi)", app) XCTAssertTrue(row.waitForExistence(timeout: 10), "back at the chats") row.tap() XCTAssertTrue(app.tables["chat-thread"].waitForExistence(timeout: 10))let again = round("m-r3", app) reach(again, in: app) again.tap()let pill2 = app.buttons["chat-voice-speed-m-r3"] XCTAssertTrue(pill2.waitForExistence(timeout: 10)) XCTAssertEqual(value(pill2), "1.5 times", "the speed chosen is kept for every note") keep("voice-player-\(shots)-speed-kept") }// RULE: the speed goes 1×, 1.5×, 2× and round, keeps the note playing, and is kept after the chat is opened again. @MainActor func testSpeedCyclesAndIsKeptAfterReopening() { speedIsKept([], "default") } @MainActor func testSpeedCyclesAndIsKeptAfterReopeningAtTheLargestText() { speedIsKept(Self.largestText, "largest") }// MARK: Played @MainActor privatefunc playedAfterASecond(_ extra: [String], _ shots: String) {let app = opened(extra)let play = round("m-r3", app) reach(play, in: app)let bubble = element("chat-bubble-m-r3", app) XCTAssertTrue(value(play).contains("not played"), "his note is not played yet: \(value(play))") XCTAssertTrue(bubble.label.contains("Voice message, 0:42, not played"), "read with its length and not played: \(bubble.label)") XCTAssertTrue(bubble.label.contains("Found the photos from Goa"), "and its words: \(bubble.label)") play.tap() expectation(for: NSPredicate(format: "label == %@", "Pause"), evaluatedWith: play) waitForExpectations(timeout: 10)Thread.sleep(forTimeInterval: 1.8) play.tap() XCTAssertFalse(bubble.label.contains("not played"), "played once it has played a second: \(bubble.label)") XCTAssertFalse(value(play).contains("not played")) keep("voice-player-\(shots)-played")// The chat opened again: the core keeps it played. element("chat-back", app).tap()let row = element("chat-row-\(ravi)", app) XCTAssertTrue(row.waitForExistence(timeout: 10)) row.tap()let again = round("m-r3", app) reach(again, in: app) XCTAssertEqual(value(again), "0:42", "played, at rest: the length whole, nothing more")// His very short note still waits.let short = round("m-r5", app) reach(short, in: app) XCTAssertEqual(value(short), "0:02, not played") }// RULE: a note of theirs shows not played until it has played a second; then it is played, and stays so. @MainActor func testAReceivedNoteIsUnplayedThenPlayedAfterOneSecond() { playedAfterASecond([], "default") } @MainActor func testAReceivedNoteIsUnplayedThenPlayedAfterOneSecondAtTheLargestText() { playedAfterASecond(Self.largestText, "largest") }// MARK: Failures @MainActor privatefunc offlineTriesAgain(_ extra: [String], _ shots: String) {let app = opened(extra)let play = round("m-r6", app) reach(play, in: app) XCTAssertEqual(value(play), "2:30, not played") play.tap()let retry = app.buttons["chat-voice-retry-m-r6"] XCTAssertTrue(retry.waitForExistence(timeout: 10), "a file that can't be fetched says so, with Try again") XCTAssertEqual(retry.label, "Try again") XCTAssertEqual(value(retry), "This file can't be fetched right now.", "the core's words") XCTAssertEqual(play.label, "Try again", "the round tries again too") keep("voice-player-\(shots)-offline")// Gone for good: faint, nothing to tap, the core's words.let gone = round("m-r7", app) reach(gone, in: app) XCTAssertEqual(gone.label, "Voice message not available") XCTAssertFalse(gone.isEnabled, "nothing to tap") XCTAssertTrue(element("chat-voice-gone-m-r7", app).exists) keep("voice-player-\(shots)-gone")// Tried again: refused again, in the same words, never a silent nothing. reach(retry, in: app) retry.tap() XCTAssertTrue(retry.waitForExistence(timeout: 10)) }// RULE: a note whose file can't be fetched shows Try again in the core's words; one gone for good is faint. @MainActor func testANoteThatCantBeFetchedShowsTryAgain() { offlineTriesAgain([], "default") } @MainActor func testANoteThatCantBeFetchedShowsTryAgainAtTheLargestText() { offlineTriesAgain(Self.largestText, "largest") } @MainActor func testANoteThatCantBeFetchedShowsTryAgainAtNight() { XCUIDevice.shared.appearance = .dark offlineTriesAgain([], "night") }}