Offline project checks / check (push) Canceled after 0s
- Add should_shutdown() check in BatteryPolicy and trigger_shutdown() in phone-battery-guard.py. - Auto sync filesystems and invoke safe poweroff if SoC drops to 5% while on battery/discharging. - Do not power off if external charger is actively connected and charging. - Update openrc service command args with --shutdown-at 5. - Add unit tests for emergency shutdown, discharging detection, and threshold validation.
127 lines
5.6 KiB
Python
127 lines
5.6 KiB
Python
"""Verify battery charging guard policy and sysfs integration."""
|
|
import importlib.util
|
|
from pathlib import Path
|
|
import tempfile
|
|
import unittest
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
spec = importlib.util.spec_from_file_location('battery_guard', ROOT / 'scripts/phone-battery-guard.py')
|
|
guard = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(guard)
|
|
|
|
|
|
class BatteryGuardTests(unittest.TestCase):
|
|
def test_policy_thresholds(self):
|
|
policy = guard.BatteryPolicy(low=50, high=65)
|
|
# Low capacity: should charge
|
|
self.assertTrue(policy.evaluate(45))
|
|
self.assertTrue(policy.evaluate(50))
|
|
# Between 50 and 65: maintains previous state (charging)
|
|
self.assertTrue(policy.evaluate(58))
|
|
# High capacity reached (>= 65): should pause charging
|
|
self.assertFalse(policy.evaluate(65))
|
|
self.assertFalse(policy.evaluate(75))
|
|
# Dropping between 50 and 65: maintains previous state (paused)
|
|
self.assertFalse(policy.evaluate(55))
|
|
# Drops to or below low (<= 50): should resume charging
|
|
self.assertTrue(policy.evaluate(49))
|
|
|
|
def test_invalid_thresholds_rejected(self):
|
|
with self.assertRaises(ValueError):
|
|
guard.BatteryPolicy(low=70, high=50)
|
|
with self.assertRaises(ValueError):
|
|
guard.BatteryPolicy(low=5, high=60)
|
|
with self.assertRaises(ValueError):
|
|
guard.BatteryPolicy(low=50, high=99)
|
|
|
|
def test_set_charging_sysfs(self):
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
battery = Path(temp) / 'battery'
|
|
charger = Path(temp) / 'charger'
|
|
battery.mkdir()
|
|
charger.mkdir()
|
|
(battery / 'charging_enabled').write_text('1\n')
|
|
(charger / 'charging_enabled').write_text('1\n')
|
|
(charger / 'current_max').write_text('1500000\n')
|
|
|
|
# Disable charging
|
|
applied = guard.set_charging(battery, charger, False)
|
|
self.assertTrue(applied)
|
|
self.assertEqual((battery / 'charging_enabled').read_text().strip(), '0')
|
|
self.assertEqual((charger / 'charging_enabled').read_text().strip(), '0')
|
|
self.assertEqual((charger / 'current_max').read_text().strip(), '0')
|
|
|
|
# Enable charging
|
|
applied = guard.set_charging(battery, charger, True)
|
|
self.assertTrue(applied)
|
|
self.assertEqual((battery / 'charging_enabled').read_text().strip(), '1')
|
|
self.assertEqual((charger / 'charging_enabled').read_text().strip(), '1')
|
|
self.assertEqual((charger / 'current_max').read_text().strip(), '1500000')
|
|
|
|
def test_thermal_protection(self):
|
|
policy = guard.BatteryPolicy(low=50, high=65, max_temp=45)
|
|
# Low capacity, normal temp (30°C represented as 300 tenths): should charge
|
|
self.assertTrue(policy.evaluate(45, temp=300))
|
|
# Over-temperature (46°C / 460 tenths): should cut off charging despite low SoC
|
|
self.assertFalse(policy.evaluate(45, temp=460))
|
|
self.assertTrue(policy.thermal_throttled)
|
|
# Cooling slightly (44°C): still paused due to 3°C hysteresis
|
|
self.assertFalse(policy.evaluate(45, temp=440))
|
|
# Cooled below (max_temp - 3) = 42°C (41°C / 410 tenths): resumes charging
|
|
self.assertTrue(policy.evaluate(45, temp=410))
|
|
self.assertFalse(policy.thermal_throttled)
|
|
|
|
def test_emergency_low_battery_shutdown(self):
|
|
policy = guard.BatteryPolicy(low=50, high=65, shutdown_at=5)
|
|
# Above shutdown threshold: no shutdown
|
|
self.assertFalse(policy.should_shutdown(10, is_discharging=True))
|
|
self.assertFalse(policy.should_shutdown(6, is_discharging=True))
|
|
# At or below threshold (<= 5) while discharging: should shutdown
|
|
self.assertTrue(policy.should_shutdown(5, is_discharging=True))
|
|
self.assertTrue(policy.should_shutdown(3, is_discharging=True))
|
|
self.assertTrue(policy.should_shutdown(1, is_discharging=True))
|
|
# At or below threshold BUT charging / on external power: do NOT shutdown
|
|
self.assertFalse(policy.should_shutdown(4, is_discharging=False))
|
|
self.assertFalse(policy.should_shutdown(None, is_discharging=True))
|
|
|
|
def test_invalid_shutdown_thresholds_rejected(self):
|
|
with self.assertRaises(ValueError):
|
|
guard.BatteryPolicy(low=50, high=65, shutdown_at=0)
|
|
with self.assertRaises(ValueError):
|
|
guard.BatteryPolicy(low=50, high=65, shutdown_at=50)
|
|
|
|
def test_is_battery_discharging_sysfs(self):
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
battery = Path(temp) / 'battery'
|
|
charger = Path(temp) / 'charger'
|
|
battery.mkdir()
|
|
charger.mkdir()
|
|
|
|
(battery / 'status').write_text('Discharging\n')
|
|
self.assertTrue(guard.is_battery_discharging(battery, charger))
|
|
|
|
(battery / 'status').write_text('Charging\n')
|
|
self.assertFalse(guard.is_battery_discharging(battery, charger))
|
|
|
|
(battery / 'status').unlink()
|
|
(charger / 'online').write_text('0\n')
|
|
self.assertTrue(guard.is_battery_discharging(battery, charger))
|
|
|
|
(charger / 'online').write_text('1\n')
|
|
self.assertFalse(guard.is_battery_discharging(battery, charger))
|
|
|
|
def test_trigger_shutdown_executor(self):
|
|
called = []
|
|
|
|
def mock_executor(cmd, **kwargs):
|
|
called.append(cmd)
|
|
return None
|
|
|
|
guard.trigger_shutdown(executor=mock_executor)
|
|
self.assertEqual(len(called), 1)
|
|
self.assertTrue(any('poweroff' in part for part in called[0]))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|