mirror of
https://github.com/kuhyx/screen-locker.git
synced 2026-11-10 20:27:13 +01:00
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
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
235 lines
7.7 KiB
Python
235 lines
7.7 KiB
Python
"""Tests for sick-day state save/load and config restoration."""
|
|
|
|
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 TestSaveSickDayState:
|
|
"""Tests for _save_sick_day_state method."""
|
|
|
|
def test_saves_state_successfully(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test saves state file with correct content."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
state_file = tmp_path / "state.json"
|
|
with patch(
|
|
"screen_locker._shutdown_sick_state.SICK_DAY_STATE_FILE",
|
|
state_file,
|
|
):
|
|
result = locker._save_sick_day_state("2026-03-21", 1260, 1200)
|
|
assert result is True
|
|
data = json.loads(state_file.read_text())
|
|
assert data["date"] == "2026-03-21"
|
|
assert data["original_mon_wed_minutes"] == 1260
|
|
assert data["original_thu_sun_minutes"] == 1200
|
|
|
|
def test_returns_false_on_oserror(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test returns False when write fails."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
mock_path = MagicMock()
|
|
mock_path.open.side_effect = OSError("permission denied")
|
|
with patch(
|
|
"screen_locker._shutdown_sick_state.SICK_DAY_STATE_FILE",
|
|
mock_path,
|
|
):
|
|
result = locker._save_sick_day_state("2026-03-21", 1260, 1200)
|
|
assert result is False
|
|
|
|
|
|
class TestLoadSickDayState:
|
|
"""Tests for _load_sick_day_state method."""
|
|
|
|
def test_loads_valid_state(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test loads state with all fields present."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
state_file = tmp_path / "state.json"
|
|
state_file.write_text(
|
|
json.dumps(
|
|
{
|
|
"date": "2026-03-20",
|
|
"original_mon_wed_minutes": 1260,
|
|
"original_thu_sun_minutes": 1200,
|
|
}
|
|
)
|
|
)
|
|
with patch(
|
|
"screen_locker._shutdown_sick_state.SICK_DAY_STATE_FILE",
|
|
state_file,
|
|
):
|
|
result = locker._load_sick_day_state()
|
|
assert result == ("2026-03-20", 1260, 1200)
|
|
|
|
def test_returns_none_when_fields_missing(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test returns None when required fields are missing."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
state_file = tmp_path / "state.json"
|
|
state_file.write_text(json.dumps({"date": "2026-03-20"}))
|
|
with patch(
|
|
"screen_locker._shutdown_sick_state.SICK_DAY_STATE_FILE",
|
|
state_file,
|
|
):
|
|
result = locker._load_sick_day_state()
|
|
assert result is None
|
|
|
|
|
|
class TestWriteRestoredConfig:
|
|
"""Tests for _write_restored_config method."""
|
|
|
|
def test_restores_config_and_removes_state(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test restores config values and deletes state file."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
state_file = tmp_path / "state.json"
|
|
state_file.write_text("{}")
|
|
with (
|
|
patch.object(
|
|
locker, "_read_shutdown_config", return_value=(1200, 1140, 480)
|
|
),
|
|
patch.object(
|
|
locker, "_write_shutdown_config", return_value=True
|
|
) as mock_write,
|
|
patch(
|
|
"screen_locker._shutdown_sick_state.SICK_DAY_STATE_FILE",
|
|
state_file,
|
|
),
|
|
):
|
|
locker._write_restored_config(1260, 1200, "2026-03-20")
|
|
mock_write.assert_called_once_with(1260, 1200, 480, restore=True)
|
|
assert not state_file.exists()
|
|
|
|
def test_still_removes_state_when_config_read_fails(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test removes state file even when config read returns None."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
state_file = tmp_path / "state.json"
|
|
state_file.write_text("{}")
|
|
with (
|
|
patch.object(locker, "_read_shutdown_config", return_value=None),
|
|
patch(
|
|
"screen_locker._shutdown_sick_state.SICK_DAY_STATE_FILE",
|
|
state_file,
|
|
),
|
|
):
|
|
locker._write_restored_config(1260, 1200, "2026-03-20")
|
|
assert not state_file.exists()
|
|
|
|
|
|
class TestAdjustShutdownTimeBy:
|
|
"""Tests for _adjust_shutdown_time_by method (extra-workout bonus)."""
|
|
|
|
def test_adjusts_time_successfully(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Normal path: reads config, increments both hours, writes back."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
object.__setattr__(
|
|
locker, "_read_shutdown_config", MagicMock(return_value=(1260, 1260, 300))
|
|
)
|
|
object.__setattr__(
|
|
locker, "_write_shutdown_config", MagicMock(return_value=True)
|
|
)
|
|
assert locker._adjust_shutdown_time_by(60) is True
|
|
locker._write_shutdown_config.assert_called_once_with(
|
|
1320, 1320, 300, restore=True
|
|
)
|
|
|
|
def test_caps_at_midnight(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Times are capped at 1440 minutes (midnight-safe shutdown)."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
object.__setattr__(
|
|
locker, "_read_shutdown_config", MagicMock(return_value=(1380, 1380, 300))
|
|
)
|
|
object.__setattr__(
|
|
locker, "_write_shutdown_config", MagicMock(return_value=True)
|
|
)
|
|
locker._adjust_shutdown_time_by(120)
|
|
locker._write_shutdown_config.assert_called_once_with(
|
|
1440, 1440, 300, restore=True
|
|
)
|
|
|
|
def test_returns_false_when_config_is_none(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""_read_shutdown_config returns None → return False immediately."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
object.__setattr__(
|
|
locker, "_read_shutdown_config", MagicMock(return_value=None)
|
|
)
|
|
assert locker._adjust_shutdown_time_by(1) is False
|
|
|
|
def test_returns_false_on_oserror(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""OSError during read is caught; returns False."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
object.__setattr__(
|
|
locker,
|
|
"_read_shutdown_config",
|
|
MagicMock(side_effect=OSError("permission denied")),
|
|
)
|
|
assert locker._adjust_shutdown_time_by(1) is False
|
|
|
|
def test_returns_false_on_value_error(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""ValueError during processing is caught; returns False."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
object.__setattr__(
|
|
locker,
|
|
"_read_shutdown_config",
|
|
MagicMock(side_effect=ValueError("bad value")),
|
|
)
|
|
assert locker._adjust_shutdown_time_by(1) is False
|