Cotton code
113 lines · 5.9 KB · SwiftRaw
// Days notifies on iPhone (owner via the coordinator, 24 Nov 2026; DECISIONS "Alerts"): on a fresh install, the first// item that earns an alert brings the core's card saying why (Reminders, Continue, Not now) before iOS asks; allowed,// the event's alert, ten minutes before it, is scheduled on the phone and comes as a banner with Days killed. An event// deleted before its alert never comes. Run on a fresh install (the runner uninstalls Days Dev first).import XCTestfinal class DaysNoticesTests: XCTestCase {    @MainActor private var springboard: XCUIApplication { XCUIApplication(bundleIdentifier: "com.apple.springboard") }    override func setUp() {        continueAfterFailure = false        XCUIDevice.shared.orientation = .portrait    }    @MainActor private func keep(_ name: String) {        let shot = XCTAttachment(screenshot: XCUIScreen.main.screenshot())        shot.name = name        shot.lifetime = .keepAlways        add(shot)    }    /// The minute an alert ten minutes before it fires in a minute or two: "14:37", and when it fires.    private func eventTime() -> (words: String, fires: Date) {        let calendar = Calendar.current        let now = Date()        let seconds = calendar.component(.second, from: now)        // The next whole minute, one later when it is too near to type and save in time.        let ahead = seconds > 30 ? 2 : 1        let minute = calendar.dateInterval(of: .minute, for: now)!.start        let fires = calendar.date(byAdding: .minute, value: ahead, to: minute)!        let starts = calendar.date(byAdding: .minute, value: 10, to: fires)!        let words = String(format: "%02d:%02d", calendar.component(.hour, from: starts), calendar.component(.minute, from: starts))        return (words, fires)    }    /// Typed in the adding bar and added: an event today at `time`.    @MainActor private func addEvent(_ app: XCUIApplication, _ title: String, at time: String) {        DaysUI.button(app, "Add").tap()        let field = DaysUI.field(app, "What, and when?")        XCTAssertTrue(field.waitForExistence(timeout: 10), "the adding bar opened")        field.typeText("\(title) today at \(time)")        let understood = app.buttons.matching(NSPredicate(format: "label BEGINSWITH %@", "Understood as")).firstMatch        XCTAssertTrue(understood.waitForExistence(timeout: 10), "the line is understood")        XCTAssertTrue(understood.label.contains(time), "as an event at \(time): \(understood.label)")        DaysUI.button(app, "Add").tap()    }    /// The banner naming `title`, on the lock of the home screen, within `timeout`.    @MainActor private func banner(_ title: String, timeout: TimeInterval) -> XCUIElement {        let shown = springboard.descendants(matching: .any).matching(NSPredicate(format: "label CONTAINS %@", title)).firstMatch        _ = shown.waitForExistence(timeout: timeout)        return shown    }    // RULE: the first event with an alert brings the reason card (the core's words, Continue and Not now), and only    // then does iOS ask; allowed, the alert comes as a banner ten minutes before the event with Days killed.    @MainActor func testAnEventsAlertComesWithDaysKilled() {        let app = DaysUI.launch()        DaysUI.days(app)        XCTAssertFalse(springboard.alerts.firstMatch.exists, "nothing asked at launch")        let (time, fires) = eventTime()        addEvent(app, "Invented dentist", at: time)        let go = app.buttons["days-leave-go"], notNow = app.buttons["days-leave-not-now"]        XCTAssertTrue(go.waitForExistence(timeout: 15), "why, before iOS asks")        XCTAssertEqual(go.label, "Continue")        XCTAssertEqual(notNow.label, "Not now")        XCTAssertTrue(app.staticTexts["Reminders"].exists, "the card's title")        XCTAssertFalse(springboard.alerts.firstMatch.exists, "iOS has not asked yet")        keep("days-leave-why")        go.tap()        let allow = springboard.alerts.buttons["Allow"]        XCTAssertTrue(allow.waitForExistence(timeout: 10), "then iOS asks")        keep("days-leave-ios-asks")        allow.tap()        // The pass runs after the answer: a moment, then Days is killed.        Thread.sleep(forTimeInterval: 4)        app.terminate()        XCUIDevice.shared.press(.home)        let wait = max(30, fires.timeIntervalSinceNow + 45)        let shown = banner("Invented dentist", timeout: wait)        XCTAssertTrue(shown.exists, "the alert came as a banner with Days killed (waited \(Int(wait)) s)")        keep("days-banner")    }    // RULE: an event deleted before its alert never comes. Runs after the first (leave given); alone, it gives it.    @MainActor func testTheAlertOfADeletedEventNeverComes() {        let app = DaysUI.launch()        DaysUI.days(app)        let (time, fires) = eventTime()        addEvent(app, "Invented cancelled lunch", at: time)        if app.buttons["days-leave-go"].waitForExistence(timeout: 5) {            app.buttons["days-leave-go"].tap()            let allow = springboard.alerts.buttons["Allow"]            if allow.waitForExistence(timeout: 10) { allow.tap() }        }        let row = DaysUI.row(app, "Invented cancelled lunch")        XCTAssertTrue(row.waitForExistence(timeout: 10), "the event stands")        row.tap()        let delete = app.buttons["Delete"]        XCTAssertTrue(delete.waitForExistence(timeout: 10), "its sheet offers Delete")        delete.tap()        let confirm = app.buttons.matching(NSPredicate(format: "label == 'Delete'")).element(boundBy: 0)        if confirm.waitForExistence(timeout: 3), confirm.isHittable { confirm.tap() }        XCTAssertTrue(row.waitForNonExistence(timeout: 10), "deleted")        Thread.sleep(forTimeInterval: 4)        app.terminate()        XCUIDevice.shared.press(.home)        let wait = max(30, fires.timeIntervalSinceNow + 45)        XCTAssertFalse(banner("Invented cancelled lunch", timeout: wait).exists, "no banner for a deleted event")        keep("days-deleted-quiet")    }}