Cotton code
153 lines · 7.8 KB · SwiftRaw
// AN OPENED CARD STAYS WHERE IT IS (owner, 3 Nov 2026: "Do we need to shift// the todo out of the box of todos when the actions menu comes up? Why do we// need that."). A hold, or a tap that opens a record at rest, opens the card// in place on its deck: same frame, same face, the acts tier at the hub;// closing returns the deck to plain. Synthetic rows only.#if os(iOS)import CottonFacesimport CottonKitimport CottonValuesimport SwiftUIimport XCTest@testable import CottonUI@MainActorfinal class OpenedInPlaceTests: XCTestCase {    private func todo() throws -> TableDefinition {        let json = try SourceSweep.mustRead("crates/cotton-core/kits/personal/tables/todo.json", ownedBy: "deck")        return try JSONDecoder().decode(TableDefinition.self, from: Data(json.utf8))    }    private func source(_ path: String) throws -> String {        SourceSweep.withoutComments(try SourceSweep.mustRead("apps/cotton/Sources/CottonUI/" + path, ownedBy: "deck"))    }    /// RULE: opening a card in place leaves it exactly where it stood — the    /// same frame — with the acts tier up at the hub and the face's parts    /// live; the face is drawn as at rest (never its lifted, opened look);    /// the hub's close returns the deck to plain, the card unmoved.    func testTheOpenedCardStaysWhereItIsAndTheHubClosesIt() async throws {        let definition = try todo()        let records = ["synthetic-a", "synthetic-b"].map {            TableRecord(id: $0, values: ["title": .text("Invented todo"), "status": .text("open")])        }        let editor = DeckEditor.standing()        let probe = InPlaceProbe()        let host = UIHostingController(rootView: InPlaceDeck(definition: definition, records: records, probe: probe)            .environment(\.deckEditor, editor))        let scene = try XCTUnwrap(UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.first)        let previous = scene.keyWindow        let window = UIWindow(windowScene: scene)        window.frame = CGRect(x: 0, y: 0, width: 393, height: 852)        window.rootViewController = host        window.makeKeyAndVisible()        defer { window.isHidden = true; window.rootViewController = nil; previous?.makeKeyAndVisible() }        func settle() async { for _ in 0..<6 { host.view.layoutIfNeeded(); await Task.yield(); try? await Task.sleep(for: .milliseconds(20)) } }        await settle()        let rest = probe.frame        XCTAssertGreaterThan(rest.width, 0, "the card is drawn")        XCTAssertNil(probe.target, "at rest no editing target rides down")        XCTAssertNil(editor.openedTarget)        // The hold: the card opens where it is.        probe.open("synthetic-a")        await settle()        let target = DeckEditTarget(table: definition.id, record: "synthetic-a", date: definition.card?.corner.first?.column)        XCTAssertEqual(probe.frame, rest, "the opened card has not moved or grown")        XCTAssertEqual(editor.openedTarget, target, "opened \(String(describing: probe.opened)), editing \(String(describing: editor.editing)), live \(probe.target != nil), frame \(probe.frame) vs \(rest)")        XCTAssertEqual(editor.navigation.open?.editorState, .acts(target), "the acts tier stands at the hub")        XCTAssertEqual(probe.target?.target, target, "the face's parts are live")        XCTAssertFalse(probe.faceOpened, "the face keeps its look at rest: nothing lifts out of its box")        // The hub's close, the same pop the fan's hub makes.        editor.navigation.pop()        await settle()        XCTAssertNil(probe.opened, "the deck is plain again")        XCTAssertNil(editor.openedTarget)        XCTAssertNil(probe.target)        XCTAssertEqual(probe.frame, rest, "closing moves nothing")        // A pinch in or the card leaving the screen closes it too; a second        // opening on the deck replaces the first.        probe.open("synthetic-a")        await settle()        XCTAssertEqual(editor.openedTarget, target)        probe.open(nil)        await settle()        XCTAssertNil(editor.openedTarget, "a card closed by its deck lets the editor go")        XCTAssertNil(editor.navigation.open?.editorState)    }    /// RULE: no deck pushes a page for an opened record any more: the    /// record deck, Home's sync card and the search deck open in place, by    /// the one modifier; the page they pushed is gone.    func testNoDeckPushesAnOpenedPage() throws {        for path in ["Deck/DeckSurface.swift", "Surfaces/Board/HomeToday.swift", "Screens/Search/SearchDeck.swift"] {            let deck = try source(path)            XCTAssertFalse(deck.contains("OpenedRecordCard"), path)            XCTAssertFalse(deck.contains(".pageDestination("), path)            XCTAssertTrue(deck.contains(".opensInPlace("), path)        }        let opened = try source("Deck/OpenedCard.swift")        XCTAssertFalse(opened.contains("struct OpenedRecordCard"), "removed means removed")        XCTAssertTrue(opened.contains(".deckEditable(definition: definition, record: record, live: opened)"))        XCTAssertFalse(opened.components(separatedBy: "struct OpenedInPlace")[1].contains("faceOpened"),                       "the card opened in place keeps its face as at rest")        // The deck holds every move while a card stands opened.        let deck = try source("Deck/DeckSurface.swift")        XCTAssertTrue(deck.contains("guard openedInPlace == nil else { return }"))        XCTAssertGreaterThanOrEqual(deck.components(separatedBy: "guard openedInPlace == nil else { return }").count - 1, 3,                                    "the zones, the wheel and the card's own tap move nothing")        // The ink's letters narrow nothing while a card stands opened: the        // core is told so with the stroke (the ink moved into the core, 8 Nov).        XCTAssertTrue(deck.contains("narrows: openedInPlace == nil"), "the ink's line moves the deck under an opened card")        XCTAssertTrue(try source("Deck/CardDeck.swift").contains("guard opened == nil else { return }"))        // The top line stands through the hold and the acts: it fades only        // while words are written (owner, 3 Nov 2026).        let fades = try source("Deck/Shell/DeckEditable.swift").components(separatedBy: "struct DeckEditingFades")[1]            .components(separatedBy: "struct InkMark")[0]        XCTAssertTrue(fades.contains("editor?.writing != nil"))        XCTAssertFalse(fades.contains("isEditing"), "a hold, or a tier on the fan, never hides the top line")    }}/// What the probe face saw, and the deck's opened card.@MainActor @Observableprivate final class InPlaceProbe {    var opened: String?    var frame: CGRect = .zero    var target: DeckCardTarget?    var faceOpened = false    func open(_ id: String?) { opened = id }}/// Home's shape: a card deck whose face opens in place.private struct InPlaceDeck: View {    let definition: TableDefinition    let records: [TableRecord]    let probe: InPlaceProbe    var body: some View {        CardDeck(items: records, open: { probe.open($0.id) }, holds: { probe.open($0.id) }, opened: probe.opened) { record in            InPlaceFace(probe: probe)                .opensInPlace(probe.opened == record.id, definition: definition, record: record, worn: "") {                    if probe.opened == record.id { probe.open(nil) }                }        }    }}private struct InPlaceFace: View {    let probe: InPlaceProbe    @Environment(\.deckCardTarget) private var target    @Environment(\.faceOpened) private var faceOpened    var body: some View {        Color.gray            .onGeometryChange(for: CGRect.self) { $0.frame(in: .global) } action: { probe.frame = $0 }            .onChange(of: target, initial: true) { _, now in probe.target = now }            .onChange(of: faceOpened, initial: true) { _, now in probe.faceOpened = now }    }}#endif