Cotton code
107 lines · 6.1 KB · SwiftRaw
// Comms, the same reviewer pass on the iPhone (chat CONTRACT.md "Comms, the same reviewer pass", 24 Nov 2026, as// Android): a new account's Chats says how to begin under Cotton's own Downloads chat, and Calls › Record says why it// wants the microphone before the phone asks, or offers Open Settings once the phone refused; Not now beside either,// nothing opened unasked. Over Comms Dev's invented data (-chat-warm-seed YES), at the default size and the largest.import XCTestfinal class ChatReviewerPassTests: XCTestCase {    private static let largestText = ["-UIPreferredContentSizeCategoryName", "UICTContentSizeCategoryAccessibilityXXXL"]    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 launched(_ extra: [String]) -> XCUIApplication {        let app = XCUIApplication()        app.launchArguments += ["-chat-warm-seed", "YES"] + extra        app.launch()        return app    }    // MARK: an empty list says how to begin    @MainActor private func fresh(_ extra: [String], _ shots: String) {        let app = launched(["-chat-warm-fresh", "YES"] + extra)        let downloads = app.descendants(matching: .any)["chat-row-downloads"].firstMatch        XCTAssertTrue(downloads.waitForExistence(timeout: 30), "Cotton's own Downloads chat stands")        _ = ShellCard.refuse(app).waitForExistence(timeout: 6)        let words = app.descendants(matching: .any)["chat-empty"].firstMatch        XCTAssertTrue(words.waitForExistence(timeout: 5), "the list says how to begin")        XCTAssertEqual(words.label, "No chats yet. Tap the pen to write to someone.", "in the core's words")        XCTAssertGreaterThanOrEqual(words.frame.minY, downloads.frame.maxY - 1, "under the Downloads row, never over it")        XCTAssertTrue(words.isHittable || words.frame.maxY <= app.windows.firstMatch.frame.maxY, "on the screen: \(words.frame)")        if shots == "default" {            // Downloads' picture is Cotton's mark, a round of ink, never a blank round of tint (as Android): above its            // middle knot, between the knots, the round is ink.            let image = downloads.screenshot().image            let side: CGFloat = 54, u = side / 2 / 12 * 0.92            let at = CGPoint(x: 18 + side / 2, y: downloads.frame.height / 2 - 8 * u)            XCTAssertLessThan(lightness(image, at), 0.5, "Downloads draws Cotton's mark, not a blank round")        }        keep("reviewer-\(shots)-fresh")    }    /// How light the picture is at a point (in points), 0 dark to 1 light.    private func lightness(_ image: UIImage, _ point: CGPoint) -> CGFloat {        guard let cg = image.cgImage else { return 1 }        let scale = CGFloat(cg.width) / image.size.width        var pixel = [UInt8](repeating: 0, count: 4)        let context = CGContext(data: &pixel, width: 1, height: 1, bitsPerComponent: 8, bytesPerRow: 4,                                space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)!        context.draw(cg, in: CGRect(x: -point.x * scale, y: -(CGFloat(cg.height) - point.y * scale), width: CGFloat(cg.width), height: CGFloat(cg.height)))        return (0.299 * CGFloat(pixel[0]) + 0.587 * CGFloat(pixel[1]) + 0.114 * CGFloat(pixel[2])) / 255    }    // RULE: with nothing but Downloads, Chats says how to begin, under the Downloads row.    @MainActor func testANewAccountsChatsSaysHowToBegin() { fresh([], "default") }    @MainActor func testANewAccountsChatsSaysHowToBeginAtTheLargestText() { fresh(Self.largestText, "largest") }    // MARK: Record's microphone    @MainActor private func record(_ state: String, line: String, go: String, _ extra: [String], _ shots: String) {        let app = launched(["-chat-warm-place", "calls", "-chat-warm-mic", state] + extra)        let record = app.buttons["calls-record"]        XCTAssertTrue(record.waitForExistence(timeout: 30), "Calls offers Record")        _ = ShellCard.refuse(app).waitForExistence(timeout: 6)        record.tap()        let said = app.staticTexts["record-ask-line"]        XCTAssertTrue(said.waitForExistence(timeout: 5), "a card before anything is asked of the phone")        XCTAssertEqual(said.label, line, "in the core's words")        XCTAssertEqual(app.buttons["look-ask-record-ask-go"].label, go)        XCTAssertEqual(app.buttons["look-ask-record-ask-not-now"].label, "Not now")        for button in ["look-ask-record-ask-go", "look-ask-record-ask-not-now"] {            XCTAssertTrue(app.buttons[button].isHittable, "\(button) can be tapped")        }        keep("reviewer-\(shots)-record-\(state)")        // Not now: the card goes, nothing is asked and nothing opens.        app.buttons["look-ask-record-ask-not-now"].tap()        XCTAssertTrue(said.waitForNonExistence(timeout: 5), "Not now closes it")        XCTAssertEqual(app.state, .runningForeground, "Settings was not opened")        XCTAssertTrue(record.isHittable, "back on Calls")    }    // RULE: before the phone has asked, Record says why it wants the microphone, with Continue and Not now.    @MainActor func testRecordSaysWhyBeforeThePhoneAsks() {        record("notDetermined", line: "Comms uses the microphone only while you record, to keep the recording and turn it into words.",               go: "Continue", [], "default")    }    @MainActor func testRecordSaysWhyBeforeThePhoneAsksAtTheLargestText() {        record("notDetermined", line: "Comms uses the microphone only while you record, to keep the recording and turn it into words.",               go: "Continue", Self.largestText, "largest")    }    // RULE: once the phone refused, Record says so with Open Settings and Not now, and opens nothing unasked.    @MainActor func testRecordRefusedOffersOpenSettings() {        record("denied", line: "Comms needs the microphone to record. Allow it in Settings.", go: "Open Settings", [], "default")    }}