Cotton code
96 lines · 5.8 KB · SwiftRaw
// "File into…": which tables a command that asks for a table offers.import XCTest@testable import CottonUIfinal class TablePickingTests: XCTestCase {    private func table(_ id: String, _ name: String, add: Bool? = nil, readonlyTitle: Bool = false, rank: Int? = nil) -> TableDefinition {        var definition = TableDefinition(            id: id, name: name, plural: name + "s",            columns: [ColumnDefinition(key: "title", type: .text, readonly: readonlyTitle)], title: "title",            slip: SlipLayout(title: "title"), tile: TileLayout(face: .slipList),            page: [PageSection(heading: nil, columns: ["title"])])        definition.add = add        definition.filingRank = rank        return definition    }    /// RULE: File into… offers every table that takes a record by its words — never the table it runs on, one nothing is typed into, or one whose title is read-only — the usual ones first, the rest by name.    func testOffersTablesThatTakeWordsUsualOnesFirst() {        let all = [            table("thought", "Thought"), table("lesson", "Lesson"), table("person", "Person", rank: 4),            table("thread", "Chat", add: false), table("note", "Note", rank: 2), table("call", "Call", readonlyTitle: true),            table("book", "Book"), table("todo", "Todo", rank: 1),        ]        let offered = TablePicking.choices(all, from: "thought", query: "").map(\.id)        XCTAssertEqual(offered, ["todo", "note", "person", "book", "lesson"])    }    /// RULE: File into… never offers a table that needs more than the words — an event needs when it starts — so a pick never ends in a refusal; a required column with a default is no bar.    func testSkipsTablesThatNeedMoreThanTheWords() {        var event = table("event", "Event")        event.columns.append(ColumnDefinition(key: "starts_at", type: .date, required: true))        var goal = table("goal", "Goal")        var status = ColumnDefinition(key: "status", type: .text, required: true)        status.defaultValue = .text("active")        goal.columns.append(status)        XCTAssertEqual(TablePicking.choices([event, goal, table("todo", "Todo", rank: 1)], from: "thought", query: "").map(\.id), ["todo", "goal"])    }    /// RULE: File into… never offers a table over a Settings source — an assistant, a member, a machine is how the app is set up, not where words are filed.    func testSkipsSettingsTables() throws {        // The kit marks a Settings table `setup`; nothing else says so.        var assistant = table("assistant", "Assistant")        assistant.setup = true        var setup = table("synthetic_setup", "Setup")        setup.setup = true        let decoded = try JSONDecoder().decode(TableDefinition.self, from: JSONEncoder().encode(assistant))        XCTAssertEqual(decoded.setup, true)        let all = [decoded, setup, table("book", "Book")]        XCTAssertEqual(TablePicking.choices(all, from: "thought", query: "").map(\.id), ["book"])    }    /// RULE: every table a Settings source shows that the kit defines is    /// marked `setup` there, so File into… needs no list of its own to skip    /// it (a Swift copy of Settings' tables stood beside the kit's mark).    func testTheKitMarksEverySettingsTableItDefines() throws {        let shown = Set((SettingsTables.entries + SettingsTables.tools + SettingsTables.routed + SettingsTables.code).map(\.table))        let kits = SourceSweep.repoRoot.appendingPathComponent("crates/cotton-core/kits")        var defined: [TableDefinition] = []        for kit in try FileManager.default.contentsOfDirectory(atPath: kits.path) {            for table in shown {                let file = kits.appendingPathComponent("\(kit)/tables/\(table).json")                guard FileManager.default.fileExists(atPath: file.path) else { continue }                defined.append(try TableDefinition.decode(Data(contentsOf: file)))            }        }        XCTAssertFalse(defined.isEmpty, "the kits define Settings tables")        XCTAssertEqual(defined.filter { $0.setup != true }.map(\.id).sorted(), [])    }    /// RULE: File into… never offers a table whose record belongs to one parent through a link the person never sets (a scene's story) — it is made from the parent — but a todo or a note is made by its own verb, whatever it links, and a link to a picture (a person's avatar) is no parent.    func testSkipsTablesThatBelongToAParent() {        var scene = table("story_scene", "Scene")        scene.columns.append(ColumnDefinition(key: "story", type: .link, linkTo: "story_story", readonly: true))        var todo = table("todo", "Todo", rank: 1)        todo.madeByVerb = true        todo.columns.append(ColumnDefinition(key: "recurrence_id", type: .link, linkTo: "todo_recurrence", readonly: true))        var event = table("event", "Event", rank: 3)        event.columns.append(ColumnDefinition(key: "people", type: .link, linkTo: "person", readonly: true, multiple: true))        var person = table("person", "Person", rank: 4)        var avatar = ColumnDefinition(key: "avatar_photo_id", type: .link, linkTo: "photo", readonly: true)        avatar.media = true        person.columns.append(avatar)        XCTAssertEqual(            TablePicking.choices([scene, todo, event, person], from: "thought", query: "").map(\.id), ["todo", "event", "person"])    }    /// RULE: The picker's search matches a table's name or its plural, case aside.    func testSearchMatchesNameOrPlural() {        let all = [table("lesson", "Lesson"), table("book", "Book"), table("todo", "Todo")]        XCTAssertEqual(TablePicking.choices(all, from: "thought", query: "LESS").map(\.id), ["lesson"])        XCTAssertEqual(TablePicking.choices(all, from: "thought", query: "books").map(\.id), ["book"])        XCTAssertEqual(TablePicking.choices(all, from: "thought", query: "zzz").map(\.id), [])    }}