import copy import json from datetime import datetime from pathlib import Path import pytest import sky_data from sky_data import CloudHour, Tle, cloud_at, cloud_story, parse_clouds, parse_tle FIXTURES = Path(__file__).parent / "fixtures" TLE_TEXT = (FIXTURES / "celestrak-iss.tle").read_text() CLOUDS = json.loads((FIXTURES / "open-meteo-clouds.json").read_text()) def clouds_payload(): return copy.deepcopy(CLOUDS) class Response: def __init__(self, text="", payload=None): self.text, self._payload = text, payload def raise_for_status(self): pass def json(self): return self._payload def test_parse_tle_from_celestrak(): tle = parse_tle(TLE_TEXT) assert tle.name == "ISS (ZARYA)" assert tle.line1.startswith("1 25544U") assert tle.line2.startswith("2 25544 ") @pytest.mark.parametrize("text", ["", "Too many requests", "ISS\n1 nonsense\n2 nonsense\n", "No GP data found"]) def test_parse_tle_rejects_anything_else(text): with pytest.raises(ValueError): parse_tle(text) def test_fetch_tle_asks_for_the_iss_only(): calls = [] def fake_get(url, params=None, timeout=None): calls.append((url, params, timeout)) return Response(text=TLE_TEXT) assert isinstance(sky_data.fetch_tle(get=fake_get), Tle) url, params, timeout = calls[0] assert url.startswith("https://celestrak.org/") assert params == {"CATNR": 25544, "FORMAT": "TLE"} assert timeout == 10 def test_parse_clouds(): clouds = parse_clouds(clouds_payload()) assert len(clouds) == 48 assert clouds[0] == CloudHour(datetime(2026, 10, 28, 0, 0), 0) assert clouds[22] == CloudHour(datetime(2026, 10, 28, 22, 0), 82) def test_parse_clouds_keeps_nulls_as_none_and_survives_missing_sections(): p = clouds_payload() p["hourly"]["cloud_cover"][5] = None assert parse_clouds(p)[5].pct is None assert parse_clouds({}) == () def test_fetch_clouds_asks_for_hourly_cloud_cover_in_zlin(): calls = [] def fake_get(url, params=None, timeout=None): calls.append(params) return Response(payload=clouds_payload()) assert len(sky_data.fetch_clouds(get=fake_get)) == 48 params = calls[0] assert (params["latitude"], params["longitude"]) == (49.2265, 17.6627) assert params["hourly"] == "cloud_cover" assert params["timezone"] == "Europe/Prague" def test_cloud_at_matches_the_hour_containing_the_moment(): clouds = parse_clouds(clouds_payload()) assert cloud_at(clouds, datetime(2026, 10, 29, 5, 38, 12, 345678)) == 100 assert cloud_at(clouds, datetime(2026, 10, 28, 20, 0)) == 31 assert cloud_at(clouds, datetime(2026, 11, 5, 20, 0)) is None def test_cloud_story_tonight(): clouds = parse_clouds(clouds_payload()) # clear until 19 h, 31 % at 20 h, 82 % at 22 h assert cloud_story(clouds, datetime(2026, 10, 28, 19, 30)) == "Jasno do 20 h, od 22 h zataženo" def test_cloud_story_other_skies(): def flat(pct): return tuple(CloudHour(datetime(2026, 10, 28, h, 0), pct) for h in range(24)) at = datetime(2026, 10, 28, 8, 15) assert cloud_story(flat(5), at) == "Jasno" assert cloud_story(flat(40), at) == "Polojasno" assert cloud_story(flat(90), at) == "Zataženo" clearing = tuple(CloudHour(datetime(2026, 10, 28, h, 0), 30 if h < 10 else 0) for h in range(24)) assert cloud_story(clearing, at) == "Polojasno" assert cloud_story((), at) is None