Cotton code
80 lines · 4.5 KB · SwiftRaw
import CottonKitimport XCTest@testable import CottonUI/// The window wears the theme the core answers, and keeps what it drew when a/// read fails.@MainActorfinal class ThemeModelTests: XCTestCase {    private actor ThemeCore: CottonCalling {        var answer: String        init(_ answer: String) { self.answer = answer }        func set(_ answer: String) { self.answer = answer }        func callRaw(_ method: String, requestJSON: String) async -> String {            method == CottonMethod.definitionsCatalogue ? CatalogueAnswers.wearing(answer) : #"{"ok":false,"error":{"code":"missing","message":"no"}}"#        }        func subscribe(_ handler: @escaping @Sendable (CottonEvent) -> Void) async {}        func close() async {}    }    func testTheDatabasesOwnThemeIsWornAndAFailedReadKeepsIt() async {        let core = ThemeCore(##"{"ok":true,"data":{"definition":{"id":"studio","family":"plain","palette":{"light":{"accent":"#2E6B8A"}}},"drawn":"row"}}"##)        let model = ThemeModel(themes: CoreThemes(core: core))        XCTAssertEqual(model.theme, .paper, "a cold start draws Cotton's own theme")        await model.refresh()        XCTAssertEqual(model.theme.family, .plain)        XCTAssertEqual(model.theme.colors.accent, ThemeColor(hex: "#2E6B8A"))        await core.set(#"{"ok":false,"error":{"code":"unreadable","message":"locked"}}"#)        await model.refresh()        XCTAssertEqual(model.theme.family, .plain, "a failed read keeps what the window drew")    }    func testAPassedOverRowDrawsTheDefaultAndKeepsTheReason() async {        let core = ThemeCore(##"{"ok":true,"data":{"definition":{"id":"paper","name":"Cotton","family":"paper"},"drawn":"default","passed_over":"the theme \"studio\" stored here is not drawn: its palette is malformed"}}"##)        let model = ThemeModel(themes: CoreThemes(core: core))        await model.refresh()        XCTAssertEqual(model.theme, Theme.decode(Data(#"{"id":"paper","name":"Cotton","family":"paper"}"#.utf8)))        XCTAssertEqual(model.theme.family, .paper)        XCTAssertNotNil(model.passedOver)    }    /// A core that keeps the one theme row it was last given, as the crate's    /// `definitions.saveTheme` does, and answers it at the next read.    private actor SavingCore: CottonCalling {        var saved: String?        func callRaw(_ method: String, requestJSON: String) async -> String {            switch method {            case CottonMethod.definitionsSaveTheme:                let request = try? JSONSerialization.jsonObject(with: Data(requestJSON.utf8)) as? [String: Any]                let definition = request?["definition"] ?? [:]                saved = String(decoding: (try? JSONSerialization.data(withJSONObject: definition)) ?? Data(), as: UTF8.self)                return #"{"ok":true,"data":{"receipt":"Theme changed","undo":{"method":"definitions.saveTheme","args":{"definition":{"id":"database"}}}}}"#            case CottonMethod.definitionsCatalogue:                let definition = saved ?? #"{"id":"paper","family":"paper"}"#                return CatalogueAnswers.wearing(#"{"ok":true,"data":{"definition":"# + definition + #","drawn":"row"}}"#)            default:                return #"{"ok":false,"error":{"code":"missing","message":"no"}}"#            }        }        func subscribe(_ handler: @escaping @Sendable (CottonEvent) -> Void) async {}        func close() async {}    }    /// RULE: Wearing a brand kit is one act: the kit laid over the theme the database wears is written whole as its row, the window draws what the core then answers, and the act carries the core's receipt and undo.    func testABrandIsWornInOneActAndDrawnFromTheRow() async throws {        let core = SavingCore()        let model = ThemeModel(themes: CoreThemes(core: core))        await model.refresh()        let brand = JSONValue.object([            "colors": .array([.object(["name": .string("Accent"), "hex": .string("#2E6B8A")])]),            "fonts": .array([.object(["role": .string("body"), "family": .string("Inter")])]),        ])        let act = try await model.wear(brand: brand)        XCTAssertNotNil(act.receipt, "the act carries a receipt")        XCTAssertEqual(act.undo?.method, CottonMethod.definitionsSaveTheme)        XCTAssertEqual(model.theme.colors.accent, ThemeColor(hex: "#2E6B8A"))        XCTAssertEqual(model.theme.type.body, .system, "a family the binary does not carry is the system face")        XCTAssertEqual(model.theme.family, .paper, "nothing else about the theme moves")    }}