import CottonValues import Foundation import XCTest @testable import CottonUI final class RecordPageLayoutTests: XCTestCase { private func definition() throws -> TableDefinition { try TableDefinition.decode(Data(#""" {"id":"example","name":"Example","plural":"Examples","builtin":true,"engine":"example", "columns":[{"key":"title","type":"text"},{"key":"date","type":"date"}, {"key":"state","type":"choice"},{"key":"token","type":"text","secret":true}, {"key":"body","type":"longtext","shown_when":{"column":"state","values":["failed"]}}, {"key":"person","type":"link","link_to":"person"},{"key":"extra","type":"text"}], "title":"title","slip":{"title":"title"},"tile":{"face":"slip-list"}, "page":[{"columns":["title","date","state","token","body","person","extra"]}, {"heading":"Existing children","role":"side","list":{"table":"child","link":"parent","where":{"status":"open"}}}], "parts":{"shape":"sheet","head":{"facts":["date","token"],"state":"state"},"links":["person"],"body":"body", "lists":[{"table":"child","relation":"parent","heading":"Children"}], "acts":{"main":"read_now","more":["pause"],"away":"disconnect"}}} """#.utf8)) } func testValueMatchedListsUseTheParentColumnAndNeverReadAnEmptyParent() throws { let matched = try JSONDecoder().decode(PageParts.List.self, from: Data(#"{"table":"repository","match":{"column":"host","parent":"extra"},"heading":"Repositories","main_act":"hold"}"#.utf8)) let record = TableRecord(id: "connection:invented-host", values: ["extra": .text(" git.example.test ")]) XCTAssertEqual(matched.parentFilter(record), ["host": .text("git.example.test")]) XCTAssertTrue(matched.matches(SectionList(table: "repository", where: ["host": .text("git.example.test")]), parent: record)) XCTAssertFalse(matched.matches(SectionList(table: "repository", where: ["host": .text("another.example.test")]), parent: record)) for value in [RecordValue.null, .text(" \n "), .number(12), .bool(true), .list([.text("host")]), .object(["host": .text("value")])] { XCTAssertNil(matched.parentFilter(TableRecord(id: record.id, values: ["extra": value]))) } } // RULE: The source still executes its act; the definition only selects its place. This must retain fields, confirmations and entered text. func testSourceActionsUseDeclarationWithoutLosingTheirWriteContract() throws { let layout = RecordPageLayout(definition: try definition(), record: TableRecord(id: "example-one", values: [:])) let question = WriteConfirmation(title: "Disconnect?", message: "Stops reading.", confirmLabel: "Disconnect", destructive: true) let actions = [RecordAction(id: "read-now", label: "Read now").command("read_now"), RecordAction(id: "disconnect", label: "Disconnect", confirmation: question), RecordAction(id: "other", label: "Other", input: RecordActionInput(prompt: "Value")).entering(" exact ")] let placed = layout.actions(actions) XCTAssertEqual(placed.map(\.placement), [.main, .away, .more]) XCTAssertEqual(placed.map(\.id), actions.map(\.id)) XCTAssertEqual(placed[1].confirmation, question) XCTAssertEqual(placed[2].entered, " exact ") XCTAssertEqual(placed[2].input, actions[2].input) } func testTheTodosMainActDoesNotAskFirst() throws { let loaded = FixtureDefinitions.load(from: try TestResources.bundle()) let definition = try XCTUnwrap(loaded.definitions.first { $0.id == "todo" }) let layout = RecordPageLayout(definition: definition, record: TableRecord(id: "synthetic", values: ["status": .text("open")])) XCTAssertFalse(layout.asksFirst(try XCTUnwrap(layout.main))) } func testDeclaredNativeActReplacesSourceActOnceAndDraftsKeepTheirOwnActs() throws { var definition = try definition() definition.commands = try JSONDecoder().decode([RecordCommand].self, from: Data(#"[{"id":"read_now","label":"Read now","kind":"update","method":"connections.readNowByKind"},{"id":"pause","label":"Pause","kind":"update","method":"connections.pause"}]"#.utf8)) let layout = RecordPageLayout(definition: definition, record: TableRecord(id: "synthetic", values: [:])) let foot = layout.foot(actions: [RecordAction(id: "read-now", label: "Read now").command("read_now"), RecordAction(id: "extra", label: "Extra")], sourceOwned: true) XCTAssertEqual(foot.main?.id, "read_now") XCTAssertEqual(foot.more.count, 0, "the source did not offer pause on this row") XCTAssertEqual(foot.source.map(\.id), ["extra"], "the legacy execution path is not duplicated") let draft = layout.foot(actions: [RecordAction(id: "connect", label: "Connect", placement: .main)], sourceOwned: true) XCTAssertNil(draft.main) XCTAssertEqual(draft.source.map(\.id), ["connect"]) XCTAssertEqual(layout.foot(actions: [], sourceOwned: false).more.map(\.id), ["pause"]) } }