Cotton code
69 lines · 4.2 KB · SwiftRaw
// The change sheet carries the day (DECISIONS, 21 Nov 2026): picking a day// moves the item there, saves what else was changed on the sheet, and// closes; opened again, the item holds both. The month used to put the// sheet's day back to the item's when it closed, and lose a title typed// before (report.md, Days owner faults 1). Invented crowd only, put back as// it came in.import XCTestfinal class DaysSheetDayTests: DaysUITestCase {    /// `day` days from today, as the month's cell names it ("Monday 24 November"), and whether it is in another month.    private func cell(_ day: Int) -> (name: String, short: String, nextMonth: Bool) {        let cal = Calendar.current        let now = Date()        let then = cal.date(byAdding: .day, value: day, to: now)!        let f = DateFormatter()        f.dateFormat = "EEEE d MMMM"        let s = DateFormatter()        s.dateFormat = "EEE d MMM"        return (f.string(from: then), s.string(from: then), cal.component(.month, from: then) != cal.component(.month, from: now))    }    /// Picks `day` days from today on the sheet's month, which opens on the    /// item's own month (`opensOn`: today's, or the next one).    @MainActor private func pick(_ app: XCUIApplication, _ day: Int, opensOnNextMonth: Bool = false) {        DaysUI.button(app, "Pick a day").tap()        let (name, _, next) = cell(day)        XCTAssertTrue(DaysUI.button(app, "Previous month").waitForExistence(timeout: 3), "the month opened in the sheet")        if next && !opensOnNextMonth { DaysUI.button(app, "Next month").tap(); settle() }        if !next && opensOnNextMonth { DaysUI.button(app, "Previous month").tap(); settle() }        let target = app.buttons.matching(NSPredicate(format: "label BEGINSWITH %@", name)).firstMatch        XCTAssertTrue(target.waitForExistence(timeout: 3), "\(name) stands in the month")        target.tap()    }    // RULE: a title changed, then a day picked: the sheet closes, and the item is on that day with that title.    @MainActor func testADayPickedSticksWithTheTitleTyped() throws {        let app = DaysUI.launch(["-days-open", "todo"])        let title = DaysUI.field(app, "Title")        XCTAssertTrue(title.waitForExistence(timeout: 90), "the to-do's sheet opened")        try skipUnderTheShellsCard(app, wait: 0)        let was = title.value as? String ?? ""        let now = was + " moved"        title.coordinate(withNormalizedOffset: CGVector(dx: 0.98, dy: 0.5)).tap()        title.typeText(" moved")        pick(app, 3)        XCTAssertTrue(title.waitForNonExistence(timeout: 4), "picking a day saved and closed the sheet")        // To that day by the month, so it stands at the top.        let date = app.buttons.matching(NSPredicate(format: "label BEGINSWITH %@", "Today, ")).firstMatch        XCTAssertTrue(date.waitForExistence(timeout: 5) && DaysUI.scrollTo(date, in: app), "today's date stands")        date.tap()        let (name, _, next) = cell(3)        XCTAssertTrue(DaysUI.button(app, "Previous month").waitForExistence(timeout: 3), "the month opened")        if next { DaysUI.button(app, "Next month").tap(); settle() }        app.buttons.matching(NSPredicate(format: "label BEGINSWITH %@", name)).firstMatch.tap()        settle(1.5)        let row = app.buttons.matching(NSPredicate(format: "label BEGINSWITH %@", now + ",")).firstMatch        XCTAssertTrue(row.waitForExistence(timeout: 4) && row.isHittable, "\(now) stands on \(name)")        DaysUI.point(app, DaysUI.Seen(type: .button, label: row.label, frame: row.frame), CGVector(dx: 0.6, dy: 0.5)).tap()        XCTAssertTrue(title.waitForExistence(timeout: 5), "the sheet opened again")        XCTAssertEqual(title.value as? String, now, "the title typed before the month was kept")        XCTAssertTrue(app.staticTexts[cell(3).short].exists, "the day picked stuck: \(cell(3).short)")        // Put back as it came in: its own title, today.        title.coordinate(withNormalizedOffset: CGVector(dx: 0.98, dy: 0.5)).tap()        for _ in 0..<" moved".count { title.typeText(XCUIKeyboardKey.delete.rawValue) }        pick(app, 0, opensOnNextMonth: cell(3).nextMonth)        XCTAssertTrue(title.waitForNonExistence(timeout: 4), "put back on today")    }}