Cotton code
188 lines · 10.6 KB · SwiftRaw
import CottonKitimport XCTest@testable import CottonUI/// A record opened over Home's fan opens in place, on the shown card's own/// stack (lane trail, 15 Oct 2026; amendments 13:05, 13:20, 13:40 IST).@MainActorfinal class TileStackTests: XCTestCase {    private let note = Route.notesNote(id: "invented-note")    private let person = Route.peoplePerson(id: "invented-person")    private let meeting = Route.meetingsMeeting(id: "invented-meeting")    private let todo = Route.todosTodo(id: "invented-todo")    private func showing(_ tile: String = "invented-db\u{1F}notes") -> Navigator {        let navigator = Navigator()        navigator.fanOpens = { _ in false } // Mounted fan with no matching tile.        navigator.tile = tile        return navigator    }    /// RULE: On the phone nothing is pushed over the whole screen: every route the catalogue can open — a record, a thread's details, a settings page, a tool's list — opens in place on the shown surface's stack (the fan tile's, else the root's) and no path is pushed, because the owner ruled every inside page opens as a record does.    func testEveryRouteTheCatalogueOpensIsHostedInPlace() {        var walked = 0        for pattern in Route.allPatterns {            let filled = pattern.split(separator: "/", omittingEmptySubsequences: false)                .map { $0.hasPrefix("[") ? "invented-1" : String($0) }.joined(separator: "/")            guard let route = Route.parse(filled), route.tier != .launcher, route.tier != .action else { continue }            if case .projectsSlug = route { continue }            for navigator in [showing(), Navigator()] {                navigator.fanOpens = { _ in false } // The catalogue route is absent from this fan.                navigator.open(route)                XCTAssertEqual(navigator.path, [], "\(route) was pushed over the whole screen")                XCTAssertEqual(navigator.stack, [route], "\(route) is not the page in place")            }            walked += 1        }        XCTAssertGreaterThan(walked, 0)        let columns = Navigator()        columns.arrival = .columns        columns.open(note)        XCTAssertTrue(columns.tileStacks.isEmpty, "the Mac keeps its window push")    }    /// RULE: Each fan tile keeps its own stack: turning to another card and back shows the first where it was left, so "show photos" and back to Notes keeps the note open; a record already on the stack is gone back to rather than stacked again, so a link back to the note a person was opened from never grows the path.    func testEachCardKeepsItsOwnStack() {        let navigator = showing("db\u{1F}notes")        navigator.open(note)        navigator.open(person)        navigator.tile = "db\u{1F}photos"        XCTAssertEqual(navigator.stack, [])        navigator.tile = "db\u{1F}notes"        XCTAssertEqual(navigator.stack, [note, person])        navigator.open(note)        XCTAssertEqual(navigator.stack, [note])    }    /// RULE: Back steps exactly one: the left-edge swipe and the back control's tap each take off the top page and nothing else, and at the tool's own page they do nothing, because a back that skipped a page would lose where the person was.    func testBackStepsExactlyOne() {        let navigator = showing()        for route in [note, person, meeting] { navigator.open(route) }        navigator.pop()        XCTAssertEqual(navigator.stack, [note, person])        navigator.back(to: navigator.stack.count - 1)        XCTAssertEqual(navigator.stack, [note])        navigator.pop()        navigator.pop()        XCTAssertEqual(navigator.stack, [])        XCTAssertEqual(navigator.path, [])    }    /// RULE: The back control's long-press list jumps to the chosen page and drops everything above it, and the settled fan tile's tap goes back to the tool's own page; a depth that is not on the stack changes nothing.    func testTheListJumpsAndTheSettledCardGoesToTheRoot() {        let navigator = showing()        for route in [note, person, meeting, todo] { navigator.open(route) }        navigator.back(to: 2)        XCTAssertEqual(navigator.stack, [note, person])        navigator.back(to: 5)        XCTAssertEqual(navigator.stack, [note, person])        navigator.back(to: 0)        XCTAssertEqual(navigator.stack, [])    }    /// RULE: A record's X closes exactly one level — the record on top goes and the one beneath it stays, at every depth — because the X is the zoom's own step back and the long-press list is the way to jump several.    func testTheXClosesExactlyOneLevel() {        let key = "invented-db\u{1F}notes"        let navigator = showing(key)        let stack = [note, person, meeting, todo]        for route in stack { navigator.open(route) }        for depth in stride(from: stack.count, through: 1, by: -1) {            // The page at `depth` lists the tool's own page and every record under it.            let titles = (0..<depth).map { "page \($0)" }            TileBack.of(titles: titles, key: key, navigator: navigator)?.close()            XCTAssertEqual(navigator.stack, Array(stack.prefix(depth - 1)))        }    }    /// RULE: A tool's own page has no back control, and every record on the stack has one listing the pages under it (the tool's own first), because a back that cannot go back is a promise the page cannot keep.    func testARootHasNoBackControl() {        let navigator = showing()        XCTAssertNil(TileBack.of(titles: [], key: "k", navigator: navigator))        XCTAssertEqual(TileBack.of(titles: ["Notes", "A note"], key: "k", navigator: navigator)?.titles, ["Notes", "A note"])    }    /// RULE: A record opened from a slip on screen zooms out of it; one opened with no slip to grow from (a voice "open", a restored stack, a jump of several), and every move under Reduce Motion, cross-fades — there is no sliding move at all, because the fan owns the sideways axis.    func testARecordWithNoSlipCrossFadesAndNothingSlides() {        let none: (Route) -> Bool = { _ in false }        let any: (Route) -> Bool = { _ in true }        XCTAssertEqual(TileMove.way(from: [], to: [note], sourced: any, zoomed: false, reduceMotion: false), .zoomIn)        XCTAssertEqual(TileMove.way(from: [], to: [note], sourced: none, zoomed: false, reduceMotion: false), .fade)        XCTAssertEqual(TileMove.way(from: [], to: [note], sourced: any, zoomed: false, reduceMotion: true), .fade)        XCTAssertEqual(TileMove.way(from: [note, person], to: [note], sourced: none, zoomed: true, reduceMotion: false), .zoomOut)        XCTAssertEqual(TileMove.way(from: [note, person], to: [note], sourced: none, zoomed: false, reduceMotion: false), .fade)        XCTAssertEqual(TileMove.way(from: [note, person, meeting], to: [note], sourced: any, zoomed: true, reduceMotion: false), .fade)        XCTAssertEqual(TileMove.way(from: [note], to: [person], sourced: any, zoomed: true, reduceMotion: false), .fade)    }    /// RULE: Each card's stack survives a relaunch, as its routes' paths in the scene's restoration; a path that no longer parses is left out rather than restored as something else.    func testStacksSurviveARelaunch() {        let kept = TileStacks.encode(["db\u{1F}notes": [note, person], "db\u{1F}photos": []])        XCTAssertEqual(TileStacks.decode(kept), ["db\u{1F}notes": [note, person]])        XCTAssertEqual(TileStacks.decode(#"{"db\u001Fnotes":["/no/such/route/at/all"]}"#), [:])        let navigator = Navigator()        navigator.fanOpens = { _ in false } // Mounted fan with no matching tile.        navigator.restore(TileStacks.decode(kept))        navigator.tile = "db\u{1F}notes"        XCTAssertEqual(navigator.stack, [note, person])    }    /// RULE: A page's Close pops it for good — Home coming back on screen after the close does not put it back, a relaunch does not reopen it, and a launch restores no page over the whole Home (294 row 5, walker293: Copies, opened from Me's sync line, came straight back on every Close and every relaunch).    func testAClosedPageStaysClosed() {        let navigator = Navigator()        navigator.fanOpens = { _ in false } // Mounted fan with no matching tile.        var restored = false        _ = TileStacksKept.arrive(navigator, kept: "", restored: &restored)        navigator.open(note)        let kept = TileStacks.encode(navigator.tileStacks)  // written while the page stood        navigator.back(to: 0)        XCTAssertFalse(navigator.pageOpen)        let now = TileStacksKept.arrive(navigator, kept: kept, restored: &restored)        XCTAssertFalse(navigator.pageOpen, "Home's return put the closed page back")        XCTAssertEqual(TileStacks.decode(now), [:], "a relaunch would reopen it")        let launch = Navigator()        var fresh = false        let cards = TileStacks.encode([Navigator.rootTile: [person], "db\u{1F}notes": [note]])        _ = TileStacksKept.arrive(launch, kept: cards, restored: &fresh)        XCTAssertEqual(launch.tileStacks, ["db\u{1F}notes": [note]], "a launch restores cards' stacks, never a page over Home")    }    /// RULE: A record on a fan tile's page is shown — grown out of its slip or faded in — only once it has reported drawn (pageDrawn), or when fan_picture_settle_ms has passed if it never does, and then once: a page shown before it drew is a blank frame mid-zoom, and one held past the ceiling is a tap that did nothing.    func testAPageIsShownOnlyOnceDrawnOrAtTheCeiling() async {        var ceilings: [CheckedContinuation<Void, Never>] = []        func held() -> DrawnWait {            DrawnWait { _ in await withCheckedContinuation { ceilings.append($0) } }        }        func settle() async { for _ in 0..<20 { await Task.yield() } }        // Drawn first: shown when drawn, never before, and not again at the ceiling.        let drawnFirst = held()        var shown = 0        drawnFirst.then(settle: 0.35) { shown += 1 }        await settle()        XCTAssertEqual(shown, 0, "shown before it drew")        drawnFirst.markDrawn()        XCTAssertEqual(shown, 1)        ceilings.removeFirst().resume()        await settle()        XCTAssertEqual(shown, 1, "shown twice")        // Never drawn: shown at the ceiling, once, and not again when it draws late.        let never = held()        var late = 0        never.then(settle: 0.35) { late += 1 }        await settle()        XCTAssertEqual(late, 0)        ceilings.removeFirst().resume()        await settle()        XCTAssertEqual(late, 1, "held past the ceiling")        never.markDrawn()        XCTAssertEqual(late, 1, "shown twice")        // Already drawn: shown at once, with no wait.        var now = 0        never.then(settle: 0.35) { now += 1 }        XCTAssertEqual(now, 1)        XCTAssertTrue(ceilings.isEmpty)    }}