S1005403_RisCC/tests/gui/test_main_view_reset.py

101 lines
2.8 KiB
Python

import logging
from unittest.mock import Mock
import pytest
from target_simulator.gui import main_view as mv
def _make_minimal_mainview_with_comm(use_json: bool):
# Bypass __init__ to avoid Tkinter initialization in tests
inst = mv.MainView.__new__(mv.MainView)
# Provide a simple logger
inst.logger = logging.getLogger("test.main_view")
# Mock communicator
comm = Mock()
comm.is_open = True
# Emulate the internal flag used by MainView to decide JSON vs legacy
setattr(comm, "_use_json_protocol", use_json)
comm.send_commands.return_value = True
inst.target_communicator = comm
# Mock simulation hub: immediate empty state so reset returns quickly
hub = Mock()
hub.has_active_real_targets.return_value = False
inst.simulation_hub = hub
return inst, comm
def test_reset_uses_json_when_configured():
inst, comm = _make_minimal_mainview_with_comm(use_json=True)
ok = inst._reset_radar_state()
assert ok is True
# send_commands should be called once with the JSON reset payload
assert comm.send_commands.called
sent = comm.send_commands.call_args[0][0]
assert isinstance(sent, list)
assert sent == ['{"CMD":"reset"}\n']
def test_reset_uses_legacy_when_not_configured():
inst, comm = _make_minimal_mainview_with_comm(use_json=False)
ok = inst._reset_radar_state()
assert ok is True
assert comm.send_commands.called
sent = comm.send_commands.call_args[0][0]
assert isinstance(sent, list)
assert sent == ["mex.t_rows=80\n", "tgtset /-s\n"]
import pytest
from target_simulator.gui.main_view import MainView
from target_simulator.core.models import Target
class DummyCommunicator:
def __init__(self):
self.sent_commands = []
self.is_open = True
def send_commands(self, commands):
self.sent_commands.extend(commands)
return True
def connect(self, cfg):
self.is_open = True
return True
def disconnect(self):
self.is_open = False
@pytest.fixture
def main_view(tmp_path, monkeypatch):
# Create a MainView but avoid opening the Tk mainloop
mv = MainView()
# Replace actual communicators with dummy to avoid I/O
dummy = DummyCommunicator()
mv.target_communicator = dummy
return mv
def test_reset_radar_state_sends_atomic_tgtset(main_view):
mv = main_view
# Ensure that the communicator reports open
assert mv.target_communicator.is_open
# Call the reset function
result = mv._reset_radar_state()
# Expect success
assert result is True
# Verify that two commands were sent: preparatory mex.t_rows=80 then tgtset /-s
assert len(mv.target_communicator.sent_commands) == 2
assert mv.target_communicator.sent_commands[0].strip() == "mex.t_rows=80"
assert mv.target_communicator.sent_commands[1].strip() == "tgtset /-s"