Cotton code
177 lines · 9.4 KB · SwiftRaw
// Chat Dev's invented chats (-chat-warm-seed YES, the core's own answers// over invented people) drawn as the owner saw filled imported WhatsApp// conversations on an iPhone (Cotton Chat build 14): the floating header// stands on its own ground above the rows while they scroll; a picture the// store does not have says so; a file's name is never a caption; their// picture stands once over their words; a call record is a quiet centred// line. Read from the screen itself where a look is the point.import XCTestfinal class ChatImportedLookTests: XCTestCase {    private let hillTrip = "11111111-1111-4111-8111-0000000000b1"    private func launched(_ extra: [String]) -> XCUIApplication {        let app = XCUIApplication()        app.launchArguments += ["-chat-warm-seed", "YES"] + extra        app.launch()        return app    }    private func keep(_ name: String) {        let shot = XCTAttachment(screenshot: XCUIScreen.main.screenshot())        shot.name = name        shot.lifetime = .keepAlways        add(shot)    }    /// The screen's pixels, read at points.    private struct Pixels {        let bytes: [UInt8]        let width: Int        let height: Int        let scale: CGFloat        init(_ screenshot: XCUIScreenshot) {            let image = screenshot.image            let cg = image.cgImage!            width = cg.width            height = cg.height            scale = CGFloat(cg.width) / image.size.width            var bytes = [UInt8](repeating: 0, count: cg.width * cg.height * 4)            let context = CGContext(data: &bytes, width: cg.width, height: cg.height, bitsPerComponent: 8, bytesPerRow: cg.width * 4,                                    space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)!            context.draw(cg, in: CGRect(x: 0, y: 0, width: cg.width, height: cg.height))            self.bytes = bytes        }        func at(_ x: CGFloat, _ y: CGFloat) -> [Int] {            let px = min(width - 1, max(0, Int(x * scale))), py = min(height - 1, max(0, Int(y * scale)))            let i = (py * width + px) * 4            return [Int(bytes[i]), Int(bytes[i + 1]), Int(bytes[i + 2])]        }        static func near(_ a: [Int], _ b: [Int], _ by: Int = 4) -> Bool { zip(a, b).allSatisfy { abs($0 - $1) <= by } }    }    /// The Warm ground, by day and by night.    private static let grounds = [[0xFA, 0xF7, 0xF2], [0x17, 0x14, 0x11]]    // RULE: scrolled, a filled conversation's rows pass under the floating header and never show through, beside or    // above it: the header stands hittable on an opaque ground of its own from the screen's top to its foot.    func testTheHeaderStandsOnItsOwnGroundAboveTheRows() {        let app = launched(["-chat-warm-long", "400", "-chat-warm-open", "chat-warm-long"])        let thread = app.tables["chat-thread"]        XCTAssertTrue(thread.waitForExistence(timeout: 30), "the long conversation opened")        let person = app.descendants(matching: .any)["chat-person"].firstMatch        let back = app.descendants(matching: .any)["chat-back"].firstMatch        XCTAssertTrue(person.waitForExistence(timeout: 5) && back.exists)        let width = app.windows.firstMatch.frame.width        for pass in 0..<3 {            // The table is flipped (newest at the foot): a swipe down reads back, carrying rows up under the header.            thread.swipeDown(velocity: .slow)            sleep(1)            XCTAssertTrue(person.isHittable && back.isHittable, "the header stands above the rows (pass \(pass))")            let pixels = Pixels(XCUIScreen.main.screenshot())            let p = person.frame, b = back.frame            let ground = pixels.at(2, p.midY)            XCTAssertTrue(Self.grounds.contains { Pixels.near($0, ground) }, "the header's ground is the page's: \(ground)")            var samples: [(CGFloat, CGFloat)] = []            // Between the clock and the pill, across the screen.            for y in [p.minY - 8, p.minY - 4, p.minY - 1] {                for x in stride(from: CGFloat(4), to: width - 4, by: 3) { samples.append((x, y)) }            }            // Beside the rounds and the pill, down the header's height.            for y in stride(from: p.minY + 2, to: p.maxY - 2, by: 3) {                for x in [CGFloat(2), b.minX - 2, (b.maxX + p.minX) / 2, min(width - 2, p.maxX + 3)] where x > 0 && x < width { samples.append((x, y)) }            }            let through = samples.filter { !Pixels.near(pixels.at($0.0, $0.1), ground) }            if !through.isEmpty { keep("chat-look-header-through-\(pass)") }            XCTAssertTrue(through.isEmpty, "the page shows through the header's ground at \(through.prefix(6).map { "(\(Int($0.0)),\(Int($0.1)))" }) (pass \(pass))")        }        keep("chat-look-header-scrolled")    }    /// The pixels in the picture's column (beside their words) down a row's height that are not the ground.    private func faceInk(_ pixels: Pixels, _ row: CGRect, ground: [Int]) -> Int {        var ink = 0        for y in stride(from: row.minY + 2, to: row.maxY - 2, by: 2) {            for x in stride(from: CGFloat(22), to: 50, by: 3) where !Pixels.near(pixels.at(x, y), ground, 6) { ink += 1 }        }        return ink    }    /// The imported group Hill trip, opened at its newest.    private func hillTripOpened() -> (XCUIApplication, (String) -> XCUIElement) {        let app = launched(["-chat-warm-open", hillTrip])        XCTAssertTrue(app.tables["chat-thread"].waitForExistence(timeout: 30), "Hill trip opened")        let bubble = { (id: String) in app.descendants(matching: .any)["chat-bubble-\(id)"].firstMatch }        XCTAssertTrue(bubble("m-w10").waitForExistence(timeout: 10), "the newest stands at the foot")        return (app, bubble)    }    /// Back to the chat's top (the table is flipped: a swipe down reads back).    private func toTop(_ app: XCUIApplication) {        app.tables["chat-thread"].swipeDown(velocity: .slow)        app.tables["chat-thread"].swipeDown(velocity: .slow)        sleep(1)    }    // RULE: a call record an export wrote under the caller's name ("Voice call, 41 sec") is a quiet centred line,    // never their words beside their picture.    func testACallRecordIsAQuietCentredLine() {        let (app, bubble) = hillTripOpened()        toTop(app)        XCTAssertFalse(bubble("m-w6").exists, "a call record is not a message of theirs")        let call = app.staticTexts["Voice call · 41 sec"]        XCTAssertTrue(call.waitForExistence(timeout: 5), "the call's line stands")        let width = app.windows.firstMatch.frame.width        XCTAssertLessThan(abs(call.frame.midX - width / 2), 12, "the call's line is centred: \(call.frame) in \(width)")        let pixels = Pixels(XCUIScreen.main.screenshot())        keep("chat-look-call-line")        XCTAssertEqual(faceInk(pixels, call.frame, ground: pixels.at(4, call.frame.midY)), 0, "no picture beside the call line")    }    // RULE: a picture whose only words are its file's name has no caption; a picture with words keeps them.    func testAFileNameIsNotACaption() {        let (app, bubble) = hillTripOpened()        let shot = bubble("m-w8")        XCTAssertTrue(shot.waitForExistence(timeout: 5))        XCTAssertFalse(shot.label.contains("Screenshot"), "the file's name is not its caption: \(shot.label)")        XCTAssertFalse(app.staticTexts["Screenshot 2026-11-20 at 03.55.10.png"].exists)        XCTAssertFalse(bubble("m-w7").label.contains("PHOTO"), "nor the importer's marker: \(bubble("m-w7").label)")        XCTAssertTrue(bubble("m-w10").label.contains("The view from the top"), "real words beside a picture stay")        keep("chat-look-no-file-name-caption")    }    // RULE: their picture stands once over one sender's unbroken words, however many minutes apart (Bimal's four lines).    func testTheirPictureStandsOncePerRun() {        let (app, bubble) = hillTripOpened()        toTop(app)        let header = app.descendants(matching: .any)["chat-person"].firstMatch.frame        for id in ["m-w1", "m-w2", "m-w4", "m-w5"] {            XCTAssertTrue(bubble(id).waitForExistence(timeout: 5), "\(id) stands")            XCTAssertGreaterThan(bubble(id).frame.minY, header.maxY, "\(id) stands on the page, below the header")        }        let pixels = Pixels(XCUIScreen.main.screenshot())        keep("chat-look-one-picture-per-run")        let ground = pixels.at(4, bubble("m-w2").frame.midY)        XCTAssertGreaterThan(faceInk(pixels, bubble("m-w1").frame, ground: ground), 10, "Bimal's picture stands over his first line")        for id in ["m-w2", "m-w4", "m-w5"] {            XCTAssertEqual(faceInk(pixels, bubble(id).frame, ground: ground), 0, "no picture again beside \(id)")        }    }    // RULE: a picture the store does not have says "Picture not available" after one try, never an empty box; a    // picture the store has draws.    func testAGonePictureSaysSo() {        let (_, bubble) = hillTripOpened()        let gone = bubble("m-w7")        XCTAssertTrue(gone.waitForExistence(timeout: 5))        let said = NSPredicate(format: "label CONTAINS %@", "Picture not available")        XCTAssertEqual(XCTWaiter().wait(for: [XCTNSPredicateExpectation(predicate: said, object: gone)], timeout: 10), .completed,                       "the gone picture says so: \(gone.label)")        XCTAssertFalse(bubble("m-w10").label.contains("Picture not available"), "a picture the store has draws: \(bubble("m-w10").label)")        keep("chat-look-picture-not-available")    }}