Files
second-screen/tests/test_weather_view.py
T
rickzkmandClaude Opus 5.5 6c9a1420d2 Week ahead in the weather strip, reverse LCD at night
The weekday strip now shows today and the next six days (icon, max, min),
the current temperature and feels-like sit next to the clock, and the rain
graph spans the full width with the hour under every bar. From sunset to
sunrise the whole frame, seconds patch included, is drawn as a reverse LCD.
The rain label is now spelled DÉŠŤ.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-28 22:40:34 +01:00

237 lines
7.9 KiB
Python

from datetime import date, datetime, time, timedelta
import numpy as np
from PIL import ImageChops
from lcdkit import BG, INK
from tests.helpers import count_colour
from view import Patch
from weather_data import Day, Hour, Weather
from weather_view import WeatherView, is_night
NOW = datetime(2026, 10, 22, 12, 45, 8) # a Thursday
CODES = (63, 45, 61, 3, 80, 0, 71)
CELL = 65 # width of one day in the forecast strip
BAR_X, BAR_PITCH, BAR_W = 22, 37, 26
class StubPoller:
def __init__(self, value=None, version=1, age=60.0):
self.value, self.version, self._age = value, version, age
self.failures = 0
def age(self):
return self._age
def start(self):
pass
def stop(self):
pass
def day(d: date, sunrise=time(7, 20), sunset=time(17, 45)) -> Day:
"""A forecast day whose values depend only on its date."""
return Day(d, CODES[d.toordinal() % 7], float(5 + d.day % 10), float(d.day % 5),
datetime.combine(d, sunrise), datetime.combine(d, sunset))
def week(start=date(2026, 10, 22), n=7):
return tuple(day(start + timedelta(days=i)) for i in range(n))
def weather(temp=14.2, rain=(0, 0, 10, 20, 40, 70, 60, 50, 30, 20, 10, 0), code=2, days=None):
hours = tuple(Hour(datetime(2026, 10, 22, 12 + i, 0), p, 0.2) for i, p in enumerate(rain))
return Weather(temp=temp, feels=12.1, code=code, days=week() if days is None else days, hours=hours)
def view(**kw):
return WeatherView(StubPoller(**kw))
def ink_bbox(img, box):
"""Bounding box of INK pixels inside `box`, in image coordinates, or None."""
region = np.asarray(img.crop(box).convert("RGB"))
ys, xs = np.where((region == np.array(INK)).all(axis=2))
if len(xs) == 0:
return None
return (box[0] + xs.min(), box[1] + ys.min(), box[0] + xs.max(), box[1] + ys.max())
def icon_box(i):
cx = 12 + i * CELL + CELL // 2
return (cx - 16, 34, cx + 16, 68)
def hour_label_box(i):
x = BAR_X + i * BAR_PITCH
return (x, 290, x + BAR_W, 304)
def same(a, b):
return ImageChops.difference(a.convert("RGB"), b.convert("RGB")).getbbox() is None
def test_render_size_is_480x320():
assert view(value=weather()).render(NOW).size == (480, 320)
def test_frame_key_changes_each_minute_not_each_second():
v = view(value=weather())
assert v.frame_key(datetime(2026, 10, 22, 12, 45, 0)) == v.frame_key(datetime(2026, 10, 22, 12, 45, 59))
assert v.frame_key(datetime(2026, 10, 22, 12, 45, 59)) != v.frame_key(datetime(2026, 10, 22, 12, 46, 0))
def test_frame_key_changes_when_weather_updates():
p = StubPoller(value=weather(), version=1)
v = WeatherView(p)
before = v.frame_key(NOW)
p.version = 2
assert v.frame_key(NOW) != before
def test_frame_key_changes_when_stale_flips():
p = StubPoller(value=weather(), age=60.0)
v = WeatherView(p)
before = v.frame_key(NOW)
p._age = WeatherView.STALE_AFTER + 1
assert v.frame_key(NOW) != before
def test_patch_matches_full_render_region():
v = view(value=weather())
patch = v.patch(NOW)
assert isinstance(patch, Patch)
full = v.render(NOW)
region = full.crop((patch.x, patch.y, patch.x + patch.image.width, patch.y + patch.image.height))
assert same(region, patch.image)
def test_patch_changes_with_the_second():
v = view(value=weather())
a = v.patch(datetime(2026, 10, 22, 12, 45, 8)).image
b = v.patch(datetime(2026, 10, 22, 12, 45, 9)).image
assert not same(a, b)
def test_no_weather_renders_without_error():
img = view(value=None, version=0, age=None).render(NOW)
assert img.size == (480, 320)
# day names, clock and dashes are still drawn
assert count_colour(img, INK) > 1000
def test_stale_tag_only_when_old():
fresh = view(value=weather(), age=60.0).render(NOW)
old = view(value=weather(), age=7200.0).render(NOW)
bbox = ImageChops.difference(fresh.convert("RGB"), old.convert("RGB")).getbbox()
assert bbox is not None
assert bbox[0] >= 20 and bbox[1] >= 200 and bbox[2] <= 300 and bbox[3] <= 226 # on the rain header line
def test_today_is_the_first_day_and_inverted():
img = view(value=weather()).render(NOW)
assert img.getpixel((12 + 5, 14)) == INK
assert img.getpixel((12 + CELL + 5, 14)) != INK
def test_days_before_today_are_skipped():
# just after midnight the last fetch still starts with yesterday
from_yesterday = view(value=weather(days=week(start=date(2026, 10, 21)))).render(NOW)
from_today = view(value=weather(days=week(start=date(2026, 10, 22), n=6))).render(NOW)
assert same(from_yesterday, from_today)
def test_only_days_with_a_forecast_get_an_icon():
img = view(value=weather(days=week(n=3))).render(NOW)
for i in range(3):
assert ink_bbox(img, icon_box(i)) is not None, i
for i in range(3, 7):
assert ink_bbox(img, icon_box(i)) is None, i
def test_every_bar_has_its_hour_label():
img = view(value=weather()).render(NOW)
for i in range(12):
assert ink_bbox(img, hour_label_box(i)) is not None, i
def test_hour_labels_only_for_known_hours():
img = view(value=weather(rain=(10, 20))).render(NOW)
assert ink_bbox(img, hour_label_box(1)) is not None
for i in range(2, 12):
assert ink_bbox(img, hour_label_box(i)) is None, i
def test_negative_temps_stay_inside_the_frame():
for temp in (-3.4, -10.4, -19.6):
img = view(value=weather(temp=temp)).render(NOW)
box = ink_bbox(img, (380, 104, 471, 150))
assert box is not None and box[2] < 466, temp
def test_negative_temp_draws_a_minus():
cold = view(value=weather(temp=-3.4)).render(NOW)
warm = view(value=weather(temp=3.4)).render(NOW)
assert not same(cold, warm)
def test_fewer_than_twelve_hours_still_renders():
img = view(value=weather(rain=(10, 20))).render(NOW)
assert img.size == (480, 320)
def test_unknown_weather_code_renders():
assert view(value=weather(code=999)).render(NOW).size == (480, 320)
# night: from sunset to sunrise the frame is a reverse LCD
def test_is_night_between_sunset_and_sunrise():
w = weather() # sunrise 7:20, sunset 17:45
assert is_night(datetime(2026, 10, 22, 21, 0), w)
assert is_night(datetime(2026, 10, 22, 5, 30), w)
assert not is_night(datetime(2026, 10, 22, 12, 45), w)
def test_is_night_flips_exactly_at_sunrise_and_sunset():
w = weather()
assert not is_night(datetime(2026, 10, 22, 7, 20), w)
assert is_night(datetime(2026, 10, 22, 7, 19), w)
assert not is_night(datetime(2026, 10, 22, 17, 44), w)
assert is_night(datetime(2026, 10, 22, 17, 45), w)
def test_is_night_uses_todays_sun_times():
days = (day(date(2026, 10, 21), sunset=time(16, 0)), day(date(2026, 10, 22), sunset=time(17, 45)))
assert not is_night(datetime(2026, 10, 22, 16, 30), weather(days=days))
def test_is_night_without_today_uses_the_closest_known_day():
# data a week old (fetches failing): sunrise moves only minutes a day, so old times are close enough
days = (day(date(2026, 10, 15), sunrise=time(7, 8), sunset=time(17, 58)),)
assert is_night(datetime(2026, 10, 22, 21, 0), weather(days=days))
assert not is_night(datetime(2026, 10, 22, 12, 0), weather(days=days))
def test_is_night_without_any_forecast_stays_day():
assert not is_night(datetime(2026, 10, 22, 23, 0), None)
assert not is_night(datetime(2026, 10, 22, 23, 0), weather(days=()))
def test_night_frame_is_dark():
night = view(value=weather()).render(datetime(2026, 10, 22, 21, 0, 0))
day_ = view(value=weather()).render(NOW)
assert sum(night.getpixel((2, 2))) < 100
assert day_.getpixel((2, 2)) == BG
def test_night_patch_matches_full_render_region():
v = view(value=weather())
at = datetime(2026, 10, 22, 21, 0, 8)
patch = v.patch(at)
full = v.render(at)
region = full.crop((patch.x, patch.y, patch.x + patch.image.width, patch.y + patch.image.height))
assert same(region, patch.image)
assert sum(patch.image.getpixel((0, 0))) < 100