Repository Public
1import Foundation2import Testing3import SettingsModel4@testable import DesignSystem56@Suite("Themes")7struct ThemeTests {8 @Test func hexParsing() {9 #expect(RGBColor(hex: "#1F6FEB") == RGBColor(0x1f, 0x6f, 0xeb))10 #expect(RGBColor(hex: "abc") == RGBColor(0xaa, 0xbb, 0xcc))11 #expect(RGBColor(hex: "#12345") == nil)12 #expect(RGBColor(hex: "zzzzzz") == nil)13 #expect(RGBColor(1, 2, 255).hex == "#0102FF")14 }1516 @Test func contrastFormulaMatchesWCAGReferenceValues() {17 let black = RGBColor(0, 0, 0), white = RGBColor(255, 255, 255)18 #expect(abs(black.contrastRatio(with: white) - 21) < 0.01)19 #expect(abs(white.contrastRatio(with: white) - 1) < 0.001)20 #expect(abs(RGBColor(hex: "#777777")!.contrastRatio(with: white) - 4.48) < 0.05)21 }2223 @Test func everyBuiltInThemeIsReadable() {24 #expect(Set(ThemeCatalog.all.map(\.id)).count == ThemeCatalog.all.count)25 for theme in ThemeCatalog.all {26 #expect(theme.ansi.count == 16)27 let body = theme.foreground.contrastRatio(with: theme.background)28 #expect(body >= (theme.isHighContrast ? 15 : 9), "\(theme.name) text contrast \(body)")29 #expect(theme.cursor.contrastRatio(with: theme.background) >= 3, "\(theme.name) cursor")30 // Coloured text (all ANSI colours except the one meant to match the background) stays legible.31 for (index, color) in theme.ansi.enumerated() where index != (theme.isDark ? 0 : 15) && index != (theme.isDark ? 8 : 7) {32 let ratio = color.contrastRatio(with: theme.background)33 #expect(ratio >= (theme.isHighContrast ? 7 : 4.5), "\(theme.name) ansi \(index) contrast \(ratio)")34 }35 #expect(theme.secondaryText.contrastRatio(with: theme.chromeBackground) >= 4.5, "\(theme.name) secondary text")36 // Selected text remains readable.37 #expect(theme.foreground.contrastRatio(with: theme.selection) >= 4.5, "\(theme.name) selection")38 }39 }4041 @Test func themeResolutionFollowsAppearanceAndAccessibility() {42 var settings = AppSettings()43 #expect(settings.resolvedTheme(systemIsDark: true).id == "sp-graphite")44 #expect(settings.resolvedTheme(systemIsDark: false).id == "sp-daylight")45 settings.appearance = .dark46 settings.darkThemeID = "sp-midnight"47 #expect(settings.resolvedTheme(systemIsDark: false).id == "sp-midnight")48 #expect(settings.resolvedTheme(systemIsDark: false, increaseContrast: true).id == "sp-contrast-dark")49 settings.appearance = .light50 #expect(settings.resolvedTheme(systemIsDark: true, increaseContrast: true).id == "sp-contrast-light")51 }5253 @Test func themeCodableRoundTrip() throws {54 let data = try JSONEncoder().encode(ThemeCatalog.graphite)55 #expect(try JSONDecoder().decode(TerminalTheme.self, from: data) == ThemeCatalog.graphite)56 }5758 @Test func accentEngineIsStableDistinctAndReadable() {59 let hosts = ["M3U96a", "M4M64c", "bhs64", "prod-db-1", "staging", "a", "root@10.0.0.1", ""]60 for theme in ThemeCatalog.all {61 for host in hosts {62 let accent = AccentEngine.accent(forHost: host, theme: theme)63 #expect(accent == AccentEngine.accent(forHost: host, theme: theme))64 let floor = theme.isHighContrast ? 4.5 : AccentEngine.minimumContrast65 #expect(accent.contrastRatio(with: theme.chromeBackground) >= floor, "\(theme.name) \(host)")66 }67 #expect(AccentEngine.readable(theme.accent, on: theme).contrastRatio(with: theme.chromeBackground) >= AccentEngine.minimumContrast)68 }69 // Case does not change the colour of a host; reds and yellows are never produced.70 #expect(AccentEngine.hue(forHost: "M3U96a") == AccentEngine.hue(forHost: "m3u96a"))71 for host in hosts { #expect((80.0...340.0).contains(AccentEngine.hue(forHost: host))) }72 #expect(Set(hosts.map { AccentEngine.hue(forHost: $0) }).count >= hosts.count - 1)73 // Known FNV-1a value, so the mapping can never drift silently between releases.74 #expect(AccentEngine.stableHash("a") == 0xaf63dc4c8601ec8c)75 }7677 @Test func hslConversionMatchesReferenceColours() {78 #expect(RGBColor(hue: 0, saturation: 1, lightness: 0.5) == RGBColor(255, 0, 0))79 #expect(RGBColor(hue: 120, saturation: 1, lightness: 0.5) == RGBColor(0, 255, 0))80 #expect(RGBColor(hue: 240, saturation: 1, lightness: 0.5) == RGBColor(0, 0, 255))81 #expect(RGBColor(hue: 200, saturation: 0, lightness: 1) == RGBColor(255, 255, 255))82 #expect(RGBColor(hue: -120, saturation: 1, lightness: 0.5) == RGBColor(0, 0, 255))83 }8485 @Test func chromeTokensAreCoherent() {86 let compact = ChromeMetrics.metrics(for: .compact), expanded = ChromeMetrics.metrics(for: .expanded)87 #expect(compact.tabHeight < compact.titleBarHeight && expanded.tabHeight < expanded.titleBarHeight)88 #expect(expanded.titleBarHeight > compact.titleBarHeight)89 #expect(Radius.inner(of: Radius.l, inset: 3) == 7)90 #expect(Radius.inner(of: 2, inset: 6) == 2)91 for theme in ThemeCatalog.all {92 // Text of a pane header stays readable on the elevated surface.93 #expect(theme.foreground.contrastRatio(with: theme.elevatedSurface) >= 7, "\(theme.name)")94 #expect(theme.secondaryText.contrastRatio(with: theme.tintedChrome(accent: theme.accent)) >= 4.5, "\(theme.name) tinted chrome")95 }96 }9798 @Test func settingsFromVersionOneLoadWithNextDefaults() throws {99 // A 1.0.0 file knows none of the new keys.100 let old = #"{"appearance":"dark","fontSize":15,"backgroundOpacity":0.8,"backgroundBlur":true,"showSidebar":true}"#101 let settings = try JSONDecoder().decode(AppSettings.self, from: Data(old.utf8))102 #expect(settings.fontSize == 15 && settings.backgroundOpacity == 0.8 && settings.backgroundBlur && settings.showSidebar)103 #expect(settings.chromeMaterial == .translucent && settings.density == .compact && settings.motion == .system)104 #expect(settings.dynamicAccent && settings.ambientGradient && settings.completionFeedback && settings.useLiquidGlass)105 #expect(settings.toolbarItems == AppSettings.defaultToolbarItems)106 // Invalid values fall back; duplicates in the toolbar are dropped.107 let odd = #"{"density":"huge","motion":7,"toolbarItems":["find","find","zoomPane"]}"#108 let recovered = try JSONDecoder().decode(AppSettings.self, from: Data(odd.utf8))109 #expect(recovered.density == .compact && recovered.motion == .system && recovered.toolbarItems == ["find", "zoomPane"])110 let round = try JSONDecoder().decode(AppSettings.self, from: JSONEncoder().encode(recovered))111 #expect(round == recovered)112 }113}114