Cotton code
124 lines · 5.4 KB · SwiftRaw
// The capture surface and the search answer the board draws in place, and the// tap delivery that raises them — the parts of Home's footer lane (three-column// design 2026-09-13, §4.3–§4.4) that outlived Home. The board's own placement// of them is `Surfaces/HomeBoardNoLossTests.swift`.//// This target has no renderer, so the shape is a by-path source guard and the// model under the surface is behaviour, over a `ScriptedCore` with a fixed// clock.import CottonKitimport XCTest@testable import CottonUIfinal class CaptureSurfaceSourceTests: XCTestCase {    /// RULE: Capture opened from any depth on the phone is drawn OVER the stack and pops nothing, so cancelling it leaves the person on the screen they raised it from — a Todos group or a record pushed outside the path included; a kept thought's receipt holds the layer until its Done.    @MainActor    func testCaptureOpenedOverAPushedListIsDrawnOverItAndPopsNothing() throws {        let phone = Navigator()        phone.fanOpens = { _ in false } // Mounted fan with no matching tile.        phone.push(.todosTodo(id: "t-1"))        phone.open(.capture, start: .typing)        XCTAssertEqual(phone.stack, [.todosTodo(id: "t-1")], "nothing is popped")        XCTAssertTrue(phone.captureOverStack, "the stack host draws it over the pushed screens")        XCTAssertEqual(phone.capture, .typing)        phone.closeCapture(restoring: false)        XCTAssertNil(phone.capture)        XCTAssertTrue(phone.captureOverStack, "a kept thought's receipt keeps the layer up")        phone.closeCapture()        XCTAssertFalse(phone.captureOverStack)        XCTAssertEqual(phone.stack, [.todosTodo(id: "t-1")], "the person is where they were")        let home = Navigator()        home.fanOpens = { _ in false } // Mounted fan with no matching tile.        home.open(.capture)        XCTAssertFalse(home.captureOverStack, "over Home itself, Home draws it")        // A tool's board or a record page Home opens is a route on the path        // (`BoardOpensThroughRoutesTests`), so capture raised over one is        // over the stack by the rule above — no screen is pushed beside it.        home.closeCapture()        home.open(.toolBoard(id: "disk"))        home.open(.capture)        XCTAssertTrue(home.captureOverStack, "a tool's board is on the path, so capture rises over it")        XCTAssertEqual(home.stack, [.toolBoard(id: "disk")])        // The Mac draws capture in place; the list the person reads stays.        let mac = Navigator()        mac.arrival = .columns        mac.push(.todosTodo(id: "t-1"))        mac.open(.capture, start: .typing)        XCTAssertEqual(mac.path, [.todosTodo(id: "t-1")])    }}// MARK: - The surface's model@MainActorfinal class CaptureSurfaceModelTests: XCTestCase {    private static let read = """        {"thoughts": [], "projects": [            {"id": "p1", "name": "Aardvark", "status": "active"},            {"id": "p2", "name": "Badger", "status": "archived"}        ], "people": [            {"id": "h1", "fullName": "Placeholder Person"}        ]}        """    /// The model as Home builds it, over a scripted core.    private func model(        _ core: ScriptedCore,        dictionary: @escaping @MainActor () async -> DictationHints = { DictationHints() },        calendar: Calendar = .current,        now: Date = Date()    ) -> CaptureSurfaceModel {        CaptureSurfaceModel(            read: ScreenRead(core: core, read: CaptureSurfaceModel.readName, families: [.core]),            dictionary: dictionary,            calendar: calendar,            now: now        )    }    func testAsksTheOneReadTheChatSuppliedItsVocabularyFrom() async {        let core = ScriptedCore([CottonMethod.readThoughts: .ok(Self.read)])        let model = model(core)        await model.load()        let asked = await core.methodsCalled()        XCTAssertEqual(asked, [CottonMethod.readThoughts])    }    func testSuppliesActiveProjectsAndPeopleWhole() async {        let core = ScriptedCore([CottonMethod.readThoughts: .ok(Self.read)])        let model = model(core)        await model.load()        XCTAssertEqual(model.vocabulary.projects.map(\.name), ["Aardvark"], "an archived project is not offered")        XCTAssertEqual(model.vocabulary.people.map(\.name), ["Placeholder Person"])    }    func testAFailedReadLeavesTheDockWithNoCompletionsRatherThanNoDock() async {        let core = ScriptedCore([CottonMethod.readThoughts: .refuse(code: "boom", message: "no")])        let model = model(core)        await model.load()        XCTAssertTrue(model.vocabulary.projects.isEmpty)        XCTAssertTrue(model.vocabulary.people.isEmpty)    }    func testTheDictionaryIsTheOneCallItWasHanded() async {        let handed = DictationHints(groups: [DictationHints.Group(tag: "terms", terms: ["Aardvark", "Zebra"])])        let model = model(ScriptedCore(), dictionary: { handed })        XCTAssertEqual(model.hints, DictationHints(), "nothing until asked")        await model.loadHints()        XCTAssertEqual(model.hints, handed)    }    func testTodayIsTheClockItWasHandedNotTheWallClock() {        var calendar = Calendar(identifier: .gregorian)        calendar.timeZone = TimeZone(identifier: "Asia/Kolkata")!        // 2026-09-13T20:30Z is already the 14th in Kolkata.        let now = Date(timeIntervalSince1970: 1_789_331_400)        let model = model(ScriptedCore(), calendar: calendar, now: now)        XCTAssertEqual(model.todayKey, "2026-09-14")    }}