Cotton code
94 lines · 5.5 KB · SwiftRaw
import CoreTextimport CottonKitimport XCTest@testable import CottonUI/// A business flavour's look is data — a theme row by the flavour's name —/// worn where its database wears no row of its own; Cotton has none and draws/// exactly as before.@MainActorfinal class FlavourLookTests: XCTestCase {    private actor WornCore: CottonCalling {        let answer: String        init(_ 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 {}    }    private static let shippedDefault = ##"{"ok":true,"data":{"definition":{"id":"paper","name":"Cotton","family":"paper"},"drawn":"default"}}"##    private static let ownRow = ##"{"ok":true,"data":{"definition":{"id":"studio","family":"paper","palette":{"light":{"accent":"#2E6B8A"}}},"drawn":"row"}}"##    private func look(_ name: String) throws -> FlavourLook {        GalleryFaces.kitchen    }    /// RULE: a business look decodes from its row (the kitchen fixture, Nancy's as she shipped it) — her logo's colours: a cream sheet under dark green headings, terracotta accent, near-black body.    func testNancysLookReadsAsDarkGreenHeadingsOnACreamSheet() throws {        let theme = try look("kitchen").theme        XCTAssertEqual(theme.family, .plain)        XCTAssertEqual(theme.palette.light.paper, ThemeColor(hex: "#FFFBF3"))        XCTAssertEqual(theme.palette.light.ground, ThemeColor(hex: "#F7F0E3"))        XCTAssertEqual(theme.palette.light.ink, ThemeColor(hex: "#1F4A3A"))        XCTAssertEqual(theme.palette.light.inkBody, ThemeColor(hex: "#2B2621"))        XCTAssertEqual(theme.palette.light.accent, ThemeColor(hex: "#B4532A"))    }    /// RULE: Nancy's card is her theme's data, as nan-*.png draw it — her cream card (`palette.faces.cream`) on her warmer page, 26 pt corners, no grain, a soft drop under it.    func testNancysCardIsHerCreamRoundedCard() throws {        let theme = try look("kitchen").theme        XCTAssertEqual(theme.palette.light.faces["cream"], ThemeColor(hex: "#FFFBF3"))        XCTAssertEqual(theme.scale.radius(for: CGSize(width: 360, height: 450)), 26)        XCTAssertFalse(CardMaterial.grain(theme), "no mottle, no rim")        XCTAssertTrue(CardMaterial.drops(theme))        XCTAssertEqual(CardMaterial.inset(theme, width: 360), 0)    }    /// RULE: no app ships a look now (the business apps left, 22 Nov 2026): every flavour in `App/Flavours`, and Cotton's own, wears the database's theme as it always did.    func testNoFlavourShipsALook() throws {        let root = SourceSweep.repoRoot.appendingPathComponent("apps/cotton")        var flavours = [try JSONDecoder().decode(FlavourManifest.self, from: Data(contentsOf: root.appendingPathComponent("App/CottonManifest/flavour.json"))).flavour]        for folder in try FileManager.default.contentsOfDirectory(atPath: root.appendingPathComponent("App/Flavours").path) where !folder.hasPrefix(".") {            flavours.append(try JSONDecoder().decode(FlavourManifest.self, from: Data(contentsOf: root.appendingPathComponent("App/Flavours/\(folder)/flavour.json"))).flavour)        }        XCTAssertTrue(flavours.contains("days"))        for flavour in flavours {            XCTAssertNil(FlavourLook.shipped(for: flavour), "\(flavour) wears the database's theme as it always did")        }        XCTAssertNil(FlavourLook.shipped(for: nil))        XCTAssertNil(FlavourLook.current, "a test bundle carries no manifest, so no look")    }    /// RULE: a flavour's look is worn from the first frame and wherever the database wears the shipped default, and a row the database chose wins over it.    func testTheLookDressesTheDefaultAndGivesWayToTheDatabasesOwnRow() async throws {        let nancy = try look("kitchen")        let model = ThemeModel(themes: CoreThemes(core: WornCore(Self.shippedDefault)), look: nancy)        XCTAssertEqual(model.theme, nancy.theme, "a business app's cold start is already its look")        await model.refresh()        XCTAssertEqual(model.theme, nancy.theme)        let chosen = ThemeModel(themes: CoreThemes(core: WornCore(Self.ownRow)), look: nancy)        await chosen.refresh()        XCTAssertEqual(chosen.theme.palette.light.accent, ThemeColor(hex: "#2E6B8A"), "the database's own row is the database's")        XCTAssertEqual(chosen.theme.family, .paper)    }    /// RULE: with no look — Cotton — the window draws exactly the theme the core answers, default or row.    func testNoLookDrawsTheCoresThemeUnchanged() async {        let model = ThemeModel(themes: CoreThemes(core: WornCore(Self.shippedDefault)), look: nil)        XCTAssertEqual(model.theme, .paper)        await model.refresh()        XCTAssertEqual(model.theme, Theme.decode(Data(#"{"id":"paper","name":"Cotton","family":"paper"}"#.utf8)))    }    /// RULE: the flavour's faces are bundled and register, so Montserrat draws as Montserrat and not as the system's stand-in.    func testMontserratIsBundledAndMeasuresAsItself() {        for (face, name) in [(ThemeFace.montserrat, CottonFonts.brandRegularFace), (.montserratSemibold, CottonFonts.brandHeadingFace)] {            XCTAssertEqual(face.postScriptName, name)            XCTAssertEqual(CTFontCopyPostScriptName(face.measuring(14)) as String, name, "\(name) is registered from the bundle")        }    }}