Cotton code
76 lines · 6.2 KB · SwiftRaw
import CottonKitimport Foundationimport SwiftUIimport XCTest@testable import CottonUI/// Every screen a tap from Home reaches since 05b9ea8b1 (everything a board/// opens is a route) pays for its body on the phone's 1 MB main-thread/// stack; build 230 crashed on the first of them, the table's list/// (`SurfaceStackTests` carries that story and the list's and page's own/// guards). These are the others a route opens — the boards, search, and the/// route wrappers around the list and the page — measured the same way, so/// the next tap is not the next crash.final class RouteSurfaceStackTests: XCTestCase {    /// RULE: every surface a route opens keeps its body small — Home's and the Tools board, the board surface, search, and the list, page and filtered-list routes for every bundled table: view value at most 8 KB (a route wrapper's body is the surface struct it draws, copied through a modifier or two while the route table boxes the wrapper itself; a record page is 7.3 KB, 2.7 KB of it its table's definition), type at most 40 nested generic levels, at most 256 KB of main-thread stack per body evaluation in a debug build (`StackHighWater.surfaceBudget`: a quarter of the phone's 1 MB main stack, the rest held by the frames beneath it — SwiftUI's update recursion, UIKit, the screens below on the stack; debug is the upper bound); build 230 crashed on the phone's 1 MB main stack the first time a tap from Home opened a surface over that.    @MainActor    func testEverySurfaceARouteOpensStaysSmallAndShallow() throws {        var worst = SurfaceStackTests.Worst()        let home = HomeModel(core: ScriptedCore([:]), toasts: ToastCenter(scheduler: ManualToastScheduler()))        for board in [BoardDefinition.home.id, BoardDefinition.tools.id] {            let surface = HomeBoard(home: home, board: board)            worst.note("HomeBoard(\(board))", body: surface.body) { _ = surface.body }        }        for definition in [BoardDefinition.home, BoardDefinition.tools] {            let surface = BoardSurface<EmptyView>(                project: .constant(nil), board: definition,                home: BoardHome(model: home, databaseName: nil, project: nil, now: Date()),                results: nil, onOpenTable: { _ in })            worst.note("BoardSurface(\(definition.id))", body: surface.body) { _ = surface.body }        }        let search = SearchScreen(model: SearchModel(core: ScriptedCore([:]), recents: InMemorySearchRecents(), scheduler: ManualToastScheduler()))        worst.note("SearchScreen", body: search.body) { _ = search.body }        for definition in try SurfaceStackTests.bundledDefinitions() {            let list = TableListRoute(table: definition.id)            worst.note("TableListRoute(\(definition.id))", body: list.body) { _ = list.body }            let page = TablePageRoute(table: definition.id, id: "stack-guard")            worst.note("TablePageRoute(\(definition.id))", body: page.body) { _ = page.body }            if let opening = FilteredListOpening(table: definition.id, filter: nil, title: nil, query: nil) {                let filtered = FilteredListScreen(opening: opening)                worst.note("FilteredListScreen(\(definition.id))", body: filtered.body) { _ = filtered.body }            }        }        worst.assertWithin(valueBytes: 8192, depth: 40, stackBytes: StackHighWater.surfaceBudget, of: "route surface")    }    /// RULE: the deepest route chain stays within half the phone's stack — Home (its board route and the board surface), then a bundled table's list (its route and the list surface), then a record's page (its route and the page surface), their body evaluations summed, touch at most 512 KB of main-thread stack in a debug build (`StackHighWater.chainBudget`: the phone's main thread has 1 MB, and the other half is the frames beneath every body — SwiftUI's update recursion, UIKit, the run loop; debug is the upper bound); builds 227 and 230 overflowed that 1 MB on the second and third screens of this chain. TableDefinition's copy-on-write storage (11 Oct) took the chain from 756 KB to 360 KB.    @MainActor    func testTheDeepestRouteChainStaysWithinHalfThePhonesStack() throws {        let home = HomeModel(core: ScriptedCore([:]), toasts: ToastCenter(scheduler: ManualToastScheduler()))        var homeCost = 0        for board in [BoardDefinition.home, BoardDefinition.tools] {            let route = HomeBoard(home: home, board: board.id)            let surface = BoardSurface<EmptyView>(                project: .constant(nil), board: board,                home: BoardHome(model: home, databaseName: nil, project: nil, now: Date()),                results: nil, onOpenTable: { _ in })            homeCost = max(homeCost, StackHighWater.measure { _ = route.body } + StackHighWater.measure { _ = surface.body })        }        var worst = (bytes: 0, table: "", list: 0, page: 0)        for definition in try SurfaceStackTests.bundledDefinitions() {            let listRoute = TableListRoute(table: definition.id)            let list = RecordListSurface(definition: definition) { _ in }            let pageRoute = TablePageRoute(table: definition.id, id: "stack-guard")            let page = DeckSurface(definition: definition, filter: [:], view: nil, opensAt: "stack-guard")            let listCost = StackHighWater.measure { _ = listRoute.body } + StackHighWater.measure { _ = list.body }            let pageCost = StackHighWater.measure { _ = pageRoute.body } + StackHighWater.measure { _ = page.body }            let chain = homeCost + listCost + pageCost            if chain > worst.bytes { worst = (chain, definition.id, listCost, pageCost) }        }        print("RouteSurfaceStackTests chain: \(worst.bytes / 1024) KB (\(worst.table): Home \(homeCost / 1024) + list \(worst.list / 1024) + page \(worst.page / 1024))")        XCTAssertLessThanOrEqual(            worst.bytes, StackHighWater.chainBudget,            "Home → \(worst.table)'s list → its page touched \(worst.bytes / 1024) KB of main-thread stack (Home \(homeCost / 1024), list \(worst.list / 1024), page \(worst.page / 1024)); the phone has 1 MB for this chain and every frame beneath it (builds 227, 230) — cut the heaviest body into stages built in their own frames")    }}