Frame.theme (7 presets in app/theme_tokens.py) drives font family, corner radius, drop shadow, and an accent hue for every modern-style widget's header/accent region. Rich accent colors (not just the 6 flat panel inks) are approximated via denser Bayer stippling confined to just that region (html_render.ordered_dither_regions), so icon/text content elsewhere stays exactly as crisp as it is today -- verified directly against real Chromium renders, both in unit tests and via run-server. "classic" is a byte-identical no-visual-change default: weather's header keeps its original fixed blue gradient, tasks/calendar keep their flat THEME_* ink. Themes are purely stylistic -- battery's charge-level color, calendar/ tasks' per-owner event chips, and text's own per-widget font choice are never touched.
186 lines
7.9 KiB
Python
186 lines
7.9 KiB
Python
"""app.widgets.tasks -- unit-level, no HTTP: constructs Widget/
|
|
TaskWidgetConfig rows directly and monkeypatches the underlying fetch
|
|
call (get_or_refresh_tasks_for_widget, throttle/multi-list-merge/CalDAV
|
|
-fetch logic covered separately -- see test_widget_config_and_queue_
|
|
endpoints.py and test_permission_boundaries.py for the FrameTaskList
|
|
inclusion/color HTTP-layer coverage). Split out of the old calendar
|
|
widget's week-view-only, single-list task list -- see
|
|
models.TaskWidgetConfig/FrameTaskList."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import time
|
|
|
|
from app import widgets
|
|
from app.models import Frame, TaskWidgetConfig, Widget
|
|
|
|
|
|
def _make_widget(db_session, **cfg_kwargs) -> tuple[Frame, Widget]:
|
|
frame = db_session.get(Frame, 1)
|
|
widget = Widget(frame_id=frame.id, widget_type="tasks", 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(TaskWidgetConfig(widget_id=widget.id, **cfg_kwargs))
|
|
db_session.commit()
|
|
return frame, widget
|
|
|
|
|
|
def test_render_produces_a_correctly_sized_image_with_no_lists_included(db_session, monkeypatch):
|
|
"""No placeholder for "nothing included yet" -- same posture as
|
|
app/widgets/calendar.py (see tasks.py's own module docstring):
|
|
get_or_refresh_tasks_for_widget already returns [] with nothing
|
|
included, and render() draws straight through that."""
|
|
frame, widget = _make_widget(db_session)
|
|
monkeypatch.setattr(widgets.tasks, "get_or_refresh_tasks_for_widget", lambda db, frame, widget: [])
|
|
|
|
img = widgets.tasks.render(db_session, frame, widget, 300, 200)
|
|
assert img.size == (300, 200)
|
|
assert img.mode == "RGB"
|
|
|
|
|
|
def test_render_produces_a_correctly_sized_image_with_tasks(db_session, monkeypatch):
|
|
frame, widget = _make_widget(db_session)
|
|
monkeypatch.setattr(widgets.tasks, "get_or_refresh_tasks_for_widget",
|
|
lambda db, frame, widget: [
|
|
{"summary": "Buy milk", "due": None, "completed_at": None,
|
|
"owner_display_name": "Alice", "color_index": None},
|
|
])
|
|
|
|
img = widgets.tasks.render(db_session, frame, widget, 300, 200)
|
|
assert img.size == (300, 200)
|
|
assert img.mode == "RGB"
|
|
|
|
|
|
def test_render_with_completed_tasks(db_session, monkeypatch):
|
|
"""show_completed's merged result includes completed tasks too (see
|
|
caldav_client.fetch_tasks' completed_since) -- render() must handle
|
|
that shape (a non-None completed_at) without error."""
|
|
frame, widget = _make_widget(db_session)
|
|
monkeypatch.setattr(widgets.tasks, "get_or_refresh_tasks_for_widget",
|
|
lambda db, frame, widget: [
|
|
{"summary": "Walk the dog", "due": "2026-08-01", "completed_at": None,
|
|
"owner_display_name": "Alice", "color_index": 2},
|
|
{"summary": "Buy milk", "due": None, "completed_at": "2026-08-01T10:00:00+00:00",
|
|
"owner_display_name": "Alice", "color_index": 2},
|
|
])
|
|
|
|
img = widgets.tasks.render(db_session, frame, widget, 300, 200)
|
|
assert img.size == (300, 200)
|
|
|
|
|
|
def _capture_build_tasks_title(monkeypatch):
|
|
"""Wraps the real _build_tasks to record the `title` it was called
|
|
with, while still rendering for real (not a bare stub) so this
|
|
keeps exercising the actual render path."""
|
|
seen_titles = []
|
|
real_build_tasks = widgets.tasks._build_tasks
|
|
|
|
def spy(tasks, target_w, target_h, palette_rgb=None, title="Tasks"):
|
|
seen_titles.append(title)
|
|
return real_build_tasks(tasks, target_w, target_h, palette_rgb, title)
|
|
|
|
monkeypatch.setattr(widgets.tasks, "_build_tasks", spy)
|
|
return seen_titles
|
|
|
|
|
|
def test_render_passes_custom_name_as_title(db_session, monkeypatch):
|
|
frame, widget = _make_widget(db_session, name="Chores")
|
|
monkeypatch.setattr(widgets.tasks, "get_or_refresh_tasks_for_widget", lambda db, frame, widget: [])
|
|
seen_titles = _capture_build_tasks_title(monkeypatch)
|
|
|
|
widgets.tasks.render(db_session, frame, widget, 300, 200)
|
|
assert seen_titles == ["Chores"]
|
|
|
|
|
|
def test_render_falls_back_to_default_title_when_name_is_blank(db_session, monkeypatch):
|
|
frame, widget = _make_widget(db_session) # name defaults to ""
|
|
monkeypatch.setattr(widgets.tasks, "get_or_refresh_tasks_for_widget", lambda db, frame, widget: [])
|
|
seen_titles = _capture_build_tasks_title(monkeypatch)
|
|
|
|
widgets.tasks.render(db_session, frame, widget, 300, 200)
|
|
assert seen_titles == ["Tasks"]
|
|
|
|
|
|
def test_render_with_a_very_long_custom_name_still_fits(db_session, monkeypatch):
|
|
"""calendar_render._draw_tasks truncates the title to fit -- a name
|
|
far longer than any placed widget could display must not error or
|
|
overflow the requested size."""
|
|
frame, widget = _make_widget(db_session, name="A" * 200)
|
|
monkeypatch.setattr(widgets.tasks, "get_or_refresh_tasks_for_widget", lambda db, frame, widget: [])
|
|
|
|
img = widgets.tasks.render(db_session, frame, widget, 200, 192)
|
|
assert img.size == (200, 192)
|
|
|
|
|
|
def test_render_at_minimum_grid_footprint(db_session, monkeypatch):
|
|
"""grid.MIN_FOOTPRINT["tasks"] is (2, 2) cells -- on an 8x5 grid
|
|
against a full 800x480 panel that's a 200x192 box, the smallest a
|
|
tasks widget can actually be placed at."""
|
|
frame, widget = _make_widget(db_session)
|
|
monkeypatch.setattr(widgets.tasks, "get_or_refresh_tasks_for_widget", lambda db, frame, widget: [])
|
|
|
|
img = widgets.tasks.render(db_session, frame, widget, 200, 192)
|
|
assert img.size == (200, 192)
|
|
|
|
|
|
def test_no_button_actions():
|
|
"""A passive checklist on the same throttled-refresh cadence as
|
|
weather -- nothing to advance/back/force."""
|
|
assert widgets.tasks.ACTIONS == {}
|
|
assert widgets.tasks.ACTION_LABELS == {}
|
|
|
|
|
|
def test_render_modern_style(db_session, monkeypatch):
|
|
from PIL import Image
|
|
|
|
from app import html_render
|
|
|
|
frame, widget = _make_widget(db_session, render_style="modern", name="Chores")
|
|
tasks = [
|
|
{"summary": "Buy milk", "due": None, "completed_at": None,
|
|
"owner_display_name": "Alice", "color_index": None},
|
|
{"summary": "Walk the dog", "due": "2026-08-01", "completed_at": None,
|
|
"owner_display_name": "Alice", "color_index": None},
|
|
]
|
|
monkeypatch.setattr(widgets.tasks, "get_or_refresh_tasks_for_widget", lambda db, frame, widget: tasks)
|
|
|
|
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.tasks.render(db_session, frame, widget, 300, 200)
|
|
assert img.size == (300, 200)
|
|
assert img.mode == "RGB"
|
|
|
|
|
|
def test_render_modern_threads_frame_theme_through_to_resolve_theme(db_session, monkeypatch):
|
|
"""Same spy-on-resolve_theme approach as test_widgets_weather.py's
|
|
equivalent test -- confirms tasks.py's render() passes frame.theme
|
|
into html_render.build_tasks (proving the widget-level threading, not
|
|
re-testing ordered_dither_regions itself, which test_html_render.py
|
|
already covers)."""
|
|
from PIL import Image
|
|
|
|
from app import html_render, theme_tokens
|
|
|
|
calls = []
|
|
real_resolve = theme_tokens.resolve_theme
|
|
|
|
def _spy(theme_name, widget_kind, palette_rgb):
|
|
calls.append((theme_name, widget_kind))
|
|
return real_resolve(theme_name, widget_kind, palette_rgb)
|
|
|
|
monkeypatch.setattr(theme_tokens, "resolve_theme", _spy)
|
|
monkeypatch.setattr(html_render, "theme_tokens", theme_tokens)
|
|
monkeypatch.setattr(html_render, "render_html_to_image",
|
|
lambda html, target_w, target_h: Image.new("RGB", (target_w, target_h), (255, 255, 255)))
|
|
|
|
frame, widget = _make_widget(db_session, render_style="modern")
|
|
frame.theme = "slate"
|
|
monkeypatch.setattr(widgets.tasks, "get_or_refresh_tasks_for_widget", lambda db, frame, widget: [])
|
|
|
|
widgets.tasks.render(db_session, frame, widget, 300, 200)
|
|
assert ("slate", "tasks") in calls
|