// The keys a record that is handed on reads (`HandingKeys`): `hand_over`, // slip `links` and `attachments` — each decoded, read the // same when absent, and drawn once by the shared faces and surfaces, never by // one table's Swift. Every record is invented. import CottonKit import CottonValues import Foundation import XCTest @testable import CottonUI final class HandingKeysTests: XCTestCase { private static let bare = """ {"id":"example","name":"Example","plural":"Examples","builtin":false, "columns":[{"key":"words","type":"longtext"},{"key":"state","type":"choice","choices":["open","done"]}], "title":"words","slip":{"title":"words"},"tile":{"face":"slip-list"}, "page":[{"heading":null,"columns":["words"]}] """ private func definition(_ extra: String = "", slip: String? = nil) throws -> TableDefinition { var json = Self.bare if let slip { json = json.replacingOccurrences(of: #""slip":{"title":"words"}"#, with: #""slip":"# + slip) } return try TableDefinition.decode(Data((json + extra + "}").utf8)) } private func linked(_ links: [(String, String)], state: String = "done") -> TableRecord { TableRecord(id: "r1", values: [ "words": .text(" Buy example milk \n"), "state": .text(state), "links": .list(links.map { .object(["table": .text($0.0), "id": .text($0.1), "title": .text("Example \($0.0)")]) }), ]) } /// RULE: The three keys decode in the format's spelling and survive a round trip, and a definition without them reads exactly as before — no hand over, no links under the slip, a read-only strip — because every other table's lists and pages must not change. A definition written before the session inbox went, still carrying `send_to_session`, reads too, and offers nothing for it. func testTheKeysDecodeRoundTripAndTheirAbsenceReadsAsBefore() throws { let full = try definition( """ ,"hand_over":{"when":{"column":"state","values":["done"]}}, "attachments":{"editable":true,"sources":["camera","library"]}, "send_to_session":{"body":"words","record_as":"thoughtId"} """, slip: #"{"title":"words","links":{"except":["person"]}}"#) XCTAssertEqual(full.handOver, RecordHandOver(when: SectionWhen(column: "state", values: ["done"]))) XCTAssertEqual(full.attachments?.sources, [.camera, .library]) XCTAssertEqual(full.slip.links, LinkedSection(except: ["person"])) XCTAssertEqual(try JSONDecoder().decode(TableDefinition.self, from: JSONEncoder().encode(full)), full) let bare = try definition() XCTAssertNil(bare.handOver) XCTAssertNil(bare.slip.links) XCTAssertNil(bare.attachments) let record = linked([("todo", "t1")]) XCTAssertEqual(bare.slipLinks(of: record), []) let defaulted = try definition(#","attachments":{}"#) XCTAssertEqual(defaulted.attachments, AttachmentsLayout(editable: true, sources: [.library, .camera])) } /// RULE: Slip `links` names the record's links under its slip by the kind's word and an arrow ("Todo ›"), each opening its record through `recordOpener`, less the tables it leaves out — drawn by `SlipFace` for every table, so a filed capture keeps its links to what it became. func testSlipLinksAreTheKindsWordEachOpeningItsRecord() throws { let definition = try definition(slip: #"{"title":"words","links":{"except":["person"]}}"#) let links = definition.slipLinks(of: linked([("todo", "t1"), ("person", "p1"), ("note", "n1")])) XCTAssertEqual(links.map(\.key), ["todo:t1", "note:n1"]) } /// RULE: An editable strip writes the WHOLE list as the record's `attachments` through the generic `update_record` — `{kind, id}` only, so a caption is never promoted by a reorder — and the extras section draws it for any table that says so, from the page. func testAnEditableStripWritesTheWholeListThroughUpdateRecord() throws { let list = [ RecordAttachment(kind: "photo", id: "photo:a", position: 0), RecordAttachment(kind: "photo", id: "photo:b", position: 1), ] XCTAssertEqual( TableDefinition.attachmentsPatch(list), ["attachments": .list([ .object(["kind": .text("photo"), "id": .text("photo:a")]), .object(["kind": .text("photo"), "id": .text("photo:b")]), ])]) } }