110 lines
3.8 KiB
Swift
110 lines
3.8 KiB
Swift
//
|
|
// MMBatteryTests.swift
|
|
// MacMonitorTests
|
|
//
|
|
// Unit and integration tests for MMBatteryTelemetryProvider.
|
|
//
|
|
|
|
import XCTest
|
|
@testable import MacMonitor
|
|
|
|
final class MMBatteryTests: XCTestCase {
|
|
var provider: MMBatteryTelemetryProvider!
|
|
|
|
override func setUp() {
|
|
super.setUp()
|
|
provider = MMBatteryTelemetryProvider()
|
|
}
|
|
|
|
override func tearDown() {
|
|
provider = nil
|
|
super.tearDown()
|
|
}
|
|
|
|
func testDomainAndIdentifier() {
|
|
XCTAssertEqual(provider.domain, .power)
|
|
XCTAssertEqual(provider.providerIdentifier, "com.i3omb.macmonitor.telemetry.battery")
|
|
}
|
|
|
|
func testCalculateBatteryHealthWithSimulatedNormalBattery() {
|
|
let raw: [String: Any] = [
|
|
"BatteryInstalled": true,
|
|
"CycleCount": 250,
|
|
"DesignCycleCount9C": 1000,
|
|
"CurrentCapacity": 3000,
|
|
"MaxCapacity": 4000,
|
|
"DesignCapacity": 4400,
|
|
"Temperature": 3150, // 31.5 C
|
|
"Voltage": 11100, // 11.1 V
|
|
"Amperage": -1500, // -1.5 A
|
|
"IsCharging": false,
|
|
"FullyCharged": false,
|
|
"ExternalConnected": false,
|
|
"TimeRemaining": 120,
|
|
"Manufacturer": "SMP",
|
|
"Serial": "TEST123456",
|
|
"DeviceName": "bq20z451"
|
|
]
|
|
|
|
let health = MMBatteryTelemetryProvider.calculateBatteryHealth(withProperties: raw)
|
|
XCTAssertEqual(health["hasBattery"] as? Bool, true)
|
|
XCTAssertEqual(health["installed"] as? Bool, true)
|
|
XCTAssertEqual(health["healthCondition"] as? String, "Normal")
|
|
|
|
let healthPercent = (health["healthPercent"] as? Double) ?? 0.0
|
|
XCTAssertEqual(healthPercent, 90.9, accuracy: 0.1)
|
|
XCTAssertEqual(health["cycleCount"] as? Int, 250)
|
|
XCTAssertEqual(health["designCycleCount"] as? Int, 1000)
|
|
XCTAssertEqual(health["temperature"] as? Double, 31.5)
|
|
let watts = (health["watts"] as? Double) ?? 0.0
|
|
XCTAssertEqual(watts, 16.7, accuracy: 0.2)
|
|
XCTAssertEqual(health["manufacturer"] as? String, "SMP")
|
|
}
|
|
|
|
func testCalculateBatteryHealthWithServiceRecommended() {
|
|
let raw: [String: Any] = [
|
|
"BatteryInstalled": true,
|
|
"CycleCount": 1100,
|
|
"DesignCycleCount9C": 1000,
|
|
"CurrentCapacity": 1500,
|
|
"MaxCapacity": 2500,
|
|
"DesignCapacity": 4400, // ~56.8% health
|
|
"Temperature": 3200,
|
|
"Voltage": 10800,
|
|
"Amperage": -1000,
|
|
"IsCharging": false,
|
|
"FullyCharged": false,
|
|
"ExternalConnected": false,
|
|
"TimeRemaining": 90,
|
|
"Manufacturer": "Apple"
|
|
]
|
|
|
|
let health = MMBatteryTelemetryProvider.calculateBatteryHealth(withProperties: raw)
|
|
XCTAssertEqual(health["healthCondition"] as? String, "Service Recommended")
|
|
let healthPercent = (health["healthPercent"] as? Double) ?? 0.0
|
|
XCTAssertLessThan(healthPercent, 70.0)
|
|
}
|
|
|
|
func testCalculateBatteryHealthWithNil() {
|
|
let health = MMBatteryTelemetryProvider.calculateBatteryHealth(withProperties: nil)
|
|
XCTAssertEqual(health["hasBattery"] as? Bool, false)
|
|
XCTAssertEqual(health["installed"] as? Bool, false)
|
|
XCTAssertEqual(health["healthCondition"] as? String, "No Battery")
|
|
}
|
|
|
|
func testLiveSampleBattery() {
|
|
do {
|
|
let sample = try provider.sampleTelemetry()
|
|
XCTAssertNotNil(sample)
|
|
if provider.hasBattery {
|
|
XCTAssertEqual(sample["hasBattery"] as? Bool, true)
|
|
XCTAssertNotNil(sample["cycleCount"])
|
|
XCTAssertNotNil(sample["healthPercent"])
|
|
XCTAssertNotNil(sample["healthCondition"])
|
|
}
|
|
} catch {
|
|
XCTFail("sampleTelemetry threw error: \(error)")
|
|
}
|
|
}
|
|
}
|