"""Events through the one door: same shape both series, the held line, ring safety.""" from __future__ import annotations import dataclasses import os import sys import pytest sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from device_access_fakes import FakeMicromateClient, FakeMiniMateClient # noqa: E402 from sfm.device_access.access import DeviceAccess # noqa: E402 from sfm.device_access.errors import BadRequest, CallHomeInProgress # noqa: E402 from sfm.device_access.models import UnitAddress # noqa: E402 from sfm.device_access.s4 import RingDetected # noqa: E402 ADDR = UnitAddress(host="10.0.0.5", tcp_port=9034, port=None, baud=None) def access(s3=(), s4=()): s3q, s4q = list(s3), list(s4) return DeviceAccess(s3_client_factory=lambda a, **kw: s3q.pop(0), s4_client_factory=lambda a, **kw: s4q.pop(0), start_reaper=False) def test_both_series_list_events_in_the_same_shape(): s3 = access(s3=[FakeMiniMateClient()]).list_events(ADDR, "BE18193") s4 = access(s4=[FakeMicromateClient()]).list_events(ADDR, "UM20147") f3 = [set(dataclasses.asdict(e)) for e in s3.events] f4 = [set(dataclasses.asdict(e)) for e in s4.events] assert all(f == f3[0] for f in f3 + f4) def test_no_shared_field_is_empty_for_either_series(): # A field one series cannot fill belongs under `detail`, not at the top level. # Except Series 3's record_type: its 0C cannot tell a histogram yet, and null beats a wrong type. allowed = {3: {"record_type"}, 4: set()} for out in (access(s3=[FakeMiniMateClient()]).list_events(ADDR, "BE18193"), access(s4=[FakeMicromateClient()]).list_events(ADDR, "UM20147")): for e in out.events: empty = {k for k, v in dataclasses.asdict(e).items() if v is None} assert empty == allowed[out.series], (out.series, empty) def test_a_micromate_list_and_download_share_one_held_line(): fake = FakeMicromateClient() da = access(s4=[fake]) listed = da.list_events(ADDR, "UM20147") dl = da.download_event(ADDR, "UM20147", listed.events[0].event_id) assert fake.opened == 1 and dl.idf_bytes assert da.held_serial(ADDR) == "UM20147" def test_an_id_from_another_unit_is_refused_before_any_io(): fake = FakeMicromateClient() with pytest.raises(BadRequest): access(s4=[fake]).download_event(ADDR, "UM20147", "UM12947:055d4a81:20250531135901") assert fake.opened == 0 def test_a_minimate_download_goes_through_the_series3_adapter(): dl = access(s3=[FakeMiniMateClient()]).download_event(ADDR, "BE18193", "BE18193:01110000:-") assert dl.series == 3 and dl.a5_frames and all(f.sub == 0xA5 for f in dl.a5_frames) def test_a_ring_mid_download_releases_the_line_and_downloads_nothing(): fake = FakeMicromateClient() def ring(*a, **kw): raise RingDetected("RADIO RING") fake.fetch_event = ring da = access(s4=[fake]) with pytest.raises(CallHomeInProgress): da.download_event(ADDR, "UM20147", "UM20147:055d4a81:20250531135901") assert fake.closed == 1 and da.held_serial(ADDR) is None def test_a_library_error_mid_download_hangs_up(): # Only a DeviceError verdict keeps the line; anything the library raises means # the link is suspect. A fresh DeviceAccess, so the held-line retry cannot apply. from micromate.protocol import TimeoutError as MicromateTimeout from sfm.device_access.errors import NoAnswer fake = FakeMicromateClient() def quiet(*a, **kw): raise MicromateTimeout("quiet") fake.fetch_event = quiet da = access(s4=[fake]) with pytest.raises(NoAnswer): da.download_event(ADDR, "UM20147", "UM20147:055d4a81:20250531135901") assert fake.closed == 1 and da.held_serial(ADDR) is None def test_a_stale_or_missing_event_keeps_the_line(): # The operation's own verdict (404/409) says nothing is wrong with the link; # hanging up would cost a call-up for no reason. from sfm.device_access.errors import EventNotFound, StaleEvent fake = FakeMicromateClient() da = access(s4=[fake]) with pytest.raises(StaleEvent): da.download_event(ADDR, "UM20147", "UM20147:055d4a81:20240101000000") with pytest.raises(EventNotFound): da.download_event(ADDR, "UM20147", "UM20147:deadbeef:-") assert fake.opened == 1 and fake.closed == 0 assert da.held_serial(ADDR) == "UM20147"