Cotton code
147 lines · 9.1 KB · SwiftRaw
// Every list in Story scrolls under a swipe that starts on any part of a// row, as a person's thumb lands: a scene row's number, its place and time,// the line under it, its length and its ⋯; a face of the cast and its name;// a scene's lines and their faces; the speaker chooser's rows; the pages in// Read; the places. A row that swallowed the swipe (owner's phone, 20 Nov// 2026) fails here.import XCTestfinal class StoryListSwipeTests: StoryUITestCase {    /// The scene row nearest the middle of the window.    @MainActor private func middleRow(_ app: XCUIApplication) -> XCUIElement? {        let window = app.windows.firstMatch.frame        let rows = app.buttons.matching(NSPredicate(format: "label CONTAINS %@ AND label CONTAINS %@", " · ", " min"))        return rows.allElementsBoundByIndex.first { row in            let frame = row.frame            return frame.minY > window.height * 0.3 && frame.maxY < window.height * 0.75        }    }    // RULE: a swipe up starting on any part of a scene row (number, place and time, its line, length, ⋯) scrolls the home.    @MainActor func testASwipeFromEveryPartOfASceneRowScrollsTheHome() {        let app = StoryUI.launch()        _ = StoryUI.home(app)        // Past the cast and the pills, so a row stands mid-screen.        StoryUI.drag(app, from: CGVector(dx: 0.5, dy: 0.8), by: CGVector(dx: 0, dy: -250))        let parts: [(String, CGVector)] = [            ("its number", CGVector(dx: 0.03, dy: 0.3)),            ("its place and time", CGVector(dx: 0.35, dy: 0.3)),            ("its line", CGVector(dx: 0.4, dy: 0.78)),            ("its length", CGVector(dx: 0.84, dy: 0.3)),            ("its ⋯", CGVector(dx: 0.97, dy: 0.3)),        ]        for (part, at) in parts {            guard let row = middleRow(app) else { return XCTFail("a scene row stands mid-screen before a swipe on \(part)") }            let label = row.label            let before = row.frame.minY            StoryUI.drag(row, at: at, by: CGVector(dx: 0, dy: -220))            let same = app.buttons.matching(NSPredicate(format: "label == %@", label)).firstMatch            let after = same.exists ? same.frame.minY : -.infinity            XCTAssertLessThan(after, before - 100, "a swipe on a scene row's \(part) scrolled the home")            XCTAssertFalse(app.staticTexts["Write a line…"].exists, "the swipe on \(part) opened no scene")        }    }    // RULE: the cast scrolls sideways from a face and from a name under it.    @MainActor func testTheCastScrollsFromAFaceAndFromItsName() {        let app = StoryUI.launch()        _ = StoryUI.home(app)        let faces = app.buttons.matching(NSPredicate(format: "label CONTAINS %@", " lines"))        for (part, at) in [("face", CGVector(dx: 0.5, dy: 0.3)), ("name", CGVector(dx: 0.5, dy: 0.92))] {            let width = app.windows.firstMatch.frame.width            guard let face = faces.allElementsBoundByIndex.first(where: { $0.frame.minX > 0 && $0.frame.maxX < width && $0.frame.width > 0 }) else {                return XCTFail("a face stands whole on screen")            }            let label = face.label            let x = face.frame.minX            StoryUI.drag(face, at: at, by: CGVector(dx: -160, dy: 0))            let same = faces.matching(NSPredicate(format: "label == %@", label)).firstMatch            XCTAssertLessThan(same.exists ? same.frame.minX : -.infinity, x - 60, "the cast moved sideways under a swipe on a \(part)")            XCTAssertTrue(StoryUI.row(app, "Waiting Room").exists, "the swipe on a \(part) opened no character")        }        // A swipe up on a face still scrolls the home.        let width = app.windows.firstMatch.frame.width        if let face = faces.allElementsBoundByIndex.first(where: { $0.frame.minX > 0 && $0.frame.maxX < width && $0.frame.width > 0 }) {            let y = face.frame.minY            StoryUI.drag(face, at: CGVector(dx: 0.5, dy: 0.4), by: CGVector(dx: 0, dy: -200))            XCTAssertLessThan(face.exists ? face.frame.minY : -.infinity, y - 80, "a swipe up on a face scrolled the home")        }    }    // RULE: a scene's lines scroll under a swipe on a line's words and on a speaker's face.    @MainActor func testASceneScrollsFromItsLinesAndFaces() {        let app = StoryUI.launch()        let row = StoryUI.row(app, "Platform One")        XCTAssertTrue(row.waitForExistence(timeout: 60))        row.tap()        let lines = app.descendants(matching: .any).matching(identifier: "ink-line")        XCTAssertTrue(lines.firstMatch.waitForExistence(timeout: 10), "the scene opened")        // It opens at its last line: swipe down to go back up it.        for (part, at) in [("words", CGVector(dx: 0.5, dy: 0.7)), ("face", CGVector(dx: 0.02, dy: 0.3))] {            let window = app.windows.firstMatch.frame            // A whole line under the scene's head (the shell's card, when one stands, pushes the scene down).            let head = StoryUI.button(app, "Back to your scenes").frame.maxY            guard let line = lines.allElementsBoundByIndex.first(where: { $0.frame.minY > max(window.height * 0.25, head + 20) && $0.frame.maxY < window.height * 0.75 && $0.label.contains(" says: ") }) else {                return XCTFail("a spoken line stands mid-screen")            }            let id = line.label            let y = line.frame.minY            StoryUI.drag(line, at: at, by: CGVector(dx: 0, dy: 200))            let same = lines.allElementsBoundByIndex.first { $0.label == id }            XCTAssertGreaterThan(same?.frame.minY ?? .infinity, y + 80, "a swipe on a line's \(part) scrolled the scene")            XCTAssertFalse(app.buttons["Write after this"].exists, "the swipe on a line's \(part) opened no acts")        }    }    // RULE: the speaker chooser's rows scroll under a swipe on a row; a pull down while    // they are scrolled takes them back up and keeps the panel; at their top it closes it.    @MainActor func testTheSpeakerRowsScrollAndAPullFirstTakesThemBack() {        let app = StoryUI.launch(["-story-ink-open", "speaker"])        let rows = app.scrollViews["ink-speaker-rows"]        XCTAssertTrue(rows.waitForExistence(timeout: 60), "the speaker chooser opened")        let first = rows.buttons.firstMatch        XCTAssertTrue(first.waitForExistence(timeout: 5))        let name = first.label        let y = first.frame.minY        StoryUI.drag(first, at: CGVector(dx: 0.3, dy: 0.5), by: CGVector(dx: 0, dy: -260))        let same = rows.buttons.matching(NSPredicate(format: "label == %@", name)).firstMatch        XCTAssertLessThan(same.frame.minY, y - 100, "the rows scrolled under a swipe on a row")        // A pull down on the rows now scrolls them back; the panel stays.        StoryUI.drag(rows, at: CGVector(dx: 0.5, dy: 0.3), by: CGVector(dx: 0, dy: 200))        XCTAssertTrue(rows.exists && rows.isHittable, "a pull down on scrolled rows kept the panel")        // Back at the top, a pull down closes it.        StoryUI.drag(rows, at: CGVector(dx: 0.5, dy: 0.2), by: CGVector(dx: 0, dy: 300))        StoryUI.drag(rows, at: CGVector(dx: 0.5, dy: 0.2), by: CGVector(dx: 0, dy: 300))        XCTAssertTrue(rows.waitForNonExistence(timeout: 3), "a pull down at the rows' top closed the panel")    }    // RULE: Read's pages scroll under a swipe on a printed line.    @MainActor func testReadScrollsFromAPrintedLine() {        let app = StoryUI.launch(["-story-ink-open", "read"])        let foot = app.staticTexts.matching(NSPredicate(format: "label BEGINSWITH %@", "Page ")).firstMatch        XCTAssertTrue(foot.waitForExistence(timeout: 60), "Read opened")        // A printed line found once on the first pages: scene 2's heading.        let line = app.buttons["EXT. PLATFORM ONE - NIGHT"]        XCTAssertTrue(line.waitForExistence(timeout: 5))        let words = line.label        let y = line.frame.minY        StoryUI.drag(line, at: CGVector(dx: 0.5, dy: 0.5), by: CGVector(dx: 0, dy: -300))        XCTAssertLessThan(line.exists ? line.frame.minY : -.infinity, y - 120, "the pages scrolled under a swipe on the line \(words)")        XCTAssertTrue(foot.exists, "the swipe opened no scene")    }    // RULE: the places scroll under a swipe on a place's row, and the last place is reachable.    @MainActor func testThePlacesScrollFromARow() {        let app = StoryUI.launch(["-story-ink-open", "places"])        let rows = app.buttons.matching(NSPredicate(format: "label CONTAINS %@", "scene"))        XCTAssertTrue(rows.firstMatch.waitForExistence(timeout: 60), "Places opened")        // A place's row, not Back ("Back to your scenes"), which the shell's card can push below 200.        guard let row = rows.allElementsBoundByIndex.first(where: { $0.isHittable && $0.frame.minY > 200 && $0.label != "Back to your scenes" }) else { return XCTFail("a place row stands") }        let label = row.label        let y = row.frame.minY        StoryUI.drag(row, at: CGVector(dx: 0.3, dy: 0.5), by: CGVector(dx: 0, dy: -200))        let same = rows.matching(NSPredicate(format: "label == %@", label)).firstMatch        XCTAssertLessThan(same.exists ? same.frame.minY : -.infinity, y - 80, "the places scrolled under a swipe on a row")    }}