79 lines
2.5 KiB
Swift
79 lines
2.5 KiB
Swift
import AppKit
|
|
import Foundation
|
|
import Testing
|
|
|
|
@testable import phbar
|
|
|
|
@MainActor
|
|
struct AppDelegateTests {
|
|
|
|
// MARK: - ScreenSync.diff
|
|
|
|
@Test func diffReportsAddedWhenNothingOccupied() throws {
|
|
let screen = try #require(NSScreen.main)
|
|
let plan = PHScreenSync.diff(current: [screen], occupied: [])
|
|
#expect(plan.added == [screen])
|
|
#expect(plan.removed.isEmpty)
|
|
#expect(plan.kept.isEmpty)
|
|
}
|
|
|
|
@Test func diffReportsRemovedWhenScreenGone() throws {
|
|
let screen = try #require(NSScreen.main)
|
|
let plan = PHScreenSync.diff(current: [], occupied: [screen])
|
|
#expect(plan.removed == [screen])
|
|
#expect(plan.added.isEmpty)
|
|
#expect(plan.kept.isEmpty)
|
|
}
|
|
|
|
@Test func diffKeepsScreenStillAttached() throws {
|
|
let screen = try #require(NSScreen.main)
|
|
let plan = PHScreenSync.diff(current: [screen], occupied: [screen])
|
|
#expect(plan.kept == [screen])
|
|
#expect(plan.added.isEmpty)
|
|
#expect(plan.removed.isEmpty)
|
|
}
|
|
|
|
/// A disconnected monitor reappears as a *new* `NSScreen` instance, so the
|
|
/// same physical display must be reported as remove + add, never a keep.
|
|
@Test func diffTreatsDistinctInstancesAsRemoveThenAdd() throws {
|
|
let main = try #require(NSScreen.main)
|
|
guard let other = NSScreen.screens.first(where: { $0 !== main }) else {
|
|
// Single-monitor environment: nothing to contrast against. Verify
|
|
// identity semantics hold for the same object re-presented instead.
|
|
let kept = PHScreenSync.diff(current: [main], occupied: [main])
|
|
#expect(kept.kept == [main])
|
|
return
|
|
}
|
|
|
|
let plan = PHScreenSync.diff(current: [other], occupied: [main])
|
|
#expect(plan.removed == [main])
|
|
#expect(plan.added == [other])
|
|
#expect(plan.kept.isEmpty)
|
|
}
|
|
|
|
@Test func diffIgnoresOrdering() throws {
|
|
let main = try #require(NSScreen.main)
|
|
guard let other = NSScreen.screens.first(where: { $0 !== main }) else {
|
|
// Single-monitor environment: ordering is trivially stable.
|
|
return
|
|
}
|
|
let plan = PHScreenSync.diff(current: [other, main], occupied: [main, other])
|
|
#expect(Set(plan.kept.map(ObjectIdentifier.init)) == Set([main, other].map(ObjectIdentifier.init)))
|
|
#expect(plan.added.isEmpty)
|
|
#expect(plan.removed.isEmpty)
|
|
}
|
|
|
|
// MARK: - BarControllerFactory
|
|
|
|
@Test func factoryBuildsControllerForScreen() throws {
|
|
let screen = try #require(NSScreen.main)
|
|
let config = try PHConfig.load()
|
|
let factory = PHBarFactory(config: config, environment: .process, debug: false)
|
|
|
|
let controller = try factory.make(for: screen)
|
|
|
|
#expect(controller.screen === screen)
|
|
#expect(controller.blocks.isEmpty == false)
|
|
}
|
|
}
|