Files
espresso_frame/server/tests/test_widgets_static.py
T
tfaour e331f5e5a1
Build and push server image / test (push) Successful in 43s
Build and push server image / build-and-push (push) Successful in 3m51s
Build and push server image / deploy (push) Failing after 1m57s
Roll out "modern" HTML/CSS render style to every widget except photos
Extends weather's experimental Chromium+Jinja2 render style to battery,
text, tasks, static image, whiteboard, and calendar (all four view
modes -- agenda/today_tomorrow/week/month), and gives the photos widget
its own genuinely independent palette + dithering strength.

Photos: Frame.photo_palette_rgb/photo_dither_strength (mirroring the
existing palette_rgb/dither_strength), with a second "Photos
configuration" card in Advanced Configuration. widgets/photos.py's
render() quantizes itself against these before returning -- no
render_panel changes needed, since photos is the only widget that
genuinely needs a different reference palette and can carry that
itself, the same way modern-style widgets already self-dither via
ordered_dither.

Battery/text/tasks/static image/whiteboard: same render_style pattern
weather established (render_style column, html_render.py build
function, Jinja2 template, dialog toggle). Static image/whiteboard get
their first-ever visual chrome (a rounded-corner shadowed card,
shared framed_image.html.jinja) since classic draws them with zero
frame at all. Fixed the same "preview endpoint bypasses render_style"
bug weather originally shipped with, for tasks/static/whiteboard/
calendar's preview endpoints.

Calendar: own module (app/calendar_html_render.py, mirroring
calendar_render.py's separation from the simpler widgets) covering all
four view modes, not just agenda -- reuses calendar_render's own
private helpers so event colors/times/weather/month-grid math match
classic exactly. Found and fixed two real cross-day layout bugs along
the way: a per-day header height that varied based on whether that
specific day had a weather entry (misaligning where every other day's
event rows started across the week/month grid), and regular-weight
small text being fragile under Bayer ordered dithering (out-of-month
day numbers degraded into unrecognizable speckle) -- fixed by using
bold everywhere and de-emphasizing via size instead of weight/gray,
since gray text has the same dithering fragility this project's PIL
renderers already avoid for exactly this reason.

Migrations 32-38 (Frame's two new columns, then one render_style column
per widget config table). 452 tests passing, including new dispatch/
migration coverage per widget type and a dedicated photos test proving
photo_palette_rgb produces genuinely independent quantization from the
frame's main palette_rgb.
2026-07-31 03:52:19 +00:00

90 lines
3.2 KiB
Python

"""app.widgets.static_image -- unit-level, no HTTP: constructs Widget/
StaticWidgetConfig rows directly. The decode-at-upload-time path (see
app/image_upload.py) is covered separately in test_image_upload.py and
the HTTP-level upload endpoint in
test_widget_config_and_queue_endpoints.py -- these tests only exercise
render(), which always starts from already-decoded PNG bytes."""
from __future__ import annotations
import io
import time
from PIL import Image
from app import widgets
from app.models import Frame, StaticWidgetConfig, Widget
def _png_bytes(size=(40, 30), color=(200, 50, 50)) -> bytes:
buf = io.BytesIO()
Image.new("RGB", size, color).save(buf, format="PNG")
return buf.getvalue()
def _make_widget(db_session, **cfg_kwargs) -> tuple[Frame, Widget]:
frame = db_session.get(Frame, 1)
widget = Widget(frame_id=frame.id, widget_type="static", x=0, y=0, w=2, h=2,
sort_order=0, created_at=time.time())
db_session.add(widget)
db_session.flush()
db_session.add(StaticWidgetConfig(widget_id=widget.id, **cfg_kwargs))
db_session.commit()
return frame, widget
def test_render_shows_a_placeholder_when_no_image_uploaded(db_session):
frame, widget = _make_widget(db_session)
img = widgets.static_image.render(db_session, frame, widget, 300, 200)
assert img.size == (300, 200)
assert img.mode == "RGB"
def test_render_composes_the_uploaded_image(db_session):
frame, widget = _make_widget(db_session, image=_png_bytes())
img = widgets.static_image.render(db_session, frame, widget, 300, 200)
assert img.size == (300, 200)
assert img.mode == "RGB"
def test_render_respects_display_mode_stretch_fill(db_session):
frame, widget = _make_widget(db_session, image=_png_bytes(), display_mode="stretch_fill")
img = widgets.static_image.render(db_session, frame, widget, 300, 200)
assert img.size == (300, 200)
def test_render_respects_display_mode_letterbox(db_session):
frame, widget = _make_widget(db_session, image=_png_bytes(size=(100, 10)), display_mode="letterbox")
img = widgets.static_image.render(db_session, frame, widget, 300, 200)
assert img.size == (300, 200)
def test_render_at_minimum_grid_footprint(db_session):
"""grid.MIN_FOOTPRINT["static"] is (1, 1) cells -- on an 8x5 grid
against a full 800x480 panel that's a 100x96 box, the smallest a
static widget can actually be placed at."""
frame, widget = _make_widget(db_session, image=_png_bytes())
img = widgets.static_image.render(db_session, frame, widget, 100, 96)
assert img.size == (100, 96)
def test_no_button_actions():
"""A fixed uploaded image -- nothing to advance/back/check."""
assert widgets.static_image.ACTIONS == {}
assert widgets.static_image.ACTION_LABELS == {}
def test_render_modern_style(db_session, monkeypatch):
from app import html_render
frame, widget = _make_widget(db_session, image=_png_bytes(), render_style="modern")
def _stub(html, target_w, target_h):
return Image.new("RGB", (target_w, target_h), (255, 255, 255))
monkeypatch.setattr(html_render, "render_html_to_image", _stub)
img = widgets.static_image.render(db_session, frame, widget, 300, 200)
assert img.size == (300, 200)
assert img.mode == "RGB"