97 lines
7.1 KiB
Swift
97 lines
7.1 KiB
Swift
import Foundation
|
||
|
||
@main struct BatteryPowerChecks {
|
||
static func main() {
|
||
func read(_ telemetry: [String: Any], extra: [String: Any] = [:], connected: Bool = true) -> BatteryPowerFlow {
|
||
var monitor = BatteryMonitor()
|
||
var raw = extra
|
||
raw["ExternalConnected"] = connected
|
||
raw["PowerTelemetryData"] = telemetry
|
||
return monitor.powerFlow(raw, externalSupply: connected)
|
||
}
|
||
let adapter = ["AdapterDetails": ["Watts": 65]]
|
||
let charging = read(["BatteryPower": 6_000], extra: adapter)
|
||
precondition(charging.battery == PowerReading(watts: 6) && charging.state == .charging)
|
||
precondition(charging.adapterCapacity == 65 && charging.input == nil)
|
||
precondition(read([:], extra: adapter).battery == nil)
|
||
let combined = read(["BatteryPower": NSNumber(value: UInt64(bitPattern: -8000)), "SystemPowerIn": 30_000])
|
||
precondition(combined.battery?.watts == -8 && combined.state == .combined)
|
||
precondition(combined.load == PowerReading(watts: 38, source: .balance))
|
||
let standby = read(["BatteryPower": -4700, "SystemPowerIn": 100, "SystemLoad": 6300], extra: adapter)
|
||
precondition(standby.state == .battery && standby.input?.watts == 0.1 && standby.jointSupply == nil)
|
||
let tinyDischarge = read(["BatteryPower": -100, "SystemPowerIn": 6300, "SystemLoad": 6400])
|
||
precondition(tinyDischarge.state == .external && tinyDischarge.jointSupply == nil)
|
||
let contradictory = read(["BatteryPower": -4700, "SystemPowerIn": 6000, "SystemLoad": 6300])
|
||
precondition(contradictory.state == .discharging && contradictory.jointSupply == nil)
|
||
var primaryBattery = BatteryMonitor()
|
||
let withAssist = primaryBattery.powerFlow(["ExternalConnected": true, "PowerTelemetryData":
|
||
["SystemPowerIn": 12_000, "BatteryPower": -8000, "SystemLoad": 20_000]], externalSupply: false)
|
||
precondition(withAssist.state == .combined && withAssist.jointSupply?.batteryFraction == 0.4)
|
||
let estimatedInput = read(["BatteryPower": -8000, "SystemLoad": 38_000])
|
||
precondition(estimatedInput.jointSupply?.adapter == PowerReading(watts: 30, source: .balance))
|
||
let estimatedAssist = read(["SystemPowerIn": 30_000, "SystemLoad": 38_000])
|
||
precondition(estimatedAssist.jointSupply?.discharge == PowerReading(watts: 8, source: .balance))
|
||
let staleCharge = read(["BatteryPower": 6_000], extra: ["Voltage": 12_000, "InstantAmperage": 0, "IsCharging": false])
|
||
precondition(staleCharge.battery?.watts == 0 && staleCharge.state == .external)
|
||
let unknownWatts = read([:], extra: ["IsCharging": true])
|
||
precondition(unknownWatts.battery == nil && unknownWatts.state == .charging)
|
||
let idle = read(["SystemPowerIn": 17_453, "BatteryPower": 0])
|
||
precondition(idle.battery?.watts == 0 && idle.state == .external)
|
||
let estimated = read(["SystemPowerIn": 65_000, "SystemLoad": 59_000])
|
||
precondition(estimated.battery == PowerReading(watts: 6, source: .balance))
|
||
precondition(read(["SystemPowerIn": 30_000, "SystemLoad": 38_000]).state == .combined)
|
||
precondition(read(["SystemPowerIn": 30_000, "SystemLoad": 30_200]).state == .external)
|
||
precondition(read(["BatteryPower": 6_000, "SystemLoad": 0]).input == PowerReading(watts: 6, source: .balance))
|
||
precondition(read(["SystemVoltageIn": 20_000, "SystemCurrentIn": 300]).input?.watts == 6)
|
||
let pack = ["Voltage": NSNumber(value: 12_000), "InstantAmperage": NSNumber(value: UInt32(bitPattern: -1500))]
|
||
let discharge = read(["SystemPowerIn": 0, "BatteryPower": 0], extra: pack, connected: false)
|
||
precondition(discharge.battery == PowerReading(watts: -18, source: .instantaneous) && discharge.state == .battery)
|
||
let unplugged = read(["SystemLoad": 18_000], connected: false)
|
||
precondition(unplugged.battery == PowerReading(watts: -18, source: .balance))
|
||
precondition(read(["SystemLoad": 18_000, "SystemPowerIn": 17_000], connected: false).battery == nil)
|
||
precondition(read(["BatteryPower": Double.nan]).battery == nil)
|
||
precondition(read(["BatteryPower": NSNumber(value: true)]).battery == nil)
|
||
precondition(read([:], extra: ["Voltage": 12_000, "Amperage": -1000]).battery == PowerReading(watts: -12, source: .average))
|
||
precondition(MetricFormat.signedWatts(-0.02) == "0 W" && MetricFormat.signedWatts(6) == "+6.0 W")
|
||
precondition(MetricFormat.signedWatts(-18) == "−18.0 W" && MetricFormat.signedWatts(nil) == "--")
|
||
|
||
var monitor = BatteryMonitor()
|
||
let now = Date()
|
||
var raw: [String: Any] = ["ExternalConnected": true, "PowerTelemetryData": ["BatteryPower": 6_000]]
|
||
precondition(monitor.powerFlow(raw, externalSupply: true, now: now).battery?.watts == 6)
|
||
monitor.invalidate()
|
||
precondition(monitor.powerFlow(raw, externalSupply: true, now: now).battery == nil)
|
||
raw["ExternalConnected"] = false
|
||
precondition(monitor.powerFlow(raw, externalSupply: false, now: now).battery == nil)
|
||
raw["PowerTelemetryData"] = ["BatteryPower": -18_000]
|
||
precondition(monitor.powerFlow(raw, externalSupply: false, now: now).battery?.watts == -18)
|
||
precondition(monitor.powerFlow(raw, externalSupply: false, now: now.addingTimeInterval(121)).battery == nil)
|
||
// Physical disconnection can precede an IOPS update, while both cached power registers still show AC.
|
||
let unplug = monitor.powerFlow(["ExternalConnected": false, "PowerTelemetryData": ["SystemPowerIn": 30_000, "BatteryPower": 6_000]], externalSupply: true)
|
||
precondition(unplug.state == .battery && unplug.battery == nil)
|
||
var live = unplug
|
||
live.applyBatterySensor(6)
|
||
precondition(live.battery == nil)
|
||
live.applyBatterySensor(-12.6)
|
||
precondition(live.battery == PowerReading(watts: -12.6, source: .smc) && live.state == .battery)
|
||
let delayedRegistry = BatteryPowerFlow(connected: true, externalSupply: false, charging: true, input: PowerReading(watts: 20))
|
||
precondition(delayedRegistry.state == .battery)
|
||
var joint = combined
|
||
joint.applyBatterySensor(-10)
|
||
precondition(joint.battery?.watts == -10 && joint.state == .combined && joint.load == nil)
|
||
precondition(joint.jointSupply?.batteryFraction == 8.0 / 38 && joint.jointSupply?.discharge.watts == 8)
|
||
joint.applyBatterySensor(2)
|
||
precondition(joint.state == .charging && joint.jointSupply == nil)
|
||
var missingPair = read(["SystemPowerIn": 6000])
|
||
missingPair.applyBatterySensor(-5)
|
||
precondition(missingPair.state == .discharging && missingPair.jointSupply == nil)
|
||
var clock = BatteryMetrics(charge: 0.8, flow: combined, minutesToFull: 20, minutesToEmpty: 345)
|
||
precondition(clock.minutesRemaining == 345)
|
||
clock.flow = charging
|
||
precondition(clock.minutesRemaining == 20)
|
||
clock.flow = idle
|
||
precondition(clock.minutesRemaining == nil && BatteryMonitor.positiveMinutes(65535) == nil)
|
||
print("PASS: signed battery watts, adapter limits, joint supply, measured and derived values, stale telemetry")
|
||
}
|
||
}
|