// The shell writes a card's words (owner, 28 Oct 2026: a face has one// state, view) in the editor the core answers (`shell.input`): the reader// asks, draws the line or the page, commits the words with the `writes` the// ask answered, and says what came of it. What a commit writes is the core's// (`shell_input_tests.rs`).importCottonFacesimportCottonKitimportCottonValuesimport XCTest@testable importCottonUI@MainActorfinalclassDeckWritingTests: XCTestCase {privatelet target = DeckEditTarget(table: "todo", record: "t1", date: "due_at")privatefunc definition(_ face: String = "todo.card") throws -> TableDefinition {let json = """ {"id":"todo","name":"Todo","plural":"Todos","builtin":false, "columns":[{"key":"title","name":"Title","type":"text"},{"key":"notes","name":"Notes","type":"longtext"}, {"key":"due_at","name":"Due","type":"date"}], "title":"title","date":"due_at","project":null,"order":null, "slip":{"title":"title"},"tile":{"face":"slip-list"},"page":[],"commands":[],"tables":[], "create":{"label":"New todo"}, "card":{"face":"\(face)","fields":[{"slot":"title","column":"title"},{"slot":"body","column":"notes"}]}} """returntry JSONDecoder().decode(TableDefinition.self, from: Data(json.utf8)) }/// The core's answers to the input's calls, as a todo's.privatefunc core(refusing: String? = nil, draftCard: String = "null") -> InputCore {InputCore { method, request inswitch (method, request["op"]?.string, request["kind"]?.string ?? request["writes"]?["kind"]?.string) {case ("shell.input", "ask", "column"):let page = request["column"]?.string == "notes"return .success(InputCore.json(""" {"editor":"\(page ? "page" : "line")","words":"\(page ? "Two kilos" : "Buy rice")","title":"Buy rice", "keeps_words":\(!page),"writes":{"kind":"column","table":"todo","id":"t1","column":"\(request["column"]?.string ?? "")"}} """))case ("shell.input", "ask", "group"):return .success(InputCore.json(#"{"editor":"line","words":"","hint":"Name the list","keeps_words":false,"writes":{"kind":"group","table":"todo","id":"t1"}}"#))case ("shell.input", "ask", "new"):return .success(InputCore.json(#"{"editor":"line","words":"","hint":"New todo","keeps_words":true,"writes":{"kind":"new","table":"todo","draft":"draft-1","column":"title"}}"#))case ("shell.input", "commit", _):iflet refusing { return .success(.object(["refusal": .string(refusing)])) }if request["writes"]?["kind"]?.string == "new" {return .success(InputCore.json(#""" {"made":"t9","change":{"id":"c9","byline":"You","record_id":"t9","undoable":true}, "record":{"id":"t9","data":{"title":"Call Ravi"}}, "next":{"editor":"page","words":"","title":"Call Ravi","keeps_words":false,"writes":{"kind":"column","table":"todo","id":"t9","column":"notes"}}} """#)) }return .success(InputCore.json(#"{"change":{"id":"c1","byline":"You","record_id":"t1","undoable":true},"record":{"id":"t1","data":{"title":"Buy brown rice"}}}"#))case ("shell.draft", _, _):return .success(InputCore.json(#"{"id":"draft-1","table":"todo","values":{"due_at":"2026-10-28"},"card":\#(draftCard),"staged":{"acts":[]}}"#))case ("shell.opened", _, _):return .success(InputCore.json(#"{"acts":[],"parts":{"title":{"does":"line","column":"title"}}}"#))default:return .failure(CottonCallError(code: "missing", message: "Not here")) } } }/// RULE: a tapped part's words are asked of the core, and the line or the/// page stands as it answers, its words and title its; the part is/// selected while it stands.func testATappedPartOpensTheEditorTheCoreAnswers() asyncthrows {let core = core()let editor = DeckEditor.standing() editor.select(.title, record: "t1") editor.write(target, column: "title", store: nil, core: core)tryawait eventually { editor.writing != nil }let line = try XCTUnwrap(editor.writing) XCTAssertEqual(line.editor, .line) XCTAssertEqual(line.words, "Buy rice") XCTAssertTrue(line.keepsWords) XCTAssertEqual(line.record, "t1") XCTAssertEqual(core.asked("shell.input", op: "ask").first?["id"], "t1") XCTAssertEqual(editor.selected(on: "t1"), .title) XCTAssertNil(editor.selected(on: "t2"), "another card is not selected") editor.dismissWriting() XCTAssertNil(editor.selected(on: "t1"), "the input gone, nothing is selected") editor.write(target, column: "notes", store: nil, core: core)tryawait eventually { editor.writing != nil } XCTAssertEqual(editor.writing?.editor, .page) XCTAssertEqual(editor.writing?.title, "Buy rice") }/// RULE: a commit hands the core its words with the writes the ask/// answered, and the write it answers is said to the deck it was made on;/// a refusal is said in the core's words.func testACommitIsTheCoresWrite() asyncthrows {let core = core()let editor = DeckEditor.standing() editor.write(target, column: "title", store: nil, core: core)tryawait eventually { editor.writing != nil } editor.commit(" Buy brown rice ") XCTAssertNil(editor.writing, "committed, the input goes")tryawait eventually { editor.landed != nil }let sent = try XCTUnwrap(core.asked("shell.input", op: "commit").first) XCTAssertEqual(sent["words"], " Buy brown rice ", "the words go as typed: the core trims them") XCTAssertEqual(sent["writes"], InputCore.json(#"{"kind":"column","table":"todo","id":"t1","column":"title"}"#)) XCTAssertEqual(editor.landed?.table, "todo") XCTAssertEqual(editor.marked?.record, "t1", "the edit leaves its ink mark")let refusing = self.core(refusing: "Give the list a name.")let named = DeckEditor.standing() named.write(target, column: "title", store: nil, core: refusing)tryawait eventually { named.writing != nil } named.commit(" ")tryawait eventually { named.refusal != nil } XCTAssertEqual(named.refusal?.sentence, "Give the list a name.") }/// RULE: a new card is the core's draft (`shell.draft`): its title is/// asked as the create's, the commit says what the create made, the card/// stands as that record, and the body follows on the whole screen where/// the core answers it next.func testANewCardIsTheCoresDraft() asyncthrows {let core = core()let editor = DeckEditor.standing()var made: TableRecord? editor.create(try definition(), preset: ["due_at": .text("2026-10-28")], store: nil, core: core) { made = $0 }tryawait eventually { editor.writing != nil }let begun = try XCTUnwrap(core.asked("shell.draft").first) XCTAssertEqual(begun["values"], InputCore.json(#"{"due_at":"2026-10-28"}"#)) XCTAssertEqual(editor.writing?.hint, "New todo") XCTAssertEqual(editor.draft?.id, "draft-1") XCTAssertEqual(editor.openedTarget?.record, "draft-1") editor.commit("Call Ravi")tryawait eventually { made != nil && editor.writing != nil } XCTAssertEqual(made?.id, "t9") XCTAssertEqual(editor.draft?.isNew, false) XCTAssertEqual(editor.openedTarget?.record, "t9", "the card stands as the record made") XCTAssertEqual(editor.writing?.editor, .page, "the body follows, as the core answered") XCTAssertNil(core.asked("shell.input", op: "commit").first?["follows"], "a todo's face writes its own parts") }/// RULE: the plus card left open once its record is made wears the/// record's card as the store reads it (`@card`), never the pre-title/// draft card: the commit's change row carries no card, so it is read/// again (Rigby 11 walk, 8 Nov 2026: a new todo stood as a blank strip).func testAMadeCardWearsTheCardItsRecordIsReadWith() asyncthrows {let core = core(draftCard: #"{"face":"todo.card","fields":{"title":""}}"#)var read = TableRecord(id: "t9", values: ["title": .text("Call Ravi")]) read.values[DeckCard.key] = .object(["face": .text("todo.card"), "fields": .object(["title": .text("Call Ravi")])])let editor = DeckEditor.standing()var made: TableRecord? editor.create(try definition(), preset: [:], store: MadeRecord(read), core: core) { made = $0 }tryawait eventually { editor.writing != nil } XCTAssertEqual(editor.draft?.card.fields["title"], .text(""), "new: the core's draft card") editor.commit("Call Ravi")tryawait eventually { made != nil && editor.landed != nil }tryawait eventually { editor.draft?.card.fields["title"] == .text("Call Ravi") } XCTAssertEqual(editor.draft?.isNew, false) }/// RULE: on a face that tags no part the shell says so on the create's/// commit (`follows`), and the core answers the body next.func testATornPageAsksItsBodyNext() asyncthrows {let core = core()let editor = DeckEditor.standing() editor.create(try definition("note.page"), preset: [:], store: nil, core: core) { _ in }tryawait eventually { editor.writing != nil } editor.commit("Spare key")tryawait eventually { !core.asked("shell.input", op: "commit").isEmpty } XCTAssertEqual(core.asked("shell.input", op: "commit").first?["follows"], .bool(true)) }/// RULE: a draft's When and Repeat are staged on the core's draft, its/// halves as chosen; the core works out the value.func testADraftsChoicesAreStagedOnTheCore() asyncthrows {let core = core()let editor = DeckEditor.standing() editor.create(try definition(), preset: [:], store: nil, core: core) { _ in }tryawait eventually { editor.writing != nil } editor.dismissWriting()let drafted = DeckEditTarget(table: "todo", record: "draft-1", date: "due_at") editor.writeWhen(drafted, day: .set("2026-11-20"), time: .keep, store: nil, core: core) editor.writeWhen(drafted, day: .keep, time: .set("09:30"), store: nil, core: core)tryawait eventually { core.asked("shell.draft").count == 3 }let stages = core.asked("shell.draft").dropFirst().map { $0["stage"] } XCTAssertEqual(stages, [InputCore.json(#"{"when":{"date":"2026-11-20"}}"#), InputCore.json(#"{"when":{"time":"09:30"}}"#)]) XCTAssertTrue(core.asked("shell.draft").dropFirst().allSatisfy { $0["id"] == "draft-1" }) } #if os(iOS)/// RULE: A face that tags no part (a note's torn page) has its title and/// body written from the long-press fan, first on it, where the core's/// parts say they write words; a face that tags its parts offers none.func testANoteIsWrittenFromTheFan() throws {let note = try definition("note.page")let parts = ["title": CardPart(does: "line", column: "title"), "body": CardPart(does: "page", column: "notes")]let items = EditingFan.items(writing: note, parts: parts) XCTAssertEqual(items.map(\.word), ["Title", "Notes"]) XCTAssertEqual(items.map(\.does), [.write(column: "title"), .write(column: "notes")]) XCTAssertTrue(EditingFan.items(writing: try definition(), parts: parts).isEmpty, "a todo's card writes its own parts") XCTAssertTrue(EditingFan.items(writing: note, parts: [:]).isEmpty, "nothing the core says it writes") } #endif}/// The record a create made, as the store reads it back with its card.privatefinalclassMadeRecord: Records, @unchecked Sendable {privatelet record: TableRecordinit(_ record: TableRecord) { self.record = record }func tableDefinitions() asyncthrows -> [TableDefinition] { [] }func count(table: String) asyncthrows -> Int { 1 }func list(table: String, project: String?, query: String?, page: Int, where filter: [String: RecordValue]) asyncthrows -> RecordPage {RecordPage(records: [record], next: nil, total: 1) }func get(table: String, id: String) asyncthrows -> TableRecord? { id == record.id ? record : nil }func create(table: String, data: [String: RecordValue]) asyncthrows -> ChangeRecord { throwRecordsError.unknownRecord(table) }func update(table: String, id: String, patch: [String: RecordValue]) asyncthrows -> ChangeRecord { throwRecordsError.unknownRecord(id) }func delete(table: String, id: String) asyncthrows -> ChangeRecord { throwRecordsError.unknownRecord(id) }func reorder(table: String, ids: [String]) asyncthrows -> ChangeRecord { throwRecordsError.noOrderColumn(table) }func link(table: String, id: String, column: String, target: String) asyncthrows -> ChangeRecord { throwRecordsError.unknownRecord(id) }func undo(change: String) asyncthrows -> ChangeRecord { throwRecordsError.unknownChange(change) }}