cleanup tests

This commit is contained in:
2026-07-12 16:16:46 +02:00
parent b872659796
commit d4d1b7bec4
8 changed files with 844 additions and 746 deletions
+112
View File
@@ -0,0 +1,112 @@
import Foundation
import Testing
@testable import phbar
// MARK: - Per-monitor resolution
//
// `PHConfig` exposes one resolution API: `theme`/`window`/`blocks`/
// `monitorOverride`, each taking a pure `(screenName, screenIndex)` pair.
// Precedence for every field: override-by-name override-by-index global.
/// Build a `PHConfig` with sensible defaults so each test only spells out the
/// fields it cares about.
private func makeConfig(
theme: String = "default",
window: String = "default",
blocks: String? = nil,
monitors: [String: PHConfigMonitorOverride]? = nil
) -> PHConfig {
PHConfig(theme: theme, window: window, blocks: blocks, env: nil, monitors: monitors)
}
// MARK: window
@Test func windowFallsBackToGlobal() {
let config = makeConfig(window: "top")
#expect(config.window(screenName: "Built-in", screenIndex: 0) == "top")
}
@Test func windowMatchesByIndex() {
let config = makeConfig(monitors: ["1": .init(window: "bottom", blocks: nil)])
#expect(config.window(screenName: "Whatever", screenIndex: 1) == "bottom")
}
@Test func windowMatchesByName() {
let config = makeConfig(monitors: ["DELL U2723QE": .init(window: "clock", blocks: nil)])
#expect(config.window(screenName: "DELL U2723QE", screenIndex: 0) == "clock")
}
@Test func windowPrefersNameOverIndex() {
let config = makeConfig(monitors: [
"0": .init(window: "byIndex", blocks: nil),
"DELL": .init(window: "byName", blocks: nil),
])
#expect(config.window(screenName: "DELL", screenIndex: 0) == "byName")
}
// MARK: blocks
@Test func blocksDefaultsToDefault() {
#expect(makeConfig().blocks(screenName: "S", screenIndex: 0) == "default")
}
@Test func blocksUsesGlobalWhenNoOverride() {
let config = makeConfig(blocks: "main")
#expect(config.blocks(screenName: "S", screenIndex: 0) == "main")
}
@Test func blocksOverrideBeatsGlobal() {
let config = makeConfig(blocks: "main", monitors: ["1": .init(window: nil, blocks: "alt")])
#expect(config.blocks(screenName: "S", screenIndex: 1) == "alt")
}
// MARK: theme
@Test func themeFallsBackToGlobal() {
let config = makeConfig(theme: "voltage")
#expect(config.theme(screenName: "S", screenIndex: 0) == "voltage")
}
@Test func themeOverrideBeatsGlobal() {
let config = makeConfig(theme: "voltage", monitors: ["1": .init(theme: "mono", window: nil, blocks: nil)])
#expect(config.theme(screenName: "S", screenIndex: 1) == "mono")
}
// MARK: independence
@Test func overrideInheritsUnsetFieldsFromGlobal() {
// An override that sets only `blocks` still inherits the global window/theme.
let config = makeConfig(theme: "voltage", window: "top", monitors: ["1": .init(window: nil, blocks: "laptop")])
#expect(config.theme(screenName: "S", screenIndex: 1) == "voltage")
#expect(config.window(screenName: "S", screenIndex: 1) == "top")
#expect(config.blocks(screenName: "S", screenIndex: 1) == "laptop")
}
@Test func themeWindowAndBlocksResolveIndependently() {
let config = makeConfig(
theme: "voltage", window: "top", blocks: "main",
monitors: [
"DELL": .init(theme: "mono", window: "clock", blocks: nil), // inherits blocks "main"
"1": .init(theme: nil, window: nil, blocks: "laptop"), // inherits theme/window
]
)
#expect(config.theme(screenName: "DELL", screenIndex: 0) == "mono")
#expect(config.window(screenName: "DELL", screenIndex: 0) == "clock")
#expect(config.blocks(screenName: "DELL", screenIndex: 0) == "main")
#expect(config.theme(screenName: "S", screenIndex: 1) == "voltage")
#expect(config.window(screenName: "S", screenIndex: 1) == "top")
#expect(config.blocks(screenName: "S", screenIndex: 1) == "laptop")
}
// MARK: monitorOverride
@Test func monitorOverrideReturnsNilWhenTableAbsent() {
#expect(makeConfig().monitorOverride(screenName: "S", screenIndex: 0) == nil)
}
@Test func monitorOverrideReturnsNilForUnmatchedScreen() {
let config = makeConfig(monitors: ["DELL": .init(window: "clock", blocks: nil)])
#expect(config.monitorOverride(screenName: "Unknown", screenIndex: 99) == nil)
}