Cotton code
95 lines · 5.8 KB · SwiftRaw
// The board's tiles: the database's package tools, each drawn from its home// table's definition.import CottonValuesimport XCTest@testable import CottonUIfinal class BoardLayoutTests: XCTestCase {    /// RULE: A manifest tool uses its first table face, and opens all its tables in manifest order.    func testClassroomManifestMakesABoardTile() throws {        let resources = CottonUIResources.bundle?.resourceURL ?? SourceSweep.iosSources.appendingPathComponent("CottonUI/Resources")        let root = resources.appendingPathComponent("Fixtures/classroom")        let manifest = try ToolDefinition.decode(Data(contentsOf: root.appendingPathComponent("manifest.json")))        let tables = try manifest.tables.map {            try TableDefinition.decode(Data(contentsOf: root.appendingPathComponent("tables/\($0).json")))        }        let items = BoardLayout.items(definitions: TableDefinitions(tables), dataTools: ToolDefinitions([manifest]))        XCTAssertEqual(items.map(\.id), ["data-classroom"])        XCTAssertEqual(items.last?.table, "lesson")        XCTAssertEqual(BoardLayout.tables(for: manifest, in: TableDefinitions(tables)).map(\.id),                       ["lesson", "class", "student", "grade"])        XCTAssertNil(BoardLayout.directTable(for: manifest, in: TableDefinitions(tables)))        var single = manifest        single.tables = ["lesson"]        XCTAssertEqual(BoardLayout.directTable(for: single, in: TableDefinitions(tables))?.id, "lesson")    }    /// RULE: Tool pictures decode both asset names and synced photo references without changing the shared manifest format.    func testManifestPhotoPicture() throws {        let data = Data(#"{"id":"art","name":"Art","picture":{"photo":"photo:abc"},"tables":["work"],"instructions":"Draw","automations":[],"author":{"device":"test","name":"Artist"},"signature":null}"#.utf8)        let tool = try ToolDefinition.decode(data)        XCTAssertEqual(tool.picture?.drawn, .photo("photo:abc"))        XCTAssertEqual(try ToolDefinition.decode(JSONEncoder().encode(tool)), tool)    }    /// RULE: A tool tile counts every matching record across pages while retaining the first page for its face.    func testTileCountIncludesLaterPages() async throws {        let definition = table("work", picture: nil)        let rows = (0..<73).map { TableRecord(id: "work-\($0)", values: ["title": .text("Work \($0)")]) }        let store = FixtureRecords(definitions: [definition], records: ["work": rows])        let read = try await ToolTileRead.load(table: "work", project: nil, query: nil, store: store)        XCTAssertEqual(read.count, 73)        XCTAssertEqual(read.records.count, FixtureRecords.pageSize)        let filtered = try await ToolTileRead.load(table: "work", project: nil, query: "Work 72", store: store)        XCTAssertEqual(filtered.count, 1)    }    /// RULE: A Home section's count comes from the same read as its rows — page 0's total — never a second count read after it, so the heading reads one number on every visit (S6).    func testTileCountComesWithItsFirstPage() async throws {        let definition = table("work", picture: nil)        let rows = (0..<73).map { TableRecord(id: "work-\($0)", values: ["title": .text("Work \($0)")]) }        let store = FixtureRecords(definitions: [definition], records: ["work": rows])        let face = try await ToolTileRead.first(table: "work", project: nil, query: nil, store: store)        XCTAssertEqual(face.count, 73)        XCTAssertNil(face.next)        let filled = try await face.filled(table: "work", project: nil, query: nil, definition: definition, store: store)        XCTAssertEqual(filled.count, 73)    }    /// RULE: The fixture Classroom package publishes its tool after compiled tools in tool_order and resolves linked titles on its table tiles.    // RULE: Retired-surface and fixed call-count pins are deleted under the owner's test policy.    // RULE: Bundled fixture reads are bounded and refuse an oversized package entry.    func testOversizedBundledJSONIsRefused() throws {        let url = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)        defer { try? FileManager.default.removeItem(at: url) }        try Data(repeating: 32, count: 1_048_577).write(to: url)        XCTAssertThrowsError(try FixtureDefinitions.readBundledJSON(url))    }    private func table(_ id: String, picture: String?, tool key: String? = nil) -> TableDefinition {        var definition = TableDefinition(            id: id, name: id, plural: id, picture: picture.map(ToolPicture.asset),            columns: [ColumnDefinition(key: "title", type: .text)], title: "title",            slip: SlipLayout(title: "title"), tile: TileLayout(face: .slipList),            page: [PageSection(heading: nil, columns: ["title"])])        definition.tool = key.map { TableTool(name: id, key: $0) }        return definition    }    /// RULE: a project section's tile opens the section's own screen (`ProjectSections.route`, one function for the tile and the page) and wears its tool's photograph even where the database holds no such tool.    func testAProjectSectionTileOpensItsSectionAndWearsItsTool() {        let memory = HomeTile(id: "project/memory/kiln", tool: "memory", name: "Memory")        XCTAssertEqual(HomeTiles.route(of: memory), .projectsMemory(slug: "kiln"))        XCTAssertEqual(HomeTiles.route(of: HomeTile(id: "project/procedures/kiln", tool: "notes")),                       .projectsSlugSection(slug: "kiln", section: "procedures"))        XCTAssertNil(HomeTiles.section(of: "project/unknown/kiln"))        let placed = HomeTiles(tiles: [memory]).place([])        XCTAssertEqual(placed.map(\.key), [memory.key])        XCTAssertEqual(placed.first?.name, "Memory")        XCTAssertEqual(placed.first?.wears, "memory")    }}