Cotton code
115 lines · 6.0 KB · SwiftRaw
// Real touches on the fan for scripts/perf/fan.sh. The test launches the// synthetic Home (com.cottondb.homepreview) with 30 tiles, waits for the// script to attach its trace and for every page picture to be drawn, then// runs the phases one after another, printing a mark at each edge so the// script cuts the trace and its samples by wall-clock time://   fan-sweep: mark <phase> <start|end> <unix seconds>// Phases: rest (no touch), record (a record open on a fan card's page, no touch —// architect, 15 Oct 2026: the same rest-CPU-zero rule), turn (swipes for a// while), turns (a count of swipes, for memory growth), energy (swipes for a// minute). Swipes go three// one way and three back, so the fan never rests on an end.import XCTest@MainActorfinal class FanSweep: XCTestCase {    private func mark(_ phase: String, _ edge: String) {        print("fan-sweep: mark \(phase) \(edge) \(String(format: "%.3f", Date().timeIntervalSince1970))")    }    private func number(_ name: String, _ fallback: Double) -> Double {        Double(ProcessInfo.processInfo.environment[name] ?? "") ?? fallback    }    func testSweep() throws {        let app = XCUIApplication(bundleIdentifier: "com.cottondb.homepreview")        app.launchArguments = ["-home-tiles", String(Int(number("FAN_TILES", 30)))]        app.launch()        let fan = app.descendants(matching: .any)["home-fan"]        XCTAssertTrue(fan.waitForExistence(timeout: 60), "the fan is on screen")        mark("settle", "start")        Thread.sleep(forTimeInterval: number("FAN_SETTLE_S", 25))        mark("settle", "end")        // The deck is the row of tiles at the foot of the fan; the rest of        // `home-fan` is the settled tile's page, which a sideways swipe does        // not turn. Swipe across the deck at the height of its tiles.        let tiles = app.descendants(matching: .any).matching(NSPredicate(format: "identifier BEGINSWITH 'fan-tile-'"))        XCTAssertTrue(tiles.firstMatch.waitForExistence(timeout: 30), "the fan's tiles are on screen")        let screen = app.windows.firstMatch.frame        // The settled tile: the one drawn nearest the screen's middle (tiles        // past the fan's visible arc are not drawn and have no frame). Not the        // widest: a leaning tile's frame is the box around it, wider than the        // upright one and hanging below the deck.        let drawn = tiles.allElementsBoundByIndex.map { $0.frame }.filter { $0.width > 0 && screen.intersects($0) }        let settled = try XCTUnwrap(drawn.min { abs($0.midX - screen.midX) < abs($1.midX - screen.midX) },                                    "a tile of the fan is drawn")        print("fan-sweep: deck tile \(settled)")        let origin = app.coordinate(withNormalizedOffset: .zero)        let row = settled.midY        let right = origin.withOffset(CGVector(dx: screen.width * 0.8, dy: row))        let left = origin.withOffset(CGVector(dx: screen.width * 0.2, dy: row))        var swipes = 0        func swipe() {            let (from, to) = (swipes / 3) % 2 == 0 ? (right, left) : (left, right)            from.press(forDuration: 0.01, thenDragTo: to, withVelocity: .fast, thenHoldForDuration: 0)            swipes += 1        }        func swipe(for seconds: Double) {            let started = Date()            while Date().timeIntervalSince(started) < seconds { swipe() }        }        mark("rest", "start")        Thread.sleep(forTimeInterval: number("FAN_REST_S", 30))        mark("rest", "end")        // A record open on a fan card's page, opened as a tap opens it, then left        // alone; Back returns to the fan before it turns.        // XCUIApplication.open relaunches the app, which would end the trace;        // the script hands the URL to the running app instead (simctl openurl).        print("fan-sweep: open cottonhomepreview://record/todo/example-todo-1")        // The system asks before a URL opens an app; say Open.        let open = XCUIApplication(bundleIdentifier: "com.apple.springboard").buttons["Open"]        if open.waitForExistence(timeout: 10) { open.tap() }        XCTAssertTrue(app.wait(for: .runningForeground, timeout: 10), "the record opened in the running app")        Thread.sleep(forTimeInterval: 3)        mark("record", "start")        Thread.sleep(forTimeInterval: number("FAN_REST_S", 30))        mark("record", "end")        let back = app.navigationBars.buttons.firstMatch        if back.exists { back.tap() } else { app.coordinate(withNormalizedOffset: CGVector(dx: 0.01, dy: 0.5))            .press(forDuration: 0.01, thenDragTo: app.coordinate(withNormalizedOffset: CGVector(dx: 0.8, dy: 0.5))) }        Thread.sleep(forTimeInterval: 3)        mark("turn", "start")        swipe(for: number("FAN_TURN_S", 20))        mark("turn", "end")        // A still second between phases, so the turns phase starts settled.        Thread.sleep(forTimeInterval: 1.5)        mark("turns", "start")        for _ in 0 ..< Int(number("FAN_TURNS", 100)) { swipe() }        mark("turns", "end")        Thread.sleep(forTimeInterval: number("FAN_AFTER_S", 5))        mark("after", "end")        mark("energy", "start")        swipe(for: number("FAN_ENERGY_S", 60))        mark("energy", "end")        // The page read down and back up: the fan folds into the circle and        // blooms out of it (fancollapse), a finger's scroll on the page each way.        Thread.sleep(forTimeInterval: 1.5)        let high = origin.withOffset(CGVector(dx: screen.width * 0.5, dy: screen.height * 0.2))        let low = origin.withOffset(CGVector(dx: screen.width * 0.5, dy: screen.height * 0.55))        mark("fold", "start")        let folding = Date()        while Date().timeIntervalSince(folding) < number("FAN_FOLD_S", 12) {            low.press(forDuration: 0.01, thenDragTo: high, withVelocity: .default, thenHoldForDuration: 0.3)            high.press(forDuration: 0.01, thenDragTo: low, withVelocity: .default, thenHoldForDuration: 0.3)        }        mark("fold", "end")        print("fan-sweep: \(swipes) swipes")    }}