Cotton code
81 lines · 4.7 KB · SwiftRaw
// An empty deck says its line alone where the card would stand (the// coordinator, 4 Nov 2026: Books' empty Reading drew a blank grey card over// its line in the Books Plus walk).import CottonFacesimport CottonPaperimport SwiftUIimport XCTest#if canImport(UIKit)import UIKit#endif@testable import CottonUI@MainActorfinal class DeckEmptyLineTests: XCTestCase {    /// RULE: an empty deck's line stands where its card's centre would: the 4:5 card as large as the room allows, laid from the room's top.    ///    /// WHY: the line alone on the ground sits where the eye already is, with the acts tiered under it as they are under a card.    func testTheLineStandsAtTheCardsCentre() {        // A tall room: the card is as wide as the room.        let tall = CGSize(width: 360, height: 640)        XCTAssertEqual(DeckEmptyLine.centre(in: tall), 360 / CardPaperKnobs.aspect / 2, accuracy: 0.001)        // A short room: the card is as tall as the room, its centre half way.        let short = CGSize(width: 360, height: 300)        XCTAssertEqual(DeckEmptyLine.centre(in: short), 150, accuracy: 0.001)        XCTAssertEqual(DeckEmptyLine.centre(in: .zero), 0)    }    /// RULE: no deck draws a card, a sheet or a tool's picture when it is empty: every empty state of a deck (`DeckSurface`, `CardDeck`) is `DeckEmptyLine`.    ///    /// WHY: the Books Plus walk's shot 01 — a blank grey card over "A place for your next book."    func testNoEmptyDeckDrawsAnObject() throws {        for path in ["Deck/DeckSurface.swift", "Deck/CardDeck.swift"] {            let code = SourceSweep.withoutComments(try SourceSweep.mustRead("apps/cotton/Sources/CottonUI/" + path, ownedBy: "deck"))            XCTAssertFalse(code.contains("QuietState("), "\(path) draws an empty object")            XCTAssertFalse(code.contains("ObjectEmpty("), "\(path) draws an empty object")            XCTAssertTrue(code.contains("DeckEmptyLine("), "\(path) says its empty line")        }    }    #if canImport(UIKit)    /// RULE: on every ground of its own an empty deck's line takes the ground's ink whole (`DeckGround.ink(.ink)`: light on a dark ground, the print's on a light one), never its pencil on a dark ground, and stands at least 4.5:1 (WCAG AA) against the ground's colour; on paper it keeps the theme's secondary ink — one rule for every ground, no app named.    ///    /// WHY: Books 1.0.0 (3) and (4) walks, 7 Nov 2026: "A place for your next book." in the cream pencil read low on the navy felt; the 12 Nov identity walk: paper's grey vanished on the terrazzo, the rose linen and the orange felt.    func testTheLineOnItsGroundTakesTheGroundsInk() {        let theme = Theme.paper        func luminance(_ colour: Color) -> CGFloat {            var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0            UIColor(colour).getRed(&r, green: &g, blue: &b, alpha: &a)            func linear(_ c: CGFloat) -> CGFloat { c <= 0.03928 ? c / 12.92 : pow((c + 0.055) / 1.055, 2.4) }            return 0.2126 * linear(r) + 0.7152 * linear(g) + 0.0722 * linear(b)        }        XCTAssertTrue(FaceGround.feltNavy.dark, "the books' felt is a dark ground")        XCTAssertFalse(FaceGround.terrazzo.dark, "the money's terrazzo is a light ground")        for ground in FaceGround.allCases where ground != .paper {            let ink = DeckEmptyLine.ink(on: ground, theme: theme)            XCTAssertEqual(ink, DeckGround.ink(.ink, on: ground, theme: theme), "\(ground): its ink")            if ground.dark {                XCTAssertNotEqual(ink, DeckGround.ink(.pencil, on: ground, theme: theme), "\(ground): never the pencil")            }            let (light, back) = (luminance(ink), luminance(ground.colour))            XCTAssertGreaterThanOrEqual((max(light, back) + 0.05) / (min(light, back) + 0.05), 4.5, "\(ground): the line stands clear of the ground")        }        XCTAssertEqual(DeckEmptyLine.ink(on: nil, theme: theme), theme.colors.inkSecondary.color)        XCTAssertEqual(DeckEmptyLine.ink(on: .paper, theme: theme), theme.colors.inkSecondary.color)    }    #endif    /// RULE: the empty line renders its words, and its one verb where it has one.    func testTheLineDrawsAtTheLargestTextSize() throws {        let line = DeckEmptyLine(title: "A place for your next book.", message: "Synthetic second line",                                 actionLabel: "Back up", actionIdentifier: "deck-back-up", action: {})            .frame(width: 393, height: 560)            .dynamicTypeSize(.accessibility5)            .environment(\.theme, Theme.paper)            .background(Theme.paper.colors.groundMuted.color)        let renderer = ImageRenderer(content: line)        renderer.scale = 2        XCTAssertNotNil(renderer.cgImage)    }}