Tests/OrgPresentationTests/PaletteTests.swift

147 lines · 8376 bytes

  1import Foundation
  2import OrgCore
  3import OrgPresentation
  4import Testing
  5
  6struct PaletteTests {
  7    @Test func defaultPalettesAreTheDefaultTheme() throws {
  8        let light = try #require(ThemeSpec.palette("default-light", .light))
  9        let dark = try #require(ThemeSpec.palette("default-dark", .dark))
 10        #expect(light.colors == ThemeSpec.default.light && dark.colors == ThemeSpec.default.dark)
 11        #expect(light.name == "Default" && ThemeSpec.palettes(.light).first == light && ThemeSpec.palettes(.dark).first == dark)
 12        #expect(ThemeSpec.defaultPaletteID(.light) == "default-light" && ThemeSpec.defaultPaletteID(.dark) == "default-dark")
 13    }
 14
 15    @Test func aPaletteIsFoundOnlyInItsAppearance() {
 16        #expect(ThemeSpec.palette("default-light", .dark) == nil)
 17        #expect(ThemeSpec.palette("nope", .light) == nil)
 18    }
 19
 20    @Test func accentIsASlotTheDefaultLeavesToTheSystem() {
 21        #expect(ThemeSpec.slots.contains { $0.key == "accent" })
 22        #expect(ThemeSpec.systemDefaults.contains("accent"))
 23        #expect(ThemeSpec.default.light["accent"] == nil && ThemeSpec.default.dark["accent"] == nil)
 24    }
 25
 26    @Test func everyPaletteHasExactlyTheSlotKeys() {
 27        let all = Set(ThemeSpec.slots.map(\.key))
 28        for palette in ThemeSpec.palettes(.light) + ThemeSpec.palettes(.dark) {
 29            let expected = palette.id.hasPrefix("default-") ? all.subtracting(ThemeSpec.systemDefaults) : all
 30            #expect(Set(palette.colors.keys) == expected, "\(palette.id): \(expected.symmetricDifference(palette.colors.keys))")
 31        }
 32    }
 33    @Test func theCatalogIsInOrder() {
 34        #expect(ThemeSpec.palettes(.light).map(\.id) == ["default-light", "solarized-light", "gruvbox-light", "modus-operandi", "catppuccin-latte", "tomorrow", "paper"])
 35        #expect(ThemeSpec.palettes(.dark).map(\.id) == ["default-dark", "solarized-dark", "gruvbox-dark", "modus-vivendi", "catppuccin-mocha", "nord", "dracula", "tokyo-night", "doom-one"])
 36    }
 37
 38    /// WCAG 2 contrast ratio.
 39    static func contrast(_ a: RGBA, _ b: RGBA) -> Double {
 40        func luminance(_ c: RGBA) -> Double {
 41            func channel(_ v: Double) -> Double { v <= 0.03928 ? v / 12.92 : pow((v + 0.055) / 1.055, 2.4) }
 42            return 0.2126 * channel(c.red) + 0.7152 * channel(c.green) + 0.0722 * channel(c.blue)
 43        }
 44        let (x, y) = (luminance(a), luminance(b))
 45        return (max(x, y) + 0.05) / (min(x, y) + 0.05)
 46    }
 47
 48    @Test func textReadsOnTheBackground() throws {
 49        let body = ["foreground", "table"]
 50        let marks = ["heading-1", "heading-2", "heading-3", "heading-4", "todo", "todo-next", "todo-waiting", "done", "link", "timestamp", "code",
 51                     "syntax-keyword", "syntax-string", "syntax-function", "syntax-type", "syntax-number", "syntax-property", "syntax-label"]
 52        for palette in ThemeSpec.palettes(.light) + ThemeSpec.palettes(.dark) {
 53            let background = try #require(palette.colors["background"])
 54            for key in body {
 55                let ratio = Self.contrast(try #require(palette.colors[key]), background)
 56                #expect(ratio >= 4.5, "\(palette.id) \(key): \(ratio)")
 57            }
 58            for key in marks {
 59                let ratio = Self.contrast(try #require(palette.colors[key]), background)
 60                #expect(ratio >= 3, "\(palette.id) \(key): \(ratio)")
 61            }
 62        }
 63    }
 64
 65    /// WCAG AA for text: 4.5:1 for every slot drawn as text, in every built-in theme; in
 66    /// Default, for keywords, priorities and the agenda's colours too.
 67    @Test func textSlotsReachAA() throws {
 68        let text = ["foreground", "table", "line-number-current", "tags", "comment", "keyword", "metadata", "syntax-comment",
 69                    "todo-cancelled", "priority-c", "agenda-date", "agenda-time", "agenda-category"]
 70        let defaults = ["todo", "todo-next", "todo-waiting", "done", "priority", "priority-a", "priority-b", "link", "timestamp",
 71                        "agenda-today", "agenda-overdue", "agenda-due-soon", "agenda-event"]
 72        for palette in ThemeSpec.palettes(.light) + ThemeSpec.palettes(.dark) {
 73            let background = try #require(palette.colors["background"])
 74            for key in text + (palette.id.hasPrefix("default-") ? defaults : []) {
 75                let ratio = Self.contrast(try #require(palette.colors[key], "\(palette.id) \(key)"), background)
 76                #expect(ratio >= 4.5, "\(palette.id) \(key): \(ratio)")
 77            }
 78        }
 79    }
 80
 81    @Test func onlySolarizedIsMarkedLowContrast() {
 82        let marked = (ThemeSpec.palettes(.light) + ThemeSpec.palettes(.dark)).filter(\.lowContrast).map(\.id)
 83        #expect(marked == ["solarized-light", "solarized-dark"])
 84    }
 85
 86    @Test func baseTakesEachAppearanceFromItsPalette() throws {
 87        let (spec, problems) = ThemeSpec.base(light: "paper", dark: "nord")
 88        #expect(problems.isEmpty)
 89        #expect(spec.light == ThemeSpec.palette("paper", .light)?.colors && spec.dark == ThemeSpec.palette("nord", .dark)?.colors)
 90        #expect(ThemeSpec.base(light: nil, dark: nil).0 == ThemeSpec.default)
 91        #expect(ThemeSpec.base(light: "", dark: "").0 == ThemeSpec.default)
 92    }
 93
 94    @Test func aPaletteOfTheOtherAppearanceIsAProblem() {
 95        let (spec, problems) = ThemeSpec.base(light: "nord", dark: "unknown")
 96        #expect(spec == ThemeSpec.default)
 97        #expect(problems == ["config.toml: unknown light-theme nord", "config.toml: unknown dark-theme unknown"])
 98    }
 99
100    @Test func aCopiedThemeReadsBackAsThePalettes() throws {
101        let light = try #require(ThemeSpec.palette("paper", .light))
102        let dark = try #require(ThemeSpec.palette("nord", .dark))
103        let text = ThemeSpec.themeFile(light: light, dark: dark)
104        #expect(text.hasPrefix("# Paper and Nord, copied from orgstar's built-in themes."))
105        let (spec, problems) = ThemeSpec.reading(try TOML.parse(text))
106        #expect(problems.isEmpty, "\(problems)")
107        #expect(spec.light.filter { light.colors[$0.key] != nil } == light.colors && spec.dark.filter { dark.colors[$0.key] != nil } == dark.colors)
108    }
109
110    @Test func iconIdsMapToAlternateNames() {
111        #expect(AppIcons.ids.first == "default" && AppIcons.ids.count == 13)
112        #expect(AppIcons.alternateName("default") == nil)
113        #expect(AppIcons.alternateName("unicorn-night") == "AppIcon-UnicornNight")
114        #expect(AppIcons.id(alternateName: "AppIcon-UnicornNight") == "unicorn-night")
115        #expect(AppIcons.id(alternateName: nil) == "default" && AppIcons.id(alternateName: "AppIcon-Gone") == "default")
116        #expect(AppIcons.name("unicorn") == "Org Star" && AppIcons.name("unicorn-night") == "Night Star" && AppIcons.name("unicorn-pastel") == "Pastel Star")
117    }
118
119    @Test func dockFilesFollowTheAppearance() {
120        #expect(AppIcons.dockFile("default", dark: true) == nil)
121        #expect(AppIcons.dockFile("gone", dark: false) == nil)
122        #expect(AppIcons.dockFile("unicorn", dark: false) == "unicorn-light")
123        #expect(AppIcons.dockFile("unicorn", dark: true) == "unicorn-dark")
124    }
125
126    @Test func aDarkOnlyChoiceLeavesTheLightSideToTheSystem() {
127        let spec = ThemeSpec.base(light: nil, dark: "nord").0
128        for key in ThemeSpec.systemDefaults {
129            #expect(spec.color(key, dark: false) == nil, "\(key)")
130            #expect(spec.color(key, dark: true) != nil, "\(key)")
131        }
132    }
133
134    @Test func editorColorsFallBackExceptForSystemKeys() {
135        let spec = ThemeSpec.base(light: nil, dark: "nord").0
136        // A system key the light side leaves unset stays unset there, not the dark side's.
137        #expect(spec.editorColor("sidebar-background", dark: false) == nil)
138        #expect(spec.editorColor("sidebar-background", dark: true) == spec.dark["sidebar-background"])
139        // Other keys keep the default theme's color for a side the spec lacks.
140        var partial = ThemeSpec.default
141        partial.light["link"] = nil
142        #expect(partial.editorColor("link", dark: false) == ThemeSpec.default.light["link"])
143        // A key neither the spec's side nor the default has takes the spec's other side.
144        let oneSided = ThemeSpec(light: [:], dark: ["custom": RGBA(red: 1, green: 0, blue: 0)])
145        #expect(oneSided.editorColor("custom", dark: false) == RGBA(red: 1, green: 0, blue: 0))
146    }
147}