// What the list surface does with the keys that shape it: a list section's// limit, the head's acts told the list's scope, and a slip's own acts drawn// under it and run through the same path as the head's. Every record is invented.importCottonKitimportCottonValuesimportFoundationimport XCTest@testable importCottonUIfinalclassListShapeTests: XCTestCase {privatefunc definition(_ extra: String = "") throws -> TableDefinition {tryTableDefinition.decode(Data(""" {"id":"example","name":"Example","plural":"Examples","builtin":false, "columns":[{"key":"title","type":"text"},{"key":"scope","type":"text"}], "title":"title","slip":{"title":"title"},"tile":{"face":"slip-list"}, "page":[{"heading":null,"columns":["title"]}]\(extra)} """.utf8)) }/// RULE: A list section with a limit draws that many slips at most and one without draws every slip; a limited heading counts the drawn against the whole ("3 of 40"), as a board's section does.func testALimitedSectionDrawsItsFirstFew() {let records = (1...5).map { TableRecord(id: "example-\($0)", values: [:]) } XCTAssertEqual(RecordsSection.drawn(records, limit: 3).map(\.id), ["example-1", "example-2", "example-3"]) XCTAssertEqual(RecordsSection.drawn(records, limit: nil).count, 5) XCTAssertEqual(RecordsSection.drawn(records, limit: 9).count, 5) XCTAssertEqual(RecordsSection.drawn(records, limit: 0), [])// The heading still counts every record read, against the few drawn. XCTAssertEqual(RecordsSection.count(drawn: 5, read: 5, more: false), "5") XCTAssertEqual(RecordsSection.count(drawn: 3, read: 40, more: false), "3 of 40") XCTAssertEqual(RecordsSection.count(drawn: 3, read: 40, more: false), SectionFace.count(shown: 3, total: 40)) XCTAssertEqual(RecordsSection.count(drawn: 3, read: 50, more: true), "3 of 50+") }// ── Acts ─────────────────────────────────────────────────────────────/// An invented table whose list acts depend on its scope and whose/// first record's slip offers one act.privatestructExampleSource: SettingsTableSource {let table = "example"func records(_ core: any CottonCalling) asyncthrows -> [TableRecord] { [TableRecord(id: "example-1", values: ["title": .text("One")]),TableRecord(id: "example-2", values: ["title": .text("Two")])] }func listActions(_ rows: [TableRecord], where filter: [String: RecordValue]) -> [RecordAction] {guardlet scope = filter["scope"]?.text else { return [RecordAction(id: "begin", label: "Begin")] }return [RecordAction(id: "begin:\(scope)", label: "Begin")] }func slipActions(_ record: TableRecord, core: any CottonCalling) async -> [RecordAction] { record.id == "example-1" ? [RecordAction(id: "poke", label: "Poke", confirmation: WriteConfirmation(title: "Poke it?", message: "", confirmLabel: "Poke"))] : [] }func perform(_ action: String, record: TableRecord?, core: any CottonCalling) asyncthrows -> SettingsActed { .outcome(.said("\(action) \(record?.id ?? "none")")) } }/// RULE: Three or more acts that each make a record by asking its fields fold into one "+" at the list's head; two, or acts that take a typed line, stay as their own links.func testThreeOrMoreNewActsFoldIntoOnePlus() {let field = ActField(key: "name", label: "Name")let new = { (id: String) inRecordAction(id: id, label: "New \(id)", fields: [field]) } XCTAssertTrue(RecordActionButtons.folds([new("login"), new("key"), new("note"), new("card"), new("ID")])) XCTAssertFalse(RecordActionButtons.folds([new("login"), new("key")]), "two links read at a glance") XCTAssertFalse(RecordActionButtons.folds([new("login"), new("key"), RecordAction(id: "find", label: "Find", input: RecordActionInput(prompt: "A name"))])) XCTAssertFalse(RecordActionButtons.folds([new("login"), new("key"), RecordAction(id: "import", label: "Import")]), "an act that asks nothing is not a New") }/// RULE: A list's head acts are asked for with the list's where, so a scoped list can offer what only it can, and a store that ignores the scope offers the same acts.func testTheHeadsActsAreToldTheListsScope() asyncthrows {let records = SettingsRecords(core: ScriptedCore(), definitions: [try definition()], sources: [ExampleSource()])let whole = await records.listActions(table: "example") XCTAssertEqual(whole.map(\.id), ["begin"])let scoped = await records.listActions(table: "example", where: ["scope": .text("garden")]) XCTAssertEqual(scoped.map(\.id), ["begin:garden"])// The protocol's own default forwards: a store that knows no scope.let unscoped = awaitUnscoped().listActions(table: "example", where: ["scope": .text("garden")]) XCTAssertEqual(unscoped.map(\.id), ["begin"])let none = awaitUnscoped().slipActions(table: "example", record: TableRecord(id: "example-1", values: [:])) XCTAssertEqual(none, [], "a store offers no slip act unless it says so") }/// RULE: A slip's own acts are drawn under its face and run through the list's act path with the record, asking first where the act asks, and a slip that offers none draws none.func testASlipsActsAreDrawnUnderItAndPerformedOnItsRecord() asyncthrows {let records = SettingsRecords(core: ScriptedCore(), definitions: [try definition()], sources: [ExampleSource()])let first = TableRecord(id: "example-1", values: ["title": .text("One")])let acts = await records.slipActions(table: "example", record: first) XCTAssertEqual(acts.map(\.id), ["poke"]) XCTAssertNotNil(acts.first?.confirmation)let second = await records.slipActions(table: "example", record: TableRecord(id: "example-2", values: [:])) XCTAssertEqual(second, [])let outcome = tryawait records.perform(try XCTUnwrap(acts.first), table: "example", record: first) XCTAssertEqual(outcome, .said("poke example-1"), "the act runs on the slip's record, read again") }privatestructUnscoped: ActingRecords {func listActions(table: String) async -> [RecordAction] { [RecordAction(id: "begin", label: "Begin")] }func pageActions(table: String, record: TableRecord) async -> [RecordAction] { [] }func perform(_ action: RecordAction, table: String, record: TableRecord?) asyncthrows -> ActionOutcome { .said("") } }}