Repository Public
1// SP Notes Core2// File: Tests/SPNotesDesignTests/DesignTokenTests.swift3// Description: Tests of the Note Visual System 2.0 tokens: colour table, hex parsing, WCAG contrast of the text pairs,4// scales, motion durations and the Reduce Motion degradation.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Testing9import SwiftUI10@testable import SPNotesDesign1112typealias T = NVColor.Token1314/// A foreground / background pair that carries text.15struct Pair: Sendable, CustomTestStringConvertible {16 let fg: T, bg: T, dark: Bool17 var ratio: Double { NVRGB.contrast(fg.rgb(dark: dark), bg.rgb(dark: dark)) }18 var testDescription: String { "\(fg.name) on \(bg.name) (\(dark ? "dark" : "light"))" }19}2021func pairs(_ list: [(T, T)]) -> [Pair] { list.flatMap { [Pair(fg: $0.0, bg: $0.1, dark: false), Pair(fg: $0.0, bg: $0.1, dark: true)] } }2223let textPairs = pairs([24 (.fg, .bg), (.fg, .bgElevated), (.fg, .bgSunken), (.fg, .bgSidebar), (.fg, .bgHover),25 (.fgMuted, .bg), (.fgMuted, .bgElevated), (.fgMuted, .bgSidebar), (.fgMuted, .bgSunken),26 (.accent, .bg), (.accent, .bgElevated), (.accentFg, .accent), (.accent, .accentSoft),27 (.ai, .bg), (.ai, .bgElevated), (.ai, .aiSoft),28])29let faintPairs = pairs([(.fgFaint, .bg), (.fgFaint, .bgElevated)])30let statusPairs = pairs([(.success, .bg), (.warning, .bg), (.danger, .bg), (.success, .bgElevated), (.warning, .bgElevated), (.danger, .bgElevated)])3132@Suite struct ColorTokenTests {33 @Test func tableIsComplete() {34 #expect(T.all.count == 18)35 #expect(Set(T.all.map(\.name)).count == 18)36 #expect(T.all.map(\.name) == ["bg", "bgElevated", "bgSunken", "bgSidebar", "bgHover", "fg", "fgMuted", "fgFaint", "line", "lineStrong",37 "accent", "accentFg", "accentSoft", "ai", "aiSoft", "success", "warning", "danger"])38 for t in T.all { #expect(t.lightHex <= 0xFFFFFF && t.darkHex <= 0xFFFFFF) }39 }4041 @Test func smallTextVariantsOfTheSemanticHues() {42 // SPEC §7: darker variants for small text in light mode; dark mode keeps the base hue.43 #expect(T.successText.light.hexString == "#006B3C" && T.successText.dark == T.success.dark)44 #expect(T.warningText.light.hexString == "#8A5A00" && T.warningText.dark == T.warning.dark)45 #expect(T.dangerText.light.hexString == "#B0181F" && T.dangerText.dark == T.danger.dark)46 for t in [T.successText, T.warningText, T.dangerText] {47 #expect(NVRGB.contrast(t.light, T.bg.light) >= 4.5, "\(t.name) is readable as small text on the light background")48 #expect(NVRGB.contrast(t.light, T.bgElevated.light) >= 4.5)49 }50 }5152 @Test func valuesMatchTheSpecification() {53 // SPEC §7 (amendment): the measured sRGB values of the web oklch tokens supersede the §5 approximations.54 let spec: [String: (String, String)] = [55 "bg": ("#F9FBFD", "#0D1016"), "bgElevated": ("#FFFFFF", "#15181F"), "bgSunken": ("#F0F3F6", "#080A0F"),56 "bgSidebar": ("#F3F6F9", "#0A0C12"), "bgHover": ("#EAEDF2", "#1D2028"), "fg": ("#151922", "#E8EBEF"),57 "fgMuted": ("#535862", "#A0A5AE"), "fgFaint": ("#848992", "#6A6F77"), "line": ("#E1E4E8", "#22252D"),58 "lineStrong": ("#CCD0D5", "#333640"), "accent": ("#2F5DDB", "#7CA5FF"), "accentFg": ("#FBFCFE", "#090F1C"),59 "accentSoft": ("#E5F0FF", "#1B2A4C"), "ai": ("#7E41D2", "#B999FB"), "aiSoft": ("#F3ECFF", "#302249"),60 "success": ("#008B4E", "#5BC083"), "warning": ("#D98B09", "#F2AF48"), "danger": ("#CC272E", "#F26A64"),61 ]62 #expect(spec.count == 18)63 for t in T.all {64 #expect(t.light.hexString == spec[t.name]?.0, "\(t.name) light")65 #expect(t.dark.hexString == spec[t.name]?.1, "\(t.name) dark")66 #expect(NVRGB(hexString: spec[t.name]!.0) == t.light)67 #expect(NVRGB(hexString: spec[t.name]!.1) == t.dark)68 }69 }7071 @Test func hexParsing() {72 #expect(NVRGB(hexString: "#FFFFFF") == NVRGB(red: 1, green: 1, blue: 1))73 #expect(NVRGB(hexString: "000000") == NVRGB(red: 0, green: 0, blue: 0))74 #expect(NVRGB(hexString: "#fff") == NVRGB(red: 1, green: 1, blue: 1))75 #expect(NVRGB(hexString: " #3157d5 ")?.hexString == "#3157D5")76 let translucent = NVRGB(hexString: "#3157D580")77 #expect(translucent?.hexString == "#3157D5")78 #expect(abs((translucent?.alpha ?? 0) - 128.0 / 255) < 1e-9)79 for bad in ["", "#", "#12", "#12345", "#1234567", "#GGGGGG", "rouge", "#123456"] { #expect(NVRGB(hexString: bad) == nil, "\(bad)") }80 #expect(NVRGB(hex: 0x3157D5).hexString == "#3157D5")81 }8283 @Test func contrastFormula() {84 let white = NVRGB(hex: 0xFFFFFF), black = NVRGB(hex: 0x000000)85 #expect(abs(NVRGB.contrast(white, black) - 21) < 1e-9)86 #expect(abs(NVRGB.contrast(white, white) - 1) < 1e-9)87 #expect(NVRGB.contrast(white, black) == NVRGB.contrast(black, white))88 // Reference value: #767676 on white is the classic 4.54 : 1.89 #expect(abs(NVRGB.contrast(NVRGB(hex: 0x767676), white) - 4.54) < 0.01)90 }9192 @Test(arguments: textPairs) func textPairsReachAA(_ pair: Pair) {93 #expect(pair.ratio >= 4.5, "\(pair.testDescription): \(String(format: "%.2f", pair.ratio))")94 }9596 @Test(arguments: faintPairs) func faintTextReachesThree(_ pair: Pair) {97 #expect(pair.ratio >= 3.0, "\(pair.testDescription): \(String(format: "%.2f", pair.ratio))")98 }99100 /// Records every measured ratio (printed, and copied into docs/DESIGN.md). Status hues are not asserted as101 /// text colours: they tint icons, dots and soft fills.102 @Test func measuredRatiosAreRecorded() {103 var lines: [String] = []104 for p in textPairs + faintPairs + statusPairs { lines.append("NVCONTRAST \(p.testDescription): \(String(format: "%.2f", p.ratio))") }105 print(lines.joined(separator: "\n"))106 #expect(lines.count == (16 + 2 + 6) * 2)107 for p in statusPairs { #expect(p.ratio > 1) }108 }109110 @MainActor @Test func dynamicColoursAndGradientBuild() {111 for t in T.all {112 _ = t.color113 #expect(t.color(for: .light) == t.light.color)114 #expect(t.color(for: .dark) == t.dark.color)115 }116 _ = NVColor.lumen117 _ = NVColor.lumen(for: .dark)118 for tone in NVTone.allCases { _ = tone.foreground; _ = tone.background }119 }120}121122@Suite struct ScaleTests {123 @Test func typeScale() {124 typealias S = NVFont.Style125 #expect(S.micro.size() == 11)126 #expect(S.ui.size() == 13)127 #expect(S.uiStrong.size() == 13)128 #expect(S.code.size() == 13)129 #expect(S.body.size() == 16)130 #expect(S.reading.size() == 16)131 #expect(S.reading.size(compact: true) == 17)132 #expect(S.h3.size() == 20)133 #expect(S.h2.size() == 24)134 #expect(S.h1.size() == 30)135 #expect(S.display.size() == 40)136 for s in S.allCases where s != .reading { #expect(s.size(compact: true) == s.size()) }137 for s in [S.h3, .h2, .h1, .display, .reading] { #expect(s.design == .serif) }138 #expect(S.code.design == .monospaced)139 for s in [S.micro, .ui, .uiStrong, .body] { #expect(s.design == .default) }140 #expect(S.display.textStyle == .largeTitle)141 #expect(S.body.textStyle == .body)142 }143144 @Test func spacingRadiusAndMeasure() {145 #expect(NVSpace.scale == [2, 4, 8, 12, 16, 24, 32, 48, 64])146 #expect(NVRadius.scale == [4, 6, 10, 14, 20, 999])147 #expect(NVSpace.readingMeasure == 680)148 #expect(NVSpace.scale == NVSpace.scale.sorted())149 }150151 @Test func glassSurfaceOpacityIsInRange() {152 #expect((0.60...0.72).contains(NVElevation.glassSurfaceOpacity))153 #expect(NVElevation.allCases.count == 3)154 }155}156157@Suite struct MotionTests {158 typealias M = NVMotion159160 @Test func durations() {161 #expect(M.Token.instant.duration == 0.090)162 #expect(M.Token.fast.duration == 0.160)163 #expect(M.Token.base.duration == 0.240)164 #expect(M.Token.slow.duration == 0.420)165 #expect(M.Token.ambient.duration == 0.900)166 #expect(M.Token.exit.duration == 0.160)167 #expect(M.springResponse == 0.38)168 #expect(M.springDamping == 0.82)169 #expect(M.Token.fast.duration < 0.2) // quick capture budget170 }171172 @Test func easings() {173 #expect(M.Curve.standard == M.Curve(x1: 0.2, y1: 0.8, x2: 0.2, y2: 1))174 #expect(M.Curve.exit == M.Curve(x1: 0.4, y1: 0, x2: 1, y2: 1))175 }176177 @Test(arguments: NVMotion.Token.allCases) func reduceMotionCapsEveryToken(_ token: NVMotion.Token) {178 #expect(M.duration(token, reduceMotion: true) <= 0.120)179 #expect(M.duration(token, reduceMotion: true) == min(token.duration, 0.120))180 #expect(M.duration(token, reduceMotion: false) == token.duration)181 #expect(M.animation(token, reduceMotion: true) == Animation.linear(duration: min(token.duration, 0.120)))182 }183184 @Test func fullMotionAnimations() {185 #expect(M.animation(.base, reduceMotion: false) == Animation.timingCurve(0.2, 0.8, 0.2, 1, duration: 0.240))186 #expect(M.animation(.exit, reduceMotion: false) == Animation.timingCurve(0.4, 0, 1, 1, duration: 0.160))187 #expect(M.animation(.spring, reduceMotion: false) == Animation.spring(response: 0.38, dampingFraction: 0.82))188 #expect(M.animation(.spring, reduceMotion: true) == Animation.linear(duration: 0.120))189 }190191 @Test func staggerDelayAndCap() {192 #expect(M.staggerStep == 0.018)193 #expect(M.staggerCap == 24)194 #expect(M.revealRise == 6)195 #expect(M.staggerDelay(index: 0, reduceMotion: false) == 0)196 #expect(abs(M.staggerDelay(index: 1, reduceMotion: false) - 0.018) < 1e-12)197 #expect(abs(M.staggerDelay(index: 23, reduceMotion: false) - 0.414) < 1e-12)198 #expect(M.staggerDelay(index: 24, reduceMotion: false) == 0)199 #expect(M.staggerDelay(index: 500, reduceMotion: false) == 0)200 #expect(M.staggerDelay(index: -1, reduceMotion: false) == 0)201 for i in 0..<40 { #expect(M.staggerDelay(index: i, reduceMotion: true) == 0) }202 #expect(M.isStaggered(index: 23))203 #expect(!M.isStaggered(index: 24))204 #expect((0..<100).filter(M.isStaggered).count == 24)205 }206207 @Test func withNVAnimationReturnsTheBodyValue() {208 #expect(withNVAnimation(.base, reduceMotion: true) { 41 + 1 } == 42)209 #expect(withNVAnimation(reduceMotion: false) { "ok" } == "ok")210 }211212 @Test func edgeShapeTrim() {213 let full = NVEdgeShape(from: .zero, to: CGPoint(x: 100, y: 0), progress: 1).path(in: .zero)214 let half = NVEdgeShape(from: .zero, to: CGPoint(x: 100, y: 0), progress: 0.5).path(in: .zero)215 let none = NVEdgeShape(from: .zero, to: CGPoint(x: 100, y: 0), progress: 0).path(in: .zero)216 #expect(abs(full.boundingRect.width - 100) < 0.01)217 #expect(abs(half.boundingRect.width - 50) < 0.5)218 #expect(none.isEmpty || none.boundingRect.width < 0.01)219 var shape = NVEdgeShape(from: .zero, to: CGPoint(x: 10, y: 10), curvature: 0.3, progress: 0.25)220 #expect(shape.animatableData == 0.25)221 shape.animatableData = 2222 #expect(abs(shape.path(in: .zero).boundingRect.maxX - 10) < 0.01) // clamped to 1223 }224}225226@Suite struct ComponentTests {227 @Test func aiBadgeLabels() {228 #expect(NVAIBadge.label(model: nil, style: .full) == "Généré par IA")229 #expect(NVAIBadge.label(model: "", style: .full) == "Généré par IA")230 #expect(NVAIBadge.label(model: "claude-haiku", style: .full) == "Généré par IA · claude-haiku")231 #expect(NVAIBadge.label(model: "x", style: .compact) == "IA")232 }233234 @Test func citationAccessibilityText() {235 #expect(NVCitationChip.accessibilityText(index: 2, label: "Boucher 2026") == "Source 2 : Boucher 2026")236 #expect(NVCitationChip.accessibilityText(index: nil, label: "Note") == "Source : Note")237 }238239 @MainActor @Test func componentsBuild() {240 _ = NVCard { Text(verbatim: "a") }.body241 _ = NVChip("tag", systemImage: "number", tone: .ai, isSelected: true) {}.body242 _ = NVSectionHeader("Titre", subtitle: "s").body243 _ = NVEmptyState(systemImage: "tray", title: "Vide", message: "m", actionTitle: "a") {}.body244 _ = NVAIBadge(model: "m").body245 _ = NVCitationChip(index: 1, label: "l") {}.body246 _ = NVToolbarGlass { Text(verbatim: "x") }.body247 _ = NVTransition.blockReveal(reduceMotion: true)248 _ = NVTransition.morph(reduceMotion: false)249 _ = NVTransition.panel(edge: .trailing, reduceMotion: false)250 _ = NVTransition.captureScaleIn(reduceMotion: false)251 _ = NVTransition.semanticZoom(reduceMotion: true)252 #expect(NVTone.allCases.count == 6)253 }254}255