import json from datetime import datetime from pathlib import Path import pytest from PIL import ImageChops import astro from iss_view import LAT_BOTTOM, LAT_TOP, MAP_H, MAP_TOP, MARKER, PANEL_TOP, TEXT, IssView, is_day, map_xy, \ pass_cells from sky_data import parse_clouds, parse_tle from tests.helpers import count_near FIXTURES = Path(__file__).parent / "fixtures" TLE = parse_tle((FIXTURES / "celestrak-iss.tle").read_text()) CLOUDS = parse_clouds(json.loads((FIXTURES / "open-meteo-clouds.json").read_text())) NOW = datetime(2026, 10, 28, 19, 30, 8) NIGHT_IN_ZLIN = (-13.3, -101.5) # subsolar point at that moment 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 view(tle=TLE, clouds=CLOUDS, tle_age=3600.0): return IssView(StubPoller(tle, age=tle_age), StubPoller(clouds)) def test_render_size_is_480x320(): assert view().render(NOW).size == (480, 320) def test_frame_key_changes_every_ten_seconds(): v = view() assert v.frame_key(datetime(2026, 10, 28, 19, 30, 0)) == v.frame_key(datetime(2026, 10, 28, 19, 30, 9)) assert v.frame_key(datetime(2026, 10, 28, 19, 30, 9)) != v.frame_key(datetime(2026, 10, 28, 19, 30, 10)) def test_frame_key_changes_when_the_orbit_updates(): tle = StubPoller(TLE) v = IssView(tle, StubPoller(CLOUDS)) before = v.frame_key(NOW) tle.version = 2 assert v.frame_key(NOW) != before def test_nothing_to_patch_between_frames(): assert view().patch(NOW) is None def test_map_is_equirectangular_under_the_header(): assert map_xy(LAT_TOP, -180) == pytest.approx((0, MAP_TOP)) assert map_xy(LAT_BOTTOM, 180) == pytest.approx((480, MAP_TOP + MAP_H)) (x0, y0), (x1, y1) = map_xy(0, 0), map_xy(10, 10) assert x1 - x0 == pytest.approx(y0 - y1) # degrees as wide as they are tall def test_the_whole_orbit_fits_on_the_map(): assert MAP_TOP < map_xy(51.7, 0)[1] and map_xy(-51.7, 0)[1] < MAP_TOP + MAP_H def test_day_and_night(): assert is_day(0, 0, (0, 0)) assert not is_day(0, 180, (0, 0)) assert not is_day(49.2, 17.7, NIGHT_IN_ZLIN) # Zlín at 19:30 in October assert is_day(-10, -50, NIGHT_IN_ZLIN) # Brazil in the afternoon def test_marker_is_where_the_iss_is(): img = view().render(NOW) fix = astro.iss_fix(TLE, NOW) x, y = map_xy(fix.lat, fix.lon) assert count_near(img, MARKER, 40, (int(x) - 9, int(y) - 9, int(x) + 10, int(y) + 10)) > 20 def test_without_an_orbit_there_is_no_marker(): with_orbit, without = view().render(NOW), view(tle=None).render(NOW) fix = astro.iss_fix(TLE, NOW) x, y = map_xy(fix.lat, fix.lon) box = (int(x) - 9, int(y) - 9, int(x) + 10, int(y) + 10) assert count_near(without, MARKER, 40, box) < count_near(with_orbit, MARKER, 40, box) / 4 def test_stale_tag_only_when_the_orbit_is_old_and_only_in_the_header(): fresh = view().render(NOW) old = view(tle_age=4 * 86400).render(NOW) bbox = ImageChops.difference(fresh, old).getbbox() assert bbox is not None and bbox[3] <= MAP_TOP def test_next_pass_grid_shows_height_direction_brightness_and_clouds(): p = astro.next_visible_pass(TLE, NOW) cells = {label: value for label, value, _ in pass_cells(p, CLOUDS)} assert cells["VÝŠKA"] == "40°" assert cells["SMĚR"] == "V → V" # out of the earth's shadow high in the east, sinking there assert cells["JASNOST"].startswith("−") and cells["JASNOST"].endswith(" mag") and "," in cells["JASNOST"] assert cells["MRAKY"] == "100 %" assert {label: value for label, value, _ in pass_cells(p, ())}["MRAKY"] == "--" def test_time_of_the_next_pass_is_large(): img = view().render(NOW).crop((0, PANEL_TOP, 200, 320)) mask = img.point(lambda v: 255 if v > 200 else 0).convert("L").point(lambda v: 255 if v == 255 else 0) top, bottom = mask.getbbox()[1], mask.getbbox()[3] assert bottom - top >= 50 # 48 px time over the date: readable across the room