import Foundation import XCTest @testable import CottonUI final class PagePartsTests: XCTestCase { /// The built-in `id` as the core hands it over (its catalogue's snapshot). private func shipped(_ id: String) throws -> TableDefinition { try XCTUnwrap(FixtureDefinitions.load().definitions.first { $0.id == id }, id) } func testExplicitPartsRoundTripBesideTheSectionArrayForEveryMappedKind() throws { for kind in ["note", "todo", "person", "event", "thread", "email_thread", "secret", "media"] { let value = try shipped(kind) let explicit = try XCTUnwrap(value.parts, kind) // The drawn page is the explicit one with any card the slips // table dealt given its width and acts; nothing else differs. var drawn = value.resolvedParts let undealt = { (cards: [PageParts.Card]?, own: [PageParts.Card]?) in cards?.enumerated().map { index, card in var card = card if own?[index].width == nil { card.width = nil } if own?[index].acts == nil { card.acts = nil } return card } } drawn.cards = undealt(drawn.cards, explicit.cards) drawn.strip = undealt(drawn.strip, explicit.strip) XCTAssertEqual(drawn, explicit, kind) XCTAssertFalse(value.page.isEmpty, "the existing section array remains available") let encoded = try JSONEncoder().encode(value) XCTAssertEqual(try TableDefinition.decode(encoded), value) let object = try XCTUnwrap(JSONSerialization.jsonObject(with: encoded) as? [String: Any]) XCTAssertNotNil(object["parts"] as? [String: Any]) XCTAssertNotNil(object["page"] as? [[String: Any]]) } } func testAnUnknownShapeRefusesTheContract() { XCTAssertThrowsError(try JSONDecoder().decode(PageParts.self, from: Data(""" {"shape":"panel","head":{"facts":[]},"links":[],"lists":[],"acts":{"more":[]}} """.utf8))) } }