screen-locker/screen_locker/tests/test_shutdown_part3.py
Krzysztof kuhy Rudnicki 275a0862fc
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (flutter, flutter-analyze, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint,flutter-analyze) (push) Waiting to run
Tests / test (push) Waiting to run
Tests / lock-popup-safety (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (0) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (1) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (2) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (3) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (4) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (5) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (6) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (7) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (8) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (9) (push) Waiting to run
Tests / flutter-coverage (push) Blocked by required conditions
Tests / web-coverage (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
shutdown: minutes end to end (*_MINUTES schedule keys, HH:MM helper args, to_hours removed)
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-10-04 19:48:24 +02:00

242 lines
7.5 KiB
Python

"""Tests for shutdown schedule adjustment coverage gaps (part 3)."""
from __future__ import annotations
from datetime import UTC
import json
from typing import TYPE_CHECKING
from unittest.mock import MagicMock, patch
from screen_locker._constants import ADJUST_SHUTDOWN_SCRIPT
from screen_locker.tests.conftest import create_locker
if TYPE_CHECKING:
from pathlib import Path
class TestRestoreOriginalConfigIfNeeded:
"""Tests for _restore_original_config_if_needed method."""
def test_no_state_file_does_nothing(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Test does nothing when no state file exists."""
locker = create_locker(mock_tk, tmp_path)
mock_file = MagicMock()
mock_file.exists.return_value = False
with patch(
"screen_locker._shutdown_sick_state.SICK_DAY_STATE_FILE",
mock_file,
):
locker._restore_original_config_if_needed()
def test_restores_when_state_from_previous_day(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Test restores config when state date differs from today."""
locker = create_locker(mock_tk, tmp_path)
state_file = tmp_path / "state.json"
state_file.write_text(
json.dumps(
{
"date": "2020-01-01",
"original_mon_wed_minutes": 1260,
"original_thu_sun_minutes": 1200,
}
)
)
with (
patch(
"screen_locker._shutdown_sick_state.SICK_DAY_STATE_FILE",
state_file,
),
patch.object(locker, "_write_restored_config") as mock_restore,
):
locker._restore_original_config_if_needed()
mock_restore.assert_called_once_with(1260, 1200, "2020-01-01")
def test_does_not_restore_when_state_from_today(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Test does not restore when state date matches today."""
locker = create_locker(mock_tk, tmp_path)
from datetime import datetime
today = datetime.now(tz=UTC).strftime("%Y-%m-%d")
state_file = tmp_path / "state.json"
state_file.write_text(
json.dumps(
{
"date": today,
"original_mon_wed_minutes": 1260,
"original_thu_sun_minutes": 1200,
}
)
)
with (
patch(
"screen_locker._shutdown_sick_state.SICK_DAY_STATE_FILE",
state_file,
),
patch.object(locker, "_write_restored_config") as mock_restore,
):
locker._restore_original_config_if_needed()
mock_restore.assert_not_called()
def test_returns_when_loaded_state_is_none(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Test returns early when loaded state is None."""
locker = create_locker(mock_tk, tmp_path)
state_file = tmp_path / "state.json"
state_file.write_text(json.dumps({"date": "2020-01-01"}))
with (
patch(
"screen_locker._shutdown_sick_state.SICK_DAY_STATE_FILE",
state_file,
),
patch.object(locker, "_write_restored_config") as mock_restore,
):
locker._restore_original_config_if_needed()
mock_restore.assert_not_called()
def test_handles_oserror(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Test handles OSError when loading state."""
locker = create_locker(mock_tk, tmp_path)
mock_file = MagicMock()
mock_file.exists.return_value = True
mock_file.open.side_effect = OSError("fail")
with patch(
"screen_locker._shutdown_sick_state.SICK_DAY_STATE_FILE",
mock_file,
):
locker._restore_original_config_if_needed()
def test_handles_json_decode_error(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Test handles JSONDecodeError when loading state."""
locker = create_locker(mock_tk, tmp_path)
state_file = tmp_path / "state.json"
state_file.write_text("not valid json{{{")
with patch(
"screen_locker._shutdown_sick_state.SICK_DAY_STATE_FILE",
state_file,
):
locker._restore_original_config_if_needed()
class TestReadShutdownConfig:
"""Tests for _read_shutdown_config method."""
def test_returns_none_when_file_missing(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Test returns None when config file doesn't exist."""
locker = create_locker(mock_tk, tmp_path)
mock_file = MagicMock()
mock_file.exists.return_value = False
with patch(
"screen_locker._shutdown.SHUTDOWN_CONFIG_FILE",
mock_file,
):
assert locker._read_shutdown_config() is None
def test_reads_valid_config(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Test reads all three config values from file."""
locker = create_locker(mock_tk, tmp_path)
config_file = tmp_path / "shutdown.conf"
config_file.write_text(
"MON_WED_MINUTES=1260\nTHU_SUN_MINUTES=1200\nMORNING_END_MINUTES=480\n"
)
with patch(
"screen_locker._shutdown.SHUTDOWN_CONFIG_FILE",
config_file,
):
result = locker._read_shutdown_config()
assert result == (1260, 1200, 480)
def test_returns_none_when_values_missing(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Test returns None when config has missing keys."""
locker = create_locker(mock_tk, tmp_path)
config_file = tmp_path / "shutdown.conf"
config_file.write_text("MON_WED_HOUR=21\n")
with patch(
"screen_locker._shutdown.SHUTDOWN_CONFIG_FILE",
config_file,
):
result = locker._read_shutdown_config()
assert result is None
class TestBuildShutdownCmd:
"""Tests for _build_shutdown_cmd method."""
def test_without_restore(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Test command without restore flag."""
locker = create_locker(mock_tk, tmp_path)
cmd = locker._build_shutdown_cmd(1260, 1200, 480, restore=False)
assert cmd == [
"/usr/bin/sudo",
str(ADJUST_SHUTDOWN_SCRIPT),
"21:00",
"20:00",
"08:00",
]
def test_with_restore(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Test command with restore flag."""
locker = create_locker(mock_tk, tmp_path)
cmd = locker._build_shutdown_cmd(1260, 1200, 480, restore=True)
assert cmd == [
"/usr/bin/sudo",
str(ADJUST_SHUTDOWN_SCRIPT),
"--restore",
"21:00",
"20:00",
"08:00",
]