Reset switch session on trigger changes and add DP2 force helper

This commit is contained in:
Lago
2026-03-27 15:53:16 +01:00
parent a4837c1172
commit 3f7c5a0677
3 changed files with 129 additions and 19 deletions
+84
View File
@@ -441,3 +441,87 @@ def test_polling_switches_when_alienware_input_is_unreadable() -> None:
assert status["last_switch_result"] == "switched_unverified"
assert status["samsung_session_successful"] is True
assert backend.calls == [(1, "DP1")]
def test_polling_resets_session_when_trigger_changes_without_disconnect() -> None:
service_module = _require_module("app.service")
hardware_module = _require_module("app.hardware")
ddm_module = _require_module("app.ddm")
KvmSwitcherService = getattr(service_module, "KvmSwitcherService", None)
HardwareScan = getattr(hardware_module, "HardwareScan", None)
DDMCommandResult = getattr(ddm_module, "DDMCommandResult", None)
if KvmSwitcherService is None or HardwareScan is None or DDMCommandResult is None:
pytest.skip("Service backend interfaces are not available yet.")
class FakeMonitorBackend:
def __init__(self) -> None:
self.call_count = 0
def scan(self):
self.call_count += 1
if self.call_count == 1:
return HardwareScan(
samsung_present=True,
trigger_input_code=19,
alienware_detected=True,
alienware_input_code=19,
errors=[],
)
if self.call_count == 2:
return HardwareScan(
samsung_present=True,
trigger_input_code=15,
alienware_detected=True,
alienware_input_code=15,
errors=[],
)
if self.call_count == 3:
return HardwareScan(
samsung_present=True,
trigger_input_code=19,
alienware_detected=True,
alienware_input_code=15,
errors=[],
)
return HardwareScan(
samsung_present=True,
trigger_input_code=19,
alienware_detected=True,
alienware_input_code=19,
errors=[],
)
class FakeDDMBackend:
def __init__(self) -> None:
self.calls: list[tuple[int, str]] = []
def is_available(self) -> bool:
return True
def resolve_alienware_slot(self, force: bool = False) -> int | None:
return 1
def invalidate_slot(self) -> None:
return None
def switch_to_port(self, slot: int, port_name: str):
self.calls.append((slot, port_name))
return DDMCommandResult(True, "ok")
config_path = Path(tempfile.mkdtemp()) / "config.json"
backend = FakeDDMBackend()
service = KvmSwitcherService(
config_store=ConfigStore(config_path),
monitor_backend=FakeMonitorBackend(),
ddm_backend=backend,
poll_interval_seconds=0.01,
retry_wait_seconds=0.0,
)
first = service.save_settings(device_port="DP2")
assert first["last_switch_result"] == "noop"
second = service.poll_once()
assert second["last_switch_result"] == "waiting_for_trigger_match"
third = service.poll_once()
assert third["last_switch_result"] == "switched"
assert backend.calls == [(1, "DP2")]