// Navigation is a value in the environment, and the route table is one switch. // // Lane I2 replaced three ways of moving between screens — an `onOpen` closure // threaded through every initialiser, a `@Binding var path: [Route]` handed to // the day lane, and an `onBack`/`onClose` for the way out — with one: // `navigator.push(route)` opens, `dismiss()` closes, `Boot/RouteScreen.swift` // draws. These tests hold the value's small contract and sweep for the old // spellings, because each was the natural thing to write for the next screen. // // `open` joined that contract with the three-column lane, and the fact worth // guarding about it is a NEGATIVE one: the capture instruction never enters // the path. `/capture` is an instruction a host turns // into a surface drawn over what is already there, and three entry points carry // them (a deep link, a notification, a held tap replayed) — // so one that pushed would leave a screen on the stack nobody can pop back // past, from four places at once. import CottonKit import XCTest @testable import CottonUI @MainActor final class NavigatorTests: XCTestCase { func testPushAndPopMoveTheTop() { let navigator = Navigator() navigator.fanOpens = { _ in false } // Mounted fan with no matching tile. XCTAssertNil(navigator.top) navigator.push(.todos(new: false)) navigator.push(.todosTodo(id: "t1")) XCTAssertEqual(navigator.top, .todosTodo(id: "t1")) navigator.pop() XCTAssertEqual(navigator.stack, [.todos(new: false)]) } func testPopAtTheRootIsNothing() { let navigator = Navigator() navigator.fanOpens = { _ in false } // Mounted fan with no matching tile. navigator.pop() XCTAssertEqual(navigator.stack, []) } func testReplaceSwapsTheTopAndPushesAtTheRoot() { let navigator = Navigator() navigator.fanOpens = { _ in false } // Mounted fan with no matching tile. navigator.replace(with: .people) XCTAssertEqual(navigator.stack, [.people], "at the root a replace is a push — the person has to land somewhere") navigator.replace(with: .peoplePerson(id: "p1")) XCTAssertEqual(navigator.stack, [.peoplePerson(id: "p1")], "a peer surface replaces rather than stacks") } func testPopToRootEmptiesThePath() { let navigator = Navigator() navigator.fanOpens = { _ in false } // Mounted fan with no matching tile. navigator.push(.notes) navigator.push(.notesNote(id: "n1")) navigator.popToRoot() XCTAssertEqual(navigator.stack, []) } func testALinkToHomeGoesBackToHomeRatherThanStackingASecondOne() { for home in [Route.root, .home] { let navigator = Navigator() navigator.fanOpens = { _ in false } // Mounted fan with no matching tile. navigator.push(.notes) navigator.push(.notesNote(id: "n1")) navigator.push(home) XCTAssertEqual(navigator.stack, [], "\(home.path) is the root, never a page on the stack") navigator.push(.notes) navigator.open(home) XCTAssertEqual(navigator.stack, [], "a tapped link to \(home.path) lands on Home too") } } // RULE: The screen on top is never pushed again — a deep link to the Meetings list that is showing leaves one, and Meetings asked to import takes its place — so Back always leaves the list rather than landing on its copy (S2). func testTheScreenOnTopIsNeverStackedTwice() { let navigator = Navigator() navigator.fanOpens = { _ in false } // Mounted fan with no matching tile. navigator.push(.notes) navigator.push(.meetings(importing: false)) navigator.push(.meetings(importing: false)) XCTAssertEqual(navigator.stack, [.notes, .meetings(importing: false)], "a second Meetings is not stacked") navigator.push(.meetings(importing: true)) XCTAssertEqual(navigator.stack, [.notes, .meetings(importing: true)], "importing is the same list with the picker up") navigator.push(.meetingsMeeting(id: "m1")) navigator.push(.meetingsMeeting(id: "m2")) XCTAssertEqual(navigator.stack.count, 4, "different records still stack") } // RULE: A link to a project opens the database page with that project chosen, never a screen of its own. func testALinkToAProjectAsksTheDatabasePageRatherThanPushingAScreen() { let navigator = Navigator() navigator.fanOpens = { _ in false } // Mounted fan with no matching tile. navigator.push(.projectsSlug(slug: "x")) XCTAssertEqual(navigator.stack, []) XCTAssertEqual(navigator.projectRequest, "x") } // MARK: - `open`, the one entry a tap comes through func testANewNavigatorIsNotCapturing() { XCTAssertNil(Navigator().capture) } func testTheCaptureRouteRaisesCaptureAndLeavesThePathAlone() { let navigator = Navigator() navigator.fanOpens = { _ in false } // Mounted fan with no matching tile. navigator.open(.capture) XCTAssertEqual(navigator.capture, .listening) XCTAssertEqual(navigator.stack, []) } func testAnOrdinaryRouteIsAPush() { let navigator = Navigator() navigator.fanOpens = { _ in false } // Mounted fan with no matching tile. navigator.open(.todos(new: false)) XCTAssertNil(navigator.capture) XCTAssertEqual(navigator.stack, [.todos(new: false)]) // Over a stack that already exists it grows it: `open` differs from // `push` on the action tier and nowhere else. navigator.open(.todosTodo(id: "t1")) XCTAssertEqual(navigator.stack, [.todos(new: false), .todosTodo(id: "t1")]) } func testCaptureCanStartWithTheKeyboardUp() { // The Mac's ⌘N and the phone's "Type instead" say so at the door; the // surface reads it on appear. let navigator = Navigator() navigator.fanOpens = { _ in false } // Mounted fan with no matching tile. navigator.open(.capture, start: .typing) XCTAssertEqual(navigator.capture, .typing) XCTAssertEqual(navigator.stack, []) } /// **A shared picture waits for the capture surface, and opens it /// typing.** The share sheet already filed the photo; the person's next /// move is saying what it is about, so the keyboard comes up — never the /// microphone the default start would have switched on over it. func testACaptureOpenedWithAPictureWaitingOpensTypingAndHandsItOverOnce() { let navigator = Navigator() navigator.fanOpens = { _ in false } // Mounted fan with no matching tile. let picture = CapturedPicture(kind: .photo, recordId: "photo:a", key: "ws/photo/a.jpg") navigator.hold(picture: picture) navigator.open(.capture) XCTAssertEqual(navigator.capture, .typing) XCTAssertEqual(navigator.stack, []) XCTAssertEqual(navigator.takeWaitingPicture(), picture) XCTAssertNil(navigator.takeWaitingPicture(), "once: the dock that takes it owns it") } func testClosingCaptureClearsItAndLeavesThePersonWhereTheyWere() { let navigator = Navigator() navigator.fanOpens = { _ in false } // Mounted fan with no matching tile. navigator.open(.todos(new: false)) navigator.open(.capture) navigator.closeCapture() XCTAssertNil(navigator.capture) // Capture rose over the pushed list and popped nothing: coming out // of it, the person is where they were. XCTAssertEqual(navigator.stack, [.todos(new: false)]) } } /// The old spellings stay gone, and the table stays one switch. final class NavigationSweepTests: XCTestCase { private static let table = "CottonUI/Boot/RouteScreen.swift" /// RULE: The phone's host has no push: no NavigationStack and no navigationDestination stand anywhere in the phone's sources, so a page cannot slide in from the right; a screen's own destination opens in place through `pageDestination` (the Mac's side of `pageDestination` keeps its own). func testThePhoneHasNoPushHost() throws { // The Mac's side of `pageDestination` pushes in its window; the // database chooser is a sheet's own steps, not a page. let allowed: Set = [ "CottonUI/Surfaces/Board/PageDestination.swift", "CottonUI/Surfaces/Databases/DatabasesScreen.swift", ] // An app with its own root and its own stack (the redesigns, 20 Nov 2026: each app is its own // object): Story's Ink pushes its pages in its own NavigationStack, the system's back gesture its // way back (assets/design/story/DECISIONS.md: "differences only where the platform insists: the // system back gesture"). Only the pushes named; a row whose file stops pushing fails. let ownStacks: [String: Set] = [ "CottonUI/Story/Ink/InkStory.swift": ["NavigationStack", "navigationDestination("], ] var offenders: [String] = [] var ownTripped: Set = [] for (name, text) in try AppSources.all() where name.hasPrefix("CottonUI/") && !allowed.contains(name) { let code = SourceSweep.withoutComments(text) for push in ["NavigationStack", "navigationDestination(", "NavigationLink"] where code.contains(push) { if ownStacks[name]?.contains(push) == true { ownTripped.insert(name) continue } offenders.append("\(name): \(push)") } } XCTAssertEqual(offenders, [], "a page pushes instead of opening in place") for name in ownStacks.keys { XCTAssertTrue(ownTripped.contains(name), "\(name) no longer pushes — take its row out of ownStacks") } } /// No screen takes a route closure or the path itself; it asks the /// environment. `PendingTaps` is the one seam that still hands a `Route` /// to a closure — the cold-start buffer delivers INTO the navigator and /// is not a screen. func testNoScreenIsHandedNavigation() throws { // The Mac's sidebar is a host composing hosts, not a screen: its row // does not push, it REPLACES the whole path (design spec §2.5). It // takes the act from the host that owns the columns, for the same // reason `PendingTaps` does — the caller knows something about where // the route lands that the callee must not decide. let allowed: Set = [ "CottonUI/Boot/PendingTaps.swift", Self.table, ] var offenders: [String] = [] for (name, text) in try AppSources.all() where !allowed.contains(name) { let code = SourceSweep.withoutComments(text) for pattern in ["(Route) -> Void", "@Binding var path: [Route]", "path: Binding<[Route]>"] where code.contains(pattern) { offenders.append("\(name): \(pattern)") } } XCTAssertEqual(offenders, [], "a screen handed navigation instead of reading `Navigator` from the environment: " + offenders.joined(separator: ", ")) } /// The way out is `dismiss()`. Only the bar and the page scaffold keep an /// optional `onBack`, for the screens that must do something else first /// (an editor that saves) — every other screen says nothing and the bar /// dismisses. func testNoScreenDeclaresItsOwnBack() throws { let allowed: Set = [ "CottonUI/Chrome/ScreenBar.swift", "CottonUI/Paper/PaperScreen.swift", ] var offenders: [String] = [] for (name, text) in try AppSources.all() where !allowed.contains(name) { let code = SourceSweep.withoutComments(text) for pattern in ["onBack: @escaping () -> Void", "onBack: (() -> Void)?", "let onBack:", "var onBack:"] where code.contains(pattern) { offenders.append("\(name): \(pattern)") } } XCTAssertEqual(offenders, [], "a screen declaring its own back: " + offenders.joined(separator: ", ") + " — say nothing and the bar dismisses") } }