Cotton code
88 lines · 4.8 KB · SwiftRaw
// A tap on an event card's picture chooses its kind (owner, 29 Oct 2026):// the core's tap map says the photo place chooses `kind` (every other picture// opens the viewer), the picture turns the fan into the kind's values as the// core words them, and a tap writes the value through the record's own write.// Every value here is invented.import CottonFacesimport CottonValuesimport XCTest@testable import CottonUI@MainActorfinal class DeckPictureChoosesTests: XCTestCase {    private let target = DeckEditTarget(table: "event", record: "e1", date: "starts_at")    private let gym = TableRecord(id: "e1", values: ["title": .text("Invented gym at 7"), "kind": .text("gym"),                                                     "picture": .text("invented-db/photos/e1.jpg")])    #if os(iOS)    /// RULE: an event's picture chooses its kind — which picture chooses is    /// the core's tap map (`shell_input_tests.rs`) — and the fan offers the    /// kind's values as the core words them, in its order, each writing the    /// column it chooses.    func testAnEventsKindIsOfferedInTheCoresWords() throws {        let rows = [OpenedChoice(value: "gym", words: "Gym"), OpenedChoice(value: "dinner", words: "Dinner")]        let items = EditingFan.items(choosing: rows, column: "kind")        XCTAssertEqual(items.map(\.id), ["choice-gym", "choice-dinner"])        XCTAssertEqual(items.first { $0.id == "choice-dinner" }?.word, "Dinner")        XCTAssertEqual(items.first { $0.id == "choice-dinner" }?.does, .choice(column: "kind", value: "dinner"))    }    #endif    /// RULE: the picture tapped turns the fan into the kind's values with the picture the card's selected part; a second tap puts the fan back; a value tapped writes the kind through the record's own write and the fan returns to its tools.    func testAKindTappedIsWritten() async throws {        let editor = DeckEditor.standing()        editor.select(.picture, record: "e1")        editor.openOptions(target, column: "kind", record: gym)        XCTAssertEqual(editor.editing, .options(target, column: "kind"))        XCTAssertEqual(editor.target, target)        XCTAssertEqual(editor.selected(on: "e1"), .picture)        XCTAssertNil(editor.selected(on: "e2"), "another card is not selected")        editor.openOptions(target, column: "kind", record: gym)        XCTAssertFalse(editor.isEditing, "a second tap puts the fan back")        let store = WrittenRecords()        editor.openOptions(target, column: "kind", record: gym)        editor.save(target, patch: ["kind": .text("dinner")], store: store)        XCTAssertFalse(editor.isEditing, "the fan returns to its tools")        XCTAssertEqual(editor.marked?.record, "e1", "the save leaves its ink mark")        for _ in 0 ..< 100 where store.written.isEmpty { try await Task.sleep(for: .milliseconds(10)) }        XCTAssertEqual(store.written.count, 1)        XCTAssertEqual(store.written.first?.table, "event")        XCTAssertEqual(store.written.first?.id, "e1")        XCTAssertEqual(store.written.first?.patch, ["kind": .text("dinner")])    }}/// The record's writes, kept as they were asked.private final class WrittenRecords: Records, @unchecked Sendable {    struct Write {        let table: String        let id: String        let patch: [String: RecordValue]    }    private let lock = NSLock()    private var writes: [Write] = []    var written: [Write] { lock.withLock { writes } }    func tableDefinitions() async throws -> [TableDefinition] { [] }    func count(table: String) async throws -> Int { 0 }    func lastChanged(table: String) async throws -> String? { nil }    func toolDefinitions() async throws -> [ToolDefinition] { [] }    func list(table: String, project: String?, query: String?, page: Int, where filter: [String: RecordValue]) async throws -> RecordPage {        RecordPage(records: [], next: nil)    }    func linkTitles(for links: [RecordLink]) async throws -> LinkTitles { LinkTitles([:]) }    func get(table: String, id: String) async throws -> TableRecord? { nil }    func create(table: String, data: [String: RecordValue]) async throws -> ChangeRecord { throw RecordsError.unknownTable(table) }    func update(table: String, id: String, patch: [String: RecordValue]) async throws -> ChangeRecord {        lock.withLock { writes.append(Write(table: table, id: id, patch: patch)) }        throw RecordsError.unknownTable(table)    }    func delete(table: String, id: String) async throws -> ChangeRecord { throw RecordsError.unknownTable(table) }    func reorder(table: String, ids: [String]) async throws -> ChangeRecord { throw RecordsError.unknownTable(table) }    func link(table: String, id: String, column: String, target: String) async throws -> ChangeRecord { throw RecordsError.unknownTable(table) }    func undo(change: String) async throws -> ChangeRecord { throw RecordsError.unknownChange(change) }}