Cotton code
52 lines · 3.6 KB · SwiftRaw
// Groups (Spec 6, note 8c089dd6; owner 5 Oct 2026): a tool's records gathered// by hand into named groups, drawn as a row of stacks above its list. The// table says only that it gathers groups and which of the existing faces a// stack is piled from; a group is a record of its own, read and written// through the same verbs every other record goes through.import CottonKitimport Foundationimport XCTest@testable import CottonUI@MainActorfinal class GroupKeysTests: XCTestCase {    private func table(_ id: String, extra: String = "") throws -> TableDefinition {        let json = """            {"id": "\(id)", "name": "\(id.capitalized)", "plural": "\(id.capitalized)s", "builtin": true,             "columns": [{"key": "name", "name": "Name", "type": "text"}, {"key": "of", "name": "Of", "type": "text"}],             "title": "name", "slip": {"title": "name"}, "tile": {"face": "slip-list"},             "page": [{"heading": null, "columns": ["name"]}]\(extra)}            """        return try JSONDecoder().decode(TableDefinition.self, from: Data(json.utf8))    }    /// RULE: A table gathers groups only when its definition says so with groups, naming one of the existing faces for its stacks; a definition without the key has no groups, and the key survives a round trip.    func testATableSaysItGathersGroupsAndWhichFaceItsStacksArePiledFrom() throws {        let people = try table("person", extra: #", "groups": {"stack": "polaroids"}"#)        XCTAssertEqual(people.groups, TableGroups(stack: .polaroids))        XCTAssertNil(try table("note").groups, "a table that never said groups gathers them")        let again = try JSONDecoder().decode(TableDefinition.self, from: JSONEncoder().encode(people))        XCTAssertEqual(again.groups?.stack, .polaroids, "groups is lost when a definition is written back")        XCTAssertThrowsError(try table("todo", extra: #", "groups": {"stack": "caps"}"#), "a face no stack is drawn from was taken")        XCTAssertEqual(Set(TableGroups.Stack.allCases.map(\.rawValue)), ["polaroids", "slips", "covers"])    }    /// RULE: A write changes ONE thing about a group — its name, records put in, or records taken out — so each is a single change with one receipt and one Undo, however many records it names.    func testAGroupWriteChangesOneThing() {        XCTAssertEqual(GroupChange.name("Family").request(id: "g"), ["id": .text("g"), "name": .text("Family")])        XCTAssertEqual(GroupChange.add(["p1", "p2"]).request(id: "g"), ["id": .text("g"), "add": .list([.text("p1"), .text("p2")])])        XCTAssertEqual(GroupChange.remove(["p1"]).request(id: "g"), ["id": .text("g"), "remove": .list([.text("p1")])])        XCTAssertEqual(GroupRecords.members(of: RecordGroup(id: "g", name: "Family", of: "person")), ["group": .text("g")])    }    /// RULE: A group is read as the core answers it — id, name, the table it gathers and its count — and a store that keeps no groups draws no stacks and offers no group act.    func testAGroupIsReadAsTheCoreAnswersItAndAStoreWithoutGroupsOffersNone() throws {        let group = try JSONDecoder().decode(RecordGroup.self, from: Data(#"{"id":"g1","name":"Family","table":"person","count":4,"top":[]}"#.utf8))        XCTAssertEqual(group, RecordGroup(id: "g1", name: "Family", of: "person", count: 4))        XCTAssertNil(GroupRecords.grouping(FixtureRecords(definitions: [], records: [:])), "the fixtures keep no groups, yet were offered them")        XCTAssertNil(GroupRecords.name(typed: "  \n"), "a blank name made a group")        XCTAssertEqual(GroupRecords.name(typed: " Family "), "Family")    }}