Cotton code
56 lines · 3.0 KB · SwiftRaw
// The board draws the core's answer: `parts.board`, in its order, a hidden part// absent, with no Swift decision about which parts exist or whether they show.import CottonKitimport Foundationimport SwiftUIimport XCTest@testable import CottonUIextension BoardEntry {    /// A part decoded from the JSON shape `shell.today` answers.    static func forTests(_ json: String) throws -> BoardEntry {        try JSONDecoder().decode(BoardEntry.self, from: Data(json.utf8))    }    /// A `notices` part naming every notice, as the core resolves one.    static func noticesForTests() throws -> BoardEntry {        let shows = BoardNoticeKind.allCases.map { "\"\($0.rawValue)\"" }.joined(separator: ",")        return try forTests(#"{"id":"notices","kind":"notices","shown":false,"why":{"key":"quiet","words":"Nothing to say."},"settings":{"backup_behind":"{behind}","backup_unset":"Unset.","backup_open":"Open","shows":["#            + shows + "]}}")    }}@MainActorfinal class BoardDrawsTheAnswerTests: XCTestCase {    private func context() -> BoardHome {        let home = HomeModel(core: ScriptedCore([:]), toasts: ToastCenter(scheduler: ManualToastScheduler()))        return BoardHome(model: home, databaseName: nil, project: nil, now: Date())    }    private func entry(_ id: String, _ kind: String, shown: Bool = true) throws -> BoardEntry {        let why = shown ? "null" : #"{"key":"quiet","words":"Nothing."}"#        let extra = kind == "head" ? #","asked":{"items":[],"total":0},"done":{"items":[]},"tags":[]"# : ""        return try .forTests(#"{"id":"\#(id)","kind":"\#(kind)","shown":\#(shown),"why":\#(why),"settings":{}\#(extra)}"#)    }    /// RULE: The board draws an answer's parts in the answer's order, a part the core says is hidden absent.    func testTheBoardDrawsTheAnswersPartsInOrderWithAHiddenPartAbsent() throws {        let entries = [try entry("sync", "sync"), try entry("head", "head"), try entry("asked", "asked", shown: false), try entry("today", "today"), try entry("list#2", "list")]        let rows = BoardSurface<EmptyView>.rows(entries, context: context())        XCTAssertEqual(rows.map(\.entry.id), ["sync", "head", "today", "list#2"], "the answer's order, the hidden part gone")        XCTAssertEqual(rows.map(\.gap), [0, 20, 20, 20])    }    /// RULE: A notice stands 16 under the head; a `notices` part the core hides still draws for what only this window holds.    func testANoticeUnderTheHeadAndThisWindowsOwnNotice() throws {        let hidden = try BoardEntry.noticesForTests()        let context = context()        XCTAssertEqual(BoardSurface<EmptyView>.rows([try entry("head", "head"), hidden], context: context).map(\.entry.id), ["head"], "nothing to say: no room")        let project = BoardHome(model: context.model, databaseName: nil, project: (name: "Invented", slug: "invented"), now: Date())        let rows = BoardSurface<EmptyView>.rows([try entry("head", "head"), hidden], context: project)        XCTAssertEqual(rows.map(\.entry.id), ["head", "notices"])        XCTAssertEqual(rows.map(\.gap), [0, 16])    }}