import CottonKit import Foundation import XCTest @testable import CottonUI /// Every definition row a database serves is drawn or said: the core names /// each row it leaves out of the catalogue's tables (`kept_as_rows`, /// `refused`), and the phone's reader logs them once by id with why /// (tables-dropped, 28 Oct 2026: 35 of Personal's 144 rows reached the fan as /// nothing at all). final class UndrawnTablesTests: XCTestCase { private final class Marker {} /// The kit household's answer (the fixture the core generates) drawing /// its `person` table alone, naming `kept` and `refused`. private func answer(kept: [[String: Any]], refused: [[String: Any]] = [], dropping key: String? = nil) throws -> Data { let bundle = try XCTUnwrap(CottonUIResources.bundle ?? CottonUIResources.locate(from: Bundle(for: Marker.self))) let url = try XCTUnwrap(bundle.url(forResource: "catalogue", withExtension: "json", subdirectory: "Fixtures")) var data = try XCTUnwrap(JSONSerialization.jsonObject(with: Data(contentsOf: url)) as? [String: Any]) let tables = try XCTUnwrap(data["tables"] as? [[String: Any]]) let pages = try XCTUnwrap(data["pages"] as? [String: Any]) data["tables"] = tables.filter { $0["id"] as? String == "person" } data["pages"] = ["person": try XCTUnwrap(pages["person"])] data["kept_as_rows"] = kept data["refused"] = refused if let key { data[key] = nil } return try JSONSerialization.data(withJSONObject: data) } /// RULE: A catalogue answer names every served row it does not draw — a storage table with no face kept as rows, a row the core refused — and the reader reads each by id with why; an answer that leaves either list out is not this build's shape and is refused whole. func testTheAnswerNamesEveryRowItDoesNotDraw() throws { let raw = try DefinitionCatalogues.read(answer( kept: [["id": "kept_mark", "reason": "no slip"]], refused: [["id": "broken", "reason": "bad column"]])) XCTAssertEqual(raw.undrawn, [ Undrawn(id: "kept_mark", reason: "no slip", kind: .keptAsRows), Undrawn(id: "broken", reason: "bad column", kind: .refused), ]) for key in ["kept_as_rows", "refused"] { XCTAssertThrowsError(try DefinitionCatalogues.read(answer(kept: [], dropping: key)), key) } } /// RULE: The reader says, once per process, how many tables it draws of the rows served and every row it draws nowhere by id, grouped by why — never a row left out silently. func testTheReaderSaysWhatItDrawsAndWhatItLeavesOutOnce() throws { let raw = try DefinitionCatalogues.read(answer(kept: [ ["id": "thread_mark", "reason": "no slip"], ["id": "dev_job", "reason": "no slip"], ])) let drawn = DefinitionCatalogue.tables(raw) XCTAssertEqual(drawn.tables.map(\.id), ["person"]) let database = "undrawn-\(UUID().uuidString)" let line = DefinitionCatalogue.undrawnLine(raw, drawn: drawn.tables.count, failures: drawn.failures, database: database) XCTAssertEqual(line, "definitions: \(database) draws 1 of 3 tables served; kept as rows (no slip): dev_job, thread_mark") let before = Log.ring.lines().filter { $0.text == line }.count DefinitionCatalogue.sayUndrawn(raw, drawn: 1, failures: [], database: database) DefinitionCatalogue.sayUndrawn(raw, drawn: 1, failures: [], database: database) XCTAssertEqual(Log.ring.lines().filter { $0.text == line }.count, before + 1, "said once") } }