DomainDigTests/ThemeTests.swift

130 lines · 5304 bytes

  1import SwiftUI
  2import XCTest
  3@testable import DomainDig
  4
  5/// The palette moved from 483 asset-catalog call sites into a `Theme` carried
  6/// in the environment (I1's sibling, W13).
  7///
  8/// The one safety the move gave up is compile-time checking of the colour
  9/// names: `ThemeToken.assetColor` looks them up by string, because the
 10/// generated `ColorResource` symbols are MainActor-isolated and
 11/// `ShapeStyle.resolve(in:)` is not. `testEveryTokenHasAColorset` and
 12/// `testNoColorsetIsMissingAToken` are what replace it.
 13final class ThemeTests: XCTestCase {
 14
 15    /// The colorsets on disk, which are the palette's definition.
 16    private func colorsetNames() throws -> Set<String> {
 17        // The bundle under test is the app's, and the catalog is compiled into
 18        // it, so the names are read from the source tree instead.
 19        let root = URL(fileURLWithPath: #filePath)
 20            .deletingLastPathComponent()
 21            .deletingLastPathComponent()
 22            .appendingPathComponent("Shared/Colors.xcassets")
 23        let entries = try FileManager.default.contentsOfDirectory(atPath: root.path)
 24        return Set(
 25            entries
 26                .filter { $0.hasSuffix(".colorset") }
 27                .map { String($0.dropLast(".colorset".count)) }
 28        )
 29    }
 30
 31    func testEveryTokenHasAColorset() throws {
 32        let names = try colorsetNames()
 33
 34        for token in ThemeToken.allCases {
 35            XCTAssertTrue(
 36                names.contains(token.rawValue),
 37                "\(token) names '\(token.rawValue)', which is not a colorset — it would render as a placeholder"
 38            )
 39        }
 40    }
 41
 42    /// The other direction: a colour added to the catalog without a token is
 43    /// unreachable through the theme, so it cannot be themed and nothing says
 44    /// so.
 45    func testNoColorsetIsMissingAToken() throws {
 46        let names = try colorsetNames()
 47        let tokens = Set(ThemeToken.allCases.map(\.rawValue))
 48
 49        XCTAssertEqual(
 50            names.subtracting(tokens),
 51            [],
 52            "these colorsets have no ThemeToken, so the theme cannot reach them"
 53        )
 54    }
 55
 56    // MARK: Defaults
 57
 58    /// The default theme has to render exactly as the asset catalog did, or the
 59    /// refactor changed the app's appearance rather than only its plumbing.
 60    func testTheDefaultThemeResolvesEveryTokenFromTheCatalog() {
 61        let theme = Theme.default
 62
 63        for token in ThemeToken.allCases {
 64            XCTAssertEqual(theme[token], token.assetColor, "\(token) should come straight from the catalog")
 65        }
 66    }
 67
 68    func testAnEmptyOverrideSetIsTheDefault() {
 69        XCTAssertEqual(Theme(overrides: [:]), Theme.default)
 70    }
 71
 72    // MARK: Accents
 73
 74    func testTheDefaultAccentOverridesNothing() {
 75        XCTAssertEqual(AccentTheme.default.theme, Theme.default)
 76    }
 77
 78    /// An accent changes the accent and nothing else. Letting it reach the
 79    /// status colours would undo the contrast work phases 1–5 of #21 did.
 80    func testAnAccentOverridesOnlyTheAccentTokens() {
 81        for accent in AccentTheme.allCases where accent != .default {
 82            let theme = accent.theme
 83
 84            XCTAssertNotEqual(theme[.accent], ThemeToken.accent.assetColor, "\(accent) should change the accent")
 85
 86            for token in ThemeToken.allCases where token != .accent && token != .onAccent {
 87                XCTAssertEqual(
 88                    theme[token],
 89                    token.assetColor,
 90                    "\(accent) must leave \(token) alone"
 91                )
 92            }
 93        }
 94    }
 95
 96    func testEveryAccentHasATitle() {
 97        for accent in AccentTheme.allCases {
 98            XCTAssertFalse(accent.title.isEmpty)
 99            XCTAssertEqual(AccentTheme(rawValue: accent.rawValue), accent, "the stored value round-trips")
100        }
101    }
102
103    /// Accent themes are Pro+ per D1. Pro+ takes `allCases`, so the check that
104    /// matters is that the capability did not land in free or pro.
105    func testAccentThemesArePrOPlusOnly() {
106        XCTAssertFalse(FeatureAccessService.entitlements(for: .free).capabilities.contains(.accentTheme))
107        XCTAssertFalse(FeatureAccessService.entitlements(for: .pro).capabilities.contains(.accentTheme))
108        XCTAssertTrue(FeatureAccessService.entitlements(for: .proPlus).capabilities.contains(.accentTheme))
109        XCTAssertEqual(FeatureAccessService.upgradeMessage(for: .accentTheme), "Available in Pro+")
110    }
111
112    // MARK: Badge models carry tokens
113
114    /// The badges are built outside a view body, which is why their colours are
115    /// tokens rather than resolved values.
116    func testStatusBadgesNameTokensRatherThanColors() {
117        let registered = AppStatusFactory.availability(.registered)
118        XCTAssertFalse(registered.title.isEmpty)
119
120        // A token, so the theme resolves it at draw time.
121        XCTAssertEqual(Theme.default[registered.foreground], registered.foreground.assetColor)
122        XCTAssertEqual(Theme.default[registered.background], registered.background.assetColor)
123    }
124
125    func testEveryToneMapsToATokenPair() {
126        for tone in [AppStatusTone.positive, .warning, .critical, .info, .neutral] {
127            XCTAssertNotEqual(tone.foreground, tone.surface, "\(tone) should not use one colour for both")
128        }
129    }
130}