Cotton code
91 lines · 5.0 KB · SwiftRaw
// The viewer's Details shows the record's acts (review, 2 Nov 2026). When// opened acts moved onto the shared tier path (1d138db912), `EditingFan`// stopped drawing and only deals its cards onto that path, which Home's fan// draws — under the viewer's full-screen cover. Details was a dim layer with// nothing on it.#if os(iOS)import CottonKitimport CottonValuesimport SwiftUIimport UIKitimport XCTest@testable import CottonUI@MainActorfinal class ViewerDetailsActsTests: XCTestCase {    /// RULE: Details over a picture shows the record's acts on the viewer itself, as the deck's opened card does.    func testDetailsDrawsTheRecordsActsOverTheViewer() async throws {        let json = try SourceSweep.mustRead("crates/cotton-core/kits/personal/tables/todo.json", ownedBy: "viewer")        let definition = try JSONDecoder().decode(TableDefinition.self, from: Data(json.utf8))        let record = TableRecord(id: "synthetic-todo", values: ["title": .text("Synthetic todo"), "status": .text("open")])        let done = try XCTUnwrap(definition.commands?.first { $0.id == "done" }, "the todo kit carries Done")        let editor = DeckEditor.session        // No fan stands here to connect its tiers: the test connects its own.        let tiers = FanTiers()        tiers.setRoot(FanTier(id: "editor-root", cards: []))        editor.connect(tiers)        editor.close()        defer { editor.close() }        // The held card's acts are the core's (`shell.opened`): it answers Done.        let core = ScriptedCore([CottonMethod.shellOpened: .ok("""            {"acts":[{"id":"done","label":"\(done.label)","kind":"command","rank":"main","primary":true,"red":false}]}            """)])        let host = UIHostingController(rootView: AnyView(            ViewerDetails(record: ViewerRecord(table: definition.id, id: record.id), onClose: {})                .environment(\.records, OneRecord(record))                .environment(\.tableDefinitions, TableDefinitions([definition]))                .environment(\.cottonCore, core)))        let window = try Self.window(host)        defer { window.isHidden = true; window.rootViewController = nil }        let deadline = Date().addingTimeInterval(8)        while Date() < deadline, !Self.shows(done.label, in: host.view) {            try await Task.sleep(for: .milliseconds(20))        }        XCTAssertTrue(editor.isEditing, "Details opened the record's acts")        XCTAssertTrue(Self.shows(done.label, in: host.view), "the acts stand on the viewer, not on the fan it covers")    }    private static func window(_ root: UIViewController) throws -> UIWindow {        let scene = try XCTUnwrap(UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.first,                                  "the host app has no window scene")        let window = UIWindow(windowScene: scene)        window.frame = CGRect(x: 0, y: 0, width: 402, height: 874)        window.rootViewController = root        window.makeKeyAndVisible()        return window    }    private static func nodes(under root: NSObject) -> [NSObject] {        var children: [NSObject] = (root.accessibilityElements as? [NSObject]) ?? []        let count = root.accessibilityElementCount()        if children.isEmpty, count > 0, count != NSNotFound {            children = (0..<count).compactMap { root.accessibilityElement(at: $0) as? NSObject }        }        if let view = root as? UIView { children += view.subviews }        return [root] + children.flatMap { nodes(under: $0) }    }    private static func shows(_ words: String, in root: UIView) -> Bool {        nodes(under: root).contains { $0.accessibilityLabel == words }    }}/// One record, as the viewer's Details reads it.private final class OneRecord: Records, @unchecked Sendable {    private let record: TableRecord    init(_ record: TableRecord) { self.record = record }    func tableDefinitions() async throws -> [TableDefinition] { [] }    func count(table: String) async throws -> Int { 1 }    func list(table: String, project: String?, query: String?, page: Int, where filter: [String: RecordValue]) async throws -> RecordPage {        RecordPage(records: [record], next: nil, total: 1)    }    func get(table: String, id: String) async throws -> TableRecord? { id == record.id ? record : nil }    func create(table: String, data: [String: RecordValue]) async throws -> ChangeRecord { throw RecordsError.unknownRecord(table) }    func update(table: String, id: String, patch: [String: RecordValue]) async throws -> ChangeRecord { throw RecordsError.unknownRecord(id) }    func delete(table: String, id: String) async throws -> ChangeRecord { throw RecordsError.unknownRecord(id) }    func reorder(table: String, ids: [String]) async throws -> ChangeRecord { throw RecordsError.noOrderColumn(table) }    func link(table: String, id: String, column: String, target: String) async throws -> ChangeRecord { throw RecordsError.unknownRecord(id) }    func undo(change: String) async throws -> ChangeRecord { throw RecordsError.unknownChange(change) }}#endif