Cotton code
192 lines · 10.6 KB · SwiftRaw
// COTTON DATA DURING A FIRST SYNC (the owner's find on build 2, 27 Nov 2026: with his copy's first pass running, every// inner screen stood blank: a table with its title and (i) alone, You › Storage and Keep everything with their titles// alone). Cotton Data Dev on a simulator, signed in to the dev server as an INVENTED account as large as his// (TEST_RUNNER_WALK_COTTON_PHONE, its sign-in code TEST_RUNNER_WALK_COTTON_CODE and its recovery code// TEST_RUNNER_WALK_COTTON_RECOVERY: never written to the log or a picture), Keep everything on, so the whole copy's// first pass runs while each inner screen is opened and timed.import XCTestfinal class DataFirstSyncTests: XCTestCase {    override func setUp() { continueAfterFailure = false }    @MainActor private func element(_ app: XCUIApplication, _ id: String) -> XCUIElement {        app.descendants(matching: .any)[id].firstMatch    }    @MainActor private func shot(_ app: XCUIApplication, _ name: String) {        let attachment = XCTAttachment(screenshot: app.screenshot())        attachment.name = name        attachment.lifetime = .keepAlways        add(attachment)    }    /// The screen's tree, kept when a step does not find what it looks for (no typed code is in it: the fields are    /// left before it is taken).    @MainActor private func tree(_ app: XCUIApplication, _ name: String) {        let attachment = XCTAttachment(string: app.debugDescription)        attachment.name = name        attachment.lifetime = .keepAlways        add(attachment)    }    private func env(_ key: String) throws -> String {        try XCTUnwrap(ProcessInfo.processInfo.environment[key].flatMap { $0.isEmpty ? nil : $0 }, key)    }    /// First open: Start (Keep everything as the screen offers it, on), then You › the card › Sign in with the    /// invented number and its code, then the key opened with its recovery code.    @MainActor func testSignInAndKeepEverything() throws {        let phone = try env("WALK_COTTON_PHONE")        let code = try env("WALK_COTTON_CODE")        let recovery = try env("WALK_COTTON_RECOVERY")        let app = XCUIApplication()        app.launch()        let start = element(app, "data-start")        let tables = element(app, "data-tab-tables")        let deadline = Date().addingTimeInterval(60)        while Date() < deadline, !start.exists, !tables.exists { _ = tables.waitForExistence(timeout: 1) }        if start.exists {            let keep = element(app, "data-welcome-keep")            if keep.exists, (keep.value as? String) == "0" { keep.tap() }            start.tap()        }        XCTAssertTrue(tables.waitForExistence(timeout: 30))        element(app, "data-tab-you").tap()        let card = element(app, "settings-card")        XCTAssertTrue(card.waitForExistence(timeout: 20))        card.tap()        let signIn = element(app, "settings-profile-account_sign_in")        if signIn.waitForExistence(timeout: 10) {            signIn.tap()            let field = app.textFields.firstMatch            guard field.waitForExistence(timeout: 15) else { tree(app, "no number field"); return XCTFail("the number is asked") }            field.tap()            field.typeText(phone)            let send = element(app, "home-customer-send")            if send.waitForExistence(timeout: 3) { send.tap() } else { app.keyboards.buttons.element(boundBy: app.keyboards.buttons.count - 1).tap() }            let codeField = element(app, "home-customer-code")            guard codeField.waitForExistence(timeout: 120) else { tree(app, "no code field"); return XCTFail("the code is asked") }            codeField.tap()            codeField.typeText(code)            // An account with no name yet is asked one before Sign in can be tapped: an invented one.            let name = element(app, "home-customer-name")            if name.exists {                name.tap()                name.typeText("Rowan Vale")            }            let confirm = element(app, "home-customer-confirm")            // The number pad covers Sign in on a small sheet: the sheet is scrolled until it shows above the keys.            if confirm.waitForExistence(timeout: 5) {                var lifts = 0                while !confirm.isHittable && lifts < 4 { element(app, "home-customer-sent-to").swipeUp(velocity: .slow); lifts += 1 }                if confirm.isHittable { confirm.tap() } else { tree(app, "sign in hidden"); return XCTFail("Sign in can be tapped") }            }        }        openKey(app, recovery)        element(app, "data-tab-tables").tap()        Thread.sleep(forTimeInterval: 5)        shot(app, "Signed in, Tables")    }    /// The key: this account's other devices hold it, so a new phone asks one to approve; with none to hand it takes    /// No other device?, then the recovery code. Nothing is pictured while the code is on the screen.    @MainActor private func openKey(_ app: XCUIApplication, _ recovery: String) {        let noDevice = element(app, "getting-in-no-device")        let have = element(app, "recovery-have-code")        let entry = element(app, "recovery-entry-field")        let tables = element(app, "data-tab-tables")        let until = Date().addingTimeInterval(180)        while Date() < until, !noDevice.exists, !have.exists, !entry.exists { _ = entry.waitForExistence(timeout: 2) }        if noDevice.exists, !have.exists, !entry.exists { noDevice.tap(); _ = have.waitForExistence(timeout: 10) }        if have.exists, !entry.exists { have.tap() }        guard entry.waitForExistence(timeout: 20) else { tree(app, "no recovery entry"); return }        entry.tap()        entry.typeText(recovery)        element(app, "recovery-entry-open").tap()        XCTAssertTrue(entry.waitForNonExistence(timeout: 120), "the key opened")        _ = tables.waitForExistence(timeout: 30)    }    /// A phone signed in whose key is not open yet (the walk stopped at the key): the recovery code opens it.    @MainActor func testOpenTheKey() throws {        let recovery = try env("WALK_COTTON_RECOVERY")        let app = XCUIApplication()        app.launch()        openKey(app, recovery)        element(app, "data-tab-tables").tap()        Thread.sleep(forTimeInterval: 5)        shot(app, "Key open, Tables")    }    /// Filling's percent as the Tables strip says it, before and after the app is closed and opened again mid-pass    /// (the owner's phone said 0% with 6 hours left while it held 4.3 GB).    @MainActor func testFillAcrossARelaunch() throws {        let app = XCUIApplication()        app.launch()        let strip = element(app, "data-tables-filling")        guard strip.waitForExistence(timeout: 60) else { shot(app, "no filling strip"); return }        Thread.sleep(forTimeInterval: 20)        let before = strip.label        shot(app, "fill before relaunch")        app.terminate()        app.launch()        guard strip.waitForExistence(timeout: 60) else { shot(app, "after relaunch, no filling strip"); print("DATA FILL before \(before) after: done"); return }        let first = strip.label        Thread.sleep(forTimeInterval: 20)        print("DATA FILL before: \(before) | at relaunch: \(first) | 20 s on: \(strip.label)")        shot(app, "fill after relaunch")    }    /// While the first pass runs: each inner screen says something within a second (rows, a loading line or the    /// refusal's words), and its own content within a few. Timed and pictured.    @MainActor func testInnerScreensDuringTheFirstPass() throws {        let app = XCUIApplication()        app.launch()        let tables = element(app, "data-tab-tables")        XCTAssertTrue(tables.waitForExistence(timeout: 60))        let filling = element(app, "data-tables-filling")        _ = filling.waitForExistence(timeout: 20)        shot(app, "1 Tables while filling")        for table in ["event", "note", "memory", "person"] {            let line = element(app, "data-table-\(table)")            var swipes = 0            while (!line.exists || !line.isHittable) && swipes < 12 { app.swipeUp(velocity: .slow); swipes += 1 }            guard line.exists else { continue }            let began = Date()            line.tap()            let said = app.descendants(matching: .any).matching(NSPredicate(format: "identifier BEGINSWITH 'data-row-' OR identifier == 'data-rows-loading' OR identifier == 'data-rows-refused' OR identifier == 'data-first-sync'")).firstMatch            let first = said.waitForExistence(timeout: 1.5)            let firstAt = Date().timeIntervalSince(began)            let row = app.descendants(matching: .any).matching(NSPredicate(format: "identifier BEGINSWITH 'data-row-' AND NOT identifier BEGINSWITH 'data-row-page'")).firstMatch            let rows = row.waitForExistence(timeout: 30)            let rowsAt = Date().timeIntervalSince(began)            shot(app, "2 \(table): said \(first ? String(format: "%.1f s", firstAt) : "nothing in 1.5 s"), rows \(rows ? String(format: "%.1f s", rowsAt) : "none in 30 s")")            print("DATA TIMING \(table): first \(first ? String(format: "%.2f", firstAt) : "none") rows \(rows ? String(format: "%.2f", rowsAt) : "none")")            element(app, "data-back").tap()            Thread.sleep(forTimeInterval: 1)            for _ in 0..<4 { app.swipeDown(velocity: .fast) }        }        // You › Keep everything, then You › Storage.        element(app, "data-tab-you").tap()        for (row, title) in [("keep", "Keep"), ("storage", "Storage")] {            let place = app.descendants(matching: .any).matching(NSPredicate(format: "identifier CONTAINS[c] %@", "settings-own-\(row)")).firstMatch            let shared = app.descendants(matching: .any).matching(NSPredicate(format: "identifier CONTAINS[c] %@", "settings-row-\(row)")).firstMatch            var swipes = 0            while !place.exists && !shared.exists && swipes < 8 { app.swipeUp(velocity: .slow); swipes += 1 }            let target = place.exists ? place : shared            guard target.exists else { tree(app, "no \(row) row"); continue }            let began = Date()            target.tap()            Thread.sleep(forTimeInterval: 1)            shot(app, "3 \(title) after 1 s")            Thread.sleep(forTimeInterval: 9)            shot(app, "3 \(title) after 10 s")            print("DATA TIMING \(row): pictured at 1 and 10 s (\(String(format: "%.1f", Date().timeIntervalSince(began))))")            let back = element(app, "data-back")            if back.exists { back.tap() } else { app.navigationBars.buttons.firstMatch.tap() }            Thread.sleep(forTimeInterval: 1)            for _ in 0..<4 { app.swipeDown(velocity: .fast) }        }    }}