Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Tests/SPGlassTokensTests/GlassTokensTests.swift8// Module : SPGlassTokensTests9// Repository : https://git.spboucher.ai/sp-glasskit10// Project Website : https://glass.spboucher.ai11// Created : 2026-12-0212// Last Modified : 2026-12-0213//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : MIT33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Foundation42import Testing43import SPGlassCore44@testable import SPGlassTokens4546@Suite("Colour values")47struct GlassRGBATests {48 @Test("Hex parsing", arguments: [49 ("#FFFFFF", "#FFFFFF"), ("000000", "#000000"), ("#abc", "#AABBCC"), ("#6366f1", "#6366F1"),50 ("#11223380", "#11223380"), (" #0EA5E9 ", "#0EA5E9"), ("#112233FF", "#112233"),51 ])52 func parses(input: String, expected: String) throws {53 #expect(try GlassRGBA(hex: input).hexString == expected)54 }5556 @Test("Invalid hex is refused", arguments: ["", "#", "#12", "#12345", "#GGGGGG", "#1234567", "#123456789", "red", "#12 456", "#١٢٣٤٥٦", "#-12345"])57 func refuses(input: String) {58 #expect(throws: GlassColorError.self) { try GlassRGBA(hex: input) }59 }6061 @Test func componentsAreClamped() {62 let colour = GlassRGBA(red: 2, green: -1, blue: .nan, alpha: .infinity)63 #expect(colour.red == 1 && colour.green == 0 && colour.blue == 0 && colour.alpha == 0)64 }6566 @Test func literalInitialiserRoundTrips() {67 let colour = GlassRGBA(rgb: 0x6366F1)68 #expect(colour.hexString == "#6366F1")69 #expect(colour.rgbValue == 0x6366F1)70 }7172 @Test func contrastMatchesWCAGReferenceValues() {73 #expect(abs(GlassRGBA.black.contrastRatio(against: .white) - 21) < 0.001)74 #expect(abs(GlassRGBA.white.contrastRatio(against: .white) - 1) < 0.001)75 // #767676 on white is the classic 4.54:1 AA boundary.76 let grey = GlassRGBA(rgb: 0x767676)77 #expect(abs(grey.contrastRatio(against: .white) - 4.54) < 0.01)78 }7980 @Test func translucentColourIsCompositedBeforeMeasuring() {81 let half = GlassRGBA.black.opacity(0.5)82 #expect(half.composited(over: .white).hexString == "#808080")83 #expect(half.contrastRatio(against: .white) < GlassRGBA.black.contrastRatio(against: .white))84 }8586 @Test func codableUsesHexStrings() throws {87 let data = try JSONEncoder().encode([GlassRGBA(rgb: 0x10B981)])88 #expect(String(decoding: data, as: UTF8.self) == "[\"#10B981\"]")89 #expect(try JSONDecoder().decode([GlassRGBA].self, from: data) == [GlassRGBA(rgb: 0x10B981)])90 #expect(throws: DecodingError.self) { try JSONDecoder().decode([GlassRGBA].self, from: Data("[\"nope\"]".utf8)) }91 }92}9394@Suite("Palette and scales")95struct GlassPaletteTests {96 @Test func paletteDecodesKnownTokensAndRefusesUnknownOnes() throws {97 let palette = try JSONDecoder().decode(GlassPalette.self, from: Data(##"{"primary":"#112233","surface":"#FFFFFF"}"##.utf8))98 #expect(palette[.primary] == GlassRGBA(rgb: 0x112233))99 #expect(palette[.accent] == nil)100 #expect(palette.definedTokens == [.primary, .surface])101 #expect(throws: DecodingError.self) { try JSONDecoder().decode(GlassPalette.self, from: Data(##"{"blur":"#112233"}"##.utf8)) }102 }103104 @Test func paletteMergeKeepsUntouchedRoles() {105 let base = GlassPalette([.primary: .black, .accent: .white])106 let merged = base.merging(GlassPalette([.accent: .black]))107 #expect(merged[.primary] == .black && merged[.accent] == .black)108 }109110 @Test func defaultSpacingScaleIsTheMandatedOne() {111 #expect(GlassSpacing.standard.values == [4, 8, 12, 16, 24, 32, 48])112 }113114 @Test func spacingScalesWithDensityAndStaysWhole() {115 let compact = GlassSpacing.standard.scaled(by: GlassDensity.compact.spacingMultiplier)116 #expect(compact.values == [3, 6, 10, 13, 19, 26, 38])117 #expect(GlassSpacing.standard.scaled(by: 1) == .standard)118 }119120 @Test func partialJSONFallsBackToDefaults() throws {121 let spacing = try JSONDecoder().decode(GlassSpacing.self, from: Data(##"{"md":20}"##.utf8))122 #expect(spacing.md == 20 && spacing.xs == 8)123 let motion = try JSONDecoder().decode(GlassMotion.self, from: Data("{}".utf8))124 #expect(motion == .standardMotion)125 let typography = try JSONDecoder().decode(GlassTypography.self, from: Data(##"{"design":"serif"}"##.utf8))126 #expect(typography.design == .serif && typography.headingWeight == .semibold)127 }128129 @Test func materialTokensExposeOnlyVariantAndTint() throws {130 let data = try JSONEncoder().encode(GlassMaterialTokens(variant: .clear, tint: .accent))131 let object = try #require(JSONSerialization.jsonObject(with: data) as? [String: Any])132 #expect(Set(object.keys) == ["variant", "tint"])133 }134135 @Test("Hit targets and control heights per platform", arguments: GlassPlatform.allCases)136 func metrics(platform: GlassPlatform) {137 let metrics = GlassControlMetrics(platform: platform)138 #expect(metrics.minimumHitTarget == (platform == .macOS ? 24 : 44))139 #expect(metrics.height(.small) < metrics.height(.regular))140 #expect(metrics.height(.regular) < metrics.height(.large))141 if platform != .macOS { #expect(metrics.height(.regular) >= 44) }142 }143}144