Files
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

181 lines
8.0 KiB
Python

"""app.widgets.photos -- unit-level, no HTTP: constructs Widget/
PhotoWidgetConfig rows directly and monkeypatches the Immich-facing
calls (list_assets/fetch_source_and_faces/immich_client_for) rather than
standing up a fake Immich server, since this module's own logic (what it
does with whatever Immich returns) is what's under test, not Immich's
API shape -- already covered by ImmichClient's own tests if any, and by
this suite's HTTP-level permission-boundary tests elsewhere."""
from __future__ import annotations
import time
from PIL import Image
from app import widgets
from app.db import widget_locked
from app.models import Frame, PhotoWidgetConfig, Widget
_ASSETS = [{"id": "asset-1"}, {"id": "asset-2"}, {"id": "asset-3"}]
def _make_widget(db_session, album_id="album-1") -> tuple[Frame, Widget]:
frame = db_session.get(Frame, 1)
widget = Widget(frame_id=frame.id, widget_type="photos", 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(PhotoWidgetConfig(widget_id=widget.id, album_id=album_id, queue_target_len=5))
db_session.commit()
return frame, widget
def test_render_unconfigured_widget_returns_correctly_sized_placeholder(db_session):
frame, widget = _make_widget(db_session, album_id="")
img = widgets.photos.render(db_session, frame, widget, 400, 300)
assert img.size == (400, 300)
assert img.mode == "RGB"
def test_render_configured_widget_composes_a_photo(db_session, monkeypatch):
frame, widget = _make_widget(db_session)
monkeypatch.setattr(widgets.photos, "immich_client_for", lambda frame: object())
monkeypatch.setattr(widgets.photos, "list_assets", lambda client, album_id: _ASSETS)
source = Image.new("RGB", (100, 80), (10, 20, 30))
monkeypatch.setattr(widgets.photos, "fetch_source_and_faces", lambda client, mode, asset_id: (source, None))
img = widgets.photos.render(db_session, frame, widget, 400, 300)
assert img.size == (400, 300)
cfg = db_session.get(PhotoWidgetConfig, widget.id)
assert cfg.current_asset_id == "asset-1" # get_current picked the first asset
def test_render_falls_back_to_placeholder_on_immich_failure(db_session, monkeypatch):
from fastapi import HTTPException
frame, widget = _make_widget(db_session)
def _raise(client, album_id):
raise HTTPException(502, "Could not reach Immich")
monkeypatch.setattr(widgets.photos, "immich_client_for", lambda frame: object())
monkeypatch.setattr(widgets.photos, "list_assets", _raise)
img = widgets.photos.render(db_session, frame, widget, 400, 300)
assert img.size == (400, 300) # placeholder, not a crash
def test_advance_action_moves_to_next_photo(db_session, monkeypatch):
frame, widget = _make_widget(db_session)
monkeypatch.setattr(widgets.photos, "immich_client_for", lambda frame: object())
monkeypatch.setattr(widgets.photos, "list_assets", lambda client, album_id: _ASSETS)
with widget_locked(db_session, frame.id, widget.id) as (_, _, cfg):
cfg.current_asset_id = "asset-1"
widgets.photos.ACTIONS["advance"](db_session, frame, widget)
cfg = db_session.get(PhotoWidgetConfig, widget.id)
assert cfg.current_asset_id != "asset-1"
assert cfg.history == ["asset-1"]
def test_back_action_undoes_advance(db_session, monkeypatch):
frame, widget = _make_widget(db_session)
monkeypatch.setattr(widgets.photos, "immich_client_for", lambda frame: object())
monkeypatch.setattr(widgets.photos, "list_assets", lambda client, album_id: _ASSETS)
# advance_forced only pushes the *previous* current photo onto history
# -- the very first advance from an empty current_asset_id has nothing
# to push, so a second advance is needed before there's anything for
# "back" to undo.
widgets.photos.ACTIONS["advance"](db_session, frame, widget)
first = db_session.get(PhotoWidgetConfig, widget.id).current_asset_id
widgets.photos.ACTIONS["advance"](db_session, frame, widget)
second = db_session.get(PhotoWidgetConfig, widget.id).current_asset_id
assert second != first
widgets.photos.ACTIONS["back"](db_session, frame, widget)
cfg = db_session.get(PhotoWidgetConfig, widget.id)
assert cfg.current_asset_id == first
def test_advance_action_is_a_no_op_when_unconfigured(db_session):
frame, widget = _make_widget(db_session, album_id="")
widgets.photos.ACTIONS["advance"](db_session, frame, widget) # must not raise
cfg = db_session.get(PhotoWidgetConfig, widget.id)
assert cfg.current_asset_id == ""
def test_render_quantizes_against_photo_palette_not_the_main_frame_palette(db_session, monkeypatch):
"""Regression/design test: photos.py's render() must quantize against
Frame.photo_palette_rgb, genuinely independent of Frame.palette_rgb --
the whole point of giving photos its own palette (see widgets/photos.
py's module docstring). Uses a custom photo_palette_rgb whose "black"
slot is a distinctive color that doesn't appear anywhere in
DEFAULT_PALETTE_RGB, so the assertion only passes if photo_palette_rgb
was actually the one used."""
frame, widget = _make_widget(db_session)
custom_photo_palette = [
[10, 20, 30], [255, 255, 255], [255, 219, 0], [207, 0, 15], [0, 39, 133], [0, 133, 55],
]
frame.photo_palette_rgb = custom_photo_palette
frame.photo_dither_strength = 0.0 # flat quantize -- exact, no diffusion noise to account for
db_session.commit()
assert frame.palette_rgb is None # main palette stays at its default throughout
monkeypatch.setattr(widgets.photos, "immich_client_for", lambda frame: object())
monkeypatch.setattr(widgets.photos, "list_assets", lambda client, album_id: _ASSETS)
# A near-black source photo -- under the MAIN default palette this
# would quantize to (0, 0, 0); under custom_photo_palette's distinctive
# "black" slot it must quantize to exactly (10, 20, 30) instead.
source = Image.new("RGB", (100, 80), (5, 5, 5))
monkeypatch.setattr(widgets.photos, "fetch_source_and_faces", lambda client, mode, asset_id: (source, None))
img = widgets.photos.render(db_session, frame, widget, 400, 300)
assert set(img.getdata()) == {(10, 20, 30)}
def test_render_does_not_advance_when_locked(db_session, monkeypatch):
frame, widget = _make_widget(db_session)
monkeypatch.setattr(widgets.photos, "immich_client_for", lambda frame: object())
monkeypatch.setattr(widgets.photos, "list_assets", lambda client, album_id: _ASSETS)
source = Image.new("RGB", (100, 80), (10, 20, 30))
monkeypatch.setattr(widgets.photos, "fetch_source_and_faces", lambda client, mode, asset_id: (source, None))
with widget_locked(db_session, frame.id, widget.id) as (_, _, cfg):
cfg.current_asset_id = "asset-1"
cfg.current_asset_set_at = 0.0 # long ago -- refresh_interval_s has definitely elapsed
cfg.locked = True
widgets.photos.render(db_session, frame, widget, 400, 300)
cfg = db_session.get(PhotoWidgetConfig, widget.id)
assert cfg.current_asset_id == "asset-1"
def test_advance_and_back_actions_are_no_ops_when_locked(db_session, monkeypatch):
frame, widget = _make_widget(db_session)
monkeypatch.setattr(widgets.photos, "immich_client_for", lambda frame: object())
monkeypatch.setattr(widgets.photos, "list_assets", lambda client, album_id: _ASSETS)
with widget_locked(db_session, frame.id, widget.id) as (_, _, cfg):
cfg.current_asset_id = "asset-1"
cfg.locked = True
widgets.photos.ACTIONS["advance"](db_session, frame, widget)
widgets.photos.ACTIONS["back"](db_session, frame, widget)
cfg = db_session.get(PhotoWidgetConfig, widget.id)
assert cfg.current_asset_id == "asset-1"
assert cfg.history == []
def test_advance_uses_the_frame_stats_counter_not_the_widget_config():
"""advance_forced's frame/stats split (see app/photo_queue.py) means
stats_photos_displayed should land on the Frame row, never on
PhotoWidgetConfig (which has no such column at all)."""
assert not hasattr(PhotoWidgetConfig, "stats_photos_displayed")