Files
espresso_frame/server/tests/test_widgets_whiteboard.py
T
tfaour f48daa71c8
Build and push server image / test (push) Successful in 17s
Build and push server image / build-and-push (push) Successful in 2m2s
Widget system Phase 1: per-type render/action modules
New app/widgets/ package (photos.py, calendar.py, whiteboard.py, plus
the WIDGET_TYPES registry) -- the widget-system analogue of
routers/device.py's old RENDERERS/ADVANCE_RENDERERS/BACK_RENDERERS,
generalized from "one mode owns the whole panel" to "each widget renders
into its own region and responds to named button actions." Each module
exposes render(db, frame, widget, target_w, target_h) -> Image.Image
(never raises -- a widget's own fetch hiccup falls back to a small
placeholder rather than taking the whole panel's render down) and an
ACTIONS registry for NEXT/BACK button assignment.

Supporting changes needed to give the widget modules something to call,
all mechanical/behavior-preserving for every existing caller:

- image_pipeline.py: render_panel(regions, ...) generalizes render_frame's
  tail (paste, enhance once, overlay once, quantize once, pack once) from
  one photo to N regions -- not a restructuring, since calendar mode's
  photo-inlay feature already pastes a second composed image onto the
  canvas before that single shared pipeline runs.
- photo_queue.py: advance_forced/back_forced/remove_from_rotation/
  get_current take an explicit `frame` param now that `cfg` won't always
  be the Frame itself once photo-queue state moves to PhotoWidgetConfig
  -- caught a real latent bug while doing this: get_current was reading
  refresh_interval_s off `cfg`, but that's a frame-level wake-cadence
  setting, not something that becomes per-widget, so it now reads that
  off `frame` explicitly instead.
- routers/common.py: list_assets/fetch_source_and_faces take album_id/
  display_mode directly instead of a whole Frame (both only ever read
  that one attribute off it); new get_or_refresh_*_for_widget siblings
  of the existing calendar/weather/tasks/whiteboard cache helpers, read/
  writing the new per-widget config tables -- the Frame-scoped originals
  are untouched and still what routers/device.py's actual dispatch calls
  until the Phase 2 cutover.

26 new tests (95 total): render_panel size/placement/orientation
coverage, and per-widget-type render/action tests (unconfigured ->
placeholder, a fetch failure -> placeholder not a crash, actions mutate
the right state). Full suite passes; diff-reviewed to confirm
device.py's actual RENDERERS dispatch and the old Frame-scoped
get_or_refresh_* bodies are unchanged, so this is safe to deploy on its
own despite being step 1 of a two-step cutover (see the project plan on
why the *next* step, not this one, has to ship atomically).
2026-07-24 08:44:53 -04:00

86 lines
3.1 KiB
Python

"""app.widgets.whiteboard -- unit-level, no HTTP: constructs Widget/
WhiteboardWidgetConfig rows directly and monkeypatches the underlying
fetch/render call (get_or_refresh_whiteboard_for_widget, already covered
against a real WebDAV server + mocked sidecar in
test_whiteboard_refresh_and_browse.py) rather than re-testing that
throttle/fetch logic here."""
from __future__ import annotations
import io
import time
from PIL import Image
from app import widgets
from app.models import Frame, Widget, WhiteboardWidgetConfig
def _make_widget(db_session) -> tuple[Frame, Widget]:
frame = db_session.get(Frame, 1)
widget = Widget(frame_id=frame.id, widget_type="whiteboard", x=0, y=0, w=8, h=5,
sort_order=0, created_at=time.time())
db_session.add(widget)
db_session.flush()
db_session.add(WhiteboardWidgetConfig(widget_id=widget.id, url="https://example.com/board.whiteboard"))
db_session.commit()
return frame, widget
def _tiny_png() -> bytes:
buf = io.BytesIO()
Image.new("RGB", (40, 20), (10, 20, 30)).save(buf, format="PNG")
return buf.getvalue()
def test_render_unconfigured_widget_returns_correctly_sized_placeholder(db_session):
frame = db_session.get(Frame, 1)
widget = Widget(frame_id=frame.id, widget_type="whiteboard", x=0, y=0, w=8, h=5,
sort_order=0, created_at=time.time())
db_session.add(widget)
db_session.flush()
db_session.add(WhiteboardWidgetConfig(widget_id=widget.id)) # no url/user_id set
db_session.commit()
img = widgets.whiteboard.render(db_session, frame, widget, 300, 200)
assert img.size == (300, 200)
def test_render_configured_widget_composes_the_fetched_png(db_session, monkeypatch):
frame, widget = _make_widget(db_session)
png = _tiny_png()
monkeypatch.setattr(widgets.whiteboard, "get_or_refresh_whiteboard_for_widget",
lambda db, frame, widget, force=False: png)
img = widgets.whiteboard.render(db_session, frame, widget, 300, 200)
assert img.size == (300, 200)
assert img.mode == "RGB"
def test_render_falls_back_to_placeholder_when_nothing_cached_yet(db_session, monkeypatch):
frame, widget = _make_widget(db_session)
monkeypatch.setattr(widgets.whiteboard, "get_or_refresh_whiteboard_for_widget",
lambda db, frame, widget, force=False: None)
img = widgets.whiteboard.render(db_session, frame, widget, 300, 200)
assert img.size == (300, 200)
def test_check_now_action_forces_a_refetch(db_session, monkeypatch):
frame, widget = _make_widget(db_session)
calls = []
def fake_refresh(db, frame, widget, force=False):
calls.append(force)
return _tiny_png()
monkeypatch.setattr(widgets.whiteboard, "get_or_refresh_whiteboard_for_widget", fake_refresh)
widgets.whiteboard.ACTIONS["check_now"](db_session, frame, widget)
assert calls == [True]
def test_both_buttons_map_to_check_now():
"""No real "next"/"back" concept for a static board -- both physical
buttons mean the same thing for a whiteboard widget."""
assert set(widgets.whiteboard.ACTIONS) == {"check_now"}