Cotton code
137 lines · 9.7 KB · SwiftRaw
import CottonValuesimport XCTest@testable import CottonUI/// The deck reads the core's page (`shell.deck`) and draws it as answered:/// nothing on the phone fills, orders or wears a card again.final class DeckAnswerTests: XCTestCase {    private static let page = """    {"table": "todo", "view": "todos/today",     "cards": [{"id": "t1", "face": "todo.card", "pressing": true, "pace": "late", "corner": "Yesterday",                "slots": {"title": "Pay the invented rent", "check": false},                "pages": [{"label": "Notes", "text": "Before the fifth", "markdown": true}],                "stored": {"state": "open"}, "reach": {"phones": [], "emails": [], "call": false, "message": false, "email": false},                "position": "1 / 2", "tap": {"card": "open.opened", "check": "check"}, "acts": ["complete", "when"],                "toggle": {"status": "done"},                "record": {"id": "t1", "data": {"title": "Pay the invented rent", "status": "open"}}},               {"id": "b1", "face": "book.hardback", "pressing": false, "slots": {"title": "Invented Dune"},                "pages": [], "stored": {}, "reach": {"phones": [], "emails": [], "call": false, "message": false, "email": false},                "position": "2 / 2", "tap": {"card": "open.reader"}, "acts": [],                "record": {"id": "b1", "data": {"title": "Invented Dune"}}}],     "acts": {"complete": {"label": "Mark done", "kind": "command", "red": false}, "check": {"label": "Tick or untick", "kind": "toggle", "red": false}},     "labels": {"by_slot": {"date": "Due"}, "by_column": {"title": "Title"}},     "count": 2, "head": null, "empty": null, "hand": [], "worn": null,     "plus": {"does": "doors", "label": "Add a book", "doors": [        {"id": "find", "label": "Find a book", "picture": "act-catalogue", "accepts": {"words": {"act": "find_book", "argument": "query", "lands": "book"}}},        {"id": "file", "label": "Add a file", "picture": "act-parcel", "accepts": {"files": {"types": ["com.adobe.pdf"], "many": true}}}]},     "visitor": null, "next": null}    """    /// RULE (boundary review move 2, 5 Nov 2026): a deck card is drawn as the core answered it — its face, places, readings and the labels its page names once.    func testACardIsDrawnAsAnswered() throws {        let answer = try JSONDecoder().decode(DeckAnswer.self, from: Data(Self.page.utf8))        XCTAssertEqual(answer.cards.map(\.id), ["t1", "b1"], "the core's order")        let card = answer.cards[0].drawn(labels: answer.labels)        XCTAssertTrue(card.answered)        XCTAssertEqual(card.face, "todo.card")        XCTAssertEqual(card.pace, .late)        XCTAssertEqual(card.checked, false)        XCTAssertEqual(card.pages, [CardPage(label: "Notes", text: "Before the fifth", markdown: true)])        XCTAssertEqual(card.labels[slot: "date"], "Due")        XCTAssertEqual(card.stored["state"], "open")        XCTAssertEqual(card.record, "t1")        XCTAssertEqual(answer.cards[0].record.record.id, "t1")        XCTAssertEqual(answer.cards[0].position, "1 / 2")        XCTAssertEqual(answer.cards[0].toggle, ["status": .text("done")])    }    /// RULE: what a tap on a card opens is the core's (`tap.card`), never decided again on the phone.    func testATapOpensWhatTheCoreSays() throws {        let answer = try JSONDecoder().decode(DeckAnswer.self, from: Data(Self.page.utf8))        XCTAssertEqual(answer.cards.map(\.opens), [.opened, .reader])        XCTAssertEqual(answer.acts["check"]?.kind, "toggle")    }    /// RULE (owner, 7 Nov 2026): a Music list's deck leads with the list's own card, its cassette: it says the list it stands for, its record is the list's row, and its tap plays the list.    func testAListsOwnCardStandsForTheListAndPlaysIt() throws {        let page = """        {"table": "song", "view": "music/group:g1",         "cards": [{"id": "g1", "face": "list.cassette", "list": "g1", "pressing": false,                    "slots": {"title": "Night Drives", "count": "2 songs", "length": "8:43",                              "songs": [{"record": "s1", "title": "Slow Train", "duration": 302, "play": "media:s1"}]},                    "pages": [], "stored": {}, "reach": {"phones": [], "emails": [], "call": false, "message": false, "email": false},                    "position": "1 / 3", "tap": {"card": "open.play"}, "acts": [],                    "record": {"id": "g1", "table": "record_group", "data": {"name": "Night Drives", "table_id": "song"}}}],         "acts": {"open.play": {"label": "Play the list", "kind": "open", "red": false}},         "labels": {"by_slot": {}, "by_column": {}}, "count": 3, "head": null, "empty": null, "hand": [], "worn": null,         "plus": {"does": "blank", "label": null, "doors": []}, "visitor": null, "next": null}        """        let answer = try JSONDecoder().decode(DeckAnswer.self, from: Data(page.utf8))        let list = try XCTUnwrap(answer.cards.first)        XCTAssertEqual(list.list, "g1")        XCTAssertEqual(list.opens, .play)        XCTAssertEqual(list.record.table, "record_group")        let card = list.drawn(labels: answer.labels)        XCTAssertEqual(card.face, "list.cassette")        XCTAssertEqual(card.record, "g1")        XCTAssertEqual(card.entries("songs").first?["record"]?.text, "s1")        XCTAssertNil(try JSONDecoder().decode(DeckAnswer.self, from: Data(Self.page.utf8)).cards[0].list, "a member's card stands for no list")    }    /// RULE (owner, 11 Nov 2026: ink searches the app's whole database): a result of another table says its table and is drawn by that table's labels; a fan view among the results says its tool and view, and its tap opens that view.    func testInkResultsAreEachOfTheirOwnTable() throws {        let page = """        {"table": "book", "view": "books/reading",         "cards": [{"id": "h1", "face": "highlight.passage", "table": "highlight", "pressing": false, "slots": {"title": "Rest is silence"},                    "pages": [], "stored": {}, "reach": {"phones": [], "emails": [], "call": false, "message": false, "email": false},                    "position": "1 / 2", "tap": {"card": "open.reading"}, "acts": [],                    "record": {"id": "h1", "table": "highlight", "data": {"text": "Rest is silence"}}},                   {"id": "view:books/reading", "kind": "view", "face": "fan.view", "tool": "books", "table": "book", "pressing": false,                    "view": {"id": "reading", "label": "Reading", "count": 2, "default": true, "read": {"table": "book", "where": {"status": "reading"}}},                    "tile": null, "slots": {"title": "Reading"}, "pages": [], "stored": {},                    "reach": {"phones": [], "emails": [], "call": false, "message": false, "email": false},                    "position": "2 / 2", "tap": {"card": "open.view"}, "acts": [],                    "record": {"id": "view:books/reading", "table": "book", "data": {"name": "Reading"}}}],         "acts": {"open.view": {"label": "Open the view", "kind": "open", "red": false}},         "labels": {"by_slot": {}, "by_column": {"title": "Title"}},         "labels_of": {"book": {"by_slot": {}, "by_column": {"title": "Title"}}, "highlight": {"by_slot": {"quote": "Passage"}, "by_column": {"text": "Text"}}},         "count": 2, "head": null, "empty": null, "hand": [], "worn": null,         "plus": {"does": "blank", "label": null, "doors": []}, "visitor": null, "next": null}        """        let answer = try JSONDecoder().decode(DeckAnswer.self, from: Data(page.utf8))        let highlight = answer.cards[0]        XCTAssertEqual(highlight.table, "highlight")        XCTAssertNil(highlight.opensView)        let labels = try XCTUnwrap(answer.labels_of?["highlight"])        XCTAssertEqual(highlight.drawn(labels: labels).labels[slot: "quote"], "Passage")        let view = try XCTUnwrap(answer.cards[1].opensView)        XCTAssertEqual(view.tool, "books")        XCTAssertEqual(view.view.id, "reading")        XCTAssertEqual(view.view.count, 2)        XCTAssertNil(answer.cards[1].tile)        XCTAssertNil(try JSONDecoder().decode(DeckAnswer.self, from: Data(Self.page.utf8)).labels_of, "a deck no letters searched")    }    /// RULE (coordinator, 4 Nov 2026): Plus does what the core says, its doors each a word and a picture, a door's words sent to its act and a door's files chosen in the picker.    func testPlusIsTheCoresAnswer() throws {        let plus = try JSONDecoder().decode(DeckAnswer.self, from: Data(Self.page.utf8)).plus        XCTAssertEqual(plus.does, "doors")        XCTAssertEqual(plus.doors.map(\.picture), ["act-catalogue", "act-parcel"])        XCTAssertEqual(plus.doors[0].accepts.words?.create, DefinitionCreateAct(name: "find_book", argument: "query", lands: "book"))        XCTAssertEqual(plus.doors[1].accepts.files?.many, true)    }    /// RULE: the deck holds no copy of the rules that left for the core — no order kept on the phone, no face worn there, no filling of an answered card, no tap decided.    func testTheDeckReadsAndDecidesNone() throws {        let deck = SourceSweep.withoutComments(            try SourceSweep.mustRead("apps/cotton/Sources/CottonUI/Deck/DeckSurface.swift", ownedBy: "deck"))        for gone in ["DeckOrder", "DeckCardTap", "PlusDoes", "faceChoices?.worn", "DeckCardFill.filled", "RecordListModel(", "model: RecordListModel", "InkColumns(definition).holds"] {            XCTAssertFalse(deck.contains(gone), "\(gone) left for the core")        }        XCTAssertTrue(deck.contains("DeckReading.retaining("))        let fill = SourceSweep.withoutComments(            try SourceSweep.mustRead("apps/cotton/Sources/CottonUI/Deck/DeckCardFill.swift", ownedBy: "deck"))        XCTAssertTrue(fill.contains("var filled = card.answered ? card"), "an answered card is drawn as it came")    }}