Cotton code
74 lines · 4.1 KB · SwiftRaw
// The coding session face (KINDS, 7f4a5f0fb): the deck draws it on the// framed cream card, marks its state in the ink its stored value calls for,// and turns to its task list after it.import CottonValuesimport SwiftUIimport XCTest@testable import CottonUI/// RULE: a coding session's card is drawn by its own face on the framed cream stock, never the plain face; its state is marked green while it runs, red while it waits on the owner or has failed, pencil otherwise — read off the stored value, never the label's words; and its task list (`card.pages`) is the page after the face (faces lane, 23 Oct 2026).@MainActorfinal class SessionFaceTests: XCTestCase {    /// An invented session table, its card as the sessions package names it.    static func sessions() throws -> TableDefinition {        let json = """        {"id":"session","name":"Session","plural":"Sessions","builtin":false,         "columns":[{"key":"heading","name":"Heading","type":"text"},                    {"key":"state","name":"State","type":"choice","options":["queued","starting","running","idle","waiting","ended","failed","withdrawn"]},                    {"key":"last_line","name":"Last line","type":"text"},                    {"key":"machine","name":"Machine","type":"text"},                    {"key":"summary","name":"Summary","type":"longtext"},                    {"key":"tasks","name":"Task list","type":"longtext"}],         "title":"heading","date":null,"project":null,"order":null,         "slip":{"title":"heading"},"tile":{"face":"slip-list"},"page":[],         "card":{"face":"session","fields":[            {"slot":"title","column":"heading"},{"slot":"subtitle","column":"last_line"},            {"slot":"source","column":"machine"},            {"slot":"state","column":"state","only":["queued","starting","running","waiting","failed"]},            {"slot":"body","column":"summary"}],          "pages":["tasks"]},         "tables":[]}        """        return try JSONDecoder().decode(TableDefinition.self, from: Data(json.utf8))    }    func testTheDeckDrawsASessionOnTheFramedCard() {        let card = DeckCard(face: .session, fields: ["title": .text("Invented lane")])        XCTAssertTrue(DeckFace.draws(.session))        XCTAssertTrue(DeckFace.draws(card))    }    func testTheStateIsInkedByItsStoredValue() {        XCTAssertEqual(SessionStateMark.ink("running"), .green)        XCTAssertEqual(SessionStateMark.ink("waiting"), .red)        XCTAssertEqual(SessionStateMark.ink("failed"), .red)        XCTAssertEqual(SessionStateMark.ink("queued"), .pencil)        XCTAssertEqual(SessionStateMark.ink(nil), .pencil)    }    /// RULE: a session waiting on the owner says its last line as a question — in a speech bubble on its card — read off the stored value; no other state does (faces lane, 24 Oct 2026).    func testOnlyAWaitingSessionAsks() {        XCTAssertTrue(SessionStateMark.asks("waiting"))        for state in ["running", "failed", "queued", "ended"] { XCTAssertFalse(SessionStateMark.asks(state), state) }        XCTAssertFalse(SessionStateMark.asks(nil))    }    func testTheCardReadsTheStoredStateAndTurnsToTheTaskList() throws {        let definition = try Self.sessions()        let row = TableRecord(id: "s-1", values: [            "heading": .text("Invented lane"), "state": .text("waiting"), "tasks": .text("Draw the face\nPush"),        ])        let stored = DeckCardFill.stored(definition, record: row)        XCTAssertEqual(stored["state"], "waiting")        XCTAssertNil(stored["date"], "a place the card does not fill stores nothing")        XCTAssertEqual(DeckCardFill.pages(definition, record: row), [CardPage(label: "Task list", text: "Draw the face\nPush")])        XCTAssertEqual(DeckCardFill.pages(definition, record: TableRecord(id: "s-2", values: ["tasks": .text(" ")])), [],                       "an empty task list turns to no page")    }    func testTheSourceIsCutIntoChipsWhereTheCoreJoinedIt() {        let join = ShellLines.line("cards", "join")        XCTAssertEqual(DeckCard.chips("example-mac\(join)main\(join)model-x"), ["example-mac", "main", "model-x"])    }}