SPB Git

Repository Public License

TokenTests.swift 5.5 KB 113 lines swift

1import Foundation2import SwiftUI3import Testing4@testable import LunarDesign56@Suite struct LunarTokenTests {7    @Test func corePaletteMatchesSpecification() {8        #expect(LunarPalette.obsidian.hexString == "#080B15")9        #expect(LunarPalette.midnight.hexString == "#101522")10        #expect(LunarPalette.lunarSlate.hexString == "#171F35")11        #expect(LunarPalette.silver.hexString == "#E6EAFA")12        #expect(LunarPalette.spectralViolet.hexString == "#927ADB")13        #expect(LunarPalette.lunarBlue.hexString == "#9BAEFF")14        #expect(LunarPalette.auroraMint.hexString == "#75DFC9")15        #expect(LunarPalette.all.count == 7)16    }1718    @Test func spacingAndRadiiMatchSpecification() {19        #expect(LunarSpacing.scale == [8, 12, 16, 24, 32])20        #expect(LunarRadius.scale == [10, 16, 22, 28, 36])21        #expect(LunarSpacing.unit == 4)22        #expect(LunarSpacing.scale.allSatisfy { $0.truncatingRemainder(dividingBy: LunarSpacing.unit) == 0 })23        #expect(LunarMetrics.touchTarget == 44)24    }2526    @Test func hexParsingRoundTrips() throws {27        for hex in ["#080B15", "#E6EAFA", "#927ADB", "#000000", "#FFFFFF", "#12345680"] {28            let color = try #require(LunarRGBA(hexString: hex))29            #expect(color.hexString == hex)30        }31        #expect(LunarRGBA(hexString: "9baeff")?.hexString == "#9BAEFF")32        #expect(LunarRGBA(hexString: "#abc")?.hexString == "#AABBCC")33        #expect(LunarRGBA(hexString: "  #75DFC9 ")?.hexString == "#75DFC9")34        for invalid in ["", "#12", "#12345", "#GGGGGG", "#1234567", "navy", "#123456789"] {35            #expect(LunarRGBA(hexString: invalid) == nil)36        }37        for value: UInt32 in [0x000000, 0xFFFFFF, 0x9BAEFF, 0x171F35, 0x010203] {38            #expect(LunarRGBA(hexString: LunarRGBA(hex: value).hexString) == LunarRGBA(hex: value))39        }40    }4142    @Test func relativeLuminanceAndContrastFollowWCAG() {43        #expect(LunarRGBA.black.relativeLuminance == 0)44        #expect(abs(LunarRGBA.white.relativeLuminance - 1) < 1e-9)45        #expect(abs(LunarRGBA.white.contrastRatio(against: .black) - 21) < 1e-9)46        #expect(abs(LunarRGBA.black.contrastRatio(against: .white) - 21) < 1e-9)47        #expect(LunarRGBA.white.contrastRatio(against: .white) == 1)48        // Reference value from the WCAG definition: #777777 on white is 4.48:1 (just under AA).49        let grey = LunarRGBA(hex: 0x777777)50        #expect(abs(grey.contrastRatio(against: .white) - 4.478) < 0.01)51        // A translucent foreground is composited on its backdrop first.52        #expect(abs(LunarRGBA.black.withAlpha(0.5).composited(over: .white).red - 0.5) < 1e-9)53        #expect(LunarRGBA.white.withAlpha(0).contrastRatio(against: .black) == 1)54    }5556    @Test func mixingInterpolates() {57        let mid = LunarRGBA.black.mixed(with: .white, 0.5)58        #expect(abs(mid.red - 0.5) < 1e-9 && abs(mid.green - 0.5) < 1e-9 && abs(mid.blue - 0.5) < 1e-9)59        #expect(LunarPalette.lunarBlue.mixed(with: .white, 0) == LunarPalette.lunarBlue)60        #expect(LunarPalette.lunarBlue.mixed(with: .white, 1) == .white)61    }6263    @Test func tintsMirrorTheWorkspaceCases() {64        #expect(LunarTint.allCases.map(\.rawValue) == [65            "lunarBlue", "spectralViolet", "auroraMint", "silver", "solar", "rose", "ocean", "forest", "ember",66            "nebula",67        ])68        #expect(LunarTintPalette.dark[.lunarBlue] == LunarPalette.lunarBlue)69        #expect(LunarTintPalette.dark[.spectralViolet] == LunarPalette.spectralViolet)70        #expect(LunarTintPalette.dark[.auroraMint] == LunarPalette.auroraMint)71        for palette in [LunarTintPalette.dark, .light] {72            #expect(Set(LunarTint.allCases.map { palette[$0] }).count == LunarTint.allCases.count)73        }74    }7576    @Test func motionIsCriticallyOrOverDampedEnoughToSettle() {77        for motion in LunarMotion.allCases {78            #expect(motion.response > 0 && motion.response < 1)79            #expect(motion.dampingFraction >= 0.8 && motion.dampingFraction <= 1)80        }81    }8283    @Test func typeScaleCoversEveryRole() {84        #expect(LunarTextStyle.allCases.count == 12)85        #expect(LunarTextStyle.allCases.allSatisfy { !$0.systemStyleName.isEmpty })86    }8788    @Test func tokenDocumentIsCompleteAndDeterministic() throws {89        let document = LunarTokenDocument.current90        #expect(document.themes.count == 12)91        #expect(document.palette["obsidian"] == "#080B15")92        #expect(document.palette["lunarSlate"] == "#171F35")93        #expect(document.glyphs.count == 20)94        #expect(document.tints.count == 10)95        #expect(document.themes.allSatisfy { $0.dark.count == $0.light.count && $0.dark["tint.nebula"] != nil })96        let first = try document.jsonData(), second = try LunarTokenDocument.current.jsonData()97        #expect(first == second)98        #expect(try JSONDecoder().decode(LunarTokenDocument.self, from: first) == document)99    }100101    /// The checked-in `DesignSystem/tokens.json` must match the code. Regenerate with102    /// `scripts/render-design-reference.sh --tokens`.103    @Test(.disabled(if: ReferenceRun.tokensEnabled, "the file is being rewritten by this run"))104    func exportedTokensFileIsInSync() throws {105        let url = ReferencePaths.designSystem.appendingPathComponent("tokens.json")106        guard FileManager.default.fileExists(atPath: url.path) else {107            Issue.record("DesignSystem/tokens.json is missing; run scripts/render-design-reference.sh --tokens")108            return109        }110        #expect(try Data(contentsOf: url) == LunarTokenDocument.current.jsonData())111    }112}113
Files main
Menu

Theme