Files
espresso_frame/server/tests/test_widgets_battery.py
T
tfaour eb7127718b
Build and push server image / test (push) Successful in 30s
Build and push server image / build-and-push (push) Successful in 2m4s
Build and push server image / deploy (push) Successful in 50s
Add battery widget (device's own last-reported level, no live upstream)
Shows Frame.battery_percent/battery_as_of, already set by every device
wake-on-battery report, plus routers/common.py's existing
battery_estimate_s time-remaining estimate -- nothing new to fetch or
cache. Compact (icon + percent) or detailed (+ estimate, last report
age) display mode. No button actions.
2026-07-27 19:02:21 +00:00

84 lines
3.0 KiB
Python

"""app.widgets.battery -- unit-level, no HTTP: constructs Widget/
BatteryWidgetConfig rows directly and sets Frame.battery_percent/
battery_as_of straight on the Frame row, same as routers/device.py's
frame_battery would."""
from __future__ import annotations
import time
from app import widgets
from app.models import BatteryWidgetConfig, Frame, Widget
def _make_widget(db_session, mode="detailed") -> tuple[Frame, Widget]:
frame = db_session.get(Frame, 1)
widget = Widget(frame_id=frame.id, widget_type="battery", x=0, y=0, w=1, h=1,
sort_order=0, created_at=time.time())
db_session.add(widget)
db_session.flush()
db_session.add(BatteryWidgetConfig(widget_id=widget.id, mode=mode))
db_session.commit()
return frame, widget
def test_render_shows_a_placeholder_when_frame_has_never_reported(db_session):
frame, widget = _make_widget(db_session)
assert frame.battery_percent == -1
img = widgets.battery.render(db_session, frame, widget, 300, 200)
assert img.size == (300, 200)
assert img.mode == "RGB"
def test_render_shows_the_reported_percent(db_session):
frame, widget = _make_widget(db_session)
frame.battery_percent = 42
frame.battery_as_of = time.time()
db_session.commit()
img = widgets.battery.render(db_session, frame, widget, 300, 200)
assert img.size == (300, 200)
assert img.mode == "RGB"
def test_render_compact_mode_omits_the_estimate_lines(db_session):
frame, widget = _make_widget(db_session, mode="compact")
frame.battery_percent = 80
frame.battery_as_of = time.time()
db_session.commit()
img = widgets.battery.render(db_session, frame, widget, 300, 200)
assert img.size == (300, 200)
def test_render_at_minimum_grid_footprint(db_session):
"""grid.MIN_FOOTPRINT["battery"] is (1, 1) cells -- on an 8x5 grid
against a full 800x480 panel that's a 100x96 box, the smallest a
battery widget can actually be placed at."""
frame, widget = _make_widget(db_session)
frame.battery_percent = 15
db_session.commit()
img = widgets.battery.render(db_session, frame, widget, 100, 96)
assert img.size == (100, 96)
def test_render_falls_back_to_detailed_mode_with_no_config_row(db_session):
"""widget_id has no BatteryWidgetConfig row at all -- render() must
not raise, matching every other widget type's "never raises for a
foreseeable failure" contract."""
frame = db_session.get(Frame, 1)
widget = Widget(frame_id=frame.id, widget_type="battery", x=0, y=0, w=1, h=1,
sort_order=0, created_at=time.time())
db_session.add(widget)
db_session.flush()
db_session.commit()
frame.battery_percent = 60
db_session.commit()
img = widgets.battery.render(db_session, frame, widget, 100, 96)
assert img.size == (100, 96)
def test_no_button_actions():
"""A number the device itself pushes on every wake -- nothing to
advance/back/check."""
assert widgets.battery.ACTIONS == {}
assert widgets.battery.ACTION_LABELS == {}