34 lines
1.4 KiB
Swift
34 lines
1.4 KiB
Swift
import Foundation
|
|
import IOKit.pwr_mgt
|
|
|
|
@main struct KeepAwakeChecks {
|
|
@MainActor static func main() {
|
|
let service = KeepAwakeService()
|
|
precondition(!service.isEnabled)
|
|
precondition(ownAssertions().isEmpty)
|
|
for _ in 0..<3 {
|
|
service.toggle()
|
|
precondition(service.isEnabled && !service.didFail)
|
|
let types = Set(ownAssertions().compactMap { $0[kIOPMAssertionTypeKey] as? String })
|
|
precondition(types == Set([kIOPMAssertionTypePreventUserIdleDisplaySleep, kIOPMAssertionTypePreventUserIdleSystemSleep]))
|
|
service.toggle()
|
|
precondition(!service.isEnabled && !service.didFail)
|
|
precondition(ownAssertions().isEmpty)
|
|
}
|
|
var temporary: KeepAwakeService? = KeepAwakeService()
|
|
temporary?.toggle()
|
|
precondition(ownAssertions().count == 2)
|
|
temporary = nil
|
|
precondition(ownAssertions().isEmpty)
|
|
print("KeepAwakeChecks passed: real assertions, repeated toggle and lifetime release")
|
|
}
|
|
|
|
static func ownAssertions() -> [[String: Any]] {
|
|
var value: Unmanaged<CFDictionary>?
|
|
precondition(IOPMCopyAssertionsByProcess(&value) == kIOReturnSuccess)
|
|
let processes = value!.takeRetainedValue() as NSDictionary
|
|
let assertions = processes[NSNumber(value: getpid())] as? [[String: Any]] ?? []
|
|
return assertions.filter { $0[kIOPMAssertionNameKey] as? String == "Kymo Keep Awake" }
|
|
}
|
|
}
|