mirror of
https://github.com/kuhyx/screen-locker.git
synced 2026-07-04 15:03:15 +02:00
Add scripts/check_file_length.py and a max-file-length pre-commit hook that fails any Python/shell file exceeding 400 lines. Extract UIWidgetsMixin and UIFlowsRelaxedMixin from screen_lock.py and _ui_flows.py respectively, and split 6 oversized test files into part2/part3/part4 siblings. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
286 lines
9.6 KiB
Python
286 lines
9.6 KiB
Python
"""Tests for shutdown schedule adjustment coverage gaps (part 2)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
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 TestApplyEarlierShutdown:
|
|
"""Tests for _apply_earlier_shutdown method."""
|
|
|
|
def test_returns_false_when_no_config(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test returns False when config can't be read."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
with patch.object(locker, "_read_shutdown_config", return_value=None):
|
|
assert locker._apply_earlier_shutdown("2026-03-21") is False
|
|
|
|
def test_returns_false_when_save_state_fails(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test returns False when saving state fails."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
with (
|
|
patch.object(locker, "_read_shutdown_config", return_value=(21, 20, 8)),
|
|
patch.object(locker, "_save_sick_day_state", return_value=False),
|
|
):
|
|
assert locker._apply_earlier_shutdown("2026-03-21") is False
|
|
|
|
def test_success_applies_earlier_hours(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test successful application of earlier shutdown hours."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
with (
|
|
patch.object(locker, "_read_shutdown_config", return_value=(21, 20, 8)),
|
|
patch.object(locker, "_save_sick_day_state", return_value=True),
|
|
patch.object(
|
|
locker, "_write_shutdown_config", return_value=True
|
|
) as mock_write,
|
|
):
|
|
result = locker._apply_earlier_shutdown("2026-03-21")
|
|
assert result is True
|
|
mock_write.assert_called_once_with(20, 19, 8)
|
|
|
|
def test_clamps_to_minimum_18(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test hours are clamped to minimum of 18."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
with (
|
|
patch.object(locker, "_read_shutdown_config", return_value=(18, 18, 8)),
|
|
patch.object(locker, "_save_sick_day_state", return_value=True),
|
|
patch.object(
|
|
locker, "_write_shutdown_config", return_value=True
|
|
) as mock_write,
|
|
):
|
|
locker._apply_earlier_shutdown("2026-03-21")
|
|
mock_write.assert_called_once_with(18, 18, 8)
|
|
|
|
|
|
class TestAdjustShutdownTimeEarlier:
|
|
"""Tests for _adjust_shutdown_time_earlier method."""
|
|
|
|
def test_returns_false_when_sick_mode_used_today(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test returns False when sick mode already used today."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
with (
|
|
patch.object(locker, "_restore_original_config_if_needed"),
|
|
patch.object(locker, "_sick_mode_used_today", return_value=True),
|
|
):
|
|
assert locker._adjust_shutdown_time_earlier() is False
|
|
|
|
def test_success(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test successful adjustment."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
with (
|
|
patch.object(locker, "_restore_original_config_if_needed"),
|
|
patch.object(locker, "_sick_mode_used_today", return_value=False),
|
|
patch.object(locker, "_apply_earlier_shutdown", return_value=True),
|
|
):
|
|
assert locker._adjust_shutdown_time_earlier() is True
|
|
|
|
def test_handles_oserror(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test handles OSError during apply."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
with (
|
|
patch.object(locker, "_restore_original_config_if_needed"),
|
|
patch.object(locker, "_sick_mode_used_today", return_value=False),
|
|
patch.object(
|
|
locker,
|
|
"_apply_earlier_shutdown",
|
|
side_effect=OSError("fail"),
|
|
),
|
|
):
|
|
assert locker._adjust_shutdown_time_earlier() is False
|
|
|
|
def test_handles_value_error(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test handles ValueError during apply."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
with (
|
|
patch.object(locker, "_restore_original_config_if_needed"),
|
|
patch.object(locker, "_sick_mode_used_today", return_value=False),
|
|
patch.object(
|
|
locker,
|
|
"_apply_earlier_shutdown",
|
|
side_effect=ValueError("bad"),
|
|
),
|
|
):
|
|
assert locker._adjust_shutdown_time_earlier() is False
|
|
|
|
|
|
class TestAdjustShutdownTimeLater:
|
|
"""Tests for _adjust_shutdown_time_later method."""
|
|
|
|
def test_returns_false_when_no_config(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test returns False when config is missing."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
with patch.object(locker, "_read_shutdown_config", return_value=None):
|
|
assert locker._adjust_shutdown_time_later() is False
|
|
|
|
def test_success_applies_later_hours(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test successful later adjustment with restore flag."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
with (
|
|
patch.object(locker, "_read_shutdown_config", return_value=(20, 19, 8)),
|
|
patch.object(
|
|
locker, "_write_shutdown_config", return_value=True
|
|
) as mock_write,
|
|
):
|
|
result = locker._adjust_shutdown_time_later()
|
|
assert result is True
|
|
mock_write.assert_called_once_with(22, 21, 8, restore=True)
|
|
|
|
def test_clamps_to_max_23(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test hours are clamped to maximum of 23."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
with (
|
|
patch.object(locker, "_read_shutdown_config", return_value=(22, 23, 8)),
|
|
patch.object(
|
|
locker, "_write_shutdown_config", return_value=True
|
|
) as mock_write,
|
|
):
|
|
locker._adjust_shutdown_time_later()
|
|
mock_write.assert_called_once_with(23, 23, 8, restore=True)
|
|
|
|
def test_handles_oserror(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test handles OSError."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
with patch.object(
|
|
locker,
|
|
"_read_shutdown_config",
|
|
side_effect=OSError("fail"),
|
|
):
|
|
assert locker._adjust_shutdown_time_later() is False
|
|
|
|
|
|
class TestSickModeUsedToday:
|
|
"""Tests for _sick_mode_used_today method."""
|
|
|
|
def test_returns_false_when_no_file(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test returns False when state 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.SICK_DAY_STATE_FILE",
|
|
mock_file,
|
|
):
|
|
assert locker._sick_mode_used_today() is False
|
|
|
|
def test_returns_true_when_used_today(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test returns True when state matches today."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
state_file = tmp_path / "state.json"
|
|
with patch(
|
|
"screen_locker._shutdown.SICK_DAY_STATE_FILE",
|
|
state_file,
|
|
):
|
|
from datetime import datetime, timezone
|
|
|
|
today = datetime.now(tz=timezone.utc).strftime("%Y-%m-%d")
|
|
state_file.write_text(json.dumps({"date": today}))
|
|
assert locker._sick_mode_used_today() is True
|
|
|
|
def test_returns_false_when_different_date(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test returns False when state is from different date."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
state_file = tmp_path / "state.json"
|
|
with patch(
|
|
"screen_locker._shutdown.SICK_DAY_STATE_FILE",
|
|
state_file,
|
|
):
|
|
state_file.write_text(json.dumps({"date": "2020-01-01"}))
|
|
assert locker._sick_mode_used_today() is False
|
|
|
|
def test_returns_false_on_json_error(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test returns False on JSONDecodeError."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
state_file = tmp_path / "state.json"
|
|
with patch(
|
|
"screen_locker._shutdown.SICK_DAY_STATE_FILE",
|
|
state_file,
|
|
):
|
|
state_file.write_text("not json{{{")
|
|
assert locker._sick_mode_used_today() is False
|