Files
Kymo/kymoTests/BatteryPowerChecks.swift
T
wangairnan 4b8deb2e48 feat(power): 展示电池实际充放电功率并及时响应电源切换
保留电量百分比和连续趋势,按历史采样展示电池、适配器和系统功率。面板可见时读取电池实时功率,插拔电源后立即刷新并复读。
2026-11-15 23:54:19 +08:00

71 lines
5.0 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 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)
print("PASS: signed battery watts, adapter limits, joint supply, measured and derived values, stale telemetry")
}
}