// 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. import CottonKit import CottonValues import Foundation import XCTest @testable import CottonUI final class ListShapeTests: XCTestCase { private func definition(_ extra: String = "") throws -> TableDefinition { try TableDefinition.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. private struct ExampleSource: SettingsTableSource { let table = "example" func records(_ core: any CottonCalling) async throws -> [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] { guard let 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) async throws -> 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) in RecordAction(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() async throws { 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 = await Unscoped().listActions(table: "example", where: ["scope": .text("garden")]) XCTAssertEqual(unscoped.map(\.id), ["begin"]) let none = await Unscoped().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() async throws { 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 = try await 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") } private struct Unscoped: 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?) async throws -> ActionOutcome { .said("") } } }