screen-locker/screen_locker/tests/test_phone_verification_part2.py
Krzysztof kuhy Rudnicki bdc405c8c9 Split _shutdown, _phone_verification and _status_data
Six new modules, all composed or re-exported so no caller changes:

  _shutdown.py (384)          -> _shutdown_sick_state.py  SickDayStateMixin
                                 _wake_alarm.py           WakeAlarmMixin
  _phone_verification.py (380)-> _adb_transport.py        AdbTransportMixin
                                 _http_workout_fetch.py   HttpWorkoutFetchMixin
  _status_data.py (370)       -> _status_types.py         the 7 dataclasses
                                 _status_projection.py    shutdown projection
                                                          + format_summary_line

Moving a name means moving where it is *read*, so 30-odd mock patch targets
move with it. Most importantly conftest.py's autouse _block_real_network
fixture: ruff correctly dropped the now-unused `socket` import from
_phone_verification, which silently removed the guard's target and would have
let subnet probes reach the real network. It now blocks socket in both modules
that actually own the probes, and both targets are asserted to exist.

976 tests pass at 100% branch coverage; pylint 8.70 -> 8.71; `screen_lock
--status` renders the real week unchanged. File-length gate: 41 -> 35.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EveB813BQ8abAP68VSqZ5Y
2026-08-15 20:11:28 +02:00

167 lines
5.4 KiB
Python

"""Tests for phone verification coverage gaps (part 2)."""
from __future__ import annotations
from typing import TYPE_CHECKING
from unittest.mock import MagicMock, patch
from screen_locker.tests.conftest import create_locker
if TYPE_CHECKING:
from pathlib import Path
class TestGetWirelessSerial:
"""Tests for _get_wireless_serial method."""
def test_returns_wireless_serial(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Test returns ip:port serial for a wireless device."""
locker = create_locker(mock_tk, tmp_path)
output = "List of devices attached\n192.168.1.42:5555\tdevice\n"
with patch.object(locker, "_run_adb", return_value=(True, output)):
result = locker._get_wireless_serial()
assert result == "192.168.1.42:5555"
def test_returns_none_when_adb_fails(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Test returns None when adb devices fails."""
locker = create_locker(mock_tk, tmp_path)
with patch.object(locker, "_run_adb", return_value=(False, "")):
result = locker._get_wireless_serial()
assert result is None
def test_returns_none_when_no_wireless_device(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Test returns None when only USB devices are connected."""
locker = create_locker(mock_tk, tmp_path)
output = "List of devices attached\nABC123DEF456\tdevice\n"
with patch.object(locker, "_run_adb", return_value=(True, output)):
result = locker._get_wireless_serial()
assert result is None
def test_skips_offline_wireless_device(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Test skips offline wireless devices."""
locker = create_locker(mock_tk, tmp_path)
output = "List of devices attached\n192.168.1.42:5555\toffline\n"
with patch.object(locker, "_run_adb", return_value=(True, output)):
result = locker._get_wireless_serial()
assert result is None
class TestTryAdbConnect:
"""Tests for _try_adb_connect method."""
def test_successful_connect(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Test successful ADB connect."""
locker = create_locker(mock_tk, tmp_path)
with patch.object(
locker, "_run_adb", return_value=(True, "connected to 192.168.1.42:5555")
):
result = locker._try_adb_connect("192.168.1.42:5555")
assert result is True
def test_failed_connect_unable(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Test connect failure with 'unable' in output."""
locker = create_locker(mock_tk, tmp_path)
with patch.object(
locker, "_run_adb", return_value=(False, "unable to connect")
):
result = locker._try_adb_connect("192.168.1.42:5555")
assert result is False
def test_failed_connect_with_failed(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Test connect failure with 'failed' in output."""
locker = create_locker(mock_tk, tmp_path)
with patch.object(
locker,
"_run_adb",
return_value=(False, "connected but failed to authenticate"),
):
result = locker._try_adb_connect("192.168.1.42:5555")
assert result is False
def test_no_connected_in_output(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Test connect failure when 'connected' not in output."""
locker = create_locker(mock_tk, tmp_path)
with patch.object(
locker, "_run_adb", return_value=(False, "some random output")
):
result = locker._try_adb_connect("192.168.1.42:5555")
assert result is False
class TestGetLocalSubnetPrefix:
"""Tests for _get_local_subnet_prefix method."""
def test_returns_prefix(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Test returns first three octets of local IP."""
locker = create_locker(mock_tk, tmp_path)
mock_sock = MagicMock()
mock_sock.getsockname.return_value = ("192.168.1.100", 12345)
mock_sock.__enter__ = MagicMock(return_value=mock_sock)
mock_sock.__exit__ = MagicMock(return_value=False)
with patch(
"screen_locker._adb_transport.socket.socket",
return_value=mock_sock,
):
result = locker._get_local_subnet_prefix()
assert result == "192.168.1"
def test_returns_none_on_oserror(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Test returns None when socket raises OSError."""
locker = create_locker(mock_tk, tmp_path)
with patch(
"screen_locker._adb_transport.socket.socket",
side_effect=OSError("no network"),
):
result = locker._get_local_subnet_prefix()
assert result is None